Completed
Push — update/media-extractor-summary... ( 317788...ea9570 )
by Jeremy
34:42 queued 06:44
created

Jetpack_Comments::setup_actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.4285
cc 1
eloc 8
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
		if ( false === strpos( $comment->comment_author_url, '/www.facebook.com/' ) && false === strpos( $comment->comment_author_url, '/twitter.com/' ) ) {
156
			// It's neither FB nor Twitter - bail
157
			return $avatar;
158
		}
159
160
		// It's a FB or Twitter avatar
161
		$foreign_avatar = get_comment_meta( $comment->comment_ID, 'hc_avatar', true );
162
		if ( empty( $foreign_avatar ) ) {
163
			// Can't find the avatar details - bail
164
			return $avatar;
165
		}
166
167
		// Return the FB or Twitter avatar
168
		return preg_replace( '#src=([\'"])[^\'"]+\\1#', 'src=\\1' . esc_url( $this->photon_avatar( $foreign_avatar, $size ) ) . '\\1', $avatar );
169
	}
170
171
	/** Output Methods ********************************************************/
172
173
	/**
174
	 * Start capturing the core comment_form() output
175
	 * @since JetpackComments (1.4)
176
	 */
177
	public function comment_form_before() {
178
		/**
179
		 * Filters the setting that determines if Jetpagk comments should be enabled for
180
		 * the current post type.
181
		 *
182
		 * @module comments
183
		 *
184
		 * @since 3.8.1
185
		 *
186
		 * @param boolean $return Should comments be enabled?
187
		 */
188
		if ( ! apply_filters( 'jetpack_comment_form_enabled_for_' . get_post_type(), true ) ) {
189
			return;
190
		}
191
192
		// Add some JS to the footer
193
		add_action( 'wp_footer', array( $this, 'watch_comment_parent' ), 100 );
194
195
		ob_start();
196
	}
197
198
	/**
199
	 * Noop the default comment form output, get some options, and output our
200
	 * tricked out totally radical comment form.
201
	 *
202
	 * @since JetpackComments (1.4)
203
	 */
204
	public function comment_form_after() {
205
		/** This filter is documented in modules/comments/comments.php */
206
		if ( ! apply_filters( 'jetpack_comment_form_enabled_for_' . get_post_type(), true ) ) {
207
			return;
208
		}
209
210
		// Throw it all out and drop in our replacement
211
		ob_end_clean();
212
213
		// If users are required to be logged in, and they're not, then we don't need to do anything else
214
		if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) {
215
			/**
216
			 * Changes the log in to comment prompt.
217
			 *
218
			 * @module comments
219
			 *
220
			 * @since 1.4.0
221
			 *
222
			 * @param string $var Default is "You must log in to post a comment."
223
			 */
224
			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>';
225
			return;
226
		}
227
228
		if ( in_array( 'subscriptions', Jetpack::get_active_modules() ) ) {
229
			$stb_enabled = get_option( 'stb_enabled', 1 );
230
			$stb_enabled = empty( $stb_enabled ) ? 0 : 1;
231
232
			$stc_enabled = get_option( 'stc_enabled', 1 );
233
			$stc_enabled = empty( $stc_enabled ) ? 0 : 1;
234
		} else {
235
			$stb_enabled = 0;
236
			$stc_enabled = 0;
237
		}
238
239
		$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...
240
			'blogid'               => Jetpack_Options::get_option( 'id' ),
241
			'postid'               => get_the_ID(),
242
			'comment_registration' => ( get_option( 'comment_registration' ) ? '1' : '0' ), // Need to explicitly send a '1' or a '0' for these
243
			'require_name_email'   => ( get_option( 'require_name_email' )   ? '1' : '0' ),
244
			'stc_enabled'          => $stc_enabled,
245
			'stb_enabled'          => $stb_enabled,
246
			'show_avatars'         => ( get_option( 'show_avatars' )         ? '1' : '0' ),
247
			'avatar_default'       => get_option( 'avatar_default' ),
