Completed
Push — add/amp-wp-support ( 1b42e6...6f4057 )
by
unknown
23:03 queued 08:35
created

Jetpack_AMP_Support::should_implode_css()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Manages compatibility with the amp-wp plugin
5
 *
6
 * @see https://github.com/Automattic/amp-wp
7
 */
8
class Jetpack_AMP_Support {
9
	// static $modules_to_disable = array( 'likes', 'comment-likes', 'related-posts', 'carousel', 'photon', 'lazy-images', 'notes' );
10
11
	static function init() {
12
		if ( ! self::is_amp_request() ) {
13
			return;
14
		}
15
16
		// carousel
17
		add_filter( 'jp_carousel_maybe_disable', '__return_true' );
18
19
		// sharing
20
		add_filter( 'sharing_enqueue_scripts', '__return_false' );
21
		add_filter( 'jetpack_sharing_counts', '__return_false' );
22
		add_filter( 'sharing_js', '__return_false' );
23
		add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 );
24
25
		// disable imploding CSS
26
		add_filter( 'jetpack_implode_frontend_css', array( 'Jetpack_AMP_Support', 'should_implode_css' ) );
27
28
		// enforce freedom mode for videopress
29
		add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );
30
31
		// DONE
32
		// disable likes
33
		// disable comment likes
34
		// disable related posts
35
		// disable carousel
36
		// disable photon
37
		// disable notifications
38
		// disable devicepx
39
		// modify social sharing
40
		// disable milestone widget
41
		// force using separate stylesheets to avoid unnecessary tree shaking
42
		// videopress freedom mode
43
		// import functions from jetpack-helper.php in amp-wp
44
45
46
47
	}
48
49
	static function init_filter_jetpack_modules() {
50
		if ( ! self::is_amp_request() ) {
51
			return;
52
		}
53
54
		// widgets
55
		add_filter( 'jetpack_widgets_to_include', array( 'Jetpack_AMP_Support', 'filter_available_widgets' ) );
56
	}
57
58
	static function is_amp_request() {
59
		// can't use is_amp_endpoint() since it's not ready early enough in init.
60
		// is_amp_endpoint() implementation calls is_feed, which bails with a notice if plugins_loaded isn't finished
61
		// "Conditional query tags do not work before the query is run"
62
		return ! is_admin() // this is necessary so that modules can still be enabled/disabled/configured as per normal via Jetpack admin
63
			&&
64
				function_exists( 'amp_is_canonical' ) // this is really just testing if the plugin exists
65
			&&
66
				( amp_is_canonical() || isset( $_GET[ amp_get_slug() ] ) );
67
	}
68
69
	static function filter_available_widgets( $widgets ) {
70
		if ( self::is_amp_request() ) {
71
			$widgets = array_filter( $widgets, array( 'Jetpack_AMP_Support', 'is_supported_widget' ) );
72
		}
73
74
		return $widgets;
75
	}
76
77
	static function is_supported_widget( $widget_path ) {
78
		return substr($widget_path, -14) !== '/milestone.php';
79
	}
80
81
	static function should_implode_css( $implode ) {
82
		if ( self::is_amp_request() ) {
83
			return false;
84
		}
85
86
		return $implode;
87
	}
88
89
	static function videopress_enable_freedom_mode( $options ) {
90
		$options['freedom'] = true;
91
		return $options;
92
	}
93
94
	static function render_sharing_html( $markup, $sharing_enabled ) {
95
		remove_action( 'wp_footer', 'sharing_add_footer' );
96
		if ( empty( $sharing_enabled ) ) {
97
			return $markup;
98
		}
99
		$supported_services = array(
100
			'facebook'      => array(
101
				/** This filter is documented in modules/sharedaddy/sharing-sources.php */
102
				'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ),
103
			),
104
			'twitter'       => array(),
105
			'pinterest'     => array(),
106
			'whatsapp'      => array(),
107
			'google-plus-1' => array(
108
				'type' => 'gplus',
109
			),
110
			'tumblr'        => array(),
111
			'linkedin'      => array(),
112
		);
113
		$sharing_links = array();
114
		foreach ( $sharing_enabled['visible'] as $id => $service ) {
115
			if ( ! isset( $supported_services[ $id ] ) ) {
116
				$sharing_links[] = "<!-- not supported: $id -->";
117
				continue;
118
			}
119
			$args = array_merge(
120
				array(
121
					'type' => $id,
122
				),
123
				$supported_services[ $id ]
124
			);
125
			$sharing_link = '<amp-social-share';
126
			foreach ( $args as $key => $value ) {
127
				$sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) );
128
			}
129
			$sharing_link .= '></amp-social-share>';
130
			$sharing_links[] = $sharing_link;
131
		}
132
		return preg_replace( '#(?<=<div class="sd-content">).+?(?=</div>)#s', implode( '', $sharing_links ), $markup );
133
	}
134
}
135
136
add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 );
137
138
// this is necessary since for better or worse Jetpack modules are loaded during plugins_loaded, which means we must
139
// take the opportunity to intercept initialisation before that point, either by adding explicit detection into the module,
140
// or preventing it from loading in the first place (better for performance)
141
add_action( 'plugins_loaded', array( 'Jetpack_AMP_Support', 'init_filter_jetpack_modules' ), 1 );