Completed
Push — jetpack-fusion-mock-files ( e51750...3b1561 )
by
unknown
13:31
created

Jetpack_RelatedPosts::_enqueue_assets()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 8
nop 2
dl 0
loc 29
rs 8.5806
c 0
b 0
f 0
1
<?php
2
class Jetpack_RelatedPosts {
3
	const VERSION = '20150408';
4
	const SHORTCODE = 'jetpack-related-posts';
5
	private static $instance = null;
6
	private static $instance_raw = null;
7
8
	/**
9
	 * Creates and returns a static instance of Jetpack_RelatedPosts.
10
	 *
11
	 * @return Jetpack_RelatedPosts
12
	 */
13 View Code Duplication
	public static function init() {
14
		if ( ! self::$instance ) {
15
			if ( class_exists('WPCOM_RelatedPosts') && method_exists( 'WPCOM_RelatedPosts', 'init' ) ) {
16
				self::$instance = WPCOM_RelatedPosts::init();
17
			} else {
18
				self::$instance = new Jetpack_RelatedPosts(
19
					get_current_blog_id(),
20
					Jetpack_Options::get_option( 'id' )
21
				);
22
			}
23
		}
24
25
		return self::$instance;
26
	}
27
28
	/**
29
	 * Creates and returns a static instance of Jetpack_RelatedPosts_Raw.
30
	 *
31
	 * @return Jetpack_RelatedPosts
32
	 */
33 View Code Duplication
	public static function init_raw() {
34
		if ( ! self::$instance_raw ) {
35
			if ( class_exists('WPCOM_RelatedPosts') && method_exists( 'WPCOM_RelatedPosts', 'init_raw' ) ) {
36
				self::$instance_raw = WPCOM_RelatedPosts::init_raw();
37
			} else {
38
				self::$instance_raw = new Jetpack_RelatedPosts_Raw(
39
					get_current_blog_id(),
40
					Jetpack_Options::get_option( 'id' )
41
				);
42
			}
43
		}
44
45
		return self::$instance_raw;
46
	}
47
48
	protected $_blog_id_local;
49
	protected $_blog_id_wpcom;
50
	protected $_options;
51
	protected $_allow_feature_toggle;
52
	protected $_blog_charset;
53
	protected $_convert_charset;
54
	protected $_previous_post_id;
55
	protected $_found_shortcode = false;
56
57
	/**
58
	 * Constructor for Jetpack_RelatedPosts.
59
	 *
60
	 * @param int $blog_id_local
61
	 * @param int $blog_id_wpcom
62
	 * @uses get_option, add_action, apply_filters
63
	 * @return null
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
64
	 */
65
	public function __construct( $blog_id_local, $blog_id_wpcom ) {
66
		$this->_blog_id_local = $blog_id_local;
67
		$this->_blog_id_wpcom = $blog_id_wpcom;
68
		$this->_blog_charset = get_option( 'blog_charset' );
69
		$this->_convert_charset = ( function_exists( 'iconv' ) && ! preg_match( '/^utf\-?8$/i', $this->_blog_charset ) );
70
71
		add_action( 'admin_init', array( $this, 'action_admin_init' ) );
72
		add_action( 'wp', array( $this, 'action_frontend_init' ) );
73
74
		if ( ! class_exists( 'Jetpack_Media_Summary' ) ) {
75
			jetpack_require_lib( 'class.media-summary' );
76
		}
77
78
		// Add Related Posts to the REST API Post response.
79
		if ( function_exists( 'register_rest_field' ) ) {
80
			add_action( 'rest_api_init',  array( $this, 'rest_register_related_posts' ) );
81
		}
82
	}
83
84
	/**
85
	 * =================
86
	 * ACTIONS & FILTERS
87
	 * =================
88
	 */
89
90
	/**
91
	 * Add a checkbox field to Settings > Reading for enabling related posts.
92
	 *
93
	 * @action admin_init
94
	 * @uses add_settings_field, __, register_setting, add_action
95
	 * @return null
96
	 */
97
	public function action_admin_init() {
98
99
		// Add the setting field [jetpack_relatedposts] and place it in Settings > Reading
100
		add_settings_field( 'jetpack_relatedposts', '<span id="jetpack_relatedposts">' . __( 'Related posts', 'jetpack' ) . '</span>', array( $this, 'print_setting_html' ), 'reading' );
101
		register_setting( 'reading', 'jetpack_relatedposts', array( $this, 'parse_options' ) );
102
		add_action('admin_head', array( $this, 'print_setting_head' ) );
103
104
		if( 'options-reading.php' == $GLOBALS['pagenow'] ) {
105
			// Enqueue style for live preview on the reading settings page
106
			$this->_enqueue_assets( false, true );
107
		}
108
	}
109
110
	/**
111
	 * Load related posts assets if it's a elegiable front end page or execute search and return JSON if it's an endpoint request.
112
	 *
113
	 * @global $_GET
114
	 * @action wp
115
	 * @uses add_shortcode, get_the_ID
116
	 * @returns null
117
	 */
118
	public function action_frontend_init() {
119
		// Add a shortcode handler that outputs nothing, this gets overridden later if we can display related content
120
		add_shortcode( self::SHORTCODE, array( $this, 'get_target_html_unsupported' ) );
121
122
		if ( ! $this->_enabled_for_request() )
123
			return;
124
125
		if ( isset( $_GET['relatedposts'] ) ) {
126
			$excludes = array();
127
			if ( isset( $_GET['relatedposts_exclude'] ) ) {
128
				if ( is_string( $_GET['relatedposts_exclude'] ) ) {
129
					$excludes = explode( ',', $_GET['relatedposts_exclude'] );
130
				} elseif ( is_array( $_GET['relatedposts_exclude'] ) ) {
131
					$excludes = array_values( $_GET['relatedposts_exclude'] );
132
				}
133
134
				$excludes = array_unique( array_filter( array_map( 'absint', $excludes ) ) );
135
			}
136
137
			$this->_action_frontend_init_ajax( $excludes );
138
		} else {
139
			if ( isset( $_GET['relatedposts_hit'], $_GET['relatedposts_origin'], $_GET['relatedposts_position'] ) ) {
140
				$this->_log_click( $_GET['relatedposts_origin'], get_the_ID(), $_GET['relatedposts_position'] );
141
				$this->_previous_post_id = (int) $_GET['relatedposts_origin'];
142
			}
143
144
			$this->_action_frontend_init_page();
145
		}
146
147
	}
148
149
	/**
150
	 * Render insertion point.
151
	 *
152
	 * @since 4.2.0
153
	 *
154
	 * @return string
155
	 */
156
	public function get_headline() {
157
		$options = $this->get_options();
158
159
		if ( $options['show_headline'] ) {
160
			$headline = sprintf(
161
				/** This filter is already documented in modules/sharedaddy/sharing-service.php */
162
				apply_filters( 'jetpack_sharing_headline_html', '<h3 class="jp-relatedposts-headline"><em>%s</em></h3>', esc_html( $options['headline'] ), 'related-posts' ),
163
				esc_html( $options['headline'] )
164
			);
165
		} else {
166
			$headline = '';
167
		}
168
		return $headline;
169
	}
170
171
	/**
172
	 * Adds a target to the post content to load related posts into if a shortcode for it did not already exist.
173
	 *
174
	 * @filter the_content
175
	 * @param string $content
176
	 * @returns string
177
	 */
178
	public function filter_add_target_to_dom( $content ) {
179
		if ( !$this->_found_shortcode ) {
180
			$content .= "\n" . $this->get_target_html();
181
		}
182
183
		return $content;
184
	}
185
186
	/**
187
	 * Looks for our shortcode on the unfiltered content, this has to execute early.
188
	 *
189
	 * @filter the_content
190
	 * @param string $content
191
	 * @uses has_shortcode
192
	 * @returns string
193
	 */
194
	public function test_for_shortcode( $content ) {
195
		$this->_found_shortcode = has_shortcode( $content, self::SHORTCODE );
196
197
		return $content;
198
	}
199
200
	/**
201
	 * Returns the HTML for the related posts section.
202
	 *
203
	 * @uses esc_html__, apply_filters
204
	 * @returns string
205
	 */
206
	public function get_target_html() {
207
		require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php';
208
		if ( Jetpack_Sync_Settings::is_syncing() ) {
209
			return '';
210
		}
211
212
		/**
213
		 * Filter the Related Posts headline.
214
		 *
215
		 * @module related-posts
216
		 *
217
		 * @since 3.0.0
218
		 *
219
		 * @param string $headline Related Posts heading.
220
		 */
221
		$headline = apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() );
222
223
		if ( $this->_previous_post_id ) {
224
			$exclude = "data-exclude='{$this->_previous_post_id}'";
225
		} else {
226
			$exclude = "";
227
		}
228
229
		return <<<EOT
230
<div id='jp-relatedposts' class='jp-relatedposts' $exclude>
231
	$headline
232
</div>
233
EOT;
234
	}
