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 |
||
| 21 | class The_Neverending_Home_Page { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Register actions and filters, plus parse IS settings |
||
| 25 | * |
||
| 26 | * @uses add_action, add_filter, self::get_settings |
||
| 27 | * @return null |
||
|
|
|||
| 28 | */ |
||
| 29 | function __construct() { |
||
| 30 | add_action( 'pre_get_posts', array( $this, 'posts_per_page_query' ) ); |
||
| 31 | add_action( 'admin_init', array( $this, 'settings_api_init' ) ); |
||
| 32 | add_action( 'template_redirect', array( $this, 'action_template_redirect' ) ); |
||
| 33 | add_action( 'customize_preview_init', array( $this, 'init_customizer_assets' ) ); |
||
| 34 | add_action( 'template_redirect', array( $this, 'ajax_response' ) ); |
||
| 35 | add_action( 'custom_ajax_infinite_scroll', array( $this, 'query' ) ); |
||
| 36 | add_filter( 'infinite_scroll_query_args', array( $this, 'inject_query_args' ) ); |
||
| 37 | add_filter( 'infinite_scroll_allowed_vars', array( $this, 'allowed_query_vars' ) ); |
||
| 38 | add_action( 'the_post', array( $this, 'preserve_more_tag' ) ); |
||
| 39 | add_action( 'wp_footer', array( $this, 'footer' ) ); |
||
| 40 | add_filter( 'infinite_scroll_additional_scripts', array( $this, 'add_mejs_config' ) ); |
||
| 41 | |||
| 42 | // Plugin compatibility |
||
| 43 | add_filter( 'grunion_contact_form_redirect_url', array( $this, 'filter_grunion_redirect_url' ) ); |
||
| 44 | |||
| 45 | // AMP compatibility |
||
| 46 | // needs to happen after parse_query so that Jetpack_AMP_Support::is_amp_request() is ready. |
||
| 47 | add_action( 'wp', array( $this, 'amp_load_hooks' ) ); |
||
| 48 | |||
| 49 | // Parse IS settings from theme |
||
| 50 | self::get_settings(); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Initialize our static variables |
||
| 55 | */ |
||
| 56 | static $the_time = null; |
||
| 57 | static $settings = null; // Don't access directly, instead use self::get_settings(). |
||
| 58 | |||
| 59 | static $option_name_enabled = 'infinite_scroll'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Parse IS settings provided by theme |
||
| 63 | * |
||
| 64 | * @uses get_theme_support, infinite_scroll_has_footer_widgets, sanitize_title, add_action, get_option, wp_parse_args, is_active_sidebar |
||
| 65 | * @return object |
||
| 66 | */ |
||
| 67 | static function get_settings() { |
||
| 68 | if ( is_null( self::$settings ) ) { |
||
| 69 | $css_pattern = '#[^A-Z\d\-_]#i'; |
||
| 70 | |||
| 71 | $settings = $defaults = array( |
||
| 72 | 'type' => 'scroll', // scroll | click |
||
| 73 | 'requested_type' => 'scroll', // store the original type for use when logic overrides it |
||
| 74 | 'footer_widgets' => false, // true | false | sidebar_id | array of sidebar_ids -- last two are checked with is_active_sidebar |
||
| 75 | 'container' => 'content', // container html id |
||
| 76 | 'wrapper' => true, // true | false | html class |
||
| 77 | 'render' => false, // optional function, otherwise the `content` template part will be used |
||
| 78 | 'footer' => true, // boolean to enable or disable the infinite footer | string to provide an html id to derive footer width from |
||
| 79 | 'footer_callback' => false, // function to be called to render the IS footer, in place of the default |
||
| 80 | 'posts_per_page' => false, // int | false to set based on IS type |
||
| 81 | 'click_handle' => true, // boolean to enable or disable rendering the click handler div. If type is click and this is false, page must include its own trigger with the HTML ID `infinite-handle`. |
||
| 82 | ); |
||
| 83 | |||
| 84 | // Validate settings passed through add_theme_support() |
||
| 85 | $_settings = get_theme_support( 'infinite-scroll' ); |
||
| 86 | |||
| 87 | if ( is_array( $_settings ) ) { |
||
| 88 | // Preferred implementation, where theme provides an array of options |
||
| 89 | if ( isset( $_settings[0] ) && is_array( $_settings[0] ) ) { |
||
| 90 | foreach ( $_settings[0] as $key => $value ) { |
||
| 91 | switch ( $key ) { |
||
| 92 | case 'type' : |
||
| 93 | if ( in_array( $value, array( 'scroll', 'click' ) ) ) |
||
| 94 | $settings[ $key ] = $settings['requested_type'] = $value; |
||
| 95 | |||
| 96 | break; |
||
| 97 | |||
| 98 | case 'footer_widgets' : |
||
| 99 | if ( is_string( $value ) ) |
||
| 100 | $settings[ $key ] = sanitize_title( $value ); |
||
| 101 | elseif ( is_array( $value ) ) |
||
| 102 | $settings[ $key ] = array_map( 'sanitize_title', $value ); |
||
| 103 | elseif ( is_bool( $value ) ) |
||
| 104 | $settings[ $key ] = $value; |
||
| 105 | |||
| 106 | break; |
||
| 107 | |||
| 108 | case 'container' : |
||
| 109 | View Code Duplication | case 'wrapper' : |
|
| 110 | if ( 'wrapper' == $key && is_bool( $value ) ) { |
||
| 111 | $settings[ $key ] = $value; |
||
| 112 | } else { |
||
| 113 | $value = preg_replace( $css_pattern, '', $value ); |
||
| 114 | |||
| 115 | if ( ! empty( $value ) ) |
||
| 116 | $settings[ $key ] = $value; |
||
| 117 | } |
||
| 118 | |||
| 119 | break; |
||
| 120 | |||
| 121 | case 'render' : |
||
| 122 | if ( false !== $value && is_callable( $value ) ) { |
||
| 123 | $settings[ $key ] = $value; |
||
| 124 | } |
||
| 125 | |||
| 126 | break; |
||
| 127 | |||
| 128 | View Code Duplication | case 'footer' : |
|
| 129 | if ( is_bool( $value ) ) { |
||
| 130 | $settings[ $key ] = $value; |
||
| 131 | } elseif ( is_string( $value ) ) { |
||
| 132 | $value = preg_replace( $css_pattern, '', $value ); |
||
| 133 | |||
| 134 | if ( ! empty( $value ) ) |
||
| 135 | $settings[ $key ] = $value; |
||
| 136 | } |
||
| 137 | |||
| 138 | break; |
||
| 139 | |||
| 140 | case 'footer_callback' : |
||
| 141 | if ( is_callable( $value ) ) |
||
| 142 | $settings[ $key ] = $value; |
||
| 143 | else |
||
| 144 | $settings[ $key ] = false; |
||
| 145 | |||
| 146 | break; |
||
| 147 | |||
| 148 | case 'posts_per_page' : |
||
| 149 | if ( is_numeric( $value ) ) |
||
| 150 | $settings[ $key ] = (int) $value; |
||
| 151 | |||
| 152 | break; |
||
| 153 | |||
| 154 | case 'click_handle' : |
||
| 155 | if ( is_bool( $value ) ) { |
||
| 156 | $settings[ $key ] = $value; |
||
| 157 | } |
||
| 158 | |||
| 159 | break; |
||
| 160 | |||
| 161 | default: |
||
| 162 | break; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } elseif ( is_string( $_settings[0] ) ) { |
||
| 166 | // Checks below are for backwards compatibility |
||
| 167 | |||
| 168 | // Container to append new posts to |
||
| 169 | $settings['container'] = preg_replace( $css_pattern, '', $_settings[0] ); |
||
| 170 | |||
| 171 | // Wrap IS elements? |
||
| 172 | if ( isset( $_settings[1] ) ) |
||
| 173 | $settings['wrapper'] = (bool) $_settings[1]; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | // Always ensure all values are present in the final array |
||
| 178 | $settings = wp_parse_args( $settings, $defaults ); |
||
| 179 | |||
| 180 | // Check if a legacy `infinite_scroll_has_footer_widgets()` function is defined and override the footer_widgets parameter's value. |
||
| 181 | // Otherwise, if a widget area ID or array of IDs was provided in the footer_widgets parameter, check if any contains any widgets. |
||
| 182 | // It is safe to use `is_active_sidebar()` before the sidebar is registered as this function doesn't check for a sidebar's existence when determining if it contains any widgets. |
||
| 183 | if ( function_exists( 'infinite_scroll_has_footer_widgets' ) ) { |
||
| 184 | $settings['footer_widgets'] = (bool) infinite_scroll_has_footer_widgets(); |
||
| 185 | } elseif ( is_array( $settings['footer_widgets'] ) ) { |
||
| 186 | $sidebar_ids = $settings['footer_widgets']; |
||
| 187 | $settings['footer_widgets'] = false; |
||
| 188 | |||
| 189 | foreach ( $sidebar_ids as $sidebar_id ) { |
||
| 190 | if ( is_active_sidebar( $sidebar_id ) ) { |
||
| 191 | $settings['footer_widgets'] = true; |
||
| 192 | break; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | unset( $sidebar_ids ); |
||
| 197 | unset( $sidebar_id ); |
||
| 198 | } elseif ( is_string( $settings['footer_widgets'] ) ) { |
||
| 199 | $settings['footer_widgets'] = (bool) is_active_sidebar( $settings['footer_widgets'] ); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Filter Infinite Scroll's `footer_widgets` parameter. |
||
| 204 | * |
||
| 205 | * @module infinite-scroll |
||
| 206 | * |
||
| 207 | * @since 2.0.0 |
||
| 208 | * |
||
| 209 | * @param bool $settings['footer_widgets'] Does the current theme have Footer Widgets. |
||
| 210 | */ |
||
| 211 | $settings['footer_widgets'] = apply_filters( 'infinite_scroll_has_footer_widgets', $settings['footer_widgets'] ); |
||
| 212 | |||
| 213 | // Finally, after all of the sidebar checks and filtering, ensure that a boolean value is present, otherwise set to default of `false`. |
||
| 214 | if ( ! is_bool( $settings['footer_widgets'] ) ) |
||
| 215 | $settings['footer_widgets'] = false; |
||
| 216 | |||
| 217 | // Ensure that IS is enabled and no footer widgets exist if the IS type isn't already "click". |
||
| 218 | if ( 'click' != $settings['type'] ) { |
||
| 219 | // Check the setting status |
||
| 220 | $disabled = '' === get_option( self::$option_name_enabled ) ? true : false; |
||
| 221 | |||
| 222 | // Footer content or Reading option check |
||
| 223 | if ( $settings['footer_widgets'] || $disabled ) |
||
| 224 | $settings['type'] = 'click'; |
||
| 225 | } |
||
| 226 | |||
| 227 | // Force display of the click handler and attendant bits when the type isn't `click` |
||
| 228 | if ( 'click' !== $settings['type'] ) { |
||
| 229 | $settings['click_handle'] = true; |
||
| 230 | } |
||
| 231 | |||
| 232 | // Store final settings in a class static to avoid reparsing |
||
| 233 | /** |
||
| 234 | * Filter the array of Infinite Scroll settings. |
||
| 235 | * |
||
| 236 | * @module infinite-scroll |
||
| 237 | * |
||
| 238 | * @since 2.0.0 |
||
| 239 | * |
||
| 240 | * @param array $settings Array of Infinite Scroll settings. |
||
| 241 | */ |
||
| 242 | self::$settings = apply_filters( 'infinite_scroll_settings', $settings ); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** This filter is already documented in modules/infinite-scroll/infinity.php */ |
||
| 246 | return (object) apply_filters( 'infinite_scroll_settings', self::$settings ); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Number of posts per page. |
||
| 251 | * |
||
| 252 | * @uses self::wp_query, self::get_settings, apply_filters |
||
| 253 | * @return int |
||
| 254 | */ |
||
| 255 | static function posts_per_page() { |
||
| 256 | $posts_per_page = self::get_settings()->posts_per_page ? self::get_settings()->posts_per_page : self::wp_query()->get( 'posts_per_page' ); |
||
| 257 | |||
| 258 | // Take JS query into consideration here |
||
| 259 | if ( true === isset( $_REQUEST['query_args']['posts_per_page'] ) ) { |
||
| 260 | $posts_per_page = $_REQUEST['query_args']['posts_per_page']; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Filter the number of posts per page. |
||
| 265 | * |
||
| 266 | * @module infinite-scroll |
||
| 267 | * |
||
| 268 | * @since 6.0.0 |
||
| 269 | * |
||
| 270 | * @param int $posts_per_page The number of posts to display per page. |
||
| 271 | */ |
||
| 272 | return (int) apply_filters( 'infinite_scroll_posts_per_page', $posts_per_page ); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Retrieve the query used with Infinite Scroll |
||
| 277 | * |
||
| 278 | * @global $wp_the_query |
||
| 279 | * @uses apply_filters |
||
| 280 | * @return object |
||
| 281 | */ |
||
| 282 | static function wp_query() { |
||
| 283 | global $wp_the_query; |
||
| 284 | /** |
||
| 285 | * Filter the Infinite Scroll query object. |
||
| 286 | * |
||
| 287 | * @module infinite-scroll |
||
| 288 | * |
||
| 289 | * @since 2.2.1 |
||
| 290 | * |
||
| 291 | * @param WP_Query $wp_the_query WP Query. |
||
| 292 | */ |
||
| 293 | return apply_filters( 'infinite_scroll_query_object', $wp_the_query ); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Has infinite scroll been triggered? |
||
| 298 | */ |
||
| 299 | static function got_infinity() { |
||
| 300 | /** |
||
| 301 | * Filter the parameter used to check if Infinite Scroll has been triggered. |
||
| 302 | * |
||
| 303 | * @module infinite-scroll |
||
| 304 | * |
||
| 305 | * @since 3.9.0 |
||
| 306 | * |
||
| 307 | * @param bool isset( $_GET[ 'infinity' ] ) Return true if the "infinity" parameter is set. |
||
| 308 | */ |
||
| 309 | return apply_filters( 'infinite_scroll_got_infinity', isset( $_GET[ 'infinity' ] ) ); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Is this guaranteed to be the last batch of posts? |
||
| 314 | */ |
||
| 315 | static function is_last_batch() { |
||
| 316 | /** |
||
| 317 | * Override whether or not this is the last batch for a request |
||
| 318 | * |
||
| 319 | * @module infinite-scroll |
||
| 320 | * |
||
| 321 | * @since 4.8.0 |
||
| 322 | * |
||
| 323 | * @param bool|null null Bool if value should be overridden, null to determine from query |
||
| 324 | * @param object self::wp_query() WP_Query object for current request |
||
| 325 | * @param object self::get_settings() Infinite Scroll settings |
||
| 326 | */ |
||
| 327 | $override = apply_filters( 'infinite_scroll_is_last_batch', null, self::wp_query(), self::get_settings() ); |
||
| 328 | if ( is_bool( $override ) ) { |
||
| 329 | return $override; |
||
| 330 | } |
||
| 331 | |||
| 332 | $entries = (int) self::wp_query()->found_posts; |
||
| 333 | $posts_per_page = self::posts_per_page(); |
||
| 334 | |||
| 335 | // This is to cope with an issue in certain themes or setups where posts are returned but found_posts is 0. |
||
| 336 | if ( 0 == $entries ) { |
||
| 337 | return (bool) ( count( self::wp_query()->posts ) < $posts_per_page ); |
||
| 338 | } |
||
| 339 | $paged = max( 1, self::wp_query()->get( 'paged' ) ); |
||
| 340 | |||
| 341 | // Are there enough posts for more than the first page? |
||
| 342 | if ( $entries <= $posts_per_page ) { |
||
| 343 | return true; |
||
| 344 | } |
||
| 345 | |||
| 346 | // Calculate entries left after a certain number of pages |
||
| 347 | if ( $paged && $paged > 1 ) { |
||
| 348 | $entries -= $posts_per_page * $paged; |
||
| 349 | } |
||
| 350 | |||
| 351 | // Are there some entries left to display? |
||
| 352 | return $entries <= 0; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * The more tag will be ignored by default if the blog page isn't our homepage. |
||
| 357 | * Let's force the $more global to false. |
||
| 358 | */ |
||
| 359 | function preserve_more_tag( $array ) { |
||
| 360 | global $more; |
||
| 361 | |||
| 362 | if ( self::got_infinity() ) |
||
| 363 | $more = 0; //0 = show content up to the more tag. Add more link. |
||
| 364 | |||
| 365 | return $array; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Add a checkbox field to Settings > Reading |
||
| 370 | * for enabling infinite scroll. |
||
| 371 | * |
||
| 372 | * Only show if the current theme supports infinity. |
||
| 373 | * |
||
| 374 | * @uses current_theme_supports, add_settings_field, __, register_setting |
||
| 375 | * @action admin_init |
||
| 376 | * @return null |
||
| 377 | */ |
||
| 378 | function settings_api_init() { |
||
| 379 | if ( ! current_theme_supports( 'infinite-scroll' ) ) |
||
| 380 | return; |
||
| 381 | |||
| 382 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 383 | // This setting is no longer configurable in wp-admin on WordPress.com -- leave a pointer |
||
| 384 | add_settings_field( self::$option_name_enabled, |
||
| 385 | '<span id="infinite-scroll-options">' . esc_html__( 'Infinite Scroll Behavior', 'jetpack' ) . '</span>', |
||
| 386 | array( $this, 'infinite_setting_html_calypso_placeholder' ), |
||
| 387 | 'reading' |
||
| 388 | ); |
||
| 389 | return; |
||
| 390 | } |
||
| 391 | |||
| 392 | // Add the setting field [infinite_scroll] and place it in Settings > Reading |
||
| 393 | add_settings_field( self::$option_name_enabled, '<span id="infinite-scroll-options">' . esc_html__( 'Infinite Scroll Behavior', 'jetpack' ) . '</span>', array( $this, 'infinite_setting_html' ), 'reading' ); |
||
| 394 | register_setting( 'reading', self::$option_name_enabled, 'esc_attr' ); |
||
| 395 | } |
||
| 396 | |||
| 397 | function infinite_setting_html_calypso_placeholder() { |
||
| 398 | $details = get_blog_details(); |
||
| 399 | $writing_url = Redirect::get_url( 'calypso-settings-writing', array( 'site' => $details->domain ) ); |
||
| 400 | echo '<span>' . sprintf( |
||
| 401 | /* translators: Variables are the enclosing link to the settings page */ |
||
| 402 | esc_html__( 'This option has moved. You can now manage it %1$shere%2$s.', 'jetpack' ), |
||
| 403 | '<a href="' . esc_url( $writing_url ) . '">', |
||
| 404 | '</a>' |
||
| 405 | ) . '</span>'; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * HTML code to display a checkbox true/false option |
||
| 410 | * for the infinite_scroll setting. |
||
| 411 | */ |
||
| 412 | function infinite_setting_html() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Does the legwork to determine whether the feature is enabled. |
||
| 426 | * |
||
| 427 | * @uses current_theme_supports, self::archive_supports_infinity, self::get_settings, add_filter, wp_enqueue_script, plugins_url, wp_enqueue_style, add_action |
||
| 428 | * @action template_redirect |
||
| 429 | * @return null |
||
| 430 | */ |
||
| 431 | function action_template_redirect() { |
||
| 432 | // Check that we support infinite scroll, and are on the home page. |
||
| 433 | if ( ! current_theme_supports( 'infinite-scroll' ) || ! self::archive_supports_infinity() ) |
||
| 434 | return; |
||
| 435 | |||
| 436 | $id = self::get_settings()->container; |
||
| 437 | |||
| 438 | // Check that we have an id. |
||
| 439 | if ( empty( $id ) ) |
||
| 440 | return; |
||
| 441 | |||
| 442 | // AMP infinite scroll functionality will start on amp_load_hooks(). |
||
| 443 | if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
||
| 444 | return; |
||
| 445 | } |
||
| 446 | |||
| 447 | // Add our scripts. |
||
| 448 | wp_register_script( |
||
| 449 | 'the-neverending-homepage', |
||
| 450 | Assets::get_file_url_for_environment( |
||
| 451 | '_inc/build/infinite-scroll/infinity.min.js', |
||
| 452 | 'modules/infinite-scroll/infinity.js' |
||
| 453 | ), |
||
| 454 | array(), |
||
| 455 | JETPACK__VERSION . '-is5.0.0', // Added for ability to cachebust on WP.com. |
||
| 456 | true |
||
| 457 | ); |
||
| 458 | |||
| 459 | // Add our default styles. |
||
| 460 | wp_register_style( 'the-neverending-homepage', plugins_url( 'infinity.css', __FILE__ ), array(), '20140422' ); |
||
| 461 | |||
| 462 | // Make sure there are enough posts for IS |
||
| 463 | if ( self::is_last_batch() ) { |
||
| 464 | return; |
||
| 465 | } |
||
| 466 | |||
| 467 | // Add our scripts. |
||
| 468 | wp_enqueue_script( 'the-neverending-homepage' ); |
||
| 469 | |||
| 470 | // Add our default styles. |
||
| 471 | wp_enqueue_style( 'the-neverending-homepage' ); |
||
| 472 | |||
| 473 | add_action( 'wp_footer', array( $this, 'action_wp_footer_settings' ), 2 ); |
||
| 474 | |||
| 475 | 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 |
||
| 476 | |||
| 477 | add_filter( 'infinite_scroll_results', array( $this, 'filter_infinite_scroll_results' ), 10, 3 ); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Initialize the Customizer logic separately from the main JS. |
||
| 482 | * |
||
| 483 | * @since 8.4.0 |
||
| 484 | */ |
||
| 485 | public function init_customizer_assets() { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns classes to be added to <body>. If it's enabled, 'infinite-scroll'. If set to continuous scroll, adds 'neverending' too. |
||
| 503 | * |
||
| 504 | * @since 4.7.0 No longer added as a 'body_class' filter but passed to JS environment and added using JS. |
||
| 505 | * |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | function body_class() { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * 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 |
||
| 524 | * |
||
| 525 | * @uses self::wp_query |
||
| 526 | * @uses self::get_last_post_date |
||
| 527 | * @uses self::has_only_title_matching_posts |
||
| 528 | * @return array |
||
| 529 | */ |
||
| 530 | function get_excluded_posts() { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * 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 |
||
| 553 | * |
||
| 554 | * @uses self::wp_query |
||
| 555 | * @uses self::get_excluded_posts |
||
| 556 | * @return array |
||
| 557 | */ |
||
| 558 | function get_query_vars() { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * This function checks whether all posts returned by initial wp_query match the keyword by title |
||
| 577 | * The code used in this function is borrowed from WP_Query class where it is used to construct like conditions for keywords |
||
| 578 | * |
||
| 579 | * @uses self::wp_query |
||
| 580 | * @return bool |
||
| 581 | */ |
||
| 582 | function has_only_title_matching_posts() { |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Grab the timestamp for the initial query's last post. |
||
| 614 | * |
||
| 615 | * This takes into account the query's 'orderby' parameter and returns |
||
| 616 | * false if the posts are not ordered by date. |
||
| 617 | * |
||
| 618 | * @uses self::got_infinity |
||
| 619 | * @uses self::has_only_title_matching_posts |
||
| 620 | * @uses self::wp_query |
||
| 621 | * @return string 'Y-m-d H:i:s' or false |
||
| 622 | */ |
||
| 623 | function get_last_post_date() { |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Returns the appropriate `wp_posts` table field for a given query's |
||
| 653 | * 'orderby' parameter, if applicable. |
||
| 654 | * |
||
| 655 | * @param optional object $query |
||
| 656 | * @uses self::wp_query |
||
| 657 | * @return string or false |
||
| 658 | */ |
||
| 659 | function get_query_sort_field( $query = null ) { |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Create a where clause that will make sure post queries return posts |
||
| 678 | * in the correct order, without duplicates, if a new post is added |
||
| 679 | * and we're sorting by post date. |
||
| 680 | * |
||
| 681 | * @global $wpdb |
||
| 682 | * @param string $where |
||
| 683 | * @param object $query |
||
| 684 | * @uses apply_filters |
||
| 685 | * @filter posts_where |
||
| 686 | * @return string |
||
| 687 | */ |
||
| 688 | function query_time_filter( $where, $query ) { |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Let's overwrite the default post_per_page setting to always display a fixed amount. |
||
| 729 | * |
||
| 730 | * @param object $query |
||
| 731 | * @uses is_admin, self::archive_supports_infinity, self::get_settings |
||
| 732 | * @return null |
||
| 733 | */ |
||
| 734 | function posts_per_page_query( $query ) { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Check if the IS output should be wrapped in a div. |
||
| 741 | * Setting value can be a boolean or a string specifying the class applied to the div. |
||
| 742 | * |
||
| 743 | * @uses self::get_settings |
||
| 744 | * @return bool |
||
| 745 | */ |
||
| 746 | function has_wrapper() { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Returns the Ajax url |
||
| 752 | * |
||
| 753 | * @global $wp |
||
| 754 | * @uses home_url, add_query_arg, apply_filters |
||
| 755 | * @return string |
||
| 756 | */ |
||
| 757 | function ajax_url() { |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Our own Ajax response, avoiding calling admin-ajax |
||
| 776 | */ |
||
| 777 | function ajax_response() { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Alias for renamed class method. |
||
| 803 | * |
||
| 804 | * Previously, JS settings object was unnecessarily output in the document head. |
||
| 805 | * When the hook was changed, the method name no longer made sense. |
||
| 806 | */ |
||
| 807 | function action_wp_head() { |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Prints the relevant infinite scroll settings in JS. |
||
| 813 | * |
||
| 814 | * @global $wp_rewrite |
||
| 815 | * @uses self::get_settings, esc_js, esc_url_raw, self::has_wrapper, __, apply_filters, do_action, self::get_query_vars |
||
| 816 | * @action wp_footer |
||
| 817 | * @return string |
||
| 818 | */ |
||
| 819 | function action_wp_footer_settings() { |
||
| 938 | |||
| 939 | /** |
||
| 940 | * Build path data for current request. |
||
| 941 | * Used for Google Analytics and pushState history tracking. |
||
| 942 | * |
||
| 943 | * @global $wp_rewrite |
||
| 944 | * @global $wp |
||
| 945 | * @uses user_trailingslashit, sanitize_text_field, add_query_arg |
||
| 946 | * @return string|bool |
||
| 947 | */ |
||
| 948 | private function get_request_path() { |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Return query string for current request, prefixed with '?'. |
||
| 984 | * |
||
| 985 | * @return string |
||
| 986 | */ |
||
| 987 | private function get_request_parameters() { |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Provide IS with a list of the scripts and stylesheets already present on the page. |
||
| 997 | * Since posts may contain require additional assets that haven't been loaded, this data will be used to track the additional assets. |
||
| 998 | * |
||
| 999 | * @global $wp_scripts, $wp_styles |
||
| 1000 | * @action wp_footer |
||
| 1001 | * @return string |
||
| 1002 | */ |
||
| 1003 | function action_wp_footer() { |
||
| 1060 | |||
| 1061 | /** |
||
| 1062 | * Identify additional scripts required by the latest set of IS posts and provide the necessary data to the IS response handler. |
||
| 1063 | * |
||
| 1064 | * @global $wp_scripts |
||
| 1065 | * @uses sanitize_text_field, add_query_arg |
||
| 1066 | * @filter infinite_scroll_results |
||
| 1067 | * @return array |
||
| 1068 | */ |
||
| 1069 | function filter_infinite_scroll_results( $results, $query_args, $wp_query ) { |
||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * Runs the query and returns the results via JSON. |
||
| 1281 | * Triggered by an AJAX request. |
||
| 1282 | * |
||
| 1283 | * @global $wp_query |
||
| 1284 | * @global $wp_the_query |
||
| 1285 | * @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 |
||
| 1286 | * @return string or null |
||
| 1287 | */ |
||
| 1288 | function query() { |
||
| 1472 | |||
| 1473 | /** |
||
| 1474 | * Update the $allowed_vars array with the standard WP public and private |
||
| 1475 | * query vars, as well as taxonomy vars |
||
| 1476 | * |
||
| 1477 | * @global $wp |
||
| 1478 | * @param array $allowed_vars |
||
| 1479 | * @filter infinite_scroll_allowed_vars |
||
| 1480 | * @return array |
||
| 1481 | */ |
||
| 1482 | function allowed_query_vars( $allowed_vars ) { |
||
| 1495 | |||
| 1496 | /** |
||
| 1497 | * Returns an array of stock and custom taxonomy query vars |
||
| 1498 | * |
||
| 1499 | * @global $wp_taxonomies |
||
| 1500 | * @return array |
||
| 1501 | */ |
||
| 1502 | function get_taxonomy_vars() { |
||
| 1516 | |||
| 1517 | /** |
||
| 1518 | * Update the $query_args array with the parameters provided via AJAX/GET. |
||
| 1519 | * |
||
| 1520 | * @param array $query_args |
||
| 1521 | * @filter infinite_scroll_query_args |
||
| 1522 | * @return array |
||
| 1523 | */ |
||
| 1524 | function inject_query_args( $query_args ) { |
||
| 1550 | |||
| 1551 | /** |
||
| 1552 | * Rendering fallback used when themes don't specify their own handler. |
||
| 1553 | * |
||
| 1554 | * @uses have_posts, the_post, get_template_part, get_post_format |
||
| 1555 | * @action infinite_scroll_render |
||
| 1556 | * @return string |
||
| 1557 | */ |
||
| 1558 | function render() { |
||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Allow plugins to filter what archives Infinite Scroll supports |
||
| 1568 | * |
||
| 1569 | * @uses current_theme_supports, is_home, is_archive, apply_filters, self::get_settings |
||
| 1570 | * @return bool |
||
| 1571 | */ |
||
| 1572 | public static function archive_supports_infinity() { |
||
| 1592 | |||
| 1593 | /** |
||
| 1594 | * The Infinite Blog Footer |
||
| 1595 | * |
||
| 1596 | * @uses self::get_settings, self::archive_supports_infinity, self::default_footer |
||
| 1597 | * @return string or null |
||
| 1598 | */ |
||
| 1599 | function footer() { |
||
| 1622 | |||
| 1623 | /** |
||
| 1624 | * Render default IS footer |
||
| 1625 | * |
||
| 1626 | * @uses __, wp_get_theme, apply_filters, home_url, esc_attr, get_bloginfo, bloginfo |
||
| 1627 | * @return string |
||
| 1628 | * |
||
| 1629 | */ |
||
| 1630 | private function default_footer() { |
||
| 1671 | |||
| 1672 | /** |
||
| 1673 | * Ensure that IS doesn't interfere with Grunion by stripping IS query arguments from the Grunion redirect URL. |
||
| 1674 | * When arguments are present, Grunion redirects to the IS AJAX endpoint. |
||
| 1675 | * |
||
| 1676 | * @param string $url |
||
| 1677 | * @uses remove_query_arg |
||
| 1678 | * @filter grunion_contact_form_redirect_url |
||
| 1679 | * @return string |
||
| 1680 | */ |
||
| 1681 | public function filter_grunion_redirect_url( $url ) { |
||
| 1696 | |||
| 1697 | /** |
||
| 1698 | * When the MediaElement is loaded in dynamically, we need to enforce that |
||
| 1699 | * its settings are added to the page as well. |
||
| 1700 | * |
||
| 1701 | * @param array $scripts_data New scripts exposed to the infinite scroll. |
||
| 1702 | * |
||
| 1703 | * @since 8.4.0 |
||
| 1704 | */ |
||
| 1705 | public function add_mejs_config( $scripts_data ) { |
||
| 1723 | |||
| 1724 | /** |
||
| 1725 | * Determines whether the legacy AMP Reader post templates are being used. |
||
| 1726 | * |
||
| 1727 | * @return bool |
||
| 1728 | */ |
||
| 1729 | private function is_exempted_amp_page() { |
||
| 1744 | |||
| 1745 | /** |
||
| 1746 | * Load AMP specific hooks. |
||
| 1747 | * |
||
| 1748 | * @return void |
||
| 1749 | */ |
||
| 1750 | public function amp_load_hooks() { |
||
| 1772 | |||
| 1773 | /** |
||
| 1774 | * Start the AMP output buffering. |
||
| 1775 | * |
||
| 1776 | * @return void |
||
| 1777 | */ |
||
| 1778 | public function amp_start_output_buffering() { |
||
| 1781 | |||
| 1782 | /** |
||
| 1783 | * Flush the AMP output buffer. |
||
| 1784 | * |
||
| 1785 | * @return void |
||
| 1786 | */ |
||
| 1787 | public function amp_output_buffer() { |
||
| 1792 | |||
| 1793 | /** |
||
| 1794 | * Filter the AMP output buffer contents. |
||
| 1795 | * |
||
| 1796 | * @param string $buffer Contents of the output buffer. |
||
| 1797 | * |
||
| 1798 | * @return string|false |
||
| 1799 | */ |
||
| 1800 | public function amp_finish_output_buffering( $buffer ) { |
||
| 1841 | |||
| 1842 | /** |
||
| 1843 | * Get AMP next page markup with the custom footers. |
||
| 1844 | * |
||
| 1845 | * @param string[] $footers The theme footers. |
||
| 1846 | * |
||
| 1847 | * @return string |
||
| 1848 | */ |
||
| 1849 | protected function amp_get_footer_template( $footers = array() ) { |
||
| 1866 | |||
| 1867 | /** |
||
| 1868 | * AMP Next Page markup. |
||
| 1869 | * |
||
| 1870 | * @return string |
||
| 1871 | */ |
||
| 1872 | protected function amp_footer_template() { |
||
| 1924 | |||
| 1925 | /** |
||
| 1926 | * Get the AMP next page information. |
||
| 1927 | * |
||
| 1928 | * @return array |
||
| 1929 | */ |
||
| 1930 | protected function amp_next_page() { |
||
| 1967 | |||
| 1968 | /** |
||
| 1969 | * Get the number of pages left. |
||
| 1970 | * |
||
| 1971 | * @return int |
||
| 1972 | */ |
||
| 1973 | protected static function amp_get_max_pages() { |
||
| 1978 | |||
| 1979 | /** |
||
| 1980 | * Is the last page. |
||
| 1981 | * |
||
| 1982 | * @return bool |
||
| 1983 | */ |
||
| 1984 | protected static function amp_is_last_page() { |
||
| 1987 | }; |
||
| 1988 | |||
| 2050 |
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.