Completed
Push — branch-4.0 ( fc0d6f...bf547c )
by
unknown
08:22
created

Jetpack_Comments::comment_form_before()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 20
rs 9.4285
c 0
b 0
f 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_locale(),
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              = "https://jetpack.wordpress.com/jetpack-comment/?" . http_build_query( $params );
283
		$url              = "{$url}#parent=" . urlencode( set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) );
284
		$this->signed_url = $url;
285
		$height           = $params['comment_registration'] || is_user_logged_in() ? '315' : '430'; // Iframe can be shorter if we're not allowing guest commenting
286
		$transparent      = ( $params['color_scheme'] == 'transparent' ) ? 'true' : 'false';
287
288
		if ( isset( $_GET['replytocom'] ) ) {
289
			$url .= '&replytocom=' . (int) $_GET['replytocom'];
290
		}
291
292
		// The actual iframe (loads comment form from Jetpack server)
293
		?>
294
295
		<div id="respond" class="comment-respond">
296
			<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>
297
			<form id="commentform" class="comment-form">
298
				<iframe src="<?php echo esc_url( $url ); ?>" allowtransparency="<?php echo $transparent; ?>" style="width:100%; height: <?php echo $height; ?>px;border:0;" frameBorder="0" scrolling="no" name="jetpack_remote_comment" id="jetpack_remote_comment"></iframe>
299
			</form>
300
		</div>
301
302
		<?php // Below is required for comment reply JS to work ?>
303
304
		<input type="hidden" name="comment_parent" id="comment_parent" value="" />
305
306
		<?php
307
	}
308
309
	/**
310
	 * Add some JS to wp_footer to watch for hierarchical reply parent change
311
	 *
312
	 * @since JetpackComments (1.4)
313
	 */
314
	public function watch_comment_parent() {
315
		$url_origin = 'https://jetpack.wordpress.com';
316
	?>
317
318
		<!--[if IE]>
319
		<script type="text/javascript">
320
		if ( 0 === window.location.hash.indexOf( '#comment-' ) ) {
321
			// window.location.reload() doesn't respect the Hash in IE
322
			window.location.hash = window.location.hash;
323
		}
324
		</script>
325
		<![endif]-->
326
		<script type="text/javascript">
327
			var comm_par_el = document.getElementById( 'comment_parent' ),
328
			    comm_par = (comm_par_el && comm_par_el.value) ? comm_par_el.value : '',
329
			    frame = document.getElementById( 'jetpack_remote_comment' ),
330
			    tellFrameNewParent;
331
332
			tellFrameNewParent = function() {
333
				if ( comm_par ) {
334
					frame.src = "<?php echo esc_url_raw( $this->signed_url ); ?>" + '&replytocom=' + parseInt( comm_par, 10 ).toString();
335
				} else {
336
					frame.src = "<?php echo esc_url_raw( $this->signed_url ); ?>";
337
				}
338
			};
339
340
	<?php if ( get_option( 'thread_comments' ) && get_option( 'thread_comments_depth' ) ) : ?>
341
342
			if ( 'undefined' !== typeof addComment ) {
343
				addComment._Jetpack_moveForm = addComment.moveForm;
344
345
				addComment.moveForm = function( commId, parentId, respondId, postId ) {
346
					var returnValue = addComment._Jetpack_moveForm( commId, parentId, respondId, postId ), cancelClick, cancel;
347
348
					if ( false === returnValue ) {
349
						cancel = document.getElementById( 'cancel-comment-reply-link' );
350
						cancelClick = cancel.onclick;
351
						cancel.onclick = function() {
352
							var cancelReturn = cancelClick.call( this );
353
							if ( false !== cancelReturn ) {
354
								return cancelReturn;
355
							}
356
357
							if ( !comm_par ) {
358
								return cancelReturn;
359
							}
360
361
							comm_par = 0;
362
363
							tellFrameNewParent();
364
365
							return cancelReturn;
366
						};
367
					}
368
369
					if ( comm_par == parentId ) {
370
						return returnValue;
371
					}
372
373
					comm_par = parentId;
374
375
					tellFrameNewParent();
376
377
					return returnValue;
378
				};
379
			}
380
381
	<?php endif; ?>
382
383
			if ( window.postMessage ) {
384
				if ( document.addEventListener ) {
385
					window.addEventListener( 'message', function( event ) {
386
						if ( <?php echo json_encode( esc_url_raw( $url_origin ) ); ?> !== event.origin ) {
387
							return;
388
						}
389
390
						jQuery( frame ).height( event.data );
391
					} );
392
				} else if ( document.attachEvent ) {
393
					window.attachEvent( 'message', function( event ) {
394
						if ( <?php echo json_encode( esc_url_raw( $url_origin ) ); ?> !== event.origin ) {
395
							return;
396
						}
397
398
						jQuery( frame ).height( event.data );
399
					} );
400
				}
401
			}
402
		</script>
403
404
	<?php
405
	}
406
407
	/**
408
	 * Verify the hash included in remote comments.
409
	 *
410
	 * @since JetpackComments (1.4)
411
	 * @param type $comment Not used
412
	 */
413
	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...
414
		$post_array = stripslashes_deep( $_POST );
415
416
		// Bail if missing the Jetpack token
417
		if ( ! isset( $post_array['sig'] ) ) {
418
			unset( $_POST['hc_post_as'] );
419
			return;
420
		}
421
422
		if ( FALSE !== strpos( $post_array['hc_avatar'], '.gravatar.com' ) )