235
236
	/**
237
	 * Returns the HTML for the related posts section if it's running in the loop or other instances where we don't support related posts.
238
	 *
239
	 * @returns string
240
	 */
241
	public function get_target_html_unsupported() {
242
		require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php';
243
		if ( Jetpack_Sync_Settings::is_syncing() ) {
244
			return '';
245
		}
246
		return "\n\n<!-- Jetpack Related Posts is not supported in this context. -->\n\n";
247
	}
248
249
	/**
250
	 * ========================
251
	 * PUBLIC UTILITY FUNCTIONS
252
	 * ========================
253
	 */
254
255
	/**
256
	 * Gets options set for Jetpack_RelatedPosts and merge with defaults.
257
	 *
258
	 * @uses Jetpack_Options::get_option, apply_filters
259
	 * @return array
260
	 */
261
	public function get_options() {
262
		if ( null === $this->_options ) {
263
			$this->_options = Jetpack_Options::get_option( 'relatedposts', array() );
264
			if ( ! is_array( $this->_options ) )
265
				$this->_options = array();
266
			if ( ! isset( $this->_options['enabled'] ) )
267
				$this->_options['enabled'] = true;
268
			if ( ! isset( $this->_options['show_headline'] ) )
269
				$this->_options['show_headline'] = true;
270
			if ( ! isset( $this->_options['show_thumbnails'] ) )
271
				$this->_options['show_thumbnails'] = false;
272
			if ( ! isset( $this->_options['show_date'] ) ) {
273
				$this->_options['show_date'] = true;
274
			}
275
			if ( ! isset( $this->_options['show_context'] ) ) {
276
				$this->_options['show_context'] = true;
277
			}
278
			if ( ! isset( $this->_options['layout'] ) ) {
279
				$this->_options['layout'] = 'grid';
280
			}
281
			if ( ! isset( $this->_options['headline'] ) ) {
282
				$this->_options['headline'] = esc_html__( 'Related', 'jetpack' );
283
			}
284
			if ( empty( $this->_options['size'] ) || (int)$this->_options['size'] < 1 )
285
				$this->_options['size'] = 3;
286
287
			/**
288
			 * Filter Related Posts basic options.
289
			 *
290
			 * @module related-posts
291
			 *
292
			 * @since 2.8.0
293
			 *
294
			 * @param array $this->_options Array of basic Related Posts options.
295
			 */
296
			$this->_options = apply_filters( 'jetpack_relatedposts_filter_options', $this->_options );
297
		}
298
299
		return $this->_options;
300
	}
301
302
	public function get_option( $option_name ) {
303
		$options = $this->get_options();
304
305
		if ( isset( $options[ $option_name ] ) ) {
306
			return $options[ $option_name ];
307
		}
308
309
		return false;
310
	}
311
312
	/**
313
	 * Parses input and returns normalized options array.
314
	 *
315
	 * @param array $input
316
	 * @uses self::get_options
317
	 * @return array
318
	 */
319
	public function parse_options( $input ) {
320
		$current = $this->get_options();
321
322
		if ( !is_array( $input ) )
323
			$input = array();
324
325
		if (
326
			! isset( $input['enabled'] )
327
			|| isset( $input['show_date'] )
328
			|| isset( $input['show_context'] )
329
			|| isset( $input['layout'] )
330
			|| isset( $input['headline'] )
331
			) {
332
			$input['enabled'] = '1';
333
		}
334
335
		if ( '1' == $input['enabled'] ) {
336
			$current['enabled'] = true;
337
			$current['show_headline'] = ( isset( $input['show_headline'] ) && '1' == $input['show_headline'] );
338
			$current['show_thumbnails'] = ( isset( $input['show_thumbnails'] ) && '1' == $input['show_thumbnails'] );
339
			$current['show_date'] = ( isset( $input['show_date'] ) && '1' == $input['show_date'] );
340
			$current['show_context'] = ( isset( $input['show_context'] ) && '1' == $input['show_context'] );
341
			$current['layout'] = isset( $input['layout'] ) && in_array( $input['layout'], array( 'grid', 'list' ), true ) ? $input['layout'] : 'grid';
342
			$current['headline'] = isset( $input['headline'] ) ? $input['headline'] : esc_html__( 'Related', 'jetpack' );
343
		} else {
344
			$current['enabled'] = false;
345
		}
346
347
		if ( isset( $input['size'] ) && (int)$input['size'] > 0 )
348
			$current['size'] = (int)$input['size'];
349
		else
350
			$current['size'] = null;
351
352
		return $current;
353
	}
354
355
	/**
356
	 * HTML for admin settings page.
357
	 *
358
	 * @uses self::get_options, checked, esc_html__
359
	 * @returns null
360
	 */
361
	public function print_setting_html() {
362
		$options = $this->get_options();
363
364
		$ui_settings_template = <<<EOT
365
<ul id="settings-reading-relatedposts-customize">
366
	<li>
367
		<label><input name="jetpack_relatedposts[show_headline]" type="checkbox" value="1" %s /> %s</label>
368
	</li>
369
	<li>
370
		<label><input name="jetpack_relatedposts[show_thumbnails]" type="checkbox" value="1" %s /> %s</label>
371
	</li>
372
	<li>
373
		<label><input name="jetpack_relatedposts[show_date]" type="checkbox" value="1" %s /> %s</label>
374
	</li>
375
	<li>
376
		<label><input name="jetpack_relatedposts[show_context]" type="checkbox" value="1" %s /> %s</label>
377
	</li>
378
</ul>
379
<div id='settings-reading-relatedposts-preview'>
380
	%s
381
	<div id="jp-relatedposts" class="jp-relatedposts"></div>
382
</div>
383
EOT;
384
		$ui_settings = sprintf(
385
			$ui_settings_template,
386
			checked( $options['show_headline'], true, false ),
387
			esc_html__( 'Show a "Related" header to more clearly separate the related section from posts', 'jetpack' ),
388
			checked( $options['show_thumbnails'], true, false ),
389
			esc_html__( 'Use a large and visually striking layout', 'jetpack' ),
390
			checked( $options['show_date'], true, false ),
391
			esc_html__( 'Show entry date', 'jetpack' ),
392
			checked( $options['show_context'], true, false ),
393
			esc_html__( 'Show context (category or tag)', 'jetpack' ),
394
			esc_html__( 'Preview:', 'jetpack' )
395
		);
396
397
		if ( !$this->_allow_feature_toggle() ) {
398
			$template = <<<EOT
399
<input type="hidden" name="jetpack_relatedposts[enabled]" value="1" />
400
%s
401
EOT;
402
			printf(
403
				$template,
404
				$ui_settings
405
			);
406
		} else {
407
			$template = <<<EOT
408
<ul id="settings-reading-relatedposts">
409
	<li>
410
		<label><input type="radio" name="jetpack_relatedposts[enabled]" value="0" class="tog" %s /> %s</label>
411
	</li>
412
	<li>
413
		<label><input type="radio" name="jetpack_relatedposts[enabled]" value="1" class="tog" %s /> %s</label>
414
		%s
415
	</li>
416
</ul>
417
EOT;
418
			printf(
419
				$template,
420
				checked( $options['enabled'], false, false ),
421
				esc_html__( 'Hide related content after posts', 'jetpack' ),
422
				checked( $options['enabled'], true, false ),
423
				esc_html__( 'Show related content after posts', 'jetpack' ),
424
				$ui_settings
425
			);
426
		}
427
	}