248
			'greeting'             => get_option( 'highlander_comment_form_prompt', __( 'Leave a Reply', 'jetpack' ) ),
249
			/**
250
			 * Changes the comment form prompt.
251
			 *
252
			 * @module comments
253
			 *
254
			 * @since 2.3.0
255
			 *
256
			 * @param string $var Default is "Leave a Reply to %s."
257
			 */
258
			'greeting_reply'       => apply_filters( 'jetpack_comment_form_prompt_reply', __( 'Leave a Reply to %s' , 'jetpack' ) ),
259
			'color_scheme'         => get_option( 'jetpack_comment_form_color_scheme', $this->default_color_scheme ),
260
			'lang'                 => get_bloginfo( 'language' ),
261
			'jetpack_version'      => JETPACK__VERSION,
262
		);
263
264
		// Extra parameters for logged in user
265
		if ( is_user_logged_in() ) {
266
			$current_user           = wp_get_current_user();
267
			$params['hc_post_as']   = 'jetpack';
268
			$params['hc_userid']    = $current_user->ID;
269
			$params['hc_username']  = $current_user->display_name;
270
			$params['hc_userurl']   = $current_user->user_url;
271
			$params['hc_useremail'] = md5( strtolower( trim( $current_user->user_email ) ) );
272
			if ( current_user_can( 'unfiltered_html' ) )
273
				$params['_wp_unfiltered_html_comment'] = wp_create_nonce( 'unfiltered-html-comment_' . get_the_ID() );
274
		}
275
276
		$signature = Jetpack_Comments::sign_remote_comment_parameters( $params, Jetpack_Options::get_option( 'blog_token' ) );
277
		if ( is_wp_error( $signature ) ) {
278
			$signature = 'error';
279
		}
280
281
		$params['sig']    = $signature;
282
		$url_origin       = set_url_scheme( 'http://jetpack.wordpress.com' );
283
		$url              = "{$url_origin}/jetpack-comment/?" . http_build_query( $params );
284
		$url              = "{$url}#parent=" . urlencode( set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) );
285
		$this->signed_url = $url;
286
		$height           = $params['comment_registration'] || is_user_logged_in() ? '315' : '430'; // Iframe can be shorter if we're not allowing guest commenting
287
		$transparent      = ( $params['color_scheme'] == 'transparent' ) ? 'true' : 'false';
288
289
		if ( isset( $_GET['replytocom'] ) ) {
290
			$url .= '&replytocom=' . (int) $_GET['replytocom'];
291
		}
292
293
		// The actual iframe (loads comment form from Jetpack server)
294
		?>
295
296
		<div id="respond" class="comment-respond">
297
			<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>
298
			<div id="commentform" class="comment-form">
299
				<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>
300
			</div>
301
		</div>
302
303
		<?php // Below is required for comment reply JS to work ?>
304
305
		<input type="hidden" name="comment_parent" id="comment_parent" value="" />
306
307
		<?php
308
	}
309
310
	/**
311
	 * Add some JS to wp_footer to watch for hierarchical reply parent change
312
	 *
313
	 * @since JetpackComments (1.4)
314
	 */
