Completed
Push — update/comments-ignore-author-... ( 4f8a13 )
by
unknown
11:54
created

Jetpack_Comments::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
require dirname( __FILE__ ) . '/base.php';
4
5
/**
6
 * Main Comments class
7
 *
8
 * @package JetpackComments
9
 * @version 1.4
10
 * @since 1.4
11
 */
12
class Jetpack_Comments extends Highlander_Comments_Base {
13
14
	/** Variables *************************************************************/
15
16
	/**
17
	 * Possible comment form sources
18
	 * @var array
19
	 */
20
	public $id_sources = array();
21
22
	/**
23
	 * URL
24
	 * @var string
25
	 */
26
	public $signed_url = '';
27
28
	/**
29
	 * The default comment form color scheme
30
	 * @var string
31
	 * @see ::set_default_color_theme_based_on_theme_settings()
32
	 */
33
	public $default_color_scheme =  'light';
34
35
	/** Methods ***************************************************************/
36
37
	public static function init() {
38
		static $instance = false;
39
40
		if ( !$instance ) {
41
			$instance = new Jetpack_Comments;
42
		}
43
44
		return $instance;
45
	}
46
47
	/**
48
	 * Main constructor for Comments
49
	 *
50
	 * @since JetpackComments (1.4)
51
	 */
52
	public function __construct() {
53
		parent::__construct();
54
55
		// Comments is loaded
56
57
		/**
58
		 * Fires after the Jetpack_Comments object has been instantiated
59
		 *
60
		 * @module comments
61
		 *
62
		 * @since 1.4.0
63
		 *
64
		 * @param array $jetpack_comments_loaded First element in array of type Jetpack_Comments
65
		 **/
66
		do_action_ref_array( 'jetpack_comments_loaded', array( $this ) );
67
		add_action( 'after_setup_theme', array( $this, 'set_default_color_theme_based_on_theme_settings' ), 100 );
68
	}
69
70
	public function set_default_color_theme_based_on_theme_settings() {
71
		if ( function_exists( 'twentyeleven_get_theme_options' ) ) {
72
			$theme_options = twentyeleven_get_theme_options();
73
			$theme_color_scheme = isset( $theme_options['color_scheme'] ) ? $theme_options['color_scheme'] : 'transparent';
74
		} else {
75
			$theme_color_scheme = get_theme_mod( 'color_scheme', 'transparent' );
76
		}
77
		// Default for $theme_color_scheme is 'transparent' just so it doesn't match 'light' or 'dark'
78
		// The default for Jetpack's color scheme is still defined above as 'light'
79
80
		if ( false !== stripos( $theme_color_scheme, 'light' ) ) {
81
			$this->default_color_scheme = 'light';
82
		} elseif ( false !== stripos( $theme_color_scheme, 'dark' ) ) {
83
			$this->default_color_scheme = 'dark';
84
		}
85
	}
86
87
	/** Private Methods *******************************************************/
88
89
	/**
90
	 * Set any global variables or class variables
91
	 * @since JetpackComments (1.4)
92
	 */
93
	protected function setup_globals() {
94
		parent::setup_globals();
95
96
		// Sources
97
		$this->id_sources = array(
98
			'guest',
99
			'jetpack',
100
			'wordpress',
101
			'twitter',
102
			'facebook'
103
		);
104
	}
105
106
	/**
107
	 * Setup actions for methods in this class
108
	 * @since JetpackComments (1.4)
109
	 */
110
	protected function setup_actions() {
111
		parent::setup_actions();
112
113
		// Selfishly remove everything from the existing comment form
114
		remove_all_actions( 'comment_form_before' );
115
		remove_all_actions( 'comment_form_after'  );
116
117
		// Selfishly add only our actions back to the comment form
118
		add_action( 'comment_form_before', array( $this, 'comment_form_before' ) );
119
		add_action( 'comment_form_after',  array( $this, 'comment_form_after'  ) );
120
121
		// Before a comment is posted
122
		add_action( 'pre_comment_on_post', array( $this, 'pre_comment_on_post' ), 1 );
123
124
		// After a comment is posted
125
		add_action( 'comment_post', array( $this, 'add_comment_meta' ) );
126
	}
127
128
	/**
129
	 * Setup filters for methods in this class
130
	 * @since 1.6.2
131
	 */
132
	protected function setup_filters() {
133
		parent::setup_filters();
134
135
		add_filter( 'comment_post_redirect', array( $this, 'capture_comment_post_redirect_to_reload_parent_frame' ), 100 );
136
		add_filter( 'get_avatar',            array( $this, 'get_avatar' ), 10, 4 );
137
	}
138
139
	/**
140
	 * Get the comment avatar from Gravatar, Twitter, or Facebook
141
	 *
142
	 * @since JetpackComments (1.4)
143
	 * @param string $avatar Current avatar URL
144
	 * @param string $comment Comment for the avatar
145
	 * @param int $size Size of the avatar
146
	 * @param string $default Not used
147
	 * @return string New avatar
148
	 */
149
	public function get_avatar( $avatar, $comment, $size, $default ) {
0 ignored issues
show
Unused Code introduced by
The parameter $default is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
		if ( ! isset( $comment->comment_post_ID ) || ! isset( $comment->comment_ID ) ) {
151
			// it's not a comment - bail
152
			return $avatar;
153
		}
154
155
		// It's a FB or Twitter avatar
156
		$foreign_avatar = get_comment_meta( $comment->comment_ID, 'hc_avatar', true );
157
		if ( empty( $foreign_avatar ) ) {
158
			// Can't find the avatar details - bail
159
			return $avatar;
160
		}
161
162
		// Return the FB or Twitter avatar
163
		return preg_replace( '#src=([\'"])[^\'"]+\\1#', 'src=\\1' . esc_url( $this->photon_avatar( $foreign_avatar, $size ) ) . '\\1', $avatar );
164
	}
165
166
	/** Output Methods ********************************************************/
167
168
	/**
169
	 * Start capturing the core comment_form() output
170
	 * @since JetpackComments (1.4)
171
	 */
172
	public function comment_form_before() {
173
		/**
174
		 * Filters the setting that determines if Jetpagk comments should be enabled for
175
		 * the current post type.
176
		 *
177
		 * @module comments
178
		 *
179
		 * @since 3.8.1
180
		 *
181
		 * @param boolean $return Should comments be enabled?
182
		 */
183
		if ( ! apply_filters( 'jetpack_comment_form_enabled_for_' . get_post_type(), true ) ) {
184
			return;
185
		}
186
187
		// Add some JS to the footer
188
		add_action( 'wp_footer', array( $this, 'watch_comment_parent' ), 100 );
189
190
		ob_start();
191
	}
192
193
	/**
194
	 * Noop the default comment form output, get some options, and output our
195
	 * tricked out totally radical comment form.
196
	 *
197
	 * @since JetpackComments (1.4)
198
	 */
199
	public function comment_form_after() {
200
		/** This filter is documented in modules/comments/comments.php */
201
		if ( ! apply_filters( 'jetpack_comment_form_enabled_for_' . get_post_type(), true ) ) {
202
			return;
203
		}
204
205
		// Throw it all out and drop in our replacement
206
		ob_end_clean();
207
208
		// If users are required to be logged in, and they're not, then we don't need to do anything else
209
		if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) {
210
			/**
211
			 * Changes the log in to comment prompt.
212
			 *
213
			 * @module comments
214
			 *
215
			 * @since 1.4.0
216
			 *
217
			 * @param string $var Default is "You must log in to post a comment."
218
			 */
219
			echo '<p class="must-log-in">' . sprintf( apply_filters( 'jetpack_must_log_in_to_comment', __( 'You must <a href="%s">log in</a> to post a comment.', 'jetpack' ) ), wp_login_url( get_permalink() . '#respond' ) ) . '</p>';
220
			return;
221
		}
222
223
		if ( in_array( 'subscriptions', Jetpack::get_active_modules() ) ) {
224
			$stb_enabled = get_option( 'stb_enabled', 1 );
225
			$stb_enabled = empty( $stb_enabled ) ? 0 : 1;
226
227
			$stc_enabled = get_option( 'stc_enabled', 1 );
228
			$stc_enabled = empty( $stc_enabled ) ? 0 : 1;
229
		} else {
230
			$stb_enabled = 0;
231
			$stc_enabled = 0;
232
		}
233
234
		$params  = array(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
235
			'blogid'               => Jetpack_Options::get_option( 'id' ),
236
			'postid'               => get_the_ID(),
237
			'comment_registration' => ( get_option( 'comment_registration' ) ? '1' : '0' ), // Need to explicitly send a '1' or a '0' for these
238
			'require_name_email'   => ( get_option( 'require_name_email' )   ? '1' : '0' ),
239
			'stc_enabled'          => $stc_enabled,
240
			'stb_enabled'          => $stb_enabled,
241
			'show_avatars'         => ( get_option( 'show_avatars' )         ? '1' : '0' ),
242
			'avatar_default'       => get_option( 'avatar_default' ),
243
			'greeting'             => get_option( 'highlander_comment_form_prompt', __( 'Leave a Reply', 'jetpack' ) ),
244
			/**
245
			 * Changes the comment form prompt.
246
			 *
247
			 * @module comments
248
			 *
249
			 * @since 2.3.0
250
			 *
251
			 * @param string $var Default is "Leave a Reply to %s."
252
			 */
253
			'greeting_reply'       => apply_filters( 'jetpack_comment_form_prompt_reply', __( 'Leave a Reply to %s' , 'jetpack' ) ),
254
			'color_scheme'         => get_option( 'jetpack_comment_form_color_scheme', $this->default_color_scheme ),
255
			'lang'                 => get_bloginfo( 'language' ),
256
			'jetpack_version'      => JETPACK__VERSION,
257
		);
258
259
		// Extra parameters for logged in user
260
		if ( is_user_logged_in() ) {
261
			$current_user           = wp_get_current_user();
262
			$params['hc_post_as']   = 'jetpack';
263
			$params['hc_userid']    = $current_user->ID;
264
			$params['hc_username']  = $current_user->display_name;
265
			$params['hc_userurl']   = $current_user->user_url;
266
			$params['hc_useremail'] = md5( strtolower( trim( $current_user->user_email ) ) );
267
			if ( current_user_can( 'unfiltered_html' ) )
268
				$params['_wp_unfiltered_html_comment'] = wp_create_nonce( 'unfiltered-html-comment_' . get_the_ID() );
269
		}
270
271
		$signature = Jetpack_Comments::sign_remote_comment_parameters( $params, Jetpack_Options::get_option( 'blog_token' ) );
272
		if ( is_wp_error( $signature ) ) {
273
			$signature = 'error';
274
		}
275
276
		$params['sig']    = $signature;
277
		$url_origin       = set_url_scheme( 'http://jetpack.wordpress.com' );
278
		$url              = "{$url_origin}/jetpack-comment/?" . http_build_query( $params );
279
		$url              = "{$url}#parent=" . urlencode( set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) );