428
429
	/**
430
	 * Head JS/CSS for admin settings page.
431
	 *
432
	 * @uses esc_html__
433
	 * @returns null
434
	 */
435
	public function print_setting_head() {
436
437
		// only dislay the Related Posts JavaScript on the Reading Settings Admin Page
438
		$current_screen =  get_current_screen();
439
440
		if ( is_null( $current_screen ) ) {
441
			return;
442
		}
443
444
		if( 'options-reading' != $current_screen->id )
445
			return;
446
447
		$related_headline = sprintf(
448
			'<h3 class="jp-relatedposts-headline"><em>%s</em></h3>',
449
			esc_html__( 'Related', 'jetpack' )
450
		);
451
452
		$href_params = 'class="jp-relatedposts-post-a" href="#jetpack_relatedposts" rel="nofollow" data-origin="0" data-position="0"';
453
		$related_with_images = <<<EOT
454
<div class="jp-relatedposts-items jp-relatedposts-items-visual">
455
	<div class="jp-relatedposts-post jp-relatedposts-post0 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image">
456
		<a $href_params>
457
			<img class="jp-relatedposts-post-img" src="https://jetpackme.files.wordpress.com/2014/08/1-wpios-ipad-3-1-viewsite.png?w=350&amp;h=200&amp;crop=1" width="350" alt="Big iPhone/iPad Update Now Available" scale="0">
458
		</a>
459
		<h4 class="jp-relatedposts-post-title">
460
			<a $href_params>Big iPhone/iPad Update Now Available</a>
461
		</h4>
462
		<p class="jp-relatedposts-post-excerpt">Big iPhone/iPad Update Now Available</p>
463
		<p class="jp-relatedposts-post-context">In "Mobile"</p>
464
	</div>
465
	<div class="jp-relatedposts-post jp-relatedposts-post1 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image">
466
		<a $href_params>
467
			<img class="jp-relatedposts-post-img" src="https://jetpackme.files.wordpress.com/2014/08/wordpress-com-news-wordpress-for-android-ui-update2.jpg?w=350&amp;h=200&amp;crop=1" width="350" alt="The WordPress for Android App Gets a Big Facelift" scale="0">
468
		</a>
469
		<h4 class="jp-relatedposts-post-title">
470
			<a $href_params>The WordPress for Android App Gets a Big Facelift</a>
471
		</h4>
472
		<p class="jp-relatedposts-post-excerpt">The WordPress for Android App Gets a Big Facelift</p>
473
		<p class="jp-relatedposts-post-context">In "Mobile"</p>
474
	</div>
475
	<div class="jp-relatedposts-post jp-relatedposts-post2 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image">
476
		<a $href_params>
477
			<img class="jp-relatedposts-post-img" src="https://jetpackme.files.wordpress.com/2014/08/videopresswedding.jpg?w=350&amp;h=200&amp;crop=1" width="350" alt="Upgrade Focus: VideoPress For Weddings" scale="0">
478
		</a>
479
		<h4 class="jp-relatedposts-post-title">
480
			<a $href_params>Upgrade Focus: VideoPress For Weddings</a>
481
		</h4>
482
		<p class="jp-relatedposts-post-excerpt">Upgrade Focus: VideoPress For Weddings</p>
483
		<p class="jp-relatedposts-post-context">In "Upgrade"</p>
484
	</div>
485
</div>
486
EOT;
487
		$related_with_images = str_replace( "\n", '', $related_with_images );
488
		$related_without_images = <<<EOT
489
<div class="jp-relatedposts-items jp-relatedposts-items-minimal">
490
	<p class="jp-relatedposts-post jp-relatedposts-post0" data-post-id="0" data-post-format="image">
491
		<span class="jp-relatedposts-post-title"><a $href_params>Big iPhone/iPad Update Now Available</a></span>
492
		<span class="jp-relatedposts-post-context">In "Mobile"</span>
493
	</p>
494
	<p class="jp-relatedposts-post jp-relatedposts-post1" data-post-id="0" data-post-format="image">
495
		<span class="jp-relatedposts-post-title"><a $href_params>The WordPress for Android App Gets a Big Facelift</a></span>
496
		<span class="jp-relatedposts-post-context">In "Mobile"</span>
497
	</p>
498
	<p class="jp-relatedposts-post jp-relatedposts-post2" data-post-id="0" data-post-format="image">
499
		<span class="jp-relatedposts-post-title"><a $href_params>Upgrade Focus: VideoPress For Weddings</a></span>
500
		<span class="jp-relatedposts-post-context">In "Upgrade"</span>
501
	</p>
502
</div>
503
EOT;
504
		$related_without_images = str_replace( "\n", '', $related_without_images );
505
506
		if ( $this->_allow_feature_toggle() ) {
507
			$extra_css = '#settings-reading-relatedposts-customize { padding-left:2em; margin-top:.5em; }';
508
		} else {
509
			$extra_css = '';
510
		}
511
512
		echo <<<EOT
513
<style type="text/css">
514
	#settings-reading-relatedposts .disabled { opacity:.5; filter:Alpha(opacity=50); }
515
	#settings-reading-relatedposts-preview .jp-relatedposts { background:#fff; padding:.5em; width:75%; }
516
	$extra_css
517
</style>
518
<script type="text/javascript">
519
	jQuery( document ).ready( function($) {
520
		var update_ui = function() {
521
			var is_enabled = true;
522
			if ( 'radio' == $( 'input[name="jetpack_relatedposts[enabled]"]' ).attr('type') ) {
523
				if ( '0' == $( 'input[name="jetpack_relatedposts[enabled]"]:checked' ).val() ) {
524
					is_enabled = false;
525
				}
526
			}
527
			if ( is_enabled ) {
528
				$( '#settings-reading-relatedposts-customize' )
529
					.removeClass( 'disabled' )
530
					.find( 'input' )
531
					.attr( 'disabled', false );
532
				$( '#settings-reading-relatedposts-preview' )
533
					.removeClass( 'disabled' );
534
			} else {
535
				$( '#settings-reading-relatedposts-customize' )
536
					.addClass( 'disabled' )
537
					.find( 'input' )
538
					.attr( 'disabled', true );
539
				$( '#settings-reading-relatedposts-preview' )
540
					.addClass( 'disabled' );
541
			}
542
		};
543
544
		var update_preview = function() {
545
			var html = '';
546
			if ( $( 'input[name="jetpack_relatedposts[show_headline]"]:checked' ).length ) {
547
				html += '$related_headline';
548
			}
549
			if ( $( 'input[name="jetpack_relatedposts[show_thumbnails]"]:checked' ).length ) {
550
				html += '$related_with_images';
551
			} else {
552
				html += '$related_without_images';
553
			}
554
			$( '#settings-reading-relatedposts-preview .jp-relatedposts' ).html( html );
555
			if ( $( 'input[name="jetpack_relatedposts[show_date]"]:checked' ).length ) {
556
				$( '.jp-relatedposts-post-title' ).each( function() {
557
					$( this ).after( $( '<span>August 8, 2005</span>' ) );
558
				} );
559
			}
560
			if ( $( 'input[name="jetpack_relatedposts[show_context]"]:checked' ).length ) {
561
				$( '.jp-relatedposts-post-context' ).show();
562
			} else {
563
				$( '.jp-relatedposts-post-context' ).hide();
564
			}
565
			$( '#settings-reading-relatedposts-preview .jp-relatedposts' ).show();
566
		};
567
568
		// Update on load
569
		update_preview();
570
		update_ui();
571
572
		// Update on change
573
		$( '#settings-reading-relatedposts-customize input' )
574
			.change( update_preview );
575
		$( '#settings-reading-relatedposts' )
576
			.find( 'input.tog' )
577
			.change( update_ui );
578
	});
579
</script>
580
EOT;
581
	}