315
	public function watch_comment_parent() {
316
		$url_origin = set_url_scheme( 'http://jetpack.wordpress.com' );
317
	?>
318
319
		<!--[if IE]>
320
		<script type="text/javascript">
321
		if ( 0 === window.location.hash.indexOf( '#comment-' ) ) {
322
			// window.location.reload() doesn't respect the Hash in IE
323
			window.location.hash = window.location.hash;
324
		}
325
		</script>
326
		<![endif]-->
327
		<script type="text/javascript">
328
			var comm_par_el = document.getElementById( 'comment_parent' ),
329
			    comm_par = (comm_par_el && comm_par_el.value) ? comm_par_el.value : '',
330
			    frame = document.getElementById( 'jetpack_remote_comment' ),
331
			    tellFrameNewParent;
332
333
			tellFrameNewParent = function() {
334
				if ( comm_par ) {
335
					frame.src = "<?php echo esc_url_raw( $this->signed_url ); ?>" + '&replytocom=' + parseInt( comm_par, 10 ).toString();
336
				} else {
337
					frame.src = "<?php echo esc_url_raw( $this->signed_url ); ?>";
338
				}
339
			};
340
341
	<?php if ( get_option( 'thread_comments' ) && get_option( 'thread_comments_depth' ) ) : ?>
342
343
			if ( 'undefined' !== typeof addComment ) {
344
				addComment._Jetpack_moveForm = addComment.moveForm;
345
346
				addComment.moveForm = function( commId, parentId, respondId, postId ) {
347
					var returnValue = addComment._Jetpack_moveForm( commId, parentId, respondId, postId ), cancelClick, cancel;
348
349
					if ( false === returnValue ) {
350
						cancel = document.getElementById( 'cancel-comment-reply-link' );
351
						cancelClick = cancel.onclick;
352
						cancel.onclick = function() {
353
							var cancelReturn = cancelClick.call( this );
354
							if ( false !== cancelReturn ) {
355
								return cancelReturn;
356
							}
357
358
							if ( !comm_par ) {
359
								return cancelReturn;
360
							}
361
362
							comm_par = 0;
363
364
							tellFrameNewParent();
365
366
							return cancelReturn;
367
						};
368
					}
369
370
					if ( comm_par == parentId ) {
371
						return returnValue;
372
					}
373
374
					comm_par = parentId;
375
376
					tellFrameNewParent();
377
378
					return returnValue;
379
				};
380
			}
381
382
	<?php endif; ?>
383
384
			if ( window.postMessage ) {
385
				if ( document.addEventListener ) {
386
					window.addEventListener( 'message', function( event ) {
387
						if ( <?php echo json_encode( esc_url_raw( $url_origin ) ); ?> !== event.origin ) {
388
							return;
389
						}
390
391
						jQuery( frame ).height( event.data );
392
					} );
393
				} else if ( document.attachEvent ) {
394
					window.attachEvent( 'message', function( event ) {
395
						if ( <?php echo json_encode( esc_url_raw( $url_origin ) ); ?> !== event.origin ) {
396
							return;
397
						}
398
399
						jQuery( frame ).height( event.data );
400
					} );
401
				}
402
			}
403
		</script>
404
405
	<?php
406
	}
407
408
	/**
409
	 * Verify the hash included in remote comments.
410
	 *
411
	 * @since JetpackComments (1.4)
412
	 * @param type $comment Not used
413
	 */
414
	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...
415
		$post_array = stripslashes_deep( $_POST );
416
417
		// Bail if missing the Jetpack token
418
		if ( ! isset( $post_array['sig'] ) ) {
419
			unset( $_POST['hc_post_as'] );
420
			return;
421
		}
422
423
		if ( FALSE !== strpos( $post_array['hc_avatar'], '.gravatar.com' ) )
424
			$post_array['hc_avatar'] = htmlentities( $post_array['hc_avatar'] );
425
426
		$check = Jetpack_Comments::sign_remote_comment_parameters( $post_array, Jetpack_Options::get_option( 'blog_token' ) );
427
		if ( is_wp_error( $check ) ) {
428
			wp_die( $check );
429
		}
430
431
		// Bail if token is expired or not valid
432
		if ( $check !== $post_array['sig'] )
433
			wp_die( __( 'Invalid security token.', 'jetpack' ) );
434
435
		/** This filter is documented in modules/comments/comments.php */
436
		if ( ! apply_filters( 'jetpack_comment_form_enabled_for_' . get_post_type( $post_array['comment_post_ID'] ), true ) ) {
437
			// In case the comment POST is legit, but the comments are
438
			// now disabled, we don't allow the comment
439
440
			wp_die( __( 'Comments are not allowed.', 'jetpack' ) );
441
		}
442
	}
443
444
	/** Capabilities **********************************************************/
445
446
	/**
447
	 * Add some additional comment meta after comment is saved about what
448
	 * service the comment is from, the avatar, user_id, etc...
449
	 *
450
	 * @since JetpackComments (1.4)
451
	 * @param type $comment_id
452
	 */