423
			$post_array['hc_avatar'] = htmlentities( $post_array['hc_avatar'] );
424
425
		$check = Jetpack_Comments::sign_remote_comment_parameters( $post_array, Jetpack_Options::get_option( 'blog_token' ) );
426
		if ( is_wp_error( $check ) ) {
427
			wp_die( $check );
428
		}
429
430
		// Bail if token is expired or not valid
431
		if ( $check !== $post_array['sig'] )
432
			wp_die( __( 'Invalid security token.', 'jetpack' ) );
433
434
		/** This filter is documented in modules/comments/comments.php */
435
		if ( ! apply_filters( 'jetpack_comment_form_enabled_for_' . get_post_type( $post_array['comment_post_ID'] ), true ) ) {
436
			// In case the comment POST is legit, but the comments are
437
			// now disabled, we don't allow the comment
438
439
			wp_die( __( 'Comments are not allowed.', 'jetpack' ) );
440
		}
441
	}
442
443
	/** Capabilities **********************************************************/
444
445
	/**
446
	 * Add some additional comment meta after comment is saved about what
447
	 * service the comment is from, the avatar, user_id, etc...
448
	 *
449
	 * @since JetpackComments (1.4)
450
	 * @param type $comment_id
451
	 */
452
	public function add_comment_meta( $comment_id ) {
453
		$comment_meta = array();
454
455
		switch( $this->is_highlander_comment_post() ) {
456 View Code Duplication
			case 'facebook' :
457
				$comment_meta['hc_post_as']         = 'facebook';
458
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
459
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
460
				break;
461
462 View Code Duplication
			case 'twitter' :
463
				$comment_meta['hc_post_as']         = 'twitter';
464
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
465
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
466
				break;
467
468
			case 'wordpress' :
469
				$comment_meta['hc_post_as']         = 'wordpress';
470
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
471
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
472
				$comment_meta['hc_wpcom_id_sig']    = stripslashes( $_POST['hc_wpcom_id_sig'] ); //since 1.9
473
				break;
474
475 View Code Duplication
			case 'jetpack' :
476
				$comment_meta['hc_post_as']         = 'jetpack';
477
				$comment_meta['hc_avatar']          = stripslashes( $_POST['hc_avatar'] );
478
				$comment_meta['hc_foreign_user_id'] = stripslashes( $_POST['hc_userid'] );
479
				break;
480
481
		}
482
483
		// Bail if no extra comment meta
484
		if ( empty( $comment_meta ) )
485
			return;
486
487
		// Loop through extra meta and add values
488
		foreach ( $comment_meta as $key => $value )
489
			add_comment_meta( $comment_id, $key, $value, true );
490
	}
491
	function capture_comment_post_redirect_to_reload_parent_frame( $url ) {
492
		if ( !isset( $_GET['for'] ) || 'jetpack' != $_GET['for'] ) {
493
			return $url;
494
		}
495
?>
496
<!DOCTYPE html>
497
<html <?php language_attributes(); ?>>
498
<!--<![endif]-->
499
<head>
500
<meta charset="<?php bloginfo( 'charset' ); ?>" />
501
<title><?php printf( __( 'Submitting Comment%s', 'jetpack' ), '&hellip;' ); ?></title>
502
<style type="text/css">
503
body {
504
	display: table;
505
	width: 100%;
506
	height: 60%;
507
	position: absolute;
508
	top: 0;
509
	left: 0;
510
	overflow: hidden;
511
	color: #333;
512
}
513
514
h1 {
515
	text-align: center;
516
	margin: 0;
517
	padding: 0;
518
	display: table-cell;
519
	vertical-align: middle;
520
	font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", sans-serif;
521
	font-weight: normal;
522
}
523
524
.hidden {
525
	opacity: 0;
526
}
527
528
h1 span {
529
	-moz-transition-property: opacity;
530
	-moz-transition-duration: 1s;
531
	-moz-transition-timing-function: ease-in-out;
532
533
	-webkit-transition-property: opacity;
534
	-webkit-transition-duration: 1s;
535
	-webbit-transition-timing-function: ease-in-out;
536
537
	-o-transition-property: opacity;
538
	-o-transition-duration: 1s;
539
	-o-transition-timing-function: ease-in-out;
540
541
	-ms-transition-property: opacity;
542
	-ms-transition-duration: 1s;
543
	-ms-transition-timing-function: ease-in-out;
544
545
	transition-property: opacity;
546
	transition-duration: 1s;
547
	transition-timing-function: ease-in-out;
548
}
549
</style>
550
</head>
551
<body>
552
	<h1><?php printf( __( 'Submitting Comment%s', 'jetpack' ), '<span id="ellipsis" class="hidden">&hellip;</span>' ); ?></h1>
553
<script type="text/javascript">
554
try {
555
	window.parent.location = <?php echo json_encode( $url ); ?>;
556
	window.parent.location.reload( true );
557
} catch ( e ) {
558
	window.location = <?php echo json_encode( $url ); ?>;
559
	window.location.reload( true );
560
}
561
ellipsis = document.getElementById( 'ellipsis' );
562
function toggleEllipsis() {
563
	ellipsis.className = ellipsis.className ? '' : 'hidden';
564
}
565
setInterval( toggleEllipsis, 1200 );
566
</script>
567
</body>
568
</html>
569
<?php
570
		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...
571
	}
572
}
573
574
Jetpack_Comments::init();
575