582
583
	/**
584
	 * Gets an array of related posts that match the given post_id.
585
	 *
586
	 * @param int $post_id
587
	 * @param array $args - params to use when building Elasticsearch filters to narrow down the search domain.
588
	 * @uses self::get_options, get_post_type, wp_parse_args, apply_filters
589
	 * @return array
590
	 */
591
	public function get_for_post_id( $post_id, array $args ) {
592
		$options = $this->get_options();
593
594
		if ( ! empty( $args['size'] ) )
595
			$options['size'] = $args['size'];
596
597
		if ( ! $options['enabled'] || 0 == (int)$post_id || empty( $options['size'] ) )
598
			return array();
599
600
		$defaults = array(
601
			'size' => (int)$options['size'],
602
			'post_type' => get_post_type( $post_id ),
603
			'post_formats' => array(),
604
			'has_terms' => array(),
605
			'date_range' => array(),
606
			'exclude_post_ids' => array(),
607
		);
608
		$args = wp_parse_args( $args, $defaults );
609
		/**
610
		 * Filter the arguments used to retrieve a list of Related Posts.
611
		 *
612
		 * @module related-posts
613
		 *
614
		 * @since 2.8.0
615
		 *
616
		 * @param array $args Array of options to retrieve Related Posts.
617
		 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
618
		 */
619
		$args = apply_filters( 'jetpack_relatedposts_filter_args', $args, $post_id );
620
621
		$filters = $this->_get_es_filters_from_args( $post_id, $args );
622
		/**
623
		 * Filter Elasticsearch options used to calculate Related Posts.
624
		 *
625
		 * @module related-posts
626
		 *
627
		 * @since 2.8.0
628
		 *
629
		 * @param array $filters Array of Elasticsearch filters based on the post_id and args.
630
		 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
631
		 */
632
		$filters = apply_filters( 'jetpack_relatedposts_filter_filters', $filters, $post_id );
633
634
		$results = $this->_get_related_posts( $post_id, $args['size'], $filters );
635
		/**
636
		 * Filter the array of related posts matched by Elasticsearch.
637
		 *
638
		 * @module related-posts
639
		 *
640
		 * @since 2.8.0
641
		 *
642
		 * @param array $results Array of related posts matched by Elasticsearch.
643
		 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
644
		 */
645
		return apply_filters( 'jetpack_relatedposts_returned_results', $results, $post_id );
646
	}
647
648
	/**
649
	 * =========================
650
	 * PRIVATE UTILITY FUNCTIONS
651
	 * =========================
652
	 */
653
654
	/**
655
	 * Creates an array of Elasticsearch filters based on the post_id and args.
656
	 *
657
	 * @param int $post_id
658
	 * @param array $args
659
	 * @uses apply_filters, get_post_types, get_post_format_strings
660
	 * @return array
661
	 */
662
	protected function _get_es_filters_from_args( $post_id, array $args ) {
663
		$filters = array();
664
665
		/**
666
		 * Filter the terms used to search for Related Posts.
667
		 *
668
		 * @module related-posts
669
		 *
670
		 * @since 2.8.0
671
		 *
672
		 * @param array $args['has_terms'] Array of terms associated to the Related Posts.
673
		 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
674
		 */
675
		$args['has_terms'] = apply_filters( 'jetpack_relatedposts_filter_has_terms', $args['has_terms'], $post_id );
676
		if ( ! empty( $args['has_terms'] ) ) {
677
			foreach( (array)$args['has_terms'] as $term ) {
678
				if ( mb_strlen( $term->taxonomy ) ) {
679 View Code Duplication
					switch ( $term->taxonomy ) {
680
						case 'post_tag':
681
							$tax_fld = 'tag.slug';
682
							break;
683
						case 'category':
684
							$tax_fld = 'category.slug';
685
							break;
686
						default:
687
							$tax_fld = 'taxonomy.' . $term->taxonomy . '.slug';
688
							break;
689
					}
690
					$filters[] = array( 'term' => array( $tax_fld => $term->slug ) );
691
				}
692
			}
693
		}
694
695
		/**
696
		 * Filter the Post Types where we search Related Posts.
697
		 *
698
		 * @module related-posts
699
		 *
700
		 * @since 2.8.0
701
		 *
702
		 * @param array $args['post_type'] Array of Post Types.
703
		 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
704
		 */
705
		$args['post_type'] = apply_filters( 'jetpack_relatedposts_filter_post_type', $args['post_type'], $post_id );
706
		$valid_post_types = get_post_types();
707
		if ( is_array( $args['post_type'] ) ) {
708
			$sanitized_post_types = array();
709
			foreach ( $args['post_type'] as $pt ) {
710
				if ( in_array( $pt, $valid_post_types ) )
711
					$sanitized_post_types[] = $pt;
712
			}
713
			if ( ! empty( $sanitized_post_types ) )
714
				$filters[] = array( 'terms' => array( 'post_type' => $sanitized_post_types ) );
715
		} else if ( in_array( $args['post_type'], $valid_post_types ) && 'all' != $args['post_type'] ) {
716
			$filters[] = array( 'term' => array( 'post_type' => $args['post_type'] ) );
717
		}
718
719
		/**
720
		 * Filter the Post Formats where we search Related Posts.
721
		 *
722
		 * @module related-posts
723
		 *
724
		 * @since 3.3.0
725
		 *
726
		 * @param array $args['post_formats'] Array of Post Formats.
727
		 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
728
		 */
729
		$args['post_formats'] = apply_filters( 'jetpack_relatedposts_filter_post_formats', $args['post_formats'], $post_id );
730
		$valid_post_formats = get_post_format_strings();
731
		$sanitized_post_formats = array();
732
		foreach ( $args['post_formats'] as $pf ) {
733
			if ( array_key_exists( $pf, $valid_post_formats ) ) {
734
				$sanitized_post_formats[] = $pf;
735
			}
736
		}
737
		if ( ! empty( $sanitized_post_formats ) ) {
738
			$filters[] = array( 'terms' => array( 'post_format' => $sanitized_post_formats ) );
739
		}
740
741
		/**
742
		 * Filter the date range used to search Related Posts.
743
		 *
744
		 * @module related-posts
745
		 *
746
		 * @since 2.8.0
747
		 *
748
		 * @param array $args['date_range'] Array of a month interval where we search Related Posts.
749
		 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
750
		 */
751
		$args['date_range'] = apply_filters( 'jetpack_relatedposts_filter_date_range', $args['date_range'], $post_id );
752
		if ( is_array( $args['date_range'] ) && ! empty( $args['date_range'] ) ) {
753
			$args['date_range'] = array_map( 'intval', $args['date_range'] );
754
			if ( !empty( $args['date_range']['from'] ) && !empty( $args['date_range']['to'] ) ) {
755
				$filters[] = array(
756
					'range' => array(
757
						'date_gmt' => $this->_get_coalesced_range( $args['date_range'] ),
758
					)
759
				);
760
			}
761
		}
762
763
		/**
764
		 * Filter the Post IDs excluded from appearing in Related Posts.
765
		 *
766
		 * @module related-posts
767
		 *
768
		 * @since 2.9.0
769
		 *
770
		 * @param array $args['exclude_post_ids'] Array of Post IDs.
771
		 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
772
		 */
773
		$args['exclude_post_ids'] = apply_filters( 'jetpack_relatedposts_filter_exclude_post_ids', $args['exclude_post_ids'], $post_id );
774
		if ( !empty( $args['exclude_post_ids'] ) && is_array( $args['exclude_post_ids'] ) ) {
775
			$excluded_post_ids = array();
776
			foreach ( $args['exclude_post_ids'] as $exclude_post_id) {
777
				$exclude_post_id = (int)$exclude_post_id;
778
				if ( $exclude_post_id > 0 )
779
					$excluded_post_ids[] = $exclude_post_id;
780
			}
781
			$filters[] = array( 'not' => array( 'terms' => array( 'post_id' => $excluded_post_ids ) ) );
782
		}
783
784
		return $filters;
785
	}
786
787
	/**
788
	 * Takes a range and coalesces it into a month interval bracketed by a time as determined by the blog_id to enhance caching.
789
	 *
790
	 * @param array $date_range
791
	 * @return array
792
	 */
