Completed
Push — add/7777-ga-anonymize-ip ( 94dfd5 )
by
unknown
10:21
created

Jetpack_Iframe_Embed   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 27 6
A is_embedding_in_iframe() 0 8 3
A has_iframe_get_param() 0 3 2
A has_preview_get_param() 0 3 2
A has_preview_theme_preview_param() 0 3 2
A disable_autoplay() 0 3 1
A noindex() 0 3 1
A base_target_blank() 0 3 1
1
<?php
2
/**
3
 * Tweak the preview when rendered in an iframe
4
 */
5
6
class Jetpack_Iframe_Embed {
7
	static function init() {
8
		if ( ! self::is_embedding_in_iframe() ) {
9
			return;
10
		}
11
12
		// Disable the admin bar
13
		if ( ! defined( 'IFRAME_REQUEST' ) ) {
14
			define( 'IFRAME_REQUEST', true );
15
		}
16
17
		// Prevent canonical redirects
18
		remove_filter( 'template_redirect', 'redirect_canonical' );
19
20
		add_action( 'wp_head', array( 'Jetpack_Iframe_Embed', 'noindex' ), 1 );
21
		add_action( 'wp_head', array( 'Jetpack_Iframe_Embed', 'base_target_blank' ), 1 );
22
23
		add_filter( 'shortcode_atts_video', array( 'Jetpack_Iframe_Embed', 'disable_autoplay' ) );
24
		add_filter( 'shortcode_atts_audio', array( 'Jetpack_Iframe_Embed', 'disable_autoplay' ) );
25
26
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
27
			wp_enqueue_script( 'jetpack-iframe-embed', WPMU_PLUGIN_URL . '/jetpack-iframe-embed/jetpack-iframe-embed.js', array( 'jquery' ) );
28
		} else {
29
			$ver = sprintf( '%s-%s', gmdate( 'oW' ), defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '' );
30
			wp_enqueue_script( 'jetpack-iframe-embed', '//s0.wp.com/wp-content/mu-plugins/jetpack-iframe-embed/jetpack-iframe-embed.js', array( 'jquery' ), $ver );
31
		}
32
		wp_localize_script( 'jetpack-iframe-embed', '_previewSite', array( 'siteURL' => get_site_url() ) );
33
	}
34
35
	static function is_embedding_in_iframe() {
36
		return (
37
			self::has_iframe_get_param() && (
38
				self::has_preview_get_param() ||
39
				self::has_preview_theme_preview_param()
40
			)
41
		);
42
	}
43
44
	private static function has_iframe_get_param() {
45
		return isset( $_GET['iframe'] ) && $_GET['iframe'] === 'true';
46
	}
47
48
	private static function has_preview_get_param() {
49
		return isset( $_GET['preview'] ) && $_GET['preview'] === 'true';
50
	}
51
52
	private static function has_preview_theme_preview_param() {
53
		return isset( $_GET['theme_preview'] ) && $_GET['theme_preview'] === 'true';
54
	}
55
56
	/**
57
	 * Disable `autoplay` shortcode attribute in context of an iframe
58
	 * Added via `shortcode_atts_video` & `shortcode_atts_audio` in `init`
59
	 *
60
	 * @param  array $atts The output array of shortcode attributes.
61
	 *
62
	 * @return array       The output array of shortcode attributes.
63
	 */
64
	static function disable_autoplay( $atts ) {
65
		return array_merge( $atts, array( 'autoplay' => false ) );
66
	}
67
68
	/**
69
	 * We don't want search engines to index iframe previews
70
	 * Added via `wp_head` action in `init`
71
	 */
72
	static function noindex() {
73
		echo '<meta name="robots" content="noindex,nofollow" />';
74
	}
75
76
	/**
77
	 * Make sure all links and forms open in a new window by default
78
	 * (unless overridden on client-side by JS)
79
	 * Added via `wp_head` action in `init`
80
	 */
81
	static function base_target_blank() {
82
		echo '<base target="_blank" />';
83
	}
84
}
85