280
		$this->signed_url = $url;
281
		$height           = $params['comment_registration'] || is_user_logged_in() ? '315' : '430'; // Iframe can be shorter if we're not allowing guest commenting
282
		$transparent      = ( $params['color_scheme'] == 'transparent' ) ? 'true' : 'false';
283
284
		if ( isset( $_GET['replytocom'] ) ) {
285
			$url .= '&replytocom=' . (int) $_GET['replytocom'];
286
		}
287
288
		// The actual iframe (loads comment form from Jetpack server)
289
		?>
290
291
		<div id="respond" class="comment-respond">
292
			<h3 id="reply-title" class="comment-reply-title"><?php comment_form_title( esc_html( $params['greeting'] ), esc_html( $params['greeting_reply'] ) ); ?> <small><?php cancel_comment_reply_link( esc_html__( 'Cancel reply' , 'jetpack') ); ?></small></h3>
293
			<div id="commentform" class="comment-form">
294
				<iframe src="<?php echo esc_url( $url ); ?>" allowtransparency="<?php echo $transparent; ?>" style="width:100%; height: <?php echo $height; ?>px;border:0px;" frameBorder="0" scrolling="no" name="jetpack_remote_comment" id="jetpack_remote_comment"></iframe>
295
			</div>
296
		</div>