793
	protected function _get_coalesced_range( array $date_range ) {
794
		$now = time();
795
		$coalesce_time = $this->_blog_id_wpcom % 86400;
796
		$current_time = $now - strtotime( 'today', $now );
797
798
		if ( $current_time < $coalesce_time && '01' == date( 'd', $now ) ) {
799
			// Move back 1 period
800
			return array(
801
				'from' => date( 'Y-m-01', strtotime( '-1 month', $date_range['from'] ) ) . ' ' . date( 'H:i:s', $coalesce_time ),
802
				'to'   => date( 'Y-m-01', $date_range['to'] ) . ' ' . date( 'H:i:s', $coalesce_time ),
803
			);
804
		} else {
805
			// Use current period
806
			return array(
807
				'from' => date( 'Y-m-01', $date_range['from'] ) . ' ' . date( 'H:i:s', $coalesce_time ),
808
				'to'   => date( 'Y-m-01', strtotime( '+1 month', $date_range['to'] ) ) . ' ' . date( 'H:i:s', $coalesce_time ),
809
			);
810
		}
811
	}
812
813
	/**
814
	 * Generate and output ajax response for related posts API call.
815
	 * NOTE: Calls exit() to end all further processing after payload has been outputed.
816
	 *
817
	 * @param array $excludes array of post_ids to exclude
818
	 * @uses send_nosniff_header, self::get_for_post_id, get_the_ID
819
	 * @return null
820
	 */
821
	protected function _action_frontend_init_ajax( array $excludes ) {
822
		define( 'DOING_AJAX', true );
823
824
		header( 'Content-type: application/json; charset=utf-8' ); // JSON can only be UTF-8
825
		send_nosniff_header();
826
827
		$options = $this->get_options();
828
829
		if ( isset( $_GET['jetpackrpcustomize'] ) ) {
830
831
			// If we're in the customizer, add dummy content.
832
			$date_now = current_time( get_option( 'date_format' ) );
833
			$related_posts = array(
834
				array(
835
					'id'       => - 1,
836
					'url'      => 'https://jetpackme.files.wordpress.com/2014/08/1-wpios-ipad-3-1-viewsite.png?w=350&h=200&crop=1',
837
					'url_meta' => array(
838
						'origin'   => 0,
839
						'position' => 0
840
					),
841
					'title'    => esc_html__( 'Big iPhone/iPad Update Now Available', 'jetpack' ),
842
					'date'     => $date_now,
843
					'format'   => false,
844
					'excerpt'  => esc_html__( 'It is that time of the year when devices are shiny again.', 'jetpack' ),
845
					'rel'      => 'nofollow',
846
					'context'  => esc_html__( 'In "Mobile"', 'jetpack' ),
847
					'img'      => array(
848
						'src'    => 'https://jetpackme.files.wordpress.com/2014/08/1-wpios-ipad-3-1-viewsite.png?w=350&h=200&crop=1',
849
						'width'  => 350,
850
						'height' => 200
851
					),
852
					'classes'  => array()
853
				),
854
				array(
855
					'id'       => - 1,
856
					'url'      => 'https://jetpackme.files.wordpress.com/2014/08/wordpress-com-news-wordpress-for-android-ui-update2.jpg?w=350&h=200&crop=1',
857
					'url_meta' => array(
858
						'origin'   => 0,
859
						'position' => 0
860
					),
861
					'title'    => esc_html__( 'The WordPress for Android App Gets a Big Facelift', 'jetpack' ),
862
					'date'     => $date_now,
863
					'format'   => false,
864
					'excerpt'  => esc_html__( 'Writing is new again in Android with the new WordPress app.', 'jetpack' ),
865
					'rel'      => 'nofollow',
866
					'context'  => esc_html__( 'In "Mobile"', 'jetpack' ),
867
					'img'      => array(
868
						'src'    => 'https://jetpackme.files.wordpress.com/2014/08/wordpress-com-news-wordpress-for-android-ui-update2.jpg?w=350&h=200&crop=1',
869
						'width'  => 350,
870
						'height' => 200
871
					),
872
					'classes'  => array()
873
				),
874
				array(
875
					'id'       => - 1,
876
					'url'      => 'https://jetpackme.files.wordpress.com/2014/08/videopresswedding.jpg?w=350&h=200&crop=1',
877
					'url_meta' => array(
878
						'origin'   => 0,
879
						'position' => 0
880
					),
881
					'title'    => esc_html__( 'Upgrade Focus, VideoPress for weddings', 'jetpack' ),
882
					'date'     => $date_now,
883
					'format'   => false,
884
					'excerpt'  => esc_html__( 'Weddings are in the spotlight now with VideoPress for weddings.', 'jetpack' ),
885
					'rel'      => 'nofollow',
886
					'context'  => esc_html__( 'In "Mobile"', 'jetpack' ),
887
					'img'      => array(
888
						'src'    => 'https://jetpackme.files.wordpress.com/2014/08/videopresswedding.jpg?w=350&h=200&crop=1',
889
						'width'  => 350,
890
						'height' => 200
891
					),
892
					'classes'  => array()
893
				),
894
			);
895
896
			for ( $total = 0; $total < $options['size'] - 3; $total++ ) {
897
				$related_posts[] = $related_posts[ $total ];
898
			}
899
900
			$current_post = get_post();
901
902
			// Exclude current post after filtering to make sure it's excluded and not lost during filtering.
903
			$excluded_posts = array_merge(
904
				/** This filter is already documented in modules/related-posts/jetpack-related-posts.php */
905
				apply_filters( 'jetpack_relatedposts_filter_exclude_post_ids', array() ),
906
				array( $current_post->ID )
907
			);
908
909
			// Fetch posts with featured image.
910
			$with_post_thumbnails = get_posts( array(
911
				'posts_per_page'   => $options['size'],
912
				'post__not_in'     => $excluded_posts,
913
				'post_type'        => $current_post->post_type,
914
				'meta_key'         => '_thumbnail_id',
915
				'suppress_filters' => false,
916
			) );
917
918
			// If we don't have enough, fetch posts without featured image.
919
			if ( 0 < ( $more = $options['size'] - count( $with_post_thumbnails ) ) ) {
920
				$no_post_thumbnails = get_posts( array(
921
					'posts_per_page'  => $more,
922
					'post__not_in'    => $excluded_posts,
923
					'post_type'       => $current_post->post_type,
924
					'meta_query' => array(
925
						array(
926
							'key'     => '_thumbnail_id',
927
							'compare' => 'NOT EXISTS',
928
						),
929
					),
930
					'suppress_filters' => false,
931
				) );
932
			} else {
933
				$no_post_thumbnails = array();
934
			}
935
936
			foreach ( array_merge( $with_post_thumbnails, $no_post_thumbnails ) as $index => $real_post ) {
937
				$related_posts[ $index ]['id']      = $real_post->ID;
938
				$related_posts[ $index ]['url']     = esc_url( get_permalink( $real_post ) );
939
				$related_posts[ $index ]['title']   = $this->_to_utf8( $this->_get_title( $real_post->post_title, $real_post->post_content ) );
940
				$related_posts[ $index ]['date']    = get_the_date( '', $real_post );
941
				$related_posts[ $index ]['excerpt'] = html_entity_decode( $this->_to_utf8( $this->_get_excerpt( $real_post->post_excerpt, $real_post->post_content ) ), ENT_QUOTES, 'UTF-8' );
942
				$related_posts[ $index ]['img']     = $this->_generate_related_post_image_params( $real_post->ID );
943
				$related_posts[ $index ]['context'] = $this->_generate_related_post_context( $real_post->ID );
944
			}
945
		} else {
946
			$related_posts = $this->get_for_post_id(
947
				get_the_ID(),
948
				array(
949
					'exclude_post_ids' => $excludes,
950
				)
951
			);
952
		}
953
954
		$response = array(
955
			'version' => self::VERSION,
956
			'show_thumbnails' => (bool) $options['show_thumbnails'],
957
			'show_date' => (bool) $options['show_date'],
958
			'show_context' => (bool) $options['show_context'],
959
			'layout' => (string) $options['layout'],
960
			'headline' => (string) $options['headline'],
961
			'items' => array(),
962
		);
963
964
		if ( count( $related_posts ) == $options['size'] )
965
			$response['items'] = $related_posts;
966
967
		echo json_encode( $response );
968
969
		exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method _action_frontend_init_ajax() 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...
970
	}
