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 The_Neverending_Home_Page 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 The_Neverending_Home_Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class The_Neverending_Home_Page { |
||
| 19 | /** |
||
| 20 | * Register actions and filters, plus parse IS settings |
||
| 21 | * |
||
| 22 | * @uses add_action, add_filter, self::get_settings |
||
| 23 | * @return null |
||
|
|
|||
| 24 | */ |
||
| 25 | function __construct() { |
||
| 26 | add_action( 'pre_get_posts', array( $this, 'posts_per_page_query' ) ); |
||
| 27 | |||
| 28 | add_action( 'admin_init', array( $this, 'settings_api_init' ) ); |
||
| 29 | add_action( 'template_redirect', array( $this, 'action_template_redirect' ) ); |
||
| 30 | add_action( 'template_redirect', array( $this, 'ajax_response' ) ); |
||
| 31 | add_action( 'custom_ajax_infinite_scroll', array( $this, 'query' ) ); |
||
| 32 | add_filter( 'infinite_scroll_query_args', array( $this, 'inject_query_args' ) ); |
||
| 33 | add_filter( 'infinite_scroll_allowed_vars', array( $this, 'allowed_query_vars' ) ); |
||
| 34 | add_action( 'the_post', array( $this, 'preserve_more_tag' ) ); |
||
| 35 | add_action( 'wp_footer', array( $this, 'footer' ) ); |
||
| 36 | |||
| 37 | // Plugin compatibility |
||
| 38 | add_filter( 'grunion_contact_form_redirect_url', array( $this, 'filter_grunion_redirect_url' ) ); |
||
| 39 | |||
| 40 | // Parse IS settings from theme |
||
| 41 | self::get_settings(); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Initialize our static variables |
||
| 46 | */ |
||
| 47 | static $the_time = null; |
||
| 48 | static $settings = null; // Don't access directly, instead use self::get_settings(). |
||
| 49 | |||
| 50 | static $option_name_enabled = 'infinite_scroll'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Parse IS settings provided by theme |
||
| 54 | * |
||
| 55 | * @uses get_theme_support, infinite_scroll_has_footer_widgets, sanitize_title, add_action, get_option, wp_parse_args, is_active_sidebar |
||
| 56 | * @return object |
||
| 57 | */ |
||
| 58 | static function get_settings() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Retrieve the query used with Infinite Scroll |
||
| 255 | * |
||
| 256 | * @global $wp_the_query |
||
| 257 | * @uses apply_filters |
||
| 258 | * @return object |
||
| 259 | */ |
||
| 260 | static function wp_query() { |
||
| 261 | global $wp_the_query; |
||
| 262 | /** |
||
| 263 | * Filter the Infinite Scroll query object. |
||
| 264 | * |
||
| 265 | * @module infinite-scroll |
||
| 266 | * |
||
| 267 | * @since 2.2.1 |
||
| 268 | * |
||
| 269 | * @param WP_Query $wp_the_query WP Query. |
||
| 270 | */ |
||
| 271 | return apply_filters( 'infinite_scroll_query_object', $wp_the_query ); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Has infinite scroll been triggered? |
||
| 276 | */ |
||
| 277 | static function got_infinity() { |
||
| 278 | /** |
||
| 279 | * Filter the parameter used to check if Infinite Scroll has been triggered. |
||
| 280 | * |
||
| 281 | * @module infinite-scroll |
||
| 282 | * |
||
| 283 | * @since 3.9.0 |
||
| 284 | * |
||
| 285 | * @param bool isset( $_GET[ 'infinity' ] ) Return true if the "infinity" parameter is set. |
||
| 286 | */ |
||
| 287 | return apply_filters( 'infinite_scroll_got_infinity', isset( $_GET[ 'infinity' ] ) ); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Is this guaranteed to be the last batch of posts? |
||
| 292 | */ |
||
| 293 | static function is_last_batch() { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * The more tag will be ignored by default if the blog page isn't our homepage. |
||
| 299 | * Let's force the $more global to false. |
||
| 300 | */ |
||
| 301 | function preserve_more_tag( $array ) { |
||
| 302 | global $more; |
||
| 303 | |||
| 304 | if ( self::got_infinity() ) |
||
| 305 | $more = 0; //0 = show content up to the more tag. Add more link. |
||
| 306 | |||
| 307 | return $array; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Add a checkbox field to Settings > Reading |
||
| 312 | * for enabling infinite scroll. |
||
| 313 | * |
||
| 314 | * Only show if the current theme supports infinity. |
||
| 315 | * |
||
| 316 | * @uses current_theme_supports, add_settings_field, __, register_setting |
||
| 317 | * @action admin_init |
||
| 318 | * @return null |
||
| 319 | */ |
||
| 320 | function settings_api_init() { |
||
| 321 | if ( ! current_theme_supports( 'infinite-scroll' ) ) |
||
| 322 | return; |
||
| 323 | |||
| 324 | // Add the setting field [infinite_scroll] and place it in Settings > Reading |
||
| 325 | add_settings_field( self::$option_name_enabled, '<span id="infinite-scroll-options">' . __( 'To infinity and beyond', 'jetpack' ) . '</span>', array( $this, 'infinite_setting_html' ), 'reading' ); |
||
| 326 | register_setting( 'reading', self::$option_name_enabled, 'esc_attr' ); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * HTML code to display a checkbox true/false option |
||
| 331 | * for the infinite_scroll setting. |
||
| 332 | */ |
||
| 333 | function infinite_setting_html() { |
||
| 334 | $notice = '<em>' . __( 'We’ve changed this option to a click-to-scroll version for you since you have footer widgets in Appearance → Widgets, or your theme uses click-to-scroll as the default behavior.', 'jetpack' ) . '</em>'; |
||
| 335 | |||
| 336 | // If the blog has footer widgets, show a notice instead of the checkbox |
||
| 337 | if ( self::get_settings()->footer_widgets || 'click' == self::get_settings()->requested_type ) { |
||
| 338 | echo '<label>' . $notice . '</label>'; |
||
| 339 | } else { |
||
| 340 | echo '<label><input name="infinite_scroll" type="checkbox" value="1" ' . checked( 1, '' !== get_option( self::$option_name_enabled ), false ) . ' /> ' . __( 'Scroll Infinitely', 'jetpack' ) . '</br><small>' . sprintf( __( '(Shows %s posts on each load)', 'jetpack' ), number_format_i18n( self::get_settings()->posts_per_page ) ) . '</small>' . '</label>'; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Does the legwork to determine whether the feature is enabled. |
||
| 346 | * |
||
| 347 | * @uses current_theme_supports, self::archive_supports_infinity, self::get_settings, add_filter, wp_enqueue_script, plugins_url, wp_enqueue_style, add_action |
||
| 348 | * @action template_redirect |
||
| 349 | * @return null |
||
| 350 | */ |
||
| 351 | function action_template_redirect() { |
||
| 352 | // Check that we support infinite scroll, and are on the home page. |
||
| 353 | if ( ! current_theme_supports( 'infinite-scroll' ) || ! self::archive_supports_infinity() ) |
||
| 354 | return; |
||
| 355 | |||
| 356 | $id = self::get_settings()->container; |
||
| 357 | |||
| 358 | // Check that we have an id. |
||
| 359 | if ( empty( $id ) ) |
||
| 360 | return; |
||
| 361 | |||
| 362 | // Make sure there are enough posts for IS |
||
| 363 | if ( 'click' == self::get_settings()->type && self::is_last_batch() ) |
||
| 364 | return; |
||
| 365 | |||
| 366 | // Add a class to the body. |
||
| 367 | add_filter( 'body_class', array( $this, 'body_class' ) ); |
||
| 368 | |||
| 369 | // Add our scripts. |
||
| 370 | wp_enqueue_script( 'the-neverending-homepage', plugins_url( 'infinity.js', __FILE__ ), array( 'jquery' ), 20141016, true ); |
||
| 371 | |||
| 372 | // Add our default styles. |
||
| 373 | wp_enqueue_style( 'the-neverending-homepage', plugins_url( 'infinity.css', __FILE__ ), array(), '20140422' ); |
||
| 374 | |||
| 375 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_spinner_scripts' ) ); |
||
| 376 | |||
| 377 | add_action( 'wp_footer', array( $this, 'action_wp_footer_settings' ), 2 ); |
||
| 378 | |||
| 379 | add_action( 'wp_footer', array( $this, 'action_wp_footer' ), 21 ); // Core prints footer scripts at priority 20, so we just need to be one later than that |
||
| 380 | |||
| 381 | add_filter( 'infinite_scroll_results', array( $this, 'filter_infinite_scroll_results' ), 10, 3 ); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Enqueue spinner scripts. |
||
| 386 | */ |
||
| 387 | function enqueue_spinner_scripts() { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Adds an 'infinite-scroll' class to the body. |
||
| 393 | */ |
||
| 394 | function body_class( $classes ) { |
||
| 395 | // Do not add infinity-scroll class if disabled through the Reading page |
||
| 396 | $disabled = '' === get_option( self::$option_name_enabled ) ? true : false; |
||
| 397 | if ( ! $disabled || 'click' == self::get_settings()->type ) { |
||
| 398 | $classes[] = 'infinite-scroll'; |
||
| 399 | |||
| 400 | if ( 'scroll' == self::get_settings()->type ) |
||
| 401 | $classes[] = 'neverending'; |
||
| 402 | } |
||
| 403 | |||
| 404 | return $classes; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * In case IS is activated on search page, we have to exclude initially loaded posts which match the keyword by title, not the content as they are displayed before content-matching ones |
||
| 409 | * |
||
| 410 | * @uses self::wp_query |
||
| 411 | * @uses self::get_last_post_date |
||
| 412 | * @uses self::has_only_title_matching_posts |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | function get_excluded_posts() { |
||
| 416 | |||
| 417 | $excluded_posts = array(); |
||
| 418 | //loop through posts returned by wp_query call |
||
| 419 | foreach( self::wp_query()->get_posts() as $post ) { |
||
| 420 | |||
| 421 | $orderby = isset( self::wp_query()->query_vars['orderby'] ) ? self::wp_query()->query_vars['orderby'] : ''; |
||
| 422 | $post_date = ( ! empty( $post->post_date ) ? $post->post_date : false ); |
||
| 423 | if ( 'modified' === $orderby || false === $post_date ) { |
||
| 424 | $post_date = $post->post_modified; |
||
| 425 | } |
||
| 426 | |||
| 427 | //in case all posts initially displayed match the keyword by title we add em all to excluded posts array |
||
| 428 | //else, we add only posts which are older than last_post_date param as newer are natually excluded by last_post_date condition in the SQL query |
||
| 429 | if ( self::has_only_title_matching_posts() || $post_date <= self::get_last_post_date() ) { |
||
| 430 | array_push( $excluded_posts, $post->ID ); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | return $excluded_posts; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * In case IS is active on search, we have to exclude posts matched by title rather than by post_content in order to prevent dupes on next pages |
||
| 438 | * |
||
| 439 | * @uses self::wp_query |
||
| 440 | * @uses self::get_excluded_posts |
||
| 441 | * @return array |
||
| 442 | */ |
||
| 443 | function get_query_vars() { |
||
| 444 | |||
| 445 | $query_vars = self::wp_query()->query_vars; |
||
| 446 | //applies to search page only |
||
| 447 | if ( true === self::wp_query()->is_search() ) { |
||
| 448 | //set post__not_in array in query_vars in case it does not exists |
||
| 449 | if ( false === isset( $query_vars['post__not_in'] ) ) { |
||
| 450 | $query_vars['post__not_in'] = array(); |
||
| 451 | } |
||
| 452 | //get excluded posts |
||
| 453 | $excluded = self::get_excluded_posts(); |
||
| 454 | //merge them with other post__not_in posts (eg.: sticky posts) |
||
| 455 | $query_vars['post__not_in'] = array_merge( $query_vars['post__not_in'], $excluded ); |
||
| 456 | } |
||
| 457 | return $query_vars; |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * This function checks whether all posts returned by initial wp_query match the keyword by title |
||
| 462 | * The code used in this function is borrowed from WP_Query class where it is used to construct like conditions for keywords |
||
| 463 | * |
||
| 464 | * @uses self::wp_query |
||
| 465 | * @return bool |
||
| 466 | */ |
||
| 467 | function has_only_title_matching_posts() { |
||
| 468 | |||
| 469 | //apply following logic for search page results only |
||
| 470 | if ( false === self::wp_query()->is_search() ) { |
||
| 471 | return false; |
||
| 472 | } |
||
| 473 | |||
| 474 | //grab the last posts in the stack as if the last one is title-matching the rest is title-matching as well |
||
| 475 | $post = end( self::wp_query()->posts ); |
||
| 476 | |||
| 477 | //code inspired by WP_Query class |
||
| 478 | if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', self::wp_query()->get( 's' ), $matches ) ) { |
||
| 479 | $search_terms = self::wp_query()->query_vars['search_terms']; |
||
| 480 | // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence |
||
| 481 | if ( empty( $search_terms ) || count( $search_terms ) > 9 ) { |
||
| 482 | $search_terms = array( self::wp_query()->get( 's' ) ); |
||
| 483 | } |
||
| 484 | } else { |
||
| 485 | $search_terms = array( self::wp_query()->get( 's' ) ); |
||
| 486 | } |
||
| 487 | |||
| 488 | //actual testing. As search query combines multiple keywords with AND, it's enough to check if any of the keywords is present in the title |
||
| 489 | $term = current( $search_terms ); |
||
| 490 | if ( ! empty( $term ) && false !== strpos( $post->post_title, $term ) ) { |
||
| 491 | return true; |
||
| 492 | } |
||
| 493 | |||
| 494 | return false; |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Grab the timestamp for the initial query's last post. |
||
| 499 | * |
||
| 500 | * This takes into account the query's 'orderby' parameter and returns |
||
| 501 | * false if the posts are not ordered by date. |
||
| 502 | * |
||
| 503 | * @uses self::got_infinity |
||
| 504 | * @uses self::has_only_title_matching_posts |
||
| 505 | * @uses self::wp_query |
||
| 506 | * @return string 'Y-m-d H:i:s' or false |
||
| 507 | */ |
||
| 508 | function get_last_post_date() { |
||
| 509 | if ( self::got_infinity() ) |
||
| 510 | return; |
||
| 511 | |||
| 512 | if ( ! self::wp_query()->have_posts() ) { |
||
| 513 | return null; |
||
| 514 | } |
||
| 515 | |||
| 516 | //In case there are only title-matching posts in the initial WP_Query result, we don't want to use the last_post_date param yet |
||
| 517 | if ( true === self::has_only_title_matching_posts() ) { |
||
| 518 | return false; |
||
| 519 | } |
||
| 520 | |||
| 521 | $post = end( self::wp_query()->posts ); |
||
| 522 | $orderby = isset( self::wp_query()->query_vars['orderby'] ) ? |
||
| 523 | self::wp_query()->query_vars['orderby'] : ''; |
||
| 524 | $post_date = ( ! empty( $post->post_date ) ? $post->post_date : false ); |
||
| 525 | View Code Duplication | switch ( $orderby ) { |
|
| 526 | case 'modified': |
||
| 527 | return $post->post_modified; |
||
| 528 | case 'date': |
||
| 529 | case '': |
||
| 530 | return $post_date; |
||
| 531 | default: |
||
| 532 | return false; |
||
| 533 | } |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Returns the appropriate `wp_posts` table field for a given query's |
||
| 538 | * 'orderby' parameter, if applicable. |
||
| 539 | * |
||
| 540 | * @param optional object $query |
||
| 541 | * @uses self::wp_query |
||
| 542 | * @return string or false |
||
| 543 | */ |
||
| 544 | function get_query_sort_field( $query = null ) { |
||
| 545 | if ( empty( $query ) ) |
||
| 546 | $query = self::wp_query(); |
||
| 547 | |||
| 548 | $orderby = isset( $query->query_vars['orderby'] ) ? $query->query_vars['orderby'] : ''; |
||
| 549 | |||
| 550 | View Code Duplication | switch ( $orderby ) { |
|
| 551 | case 'modified': |
||
| 552 | return 'post_modified'; |
||
| 553 | case 'date': |
||
| 554 | case '': |
||
| 555 | return 'post_date'; |
||
| 556 | default: |
||
| 557 | return false; |
||
| 558 | } |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Create a where clause that will make sure post queries |
||
| 563 | * will always return results prior to (descending sort) |
||
| 564 | * or before (ascending sort) the last post date. |
||
| 565 | * |
||
| 566 | * @global $wpdb |
||
| 567 | * @param string $where |
||
| 568 | * @param object $query |
||
| 569 | * @uses apply_filters |
||
| 570 | * @filter posts_where |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | function query_time_filter( $where, $query ) { |
||
| 574 | if ( self::got_infinity() ) { |
||
| 575 | global $wpdb; |
||
| 576 | |||
| 577 | $sort_field = self::get_query_sort_field( $query ); |
||
| 578 | if ( false == $sort_field ) |
||
| 579 | return $where; |
||
| 580 | |||
| 581 | $last_post_date = $_REQUEST['last_post_date']; |
||
| 582 | // Sanitize timestamp |
||
| 583 | if ( empty( $last_post_date ) || !preg_match( '|\d{4}\-\d{2}\-\d{2}|', $last_post_date ) ) |
||
| 584 | return $where; |
||
| 585 | |||
| 586 | $operator = 'ASC' == $_REQUEST['query_args']['order'] ? '>' : '<'; |
||
| 587 | |||
| 588 | // Construct the date query using our timestamp |
||
| 589 | $clause = $wpdb->prepare( " AND {$wpdb->posts}.{$sort_field} {$operator} %s", $last_post_date ); |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Filter Infinite Scroll's SQL date query making sure post queries |
||
| 593 | * will always return results prior to (descending sort) |
||
| 594 | * or before (ascending sort) the last post date. |
||
| 595 | * |
||
| 596 | * @module infinite-scroll |
||
| 597 | * |
||
| 598 | * @param string $clause SQL Date query. |
||
| 599 | * @param object $query Query. |
||
| 600 | * @param string $operator Query operator. |
||
| 601 | * @param string $last_post_date Last Post Date timestamp. |
||
| 602 | */ |
||
| 603 | $where .= apply_filters( 'infinite_scroll_posts_where', $clause, $query, $operator, $last_post_date ); |
||
| 604 | } |
||
| 605 | |||
| 606 | return $where; |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Let's overwrite the default post_per_page setting to always display a fixed amount. |
||
| 611 | * |
||
| 612 | * @param object $query |
||
| 613 | * @uses is_admin, self::archive_supports_infinity, self::get_settings |
||
| 614 | * @return null |
||
| 615 | */ |
||
| 616 | function posts_per_page_query( $query ) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Check if the IS output should be wrapped in a div. |
||
| 623 | * Setting value can be a boolean or a string specifying the class applied to the div. |
||
| 624 | * |
||
| 625 | * @uses self::get_settings |
||
| 626 | * @return bool |
||
| 627 | */ |
||
| 628 | function has_wrapper() { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Returns the Ajax url |
||
| 634 | * |
||
| 635 | * @global $wp |
||
| 636 | * @uses home_url, add_query_arg, apply_filters |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | function ajax_url() { |
||
| 640 | $base_url = set_url_scheme( home_url( '/' ) ); |
||
| 641 | |||
| 642 | $ajaxurl = add_query_arg( array( 'infinity' => 'scrolling' ), $base_url ); |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Filter the Infinite Scroll Ajax URL. |
||
| 646 | * |
||
| 647 | * @module infinite-scroll |
||
| 648 | * |
||
| 649 | * @since 2.0.0 |
||
| 650 | * |
||
| 651 | * @param string $ajaxurl Infinite Scroll Ajax URL. |
||
| 652 | */ |
||
| 653 | return apply_filters( 'infinite_scroll_ajax_url', $ajaxurl ); |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Our own Ajax response, avoiding calling admin-ajax |
||
| 658 | */ |
||
| 659 | function ajax_response() { |
||
| 660 | // Only proceed if the url query has a key of "Infinity" |
||
| 661 | if ( ! self::got_infinity() ) |
||
| 662 | return false; |
||
| 663 | |||
| 664 | // This should already be defined below, but make sure. |
||
| 665 | if ( ! defined( 'DOING_AJAX' ) ) { |
||
| 666 | define( 'DOING_AJAX', true ); |
||
| 667 | } |
||
| 668 | |||
| 669 | @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
||
| 670 | send_nosniff_header(); |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Fires at the end of the Infinite Scroll Ajax response. |
||
| 674 | * |
||
| 675 | * @module infinite-scroll |
||
| 676 | * |
||
| 677 | * @since 2.0.0 |
||
| 678 | */ |
||
| 679 | do_action( 'custom_ajax_infinite_scroll' ); |
||
| 680 | die( '0' ); |
||
| 681 | } |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Alias for renamed class method. |
||
| 685 | * |
||
| 686 | * Previously, JS settings object was unnecessarily output in the document head. |
||
| 687 | * When the hook was changed, the method name no longer made sense. |
||
| 688 | */ |
||
| 689 | function action_wp_head() { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Prints the relevant infinite scroll settings in JS. |
||
| 695 | * |
||
| 696 | * @global $wp_rewrite |
||
| 697 | * @uses self::get_settings, esc_js, esc_url_raw, self::has_wrapper, __, apply_filters, do_action, self::get_query_vars |
||
| 698 | * @action wp_footer |
||
| 699 | * @return string |
||
| 700 | */ |
||
| 701 | function action_wp_footer_settings() { |
||
| 702 | global $wp_rewrite; |
||
| 703 | global $currentday; |
||
| 704 | |||
| 705 | // Default click handle text |
||
| 706 | $click_handle_text = __( 'Older posts', 'jetpack' ); |
||
| 707 | |||
| 708 | // If a single CPT is displayed, use its plural name instead of "posts" |
||
| 709 | // Could be empty (posts) or an array of multiple post types. |
||
| 710 | // In the latter two cases cases, the default text is used, leaving the `infinite_scroll_js_settings` filter for further customization. |
||
| 711 | $post_type = self::wp_query()->get( 'post_type' ); |
||
| 712 | if ( is_string( $post_type ) && ! empty( $post_type ) ) { |
||
| 713 | $post_type = get_post_type_object( $post_type ); |
||
| 714 | |||
| 715 | if ( is_object( $post_type ) && ! is_wp_error( $post_type ) ) { |
||
| 716 | if ( isset( $post_type->labels->name ) ) { |
||
| 717 | $cpt_text = $post_type->labels->name; |
||
| 718 | } elseif ( isset( $post_type->label ) ) { |
||
| 719 | $cpt_text = $post_type->label; |
||
| 720 | } |
||
| 721 | |||
| 722 | if ( isset( $cpt_text ) ) { |
||
| 723 | $click_handle_text = sprintf( __( 'Older %s', 'jetpack' ), $cpt_text ); |
||
| 724 | unset( $cpt_text ); |
||
| 725 | } |
||
| 726 | } |
||
| 727 | } |
||
| 728 | unset( $post_type ); |
||
| 729 | |||
| 730 | // Base JS settings |
||
| 731 | $js_settings = array( |
||
| 732 | 'id' => self::get_settings()->container, |
||
| 733 | 'ajaxurl' => esc_url_raw( self::ajax_url() ), |
||
| 734 | 'type' => esc_js( self::get_settings()->type ), |
||
| 735 | 'wrapper' => self::has_wrapper(), |
||
| 736 | 'wrapper_class' => is_string( self::get_settings()->wrapper ) ? esc_js( self::get_settings()->wrapper ) : 'infinite-wrap', |
||
| 737 | 'footer' => is_string( self::get_settings()->footer ) ? esc_js( self::get_settings()->footer ) : self::get_settings()->footer, |
||
| 738 | 'click_handle' => esc_js( self::get_settings()->click_handle ), |
||
| 739 | 'text' => esc_js( $click_handle_text ), |
||
| 740 | 'totop' => esc_js( __( 'Scroll back to top', 'jetpack' ) ), |
||
| 741 | 'currentday' => $currentday, |
||
| 742 | 'order' => 'DESC', |
||
| 743 | 'scripts' => array(), |
||
| 744 | 'styles' => array(), |
||
| 745 | 'google_analytics' => false, |
||
| 746 | 'offset' => self::wp_query()->get( 'paged' ), |
||
| 747 | 'history' => array( |
||
| 748 | 'host' => preg_replace( '#^http(s)?://#i', '', untrailingslashit( get_option( 'home' ) ) ), |
||
| 749 | 'path' => self::get_request_path(), |
||
| 750 | 'use_trailing_slashes' => $wp_rewrite->use_trailing_slashes, |
||
| 751 | 'parameters' => self::get_request_parameters(), |
||
| 752 | ), |
||
| 753 | 'query_args' => self::get_query_vars(), |
||
| 754 | 'last_post_date' => self::get_last_post_date(), |
||
| 755 | ); |
||
| 756 | |||
| 757 | // Optional order param |
||
| 758 | if ( isset( $_REQUEST['order'] ) ) { |
||
| 759 | $order = strtoupper( $_REQUEST['order'] ); |
||
| 760 | |||
| 761 | if ( in_array( $order, array( 'ASC', 'DESC' ) ) ) |
||
| 762 | $js_settings['order'] = $order; |
||
| 763 | } |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Filter the Infinite Scroll JS settings outputted in the head. |
||
| 767 | * |
||
| 768 | * @module infinite-scroll |
||
| 769 | * |
||
| 770 | * @since 2.0.0 |
||
| 771 | * |
||
| 772 | * @param array $js_settings Infinite Scroll JS settings. |
||
| 773 | */ |
||
| 774 | $js_settings = apply_filters( 'infinite_scroll_js_settings', $js_settings ); |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Fires before Infinite Scroll outputs inline JavaScript in the head. |
||
| 778 | * |
||
| 779 | * @module infinite-scroll |
||
| 780 | * |
||
| 781 | * @since 2.0.0 |
||
| 782 | */ |
||
| 783 | do_action( 'infinite_scroll_wp_head' ); |
||
| 784 | |||
| 785 | ?> |
||
| 786 | <script type="text/javascript"> |
||
| 787 | //<![CDATA[ |
||
| 788 | var infiniteScroll = <?php echo json_encode( array( 'settings' => $js_settings ) ); ?>; |
||
| 789 | //]]> |
||
| 790 | </script> |
||
| 791 | <?php |
||
| 792 | } |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Build path data for current request. |
||
| 796 | * Used for Google Analytics and pushState history tracking. |
||
| 797 | * |
||
| 798 | * @global $wp_rewrite |
||
| 799 | * @global $wp |
||
| 800 | * @uses user_trailingslashit, sanitize_text_field, add_query_arg |
||
| 801 | * @return string|bool |
||
| 802 | */ |
||
| 803 | private function get_request_path() { |
||
| 804 | global $wp_rewrite; |
||
| 805 | |||
| 806 | if ( $wp_rewrite->using_permalinks() ) { |
||
| 807 | global $wp; |
||
| 808 | |||
| 809 | // If called too early, bail |
||
| 810 | if ( ! isset( $wp->request ) ) |
||
| 811 | return false; |
||
| 812 | |||
| 813 | // Determine path for paginated version of current request |
||
| 814 | if ( false != preg_match( '#' . $wp_rewrite->pagination_base . '/\d+/?$#i', $wp->request ) ) |
||
| 815 | $path = preg_replace( '#' . $wp_rewrite->pagination_base . '/\d+$#i', $wp_rewrite->pagination_base . '/%d', $wp->request ); |
||
| 816 | else |
||
| 817 | $path = $wp->request . '/' . $wp_rewrite->pagination_base . '/%d'; |
||
| 818 | |||
| 819 | // Slashes everywhere we need them |
||
| 820 | if ( 0 !== strpos( $path, '/' ) ) |
||
| 821 | $path = '/' . $path; |
||
| 822 | |||
| 823 | $path = user_trailingslashit( $path ); |
||
| 824 | } else { |
||
| 825 | // Clean up raw $_REQUEST input |
||
| 826 | $path = array_map( 'sanitize_text_field', $_REQUEST ); |
||
| 827 | $path = array_filter( $path ); |
||
| 828 | |||
| 829 | $path['paged'] = '%d'; |
||
| 830 | |||
| 831 | $path = add_query_arg( $path, '/' ); |
||
| 832 | } |
||
| 833 | |||
| 834 | return empty( $path ) ? false : $path; |
||
| 835 | } |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Return query string for current request, prefixed with '?'. |
||
| 839 | * |
||
| 840 | * @return string |
||
| 841 | */ |
||
| 842 | private function get_request_parameters() { |
||
| 843 | $uri = $_SERVER[ 'REQUEST_URI' ]; |
||
| 844 | $uri = preg_replace( '/^[^?]*(\?.*$)/', '$1', $uri, 1, $count ); |
||
| 845 | if ( $count != 1 ) |
||
| 846 | return ''; |
||
| 847 | return $uri; |
||
| 848 | } |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Provide IS with a list of the scripts and stylesheets already present on the page. |
||
| 852 | * Since posts may contain require additional assets that haven't been loaded, this data will be used to track the additional assets. |
||
| 853 | * |
||
| 854 | * @global $wp_scripts, $wp_styles |
||
| 855 | * @action wp_footer |
||
| 856 | * @return string |
||
| 857 | */ |
||
| 858 | function action_wp_footer() { |
||
| 859 | global $wp_scripts, $wp_styles; |
||
| 860 | |||
| 861 | $scripts = is_a( $wp_scripts, 'WP_Scripts' ) ? $wp_scripts->done : array(); |
||
| 862 | /** |
||
| 863 | * Filter the list of scripts already present on the page. |
||
| 864 | * |
||
| 865 | * @module infinite-scroll |
||
| 866 | * |
||
| 867 | * @since 2.1.2 |
||
| 868 | * |
||
| 869 | * @param array $scripts Array of scripts present on the page. |
||
| 870 | */ |
||
| 871 | $scripts = apply_filters( 'infinite_scroll_existing_scripts', $scripts ); |
||
| 872 | |||
| 873 | $styles = is_a( $wp_styles, 'WP_Styles' ) ? $wp_styles->done : array(); |
||
| 874 | /** |
||
| 875 | * Filter the list of styles already present on the page. |
||
| 876 | * |
||
| 877 | * @module infinite-scroll |
||
| 878 | * |
||
| 879 | * @since 2.1.2 |
||
| 880 | * |
||
| 881 | * @param array $styles Array of styles present on the page. |
||
| 882 | */ |
||
| 883 | $styles = apply_filters( 'infinite_scroll_existing_stylesheets', $styles ); |
||
| 884 | |||
| 885 | ?><script type="text/javascript"> |
||
| 886 | jQuery.extend( infiniteScroll.settings.scripts, <?php echo json_encode( $scripts ); ?> ); |
||
| 887 | jQuery.extend( infiniteScroll.settings.styles, <?php echo json_encode( $styles ); ?> ); |
||
| 888 | </script><?php |
||
| 889 | } |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Identify additional scripts required by the latest set of IS posts and provide the necessary data to the IS response handler. |
||
| 893 | * |
||
| 894 | * @global $wp_scripts |
||
| 895 | * @uses sanitize_text_field, add_query_arg |
||
| 896 | * @filter infinite_scroll_results |
||
| 897 | * @return array |
||
| 898 | */ |
||
| 899 | function filter_infinite_scroll_results( $results, $query_args, $wp_query ) { |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Runs the query and returns the results via JSON. |
||
| 1093 | * Triggered by an AJAX request. |
||
| 1094 | * |
||
| 1095 | * @global $wp_query |
||
| 1096 | * @global $wp_the_query |
||
| 1097 | * @uses current_theme_supports, get_option, self::wp_query, current_user_can, apply_filters, self::get_settings, add_filter, WP_Query, remove_filter, have_posts, wp_head, do_action, add_action, this::render, this::has_wrapper, esc_attr, wp_footer, sharing_register_post_for_share_counts, get_the_id |
||
| 1098 | * @return string or null |
||
| 1099 | */ |
||
| 1100 | function query() { |
||
| 1267 | |||
| 1268 | /** |
||
| 1269 | * Update the $allowed_vars array with the standard WP public and private |
||
| 1270 | * query vars, as well as taxonomy vars |
||
| 1271 | * |
||
| 1272 | * @global $wp |
||
| 1273 | * @param array $allowed_vars |
||
| 1274 | * @filter infinite_scroll_allowed_vars |
||
| 1275 | * @return array |
||
| 1276 | */ |
||
| 1277 | function allowed_query_vars( $allowed_vars ) { |
||
| 1278 | global $wp; |
||
| 1279 | |||
| 1280 | $allowed_vars += $wp->public_query_vars; |
||
| 1281 | $allowed_vars += $wp->private_query_vars; |
||
| 1282 | $allowed_vars += $this->get_taxonomy_vars(); |
||
| 1283 | |||
| 1284 | foreach ( array_keys( $allowed_vars, 'paged' ) as $key ) { |
||
| 1285 | unset( $allowed_vars[ $key ] ); |
||
| 1286 | } |
||
| 1290 | |||
| 1291 | /** |
||
| 1292 | * Returns an array of stock and custom taxonomy query vars |
||
| 1293 | * |
||
| 1294 | * @global $wp_taxonomies |
||
| 1295 | * @return array |
||
| 1296 | */ |
||
| 1297 | function get_taxonomy_vars() { |
||
| 1311 | |||
| 1312 | /** |
||
| 1313 | * Update the $query_args array with the parameters provided via AJAX/GET. |
||
| 1314 | * |
||
| 1315 | * @param array $query_args |
||
| 1316 | * @filter infinite_scroll_query_args |
||
| 1317 | * @return array |
||
| 1318 | */ |
||
| 1319 | function inject_query_args( $query_args ) { |
||
| 1345 | |||
| 1346 | /** |
||
| 1347 | * Rendering fallback used when themes don't specify their own handler. |
||
| 1348 | * |
||
| 1349 | * @uses have_posts, the_post, get_template_part, get_post_format |
||
| 1350 | * @action infinite_scroll_render |
||
| 1351 | * @return string |
||
| 1352 | */ |
||
| 1353 | function render() { |
||
| 1360 | |||
| 1361 | /** |
||
| 1362 | * Allow plugins to filter what archives Infinite Scroll supports |
||
| 1363 | * |
||
| 1364 | * @uses current_theme_supports, is_home, is_archive, apply_filters, self::get_settings |
||
| 1365 | * @return bool |
||
| 1366 | */ |
||
| 1367 | public static function archive_supports_infinity() { |
||
| 1368 | $supported = current_theme_supports( 'infinite-scroll' ) && ( is_home() || is_archive() || is_search() ); |
||
| 1369 | |||
| 1370 | // Disable when previewing a non-active theme in the customizer |
||
| 1371 | if ( is_customize_preview() && ! $GLOBALS['wp_customize']->is_theme_active() ) { |
||
| 1372 | return false; |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | /** |
||
| 1376 | * Allow plugins to filter what archives Infinite Scroll supports. |
||
| 1377 | * |
||
| 1378 | * @module infinite-scroll |
||
| 1379 | * |
||
| 1380 | * @since 2.0.0 |
||
| 1381 | * |
||
| 1382 | * @param bool $supported Does the Archive page support Infinite Scroll. |
||
| 1383 | * @param object self::get_settings() IS settings provided by theme. |
||
| 1384 | */ |
||
| 1385 | return (bool) apply_filters( 'infinite_scroll_archive_supported', $supported, self::get_settings() ); |
||
| 1386 | } |
||
| 1387 | |||
| 1388 | /** |
||
| 1389 | * The Infinite Blog Footer |
||
| 1390 | * |
||
| 1391 | * @uses self::get_settings, self::archive_supports_infinity, self::default_footer |
||
| 1392 | * @return string or null |
||
| 1393 | */ |
||
| 1394 | function footer() { |
||
| 1409 | |||
| 1410 | /** |
||
| 1411 | * Render default IS footer |
||
| 1412 | * |
||
| 1413 | * @uses __, wp_get_theme, get_current_theme, apply_filters, home_url, esc_attr, get_bloginfo, bloginfo |
||
| 1414 | * @return string |
||
| 1415 | */ |
||
| 1416 | private function default_footer() { |
||
| 1451 | |||
| 1452 | /** |
||
| 1453 | * Ensure that IS doesn't interfere with Grunion by stripping IS query arguments from the Grunion redirect URL. |
||
| 1454 | * When arguments are present, Grunion redirects to the IS AJAX endpoint. |
||
| 1455 | * |
||
| 1456 | * @param string $url |
||
| 1457 | * @uses remove_query_arg |
||
| 1458 | * @filter grunion_contact_form_redirect_url |
||
| 1459 | * @return string |
||
| 1460 | */ |
||
| 1461 | public function filter_grunion_redirect_url( $url ) { |
||
| 1476 | }; |
||
| 1477 | |||
| 1478 | /** |
||
| 1677 |
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.