297
298
		<?php // Below is required for comment reply JS to work ?>
299
300
		<input type="hidden" name="comment_parent" id="comment_parent" value="" />
301
302
		<?php
303
	}
304
305
	/**
306
	 * Add some JS to wp_footer to watch for hierarchical reply parent change
307
	 *
308
	 * @since JetpackComments (1.4)
309
	 */
310
	public function watch_comment_parent() {
311
		$url_origin = set_url_scheme( 'http://jetpack.wordpress.com' );
312
	?>
313
314
		<!--[if IE]>
315
		<script type="text/javascript">
316
		if ( 0 === window.location.hash.indexOf( '#comment-' ) ) {
317
			// window.location.reload() doesn't respect the Hash in IE
318
			window.location.hash = window.location.hash;
319
		}
320
		</script>
321
		<![endif]-->
322
		<script type="text/javascript">
323
			var comm_par_el = document.getElementById( 'comment_parent' ),
324
			    comm_par = (comm_par_el && comm_par_el.value) ? comm_par_el.value : '',
325
			    frame = document.getElementById( 'jetpack_remote_comment' ),
326
			    tellFrameNewParent;
327
328
			tellFrameNewParent = function() {
329
				if ( comm_par ) {
330
					frame.src = "<?php echo esc_url_raw( $this->signed_url ); ?>" + '&replytocom=' + parseInt( comm_par, 10 ).toString();
331
				} else {
332
					frame.src = "<?php echo esc_url_raw( $this->signed_url ); ?>";
333
				}
334
			};
335
336
	<?php if ( get_option( 'thread_comments' ) && get_option( 'thread_comments_depth' ) ) : ?>
337
338
			if ( 'undefined' !== typeof addComment ) {
339
				addComment._Jetpack_moveForm = addComment.moveForm;
340
341
				addComment.moveForm = function( commId, parentId, respondId, postId ) {
342
					var returnValue = addComment._Jetpack_moveForm( commId, parentId, respondId, postId ), cancelClick, cancel;
343
344
					if ( false === returnValue ) {
345
						cancel = document.getElementById( 'cancel-comment-reply-link' );
346
						cancelClick = cancel.onclick;
347
						cancel.onclick = function() {
348
							var cancelReturn = cancelClick.call( this );
349
							if ( false !== cancelReturn ) {
350
								return cancelReturn;
351
							}
352
353
							if ( !comm_par ) {
354
								return cancelReturn;
355
							}
356
357
							comm_par = 0;
358
359
							tellFrameNewParent();
360
361
							return cancelReturn;
362
						};
363
					}
364
365
					if ( comm_par == parentId ) {
366
						return returnValue;
367
					}
368
369
					comm_par = parentId;
370
371
					tellFrameNewParent();
372
373
					return returnValue;
374
				};
375
			}
376
377
	<?php endif; ?>
378
379
			if ( window.postMessage ) {
380
				if ( document.addEventListener ) {
381
					window.addEventListener( 'message', function( event ) {
382
						if ( <?php echo json_encode( esc_url_raw( $url_origin ) ); ?> !== event.origin ) {
383
							return;
384
						}
385
386
						jQuery( frame ).height( event.data );
387
					} );
388
				} else if ( document.attachEvent ) {
389
					window.attachEvent( 'message', function( event ) {
390
						if ( <?php echo json_encode( esc_url_raw( $url_origin ) ); ?> !== event.origin ) {
391
							return;
392
						}
393
394
						jQuery( frame ).height( event.data );
395
					} );
396
				}
397
			}
398
		</script>
399
400
	<?php
401
	}