971
972
	/**
973
	 * Returns a UTF-8 encoded array of post information for the given post_id
974
	 *
975
	 * @param int $post_id
976
	 * @param int $position
977
	 * @param int $origin The post id that this is related to
978
	 * @uses get_post, get_permalink, remove_query_arg, get_post_format, apply_filters
979
	 * @return array
980
	 */
981
	protected function _get_related_post_data_for_post( $post_id, $position, $origin ) {
982
		$post = get_post( $post_id );
983
984
		return array(
985
			'id' => $post->ID,
986
			'url' => get_permalink( $post->ID ),
987
			'url_meta' => array( 'origin' => $origin, 'position' => $position ),
988
			'title' => $this->_to_utf8( $this->_get_title( $post->post_title, $post->post_content ) ),
989
			'date' => get_the_date( '', $post->ID ),
990
			'format' => get_post_format( $post->ID ),
991
			'excerpt' => html_entity_decode( $this->_to_utf8( $this->_get_excerpt( $post->post_excerpt, $post->post_content ) ), ENT_QUOTES, 'UTF-8' ),
992
			/**
993
			 * Filters the rel attribute for the Related Posts' links.
994
			 *
995
			 * @module related-posts
996
			 *
997
			 * @since 3.7.0
998
			 *
999
			 * @param string nofollow Link rel attribute for Related Posts' link. Default is nofollow.
1000
			 * @param int $post->ID Post ID.
1001
			 */
1002
			'rel' => apply_filters( 'jetpack_relatedposts_filter_post_link_rel', 'nofollow', $post->ID ),
1003
			/**
1004
			 * Filter the context displayed below each Related Post.
1005
			 *
1006
			 * @module related-posts
1007
			 *
1008
			 * @since 3.0.0
1009
			 *
1010
			 * @param string $this->_to_utf8( $this->_generate_related_post_context( $post->ID ) ) Context displayed below each related post.
1011
			 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
1012
			 */
1013
			'context' => apply_filters(
1014
				'jetpack_relatedposts_filter_post_context',
1015
				$this->_to_utf8( $this->_generate_related_post_context( $post->ID ) ),
1016
				$post->ID
1017
			),
1018
			'img' => $this->_generate_related_post_image_params( $post->ID ),
1019
			/**
1020
			 * Filter the post css classes added on HTML markup.
1021
			 *
1022
			 * @module related-posts
1023
			 *
1024
			 * @since 3.8.0
1025
			 *
1026
			 * @param array array() CSS classes added on post HTML markup.
1027
			 * @param string $post_id Post ID.
1028
			 */
1029
			'classes' => apply_filters(
1030
				'jetpack_relatedposts_filter_post_css_classes',
1031
				array(),
1032
				$post->ID
1033
			),
1034
		);
1035
	}
1036
1037
	/**
1038
	 * Returns either the title or a small excerpt to use as title for post.
1039
	 *
1040
	 * @param string $post_title
1041
	 * @param string $post_content
1042
	 * @uses strip_shortcodes, wp_trim_words, __
1043
	 * @return string
1044
	 */
1045
	protected function _get_title( $post_title, $post_content ) {
1046
		if ( ! empty( $post_title ) ) {
1047
			return wp_strip_all_tags( $post_title );
1048
		}
1049
1050
		$post_title = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $post_content ) ), 5, '…' );
1051
		if ( ! empty( $post_title ) ) {
1052
			return $post_title;
1053
		}
1054
1055
		return __( 'Untitled Post', 'jetpack' );
1056
	}
1057
1058
	/**
1059
	 * Returns a plain text post excerpt for title attribute of links.
1060
	 *
1061
	 * @param string $post_excerpt
1062
	 * @param string $post_content
1063
	 * @uses strip_shortcodes, wp_strip_all_tags, wp_trim_words
1064
	 * @return string
1065
	 */
1066
	protected function _get_excerpt( $post_excerpt, $post_content ) {
1067
		if ( empty( $post_excerpt ) )
1068
			$excerpt = $post_content;
1069
		else
1070
			$excerpt = $post_excerpt;
1071
1072
		return wp_trim_words( wp_strip_all_tags( strip_shortcodes( $excerpt ) ), 50, '…' );
1073
	}
1074
1075
	/**
1076
	 * Generates the thumbnail image to be used for the post. Uses the
1077
	 * image as returned by Jetpack_PostImages::get_image()
1078
	 *
1079
	 * @param int $post_id
1080
	 * @uses self::get_options, apply_filters, Jetpack_PostImages::get_image, Jetpack_PostImages::fit_image_url
1081
	 * @return string
1082
	 */
1083
	protected function _generate_related_post_image_params( $post_id ) {
1084
		$options = $this->get_options();
1085
		$image_params = array(
1086
			'src' => '',
1087
			'width' => 0,
1088
			'height' => 0,
1089
		);
1090
1091
		if ( ! $options['show_thumbnails'] ) {
1092
			return $image_params;
1093
		}
1094
1095
		/**
1096
		 * Filter the size of the Related Posts images.
1097
		 *
1098
		 * @module related-posts
1099
		 *
1100
		 * @since 2.8.0
1101
		 *
1102
		 * @param array array( 'width' => 350, 'height' => 200 ) Size of the images displayed below each Related Post.
1103
		 */
1104
		$thumbnail_size = apply_filters(
1105
			'jetpack_relatedposts_filter_thumbnail_size',
1106
			array( 'width' => 350, 'height' => 200 )
1107
		);
1108
		if ( !is_array( $thumbnail_size ) ) {
1109
			$thumbnail_size = array(
1110
				'width' => (int)$thumbnail_size,
1111
				'height' => (int)$thumbnail_size
1112
			);
1113
		}
1114
1115
		// Try to get post image
1116
		if ( class_exists( 'Jetpack_PostImages' ) ) {
1117
			$img_url = '';
1118
			$post_image = Jetpack_PostImages::get_image(
1119
				$post_id,
1120
				$thumbnail_size
1121
			);
1122
1123
			if ( is_array($post_image) ) {
1124
				$img_url = $post_image['src'];
1125
			} elseif ( class_exists( 'Jetpack_Media_Summary' ) ) {
1126
				$media = Jetpack_Media_Summary::get( $post_id );
1127
1128
				if ( is_array($media) && !empty( $media['image'] ) ) {
1129
					$img_url = $media['image'];
1130
				}
1131
			}
1132
1133
			if ( !empty( $img_url ) ) {
1134
				$image_params['width'] = $thumbnail_size['width'];
1135
				$image_params['height'] = $thumbnail_size['height'];
1136
				$image_params['src'] = Jetpack_PostImages::fit_image_url(
1137
					$img_url,
1138
					$thumbnail_size['width'],
1139
					$thumbnail_size['height']
1140
				);
1141
			}
1142
		}
1143
1144
		return $image_params;
1145
	}
1146
1147
	/**
1148
	 * Returns the string UTF-8 encoded
1149
	 *
1150
	 * @param string $text
1151
	 * @return string
1152
	 */
1153
	protected function _to_utf8( $text ) {
1154
		if ( $this->_convert_charset ) {
1155
			return iconv( $this->_blog_charset, 'UTF-8', $text );
1156
		} else {
1157
			return $text;
1158
		}
1159
	}
1160
1161
	/**
1162
	 * =============================================
1163
	 * PROTECTED UTILITY FUNCTIONS EXTENDED BY WPCOM
1164
	 * =============================================
1165
	 */
1166
1167
	/**
1168
	 * Workhorse method to return array of related posts matched by Elasticsearch.
1169
	 *
1170
	 * @param int $post_id
1171
	 * @param int $size
1172
	 * @param array $filters
1173
	 * @uses wp_remote_post, is_wp_error, get_option, wp_remote_retrieve_body, get_post, add_query_arg, remove_query_arg, get_permalink, get_post_format, apply_filters
1174
	 * @return array
1175
	 */
