|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Iframely\Embed; |
| 4 | + |
| 5 | +use Iframely\Options; |
| 6 | +use Iframely\Utils; |
| 7 | + |
| 8 | +class Amp |
| 9 | +{ |
| 10 | + public static function run(): void |
| 11 | + { |
| 12 | + # Make compatible with Automatic AMP-WP plugin: https://github.com/Automattic/amp-wp |
| 13 | + add_filter('oembed_fetch_url', [self::class, 'maybe_add_iframe_amp'], 10, 3); |
| 14 | + add_filter('embed_oembed_html', [self::class, 'iframely_filter_oembed_result'], 10, 3); |
| 15 | + add_filter('amp_content_embed_handlers', [self::class, 'maybe_disable_default_embed_handlers'], 10, 2); |
| 16 | + } |
| 17 | + |
| 18 | + public static function is_iframely_amp($args): bool |
| 19 | + { |
| 20 | + return |
| 21 | + (is_array($args) && array_key_exists('iframely', $args) && $args['iframely'] === 'amp') |
| 22 | + || (is_string($args) && Utils::stringContains($args, 'iframely=amp')) |
| 23 | + || (function_exists('is_amp_endpoint') && is_amp_endpoint()); |
| 24 | + } |
| 25 | + |
| 26 | + public static function maybe_add_iframe_amp($provider, $args, $url) |
| 27 | + { |
| 28 | + if (self::is_iframely_amp($args) && Utils::stringContains($provider, 'iframe.ly')) { |
| 29 | + $provider = add_query_arg('amp', '1', $provider); |
| 30 | + } |
| 31 | + return $provider; |
| 32 | + } |
| 33 | + |
| 34 | + public static function iframely_filter_oembed_result($html, $url, $args) |
| 35 | + { |
| 36 | + if (Utils::stringContains($html, '<amp-iframe')) { // covers "amp-iframely" |
| 37 | + // Avoid corrupted amp-iframe overflow div as a result of wpautop |
| 38 | + remove_filter('the_content', 'wpautop'); |
| 39 | + // Restore wpautop if it was disabled |
| 40 | + add_filter('the_content', [self::class, 'iframely_autop_on_amp'], 1000); |
| 41 | + } |
| 42 | + |
| 43 | + return $html; |
| 44 | + } |
| 45 | + |
| 46 | + public static function iframely_autop_on_amp($content) |
| 47 | + { |
| 48 | + // Logic is taken from wpautop itself re <pre> |
| 49 | + if (Utils::stringContains($content, '<amp-iframe')) { |
| 50 | + $chunks = explode('</amp-iframe>', $content); |
| 51 | + $content = ''; |
| 52 | + |
| 53 | + foreach ($chunks as $chunk) { |
| 54 | + $start = strpos($chunk, '<amp-iframe'); |
| 55 | + // Malformed html? |
| 56 | + if ($start === false) { |
| 57 | + $content .= $chunk; |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + $iframe = substr($chunk, $start) . '</amp-iframe>'; |
| 62 | + $p = wpautop(substr($chunk, 0, $start)); |
| 63 | + |
| 64 | + $content .= $p . $iframe; |
| 65 | + } |
| 66 | + } else { |
| 67 | + $content = wpautop($content); |
| 68 | + } |
| 69 | + |
| 70 | + return $content; |
| 71 | + } |
| 72 | + |
| 73 | + public static function maybe_disable_default_embed_handlers($embed_handler_classes) |
| 74 | + { |
| 75 | + //return ! get_site_option( 'iframely_only_shortcode' ) ? array() : $embed_handler_classes; |
| 76 | + if (!class_exists('Jetpack_AMP_Support') && Options::isBuiltinsReplaced()) { |
| 77 | + return []; |
| 78 | + } |
| 79 | + return $embed_handler_classes; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
0 commit comments