453
	public function add_comment_meta( $comment_id ) {
454
		$comment_meta = array();
455
456
		switch( $this->is_highlander_comment_post() ) {
457 View Code Duplication
			case 'facebook' :
458
				$comment_meta['hc_post_as']         = 'facebook';
459
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
460
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
461
				break;
462
463 View Code Duplication
			case 'twitter' :
464
				$comment_meta['hc_post_as']         = 'twitter';
465
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
466
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
467
				break;
468
469
			case 'wordpress' :
470
				$comment_meta['hc_post_as']         = 'wordpress';
471
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
472
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
473
				$comment_meta['hc_wpcom_id_sig']    = stripslashes( $_POST['hc_wpcom_id_sig'] ); //since 1.9
474
				break;
475
476 View Code Duplication
			case 'jetpack' :
477
				$comment_meta['hc_post_as']         = 'jetpack';
478
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
479
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
480
				break;
481
482
		}
483
484
		// Bail if no extra comment meta
485
		if ( empty( $comment_meta ) )
486
			return;
487
488
		// Loop through extra meta and add values
489
		foreach ( $comment_meta as $key => $value )
490
			add_comment_meta( $comment_id, $key, $value, true );
491
	}
492
	function capture_comment_post_redirect_to_reload_parent_frame( $url ) {
493
		if ( !isset( $_GET['for'] ) || 'jetpack' != $_GET['for'] ) {
494
			return $url;
495
		}
496
?>
497
<!DOCTYPE html>
498
<html <?php language_attributes(); ?>>
499
<!--<![endif]-->
500
<head>
501
<meta charset="<?php bloginfo( 'charset' ); ?>" />
502
<title><?php printf( __( 'Submitting Comment%s', 'jetpack' ), '&hellip;' ); ?></title>
503
<style type="text/css">
504
body {
505
	display: table;
506
	width: 100%;
507
	height: 60%;
508
	position: absolute;
509
	top: 0;
510
	left: 0;
511
	overflow: hidden;
512
	color: #333;
513
}
514
515
h1 {
516
	text-align: center;
517
	margin: 0;
518
	padding: 0;
519
	display: table-cell;
520
	vertical-align: middle;
521
	font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", sans-serif;
522
	font-weight: normal;
523
}
524
525
.hidden {
526
	opacity: 0;
527
}
528
529
h1 span {
530
	-moz-transition-property: opacity;
531
	-moz-transition-duration: 1s;
532
	-moz-transition-timing-function: ease-in-out;
533
534
	-webkit-transition-property: opacity;
535
	-webkit-transition-duration: 1s;
536
	-webbit-transition-timing-function: ease-in-out;
537
538
	-o-transition-property: opacity;
539
	-o-transition-duration: 1s;
540
	-o-transition-timing-function: ease-in-out;
541
542
	-ms-transition-property: opacity;
543
	-ms-transition-duration: 1s;
544
	-ms-transition-timing-function: ease-in-out;
545
546
	transition-property: opacity;
547
	transition-duration: 1s;
548
	transition-timing-function: ease-in-out;
549
}
550
</style>
551
</head>
552
<body>
553
	<h1><?php printf( __( 'Submitting Comment%s', 'jetpack' ), '<span id="ellipsis" class="hidden">&hellip;</span>' ); ?></h1>
554
<script type="text/javascript">
555
try {
556
	window.parent.location = <?php echo json_encode( $url ); ?>;
557
	window.parent.location.reload( true );
558
} catch ( e ) {
559
	window.location = <?php echo json_encode( $url ); ?>;
560
	window.location.reload( true );
561
}
562
ellipsis = document.getElementById( 'ellipsis' );
563
function toggleEllipsis() {
564
	ellipsis.className = ellipsis.className ? '' : 'hidden';
565
}
566
setInterval( toggleEllipsis, 1200 );
567
</script>
568
</body>
569
</html>
570
<?php
571
		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...
572
	}
573
}
574
575
Jetpack_Comments::init();
576