Completed
Push — add/amp-wp-support ( 1c8249 )
by
unknown
09:38
created

Jetpack_AMP::is_amp_request()   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 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Module Name: AMP
4
 * Module Description: Add AMP support for Jetpack features
5
 * Sort Order: 40
6
 * Recommendation Order: 18
7
 * First Introduced: 6.2.0
8
 * Requires Connection: Yes
9
 * Auto Activate: No
10
 * Module Tags: AMP
11
 * Feature: Traffic
12
 * Additional Search Queries: amp, performance, mobile
13
 */
14
15
/**
16
 * TODO:
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
17
 * * prompt to sideload AMP plugin
18
 * * gutenberg support?
19
 * * etc.
20
 */
21
22
class Jetpack_AMP {
23
	/**
24
	 * @var Jetpack_AMP
25
	 */
26
	private static $__instance = null;
27
	private $is_amp_request_cache = null;
28
29
	/**
30
	 * Singleton implementation
31
	 *
32
	 * @return Jetpack_AMP
33
	 */
34
	public static function instance() {
35
		if ( is_null( self::$__instance ) ) {
36
			self::$__instance = new Jetpack_AMP;
37
		}
38
39
		return self::$__instance;
40
	}
41
42
	private function __construct() {
43
		add_action( 'template_redirect', array( $this, 'disable_comment_likes' ) );
44
		add_action( 'template_redirect', array( $this, 'disable_likes' ) );
45
		add_action( 'wp', array( $this, 'disable_related_posts' ), 1 );
46
	}
47
48
	private function is_amp_request() {
49
		if ( is_null( $this->is_amp_request_cache ) ) {
50
			$this->is_amp_request_cache = is_amp_endpoint();
51
		}
52
53
		return $this->is_amp_request_cache;
54
	}
55
56
	public function disable_comment_likes() {
57
		if ( ! $this->is_amp_request() || ! Jetpack::is_module_active( 'comment-likes' ) ) {
58
			return;
59
		}
60
61
		$module = Jetpack_Comment_Likes::init();
62
63
		// Undo \Jetpack_Comment_Likes::frontend_init().
64
		remove_action( 'wp_enqueue_scripts', array( $module, 'load_styles_register_scripts' ) );
65
		remove_filter( 'comment_text', array( $module, 'comment_likes' ) );
66
	}
67
68
	public function disable_likes() {
69
		if ( ! $this->is_amp_request() || ! Jetpack::is_module_active( 'likes' ) ) {
70
			return;
71
		}
72
73
		$module = Jetpack_Likes::init();
74
75
		// Undo \Jetpack_Likes::action_init().
76
		remove_filter( 'the_content', array( $module, 'post_likes' ), 30 );
77
		remove_filter( 'the_excerpt', array( $module, 'post_likes' ), 30 );
78
		remove_filter( 'post_flair', array( $module, 'post_likes' ), 30 );
79
		remove_filter( 'post_flair_block_css', array( $module, 'post_flair_service_enabled_like' ) );
80
		wp_dequeue_script( 'postmessage' );
81
		wp_dequeue_script( 'jetpack_resize' );
82
		wp_dequeue_script( 'jetpack_likes_queuehandler' );
83
		wp_dequeue_style( 'jetpack_likes' );
84
	}
85
86
	// Disable Related Posts since not available in AMP.
87
	public function disable_related_posts() {
88
		if ( ! $this->is_amp_request() || ! Jetpack::is_module_active( 'related-posts' ) ) {
89
			return;
90
		}
91
		add_filter( 'jetpack_relatedposts_filter_enabled_for_request', '__return_false' );
92
	}
93
}
94
95
if ( function_exists( 'is_amp_endpoint' ) ) {
96
	Jetpack_AMP::instance();
97
}
98
99