Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_RelatedPosts often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_RelatedPosts, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 2 | class Jetpack_RelatedPosts { | ||
| 3 | const VERSION = '20150408'; | ||
| 4 | const SHORTCODE = 'jetpack-related-posts'; | ||
| 5 | |||
| 6 | /** | ||
| 7 | * Creates and returns a static instance of Jetpack_RelatedPosts. | ||
| 8 | * | ||
| 9 | * @return Jetpack_RelatedPosts | ||
| 10 | */ | ||
| 11 | View Code Duplication | 	public static function init() { | |
| 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() { | |
| 49 | |||
| 50 | protected $_blog_id_local; | ||
| 51 | protected $_blog_id_wpcom; | ||
| 52 | protected $_options; | ||
| 53 | protected $_allow_feature_toggle; | ||
| 54 | protected $_blog_charset; | ||
| 55 | protected $_convert_charset; | ||
| 56 | protected $_previous_post_id; | ||
| 57 | protected $_found_shortcode = false; | ||
| 58 | |||
| 59 | /** | ||
| 60 | * Constructor for Jetpack_RelatedPosts. | ||
| 61 | * | ||
| 62 | * @param int $blog_id_local | ||
| 63 | * @param int $blog_id_wpcom | ||
| 64 | * @uses get_option, add_action, apply_filters | ||
| 65 | * @return null | ||
|  | |||
| 66 | */ | ||
| 67 | 	public function __construct( $blog_id_local, $blog_id_wpcom ) { | ||
| 80 | |||
| 81 | /** | ||
| 82 | * ================= | ||
| 83 | * ACTIONS & FILTERS | ||
| 84 | * ================= | ||
| 85 | */ | ||
| 86 | |||
| 87 | /** | ||
| 88 | * Add a checkbox field to Settings > Reading for enabling related posts. | ||
| 89 | * | ||
| 90 | * @action admin_init | ||
| 91 | * @uses add_settings_field, __, register_setting, add_action | ||
| 92 | * @return null | ||
| 93 | */ | ||
| 94 | 	public function action_admin_init() { | ||
| 106 | |||
| 107 | /** | ||
| 108 | * Load related posts assets if it's a elegiable front end page or execute search and return JSON if it's an endpoint request. | ||
| 109 | * | ||
| 110 | * @global $_GET | ||
| 111 | * @action wp | ||
| 112 | * @uses add_shortcode, get_the_ID | ||
| 113 | * @returns null | ||
| 114 | */ | ||
| 115 | 	public function action_frontend_init() { | ||
| 139 | |||
| 140 | /** | ||
| 141 | * Adds a target to the post content to load related posts into if a shortcode for it did not already exist. | ||
| 142 | * | ||
| 143 | * @filter the_content | ||
| 144 | * @param string $content | ||
| 145 | * @returns string | ||
| 146 | */ | ||
| 147 | 	public function filter_add_target_to_dom( $content ) { | ||
| 154 | |||
| 155 | /** | ||
| 156 | * Looks for our shortcode on the unfiltered content, this has to execute early. | ||
| 157 | * | ||
| 158 | * @filter the_content | ||
| 159 | * @param string $content | ||
| 160 | * @uses has_shortcode | ||
| 161 | * @returns string | ||
| 162 | */ | ||
| 163 | 	public function test_for_shortcode( $content ) { | ||
| 168 | |||
| 169 | /** | ||
| 170 | * Returns the HTML for the related posts section. | ||
| 171 | * | ||
| 172 | * @uses esc_html__, apply_filters | ||
| 173 | * @returns string | ||
| 174 | */ | ||
| 175 | 	public function get_target_html() { | ||
| 210 | |||
| 211 | /** | ||
| 212 | * 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. | ||
| 213 | * | ||
| 214 | * @returns string | ||
| 215 | */ | ||
| 216 | 	public function get_target_html_unsupported() { | ||
| 219 | |||
| 220 | /** | ||
| 221 | * ======================== | ||
| 222 | * PUBLIC UTILITY FUNCTIONS | ||
| 223 | * ======================== | ||
| 224 | */ | ||
| 225 | |||
| 226 | /** | ||
| 227 | * Gets options set for Jetpack_RelatedPosts and merge with defaults. | ||
| 228 | * | ||
| 229 | * @uses Jetpack_Options::get_option, apply_filters | ||
| 230 | * @return array | ||
| 231 | */ | ||
| 232 | 	public function get_options() { | ||
| 260 | |||
| 261 | /** | ||
| 262 | * Parses input and returnes normalized options array. | ||
| 263 | * | ||
| 264 | * @param array $input | ||
| 265 | * @uses self::get_options | ||
| 266 | * @return array | ||
| 267 | */ | ||
| 268 | 	public function parse_options( $input ) { | ||
| 289 | |||
| 290 | /** | ||
| 291 | * HTML for admin settings page. | ||
| 292 | * | ||
| 293 | * @uses self::get_options, checked, esc_html__ | ||
| 294 | * @returns null | ||
| 295 | */ | ||
| 296 | 	public function print_setting_html() { | ||
| 353 | |||
| 354 | /** | ||
| 355 | * Head JS/CSS for admin settings page. | ||
| 356 | * | ||
| 357 | * @uses esc_html__ | ||
| 358 | * @returns null | ||
| 359 | */ | ||
| 360 | 	public function print_setting_head() { | ||
| 361 | |||
| 362 | // only dislay the Related Posts JavaScript on the Reading Settings Admin Page | ||
| 363 | $current_screen = get_current_screen(); | ||
| 364 | |||
| 365 | 		if ( is_null( $current_screen ) ) { | ||
| 366 | return; | ||
| 367 | } | ||
| 368 | |||
| 369 | if( 'options-reading' != $current_screen->id ) | ||
| 370 | return; | ||
| 371 | |||
| 372 | $related_headline = sprintf( | ||
| 373 | '<h3 class="jp-relatedposts-headline"><em>%s</em></h3>', | ||
| 374 | esc_html__( 'Related', 'jetpack' ) | ||
| 375 | ); | ||
| 376 | |||
| 377 | $href_params = 'class="jp-relatedposts-post-a" href="#jetpack_relatedposts" rel="nofollow" data-origin="0" data-position="0"'; | ||
| 378 | $related_with_images = <<<EOT | ||
| 379 | <div class="jp-relatedposts-items jp-relatedposts-items-visual"> | ||
| 380 | <div class="jp-relatedposts-post jp-relatedposts-post0 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image"> | ||
| 381 | <a $href_params> | ||
| 382 | <img class="jp-relatedposts-post-img" src="http://jetpackme.files.wordpress.com/2014/08/1-wpios-ipad-3-1-viewsite.png?w=350&h=200&crop=1" width="350" alt="Big iPhone/iPad Update Now Available" scale="0"> | ||
| 383 | </a> | ||
| 384 | <h4 class="jp-relatedposts-post-title"> | ||
| 385 | <a $href_params>Big iPhone/iPad Update Now Available</a> | ||
| 386 | </h4> | ||
| 387 | <p class="jp-relatedposts-post-excerpt">Big iPhone/iPad Update Now Available</p> | ||
| 388 | <p class="jp-relatedposts-post-context">In "Mobile"</p> | ||
| 389 | </div> | ||
| 390 | <div class="jp-relatedposts-post jp-relatedposts-post1 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image"> | ||
| 391 | <a $href_params> | ||
| 392 | <img class="jp-relatedposts-post-img" src="http://jetpackme.files.wordpress.com/2014/08/wordpress-com-news-wordpress-for-android-ui-update2.jpg?w=350&h=200&crop=1" width="350" alt="The WordPress for Android App Gets a Big Facelift" scale="0"> | ||
| 393 | </a> | ||
| 394 | <h4 class="jp-relatedposts-post-title"> | ||
| 395 | <a $href_params>The WordPress for Android App Gets a Big Facelift</a> | ||
| 396 | </h4> | ||
| 397 | <p class="jp-relatedposts-post-excerpt">The WordPress for Android App Gets a Big Facelift</p> | ||
| 398 | <p class="jp-relatedposts-post-context">In "Mobile"</p> | ||
| 399 | </div> | ||
| 400 | <div class="jp-relatedposts-post jp-relatedposts-post2 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image"> | ||
| 401 | <a $href_params> | ||
| 402 | <img class="jp-relatedposts-post-img" src="http://jetpackme.files.wordpress.com/2014/08/videopresswedding.jpg?w=350&h=200&crop=1" width="350" alt="Upgrade Focus: VideoPress For Weddings" scale="0"> | ||
| 403 | </a> | ||
| 404 | <h4 class="jp-relatedposts-post-title"> | ||
| 405 | <a $href_params>Upgrade Focus: VideoPress For Weddings</a> | ||
| 406 | </h4> | ||
| 407 | <p class="jp-relatedposts-post-excerpt">Upgrade Focus: VideoPress For Weddings</p> | ||
| 408 | <p class="jp-relatedposts-post-context">In "Upgrade"</p> | ||
| 409 | </div> | ||
| 410 | </div> | ||
| 411 | EOT; | ||
| 412 | $related_with_images = str_replace( "\n", '', $related_with_images ); | ||
| 413 | $related_without_images = <<<EOT | ||
| 414 | <div class="jp-relatedposts-items jp-relatedposts-items-minimal"> | ||
| 415 | <p class="jp-relatedposts-post jp-relatedposts-post0" data-post-id="0" data-post-format="image"> | ||
| 416 | <span class="jp-relatedposts-post-title"><a $href_params>Big iPhone/iPad Update Now Available</a></span> | ||
| 417 | <span class="jp-relatedposts-post-context">In "Mobile"</span> | ||
| 418 | </p> | ||
| 419 | <p class="jp-relatedposts-post jp-relatedposts-post1" data-post-id="0" data-post-format="image"> | ||
| 420 | <span class="jp-relatedposts-post-title"><a $href_params>The WordPress for Android App Gets a Big Facelift</a></span> | ||
| 421 | <span class="jp-relatedposts-post-context">In "Mobile"</span> | ||
| 422 | </p> | ||
| 423 | <p class="jp-relatedposts-post jp-relatedposts-post2" data-post-id="0" data-post-format="image"> | ||
| 424 | <span class="jp-relatedposts-post-title"><a $href_params>Upgrade Focus: VideoPress For Weddings</a></span> | ||
| 425 | <span class="jp-relatedposts-post-context">In "Upgrade"</span> | ||
| 426 | </p> | ||
| 427 | </div> | ||
| 428 | EOT; | ||
| 429 | $related_without_images = str_replace( "\n", '', $related_without_images ); | ||
| 430 | |||
| 431 | 		if ( $this->_allow_feature_toggle() ) { | ||
| 432 | 			$extra_css = '#settings-reading-relatedposts-customize { padding-left:2em; margin-top:.5em; }'; | ||
| 433 | 		} else { | ||
| 434 | $extra_css = ''; | ||
| 435 | } | ||
| 436 | |||
| 437 | echo <<<EOT | ||
| 438 | <style type="text/css"> | ||
| 439 | 	#settings-reading-relatedposts .disabled { opacity:.5; filter:Alpha(opacity=50); } | ||
| 440 | 	#settings-reading-relatedposts-preview .jp-relatedposts { background:#fff; padding:.5em; width:75%; } | ||
| 441 | $extra_css | ||
| 442 | </style> | ||
| 443 | <script type="text/javascript"> | ||
| 444 | 	jQuery( document ).ready( function($) { | ||
| 445 | 		var update_ui = function() { | ||
| 446 | var is_enabled = true; | ||
| 447 | 			if ( 'radio' == $( 'input[name="jetpack_relatedposts[enabled]"]' ).attr('type') ) { | ||
| 448 | 				if ( '0' == $( 'input[name="jetpack_relatedposts[enabled]"]:checked' ).val() ) { | ||
| 449 | is_enabled = false; | ||
| 450 | } | ||
| 451 | } | ||
| 452 | 			if ( is_enabled ) { | ||
| 453 | $( '#settings-reading-relatedposts-customize' ) | ||
| 454 | .removeClass( 'disabled' ) | ||
| 455 | .find( 'input' ) | ||
| 456 | .attr( 'disabled', false ); | ||
| 457 | $( '#settings-reading-relatedposts-preview' ) | ||
| 458 | .removeClass( 'disabled' ); | ||
| 459 | 			} else { | ||
| 460 | $( '#settings-reading-relatedposts-customize' ) | ||
| 461 | .addClass( 'disabled' ) | ||
| 462 | .find( 'input' ) | ||
| 463 | .attr( 'disabled', true ); | ||
| 464 | $( '#settings-reading-relatedposts-preview' ) | ||
| 465 | .addClass( 'disabled' ); | ||
| 466 | } | ||
| 467 | }; | ||
| 468 | |||
| 469 | 		var update_preview = function() { | ||
| 470 | var html = ''; | ||
| 471 | 			if ( $( 'input[name="jetpack_relatedposts[show_headline]"]:checked' ).length ) { | ||
| 472 | html += '$related_headline'; | ||
| 473 | } | ||
| 474 | 			if ( $( 'input[name="jetpack_relatedposts[show_thumbnails]"]:checked' ).length ) { | ||
| 475 | html += '$related_with_images'; | ||
| 476 | 			} else { | ||
| 477 | html += '$related_without_images'; | ||
| 478 | } | ||
| 479 | $( '#settings-reading-relatedposts-preview .jp-relatedposts' ) | ||
| 480 | .html( html ) | ||
| 481 | .show(); | ||
| 482 | }; | ||
| 483 | |||
| 484 | // Update on load | ||
| 485 | update_preview(); | ||
| 486 | update_ui(); | ||
| 487 | |||
| 488 | // Update on change | ||
| 489 | $( '#settings-reading-relatedposts-customize input' ) | ||
| 490 | .change( update_preview ); | ||
| 491 | $( '#settings-reading-relatedposts' ) | ||
| 492 | .find( 'input.tog' ) | ||
| 493 | .change( update_ui ); | ||
| 494 | }); | ||
| 495 | </script> | ||
| 496 | EOT; | ||
| 497 | } | ||
| 498 | |||
| 499 | /** | ||
| 500 | * Gets an array of related posts that match the given post_id. | ||
| 501 | * | ||
| 502 | * @param int $post_id | ||
| 503 | * @param array $args - params to use when building ElasticSearch filters to narrow down the search domain. | ||
| 504 | * @uses self::get_options, get_post_type, wp_parse_args, apply_filters | ||
| 505 | * @return array | ||
| 506 | */ | ||
| 507 | 	public function get_for_post_id( $post_id, array $args ) { | ||
| 563 | |||
| 564 | /** | ||
| 565 | * ========================= | ||
| 566 | * PRIVATE UTILITY FUNCTIONS | ||
| 567 | * ========================= | ||
| 568 | */ | ||
| 569 | |||
| 570 | /** | ||
| 571 | * Creates an array of ElasticSearch filters based on the post_id and args. | ||
| 572 | * | ||
| 573 | * @param int $post_id | ||
| 574 | * @param array $args | ||
| 575 | * @uses apply_filters, get_post_types, get_post_format_strings | ||
| 576 | * @return array | ||
| 577 | */ | ||
| 578 | 	protected function _get_es_filters_from_args( $post_id, array $args ) { | ||
| 701 | |||
| 702 | /** | ||
| 703 | * Takes a range and coalesces it into a month interval bracketed by a time as determined by the blog_id to enhance caching. | ||
| 704 | * | ||
| 705 | * @param array $date_range | ||
| 706 | * @return array | ||
| 707 | */ | ||
| 708 | 	protected function _get_coalesced_range( array $date_range ) { | ||
| 727 | |||
| 728 | /** | ||
| 729 | * Generate and output ajax response for related posts API call. | ||
| 730 | * NOTE: Calls exit() to end all further processing after payload has been outputed. | ||
| 731 | * | ||
| 732 | * @param array $excludes array of post_ids to exclude | ||
| 733 | * @uses send_nosniff_header, self::get_for_post_id, get_the_ID | ||
| 734 | * @return null | ||
| 735 | */ | ||
| 736 | 	protected function _action_frontend_init_ajax( array $excludes ) { | ||
| 764 | |||
| 765 | /** | ||
| 766 | * Returns a UTF-8 encoded array of post information for the given post_id | ||
| 767 | * | ||
| 768 | * @param int $post_id | ||
| 769 | * @param int $position | ||
| 770 | * @param int $origin The post id that this is related to | ||
| 771 | * @uses get_post, get_permalink, remove_query_arg, get_post_format, apply_filters | ||
| 772 | * @return array | ||
| 773 | */ | ||
| 774 | 	protected function _get_related_post_data_for_post( $post_id, $position, $origin ) { | ||
| 829 | |||
| 830 | /** | ||
| 831 | * Returns either the title or a small excerpt to use as title for post. | ||
| 832 | * | ||
| 833 | * @param string $post_title | ||
| 834 | * @param string $post_content | ||
| 835 | * @uses strip_shortcodes, wp_trim_words, __ | ||
| 836 | * @return string | ||
| 837 | */ | ||
| 838 | 	protected function _get_title( $post_title, $post_content ) { | ||
| 850 | |||
| 851 | /** | ||
| 852 | * Returns a plain text post excerpt for title attribute of links. | ||
| 853 | * | ||
| 854 | * @param string $post_excerpt | ||
| 855 | * @param string $post_content | ||
| 856 | * @uses strip_shortcodes, wp_strip_all_tags, wp_trim_words | ||
| 857 | * @return string | ||
| 858 | */ | ||
| 859 | 	protected function _get_excerpt( $post_excerpt, $post_content ) { | ||
| 867 | |||
| 868 | /** | ||
| 869 | * Generates the thumbnail image to be used for the post. Uses the | ||
| 870 | * image as returned by Jetpack_PostImages::get_image() | ||
| 871 | * | ||
| 872 | * @param int $post_id | ||
| 873 | * @uses self::get_options, apply_filters, Jetpack_PostImages::get_image, Jetpack_PostImages::fit_image_url | ||
| 874 | * @return string | ||
| 875 | */ | ||
| 876 | 	protected function _generate_related_post_image_params( $post_id ) { | ||
| 939 | |||
| 940 | /** | ||
| 941 | * Returns the string UTF-8 encoded | ||
| 942 | * | ||
| 943 | * @param string $text | ||
| 944 | * @return string | ||
| 945 | */ | ||
| 946 | 	protected function _to_utf8( $text ) { | ||
| 953 | |||
| 954 | /** | ||
| 955 | * ============================================= | ||
| 956 | * PROTECTED UTILITY FUNCTIONS EXTENDED BY WPCOM | ||
| 957 | * ============================================= | ||
| 958 | */ | ||
| 959 | |||
| 960 | /** | ||
| 961 | * Workhorse method to return array of related posts matched by ElasticSearch. | ||
| 962 | * | ||
| 963 | * @param int $post_id | ||
| 964 | * @param int $size | ||
| 965 | * @param array $filters | ||
| 966 | * @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 | ||
| 967 | * @return array | ||
| 968 | */ | ||
| 969 | 	protected function _get_related_posts( $post_id, $size, array $filters ) { | ||
| 996 | |||
| 997 | /** | ||
| 998 | * Get array of related posts matched by ElasticSearch. | ||
| 999 | * | ||
| 1000 | * @param int $post_id | ||
| 1001 | * @param int $size | ||
| 1002 | * @param array $filters | ||
| 1003 | * @uses wp_remote_post, is_wp_error, wp_remote_retrieve_body, get_post_meta, update_post_meta | ||
| 1004 | * @return array | ||
| 1005 | */ | ||
| 1006 | 	protected function _get_related_post_ids( $post_id, $size, array $filters ) { | ||
| 1007 | $now_ts = time(); | ||
| 1008 | $cache_meta_key = '_jetpack_related_posts_cache'; | ||
| 1009 | |||
| 1010 | $body = array( | ||
| 1011 | 'size' => (int) $size, | ||
| 1012 | ); | ||
| 1013 | |||
| 1014 | if ( !empty( $filters ) ) | ||
| 1015 | $body['filter'] = array( 'and' => $filters ); | ||
| 1016 | |||
| 1017 | // Build cache key | ||
| 1018 | $cache_key = md5( serialize( $body ) ); | ||
| 1019 | |||
| 1020 | // Load all cached values | ||
| 1021 | 		if ( wp_using_ext_object_cache() ) { | ||
| 1022 | 			$transient_name = "{$cache_meta_key}_{$cache_key}_{$post_id}"; | ||
| 1023 | $cache = get_transient( $transient_name ); | ||
| 1024 | 			if ( false !== $cache ) { | ||
| 1025 | return $cache; | ||
| 1026 | } | ||
| 1027 | 		} else { | ||
| 1028 | $cache = get_post_meta( $post_id, $cache_meta_key, true ); | ||
| 1029 | |||
| 1030 | if ( empty( $cache ) ) | ||
| 1031 | $cache = array(); | ||
| 1032 | |||
| 1033 | |||
| 1034 | // Cache is valid! Return cached value. | ||
| 1035 | 			if ( isset( $cache[ $cache_key ] ) && is_array( $cache[ $cache_key ] ) && $cache[ $cache_key ][ 'expires' ] > $now_ts ) { | ||
| 1036 | return $cache[ $cache_key ][ 'payload' ]; | ||
| 1037 | } | ||
| 1038 | } | ||
| 1039 | |||
| 1040 | $response = wp_remote_post( | ||
| 1041 | 			"https://public-api.wordpress.com/rest/v1/sites/{$this->_blog_id_wpcom}/posts/$post_id/related/", | ||
| 1042 | array( | ||
| 1043 | 'timeout' => 10, | ||
| 1044 | 'user-agent' => 'jetpack_related_posts', | ||
| 1045 | 'sslverify' => true, | ||
| 1046 | 'body' => $body, | ||
| 1047 | ) | ||
| 1048 | ); | ||
| 1049 | |||
| 1050 | // Oh no... return nothing don't cache errors. | ||
| 1051 | 		if ( is_wp_error( $response ) ) { | ||
| 1052 | if ( isset( $cache[ $cache_key ] ) && is_array( $cache[ $cache_key ] ) ) | ||
| 1053 | return $cache[ $cache_key ][ 'payload' ]; // return stale | ||
| 1054 | else | ||
| 1055 | return array(); | ||
| 1056 | } | ||
| 1057 | |||
| 1058 | $results = json_decode( wp_remote_retrieve_body( $response ), true ); | ||
| 1059 | $related_posts = array(); | ||
| 1060 | 		if ( is_array( $results ) && !empty( $results['hits'] ) ) { | ||
| 1061 | 			foreach( $results['hits'] as $hit ) { | ||
| 1062 | $related_posts[] = array( | ||
| 1063 | 'id' => $hit['fields']['post_id'], | ||
| 1064 | ); | ||
| 1065 | } | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | // An empty array might indicate no related posts or that posts | ||
| 1069 | // are not yet synced to WordPress.com, so we cache for only 1 | ||
| 1070 | // minute in this case | ||
| 1071 | 		if ( empty( $related_posts ) ) { | ||
| 1072 | $cache_ttl = 60; | ||
| 1073 | 		} else { | ||
| 1074 | $cache_ttl = 12 * HOUR_IN_SECONDS; | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | // Update cache | ||
| 1078 | 		if ( wp_using_ext_object_cache() ) { | ||
| 1079 | set_transient( $transient_name, $related_posts, $cache_ttl ); | ||
| 1080 | 		} else { | ||
| 1081 | // Copy all valid cache values | ||
| 1082 | $new_cache = array(); | ||
| 1083 | 			foreach ( $cache as $k => $v ) { | ||
| 1084 | 				if ( is_array( $v ) && $v[ 'expires' ] > $now_ts ) { | ||
| 1085 | $new_cache[ $k ] = $v; | ||
| 1086 | } | ||
| 1087 | } | ||
| 1088 | |||
| 1089 | // Set new cache value | ||
| 1090 | $cache_expires = $cache_ttl + $now_ts; | ||
| 1091 | $new_cache[ $cache_key ] = array( | ||
| 1092 | 'expires' => $cache_expires, | ||
| 1093 | 'payload' => $related_posts, | ||
| 1094 | ); | ||
| 1095 | update_post_meta( $post_id, $cache_meta_key, $new_cache ); | ||
| 1096 | } | ||
| 1097 | |||
| 1098 | return $related_posts; | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | /** | ||
| 1102 | * Filter out any hits that are not public anymore. | ||
| 1103 | * | ||
| 1104 | * @param array $related_posts | ||
| 1105 | * @uses get_post_stati, get_post_status | ||
| 1106 | * @return array | ||
| 1107 | */ | ||
| 1108 | 	protected function _filter_non_public_posts( array $related_posts ) { | ||
| 1119 | |||
| 1120 | /** | ||
| 1121 | * Generates a context for the related content (second line in related post output). | ||
| 1122 | * Order of importance: | ||
| 1123 | * - First category (Not 'Uncategorized') | ||
| 1124 | * - First post tag | ||
| 1125 | * - Number of comments | ||
| 1126 | * | ||
| 1127 | * @param int $post_id | ||
| 1128 | * @uses get_the_category, get_the_terms, get_comments_number, number_format_i18n, __, _n | ||
| 1129 | * @return string | ||
| 1130 | */ | ||
| 1131 | 	protected function _generate_related_post_context( $post_id ) { | ||
| 1188 | |||
| 1189 | /** | ||
| 1190 | * Logs clicks for clickthrough analysis and related result tuning. | ||
| 1191 | * | ||
| 1192 | * @return null | ||
| 1193 | */ | ||
| 1194 | 	protected function _log_click( $post_id, $to_post_id, $link_position ) { | ||
| 1197 | |||
| 1198 | /** | ||
| 1199 | * Determines if the current post is able to use related posts. | ||
| 1200 | * | ||
| 1201 | * @uses self::get_options, is_admin, is_single, apply_filters | ||
| 1202 | * @return bool | ||
| 1203 | */ | ||
| 1204 | 	protected function _enabled_for_request() { | ||
| 1235 | |||
| 1236 | /** | ||
| 1237 | * Adds filters and enqueues assets. | ||
| 1238 | * | ||
| 1239 | * @uses self::_enqueue_assets, self::_setup_shortcode, add_filter | ||
| 1240 | * @return null | ||
| 1241 | */ | ||
| 1242 | 	protected function _action_frontend_init_page() { | ||
| 1248 | |||
| 1249 | /** | ||
| 1250 | * Enqueues assets needed to do async loading of related posts. | ||
| 1251 | * | ||
| 1252 | * @uses wp_enqueue_script, wp_enqueue_style, plugins_url | ||
| 1253 | * @return null | ||
| 1254 | */ | ||
| 1255 | 	protected function _enqueue_assets( $script, $style ) { | ||
| 1278 | |||
| 1279 | /** | ||
| 1280 | * Sets up the shortcode processing. | ||
| 1281 | * | ||
| 1282 | * @uses add_filter, add_shortcode | ||
| 1283 | * @return null | ||
| 1284 | */ | ||
| 1285 | 	protected function _setup_shortcode() { | ||
| 1290 | |||
| 1291 | 	protected function _allow_feature_toggle() { | ||
| 1306 | } | ||
| 1307 | |||
| 1362 | 
Adding a
@returnannotation 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.