1176
	protected function _get_related_posts( $post_id, $size, array $filters ) {
1177
		$hits = $this->_filter_non_public_posts(
1178
			$this->_get_related_post_ids(
1179
				$post_id,
1180
				$size,
1181
				$filters
1182
			)
1183
		);
1184
1185
		/**
1186
		 * Filter the Related Posts matched by Elasticsearch.
1187
		 *
1188
		 * @module related-posts
1189
		 *
1190
		 * @since 2.9.0
1191
		 *
1192
		 * @param array $hits Array of Post IDs matched by Elasticsearch.
1193
		 * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
1194
		 */
1195
		$hits = apply_filters( 'jetpack_relatedposts_filter_hits', $hits, $post_id );
1196
1197
		$related_posts = array();
1198
		foreach ( $hits as $i => $hit ) {
1199
			$related_posts[] = $this->_get_related_post_data_for_post( $hit['id'], $i, $post_id );
1200
		}
1201
		return $related_posts;
1202
	}
1203
1204
	/**
1205
	 * Get array of related posts matched by Elasticsearch.
1206
	 *
1207
	 * @param int $post_id
1208
	 * @param int $size
1209
	 * @param array $filters
1210
	 * @uses wp_remote_post, is_wp_error, wp_remote_retrieve_body, get_post_meta, update_post_meta
1211
	 * @return array
1212
	 */
1213
	protected function _get_related_post_ids( $post_id, $size, array $filters ) {
1214
		$now_ts = time();
1215
		$cache_meta_key = '_jetpack_related_posts_cache';
1216
1217
		$body = array(
1218
			'size' => (int) $size,
1219
		);
1220
1221
		if ( !empty( $filters ) )
1222
			$body['filter'] = array( 'and' => $filters );
1223
1224
		// Build cache key
1225
		$cache_key = md5( serialize( $body ) );
1226
1227
		// Load all cached values
1228
		if ( wp_using_ext_object_cache() ) {
1229
			$transient_name = "{$cache_meta_key}_{$cache_key}_{$post_id}";
1230
			$cache = get_transient( $transient_name );
1231
			if ( false !== $cache ) {
1232
				return $cache;
1233
			}
1234
		} else {
1235
			$cache = get_post_meta( $post_id, $cache_meta_key, true );
1236
1237
			if ( empty( $cache ) )
1238
				$cache = array();
1239
1240
1241
			// Cache is valid! Return cached value.
1242
			if ( isset( $cache[ $cache_key ] ) && is_array( $cache[ $cache_key ] ) && $cache[ $cache_key ][ 'expires' ] > $now_ts ) {
1243
				return $cache[ $cache_key ][ 'payload' ];
1244
			}
1245
		}
1246
1247
		$response = wp_remote_post(
1248
			"https://public-api.wordpress.com/rest/v1/sites/{$this->_blog_id_wpcom}/posts/$post_id/related/",
1249
			array(
1250
				'timeout' => 10,
1251
				'user-agent' => 'jetpack_related_posts',
1252
				'sslverify' => true,
1253
				'body' => $body,
1254
			)
1255
		);
1256
1257
		// Oh no... return nothing don't cache errors.
1258
		if ( is_wp_error( $response ) ) {
1259
			if ( isset( $cache[ $cache_key ] ) && is_array( $cache[ $cache_key ] ) )
1260
				return $cache[ $cache_key ][ 'payload' ]; // return stale
1261
			else
1262
				return array();
1263
		}
1264
1265
		$results = json_decode( wp_remote_retrieve_body( $response ), true );
1266
		$related_posts = array();
1267
		if ( is_array( $results ) && !empty( $results['hits'] ) ) {
1268
			foreach( $results['hits'] as $hit ) {
1269
				$related_posts[] = array(
1270
					'id' => $hit['fields']['post_id'],
1271
				);
1272
			}
1273
		}
1274
1275
		// An empty array might indicate no related posts or that posts
1276
		// are not yet synced to WordPress.com, so we cache for only 1
1277
		// minute in this case
1278
		if ( empty( $related_posts ) ) {
1279
			$cache_ttl = 60;
1280
		} else {
1281
			$cache_ttl = 12 * HOUR_IN_SECONDS;
1282
		}
1283
1284
		// Update cache
1285
		if ( wp_using_ext_object_cache() ) {
1286
			set_transient( $transient_name, $related_posts, $cache_ttl );
0 ignored issues
show
Bug introduced by
The variable $transient_name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1287
		} else {
1288
			// Copy all valid cache values
1289
			$new_cache = array();
1290
			foreach ( $cache as $k => $v ) {
1291
				if ( is_array( $v ) && $v[ 'expires' ] > $now_ts ) {
1292
					$new_cache[ $k ] = $v;
1293
				}
1294
			}
1295
1296
			// Set new cache value
1297
			$cache_expires = $cache_ttl + $now_ts;
1298
			$new_cache[ $cache_key ] = array(
1299
				'expires' => $cache_expires,
1300
				'payload' => $related_posts,
1301
			);
1302
			update_post_meta( $post_id, $cache_meta_key, $new_cache );
1303
		}
1304
1305
		return $related_posts;
1306
	}
1307
1308
	/**
1309
	 * Filter out any hits that are not public anymore.
1310
	 *
1311
	 * @param array $related_posts
1312
	 * @uses get_post_stati, get_post_status
1313
	 * @return array
1314
	 */
1315
	protected function _filter_non_public_posts( array $related_posts ) {
1316
		$public_stati = get_post_stati( array( 'public' => true ) );
1317
1318
		$filtered = array();
1319
		foreach ( $related_posts as $hit ) {
1320
			if ( in_array( get_post_status( $hit['id'] ), $public_stati ) ) {
1321
				$filtered[] = $hit;
1322
			}
1323
		}
1324
		return $filtered;
1325
	}
1326
1327
	/**
1328
	 * Generates a context for the related content (second line in related post output).
1329
	 * Order of importance:
1330
	 *   - First category (Not 'Uncategorized')
1331
	 *   - First post tag
1332
	 *   - Number of comments
1333
	 *
1334
	 * @param int $post_id
1335
	 * @uses get_the_category, get_the_terms, get_comments_number, number_format_i18n, __, _n
1336
	 * @return string
1337
	 */
1338
	protected function _generate_related_post_context( $post_id ) {
1339
		$categories = get_the_category( $post_id );
1340 View Code Duplication
		if ( is_array( $categories ) ) {
1341
			foreach ( $categories as $category ) {
1342
				if ( 'uncategorized' != $category->slug && '' != trim( $category->name ) ) {
1343
					$post_cat_context = sprintf(
1344
						_x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1345
						$category->name
1346
					);
1347
					/**
1348
					 * Filter the "In Category" line displayed in the post context below each Related Post.
1349
					 *
1350
					 * @module related-posts
1351
					 *
1352
					 * @since 3.2.0
1353
					 *
1354
					 * @param string $post_cat_context "In Category" line displayed in the post context below each Related Post.
1355
					 * @param array $category Array containing information about the category.
1356
					 */
1357
					return apply_filters( 'jetpack_relatedposts_post_category_context', $post_cat_context, $category );
1358
				}
1359
			}
1360
		}
1361
1362
		$tags = get_the_terms( $post_id, 'post_tag' );
1363 View Code Duplication
		if ( is_array( $tags ) ) {
1364
			foreach ( $tags as $tag ) {
1365
				if ( '' != trim( $tag->name ) ) {
1366
					$post_tag_context = sprintf(
1367
						_x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1368
						$tag->name
1369
					);
1370
					/**
1371
					 * Filter the "In Tag" line displayed in the post context below each Related Post.
1372
					 *
1373
					 * @module related-posts
1374
					 *
1375
					 * @since 3.2.0
1376
					 *
1377
					 * @param string $post_tag_context "In Tag" line displayed in the post context below each Related Post.
1378
					 * @param array $tag Array containing information about the tag.
1379
					 */
1380
					return apply_filters( 'jetpack_relatedposts_post_tag_context', $post_tag_context, $tag );
1381
				}
1382
			}
1383
		}
1384
1385
		$comment_count = get_comments_number( $post_id );
1386
		if ( $comment_count > 0 ) {
1387
			return sprintf(
1388
				_n( 'With 1 comment', 'With %s comments', $comment_count, 'jetpack' ),
1389
				number_format_i18n( $comment_count )
1390
			);
1391
		}
1392
1393
		return __( 'Similar post', 'jetpack' );
1394
	}
