Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | Plugin Name: The Neverending Home Page. |
||
| 5 | Plugin URI: http://automattic.com/ |
||
| 6 | Description: Adds infinite scrolling support to the front-end blog post view for themes, pulling the next set of posts automatically into view when the reader approaches the bottom of the page. |
||
| 7 | Version: 1.1 |
||
| 8 | Author: Automattic |
||
| 9 | Author URI: http://automattic.com/ |
||
| 10 | License: GNU General Public License v2 or later |
||
| 11 | License URI: http://www.gnu.org/licenses/gpl-2.0.html |
||
| 12 | */ |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Class: The_Neverending_Home_Page relies on add_theme_support, expects specific |
||
| 16 | * styling from each theme; including fixed footer. |
||
| 17 | */ |
||
| 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() { |
||
| 59 | if ( is_null( self::$settings ) ) { |
||
| 60 | $css_pattern = '#[^A-Z\d\-_]#i'; |
||
| 61 | |||
| 62 | $settings = $defaults = array( |
||
| 63 | 'type' => 'scroll', // scroll | click |
||
| 64 | 'requested_type' => 'scroll', // store the original type for use when logic overrides it |
||
| 65 | 'footer_widgets' => false, // true | false | sidebar_id | array of sidebar_ids -- last two are checked with is_active_sidebar |
||
| 66 | 'container' => 'content', // container html id |
||
| 67 | 'wrapper' => true, // true | false | html class |
||
| 68 | 'render' => false, // optional function, otherwise the `content` template part will be used |
||
| 69 | 'footer' => true, // boolean to enable or disable the infinite footer | string to provide an html id to derive footer width from |
||
| 70 | 'footer_callback' => false, // function to be called to render the IS footer, in place of the default |
||
| 71 | 'posts_per_page' => false, // int | false to set based on IS type |
||
| 72 | '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`. |
||
| 73 | ); |
||
| 74 | |||
| 75 | // Validate settings passed through add_theme_support() |
||
| 76 | $_settings = get_theme_support( 'infinite-scroll' ); |
||
| 77 | |||
| 78 | if ( is_array( $_settings ) ) { |
||
| 79 | // Preferred implementation, where theme provides an array of options |
||
| 80 | if ( isset( $_settings[0] ) && is_array( $_settings[0] ) ) { |
||
| 81 | foreach ( $_settings[0] as $key => $value ) { |
||
| 82 | switch ( $key ) { |
||
| 83 | case 'type' : |
||
| 84 | if ( in_array( $value, array( 'scroll', 'click' ) ) ) |
||
| 85 | $settings[ $key ] = $settings['requested_type'] = $value; |
||
| 86 | |||
| 87 | break; |
||
| 88 | |||
| 89 | case 'footer_widgets' : |
||
| 90 | if ( is_string( $value ) ) |
||
| 91 | $settings[ $key ] = sanitize_title( $value ); |
||
| 92 | elseif ( is_array( $value ) ) |
||
| 93 | $settings[ $key ] = array_map( 'sanitize_title', $value ); |
||
| 94 | elseif ( is_bool( $value ) ) |
||
| 95 | $settings[ $key ] = $value; |
||
| 96 | |||
| 97 | break; |
||
| 98 | |||
| 99 | case 'container' : |
||
| 100 | View Code Duplication | case 'wrapper' : |
|
| 101 | if ( 'wrapper' == $key && is_bool( $value ) ) { |
||
| 102 | $settings[ $key ] = $value; |
||
| 103 | } else { |
||
| 104 | $value = preg_replace( $css_pattern, '', $value ); |
||
| 105 | |||
| 106 | if ( ! empty( $value ) ) |
||
| 107 | $settings[ $key ] = $value; |
||
| 108 | } |
||
| 109 | |||
| 110 | break; |
||
| 111 | |||
| 112 | case 'render' : |
||
| 113 | if ( false !== $value && is_callable( $value ) ) { |
||
| 114 | $settings[ $key ] = $value; |
||
| 115 | |||
| 116 | add_action( 'infinite_scroll_render', $value ); |
||
| 117 | } |
||
| 118 | |||
| 119 | break; |
||
| 120 | |||
| 121 | View Code Duplication | case 'footer' : |
|
| 122 | if ( is_bool( $value ) ) { |
||
| 123 | $settings[ $key ] = $value; |
||
| 124 | } elseif ( is_string( $value ) ) { |
||
| 125 | $value = preg_replace( $css_pattern, '', $value ); |
||
| 126 | |||
| 127 | if ( ! empty( $value ) ) |
||
| 128 | $settings[ $key ] = $value; |
||
| 129 | } |
||
| 130 | |||
| 131 | break; |
||
| 132 | |||
| 133 | case 'footer_callback' : |
||
| 134 | if ( is_callable( $value ) ) |
||
| 135 | $settings[ $key ] = $value; |
||
| 136 | else |
||
| 137 | $settings[ $key ] = false; |
||
| 138 | |||
| 139 | break; |
||
| 140 | |||
| 141 | case 'posts_per_page' : |
||
| 142 | if ( is_numeric( $value ) ) |
||
| 143 | $settings[ $key ] = (int) $value; |
||
| 144 | |||
| 145 | break; |
||
| 146 | |||
| 147 | case 'click_handle' : |
||
| 148 | if ( is_bool( $value ) ) { |
||
| 149 | $settings[ $key ] = $value; |
||
| 150 | } |
||
| 151 | |||
| 152 | break; |
||
| 153 | |||
| 154 | default: |
||
| 155 | continue; |
||
| 156 | |||
| 157 | break; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } elseif ( is_string( $_settings[0] ) ) { |
||
| 161 | // Checks below are for backwards compatibility |
||
| 162 | |||
| 163 | // Container to append new posts to |
||
| 164 | $settings['container'] = preg_replace( $css_pattern, '', $_settings[0] ); |
||
| 165 | |||
| 166 | // Wrap IS elements? |
||
| 167 | if ( isset( $_settings[1] ) ) |
||
| 168 | $settings['wrapper'] = (bool) $_settings[1]; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | // Always ensure all values are present in the final array |
||
| 173 | $settings = wp_parse_args( $settings, $defaults ); |
||
| 174 | |||
| 175 | // Check if a legacy `infinite_scroll_has_footer_widgets()` function is defined and override the footer_widgets parameter's value. |
||
| 176 | // Otherwise, if a widget area ID or array of IDs was provided in the footer_widgets parameter, check if any contains any widgets. |
||
| 177 | // 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. |
||
| 178 | if ( function_exists( 'infinite_scroll_has_footer_widgets' ) ) { |
||
| 179 | $settings['footer_widgets'] = (bool) infinite_scroll_has_footer_widgets(); |
||
| 180 | } elseif ( is_array( $settings['footer_widgets'] ) ) { |
||
| 181 | $sidebar_ids = $settings['footer_widgets']; |
||
| 182 | $settings['footer_widgets'] = false; |
||
| 183 | |||
| 184 | foreach ( $sidebar_ids as $sidebar_id ) { |
||
| 185 | if ( is_active_sidebar( $sidebar_id ) ) { |
||
| 186 | $settings['footer_widgets'] = true; |
||
| 187 | break; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | unset( $sidebar_ids ); |
||
| 192 | unset( $sidebar_id ); |
||
| 193 | } elseif ( is_string( $settings['footer_widgets'] ) ) { |
||
| 194 | $settings['footer_widgets'] = (bool) is_active_sidebar( $settings['footer_widgets'] ); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Filter Infinite Scroll's `footer_widgets` parameter. |
||
| 199 | * |
||
| 200 | * @module infinite-scroll |
||
| 201 | * |
||
| 202 | * @since 2.0.0 |
||
| 203 | * |
||
| 204 | * @param bool $settings['footer_widgets'] Does the current theme have Footer Widgets. |
||
| 205 | */ |
||
| 206 | $settings['footer_widgets'] = apply_filters( 'infinite_scroll_has_footer_widgets', $settings['footer_widgets'] ); |
||
| 207 | |||
| 208 | // Finally, after all of the sidebar checks and filtering, ensure that a boolean value is present, otherwise set to default of `false`. |
||
| 209 | if ( ! is_bool( $settings['footer_widgets'] ) ) |
||
| 210 | $settings['footer_widgets'] = false; |
||
| 211 | |||
| 212 | // Ensure that IS is enabled and no footer widgets exist if the IS type isn't already "click". |
||
| 213 | if ( 'click' != $settings['type'] ) { |
||
| 214 | // Check the setting status |
||
| 215 | $disabled = '' === get_option( self::$option_name_enabled ) ? true : false; |
||
| 216 | |||
| 217 | // Footer content or Reading option check |
||
| 218 | if ( $settings['footer_widgets'] || $disabled ) |
||
| 219 | $settings['type'] = 'click'; |
||
| 220 | } |
||
| 221 | |||
| 222 | // posts_per_page defaults to 7 for scroll, posts_per_page option for click |
||
| 223 | if ( false === $settings['posts_per_page'] ) { |
||
| 224 | if ( 'scroll' === $settings['type'] ) { |
||
| 225 | $settings['posts_per_page'] = 7; |
||
| 226 | } |
||
| 227 | else { |
||
| 228 | $settings['posts_per_page'] = (int) get_option( 'posts_per_page' ); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | // If IS is set to click, and if the site owner changed posts_per_page, let's use that |
||
| 233 | if ( |
||
| 234 | 'click' == $settings['type'] |
||
| 235 | && ( '10' !== get_option( 'posts_per_page' ) ) |
||
| 236 | ) { |
||
| 237 | $settings['posts_per_page'] = (int) get_option( 'posts_per_page' ); |
||
| 238 | } |
||
| 239 | |||
| 240 | // Force display of the click handler and attendant bits when the type isn't `click` |
||
| 241 | if ( 'click' !== $settings['type'] ) { |
||
| 242 | $settings['click_handle'] = true; |
||
| 243 | } |
||
| 244 | |||
| 245 | // Store final settings in a class static to avoid reparsing |
||
| 246 | /** |
||
| 247 | * Filter the array of Infinite Scroll settings. |
||
| 248 | * |
||
| 249 | * @module infinite-scroll |
||
| 250 | * |
||
| 251 | * @since 2.0.0 |
||
| 252 | * |
||
| 253 | * @param array $settings Array of Infinite Scroll settings. |
||
| 254 | */ |
||
| 255 | self::$settings = apply_filters( 'infinite_scroll_settings', $settings ); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** This filter is already documented in modules/infinite-scroll/infinity.php */ |
||
| 259 | return (object) apply_filters( 'infinite_scroll_settings', self::$settings ); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Retrieve the query used with Infinite Scroll |
||
| 264 | * |
||
| 265 | * @global $wp_the_query |
||
| 266 | * @uses apply_filters |
||
| 267 | * @return object |
||
| 268 | */ |
||
| 269 | static function wp_query() { |
||
| 270 | global $wp_the_query; |
||
| 271 | /** |
||
| 272 | * Filter the Infinite Scroll query object. |
||
| 273 | * |
||
| 274 | * @module infinite-scroll |
||
| 275 | * |
||
| 276 | * @since 2.2.1 |
||
| 277 | * |
||
| 278 | * @param WP_Query $wp_the_query WP Query. |
||
| 279 | */ |
||
| 280 | return apply_filters( 'infinite_scroll_query_object', $wp_the_query ); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Has infinite scroll been triggered? |
||
| 285 | */ |
||
| 286 | static function got_infinity() { |
||
| 287 | /** |
||
| 288 | * Filter the parameter used to check if Infinite Scroll has been triggered. |
||
| 289 | * |
||
| 290 | * @module infinite-scroll |
||
| 291 | * |
||
| 292 | * @since 3.9.0 |
||
| 293 | * |
||
| 294 | * @param bool isset( $_GET[ 'infinity' ] ) Return true if the "infinity" parameter is set. |
||
| 295 | */ |
||
| 296 | return apply_filters( 'infinite_scroll_got_infinity', isset( $_GET[ 'infinity' ] ) ); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Is this guaranteed to be the last batch of posts? |
||
| 301 | */ |
||
| 302 | static function is_last_batch() { |
||
| 303 | $post_type = get_post_type(); |
||
| 304 | $entries = wp_count_posts( empty( $post_type ) ? 'post' : $post_type )->publish; |
||
| 305 | if ( self::wp_query()->get( 'paged' ) && self::wp_query()->get( 'paged' ) > 1 ) { |
||
| 306 | $entries -= self::get_settings()->posts_per_page * self::wp_query()->get( 'paged' ); |
||
| 307 | } |
||
| 308 | return $entries <= self::get_settings()->posts_per_page; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * The more tag will be ignored by default if the blog page isn't our homepage. |
||
| 313 | * Let's force the $more global to false. |
||
| 314 | */ |
||
| 315 | function preserve_more_tag( $array ) { |
||
| 316 | global $more; |
||
| 317 | |||
| 318 | if ( self::got_infinity() ) |
||
| 319 | $more = 0; //0 = show content up to the more tag. Add more link. |
||
| 320 | |||
| 321 | return $array; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Add a checkbox field to Settings > Reading |
||
| 326 | * for enabling infinite scroll. |
||
| 327 | * |
||
| 328 | * Only show if the current theme supports infinity. |
||
| 329 | * |
||
| 330 | * @uses current_theme_supports, add_settings_field, __, register_setting |
||
| 331 | * @action admin_init |
||
| 332 | * @return null |
||
| 333 | */ |
||
| 334 | function settings_api_init() { |
||
| 335 | if ( ! current_theme_supports( 'infinite-scroll' ) ) |
||
| 336 | return; |
||
| 337 | |||
| 338 | // Add the setting field [infinite_scroll] and place it in Settings > Reading |
||
| 339 | 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' ); |
||
| 340 | register_setting( 'reading', self::$option_name_enabled, 'esc_attr' ); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * HTML code to display a checkbox true/false option |
||
| 345 | * for the infinite_scroll setting. |
||
| 346 | */ |
||
| 347 | function infinite_setting_html() { |
||
| 348 | $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>'; |
||
| 349 | |||
| 350 | // If the blog has footer widgets, show a notice instead of the checkbox |
||
| 351 | if ( self::get_settings()->footer_widgets || 'click' == self::get_settings()->requested_type ) { |
||
| 352 | echo '<label>' . $notice . '</label>'; |
||
| 353 | } else { |
||
| 354 | echo '<label><input name="infinite_scroll" type="checkbox" value="1" ' . checked( 1, '' !== get_option( self::$option_name_enabled ), false ) . ' /> ' . esc_html__( 'Check to load posts as you scroll. Uncheck to show clickable button to load posts', 'jetpack' ) . '</label>'; |
||
| 355 | echo '<p class="description">' . sprintf( esc_html__( 'Shows %s posts on each load.', 'jetpack' ), number_format_i18n( self::get_settings()->posts_per_page ) ) . '</p>'; |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Does the legwork to determine whether the feature is enabled. |
||
| 361 | * |
||
| 362 | * @uses current_theme_supports, self::archive_supports_infinity, self::get_settings, add_filter, wp_enqueue_script, plugins_url, wp_enqueue_style, add_action |
||
| 363 | * @action template_redirect |
||
| 364 | * @return null |
||
| 365 | */ |
||
| 366 | function action_template_redirect() { |
||
| 367 | // Check that we support infinite scroll, and are on the home page. |
||
| 368 | if ( ! current_theme_supports( 'infinite-scroll' ) || ! self::archive_supports_infinity() ) |
||
| 369 | return; |
||
| 370 | |||
| 371 | $id = self::get_settings()->container; |
||
| 372 | |||
| 373 | // Check that we have an id. |
||
| 374 | if ( empty( $id ) ) |
||
| 375 | return; |
||
| 376 | |||
| 377 | // Add our scripts. |
||
| 378 | wp_register_script( 'the-neverending-homepage', plugins_url( 'infinity.js', __FILE__ ), array( 'jquery' ), '4.0.0', true ); |
||
| 379 | |||
| 380 | // Add our default styles. |
||
| 381 | wp_register_style( 'the-neverending-homepage', plugins_url( 'infinity.css', __FILE__ ), array(), '20140422' ); |
||
| 382 | |||
| 383 | // Make sure there are enough posts for IS |
||
| 384 | if ( self::is_last_batch() ) { |
||
| 385 | return; |
||
| 386 | } |
||
| 387 | |||
| 388 | // Add a class to the body. |
||
| 389 | add_filter( 'body_class', array( $this, 'body_class' ) ); |
||
| 390 | |||
| 391 | // Add our scripts. |
||
| 392 | wp_enqueue_script( 'the-neverending-homepage' ); |
||
| 393 | |||
| 394 | // Add our default styles. |
||
| 395 | wp_enqueue_style( 'the-neverending-homepage' ); |
||
| 396 | |||
| 397 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_spinner_scripts' ) ); |
||
| 398 | |||
| 399 | add_action( 'wp_footer', array( $this, 'action_wp_footer_settings' ), 2 ); |
||
| 400 | |||
| 401 | 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 |
||
| 402 | |||
| 403 | add_filter( 'infinite_scroll_results', array( $this, 'filter_infinite_scroll_results' ), 10, 3 ); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Enqueue spinner scripts. |
||
| 408 | */ |
||
| 409 | function enqueue_spinner_scripts() { |
||
| 410 | wp_enqueue_script( 'jquery.spin' ); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Adds an 'infinite-scroll' class to the body. |
||
| 415 | */ |
||
| 416 | function body_class( $classes ) { |
||
| 417 | // Do not add infinity-scroll class if disabled through the Reading page |
||
| 418 | $disabled = '' === get_option( self::$option_name_enabled ) ? true : false; |
||
| 419 | if ( ! $disabled || 'click' == self::get_settings()->type ) { |
||
| 420 | $classes[] = 'infinite-scroll'; |
||
| 421 | |||
| 422 | if ( 'scroll' == self::get_settings()->type ) |
||
| 423 | $classes[] = 'neverending'; |
||
| 424 | } |
||
| 425 | |||
| 426 | return $classes; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * 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 |
||
| 431 | * |
||
| 432 | * @uses self::wp_query |
||
| 433 | * @uses self::get_last_post_date |
||
| 434 | * @uses self::has_only_title_matching_posts |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | function get_excluded_posts() { |
||
| 438 | |||
| 439 | $excluded_posts = array(); |
||
| 440 | //loop through posts returned by wp_query call |
||
| 441 | foreach( self::wp_query()->get_posts() as $post ) { |
||
| 442 | |||
| 443 | $orderby = isset( self::wp_query()->query_vars['orderby'] ) ? self::wp_query()->query_vars['orderby'] : ''; |
||
| 444 | $post_date = ( ! empty( $post->post_date ) ? $post->post_date : false ); |
||
| 445 | if ( 'modified' === $orderby || false === $post_date ) { |
||
| 446 | $post_date = $post->post_modified; |
||
| 447 | } |
||
| 448 | |||
| 449 | //in case all posts initially displayed match the keyword by title we add em all to excluded posts array |
||
| 450 | //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 |
||
| 451 | if ( self::has_only_title_matching_posts() || $post_date <= self::get_last_post_date() ) { |
||
| 452 | array_push( $excluded_posts, $post->ID ); |
||
| 453 | } |
||
| 454 | } |
||
| 455 | return $excluded_posts; |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * 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 |
||
| 460 | * |
||
| 461 | * @uses self::wp_query |
||
| 462 | * @uses self::get_excluded_posts |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | function get_query_vars() { |
||
| 466 | |||
| 467 | $query_vars = self::wp_query()->query_vars; |
||
| 468 | //applies to search page only |
||
| 469 | if ( true === self::wp_query()->is_search() ) { |
||
| 470 | //set post__not_in array in query_vars in case it does not exists |
||
| 471 | if ( false === isset( $query_vars['post__not_in'] ) ) { |
||
| 472 | $query_vars['post__not_in'] = array(); |
||
| 473 | } |
||
| 474 | //get excluded posts |
||
| 475 | $excluded = self::get_excluded_posts(); |
||
| 476 | //merge them with other post__not_in posts (eg.: sticky posts) |
||
| 477 | $query_vars['post__not_in'] = array_merge( $query_vars['post__not_in'], $excluded ); |
||
| 478 | } |
||
| 479 | return $query_vars; |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * This function checks whether all posts returned by initial wp_query match the keyword by title |
||
| 484 | * The code used in this function is borrowed from WP_Query class where it is used to construct like conditions for keywords |
||
| 485 | * |
||
| 486 | * @uses self::wp_query |
||
| 487 | * @return bool |
||
| 488 | */ |
||
| 489 | function has_only_title_matching_posts() { |
||
| 490 | |||
| 491 | //apply following logic for search page results only |
||
| 492 | if ( false === self::wp_query()->is_search() ) { |
||
| 493 | return false; |
||
| 494 | } |
||
| 495 | |||
| 496 | //grab the last posts in the stack as if the last one is title-matching the rest is title-matching as well |
||
| 497 | $post = end( self::wp_query()->posts ); |
||
| 498 | |||
| 499 | //code inspired by WP_Query class |
||
| 500 | if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', self::wp_query()->get( 's' ), $matches ) ) { |
||
| 501 | $search_terms = self::wp_query()->query_vars['search_terms']; |
||
| 502 | // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence |
||
| 503 | if ( empty( $search_terms ) || count( $search_terms ) > 9 ) { |
||
| 504 | $search_terms = array( self::wp_query()->get( 's' ) ); |
||
| 505 | } |
||
| 506 | } else { |
||
| 507 | $search_terms = array( self::wp_query()->get( 's' ) ); |
||
| 508 | } |
||
| 509 | |||
| 510 | //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 |
||
| 511 | $term = current( $search_terms ); |
||
| 512 | if ( ! empty( $term ) && false !== strpos( $post->post_title, $term ) ) { |
||
| 513 | return true; |
||
| 514 | } |
||
| 515 | |||
| 516 | return false; |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Grab the timestamp for the initial query's last post. |
||
| 521 | * |
||
| 522 | * This takes into account the query's 'orderby' parameter and returns |
||
| 523 | * false if the posts are not ordered by date. |
||
| 524 | * |
||
| 525 | * @uses self::got_infinity |
||
| 526 | * @uses self::has_only_title_matching_posts |
||
| 527 | * @uses self::wp_query |
||
| 528 | * @return string 'Y-m-d H:i:s' or false |
||
| 529 | */ |
||
| 530 | function get_last_post_date() { |
||
| 531 | if ( self::got_infinity() ) |
||
| 532 | return; |
||
| 533 | |||
| 534 | if ( ! self::wp_query()->have_posts() ) { |
||
| 535 | return null; |
||
| 536 | } |
||
| 537 | |||
| 538 | //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 |
||
| 539 | if ( true === self::has_only_title_matching_posts() ) { |
||
| 540 | return false; |
||
| 541 | } |
||
| 542 | |||
| 543 | $post = end( self::wp_query()->posts ); |
||
| 544 | $orderby = isset( self::wp_query()->query_vars['orderby'] ) ? |
||
| 545 | self::wp_query()->query_vars['orderby'] : ''; |
||
| 546 | $post_date = ( ! empty( $post->post_date ) ? $post->post_date : false ); |
||
| 547 | View Code Duplication | switch ( $orderby ) { |
|
| 548 | case 'modified': |
||
| 549 | return $post->post_modified; |
||
| 550 | case 'date': |
||
| 551 | case '': |
||
| 552 | return $post_date; |
||
| 553 | default: |
||
| 554 | return false; |
||
| 555 | } |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Returns the appropriate `wp_posts` table field for a given query's |
||
| 560 | * 'orderby' parameter, if applicable. |
||
| 561 | * |
||
| 562 | * @param optional object $query |
||
| 563 | * @uses self::wp_query |
||
| 564 | * @return string or false |
||
| 565 | */ |
||
| 566 | function get_query_sort_field( $query = null ) { |
||
| 567 | if ( empty( $query ) ) |
||
| 568 | $query = self::wp_query(); |
||
| 569 | |||
| 570 | $orderby = isset( $query->query_vars['orderby'] ) ? $query->query_vars['orderby'] : ''; |
||
| 571 | |||
| 572 | View Code Duplication | switch ( $orderby ) { |
|
| 573 | case 'modified': |
||
| 574 | return 'post_modified'; |
||
| 575 | case 'date': |
||
| 576 | case '': |
||
| 577 | return 'post_date'; |
||
| 578 | default: |
||
| 579 | return false; |
||
| 580 | } |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Create a where clause that will make sure post queries |
||
| 585 | * will always return results prior to (descending sort) |
||
| 586 | * or before (ascending sort) the last post date. |
||
| 587 | * |
||
| 588 | * @global $wpdb |
||
| 589 | * @param string $where |
||
| 590 | * @param object $query |
||
| 591 | * @uses apply_filters |
||
| 592 | * @filter posts_where |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | function query_time_filter( $where, $query ) { |
||
| 596 | if ( self::got_infinity() ) { |
||
| 597 | global $wpdb; |
||
| 598 | |||
| 599 | $sort_field = self::get_query_sort_field( $query ); |
||
| 600 | if ( false == $sort_field ) |
||
| 601 | return $where; |
||
| 602 | |||
| 603 | $last_post_date = $_REQUEST['last_post_date']; |
||
| 604 | // Sanitize timestamp |
||
| 605 | if ( empty( $last_post_date ) || !preg_match( '|\d{4}\-\d{2}\-\d{2}|', $last_post_date ) ) |
||
| 606 | return $where; |
||
| 607 | |||
| 608 | $operator = 'ASC' == $_REQUEST['query_args']['order'] ? '>' : '<'; |
||
| 609 | |||
| 610 | // Construct the date query using our timestamp |
||
| 611 | $clause = $wpdb->prepare( " AND {$wpdb->posts}.{$sort_field} {$operator} %s", $last_post_date ); |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Filter Infinite Scroll's SQL date query making sure post queries |
||
| 615 | * will always return results prior to (descending sort) |
||
| 616 | * or before (ascending sort) the last post date. |
||
| 617 | * |
||
| 618 | * @module infinite-scroll |
||
| 619 | * |
||
| 620 | * @param string $clause SQL Date query. |
||
| 621 | * @param object $query Query. |
||
| 622 | * @param string $operator Query operator. |
||
| 623 | * @param string $last_post_date Last Post Date timestamp. |
||
| 624 | */ |
||
| 625 | $where .= apply_filters( 'infinite_scroll_posts_where', $clause, $query, $operator, $last_post_date ); |
||
| 626 | } |
||
| 627 | |||
| 628 | return $where; |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Let's overwrite the default post_per_page setting to always display a fixed amount. |
||
| 633 | * |
||
| 634 | * @param object $query |
||
| 635 | * @uses is_admin, self::archive_supports_infinity, self::get_settings |
||
| 636 | * @return null |
||
| 637 | */ |
||
| 638 | function posts_per_page_query( $query ) { |
||
| 639 | if ( ! is_admin() && self::archive_supports_infinity() && $query->is_main_query() ) |
||
| 640 | $query->set( 'posts_per_page', self::get_settings()->posts_per_page ); |
||
| 641 | } |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Check if the IS output should be wrapped in a div. |
||
| 645 | * Setting value can be a boolean or a string specifying the class applied to the div. |
||
| 646 | * |
||
| 647 | * @uses self::get_settings |
||
| 648 | * @return bool |
||
| 649 | */ |
||
| 650 | function has_wrapper() { |
||
| 651 | return (bool) self::get_settings()->wrapper; |
||
| 652 | } |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Returns the Ajax url |
||
| 656 | * |
||
| 657 | * @global $wp |
||
| 658 | * @uses home_url, add_query_arg, apply_filters |
||
| 659 | * @return string |
||
| 660 | */ |
||
| 661 | function ajax_url() { |
||
| 662 | $base_url = set_url_scheme( home_url( '/' ) ); |
||
| 663 | |||
| 664 | $ajaxurl = add_query_arg( array( 'infinity' => 'scrolling' ), $base_url ); |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Filter the Infinite Scroll Ajax URL. |
||
| 668 | * |
||
| 669 | * @module infinite-scroll |
||
| 670 | * |
||
| 671 | * @since 2.0.0 |
||
| 672 | * |
||
| 673 | * @param string $ajaxurl Infinite Scroll Ajax URL. |
||
| 674 | */ |
||
| 675 | return apply_filters( 'infinite_scroll_ajax_url', $ajaxurl ); |
||
| 676 | } |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Our own Ajax response, avoiding calling admin-ajax |
||
| 680 | */ |
||
| 681 | function ajax_response() { |
||
| 682 | // Only proceed if the url query has a key of "Infinity" |
||
| 683 | if ( ! self::got_infinity() ) |
||
| 684 | return false; |
||
| 685 | |||
| 686 | // This should already be defined below, but make sure. |
||
| 687 | if ( ! defined( 'DOING_AJAX' ) ) { |
||
| 688 | define( 'DOING_AJAX', true ); |
||
| 689 | } |
||
| 690 | |||
| 691 | @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
||
| 692 | send_nosniff_header(); |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Fires at the end of the Infinite Scroll Ajax response. |
||
| 696 | * |
||
| 697 | * @module infinite-scroll |
||
| 698 | * |
||
| 699 | * @since 2.0.0 |
||
| 700 | */ |
||
| 701 | do_action( 'custom_ajax_infinite_scroll' ); |
||
| 702 | die( '0' ); |
||
| 703 | } |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Alias for renamed class method. |
||
| 707 | * |
||
| 708 | * Previously, JS settings object was unnecessarily output in the document head. |
||
| 709 | * When the hook was changed, the method name no longer made sense. |
||
| 710 | */ |
||
| 711 | function action_wp_head() { |
||
| 712 | $this->action_wp_footer_settings(); |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Prints the relevant infinite scroll settings in JS. |
||
| 717 | * |
||
| 718 | * @global $wp_rewrite |
||
| 719 | * @uses self::get_settings, esc_js, esc_url_raw, self::has_wrapper, __, apply_filters, do_action, self::get_query_vars |
||
| 720 | * @action wp_footer |
||
| 721 | * @return string |
||
| 722 | */ |
||
| 723 | function action_wp_footer_settings() { |
||
| 724 | global $wp_rewrite; |
||
| 725 | global $currentday; |
||
| 726 | |||
| 727 | // Default click handle text |
||
| 728 | $click_handle_text = __( 'Older posts', 'jetpack' ); |
||
| 729 | |||
| 730 | // If a single CPT is displayed, use its plural name instead of "posts" |
||
| 731 | // Could be empty (posts) or an array of multiple post types. |
||
| 732 | // In the latter two cases cases, the default text is used, leaving the `infinite_scroll_js_settings` filter for further customization. |
||
| 733 | $post_type = self::wp_query()->get( 'post_type' ); |
||
| 734 | if ( is_string( $post_type ) && ! empty( $post_type ) ) { |
||
| 735 | $post_type = get_post_type_object( $post_type ); |
||
| 736 | |||
| 737 | if ( is_object( $post_type ) && ! is_wp_error( $post_type ) ) { |
||
| 738 | if ( isset( $post_type->labels->name ) ) { |
||
| 739 | $cpt_text = $post_type->labels->name; |
||
| 740 | } elseif ( isset( $post_type->label ) ) { |
||
| 741 | $cpt_text = $post_type->label; |
||
| 742 | } |
||
| 743 | |||
| 744 | if ( isset( $cpt_text ) ) { |
||
| 745 | $click_handle_text = sprintf( __( 'Older %s', 'jetpack' ), $cpt_text ); |
||
| 746 | unset( $cpt_text ); |
||
| 747 | } |
||
| 748 | } |
||
| 749 | } |
||
| 750 | |||
| 751 | unset( $post_type ); |
||
| 752 | |||
| 753 | // Base JS settings |
||
| 754 | $js_settings = array( |
||
| 755 | 'id' => self::get_settings()->container, |
||
| 756 | 'ajaxurl' => esc_url_raw( self::ajax_url() ), |
||
| 757 | 'type' => esc_js( self::get_settings()->type ), |
||
| 758 | 'wrapper' => self::has_wrapper(), |
||
| 759 | 'wrapper_class' => is_string( self::get_settings()->wrapper ) ? esc_js( self::get_settings()->wrapper ) : 'infinite-wrap', |
||
| 760 | 'footer' => is_string( self::get_settings()->footer ) ? esc_js( self::get_settings()->footer ) : self::get_settings()->footer, |
||
| 761 | 'click_handle' => esc_js( self::get_settings()->click_handle ), |
||
| 762 | 'text' => esc_js( $click_handle_text ), |
||
| 763 | 'totop' => esc_js( __( 'Scroll back to top', 'jetpack' ) ), |
||
| 764 | 'currentday' => $currentday, |
||
| 765 | 'order' => 'DESC', |
||
| 766 | 'scripts' => array(), |
||
| 767 | 'styles' => array(), |
||
| 768 | 'google_analytics' => false, |
||
| 769 | 'offset' => self::wp_query()->get( 'paged' ), |
||
| 770 | 'history' => array( |
||
| 771 | 'host' => preg_replace( '#^http(s)?://#i', '', untrailingslashit( get_option( 'home' ) ) ), |
||
| 772 | 'path' => self::get_request_path(), |
||
| 773 | 'use_trailing_slashes' => $wp_rewrite->use_trailing_slashes, |
||
| 774 | 'parameters' => self::get_request_parameters(), |
||
| 775 | ), |
||
| 776 | 'query_args' => self::get_query_vars(), |
||
| 777 | 'last_post_date' => self::get_last_post_date(), |
||
| 778 | ); |
||
| 779 | |||
| 780 | // Optional order param |
||
| 781 | if ( isset( $_REQUEST['order'] ) ) { |
||
| 782 | $order = strtoupper( $_REQUEST['order'] ); |
||
| 783 | |||
| 784 | if ( in_array( $order, array( 'ASC', 'DESC' ) ) ) |
||
| 785 | $js_settings['order'] = $order; |
||
| 786 | } |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Filter the Infinite Scroll JS settings outputted in the head. |
||
| 790 | * |
||
| 791 | * @module infinite-scroll |
||
| 792 | * |
||
| 793 | * @since 2.0.0 |
||
| 794 | * |
||
| 795 | * @param array $js_settings Infinite Scroll JS settings. |
||
| 796 | */ |
||
| 797 | $js_settings = apply_filters( 'infinite_scroll_js_settings', $js_settings ); |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Fires before Infinite Scroll outputs inline JavaScript in the head. |
||
| 801 | * |
||
| 802 | * @module infinite-scroll |
||
| 803 | * |
||
| 804 | * @since 2.0.0 |
||
| 805 | */ |
||
| 806 | do_action( 'infinite_scroll_wp_head' ); |
||
| 807 | |||
| 808 | ?> |
||
| 809 | <script type="text/javascript"> |
||
| 810 | //<![CDATA[ |
||
| 811 | var infiniteScroll = <?php echo json_encode( array( 'settings' => $js_settings ) ); ?>; |
||
| 812 | //]]> |
||
| 813 | </script> |
||
| 814 | <?php |
||
| 815 | } |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Build path data for current request. |
||
| 819 | * Used for Google Analytics and pushState history tracking. |
||
| 820 | * |
||
| 821 | * @global $wp_rewrite |
||
| 822 | * @global $wp |
||
| 823 | * @uses user_trailingslashit, sanitize_text_field, add_query_arg |
||
| 824 | * @return string|bool |
||
| 825 | */ |
||
| 826 | private function get_request_path() { |
||
| 827 | global $wp_rewrite; |
||
| 828 | |||
| 829 | if ( $wp_rewrite->using_permalinks() ) { |
||
| 830 | global $wp; |
||
| 831 | |||
| 832 | // If called too early, bail |
||
| 833 | if ( ! isset( $wp->request ) ) |
||
| 834 | return false; |
||
| 835 | |||
| 836 | // Determine path for paginated version of current request |
||
| 837 | if ( false != preg_match( '#' . $wp_rewrite->pagination_base . '/\d+/?$#i', $wp->request ) ) |
||
| 838 | $path = preg_replace( '#' . $wp_rewrite->pagination_base . '/\d+$#i', $wp_rewrite->pagination_base . '/%d', $wp->request ); |
||
| 839 | else |
||
| 840 | $path = $wp->request . '/' . $wp_rewrite->pagination_base . '/%d'; |
||
| 841 | |||
| 842 | // Slashes everywhere we need them |
||
| 843 | if ( 0 !== strpos( $path, '/' ) ) |
||
| 844 | $path = '/' . $path; |
||
| 845 | |||
| 846 | $path = user_trailingslashit( $path ); |
||
| 847 | } else { |
||
| 848 | // Clean up raw $_REQUEST input |
||
| 849 | $path = array_map( 'sanitize_text_field', $_REQUEST ); |
||
| 850 | $path = array_filter( $path ); |
||
| 851 | |||
| 852 | $path['paged'] = '%d'; |
||
| 853 | |||
| 854 | $path = add_query_arg( $path, '/' ); |
||
| 855 | } |
||
| 856 | |||
| 857 | return empty( $path ) ? false : $path; |
||
| 858 | } |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Return query string for current request, prefixed with '?'. |
||
| 862 | * |
||
| 863 | * @return string |
||
| 864 | */ |
||
| 865 | private function get_request_parameters() { |
||
| 866 | $uri = $_SERVER[ 'REQUEST_URI' ]; |
||
| 867 | $uri = preg_replace( '/^[^?]*(\?.*$)/', '$1', $uri, 1, $count ); |
||
| 868 | if ( $count != 1 ) |
||
| 869 | return ''; |
||
| 870 | return $uri; |
||
| 871 | } |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Provide IS with a list of the scripts and stylesheets already present on the page. |
||
| 875 | * Since posts may contain require additional assets that haven't been loaded, this data will be used to track the additional assets. |
||
| 876 | * |
||
| 877 | * @global $wp_scripts, $wp_styles |
||
| 878 | * @action wp_footer |
||
| 879 | * @return string |
||
| 880 | */ |
||
| 881 | function action_wp_footer() { |
||
| 882 | global $wp_scripts, $wp_styles; |
||
| 883 | |||
| 884 | $scripts = is_a( $wp_scripts, 'WP_Scripts' ) ? $wp_scripts->done : array(); |
||
| 885 | /** |
||
| 886 | * Filter the list of scripts already present on the page. |
||
| 887 | * |
||
| 888 | * @module infinite-scroll |
||
| 889 | * |
||
| 890 | * @since 2.1.2 |
||
| 891 | * |
||
| 892 | * @param array $scripts Array of scripts present on the page. |
||
| 893 | */ |
||
| 894 | $scripts = apply_filters( 'infinite_scroll_existing_scripts', $scripts ); |
||
| 895 | |||
| 896 | $styles = is_a( $wp_styles, 'WP_Styles' ) ? $wp_styles->done : array(); |
||
| 897 | /** |
||
| 898 | * Filter the list of styles already present on the page. |
||
| 899 | * |
||
| 900 | * @module infinite-scroll |
||
| 901 | * |
||
| 902 | * @since 2.1.2 |
||
| 903 | * |
||
| 904 | * @param array $styles Array of styles present on the page. |
||
| 905 | */ |
||
| 906 | $styles = apply_filters( 'infinite_scroll_existing_stylesheets', $styles ); |
||
| 907 | |||
| 908 | ?><script type="text/javascript"> |
||
| 909 | jQuery.extend( infiniteScroll.settings.scripts, <?php echo json_encode( $scripts ); ?> ); |
||
| 910 | jQuery.extend( infiniteScroll.settings.styles, <?php echo json_encode( $styles ); ?> ); |
||
| 911 | </script><?php |
||
| 912 | } |
||
| 913 | |||
| 914 | /** |
||
| 915 | * Identify additional scripts required by the latest set of IS posts and provide the necessary data to the IS response handler. |
||
| 916 | * |
||
| 917 | * @global $wp_scripts |
||
| 918 | * @uses sanitize_text_field, add_query_arg |
||
| 919 | * @filter infinite_scroll_results |
||
| 920 | * @return array |
||
| 921 | */ |
||
| 922 | function filter_infinite_scroll_results( $results, $query_args, $wp_query ) { |
||
| 923 | // Don't bother unless there are posts to display |
||
| 924 | if ( 'success' != $results['type'] ) |
||
| 925 | return $results; |
||
| 926 | |||
| 927 | // Parse and sanitize the script handles already output |
||
| 928 | $initial_scripts = isset( $_REQUEST['scripts'] ) && is_array( $_REQUEST['scripts'] ) ? array_map( 'sanitize_text_field', $_REQUEST['scripts'] ) : false; |
||
| 929 | |||
| 930 | if ( is_array( $initial_scripts ) ) { |
||
| 931 | global $wp_scripts; |
||
| 932 | |||
| 933 | // Identify new scripts needed by the latest set of IS posts |
||
| 934 | $new_scripts = array_diff( $wp_scripts->done, $initial_scripts ); |
||
| 935 | |||
| 936 | // If new scripts are needed, extract relevant data from $wp_scripts |
||
| 937 | if ( ! empty( $new_scripts ) ) { |
||
| 938 | $results['scripts'] = array(); |
||
| 939 | |||
| 940 | foreach ( $new_scripts as $handle ) { |
||
| 941 | // Abort if somehow the handle doesn't correspond to a registered script |
||
| 942 | if ( ! isset( $wp_scripts->registered[ $handle ] ) ) |
||
| 943 | continue; |
||
| 944 | |||
| 945 | // Provide basic script data |
||
| 946 | $script_data = array( |
||
| 947 | 'handle' => $handle, |
||
| 948 | 'footer' => ( is_array( $wp_scripts->in_footer ) && in_array( $handle, $wp_scripts->in_footer ) ), |
||
| 949 | 'extra_data' => $wp_scripts->print_extra_script( $handle, false ) |
||
| 950 | ); |
||
| 951 | |||
| 952 | // Base source |
||
| 953 | $src = $wp_scripts->registered[ $handle ]->src; |
||
| 954 | |||
| 955 | // Take base_url into account |
||
| 956 | if ( strpos( $src, 'http' ) !== 0 ) |
||
| 957 | $src = $wp_scripts->base_url . $src; |
||
| 958 | |||
| 959 | // Version and additional arguments |
||
| 960 | View Code Duplication | if ( null === $wp_scripts->registered[ $handle ]->ver ) |
|
| 961 | $ver = ''; |
||
| 962 | else |
||
| 963 | $ver = $wp_scripts->registered[ $handle ]->ver ? $wp_scripts->registered[ $handle ]->ver : $wp_scripts->default_version; |
||
| 964 | |||
| 965 | View Code Duplication | if ( isset( $wp_scripts->args[ $handle ] ) ) |
|
| 966 | $ver = $ver ? $ver . '&' . $wp_scripts->args[$handle] : $wp_scripts->args[$handle]; |
||
| 967 | |||
| 968 | // Full script source with version info |
||
| 969 | $script_data['src'] = add_query_arg( 'ver', $ver, $src ); |
||
| 970 | |||
| 971 | // Add script to data that will be returned to IS JS |
||
| 972 | array_push( $results['scripts'], $script_data ); |
||
| 973 | } |
||
| 974 | } |
||
| 975 | } |
||
| 976 | |||
| 977 | // Expose additional script data to filters, but only include in final `$results` array if needed. |
||
| 978 | if ( ! isset( $results['scripts'] ) ) |
||
| 979 | $results['scripts'] = array(); |
||
| 980 | |||
| 981 | /** |
||
| 982 | * Filter the additional scripts required by the latest set of IS posts. |
||
| 983 | * |
||
| 984 | * @module infinite-scroll |
||
| 985 | * |
||
| 986 | * @since 2.1.2 |
||
| 987 | * |
||
| 988 | * @param array $results['scripts'] Additional scripts required by the latest set of IS posts. |
||
| 989 | * @param array|bool $initial_scripts Set of scripts loaded on each page. |
||
| 990 | * @param array $results Array of Infinite Scroll results. |
||
| 991 | * @param array $query_args Array of Query arguments. |
||
| 992 | * @param WP_Query $wp_query WP Query. |
||
| 993 | */ |
||
| 994 | $results['scripts'] = apply_filters( |
||
| 995 | 'infinite_scroll_additional_scripts', |
||
| 996 | $results['scripts'], |
||
| 997 | $initial_scripts, |
||
| 998 | $results, |
||
| 999 | $query_args, |
||
| 1000 | $wp_query |
||
| 1001 | ); |
||
| 1002 | |||
| 1003 | if ( empty( $results['scripts'] ) ) |
||
| 1004 | unset( $results['scripts' ] ); |
||
| 1005 | |||
| 1006 | // Parse and sanitize the style handles already output |
||
| 1007 | $initial_styles = isset( $_REQUEST['styles'] ) && is_array( $_REQUEST['styles'] ) ? array_map( 'sanitize_text_field', $_REQUEST['styles'] ) : false; |
||
| 1008 | |||
| 1009 | if ( is_array( $initial_styles ) ) { |
||
| 1010 | global $wp_styles; |
||
| 1011 | |||
| 1012 | // Identify new styles needed by the latest set of IS posts |
||
| 1013 | $new_styles = array_diff( $wp_styles->done, $initial_styles ); |
||
| 1014 | |||
| 1015 | // If new styles are needed, extract relevant data from $wp_styles |
||
| 1016 | if ( ! empty( $new_styles ) ) { |
||
| 1017 | $results['styles'] = array(); |
||
| 1018 | |||
| 1019 | foreach ( $new_styles as $handle ) { |
||
| 1020 | // Abort if somehow the handle doesn't correspond to a registered stylesheet |
||
| 1021 | if ( ! isset( $wp_styles->registered[ $handle ] ) ) |
||
| 1022 | continue; |
||
| 1023 | |||
| 1024 | // Provide basic style data |
||
| 1025 | $style_data = array( |
||
| 1026 | 'handle' => $handle, |
||
| 1027 | 'media' => 'all' |
||
| 1028 | ); |
||
| 1029 | |||
| 1030 | // Base source |
||
| 1031 | $src = $wp_styles->registered[ $handle ]->src; |
||
| 1032 | |||
| 1033 | // Take base_url into account |
||
| 1034 | if ( strpos( $src, 'http' ) !== 0 ) |
||
| 1035 | $src = $wp_styles->base_url . $src; |
||
| 1036 | |||
| 1037 | // Version and additional arguments |
||
| 1038 | View Code Duplication | if ( null === $wp_styles->registered[ $handle ]->ver ) |
|
| 1039 | $ver = ''; |
||
| 1040 | else |
||
| 1041 | $ver = $wp_styles->registered[ $handle ]->ver ? $wp_styles->registered[ $handle ]->ver : $wp_styles->default_version; |
||
| 1042 | |||
| 1043 | View Code Duplication | if ( isset($wp_styles->args[ $handle ] ) ) |
|
| 1044 | $ver = $ver ? $ver . '&' . $wp_styles->args[$handle] : $wp_styles->args[$handle]; |
||
| 1045 | |||
| 1046 | // Full stylesheet source with version info |
||
| 1047 | $style_data['src'] = add_query_arg( 'ver', $ver, $src ); |
||
| 1048 | |||
| 1049 | // Parse stylesheet's conditional comments if present, converting to logic executable in JS |
||
| 1050 | if ( isset( $wp_styles->registered[ $handle ]->extra['conditional'] ) && $wp_styles->registered[ $handle ]->extra['conditional'] ) { |
||
| 1051 | // First, convert conditional comment operators to standard logical operators. %ver is replaced in JS with the IE version |
||
| 1052 | $style_data['conditional'] = str_replace( array( |
||
| 1053 | 'lte', |
||
| 1054 | 'lt', |
||
| 1055 | 'gte', |
||
| 1056 | 'gt' |
||
| 1057 | ), array( |
||
| 1058 | '%ver <=', |
||
| 1059 | '%ver <', |
||
| 1060 | '%ver >=', |
||
| 1061 | '%ver >', |
||
| 1062 | ), $wp_styles->registered[ $handle ]->extra['conditional'] ); |
||
| 1063 | |||
| 1064 | // Next, replace any !IE checks. These shouldn't be present since WP's conditional stylesheet implementation doesn't support them, but someone could be _doing_it_wrong(). |
||
| 1065 | $style_data['conditional'] = preg_replace( '#!\s*IE(\s*\d+){0}#i', '1==2', $style_data['conditional'] ); |
||
| 1066 | |||
| 1067 | // Lastly, remove the IE strings |
||
| 1068 | $style_data['conditional'] = str_replace( 'IE', '', $style_data['conditional'] ); |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | // Parse requested media context for stylesheet |
||
| 1072 | View Code Duplication | if ( isset( $wp_styles->registered[ $handle ]->args ) ) |
|
| 1073 | $style_data['media'] = esc_attr( $wp_styles->registered[ $handle ]->args ); |
||
| 1074 | |||
| 1075 | // Add stylesheet to data that will be returned to IS JS |
||
| 1076 | array_push( $results['styles'], $style_data ); |
||
| 1077 | } |
||
| 1078 | } |
||
| 1079 | } |
||
| 1080 | |||
| 1081 | // Expose additional stylesheet data to filters, but only include in final `$results` array if needed. |
||
| 1082 | if ( ! isset( $results['styles'] ) ) |
||
| 1083 | $results['styles'] = array(); |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * Filter the additional styles required by the latest set of IS posts. |
||
| 1087 | * |
||
| 1088 | * @module infinite-scroll |
||
| 1089 | * |
||
| 1090 | * @since 2.1.2 |
||
| 1091 | * |
||
| 1092 | * @param array $results['styles'] Additional styles required by the latest set of IS posts. |
||
| 1093 | * @param array|bool $initial_styles Set of styles loaded on each page. |
||
| 1094 | * @param array $results Array of Infinite Scroll results. |
||
| 1095 | * @param array $query_args Array of Query arguments. |
||
| 1096 | * @param WP_Query $wp_query WP Query. |
||
| 1097 | */ |
||
| 1098 | $results['styles'] = apply_filters( |
||
| 1099 | 'infinite_scroll_additional_stylesheets', |
||
| 1100 | $results['styles'], |
||
| 1101 | $initial_styles, |
||
| 1102 | $results, |
||
| 1103 | $query_args, |
||
| 1104 | $wp_query |
||
| 1105 | ); |
||
| 1106 | |||
| 1107 | if ( empty( $results['styles'] ) ) |
||
| 1108 | unset( $results['styles' ] ); |
||
| 1109 | |||
| 1110 | // Lastly, return the IS results array |
||
| 1111 | return $results; |
||
| 1112 | } |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Runs the query and returns the results via JSON. |
||
| 1116 | * Triggered by an AJAX request. |
||
| 1117 | * |
||
| 1118 | * @global $wp_query |
||
| 1119 | * @global $wp_the_query |
||
| 1120 | * @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 |
||
| 1121 | * @return string or null |
||
| 1122 | */ |
||
| 1123 | function query() { |
||
| 1124 | global $wp_customize; |
||
| 1125 | if ( ! isset( $_REQUEST['page'] ) || ! current_theme_supports( 'infinite-scroll' ) ) |
||
| 1126 | die; |
||
|
0 ignored issues
–
show
|
|||
| 1127 | |||
| 1128 | $page = (int) $_REQUEST['page']; |
||
| 1129 | |||
| 1130 | // Sanitize and set $previousday. Expected format: dd.mm.yy |
||
| 1131 | if ( preg_match( '/^\d{2}\.\d{2}\.\d{2}$/', $_REQUEST['currentday'] ) ) { |
||
| 1132 | global $previousday; |
||
| 1133 | $previousday = $_REQUEST['currentday']; |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | $sticky = get_option( 'sticky_posts' ); |
||
| 1137 | $post__not_in = self::wp_query()->get( 'post__not_in' ); |
||
| 1138 | |||
| 1139 | //we have to take post__not_in args into consideration here not only sticky posts |
||
| 1140 | if ( true === isset( $_REQUEST['query_args']['post__not_in'] ) ) { |
||
| 1141 | $post__not_in = array_merge( $post__not_in, array_map( 'intval', (array) $_REQUEST['query_args']['post__not_in'] ) ); |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | if ( ! empty( $post__not_in ) ) |
||
| 1145 | $sticky = array_unique( array_merge( $sticky, $post__not_in ) ); |
||
| 1146 | |||
| 1147 | $post_status = array( 'publish' ); |
||
| 1148 | if ( current_user_can( 'read_private_posts' ) ) |
||
| 1149 | array_push( $post_status, 'private' ); |
||
| 1150 | |||
| 1151 | $order = in_array( $_REQUEST['order'], array( 'ASC', 'DESC' ) ) ? $_REQUEST['order'] : 'DESC'; |
||
| 1152 | |||
| 1153 | $query_args = array_merge( self::wp_query()->query_vars, array( |
||
| 1154 | 'paged' => $page, |
||
| 1155 | 'post_status' => $post_status, |
||
| 1156 | 'posts_per_page' => self::get_settings()->posts_per_page, |
||
| 1157 | 'post__not_in' => ( array ) $sticky, |
||
| 1158 | 'order' => $order |
||
| 1159 | ) ); |
||
| 1160 | |||
| 1161 | // 4.0 ?s= compatibility, see https://core.trac.wordpress.org/ticket/11330#comment:50 |
||
| 1162 | if ( empty( $query_args['s'] ) && ! isset( self::wp_query()->query['s'] ) ) { |
||
| 1163 | unset( $query_args['s'] ); |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | // By default, don't query for a specific page of a paged post object. |
||
| 1167 | // This argument can come from merging self::wp_query() into $query_args above. |
||
| 1168 | // Since IS is only used on archives, we should always display the first page of any paged content. |
||
| 1169 | unset( $query_args['page'] ); |
||
| 1170 | |||
| 1171 | /** |
||
| 1172 | * Filter the array of main query arguments. |
||
| 1173 | * |
||
| 1174 | * @module infinite-scroll |
||
| 1175 | * |
||
| 1176 | * @since 2.0.1 |
||
| 1177 | * |
||
| 1178 | * @param array $query_args Array of Query arguments. |
||
| 1179 | */ |
||
| 1180 | $query_args = apply_filters( 'infinite_scroll_query_args', $query_args ); |
||
| 1181 | |||
| 1182 | // Add query filter that checks for posts below the date |
||
| 1183 | add_filter( 'posts_where', array( $this, 'query_time_filter' ), 10, 2 ); |
||
| 1184 | |||
| 1185 | $GLOBALS['wp_the_query'] = $GLOBALS['wp_query'] = new WP_Query( $query_args ); |
||
| 1186 | |||
| 1187 | remove_filter( 'posts_where', array( $this, 'query_time_filter' ), 10, 2 ); |
||
| 1188 | |||
| 1189 | $results = array(); |
||
| 1190 | |||
| 1191 | if ( have_posts() ) { |
||
| 1192 | // Fire wp_head to ensure that all necessary scripts are enqueued. Output isn't used, but scripts are extracted in self::action_wp_footer. |
||
| 1193 | ob_start(); |
||
| 1194 | wp_head(); |
||
| 1195 | while ( ob_get_length() ) { |
||
| 1196 | ob_end_clean(); |
||
| 1197 | } |
||
| 1198 | |||
| 1199 | $results['type'] = 'success'; |
||
| 1200 | |||
| 1201 | // First, try theme's specified rendering handler, either specified via `add_theme_support` or by hooking to this action directly. |
||
| 1202 | ob_start(); |
||
| 1203 | /** |
||
| 1204 | * Fires when rendering Infinite Scroll posts. |
||
| 1205 | * |
||
| 1206 | * @module infinite-scroll |
||
| 1207 | * |
||
| 1208 | * @since 2.0.0 |
||
| 1209 | */ |
||
| 1210 | do_action( 'infinite_scroll_render' ); |
||
| 1211 | $results['html'] = ob_get_clean(); |
||
| 1212 | |||
| 1213 | // Fall back if a theme doesn't specify a rendering function. Because themes may hook additional functions to the `infinite_scroll_render` action, `has_action()` is ineffective here. |
||
| 1214 | if ( empty( $results['html'] ) ) { |
||
| 1215 | add_action( 'infinite_scroll_render', array( $this, 'render' ) ); |
||
| 1216 | rewind_posts(); |
||
| 1217 | |||
| 1218 | ob_start(); |
||
| 1219 | /** This action is already documented in modules/infinite-scroll/infinity.php */ |
||
| 1220 | do_action( 'infinite_scroll_render' ); |
||
| 1221 | $results['html'] = ob_get_clean(); |
||
| 1222 | } |
||
| 1223 | |||
| 1224 | // If primary and fallback rendering methods fail, prevent further IS rendering attempts. Otherwise, wrap the output if requested. |
||
| 1225 | if ( empty( $results['html'] ) ) { |
||
| 1226 | unset( $results['html'] ); |
||
| 1227 | /** |
||
| 1228 | * Fires when Infinite Scoll doesn't render any posts. |
||
| 1229 | * |
||
| 1230 | * @module infinite-scroll |
||
| 1231 | * |
||
| 1232 | * @since 2.0.0 |
||
| 1233 | */ |
||
| 1234 | do_action( 'infinite_scroll_empty' ); |
||
| 1235 | $results['type'] = 'empty'; |
||
| 1236 | } elseif ( $this->has_wrapper() ) { |
||
| 1237 | $wrapper_classes = is_string( self::get_settings()->wrapper ) ? self::get_settings()->wrapper : 'infinite-wrap'; |
||
| 1238 | $wrapper_classes .= ' infinite-view-' . $page; |
||
| 1239 | $wrapper_classes = trim( $wrapper_classes ); |
||
| 1240 | |||
| 1241 | $results['html'] = '<div class="' . esc_attr( $wrapper_classes ) . '" id="infinite-view-' . $page . '" data-page-num="' . $page . '">' . $results['html'] . '</div>'; |
||
| 1242 | } |
||
| 1243 | |||
| 1244 | // Fire wp_footer to ensure that all necessary scripts are enqueued. Output isn't used, but scripts are extracted in self::action_wp_footer. |
||
| 1245 | ob_start(); |
||
| 1246 | wp_footer(); |
||
| 1247 | while ( ob_get_length() ) { |
||
| 1248 | ob_end_clean(); |
||
| 1249 | } |
||
| 1250 | |||
| 1251 | if ( 'success' == $results['type'] ) { |
||
| 1252 | global $currentday; |
||
| 1253 | $results['lastbatch'] = self::is_last_batch(); |
||
| 1254 | $results['currentday'] = $currentday; |
||
| 1255 | } |
||
| 1256 | |||
| 1257 | // Loop through posts to capture sharing data for new posts loaded via Infinite Scroll |
||
| 1258 | if ( 'success' == $results['type'] && function_exists( 'sharing_register_post_for_share_counts' ) ) { |
||
| 1259 | global $jetpack_sharing_counts; |
||
| 1260 | |||
| 1261 | while( have_posts() ) { |
||
| 1262 | the_post(); |
||
| 1263 | |||
| 1264 | sharing_register_post_for_share_counts( get_the_ID() ); |
||
| 1265 | } |
||
| 1266 | |||
| 1267 | $results['postflair'] = array_flip( $jetpack_sharing_counts ); |
||
| 1268 | } |
||
| 1269 | } else { |
||
| 1270 | /** This action is already documented in modules/infinite-scroll/infinity.php */ |
||
| 1271 | do_action( 'infinite_scroll_empty' ); |
||
| 1272 | $results['type'] = 'empty'; |
||
| 1273 | } |
||
| 1274 | |||
| 1275 | if ( is_customize_preview() ) { |
||
| 1276 | $wp_customize->remove_preview_signature(); |
||
| 1277 | } |
||
| 1278 | |||
| 1279 | wp_send_json( |
||
| 1280 | /** |
||
| 1281 | * Filter the Infinite Scroll results. |
||
| 1282 | * |
||
| 1283 | * @module infinite-scroll |
||
| 1284 | * |
||
| 1285 | * @since 2.0.0 |
||
| 1286 | * |
||
| 1287 | * @param array $results Array of Infinite Scroll results. |
||
| 1288 | * @param array $query_args Array of main query arguments. |
||
| 1289 | * @param WP_Query $wp_query WP Query. |
||
| 1290 | */ |
||
| 1291 | apply_filters( 'infinite_scroll_results', $results, $query_args, self::wp_query() ) |
||
| 1292 | ); |
||
| 1293 | } |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Update the $allowed_vars array with the standard WP public and private |
||
| 1297 | * query vars, as well as taxonomy vars |
||
| 1298 | * |
||
| 1299 | * @global $wp |
||
| 1300 | * @param array $allowed_vars |
||
| 1301 | * @filter infinite_scroll_allowed_vars |
||
| 1302 | * @return array |
||
| 1303 | */ |
||
| 1304 | function allowed_query_vars( $allowed_vars ) { |
||
| 1305 | global $wp; |
||
| 1306 | |||
| 1307 | $allowed_vars += $wp->public_query_vars; |
||
| 1308 | $allowed_vars += $wp->private_query_vars; |
||
| 1309 | $allowed_vars += $this->get_taxonomy_vars(); |
||
| 1310 | |||
| 1311 | foreach ( array_keys( $allowed_vars, 'paged' ) as $key ) { |
||
| 1312 | unset( $allowed_vars[ $key ] ); |
||
| 1313 | } |
||
| 1314 | |||
| 1315 | return array_unique( $allowed_vars ); |
||
| 1316 | } |
||
| 1317 | |||
| 1318 | /** |
||
| 1319 | * Returns an array of stock and custom taxonomy query vars |
||
| 1320 | * |
||
| 1321 | * @global $wp_taxonomies |
||
| 1322 | * @return array |
||
| 1323 | */ |
||
| 1324 | function get_taxonomy_vars() { |
||
| 1325 | global $wp_taxonomies; |
||
| 1326 | |||
| 1327 | $taxonomy_vars = array(); |
||
| 1328 | foreach ( $wp_taxonomies as $taxonomy => $t ) { |
||
| 1329 | if ( $t->query_var ) |
||
| 1330 | $taxonomy_vars[] = $t->query_var; |
||
| 1331 | } |
||
| 1332 | |||
| 1333 | // still needed? |
||
| 1334 | $taxonomy_vars[] = 'tag_id'; |
||
| 1335 | |||
| 1336 | return $taxonomy_vars; |
||
| 1337 | } |
||
| 1338 | |||
| 1339 | /** |
||
| 1340 | * Update the $query_args array with the parameters provided via AJAX/GET. |
||
| 1341 | * |
||
| 1342 | * @param array $query_args |
||
| 1343 | * @filter infinite_scroll_query_args |
||
| 1344 | * @return array |
||
| 1345 | */ |
||
| 1346 | function inject_query_args( $query_args ) { |
||
| 1347 | /** |
||
| 1348 | * Filter the array of allowed Infinite Scroll query arguments. |
||
| 1349 | * |
||
| 1350 | * @module infinite-scroll |
||
| 1351 | * |
||
| 1352 | * @since 2.6.0 |
||
| 1353 | * |
||
| 1354 | * @param array $args Array of allowed Infinite Scroll query arguments. |
||
| 1355 | * @param array $query_args Array of query arguments. |
||
| 1356 | */ |
||
| 1357 | $allowed_vars = apply_filters( 'infinite_scroll_allowed_vars', array(), $query_args ); |
||
| 1358 | |||
| 1359 | $query_args = array_merge( $query_args, array( |
||
| 1360 | 'suppress_filters' => false, |
||
| 1361 | ) ); |
||
| 1362 | |||
| 1363 | if ( is_array( $_REQUEST[ 'query_args' ] ) ) { |
||
| 1364 | foreach ( $_REQUEST[ 'query_args' ] as $var => $value ) { |
||
| 1365 | if ( in_array( $var, $allowed_vars ) && ! empty( $value ) ) |
||
| 1366 | $query_args[ $var ] = $value; |
||
| 1367 | } |
||
| 1368 | } |
||
| 1369 | |||
| 1370 | return $query_args; |
||
| 1371 | } |
||
| 1372 | |||
| 1373 | /** |
||
| 1374 | * Rendering fallback used when themes don't specify their own handler. |
||
| 1375 | * |
||
| 1376 | * @uses have_posts, the_post, get_template_part, get_post_format |
||
| 1377 | * @action infinite_scroll_render |
||
| 1378 | * @return string |
||
| 1379 | */ |
||
| 1380 | function render() { |
||
| 1381 | while ( have_posts() ) { |
||
| 1382 | the_post(); |
||
| 1383 | |||
| 1384 | get_template_part( 'content', get_post_format() ); |
||
| 1385 | } |
||
| 1386 | } |
||
| 1387 | |||
| 1388 | /** |
||
| 1389 | * Allow plugins to filter what archives Infinite Scroll supports |
||
| 1390 | * |
||
| 1391 | * @uses current_theme_supports, is_home, is_archive, apply_filters, self::get_settings |
||
| 1392 | * @return bool |
||
| 1393 | */ |
||
| 1394 | public static function archive_supports_infinity() { |
||
| 1395 | $supported = current_theme_supports( 'infinite-scroll' ) && ( is_home() || is_archive() || is_search() ); |
||
| 1396 | |||
| 1397 | // Disable when previewing a non-active theme in the customizer |
||
| 1398 | if ( is_customize_preview() && ! $GLOBALS['wp_customize']->is_theme_active() ) { |
||
| 1399 | return false; |
||
| 1400 | } |
||
| 1401 | |||
| 1402 | /** |
||
| 1403 | * Allow plugins to filter what archives Infinite Scroll supports. |
||
| 1404 | * |
||
| 1405 | * @module infinite-scroll |
||
| 1406 | * |
||
| 1407 | * @since 2.0.0 |
||
| 1408 | * |
||
| 1409 | * @param bool $supported Does the Archive page support Infinite Scroll. |
||
| 1410 | * @param object self::get_settings() IS settings provided by theme. |
||
| 1411 | */ |
||
| 1412 | return (bool) apply_filters( 'infinite_scroll_archive_supported', $supported, self::get_settings() ); |
||
| 1413 | } |
||
| 1414 | |||
| 1415 | /** |
||
| 1416 | * The Infinite Blog Footer |
||
| 1417 | * |
||
| 1418 | * @uses self::get_settings, self::archive_supports_infinity, self::default_footer |
||
| 1419 | * @return string or null |
||
| 1420 | */ |
||
| 1421 | function footer() { |
||
| 1422 | // Bail if theme requested footer not show |
||
| 1423 | if ( false == self::get_settings()->footer ) |
||
| 1424 | return; |
||
| 1425 | |||
| 1426 | // We only need the new footer for the 'scroll' type |
||
| 1427 | if ( 'scroll' != self::get_settings()->type || ! self::archive_supports_infinity() ) |
||
| 1428 | return; |
||
| 1429 | |||
| 1430 | // Display a footer, either user-specified or a default |
||
| 1431 | if ( false !== self::get_settings()->footer_callback && is_callable( self::get_settings()->footer_callback ) ) |
||
| 1432 | call_user_func( self::get_settings()->footer_callback, self::get_settings() ); |
||
| 1433 | else |
||
| 1434 | self::default_footer(); |
||
| 1435 | } |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Render default IS footer |
||
| 1439 | * |
||
| 1440 | * @uses __, wp_get_theme, get_current_theme, apply_filters, home_url, esc_attr, get_bloginfo, bloginfo |
||
| 1441 | * @return string |
||
| 1442 | */ |
||
| 1443 | private function default_footer() { |
||
| 1444 | $credits = sprintf( |
||
| 1445 | '<a href="http://wordpress.org/" target="_blank" rel="generator">%1$s</a> ', |
||
| 1446 | __( 'Proudly powered by WordPress', 'jetpack' ) |
||
| 1447 | ); |
||
| 1448 | $credits .= sprintf( |
||
| 1449 | __( 'Theme: %1$s.', 'jetpack' ), |
||
| 1450 | function_exists( 'wp_get_theme' ) ? wp_get_theme()->Name : get_current_theme() |
||
| 1451 | ); |
||
| 1452 | /** |
||
| 1453 | * Filter Infinite Scroll's credit text. |
||
| 1454 | * |
||
| 1455 | * @module infinite-scroll |
||
| 1456 | * |
||
| 1457 | * @since 2.0.0 |
||
| 1458 | * |
||
| 1459 | * @param string $credits Infinite Scroll credits. |
||
| 1460 | */ |
||
| 1461 | $credits = apply_filters( 'infinite_scroll_credit', $credits ); |
||
| 1462 | |||
| 1463 | ?> |
||
| 1464 | <div id="infinite-footer"> |
||
| 1465 | <div class="container"> |
||
| 1466 | <div class="blog-info"> |
||
| 1467 | <a id="infinity-blog-title" href="<?php echo home_url( '/' ); ?>" target="_blank" rel="home"> |
||
| 1468 | <?php bloginfo( 'name' ); ?> |
||
| 1469 | </a> |
||
| 1470 | </div> |
||
| 1471 | <div class="blog-credits"> |
||
| 1472 | <?php echo $credits; ?> |
||
| 1473 | </div> |
||
| 1474 | </div> |
||
| 1475 | </div><!-- #infinite-footer --> |
||
| 1476 | <?php |
||
| 1477 | } |
||
| 1478 | |||
| 1479 | /** |
||
| 1480 | * Ensure that IS doesn't interfere with Grunion by stripping IS query arguments from the Grunion redirect URL. |
||
| 1481 | * When arguments are present, Grunion redirects to the IS AJAX endpoint. |
||
| 1482 | * |
||
| 1483 | * @param string $url |
||
| 1484 | * @uses remove_query_arg |
||
| 1485 | * @filter grunion_contact_form_redirect_url |
||
| 1486 | * @return string |
||
| 1487 | */ |
||
| 1488 | public function filter_grunion_redirect_url( $url ) { |
||
| 1489 | // Remove IS query args, if present |
||
| 1490 | if ( false !== strpos( $url, 'infinity=scrolling' ) ) { |
||
| 1491 | $url = remove_query_arg( array( |
||
| 1492 | 'infinity', |
||
| 1493 | 'action', |
||
| 1494 | 'page', |
||
| 1495 | 'order', |
||
| 1496 | 'scripts', |
||
| 1497 | 'styles' |
||
| 1498 | ), $url ); |
||
| 1499 | } |
||
| 1500 | |||
| 1501 | return $url; |
||
| 1502 | } |
||
| 1503 | }; |
||
| 1504 | |||
| 1505 | /** |
||
| 1506 | * Initialize The_Neverending_Home_Page |
||
| 1507 | */ |
||
| 1508 | function the_neverending_home_page_init() { |
||
| 1509 | if ( ! current_theme_supports( 'infinite-scroll' ) ) |
||
| 1510 | return; |
||
| 1511 | |||
| 1512 | new The_Neverending_Home_Page; |
||
| 1513 | } |
||
| 1514 | add_action( 'init', 'the_neverending_home_page_init', 20 ); |
||
| 1515 | |||
| 1516 | /** |
||
| 1517 | * Check whether the current theme is infinite-scroll aware. |
||
| 1518 | * If so, include the files which add theme support. |
||
| 1519 | */ |
||
| 1520 | function the_neverending_home_page_theme_support() { |
||
| 1521 | $theme_name = get_stylesheet(); |
||
| 1522 | |||
| 1523 | /** |
||
| 1524 | * Filter the path to the Infinite Scroll compatibility file. |
||
| 1525 | * |
||
| 1526 | * @module infinite-scroll |
||
| 1527 | * |
||
| 1528 | * @since 2.0.0 |
||
| 1529 | * |
||
| 1530 | * @param string $str IS compatibility file path. |
||
| 1531 | * @param string $theme_name Theme name. |
||
| 1532 | */ |
||
| 1533 | $customization_file = apply_filters( 'infinite_scroll_customization_file', dirname( __FILE__ ) . "/themes/{$theme_name}.php", $theme_name ); |
||
| 1534 | |||
| 1535 | if ( is_readable( $customization_file ) ) |
||
| 1536 | require_once( $customization_file ); |
||
| 1537 | } |
||
| 1538 | add_action( 'after_setup_theme', 'the_neverending_home_page_theme_support', 5 ); |
||
| 1539 | |||
| 1540 | /** |
||
| 1541 | * Early accommodation of the Infinite Scroll AJAX request |
||
| 1542 | */ |
||
| 1543 | if ( The_Neverending_Home_Page::got_infinity() ) { |
||
| 1544 | /** |
||
| 1545 | * If we're sure this is an AJAX request (i.e. the HTTP_X_REQUESTED_WITH header says so), |
||
| 1546 | * indicate it as early as possible for actions like init |
||
| 1547 | */ |
||
| 1548 | if ( ! defined( 'DOING_AJAX' ) && |
||
| 1549 | isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && |
||
| 1550 | strtoupper( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'XMLHTTPREQUEST' |
||
| 1551 | ) { |
||
| 1552 | define( 'DOING_AJAX', true ); |
||
| 1553 | } |
||
| 1554 | |||
| 1555 | // Don't load the admin bar when doing the AJAX response. |
||
| 1556 | show_admin_bar( false ); |
||
| 1557 | } |
||
| 1558 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.