Completed
Push — try/auto-verify-existing-users ( 43ab54...e05dc1 )
by
unknown
87:23 queued 79:32
created

Jetpack_Comment_Likes::frontend_init()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Module Name: Comment Likes
4
 * Module Description: Increase visitor engagement by adding a Like button to comments.
5
 * Sort Order: 39
6
 * Recommendation Order: 17
7
 * First Introduced: 5.1
8
 * Requires Connection: Yes
9
 * Auto Activate: No
10
 * Module Tags: Social
11
 * Additional Search Queries: like widget, like button, like, likes
12
 */
13
14
Jetpack::dns_prefetch(
15
	array(
16
		'//widgets.wp.com',
17
	)
18
);
19
20
require_once dirname( __FILE__ ) . '/likes/jetpack-likes-master-iframe.php';
21
require_once dirname( __FILE__ ) . '/likes/jetpack-likes-settings.php';
22
23
class Jetpack_Comment_Likes {
24
	public static function init() {
25
		static $instance = NULL;
26
27
		if ( ! $instance ) {
28
			$instance = new Jetpack_Comment_Likes;
29
		}
30
31
		return $instance;
32
	}
33
34
	private function __construct() {
35
		$this->settings  = new Jetpack_Likes_Settings();
0 ignored issues
show
Bug introduced by
The property settings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
		$this->blog_id   = Jetpack_Options::get_option( 'id' );
0 ignored issues
show
Bug introduced by
The property blog_id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
		$this->url       = home_url();
0 ignored issues
show
Bug introduced by
The property url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
		$this->url_parts = parse_url( $this->url );
0 ignored issues
show
Bug introduced by
The property url_parts does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
		$this->domain    = $this->url_parts['host'];
0 ignored issues
show
Bug introduced by
The property domain does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
41
		add_action( 'init', array( $this, 'frontend_init' ) );
42
		add_action( 'admin_init', array( $this, 'admin_init' ) );
43
44
		if ( ! Jetpack::is_module_active( 'likes' ) ) {
45
			$active = Jetpack::get_active_modules();
46
47 View Code Duplication
			if ( ! in_array( 'sharedaddy', $active ) && ! in_array( 'publicize', $active ) ) {
48
				// we don't have a sharing page yet
49
				add_action( 'admin_menu', array( $this->settings, 'sharing_menu' ) );
50
			}
51
52 View Code Duplication
			if ( in_array( 'publicize', $active ) && ! in_array( 'sharedaddy', $active ) ) {
53
				// we have a sharing page but not the global options area
54
				add_action( 'pre_admin_screen_sharing', array( $this->settings, 'sharing_block' ), 20 );
55
				add_action( 'pre_admin_screen_sharing', array( $this->settings, 'updated_message' ), -10 );
56
			}
57
58 View Code Duplication
			if( ! in_array( 'sharedaddy', $active ) ) {
59
				add_action( 'admin_init', array( $this->settings, 'process_update_requests_if_sharedaddy_not_loaded' ) );
60
				add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_showbuttonon_init' ), 19 );
61
				add_action( 'sharing_admin_update', array( $this->settings, 'admin_settings_showbuttonon_callback' ), 19 );
62
				add_action( 'admin_init', array( $this->settings, 'add_meta_box' ) );
63
			} else {
64
				add_filter( 'sharing_meta_box_title', array( $this->settings, 'add_likes_to_sharing_meta_box_title' ) );
65
				add_action( 'start_sharing_meta_box_content', array( $this->settings, 'meta_box_content' ) );
66
			}
67
68
			add_action( 'save_post', array( $this->settings, 'meta_box_save' ) );
69
			add_action( 'edit_attachment', array( $this->settings, 'meta_box_save' ) );
70
			add_action( 'sharing_global_options', array( $this->settings, 'admin_settings_init' ), 20 );
71
			add_action( 'sharing_admin_update',   array( $this->settings, 'admin_settings_callback' ), 20 );
72
		}
73
	}
74
75
	public function admin_init() {
76
		add_filter( 'manage_edit-comments_columns', array( $this, 'add_like_count_column' ) );
77
		add_action( 'manage_comments_custom_column', array( $this, 'comment_likes_edit_column' ), 10, 2 );
78
		add_action( 'admin_print_styles-edit-comments.php', array( $this, 'enqueue_admin_styles_scripts' ) );
79
	}
80
81
	public function comment_likes_edit_column( $column_name, $comment_id ) {
82
		if ( 'comment_likes' !== $column_name ) {
83
			return;
84
		}
85
86
		$permalink = get_permalink( get_the_ID() );
87
		?>
88
		<a
89
		   data-comment-id="<?php echo absint( $comment_id ); ?>"
90
		   data-blog-id="<?php echo absint( $this->blog_id ); ?>"
91
		   class="comment-like-count"
92
		   id="comment-like-count-<?php echo absint( $comment_id ); ?>"
93
		   href="<?php echo esc_url( $permalink ); ?>#comment-<?php echo absint( $comment_id ); ?>"
94
		>
95
			<span class="like-count">0</span>
96
		</a>
97
		<?php
98
	}
99
100
	function enqueue_admin_styles_scripts() {
101
		wp_enqueue_style( 'comment-like-count', plugins_url( 'comment-likes/admin-style.css', __FILE__ ), array(), JETPACK__VERSION );
102
		wp_enqueue_script(
103
			'comment-like-count',
104
			Jetpack::get_file_url_for_environment(
105
				'_inc/build/comment-likes/comment-like-count.min.js',
106
				'modules/comment-likes/comment-like-count.js'
107
			),
108
			array( 'jquery' ),
109
			JETPACK__VERSION
110
		);
111
	}
112
113
	public function add_like_count_column( $columns ) {
114
		$columns['comment_likes'] = '<span class="vers"></span>';
115
116
		return $columns;
117
	}
118
119
	public function frontend_init() {
120
		if ( is_admin() ) {
121
			return;
122
		}
123
124
		if ( Jetpack_AMP_Support::is_amp_request() ) {
125
			return;
126
		}
127
128
		add_action( 'wp_enqueue_scripts', array( $this, 'load_styles_register_scripts' ) );
129
		add_filter( 'comment_text', array( $this, 'comment_likes' ), 10, 2 );
130
	}
131
132
	public function load_styles_register_scripts() {
133
		if ( ! wp_style_is( 'open-sans', 'registered' ) ) {
134
			wp_register_style( 'open-sans', 'https://fonts.googleapis.com/css?family=Open+Sans', array(), JETPACK__VERSION );
135
		}
136
		wp_enqueue_style( 'jetpack_likes', plugins_url( 'likes/style.css', __FILE__ ), array( 'open-sans' ), JETPACK__VERSION );
137
		wp_enqueue_script(
138
			'postmessage',
139
			Jetpack::get_file_url_for_environment( '_inc/build/postmessage.min.js', '_inc/postmessage.js' ),
140
			array( 'jquery' ),
141
			JETPACK__VERSION,
142
			false
143
		);
144
		wp_enqueue_script(
145
			'jetpack_resize',
146
			Jetpack::get_file_url_for_environment(
147
				'_inc/build/jquery.jetpack-resize.min.js',
148
				'_inc/jquery.jetpack-resize.js'
149
			),
150
			array( 'jquery' ),
151
			JETPACK__VERSION,
152
			false
153
		);
154
		wp_enqueue_script( 'jetpack_likes_queuehandler', plugins_url( 'likes/queuehandler.js' , __FILE__ ), array( 'jquery', 'postmessage', 'jetpack_resize' ), JETPACK__VERSION, true );
155
	}
156
157
	public function comment_likes( $content, $comment = null ) {
158
		if ( empty( $comment ) ) {
159
			return $content;
160
		}
161
162
		if ( ! $this->settings->is_likes_visible() ) {
163
			return $content;
164
		}
165
166
		$comment_id = get_comment_ID();
167
		if ( empty( $comment_id ) && ! empty( $comment->comment_ID ) ) {
168
			$comment_id = $comment->comment_ID;
169
		}
170
171
		if ( empty( $content ) || empty( $comment_id ) ) {
172
			return $content;
173
		}
174
175
		// In case master iframe hasn't been loaded. This could be the case when Post Likes module is disabled,
176
		// or on pages on which we have comments but post likes are disabled.
177
		if ( false === has_action( 'wp_footer', 'jetpack_likes_master_iframe' ) ) {
178
			add_action( 'wp_footer', 'jetpack_likes_master_iframe', 21 );
179
		}
180
181
		$uniqid = uniqid();
182
183
		$src     = sprintf( 'https://widgets.wp.com/likes/#blog_id=%1$d&amp;comment_id=%2$d&amp;origin=%3$s&amp;obj_id=%1$d-%2$d-%4$s', $this->blog_id, $comment_id, $this->domain, $uniqid );
184
		$name    = sprintf( 'like-comment-frame-%1$d-%2$d-%3$s', $this->blog_id, $comment_id, $uniqid );
185
		$wrapper = sprintf( 'like-comment-wrapper-%1$d-%2$d-%3$s', $this->blog_id, $comment_id, $uniqid );
186
187
		$html = '';
188
		$html .= "<div class='jetpack-comment-likes-widget-wrapper jetpack-likes-widget-unloaded' id='$wrapper' data-src='$src' data-name='$name'>";
189
		$html .= "<div class='likes-widget-placeholder comment-likes-widget-placeholder comment-likes'><span class='loading'>" . esc_html__( 'Loading...', 'jetpack' ) . "</span></div>";
190
		$html .= "<div class='comment-likes-widget jetpack-likes-widget comment-likes'><span class='comment-like-feedback'></span>";
191
		$html .= "<span class='sd-text-color'></span><a class='sd-link-color'></a>";
192
		$html .= '</div></div>';
193
194
		/**
195
		 * Filters the Comment Likes button content.
196
		 *
197
		 * @module comment-likes
198
		 *
199
		 * @since 5.1.0
200
		 *
201
		 * @param string $html Comment Likes button content.
202
		 */
203
		$like_button = apply_filters( 'comment_like_button', $html );
204
205
		return $content . $like_button;
206
	}
207
}
208
209
Jetpack_Comment_Likes::init();
210