1395
1396
	/**
1397
	 * Logs clicks for clickthrough analysis and related result tuning.
1398
	 *
1399
	 * @return null
1400
	 */
1401
	protected function _log_click( $post_id, $to_post_id, $link_position ) {
1402
1403
	}
1404
1405
	/**
1406
	 * Determines if the current post is able to use related posts.
1407
	 *
1408
	 * @uses self::get_options, is_admin, is_single, apply_filters
1409
	 * @return bool
1410
	 */
1411
	protected function _enabled_for_request() {
1412
		$enabled = is_single()
1413
			&&
1414
				! is_admin()
1415
			&&
1416
				( !$this->_allow_feature_toggle() || $this->get_option( 'enabled' ) );
1417
1418
		/**
1419
		 * Filter the Enabled value to allow related posts to be shown on pages as well.
1420
		 *
1421
		 * @module related-posts
1422
		 *
1423
		 * @since 3.3.0
1424
		 *
1425
		 * @param bool $enabled Should Related Posts be enabled on the current page.
1426
		 */
1427
		return apply_filters( 'jetpack_relatedposts_filter_enabled_for_request', $enabled );
1428
	}
1429
1430
	/**
1431
	 * Adds filters and enqueues assets.
1432
	 *
1433
	 * @uses self::_enqueue_assets, self::_setup_shortcode, add_filter
1434
	 * @return null
1435
	 */
1436
	protected function _action_frontend_init_page() {
1437
		$this->_enqueue_assets( true, true );
1438
		$this->_setup_shortcode();
1439
1440
		add_filter( 'the_content', array( $this, 'filter_add_target_to_dom' ), 40 );
1441
	}
1442
1443
	/**
1444
	 * Enqueues assets needed to do async loading of related posts.
1445
	 *
1446
	 * @uses wp_enqueue_script, wp_enqueue_style, plugins_url
1447
	 * @return null
1448
	 */
1449
	protected function _enqueue_assets( $script, $style ) {
1450
		$dependencies = is_customize_preview() ? array( 'customize-base' ) : array( 'jquery' );
1451
		if ( $script ) {
1452
			wp_enqueue_script(
1453
				'jetpack_related-posts',
1454
				Jetpack::get_file_url_for_environment(
1455
					'_inc/build/related-posts/related-posts.min.js',
1456
					'modules/related-posts/related-posts.js'
1457
				),
1458
				$dependencies,
1459
				self::VERSION
1460
			);
1461
			$related_posts_js_options = array(
1462
				/**
1463
				 * Filter each Related Post Heading structure.
1464
				 *
1465
				 * @since 4.0.0
1466
				 *
1467
				 * @param string $str Related Post Heading structure. Default to h4.
1468
				 */
1469
				'post_heading' => apply_filters( 'jetpack_relatedposts_filter_post_heading', esc_attr( 'h4' ) ),
1470
			);
1471
			wp_localize_script( 'jetpack_related-posts', 'related_posts_js_options', $related_posts_js_options );
1472
		}
1473
		if ( $style ){
1474
			wp_enqueue_style( 'jetpack_related-posts', plugins_url( 'related-posts.css', __FILE__ ), array(), self::VERSION );
1475
			wp_style_add_data( 'jetpack_related-posts', 'rtl', 'replace' );
1476
		}
1477
	}
1478
1479
	/**
1480
	 * Sets up the shortcode processing.
1481
	 *
1482
	 * @uses add_filter, add_shortcode
1483
	 * @return null
1484
	 */
1485
	protected function _setup_shortcode() {
1486
		add_filter( 'the_content', array( $this, 'test_for_shortcode' ), 0 );
1487
1488
		add_shortcode( self::SHORTCODE, array( $this, 'get_target_html' ) );
1489
	}
1490
1491
	protected function _allow_feature_toggle() {
1492
		if ( null === $this->_allow_feature_toggle ) {
1493
			/**
1494
			 * Filter the display of the Related Posts toggle in Settings > Reading.
1495
			 *
1496
			 * @module related-posts
1497
			 *
1498
			 * @since 2.8.0
1499
			 *
1500
			 * @param bool false Display a feature toggle. Default to false.
1501
			 */
1502
			$this->_allow_feature_toggle = apply_filters( 'jetpack_relatedposts_filter_allow_feature_toggle', false );
1503
		}
1504
		return $this->_allow_feature_toggle;
1505
	}
1506
1507
	/**
1508
	 * ===================================================
1509
	 * FUNCTIONS EXPOSING RELATED POSTS IN THE WP REST API
1510
	 * ===================================================
1511
	 */
1512
1513
	/**
1514
	 * Add Related Posts to the REST API Post response.
1515
	 *
1516
	 * @since 4.4.0
1517
	 *
1518
	 * @action rest_api_init
1519
	 * @uses register_rest_field, self::rest_get_related_posts
1520
	 * @return null
1521
	 */
1522
	public function rest_register_related_posts() {
1523
		register_rest_field( 'post',
1524
			'jetpack-related-posts',
1525
			array(
1526
				'get_callback' => array( $this, 'rest_get_related_posts' ),
1527
				'update_callback' => null,
1528
				'schema'          => null,
1529
			)
1530
		);
1531
	}
1532
1533
	/**
1534
	 * Build an array of Related Posts.
1535
	 *
1536
	 * @since 4.4.0
1537
	 *
1538
	 * @param array $object Details of current post.
1539
	 * @param string $field_name Name of field.
1540
	 * @param WP_REST_Request $request Current request
1541
	 *
1542
	 * @uses self::get_for_post_id
1543
	 *
1544
	 * @return array
1545
	 */
1546
	public function rest_get_related_posts( $object, $field_name, $request ) {
1547
		return $this->get_for_post_id( $object['id'], array() );
1548
	}
1549
}
1550
1551
class Jetpack_RelatedPosts_Raw extends Jetpack_RelatedPosts {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
1552
	protected $_query_name;
1553
1554
	/**
1555
	 * Allows callers of this class to tag each query with a unique name for tracking purposes.
1556
	 *
1557
	 * @param string $name
1558
	 * @return Jetpack_RelatedPosts_Raw
1559
	 */
1560
	public function set_query_name( $name ) {
1561
		$this->_query_name = (string) $name;
1562
		return $this;
1563
	}
1564
1565
	/**
1566
	 * The raw related posts class can be used by other plugins or themes
1567
	 * to get related content. This class wraps the existing RelatedPosts
1568
	 * logic thus we never want to add anything to the DOM or do anything
1569
	 * for event hooks. We will also not present any settings for this
1570
	 * class and keep it enabled as calls to this class is done
1571
	 * programmatically.
1572
	 */
1573
	public function action_admin_init() {}
1574
	public function action_frontend_init() {}
1575
	public function get_options() {
1576
		return array(
1577
			'enabled' => true,
1578
		);
1579
	}
1580
1581
	/**
1582
	 * Workhorse method to return array of related posts ids matched by Elasticsearch.
1583
	 *
1584
	 * @param int $post_id
1585
	 * @param int $size
1586
	 * @param array $filters
1587
	 * @uses wp_remote_post, is_wp_error, wp_remote_retrieve_body
1588
	 * @return array
1589
	 */
1590
	protected function _get_related_posts( $post_id, $size, array $filters ) {
1591
		$hits = $this->_filter_non_public_posts(
1592
			$this->_get_related_post_ids(
1593
				$post_id,
1594
				$size,
1595
				$filters
1596
			)
1597
		);
1598
1599
		/** This filter is already documented in modules/related-posts/related-posts.php */
1600
		$hits = apply_filters( 'jetpack_relatedposts_filter_hits', $hits, $post_id );
1601
1602
		return $hits;
1603
	}
1604
}
1605