|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * AMP-compatibility for this plugin. |
| 4 | + * |
| 5 | + * @package pym-embeds |
| 6 | + */ |
| 7 | + |
| 8 | +namespace INN\PymEmbeds\AMP; |
| 9 | + |
| 10 | +/** |
| 11 | + * Handle pym.js blocks and raw HTML blocks on AMP pages. |
| 12 | + * |
| 13 | + * @since 1.3.2.3 |
| 14 | + * @param string $output HTML block output. |
| 15 | + * @param array $block Block attributes and information. |
| 16 | + * @return string HTML block output. |
| 17 | + */ |
| 18 | +function convert_block_to_ampiframe( $output, $block ) { |
| 19 | + if ( ! is_amp() ) { |
| 20 | + return $output; |
| 21 | + } |
| 22 | + |
| 23 | + if ( 'core/html' === $block['blockName'] ) { |
| 24 | + $pym_src_regex = '~data-pym-src=[\'"]([^\'"]*)~'; |
| 25 | + $is_match = preg_match( $pym_src_regex, $block['innerHTML'], $matches ); |
| 26 | + if ( ! $is_match ) { |
| 27 | + return $output; |
| 28 | + } |
| 29 | + |
| 30 | + $pym_src = $matches[1]; |
| 31 | + return get_pym_ampiframe( $pym_src ); |
| 32 | + } elseif ( 'pym-shortcode/pym' === $block['blockName'] ) { |
| 33 | + $pym_src = $block['attrs']['src']; |
| 34 | + return get_pym_ampiframe( $pym_src, $block['attrs'] ); |
| 35 | + } |
| 36 | + |
| 37 | + return $output; |
| 38 | +} |
| 39 | +add_action( 'render_block', __NAMESPACE__ . '\convert_block_to_ampiframe', 10, 2 ); |
| 40 | + |
| 41 | +/** |
| 42 | + * Handle pym.js shortcode on AMP pages. |
| 43 | + * |
| 44 | + * @since 1.3.2.3 |
| 45 | + * @param string $output HTML shortcode output. |
| 46 | + * @param string $tag Shortcode tag. |
| 47 | + * @param array $attributes Shortcode attributes. |
| 48 | + * @return string HTML shortcode output. |
| 49 | + */ |
| 50 | +function convert_shortcode_to_ampiframe( $output, $tag, $attributes ) { |
| 51 | + if ( ! is_amp() || 'pym' !== $tag ) { |
| 52 | + return $output; |
| 53 | + } |
| 54 | + |
| 55 | + if ( empty( $attributes['src'] ) ) { |
| 56 | + return $output; |
| 57 | + } |
| 58 | + |
| 59 | + return get_pym_ampiframe( $attributes['src'], $attributes ); |
| 60 | +} |
| 61 | +add_action( 'do_shortcode_tag', __NAMESPACE__ . '\convert_shortcode_to_ampiframe', 10, 3 ); |
| 62 | + |
| 63 | +/** |
| 64 | + * Build an amp-iframe out of pym.js iframe source. This is a pretty solid solution until native pym.js AMP compatibility. |
| 65 | + * |
| 66 | + * @since 1.3.2.3 |
| 67 | + * @see https://github.com/ampproject/amphtml/issues/22714 |
| 68 | + * @param string $src iframe src. |
| 69 | + * @return string AMP-iframe HTML. |
| 70 | + */ |
| 71 | +function get_pym_ampiframe( $src, $atts = array() ) { |
| 72 | + $src_domain_parts = parse_url( $src ); |
| 73 | + $site_domain_parts = parse_url( get_site_url() ); |
| 74 | + |
| 75 | + if ( ! $src_domain_parts ) { |
| 76 | + return ''; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * Filter pym_shortcode_default_class allows setting the default class on embeds. |
| 82 | + * |
| 83 | + * @param String $default |
| 84 | + * @return String the default class name |
| 85 | + */ |
| 86 | + $default_class = apply_filters( 'pym_shortcode_default_class', 'pym' ); |
| 87 | + $shortcode_class = empty( $atts['class'] ) ? '' : esc_attr( $atts['class'] ); |
| 88 | + $gutenberg_class = empty( $atts['className'] ) ? '' : esc_attr( $atts['className'] ); |
| 89 | + $align = empty( $atts['align'] ) ? '' : 'align' . esc_attr( $atts['align'] ); |
| 90 | + $actual_classes = implode( ' ', array( |
| 91 | + $default_class, |
| 92 | + $shortcode_class, |
| 93 | + $gutenberg_class, |
| 94 | + $align, |
| 95 | + ) ); |
| 96 | + $id = empty( $atts['id'] ) ? '' : esc_attr( $atts['id'] ); |
| 97 | + |
| 98 | + $sandbox = 'allow-scripts'; |
| 99 | + if ( strcasecmp( $src_domain_parts['host'], $site_domain_parts['host'] ) ) { |
| 100 | + $sandbox .= ' allow-same-origin'; |
| 101 | + } |
| 102 | + |
| 103 | + ob_start(); |
| 104 | + ?> |
| 105 | + <div class='<?php echo esc_attr( $actual_classes ); ?>' id='<?php echo esc_attr( $id ); ?>'> |
| 106 | + <amp-iframe |
| 107 | + src='<?php echo esc_url( $src ); ?>' |
| 108 | + layout='fixed-height' |
| 109 | + height='200' |
| 110 | + sandbox='<?php echo esc_attr( $sandbox ); ?>' |
| 111 | + frameborder='0' |
| 112 | + resizable |
| 113 | + > |
| 114 | + <div placeholder><?php esc_html_e( 'Interactive graphic', 'pym-embeds' ); ?></div> |
| 115 | + <div |
| 116 | + overflow |
| 117 | + tabindex=0 |
| 118 | + aria-label='<?php esc_attr_e( 'Load interactive graphic', 'pym-embeds' ); ?>' |
| 119 | + style='padding: .5em; background:rgba(0,0,0,.7); color:#FFF; font-weight:bold;' |
| 120 | + > |
| 121 | + <?php esc_html_e( 'Load interactive graphic', 'pym-embeds' ); ?> |
| 122 | + </div> |
| 123 | + </amp-iframe> |
| 124 | + </div> |
| 125 | + <?php |
| 126 | + |
| 127 | + return ob_get_clean(); |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Check whether the current page is an AMP page. |
| 132 | + * |
| 133 | + * @since 1.3.2.3 |
| 134 | + * @return bool True if AMP page. |
| 135 | + */ |
| 136 | +function is_amp() { |
| 137 | + return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint(); |
| 138 | +} |
0 commit comments