Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Iframe_Embed
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 5
240
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
42
 is_embedding_in_iframe
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
 disable_autoplay
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 noindex
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 base_target_blank
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Tweak a preview when rendered in an iframe.
4 * This is used when rendering iFrames in the Calypso app.
5 *
6 * This file is shared between WordPress.com and Jetpack.
7 * The canonical source is Jetpack and no WordPress.com-specific code should exist in this file.
8 *
9 * @package automattic/jetpack
10 */
11
12/**
13 * Tweak a preview when rendered in an iframe.
14 */
15class Jetpack_Iframe_Embed {
16    /**
17     * Initialize class.
18     */
19    public static function init() {
20        if ( ! self::is_embedding_in_iframe() ) {
21            return;
22        }
23
24        // Disable the admin bar.
25        if ( ! defined( 'IFRAME_REQUEST' ) ) {
26            define( 'IFRAME_REQUEST', true );
27        }
28
29        // Prevent canonical redirects.
30        remove_filter( 'template_redirect', 'redirect_canonical' );
31
32        add_action( 'wp_head', array( 'Jetpack_Iframe_Embed', 'noindex' ), 1 );
33        add_action( 'wp_head', array( 'Jetpack_Iframe_Embed', 'base_target_blank' ), 1 );
34
35        add_filter( 'shortcode_atts_video', array( 'Jetpack_Iframe_Embed', 'disable_autoplay' ) );
36        add_filter( 'shortcode_atts_audio', array( 'Jetpack_Iframe_Embed', 'disable_autoplay' ) );
37
38        $ver = sprintf( '%s-%s', gmdate( 'oW' ), defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '' );
39        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
40            wp_enqueue_script(
41                'jetpack-iframe-embed',
42                '/wp-content/mu-plugins/jetpack-iframe-embed/jetpack-iframe-embed.js',
43                array( 'jquery' ),
44                $ver,
45                false
46            );
47        } else {
48            wp_enqueue_script(
49                'jetpack-iframe-embed',
50                '//s0.wp.com/wp-content/mu-plugins/jetpack-iframe-embed/jetpack-iframe-embed.js',
51                array( 'jquery' ),
52                $ver,
53                false
54            );
55        }
56        wp_localize_script( 'jetpack-iframe-embed', '_previewSite', array( 'siteURL' => get_site_url() ) );
57    }
58
59    /**
60     * Check that we are in an iFrame.
61     *
62     * @return bool
63     */
64    private static function is_embedding_in_iframe() {
65        return (
66            // phpcs:disable WordPress.Security.NonceVerification.Recommended -- No nonce needed, we're only checking for a specific screen view.
67            isset( $_GET['iframe'] ) && 'true' === $_GET['iframe']
68            && (
69                isset( $_GET['preview'] ) && 'true' === $_GET['preview']
70                || isset( $_GET['theme_preview'] ) && 'true' === $_GET['theme_preview']
71            )
72            // phpcs:enable WordPress.Security.NonceVerification.Recommended
73        );
74    }
75
76    /**
77     * Disable `autoplay` shortcode attribute in context of an iframe
78     * Added via `shortcode_atts_video` & `shortcode_atts_audio` in `init`
79     *
80     * @param  array $atts The output array of shortcode attributes.
81     *
82     * @return array       The output array of shortcode attributes.
83     */
84    public static function disable_autoplay( $atts ) {
85        return array_merge( $atts, array( 'autoplay' => false ) );
86    }
87
88    /**
89     * We don't want search engines to index iframe previews
90     * Added via `wp_head` action in `init`
91     */
92    public static function noindex() {
93        echo '<meta name="robots" content="noindex,nofollow" />';
94    }
95
96    /**
97     * Make sure all links and forms open in a new window by default
98     * (unless overridden on client-side by JS)
99     * Added via `wp_head` action in `init`
100     */
101    public static function base_target_blank() {
102        echo '<base target="_blank" />';
103    }
104}