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 | 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 |
||
|
|
|||
| 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() { |
||
| 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() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Render insertion point. |
||
| 151 | * |
||
| 152 | * @since 4.2.0 |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function get_headline() { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 301 | |||
| 302 | public function get_option( $option_name ) { |
||
| 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 ) { |
||
| 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() { |
||
| 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() { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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 | View Code Duplication | 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 ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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() { |
||
| 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() { |
||
| 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 ) { |
||
| 1478 | |||
| 1479 | /** |
||
| 1480 | * Sets up the shortcode processing. |
||
| 1481 | * |
||
| 1482 | * @uses add_filter, add_shortcode |
||
| 1483 | * @return null |
||
| 1484 | */ |
||
| 1485 | protected function _setup_shortcode() { |
||
| 1490 | |||
| 1491 | protected function _allow_feature_toggle() { |
||
| 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() { |
||
| 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 ) { |
||
| 1549 | } |
||
| 1550 | |||
| 1605 |
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.