402
403
	/**
404
	 * Verify the hash included in remote comments.
405
	 *
406
	 * @since JetpackComments (1.4)
407
	 * @param type $comment Not used
408
	 */
409
	public function pre_comment_on_post( $comment ) {
0 ignored issues
show
Unused Code introduced by
The parameter $comment is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
410
		$post_array = stripslashes_deep( $_POST );
411
412
		// Bail if missing the Jetpack token
413
		if ( ! isset( $post_array['sig'] ) ) {
414
			unset( $_POST['hc_post_as'] );
415
			return;
416
		}
417
418
		if ( FALSE !== strpos( $post_array['hc_avatar'], '.gravatar.com' ) )
419
			$post_array['hc_avatar'] = htmlentities( $post_array['hc_avatar'] );
420
421
		$check = Jetpack_Comments::sign_remote_comment_parameters( $post_array, Jetpack_Options::get_option( 'blog_token' ) );
422
		if ( is_wp_error( $check ) ) {
423
			wp_die( $check );
424
		}
425
426
		// Bail if token is expired or not valid
427
		if ( $check !== $post_array['sig'] )
428
			wp_die( __( 'Invalid security token.', 'jetpack' ) );
429
430
		/** This filter is documented in modules/comments/comments.php */
431
		if ( ! apply_filters( 'jetpack_comment_form_enabled_for_' . get_post_type( $post_array['comment_post_ID'] ), true ) ) {
432
			// In case the comment POST is legit, but the comments are
433
			// now disabled, we don't allow the comment
434
435
			wp_die( __( 'Comments are not allowed.', 'jetpack' ) );
436
		}
437
	}
