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 |
||
| 5 | class Jetpack_RelatedPosts { |
||
| 6 | const VERSION = '20190204'; |
||
| 7 | const SHORTCODE = 'jetpack-related-posts'; |
||
| 8 | |||
| 9 | private static $instance = null; |
||
| 10 | private static $instance_raw = null; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Creates and returns a static instance of Jetpack_RelatedPosts. |
||
| 14 | * |
||
| 15 | * @return Jetpack_RelatedPosts |
||
| 16 | */ |
||
| 17 | public static function init() { |
||
| 18 | if ( ! self::$instance ) { |
||
| 19 | if ( class_exists('WPCOM_RelatedPosts') && method_exists( 'WPCOM_RelatedPosts', 'init' ) ) { |
||
| 20 | self::$instance = WPCOM_RelatedPosts::init(); |
||
| 21 | } else { |
||
| 22 | self::$instance = new Jetpack_RelatedPosts(); |
||
| 23 | } |
||
| 24 | } |
||
| 25 | |||
| 26 | return self::$instance; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Creates and returns a static instance of Jetpack_RelatedPosts_Raw. |
||
| 31 | * |
||
| 32 | * @return Jetpack_RelatedPosts |
||
| 33 | */ |
||
| 34 | public static function init_raw() { |
||
| 35 | if ( ! self::$instance_raw ) { |
||
| 36 | if ( class_exists('WPCOM_RelatedPosts') && method_exists( 'WPCOM_RelatedPosts', 'init_raw' ) ) { |
||
| 37 | self::$instance_raw = WPCOM_RelatedPosts::init_raw(); |
||
| 38 | } else { |
||
| 39 | self::$instance_raw = new Jetpack_RelatedPosts_Raw(); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | return self::$instance_raw; |
||
| 44 | } |
||
| 45 | |||
| 46 | protected $_options; |
||
| 47 | protected $_allow_feature_toggle; |
||
| 48 | protected $_blog_charset; |
||
| 49 | protected $_convert_charset; |
||
| 50 | protected $_previous_post_id; |
||
| 51 | protected $_found_shortcode = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor for Jetpack_RelatedPosts. |
||
| 55 | * |
||
| 56 | * @uses get_option, add_action, apply_filters |
||
| 57 | * @return null |
||
|
|
|||
| 58 | */ |
||
| 59 | public function __construct() { |
||
| 60 | $this->_blog_charset = get_option( 'blog_charset' ); |
||
| 61 | $this->_convert_charset = ( function_exists( 'iconv' ) && ! preg_match( '/^utf\-?8$/i', $this->_blog_charset ) ); |
||
| 62 | |||
| 63 | add_action( 'admin_init', array( $this, 'action_admin_init' ) ); |
||
| 64 | add_action( 'wp', array( $this, 'action_frontend_init' ) ); |
||
| 65 | |||
| 66 | if ( ! class_exists( 'Jetpack_Media_Summary' ) ) { |
||
| 67 | jetpack_require_lib( 'class.media-summary' ); |
||
| 68 | } |
||
| 69 | |||
| 70 | // Add Related Posts to the REST API Post response. |
||
| 71 | add_action( 'rest_api_init', array( $this, 'rest_register_related_posts' ) ); |
||
| 72 | |||
| 73 | jetpack_register_block( |
||
| 74 | 'jetpack/related-posts', |
||
| 75 | array( |
||
| 76 | 'render_callback' => array( $this, 'render_block' ), |
||
| 77 | ) |
||
| 78 | ); |
||
| 79 | } |
||
| 80 | |||
| 81 | protected function get_blog_id() { |
||
| 82 | return Jetpack_Options::get_option( 'id' ); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * ================= |
||
| 87 | * ACTIONS & FILTERS |
||
| 88 | * ================= |
||
| 89 | */ |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Add a checkbox field to Settings > Reading for enabling related posts. |
||
| 93 | * |
||
| 94 | * @action admin_init |
||
| 95 | * @uses add_settings_field, __, register_setting, add_action |
||
| 96 | * @return null |
||
| 97 | */ |
||
| 98 | public function action_admin_init() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Load related posts assets if it's a elegiable front end page or execute search and return JSON if it's an endpoint request. |
||
| 113 | * |
||
| 114 | * @global $_GET |
||
| 115 | * @action wp |
||
| 116 | * @uses add_shortcode, get_the_ID |
||
| 117 | * @returns null |
||
| 118 | */ |
||
| 119 | public function action_frontend_init() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Render insertion point. |
||
| 142 | * |
||
| 143 | * @since 4.2.0 |
||
| 144 | * |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function get_headline() { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Adds a target to the post content to load related posts into if a shortcode for it did not already exist. |
||
| 164 | * Will skip adding the target if the post content contains a Related Posts block. |
||
| 165 | * |
||
| 166 | * @filter the_content |
||
| 167 | * @param string $content |
||
| 168 | * @returns string |
||
| 169 | */ |
||
| 170 | public function filter_add_target_to_dom( $content ) { |
||
| 171 | if ( has_block( 'jetpack/related-posts', $content ) ) { |
||
| 172 | return $content; |
||
| 173 | } |
||
| 174 | |||
| 175 | if ( ! $this->_found_shortcode ) { |
||
| 176 | $content .= "\n" . $this->get_target_html(); |
||
| 177 | } |
||
| 178 | |||
| 179 | return $content; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Looks for our shortcode on the unfiltered content, this has to execute early. |
||
| 184 | * |
||
| 185 | * @filter the_content |
||
| 186 | * @param string $content |
||
| 187 | * @uses has_shortcode |
||
| 188 | * @returns string |
||
| 189 | */ |
||
| 190 | public function test_for_shortcode( $content ) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns the HTML for the related posts section. |
||
| 198 | * |
||
| 199 | * @uses esc_html__, apply_filters |
||
| 200 | * @returns string |
||
| 201 | */ |
||
| 202 | public function get_target_html() { |
||
| 203 | if ( Settings::is_syncing() ) { |
||
| 204 | return ''; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Filter the Related Posts headline. |
||
| 209 | * |
||
| 210 | * @module related-posts |
||
| 211 | * |
||
| 212 | * @since 3.0.0 |
||
| 213 | * |
||
| 214 | * @param string $headline Related Posts heading. |
||
| 215 | */ |
||
| 216 | $headline = apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() ); |
||
| 217 | |||
| 218 | if ( $this->_previous_post_id ) { |
||
| 219 | $exclude = "data-exclude='{$this->_previous_post_id}'"; |
||
| 220 | } else { |
||
| 221 | $exclude = ""; |
||
| 222 | } |
||
| 223 | |||
| 224 | return <<<EOT |
||
| 225 | <div id='jp-relatedposts' class='jp-relatedposts' $exclude> |
||
| 226 | $headline |
||
| 227 | </div> |
||
| 228 | EOT; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * 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. |
||
| 233 | * |
||
| 234 | * @returns string |
||
| 235 | */ |
||
| 236 | public function get_target_html_unsupported() { |
||
| 237 | if ( Settings::is_syncing() ) { |
||
| 238 | return ''; |
||
| 239 | } |
||
| 240 | return "\n\n<!-- Jetpack Related Posts is not supported in this context. -->\n\n"; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * =============== |
||
| 245 | * GUTENBERG BLOCK |
||
| 246 | * =============== |
||
| 247 | */ |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Echoes out items for the Gutenberg block |
||
| 251 | * |
||
| 252 | * @param array $related_post The post oject. |
||
| 253 | * @param array $block_attributes The block attributes. |
||
| 254 | */ |
||
| 255 | public function render_block_item( $related_post, $block_attributes ) { |
||
| 256 | $instance_id = 'related-posts-item-' . uniqid(); |
||
| 257 | $label_id = $instance_id . '-label'; |
||
| 258 | |||
| 259 | $item_markup = sprintf( |
||
| 260 | '<ul id="%1$s" aria-labelledby="%2$s" class="jp-related-posts-i2__post" role="menuitem">', |
||
| 261 | esc_attr( $instance_id ), |
||
| 262 | esc_attr( $label_id ) |
||
| 263 | ); |
||
| 264 | |||
| 265 | $item_markup .= sprintf( |
||
| 266 | '<li class="jp-related-posts-i2__post-link"><a id="%1$s" href="%2$s" rel="%4$s">%3$s</a></li>', |
||
| 267 | esc_attr( $label_id ), |
||
| 268 | esc_url( $related_post['url'] ), |
||
| 269 | esc_attr( $related_post['title'] ), |
||
| 270 | esc_attr( $related_post['rel'] ) |
||
| 271 | ); |
||
| 272 | |||
| 273 | if ( ! empty( $block_attributes['show_thumbnails'] ) && ! empty( $related_post['img']['src'] ) ) { |
||
| 274 | $img_link = sprintf( |
||
| 275 | '<li class="jp-related-posts-i2__post-img-link"><a href="%1$s" rel="%2$s"><img src="%3$s" width="%4$s" alt="%5$s" /></a></li>', |
||
| 276 | esc_url( $related_post['url'] ), |
||
| 277 | esc_attr( $related_post['rel'] ), |
||
| 278 | esc_url( $related_post['img']['src'] ), |
||
| 279 | esc_attr( $related_post['img']['width'] ), |
||
| 280 | esc_attr( $related_post['img']['alt_text'] ) |
||
| 281 | ); |
||
| 282 | |||
| 283 | $item_markup .= $img_link; |
||
| 284 | } |
||
| 285 | |||
| 286 | if ( $block_attributes['show_date'] ) { |
||
| 287 | $date_tag = sprintf( |
||
| 288 | '<li class="jp-related-posts-i2__post-date">%1$s</li>', |
||
| 289 | esc_html( $related_post['date'] ) |
||
| 290 | ); |
||
| 291 | |||
| 292 | $item_markup .= $date_tag; |
||
| 293 | } |
||
| 294 | |||
| 295 | if ( ( $block_attributes['show_context'] ) && ! empty( $related_post['context'] ) ) { |
||
| 296 | $context_tag = sprintf( |
||
| 297 | '<li class="jp-related-posts-i2__post-context">%1$s</li>', |
||
| 298 | esc_html( $related_post['context'] ) |
||
| 299 | ); |
||
| 300 | |||
| 301 | $item_markup .= $context_tag; |
||
| 302 | } |
||
| 303 | |||
| 304 | $item_markup .= '</ul>'; |
||
| 305 | |||
| 306 | return $item_markup; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Render a related posts row. |
||
| 311 | * |
||
| 312 | * @param array $posts The posts to render into the row. |
||
| 313 | * @param array $block_attributes Block attributes. |
||
| 314 | */ |
||
| 315 | public function render_block_row( $posts, $block_attributes ) { |
||
| 316 | $rows_markup = ''; |
||
| 317 | foreach ( $posts as $post ) { |
||
| 318 | $rows_markup .= $this->render_block_item( $post, $block_attributes ); |
||
| 319 | } |
||
| 320 | return sprintf( |
||
| 321 | '<div class="jp-related-posts-i2__row" data-post-count="%1$s">%2$s</div>', |
||
| 322 | count( $posts ), |
||
| 323 | $rows_markup |
||
| 324 | ); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Render the related posts markup. |
||
| 329 | * |
||
| 330 | * @param array $attributes Block attributes. |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function render_block( $attributes ) { |
||
| 334 | $block_attributes = array( |
||
| 335 | 'show_thumbnails' => isset( $attributes['displayThumbnails'] ) && $attributes['displayThumbnails'], |
||
| 336 | 'show_date' => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true, |
||
| 337 | 'show_context' => isset( $attributes['displayContext'] ) && $attributes['displayContext'], |
||
| 338 | 'layout' => isset( $attributes['postLayout'] ) && 'list' === $attributes['postLayout'] ? $attributes['postLayout'] : 'grid', |
||
| 339 | 'size' => ! empty( $attributes['postsToShow'] ) ? absint( $attributes['postsToShow'] ) : 3, |
||
| 340 | ); |
||
| 341 | |||
| 342 | $excludes = $this->parse_numeric_get_arg( 'relatedposts_origin' ); |
||
| 343 | $related_posts = $this->get_for_post_id( |
||
| 344 | get_the_ID(), |
||
| 345 | array( |
||
| 346 | 'size' => $block_attributes['size'], |
||
| 347 | 'exclude_post_ids' => $excludes, |
||
| 348 | ) |
||
| 349 | ); |
||
| 350 | |||
| 351 | $display_lower_row = $block_attributes['size'] > 3; |
||
| 352 | |||
| 353 | if ( empty( $related_posts ) ) { |
||
| 354 | return ''; |
||
| 355 | } |
||
| 356 | |||
| 357 | switch ( count( $related_posts ) ) { |
||
| 358 | case 2: |
||
| 359 | case 4: |
||
| 360 | case 5: |
||
| 361 | $top_row_end = 2; |
||
| 362 | break; |
||
| 363 | |||
| 364 | default: |
||
| 365 | $top_row_end = 3; |
||
| 366 | break; |
||
| 367 | } |
||
| 368 | |||
| 369 | $upper_row_posts = array_slice( $related_posts, 0, $top_row_end ); |
||
| 370 | $lower_row_posts = array_slice( $related_posts, $top_row_end ); |
||
| 371 | |||
| 372 | $rows_markup = $this->render_block_row( $upper_row_posts, $block_attributes ); |
||
| 373 | if ( $display_lower_row ) { |
||
| 374 | $rows_markup .= $this->render_block_row( $lower_row_posts, $block_attributes ); |
||
| 375 | } |
||
| 376 | |||
| 377 | $target_to_dom_priority = has_filter( |
||
| 378 | 'the_content', |
||
| 379 | array( $this, 'filter_add_target_to_dom' ) |
||
| 380 | ); |
||
| 381 | remove_filter( |
||
| 382 | 'the_content', |
||
| 383 | array( $this, 'filter_add_target_to_dom' ), |
||
| 384 | $target_to_dom_priority |
||
| 385 | ); |
||
| 386 | |||
| 387 | /* |
||
| 388 | * Below is a hack to get the block content to render correctly. |
||
| 389 | * |
||
| 390 | * This functionality should be covered in /inc/blocks.php but due to an error, |
||
| 391 | * this has not been fixed as of this writing. |
||
| 392 | * |
||
| 393 | * Alda has submitted a patch to Core in order to have this issue fixed at |
||
| 394 | * https://core.trac.wordpress.org/ticket/45495 and |
||
| 395 | * made it into WordPress 5.2. |
||
| 396 | * |
||
| 397 | * @todo update when WP 5.2 is the minimum support version. |
||
| 398 | */ |
||
| 399 | $priority = has_filter( 'the_content', 'wpautop' ); |
||
| 400 | remove_filter( 'the_content', 'wpautop', $priority ); |
||
| 401 | add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 ); |
||
| 402 | |||
| 403 | return sprintf( |
||
| 404 | '<nav class="jp-relatedposts-i2" data-layout="%1$s">%2$s</nav>', |
||
| 405 | esc_attr( $block_attributes['layout'] ), |
||
| 406 | $rows_markup |
||
| 407 | ); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * ======================== |
||
| 412 | * PUBLIC UTILITY FUNCTIONS |
||
| 413 | * ======================== |
||
| 414 | */ |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Parse a numeric GET variable to an array of values. |
||
| 418 | * |
||
| 419 | * @since 6.9.0 |
||
| 420 | * |
||
| 421 | * @uses absint |
||
| 422 | * |
||
| 423 | * @param string $arg Name of the GET variable |
||
| 424 | * @return array $result Parsed value(s) |
||
| 425 | */ |
||
| 426 | public function parse_numeric_get_arg( $arg ) { |
||
| 427 | $result = array(); |
||
| 428 | |||
| 429 | if ( isset( $_GET[ $arg ] ) ) { |
||
| 430 | if ( is_string( $_GET[ $arg ] ) ) { |
||
| 431 | $result = explode( ',', $_GET[ $arg ] ); |
||
| 432 | } elseif ( is_array( $_GET[ $arg ] ) ) { |
||
| 433 | $result = array_values( $_GET[ $arg ] ); |
||
| 434 | } |
||
| 435 | |||
| 436 | $result = array_unique( array_filter( array_map( 'absint', $result ) ) ); |
||
| 437 | } |
||
| 438 | |||
| 439 | return $result; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Gets options set for Jetpack_RelatedPosts and merge with defaults. |
||
| 444 | * |
||
| 445 | * @uses Jetpack_Options::get_option, apply_filters |
||
| 446 | * @return array |
||
| 447 | */ |
||
| 448 | public function get_options() { |
||
| 449 | if ( null === $this->_options ) { |
||
| 450 | $this->_options = Jetpack_Options::get_option( 'relatedposts', array() ); |
||
| 451 | if ( ! is_array( $this->_options ) ) |
||
| 452 | $this->_options = array(); |
||
| 453 | if ( ! isset( $this->_options['enabled'] ) ) |
||
| 454 | $this->_options['enabled'] = true; |
||
| 455 | if ( ! isset( $this->_options['show_headline'] ) ) |
||
| 456 | $this->_options['show_headline'] = true; |
||
| 457 | if ( ! isset( $this->_options['show_thumbnails'] ) ) |
||
| 458 | $this->_options['show_thumbnails'] = false; |
||
| 459 | if ( ! isset( $this->_options['show_date'] ) ) { |
||
| 460 | $this->_options['show_date'] = true; |
||
| 461 | } |
||
| 462 | if ( ! isset( $this->_options['show_context'] ) ) { |
||
| 463 | $this->_options['show_context'] = true; |
||
| 464 | } |
||
| 465 | if ( ! isset( $this->_options['layout'] ) ) { |
||
| 466 | $this->_options['layout'] = 'grid'; |
||
| 467 | } |
||
| 468 | if ( ! isset( $this->_options['headline'] ) ) { |
||
| 469 | $this->_options['headline'] = esc_html__( 'Related', 'jetpack' ); |
||
| 470 | } |
||
| 471 | if ( empty( $this->_options['size'] ) || (int)$this->_options['size'] < 1 ) |
||
| 472 | $this->_options['size'] = 3; |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Filter Related Posts basic options. |
||
| 476 | * |
||
| 477 | * @module related-posts |
||
| 478 | * |
||
| 479 | * @since 2.8.0 |
||
| 480 | * |
||
| 481 | * @param array $this->_options Array of basic Related Posts options. |
||
| 482 | */ |
||
| 483 | $this->_options = apply_filters( 'jetpack_relatedposts_filter_options', $this->_options ); |
||
| 484 | } |
||
| 485 | |||
| 486 | return $this->_options; |
||
| 487 | } |
||
| 488 | |||
| 489 | public function get_option( $option_name ) { |
||
| 490 | $options = $this->get_options(); |
||
| 491 | |||
| 492 | if ( isset( $options[ $option_name ] ) ) { |
||
| 493 | return $options[ $option_name ]; |
||
| 494 | } |
||
| 495 | |||
| 496 | return false; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Parses input and returns normalized options array. |
||
| 501 | * |
||
| 502 | * @param array $input |
||
| 503 | * @uses self::get_options |
||
| 504 | * @return array |
||
| 505 | */ |
||
| 506 | public function parse_options( $input ) { |
||
| 507 | $current = $this->get_options(); |
||
| 508 | |||
| 509 | if ( !is_array( $input ) ) |
||
| 510 | $input = array(); |
||
| 511 | |||
| 512 | if ( |
||
| 513 | ! isset( $input['enabled'] ) |
||
| 514 | || isset( $input['show_date'] ) |
||
| 515 | || isset( $input['show_context'] ) |
||
| 516 | || isset( $input['layout'] ) |
||
| 517 | || isset( $input['headline'] ) |
||
| 518 | ) { |
||
| 519 | $input['enabled'] = '1'; |
||
| 520 | } |
||
| 521 | |||
| 522 | if ( '1' == $input['enabled'] ) { |
||
| 523 | $current['enabled'] = true; |
||
| 524 | $current['show_headline'] = ( isset( $input['show_headline'] ) && '1' == $input['show_headline'] ); |
||
| 525 | $current['show_thumbnails'] = ( isset( $input['show_thumbnails'] ) && '1' == $input['show_thumbnails'] ); |
||
| 526 | $current['show_date'] = ( isset( $input['show_date'] ) && '1' == $input['show_date'] ); |
||
| 527 | $current['show_context'] = ( isset( $input['show_context'] ) && '1' == $input['show_context'] ); |
||
| 528 | $current['layout'] = isset( $input['layout'] ) && in_array( $input['layout'], array( 'grid', 'list' ), true ) ? $input['layout'] : 'grid'; |
||
| 529 | $current['headline'] = isset( $input['headline'] ) ? $input['headline'] : esc_html__( 'Related', 'jetpack' ); |
||
| 530 | } else { |
||
| 531 | $current['enabled'] = false; |
||
| 532 | } |
||
| 533 | |||
| 534 | if ( isset( $input['size'] ) && (int)$input['size'] > 0 ) |
||
| 535 | $current['size'] = (int)$input['size']; |
||
| 536 | else |
||
| 537 | $current['size'] = null; |
||
| 538 | |||
| 539 | return $current; |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * HTML for admin settings page. |
||
| 544 | * |
||
| 545 | * @uses self::get_options, checked, esc_html__ |
||
| 546 | * @returns null |
||
| 547 | */ |
||
| 548 | public function print_setting_html() { |
||
| 549 | $options = $this->get_options(); |
||
| 550 | |||
| 551 | $ui_settings_template = <<<EOT |
||
| 552 | <p class="description">%s</p> |
||
| 553 | <ul id="settings-reading-relatedposts-customize"> |
||
| 554 | <li> |
||
| 555 | <label><input name="jetpack_relatedposts[show_headline]" type="checkbox" value="1" %s /> %s</label> |
||
| 556 | </li> |
||
| 557 | <li> |
||
| 558 | <label><input name="jetpack_relatedposts[show_thumbnails]" type="checkbox" value="1" %s /> %s</label> |
||
| 559 | </li> |
||
| 560 | <li> |
||
| 561 | <label><input name="jetpack_relatedposts[show_date]" type="checkbox" value="1" %s /> %s</label> |
||
| 562 | </li> |
||
| 563 | <li> |
||
| 564 | <label><input name="jetpack_relatedposts[show_context]" type="checkbox" value="1" %s /> %s</label> |
||
| 565 | </li> |
||
| 566 | </ul> |
||
| 567 | <div id='settings-reading-relatedposts-preview'> |
||
| 568 | %s |
||
| 569 | <div id="jp-relatedposts" class="jp-relatedposts"></div> |
||
| 570 | </div> |
||
| 571 | EOT; |
||
| 572 | $ui_settings = sprintf( |
||
| 573 | $ui_settings_template, |
||
| 574 | esc_html__( 'The following settings will impact all related posts on your site, except for those you created via the block editor:', 'jetpack' ), |
||
| 575 | checked( $options['show_headline'], true, false ), |
||
| 576 | esc_html__( 'Highlight related content with a heading', 'jetpack' ), |
||
| 577 | checked( $options['show_thumbnails'], true, false ), |
||
| 578 | esc_html__( 'Show a thumbnail image where available', 'jetpack' ), |
||
| 579 | checked( $options['show_date'], true, false ), |
||
| 580 | esc_html__( 'Show entry date', 'jetpack' ), |
||
| 581 | checked( $options['show_context'], true, false ), |
||
| 582 | esc_html__( 'Show context (category or tag)', 'jetpack' ), |
||
| 583 | esc_html__( 'Preview:', 'jetpack' ) |
||
| 584 | ); |
||
| 585 | |||
| 586 | if ( !$this->_allow_feature_toggle() ) { |
||
| 587 | $template = <<<EOT |
||
| 588 | <input type="hidden" name="jetpack_relatedposts[enabled]" value="1" /> |
||
| 589 | %s |
||
| 590 | EOT; |
||
| 591 | printf( |
||
| 592 | $template, |
||
| 593 | $ui_settings |
||
| 594 | ); |
||
| 595 | } else { |
||
| 596 | $template = <<<EOT |
||
| 597 | <ul id="settings-reading-relatedposts"> |
||
| 598 | <li> |
||
| 599 | <label><input type="radio" name="jetpack_relatedposts[enabled]" value="0" class="tog" %s /> %s</label> |
||
| 600 | </li> |
||
| 601 | <li> |
||
| 602 | <label><input type="radio" name="jetpack_relatedposts[enabled]" value="1" class="tog" %s /> %s</label> |
||
| 603 | %s |
||
| 604 | </li> |
||
| 605 | </ul> |
||
| 606 | EOT; |
||
| 607 | printf( |
||
| 608 | $template, |
||
| 609 | checked( $options['enabled'], false, false ), |
||
| 610 | esc_html__( 'Hide related content after posts', 'jetpack' ), |
||
| 611 | checked( $options['enabled'], true, false ), |
||
| 612 | esc_html__( 'Show related content after posts', 'jetpack' ), |
||
| 613 | $ui_settings |
||
| 614 | ); |
||
| 615 | } |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Head JS/CSS for admin settings page. |
||
| 620 | * |
||
| 621 | * @uses esc_html__ |
||
| 622 | * @returns null |
||
| 623 | */ |
||
| 624 | public function print_setting_head() { |
||
| 625 | |||
| 626 | // only dislay the Related Posts JavaScript on the Reading Settings Admin Page |
||
| 627 | $current_screen = get_current_screen(); |
||
| 628 | |||
| 629 | if ( is_null( $current_screen ) ) { |
||
| 630 | return; |
||
| 631 | } |
||
| 632 | |||
| 633 | if( 'options-reading' != $current_screen->id ) |
||
| 634 | return; |
||
| 635 | |||
| 636 | $related_headline = sprintf( |
||
| 637 | '<h3 class="jp-relatedposts-headline"><em>%s</em></h3>', |
||
| 638 | esc_html__( 'Related', 'jetpack' ) |
||
| 639 | ); |
||
| 640 | |||
| 641 | $href_params = 'class="jp-relatedposts-post-a" href="#jetpack_relatedposts" rel="nofollow" data-origin="0" data-position="0"'; |
||
| 642 | $related_with_images = <<<EOT |
||
| 643 | <div class="jp-relatedposts-items jp-relatedposts-items-visual"> |
||
| 644 | <div class="jp-relatedposts-post jp-relatedposts-post0 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image"> |
||
| 645 | <a $href_params> |
||
| 646 | <img class="jp-relatedposts-post-img" src="https://jetpackme.files.wordpress.com/2019/03/cat-blog.png" width="350" alt="Big iPhone/iPad Update Now Available" scale="0"> |
||
| 647 | </a> |
||
| 648 | <h4 class="jp-relatedposts-post-title"> |
||
| 649 | <a $href_params>Big iPhone/iPad Update Now Available</a> |
||
| 650 | </h4> |
||
| 651 | <p class="jp-relatedposts-post-excerpt">Big iPhone/iPad Update Now Available</p> |
||
| 652 | <p class="jp-relatedposts-post-context">In "Mobile"</p> |
||
| 653 | </div> |
||
| 654 | <div class="jp-relatedposts-post jp-relatedposts-post1 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image"> |
||
| 655 | <a $href_params> |
||
| 656 | <img class="jp-relatedposts-post-img" src="https://jetpackme.files.wordpress.com/2019/03/devices.jpg" width="350" alt="The WordPress for Android App Gets a Big Facelift" scale="0"> |
||
| 657 | </a> |
||
| 658 | <h4 class="jp-relatedposts-post-title"> |
||
| 659 | <a $href_params>The WordPress for Android App Gets a Big Facelift</a> |
||
| 660 | </h4> |
||
| 661 | <p class="jp-relatedposts-post-excerpt">The WordPress for Android App Gets a Big Facelift</p> |
||
| 662 | <p class="jp-relatedposts-post-context">In "Mobile"</p> |
||
| 663 | </div> |
||
| 664 | <div class="jp-relatedposts-post jp-relatedposts-post2 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image"> |
||
| 665 | <a $href_params> |
||
| 666 | <img class="jp-relatedposts-post-img" src="https://jetpackme.files.wordpress.com/2019/03/mobile-wedding.jpg" width="350" alt="Upgrade Focus: VideoPress For Weddings" scale="0"> |
||
| 667 | </a> |
||
| 668 | <h4 class="jp-relatedposts-post-title"> |
||
| 669 | <a $href_params>Upgrade Focus: VideoPress For Weddings</a> |
||
| 670 | </h4> |
||
| 671 | <p class="jp-relatedposts-post-excerpt">Upgrade Focus: VideoPress For Weddings</p> |
||
| 672 | <p class="jp-relatedposts-post-context">In "Upgrade"</p> |
||
| 673 | </div> |
||
| 674 | </div> |
||
| 675 | EOT; |
||
| 676 | $related_with_images = str_replace( "\n", '', $related_with_images ); |
||
| 677 | $related_without_images = <<<EOT |
||
| 678 | <div class="jp-relatedposts-items jp-relatedposts-items-minimal"> |
||
| 679 | <p class="jp-relatedposts-post jp-relatedposts-post0" data-post-id="0" data-post-format="image"> |
||
| 680 | <span class="jp-relatedposts-post-title"><a $href_params>Big iPhone/iPad Update Now Available</a></span> |
||
| 681 | <span class="jp-relatedposts-post-context">In "Mobile"</span> |
||
| 682 | </p> |
||
| 683 | <p class="jp-relatedposts-post jp-relatedposts-post1" data-post-id="0" data-post-format="image"> |
||
| 684 | <span class="jp-relatedposts-post-title"><a $href_params>The WordPress for Android App Gets a Big Facelift</a></span> |
||
| 685 | <span class="jp-relatedposts-post-context">In "Mobile"</span> |
||
| 686 | </p> |
||
| 687 | <p class="jp-relatedposts-post jp-relatedposts-post2" data-post-id="0" data-post-format="image"> |
||
| 688 | <span class="jp-relatedposts-post-title"><a $href_params>Upgrade Focus: VideoPress For Weddings</a></span> |
||
| 689 | <span class="jp-relatedposts-post-context">In "Upgrade"</span> |
||
| 690 | </p> |
||
| 691 | </div> |
||
| 692 | EOT; |
||
| 693 | $related_without_images = str_replace( "\n", '', $related_without_images ); |
||
| 694 | |||
| 695 | if ( $this->_allow_feature_toggle() ) { |
||
| 696 | $extra_css = '#settings-reading-relatedposts-customize { padding-left:2em; margin-top:.5em; }'; |
||
| 697 | } else { |
||
| 698 | $extra_css = ''; |
||
| 699 | } |
||
| 700 | |||
| 701 | echo <<<EOT |
||
| 702 | <style type="text/css"> |
||
| 703 | #settings-reading-relatedposts .disabled { opacity:.5; filter:Alpha(opacity=50); } |
||
| 704 | #settings-reading-relatedposts-preview .jp-relatedposts { background:#fff; padding:.5em; width:75%; } |
||
| 705 | $extra_css |
||
| 706 | </style> |
||
| 707 | <script type="text/javascript"> |
||
| 708 | jQuery( document ).ready( function($) { |
||
| 709 | var update_ui = function() { |
||
| 710 | var is_enabled = true; |
||
| 711 | if ( 'radio' == $( 'input[name="jetpack_relatedposts[enabled]"]' ).attr('type') ) { |
||
| 712 | if ( '0' == $( 'input[name="jetpack_relatedposts[enabled]"]:checked' ).val() ) { |
||
| 713 | is_enabled = false; |
||
| 714 | } |
||
| 715 | } |
||
| 716 | if ( is_enabled ) { |
||
| 717 | $( '#settings-reading-relatedposts-customize' ) |
||
| 718 | .removeClass( 'disabled' ) |
||
| 719 | .find( 'input' ) |
||
| 720 | .attr( 'disabled', false ); |
||
| 721 | $( '#settings-reading-relatedposts-preview' ) |
||
| 722 | .removeClass( 'disabled' ); |
||
| 723 | } else { |
||
| 724 | $( '#settings-reading-relatedposts-customize' ) |
||
| 725 | .addClass( 'disabled' ) |
||
| 726 | .find( 'input' ) |
||
| 727 | .attr( 'disabled', true ); |
||
| 728 | $( '#settings-reading-relatedposts-preview' ) |
||
| 729 | .addClass( 'disabled' ); |
||
| 730 | } |
||
| 731 | }; |
||
| 732 | |||
| 733 | var update_preview = function() { |
||
| 734 | var html = ''; |
||
| 735 | if ( $( 'input[name="jetpack_relatedposts[show_headline]"]:checked' ).length ) { |
||
| 736 | html += '$related_headline'; |
||
| 737 | } |
||
| 738 | if ( $( 'input[name="jetpack_relatedposts[show_thumbnails]"]:checked' ).length ) { |
||
| 739 | html += '$related_with_images'; |
||
| 740 | } else { |
||
| 741 | html += '$related_without_images'; |
||
| 742 | } |
||
| 743 | $( '#settings-reading-relatedposts-preview .jp-relatedposts' ).html( html ); |
||
| 744 | if ( $( 'input[name="jetpack_relatedposts[show_date]"]:checked' ).length ) { |
||
| 745 | $( '.jp-relatedposts-post-title' ).each( function() { |
||
| 746 | $( this ).after( $( '<span>August 8, 2005</span>' ) ); |
||
| 747 | } ); |
||
| 748 | } |
||
| 749 | if ( $( 'input[name="jetpack_relatedposts[show_context]"]:checked' ).length ) { |
||
| 750 | $( '.jp-relatedposts-post-context' ).show(); |
||
| 751 | } else { |
||
| 752 | $( '.jp-relatedposts-post-context' ).hide(); |
||
| 753 | } |
||
| 754 | $( '#settings-reading-relatedposts-preview .jp-relatedposts' ).show(); |
||
| 755 | }; |
||
| 756 | |||
| 757 | // Update on load |
||
| 758 | update_preview(); |
||
| 759 | update_ui(); |
||
| 760 | |||
| 761 | // Update on change |
||
| 762 | $( '#settings-reading-relatedposts-customize input' ) |
||
| 763 | .change( update_preview ); |
||
| 764 | $( '#settings-reading-relatedposts' ) |
||
| 765 | .find( 'input.tog' ) |
||
| 766 | .change( update_ui ); |
||
| 767 | }); |
||
| 768 | </script> |
||
| 769 | EOT; |
||
| 770 | } |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Gets an array of related posts that match the given post_id. |
||
| 774 | * |
||
| 775 | * @param int $post_id Post which we want to find related posts for. |
||
| 776 | * @param array $args - params to use when building Elasticsearch filters to narrow down the search domain. |
||
| 777 | * @uses self::get_options, get_post_type, wp_parse_args, apply_filters |
||
| 778 | * @return array |
||
| 779 | */ |
||
| 780 | public function get_for_post_id( $post_id, array $args ) { |
||
| 781 | $options = $this->get_options(); |
||
| 782 | |||
| 783 | if ( ! empty( $args['size'] ) ) { |
||
| 784 | $options['size'] = $args['size']; |
||
| 785 | } |
||
| 786 | |||
| 787 | if ( |
||
| 788 | ! $options['enabled'] |
||
| 789 | || 0 === (int) $post_id |
||
| 790 | || empty( $options['size'] ) |
||
| 791 | ) { |
||
| 792 | return array(); |
||
| 793 | } |
||
| 794 | |||
| 795 | $defaults = array( |
||
| 796 | 'size' => (int) $options['size'], |
||
| 797 | 'post_type' => get_post_type( $post_id ), |
||
| 798 | 'post_formats' => array(), |
||
| 799 | 'has_terms' => array(), |
||
| 800 | 'date_range' => array(), |
||
| 801 | 'exclude_post_ids' => array(), |
||
| 802 | ); |
||
| 803 | $args = wp_parse_args( $args, $defaults ); |
||
| 804 | /** |
||
| 805 | * Filter the arguments used to retrieve a list of Related Posts. |
||
| 806 | * |
||
| 807 | * @module related-posts |
||
| 808 | * |
||
| 809 | * @since 2.8.0 |
||
| 810 | * |
||
| 811 | * @param array $args Array of options to retrieve Related Posts. |
||
| 812 | * @param string $post_id Post ID of the post for which we are retrieving Related Posts. |
||
| 813 | */ |
||
| 814 | $args = apply_filters( 'jetpack_relatedposts_filter_args', $args, $post_id ); |
||
| 815 | |||
| 816 | $filters = $this->_get_es_filters_from_args( $post_id, $args ); |
||
| 817 | /** |
||
| 818 | * Filter Elasticsearch options used to calculate Related Posts. |
||
| 819 | * |
||
| 820 | * @module related-posts |
||
| 821 | * |
||
| 822 | * @since 2.8.0 |
||
| 823 | * |
||
| 824 | * @param array $filters Array of Elasticsearch filters based on the post_id and args. |
||
| 825 | * @param string $post_id Post ID of the post for which we are retrieving Related Posts. |
||
| 826 | */ |
||
| 827 | $filters = apply_filters( 'jetpack_relatedposts_filter_filters', $filters, $post_id ); |
||
| 828 | |||
| 829 | $results = $this->_get_related_posts( $post_id, $args['size'], $filters ); |
||
| 830 | /** |
||
| 831 | * Filter the array of related posts matched by Elasticsearch. |
||
| 832 | * |
||
| 833 | * @module related-posts |
||
| 834 | * |
||
| 835 | * @since 2.8.0 |
||
| 836 | * |
||
| 837 | * @param array $results Array of related posts matched by Elasticsearch. |
||
| 838 | * @param string $post_id Post ID of the post for which we are retrieving Related Posts. |
||
| 839 | */ |
||
| 840 | return apply_filters( 'jetpack_relatedposts_returned_results', $results, $post_id ); |
||
| 841 | } |
||
| 842 | |||
| 843 | /** |
||
| 844 | * ========================= |
||
| 845 | * PRIVATE UTILITY FUNCTIONS |
||
| 846 | * ========================= |
||
| 847 | */ |
||
| 848 | |||
| 849 | /** |
||
| 850 | * Creates an array of Elasticsearch filters based on the post_id and args. |
||
| 851 | * |
||
| 852 | * @param int $post_id |
||
| 853 | * @param array $args |
||
| 854 | * @uses apply_filters, get_post_types, get_post_format_strings |
||
| 855 | * @return array |
||
| 856 | */ |
||
| 857 | protected function _get_es_filters_from_args( $post_id, array $args ) { |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Takes a range and coalesces it into a month interval bracketed by a time as determined by the blog_id to enhance caching. |
||
| 984 | * |
||
| 985 | * @param array $date_range |
||
| 986 | * @return array |
||
| 987 | */ |
||
| 988 | protected function _get_coalesced_range( array $date_range ) { |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Generate and output ajax response for related posts API call. |
||
| 1010 | * NOTE: Calls exit() to end all further processing after payload has been outputed. |
||
| 1011 | * |
||
| 1012 | * @param array $excludes array of post_ids to exclude |
||
| 1013 | * @uses send_nosniff_header, self::get_for_post_id, get_the_ID |
||
| 1014 | * @return null |
||
| 1015 | */ |
||
| 1016 | protected function _action_frontend_init_ajax( array $excludes ) { |
||
| 1166 | |||
| 1167 | /** |
||
| 1168 | * Returns a UTF-8 encoded array of post information for the given post_id |
||
| 1169 | * |
||
| 1170 | * @param int $post_id |
||
| 1171 | * @param int $position |
||
| 1172 | * @param int $origin The post id that this is related to |
||
| 1173 | * @uses get_post, get_permalink, remove_query_arg, get_post_format, apply_filters |
||
| 1174 | * @return array |
||
| 1175 | */ |
||
| 1176 | public function get_related_post_data_for_post( $post_id, $position, $origin ) { |
||
| 1231 | |||
| 1232 | /** |
||
| 1233 | * Returns either the title or a small excerpt to use as title for post. |
||
| 1234 | * |
||
| 1235 | * @param string $post_title |
||
| 1236 | * @param string $post_content |
||
| 1237 | * @uses strip_shortcodes, wp_trim_words, __ |
||
| 1238 | * @return string |
||
| 1239 | */ |
||
| 1240 | protected function _get_title( $post_title, $post_content ) { |
||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * Returns a plain text post excerpt for title attribute of links. |
||
| 1255 | * |
||
| 1256 | * @param string $post_excerpt |
||
| 1257 | * @param string $post_content |
||
| 1258 | * @uses strip_shortcodes, wp_strip_all_tags, wp_trim_words |
||
| 1259 | * @return string |
||
| 1260 | */ |
||
| 1261 | protected function _get_excerpt( $post_excerpt, $post_content ) { |
||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * Generates the thumbnail image to be used for the post. Uses the |
||
| 1272 | * image as returned by Jetpack_PostImages::get_image() |
||
| 1273 | * |
||
| 1274 | * @param int $post_id |
||
| 1275 | * @uses self::get_options, apply_filters, Jetpack_PostImages::get_image, Jetpack_PostImages::fit_image_url |
||
| 1276 | * @return string |
||
| 1277 | */ |
||
| 1278 | protected function _generate_related_post_image_params( $post_id ) { |
||
| 1279 | $options = $this->get_options(); |
||
| 1280 | $image_params = array( |
||
| 1281 | 'alt_text' => '', |
||
| 1282 | 'src' => '', |
||
| 1283 | 'width' => 0, |
||
| 1284 | 'height' => 0, |
||
| 1285 | ); |
||
| 1286 | |||
| 1287 | /** |
||
| 1288 | * Filter the size of the Related Posts images. |
||
| 1289 | * |
||
| 1290 | * @module related-posts |
||
| 1291 | * |
||
| 1292 | * @since 2.8.0 |
||
| 1293 | * |
||
| 1294 | * @param array array( 'width' => 350, 'height' => 200 ) Size of the images displayed below each Related Post. |
||
| 1295 | */ |
||
| 1296 | $thumbnail_size = apply_filters( |
||
| 1297 | 'jetpack_relatedposts_filter_thumbnail_size', |
||
| 1298 | array( 'width' => 350, 'height' => 200 ) |
||
| 1299 | ); |
||
| 1300 | if ( !is_array( $thumbnail_size ) ) { |
||
| 1301 | $thumbnail_size = array( |
||
| 1302 | 'width' => (int)$thumbnail_size, |
||
| 1303 | 'height' => (int)$thumbnail_size |
||
| 1304 | ); |
||
| 1305 | } |
||
| 1306 | |||
| 1307 | // Try to get post image |
||
| 1308 | if ( class_exists( 'Jetpack_PostImages' ) ) { |
||
| 1309 | $img_url = ''; |
||
| 1310 | $post_image = Jetpack_PostImages::get_image( |
||
| 1311 | $post_id, |
||
| 1312 | $thumbnail_size |
||
| 1313 | ); |
||
| 1314 | |||
| 1315 | if ( is_array($post_image) ) { |
||
| 1316 | $img_url = $post_image['src']; |
||
| 1317 | } elseif ( class_exists( 'Jetpack_Media_Summary' ) ) { |
||
| 1318 | $media = Jetpack_Media_Summary::get( $post_id ); |
||
| 1319 | |||
| 1320 | if ( is_array($media) && !empty( $media['image'] ) ) { |
||
| 1321 | $img_url = $media['image']; |
||
| 1322 | } |
||
| 1323 | } |
||
| 1324 | |||
| 1325 | if ( ! empty( $img_url ) ) { |
||
| 1326 | if ( ! empty( $post_image['alt_text'] ) ) { |
||
| 1327 | $image_params['alt_text'] = $post_image['alt_text']; |
||
| 1328 | } else { |
||
| 1329 | $image_params['alt_text'] = ''; |
||
| 1330 | } |
||
| 1331 | $image_params['width'] = $thumbnail_size['width']; |
||
| 1332 | $image_params['height'] = $thumbnail_size['height']; |
||
| 1333 | $image_params['src'] = Jetpack_PostImages::fit_image_url( |
||
| 1334 | $img_url, |
||
| 1335 | $thumbnail_size['width'], |
||
| 1336 | $thumbnail_size['height'] |
||
| 1337 | ); |
||
| 1338 | } |
||
| 1339 | } |
||
| 1340 | |||
| 1341 | return $image_params; |
||
| 1342 | } |
||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * Returns the string UTF-8 encoded |
||
| 1346 | * |
||
| 1347 | * @param string $text |
||
| 1348 | * @return string |
||
| 1349 | */ |
||
| 1350 | protected function _to_utf8( $text ) { |
||
| 1351 | if ( $this->_convert_charset ) { |
||
| 1352 | return iconv( $this->_blog_charset, 'UTF-8', $text ); |
||
| 1353 | } else { |
||
| 1354 | return $text; |
||
| 1355 | } |
||
| 1356 | } |
||
| 1357 | |||
| 1358 | /** |
||
| 1359 | * ============================================= |
||
| 1360 | * PROTECTED UTILITY FUNCTIONS EXTENDED BY WPCOM |
||
| 1361 | * ============================================= |
||
| 1362 | */ |
||
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Workhorse method to return array of related posts matched by Elasticsearch. |
||
| 1366 | * |
||
| 1367 | * @param int $post_id |
||
| 1368 | * @param int $size |
||
| 1369 | * @param array $filters |
||
| 1370 | * @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 |
||
| 1371 | * @return array |
||
| 1372 | */ |
||
| 1373 | protected function _get_related_posts( $post_id, $size, array $filters ) { |
||
| 1374 | $hits = $this->_filter_non_public_posts( |
||
| 1375 | $this->_get_related_post_ids( |
||
| 1376 | $post_id, |
||
| 1377 | $size, |
||
| 1378 | $filters |
||
| 1379 | ) |
||
| 1380 | ); |
||
| 1381 | |||
| 1382 | /** |
||
| 1383 | * Filter the Related Posts matched by Elasticsearch. |
||
| 1384 | * |
||
| 1385 | * @module related-posts |
||
| 1386 | * |
||
| 1387 | * @since 2.9.0 |
||
| 1388 | * |
||
| 1389 | * @param array $hits Array of Post IDs matched by Elasticsearch. |
||
| 1390 | * @param string $post_id Post ID of the post for which we are retrieving Related Posts. |
||
| 1391 | */ |
||
| 1392 | $hits = apply_filters( 'jetpack_relatedposts_filter_hits', $hits, $post_id ); |
||
| 1393 | |||
| 1394 | $related_posts = array(); |
||
| 1395 | foreach ( $hits as $i => $hit ) { |
||
| 1396 | $related_posts[] = $this->get_related_post_data_for_post( $hit['id'], $i, $post_id ); |
||
| 1397 | } |
||
| 1398 | return $related_posts; |
||
| 1399 | } |
||
| 1400 | |||
| 1401 | /** |
||
| 1402 | * Get array of related posts matched by Elasticsearch. |
||
| 1403 | * |
||
| 1404 | * @param int $post_id |
||
| 1405 | * @param int $size |
||
| 1406 | * @param array $filters |
||
| 1407 | * @uses wp_remote_post, is_wp_error, wp_remote_retrieve_body, get_post_meta, update_post_meta |
||
| 1408 | * @return array |
||
| 1409 | */ |
||
| 1410 | protected function _get_related_post_ids( $post_id, $size, array $filters ) { |
||
| 1411 | $now_ts = time(); |
||
| 1412 | $cache_meta_key = '_jetpack_related_posts_cache'; |
||
| 1413 | |||
| 1414 | $body = array( |
||
| 1415 | 'size' => (int) $size, |
||
| 1416 | ); |
||
| 1417 | |||
| 1418 | if ( !empty( $filters ) ) |
||
| 1419 | $body['filter'] = array( 'and' => $filters ); |
||
| 1420 | |||
| 1421 | // Build cache key |
||
| 1422 | $cache_key = md5( serialize( $body ) ); |
||
| 1423 | |||
| 1424 | // Load all cached values |
||
| 1425 | if ( wp_using_ext_object_cache() ) { |
||
| 1426 | $transient_name = "{$cache_meta_key}_{$cache_key}_{$post_id}"; |
||
| 1427 | $cache = get_transient( $transient_name ); |
||
| 1428 | if ( false !== $cache ) { |
||
| 1429 | return $cache; |
||
| 1430 | } |
||
| 1431 | } else { |
||
| 1432 | $cache = get_post_meta( $post_id, $cache_meta_key, true ); |
||
| 1433 | |||
| 1434 | if ( empty( $cache ) ) |
||
| 1435 | $cache = array(); |
||
| 1436 | |||
| 1437 | |||
| 1438 | // Cache is valid! Return cached value. |
||
| 1439 | if ( isset( $cache[ $cache_key ] ) && is_array( $cache[ $cache_key ] ) && $cache[ $cache_key ][ 'expires' ] > $now_ts ) { |
||
| 1440 | return $cache[ $cache_key ][ 'payload' ]; |
||
| 1441 | } |
||
| 1442 | } |
||
| 1443 | |||
| 1444 | $response = wp_remote_post( |
||
| 1445 | "https://public-api.wordpress.com/rest/v1/sites/{$this->get_blog_id()}/posts/$post_id/related/", |
||
| 1446 | array( |
||
| 1447 | 'timeout' => 10, |
||
| 1448 | 'user-agent' => 'jetpack_related_posts', |
||
| 1449 | 'sslverify' => true, |
||
| 1450 | 'body' => $body, |
||
| 1451 | ) |
||
| 1452 | ); |
||
| 1453 | |||
| 1454 | // Oh no... return nothing don't cache errors. |
||
| 1455 | if ( is_wp_error( $response ) ) { |
||
| 1456 | if ( isset( $cache[ $cache_key ] ) && is_array( $cache[ $cache_key ] ) ) |
||
| 1457 | return $cache[ $cache_key ][ 'payload' ]; // return stale |
||
| 1458 | else |
||
| 1459 | return array(); |
||
| 1460 | } |
||
| 1461 | |||
| 1462 | $results = json_decode( wp_remote_retrieve_body( $response ), true ); |
||
| 1463 | $related_posts = array(); |
||
| 1464 | if ( is_array( $results ) && !empty( $results['hits'] ) ) { |
||
| 1465 | foreach( $results['hits'] as $hit ) { |
||
| 1466 | $related_posts[] = array( |
||
| 1467 | 'id' => $hit['fields']['post_id'], |
||
| 1468 | ); |
||
| 1469 | } |
||
| 1470 | } |
||
| 1471 | |||
| 1472 | // An empty array might indicate no related posts or that posts |
||
| 1473 | // are not yet synced to WordPress.com, so we cache for only 1 |
||
| 1474 | // minute in this case |
||
| 1475 | if ( empty( $related_posts ) ) { |
||
| 1476 | $cache_ttl = 60; |
||
| 1477 | } else { |
||
| 1478 | $cache_ttl = 12 * HOUR_IN_SECONDS; |
||
| 1479 | } |
||
| 1480 | |||
| 1481 | // Update cache |
||
| 1482 | if ( wp_using_ext_object_cache() ) { |
||
| 1483 | set_transient( $transient_name, $related_posts, $cache_ttl ); |
||
| 1484 | } else { |
||
| 1485 | // Copy all valid cache values |
||
| 1486 | $new_cache = array(); |
||
| 1487 | foreach ( $cache as $k => $v ) { |
||
| 1488 | if ( is_array( $v ) && $v[ 'expires' ] > $now_ts ) { |
||
| 1489 | $new_cache[ $k ] = $v; |
||
| 1490 | } |
||
| 1491 | } |
||
| 1492 | |||
| 1493 | // Set new cache value |
||
| 1494 | $cache_expires = $cache_ttl + $now_ts; |
||
| 1495 | $new_cache[ $cache_key ] = array( |
||
| 1496 | 'expires' => $cache_expires, |
||
| 1497 | 'payload' => $related_posts, |
||
| 1498 | ); |
||
| 1499 | update_post_meta( $post_id, $cache_meta_key, $new_cache ); |
||
| 1500 | } |
||
| 1501 | |||
| 1502 | return $related_posts; |
||
| 1503 | } |
||
| 1504 | |||
| 1505 | /** |
||
| 1506 | * Filter out any hits that are not public anymore. |
||
| 1507 | * |
||
| 1508 | * @param array $related_posts |
||
| 1509 | * @uses get_post_stati, get_post_status |
||
| 1510 | * @return array |
||
| 1511 | */ |
||
| 1512 | protected function _filter_non_public_posts( array $related_posts ) { |
||
| 1513 | $public_stati = get_post_stati( array( 'public' => true ) ); |
||
| 1514 | |||
| 1515 | $filtered = array(); |
||
| 1516 | foreach ( $related_posts as $hit ) { |
||
| 1517 | if ( in_array( get_post_status( $hit['id'] ), $public_stati ) ) { |
||
| 1518 | $filtered[] = $hit; |
||
| 1519 | } |
||
| 1520 | } |
||
| 1521 | return $filtered; |
||
| 1522 | } |
||
| 1523 | |||
| 1524 | /** |
||
| 1525 | * Generates a context for the related content (second line in related post output). |
||
| 1526 | * Order of importance: |
||
| 1527 | * - First category (Not 'Uncategorized') |
||
| 1528 | * - First post tag |
||
| 1529 | * - Number of comments |
||
| 1530 | * |
||
| 1531 | * @param int $post_id |
||
| 1532 | * @uses get_the_category, get_the_terms, get_comments_number, number_format_i18n, __, _n |
||
| 1533 | * @return string |
||
| 1534 | */ |
||
| 1535 | protected function _generate_related_post_context( $post_id ) { |
||
| 1536 | $categories = get_the_category( $post_id ); |
||
| 1537 | View Code Duplication | if ( is_array( $categories ) ) { |
|
| 1538 | foreach ( $categories as $category ) { |
||
| 1539 | if ( 'uncategorized' != $category->slug && '' != trim( $category->name ) ) { |
||
| 1540 | $post_cat_context = sprintf( |
||
| 1541 | esc_html_x( 'In "%s"', 'in {category/tag name}', 'jetpack' ), |
||
| 1542 | $category->name |
||
| 1543 | ); |
||
| 1544 | /** |
||
| 1545 | * Filter the "In Category" line displayed in the post context below each Related Post. |
||
| 1546 | * |
||
| 1547 | * @module related-posts |
||
| 1548 | * |
||
| 1549 | * @since 3.2.0 |
||
| 1550 | * |
||
| 1551 | * @param string $post_cat_context "In Category" line displayed in the post context below each Related Post. |
||
| 1552 | * @param array $category Array containing information about the category. |
||
| 1553 | */ |
||
| 1554 | return apply_filters( 'jetpack_relatedposts_post_category_context', $post_cat_context, $category ); |
||
| 1555 | } |
||
| 1556 | } |
||
| 1557 | } |
||
| 1558 | |||
| 1559 | $tags = get_the_terms( $post_id, 'post_tag' ); |
||
| 1560 | View Code Duplication | if ( is_array( $tags ) ) { |
|
| 1561 | foreach ( $tags as $tag ) { |
||
| 1562 | if ( '' != trim( $tag->name ) ) { |
||
| 1563 | $post_tag_context = sprintf( |
||
| 1564 | _x( 'In "%s"', 'in {category/tag name}', 'jetpack' ), |
||
| 1565 | $tag->name |
||
| 1566 | ); |
||
| 1567 | /** |
||
| 1568 | * Filter the "In Tag" line displayed in the post context below each Related Post. |
||
| 1569 | * |
||
| 1570 | * @module related-posts |
||
| 1571 | * |
||
| 1572 | * @since 3.2.0 |
||
| 1573 | * |
||
| 1574 | * @param string $post_tag_context "In Tag" line displayed in the post context below each Related Post. |
||
| 1575 | * @param array $tag Array containing information about the tag. |
||
| 1576 | */ |
||
| 1577 | return apply_filters( 'jetpack_relatedposts_post_tag_context', $post_tag_context, $tag ); |
||
| 1578 | } |
||
| 1579 | } |
||
| 1580 | } |
||
| 1581 | |||
| 1582 | $comment_count = get_comments_number( $post_id ); |
||
| 1583 | if ( $comment_count > 0 ) { |
||
| 1584 | return sprintf( |
||
| 1585 | _n( 'With 1 comment', 'With %s comments', $comment_count, 'jetpack' ), |
||
| 1586 | number_format_i18n( $comment_count ) |
||
| 1587 | ); |
||
| 1588 | } |
||
| 1589 | |||
| 1590 | return __( 'Similar post', 'jetpack' ); |
||
| 1591 | } |
||
| 1592 | |||
| 1593 | /** |
||
| 1594 | * Logs clicks for clickthrough analysis and related result tuning. |
||
| 1595 | * |
||
| 1596 | * @return null |
||
| 1597 | */ |
||
| 1598 | protected function _log_click( $post_id, $to_post_id, $link_position ) { |
||
| 1599 | |||
| 1600 | } |
||
| 1601 | |||
| 1602 | /** |
||
| 1603 | * Determines if the current post is able to use related posts. |
||
| 1604 | * |
||
| 1605 | * @uses self::get_options, is_admin, is_single, apply_filters |
||
| 1606 | * @return bool |
||
| 1607 | */ |
||
| 1608 | protected function _enabled_for_request() { |
||
| 1609 | $enabled = is_single() |
||
| 1610 | && ! is_attachment() |
||
| 1611 | && ! is_admin() |
||
| 1612 | && ( ! $this->_allow_feature_toggle() || $this->get_option( 'enabled' ) ); |
||
| 1613 | |||
| 1614 | if ( |
||
| 1615 | class_exists( 'Jetpack_AMP_Support' ) |
||
| 1616 | && Jetpack_AMP_Support::is_amp_request() |
||
| 1617 | ) { |
||
| 1618 | $enabled = false; |
||
| 1619 | } |
||
| 1620 | |||
| 1621 | /** |
||
| 1622 | * Filter the Enabled value to allow related posts to be shown on pages as well. |
||
| 1623 | * |
||
| 1624 | * @module related-posts |
||
| 1625 | * |
||
| 1626 | * @since 3.3.0 |
||
| 1627 | * |
||
| 1628 | * @param bool $enabled Should Related Posts be enabled on the current page. |
||
| 1629 | */ |
||
| 1630 | return apply_filters( 'jetpack_relatedposts_filter_enabled_for_request', $enabled ); |
||
| 1631 | } |
||
| 1632 | |||
| 1633 | /** |
||
| 1634 | * Adds filters and enqueues assets. |
||
| 1635 | * |
||
| 1636 | * @uses self::_enqueue_assets, self::_setup_shortcode, add_filter |
||
| 1637 | * @return null |
||
| 1638 | */ |
||
| 1639 | protected function _action_frontend_init_page() { |
||
| 1645 | |||
| 1646 | /** |
||
| 1647 | * Enqueues assets needed to do async loading of related posts. |
||
| 1648 | * |
||
| 1649 | * @uses wp_enqueue_script, wp_enqueue_style, plugins_url |
||
| 1650 | * @return null |
||
| 1651 | */ |
||
| 1652 | protected function _enqueue_assets( $script, $style ) { |
||
| 1681 | |||
| 1682 | /** |
||
| 1683 | * Sets up the shortcode processing. |
||
| 1684 | * |
||
| 1685 | * @uses add_filter, add_shortcode |
||
| 1686 | * @return null |
||
| 1687 | */ |
||
| 1688 | protected function _setup_shortcode() { |
||
| 1693 | |||
| 1694 | protected function _allow_feature_toggle() { |
||
| 1709 | |||
| 1710 | /** |
||
| 1711 | * =================================================== |
||
| 1712 | * FUNCTIONS EXPOSING RELATED POSTS IN THE WP REST API |
||
| 1713 | * =================================================== |
||
| 1714 | */ |
||
| 1715 | |||
| 1716 | /** |
||
| 1717 | * Add Related Posts to the REST API Post response. |
||
| 1718 | * |
||
| 1719 | * @since 4.4.0 |
||
| 1720 | * |
||
| 1721 | * @action rest_api_init |
||
| 1722 | * @uses register_rest_field, self::rest_get_related_posts |
||
| 1723 | * @return null |
||
| 1724 | */ |
||
| 1725 | public function rest_register_related_posts() { |
||
| 1735 | |||
| 1736 | /** |
||
| 1737 | * Build an array of Related Posts. |
||
| 1738 | * By default returns cached results that are stored for up to 12 hours. |
||
| 1739 | * |
||
| 1740 | * @since 4.4.0 |
||
| 1741 | * |
||
| 1742 | * @param array $object Details of current post. |
||
| 1743 | * @param string $field_name Name of field. |
||
| 1744 | * @param WP_REST_Request $request Current request |
||
| 1745 | * |
||
| 1746 | * @uses self::get_for_post_id |
||
| 1747 | * |
||
| 1748 | * @return array |
||
| 1749 | */ |
||
| 1750 | public function rest_get_related_posts( $object, $field_name, $request ) { |
||
| 1753 | } |
||
| 1754 | |||
| 1755 | class Jetpack_RelatedPosts_Raw extends Jetpack_RelatedPosts { |
||
| 1756 | protected $_query_name; |
||
| 1757 | |||
| 1758 | /** |
||
| 1759 | * Allows callers of this class to tag each query with a unique name for tracking purposes. |
||
| 1760 | * |
||
| 1761 | * @param string $name |
||
| 1762 | * @return Jetpack_RelatedPosts_Raw |
||
| 1763 | */ |
||
| 1764 | public function set_query_name( $name ) { |
||
| 1765 | $this->_query_name = (string) $name; |
||
| 1766 | return $this; |
||
| 1767 | } |
||
| 1768 | |||
| 1769 | /** |
||
| 1770 | * The raw related posts class can be used by other plugins or themes |
||
| 1771 | * to get related content. This class wraps the existing RelatedPosts |
||
| 1772 | * logic thus we never want to add anything to the DOM or do anything |
||
| 1773 | * for event hooks. We will also not present any settings for this |
||
| 1774 | * class and keep it enabled as calls to this class is done |
||
| 1775 | * programmatically. |
||
| 1776 | */ |
||
| 1777 | public function action_admin_init() {} |
||
| 1809 |
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.