438
439
	/** Capabilities **********************************************************/
440
441
	/**
442
	 * Add some additional comment meta after comment is saved about what
443
	 * service the comment is from, the avatar, user_id, etc...
444
	 *
445
	 * @since JetpackComments (1.4)
446
	 * @param type $comment_id
447
	 */
448
	public function add_comment_meta( $comment_id ) {
449
		$comment_meta = array();
450
451
		switch( $this->is_highlander_comment_post() ) {
452 View Code Duplication
			case 'facebook' :
453
				$comment_meta['hc_post_as']         = 'facebook';
454
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
455
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
456
				break;
457
458 View Code Duplication
			case 'twitter' :
459
				$comment_meta['hc_post_as']         = 'twitter';
460
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
461
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
462
				break;
463
464
			case 'wordpress' :
465
				$comment_meta['hc_post_as']         = 'wordpress';
466
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
467
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
468
				$comment_meta['hc_wpcom_id_sig']    = stripslashes( $_POST['hc_wpcom_id_sig'] ); //since 1.9
469
				break;
470
471 View Code Duplication
			case 'jetpack' :
472
				$comment_meta['hc_post_as']         = 'jetpack';
473
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
474
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
475
				break;
476
477
		}
478
479
		// Bail if no extra comment meta
480
		if ( empty( $comment_meta ) )
481
			return;
482
483
		// Loop through extra meta and add values
484
		foreach ( $comment_meta as $key => $value )
485
			add_comment_meta( $comment_id, $key, $value, true );
486
	}
487
	function capture_comment_post_redirect_to_reload_parent_frame( $url ) {
488
		if ( !isset( $_GET['for'] ) || 'jetpack' != $_GET['for'] ) {
489
			return $url;
490
		}
491
?>
492
<!DOCTYPE html>
493
<html <?php language_attributes(); ?>>
494
<!--<![endif]-->
495
<head>
496
<meta charset="<?php bloginfo( 'charset' ); ?>" />
497
<title><?php printf( __( 'Submitting Comment%s', 'jetpack' ), '&hellip;' ); ?></title>
498
<style type="text/css">
499
body {
500
	display: table;
501
	width: 100%;
502
	height: 60%;
503
	position: absolute;
504
	top: 0;
505
	left: 0;
506
	overflow: hidden;
507
	color: #333;
508
}
509
510
h1 {
511
	text-align: center;
512
	margin: 0;
513
	padding: 0;
514
	display: table-cell;
515
	vertical-align: middle;
516
	font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", sans-serif;
517
	font-weight: normal;
518
}
519
520
.hidden {
521
	opacity: 0;
522
}
523
524
h1 span {
525
	-moz-transition-property: opacity;
526
	-moz-transition-duration: 1s;
527
	-moz-transition-timing-function: ease-in-out;
528
529
	-webkit-transition-property: opacity;
530
	-webkit-transition-duration: 1s;
531
	-webbit-transition-timing-function: ease-in-out;
532
533
	-o-transition-property: opacity;
534
	-o-transition-duration: 1s;
535
	-o-transition-timing-function: ease-in-out;
536
537
	-ms-transition-property: opacity;
538
	-ms-transition-duration: 1s;
539
	-ms-transition-timing-function: ease-in-out;
540
541
	transition-property: opacity;
542
	transition-duration: 1s;
543
	transition-timing-function: ease-in-out;
544
}
545
</style>
546
</head>
547
<body>
548
	<h1><?php printf( __( 'Submitting Comment%s', 'jetpack' ), '<span id="ellipsis" class="hidden">&hellip;</span>' ); ?></h1>
549
<script type="text/javascript">
550
try {
551
	window.parent.location = <?php echo json_encode( $url ); ?>;
552
	window.parent.location.reload( true );
553
} catch ( e ) {
554
	window.location = <?php echo json_encode( $url ); ?>;
555
	window.location.reload( true );
556
}
557
ellipsis = document.getElementById( 'ellipsis' );
558
function toggleEllipsis() {
559
	ellipsis.className = ellipsis.className ? '' : 'hidden';
560
}
561
setInterval( toggleEllipsis, 1200 );
562
</script>
563
</body>
564
</html>
565
<?php
566
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method capture_comment_post_red...o_reload_parent_frame() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
567
	}
568
}
569
570
Jetpack_Comments::init();
571