PageRenderTime 116ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/wp-content/plugins/jetpack/modules/infinite-scroll.php

https://gitlab.com/ibnukipa/cakra
PHP | 257 lines | 102 code | 35 blank | 120 comment | 22 complexity | e7db252c2a41e36fee2e6786cda5149d MD5 | raw file
  1. <?php
  2. /**
  3. * Module Name: Infinite Scroll
  4. * Module Description: Add support for infinite scroll to your theme.
  5. * Sort Order: 26
  6. * First Introduced: 2.0
  7. * Requires Connection: No
  8. * Auto Activate: No
  9. * Module Tags: Appearance
  10. * Additional Search Queries: scroll, infinite, infinite scroll
  11. */
  12. /**
  13. * Jetpack-specific elements of Infinite Scroll
  14. */
  15. class Jetpack_Infinite_Scroll_Extras {
  16. /**
  17. * Class variables
  18. */
  19. // Oh look, a singleton
  20. private static $__instance = null;
  21. // Option names
  22. private $option_name_google_analytics = 'infinite_scroll_google_analytics';
  23. /**
  24. * Singleton implementation
  25. *
  26. * @return object
  27. */
  28. public static function instance() {
  29. if ( ! is_a( self::$__instance, 'Jetpack_Infinite_Scroll_Extras' ) )
  30. self::$__instance = new Jetpack_Infinite_Scroll_Extras;
  31. return self::$__instance;
  32. }
  33. /**
  34. * Register actions and filters
  35. *
  36. * @uses add_action, add_filter
  37. * @return null
  38. */
  39. private function __construct() {
  40. add_action( 'jetpack_modules_loaded', array( $this, 'action_jetpack_modules_loaded' ) );
  41. add_action( 'admin_init', array( $this, 'action_admin_init' ), 11 );
  42. add_action( 'after_setup_theme', array( $this, 'action_after_setup_theme' ), 5 );
  43. add_filter( 'infinite_scroll_js_settings', array( $this, 'filter_infinite_scroll_js_settings' ) );
  44. add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ) );
  45. }
  46. /**
  47. * Enable "Configure" button on module card
  48. *
  49. * @uses Jetpack::enable_module_configurable, Jetpack::module_configuration_load
  50. * @action jetpack_modules_loaded
  51. * @return null
  52. */
  53. public function action_jetpack_modules_loaded() {
  54. Jetpack::enable_module_configurable( __FILE__ );
  55. Jetpack::module_configuration_load( __FILE__, array( $this, 'module_configuration_load' ) );
  56. }
  57. /**
  58. * Redirect configure button to Settings > Reading
  59. *
  60. * @uses wp_safe_redirect, admin_url
  61. * @return null
  62. */
  63. public function module_configuration_load() {
  64. wp_safe_redirect( admin_url( 'options-reading.php#infinite-scroll-options' ) );
  65. exit;
  66. }
  67. /**
  68. * Register Google Analytics setting
  69. *
  70. * @uses add_settings_field, __, register_setting
  71. * @action admin_init
  72. * @return null
  73. */
  74. public function action_admin_init() {
  75. add_settings_field( $this->option_name_google_analytics, '<span id="infinite-scroll-google-analytics">' . __( 'Use Google Analytics with Infinite Scroll', 'jetpack' ) . '</span>', array( $this, 'setting_google_analytics' ), 'reading' );
  76. register_setting( 'reading', $this->option_name_google_analytics, array( $this, 'sanitize_boolean_value' ) );
  77. }
  78. /**
  79. * Render Google Analytics option
  80. *
  81. * @uses checked, get_option, __
  82. * @return html
  83. */
  84. public function setting_google_analytics() {
  85. echo '<label><input name="infinite_scroll_google_analytics" type="checkbox" value="1" ' . checked( true, (bool) get_option( $this->option_name_google_analytics, false ), false ) . ' /> ' . __( 'Track each Infinite Scroll post load as a page view in Google Analytics', 'jetpack' ) . '</br><small>' . __( 'By checking the box above, each new set of posts loaded via Infinite Scroll will be recorded as a page view in Google Analytics.', 'jetpack' ) . '</small>' . '</label>';
  86. }
  87. /**
  88. * Sanitize value as a boolean
  89. *
  90. * @param mixed $value
  91. * @return bool
  92. */
  93. public function sanitize_boolean_value( $value ) {
  94. return (bool) $value;
  95. }
  96. /**
  97. * Load theme's infinite scroll annotation file, if present in the IS plugin.
  98. * The `setup_theme` action is used because the annotation files should be using `after_setup_theme` to register support for IS.
  99. *
  100. * As released in Jetpack 2.0, a child theme's parent wasn't checked for in the plugin's bundled support, hence the convoluted way the parent is checked for now.
  101. *
  102. * @uses is_admin, wp_get_theme, get_theme, get_current_theme, apply_filters
  103. * @action setup_theme
  104. * @return null
  105. */
  106. function action_after_setup_theme() {
  107. $theme = function_exists( 'wp_get_theme' ) ? wp_get_theme() : get_theme( get_current_theme() );
  108. if ( ! is_a( $theme, 'WP_Theme' ) && ! is_array( $theme ) )
  109. return;
  110. /** This filter is already documented in modules/infinite-scroll/infinity.php */
  111. $customization_file = apply_filters( 'infinite_scroll_customization_file', dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Stylesheet']}.php", $theme['Stylesheet'] );
  112. if ( is_readable( $customization_file ) ) {
  113. require_once( $customization_file );
  114. }
  115. elseif ( ! empty( $theme['Template'] ) ) {
  116. $customization_file = dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Template']}.php";
  117. if ( is_readable( $customization_file ) )
  118. require_once( $customization_file );
  119. }
  120. }
  121. /**
  122. * Modify Infinite Scroll configuration information
  123. *
  124. * @uses Jetpack::get_active_modules, is_user_logged_in, stats_get_options, Jetpack_Options::get_option, get_option, JETPACK__API_VERSION, JETPACK__VERSION
  125. * @filter infinite_scroll_js_settings
  126. * @return array
  127. */
  128. public function filter_infinite_scroll_js_settings( $settings ) {
  129. // Provide WP Stats info for tracking Infinite Scroll loads
  130. // Abort if Stats module isn't active
  131. if ( in_array( 'stats', Jetpack::get_active_modules() ) ) {
  132. // Abort if user is logged in but logged-in users shouldn't be tracked.
  133. if ( is_user_logged_in() && function_exists( 'stats_get_options' ) ) {
  134. $stats_options = stats_get_options();
  135. $track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false;
  136. if ( ! $track_loggedin_users )
  137. return $settings;
  138. }
  139. // We made it this far, so gather the data needed to track IS views
  140. $settings['stats'] = 'blog=' . Jetpack_Options::get_option( 'id' ) . '&host=' . parse_url( get_option( 'home' ), PHP_URL_HOST ) . '&v=ext&j=' . JETPACK__API_VERSION . ':' . JETPACK__VERSION;
  141. // Pagetype parameter
  142. $settings['stats'] .= '&x_pagetype=infinite';
  143. if ( 'click' == $settings['type'] )
  144. $settings['stats'] .= '-click';
  145. $settings['stats'] .= '-jetpack';
  146. }
  147. // Check if Google Analytics tracking is requested
  148. $settings['google_analytics'] = (bool) get_option( $this->option_name_google_analytics );
  149. return $settings;
  150. }
  151. /**
  152. * Always load certain scripts when IS is enabled, as they can't be loaded after `document.ready` fires, meaning they can't leverage IS's script loader.
  153. *
  154. * @global $videopress
  155. * @uses do_action()
  156. * @uses apply_filters()
  157. * @uses wp_enqueue_style()
  158. * @uses wp_enqueue_script()
  159. * @action wp_enqueue_scripts
  160. * @return null
  161. */
  162. public function action_wp_enqueue_scripts() {
  163. // Do not load scripts and styles on singular pages and static pages
  164. $load_scripts_and_styles = ! ( is_singular() || is_page() );
  165. if (
  166. /**
  167. * Allow plugins to enqueue all Infinite Scroll scripts and styles on singular pages as well.
  168. *
  169. * @module infinite-scroll
  170. *
  171. * @since 3.1.0
  172. *
  173. * @param bool $load_scripts_and_styles Should scripts and styles be loaded on singular pahes and static pages. Default to false.
  174. */
  175. ! apply_filters( 'jetpack_infinite_scroll_load_scripts_and_styles', $load_scripts_and_styles )
  176. ) {
  177. return;
  178. }
  179. // VideoPress stand-alone plugin
  180. global $videopress;
  181. if ( ! empty( $videopress ) && The_Neverending_Home_Page::archive_supports_infinity() && is_a( $videopress, 'VideoPress' ) && method_exists( $videopress, 'enqueue_scripts' ) ) {
  182. $videopress->enqueue_scripts();
  183. }
  184. // VideoPress Jetpack module
  185. if ( Jetpack::is_module_active( 'videopress' ) ) {
  186. wp_enqueue_script( 'videopress' );
  187. }
  188. // Fire the post_gallery action early so Carousel scripts are present.
  189. if ( Jetpack::is_module_active( 'carousel' ) ) {
  190. /** This filter is already documented in core/wp-includes/media.php */
  191. do_action( 'post_gallery', '', '' );
  192. }
  193. // Always enqueue Tiled Gallery scripts when both IS and Tiled Galleries are enabled
  194. if ( Jetpack::is_module_active( 'tiled-gallery' ) ) {
  195. Jetpack_Tiled_Gallery::default_scripts_and_styles();
  196. }
  197. // Core's Audio and Video Shortcodes
  198. if (
  199. /** This filter is already documented in core/wp-includes/media.php */
  200. 'mediaelement' === apply_filters( 'wp_audio_shortcode_library', 'mediaelement' )
  201. ) {
  202. wp_enqueue_style( 'wp-mediaelement' );
  203. wp_enqueue_script( 'wp-mediaelement' );
  204. }
  205. if (
  206. /** This filter is already documented in core/wp-includes/media.php */
  207. 'mediaelement' === apply_filters( 'wp_video_shortcode_library', 'mediaelement' )
  208. ) {
  209. wp_enqueue_style( 'wp-mediaelement' );
  210. wp_enqueue_script( 'wp-mediaelement' );
  211. }
  212. }
  213. }
  214. Jetpack_Infinite_Scroll_Extras::instance();
  215. /**
  216. * Load main IS file
  217. */
  218. require_once( dirname( __FILE__ ) . "/infinite-scroll/infinity.php" );
  219. /**
  220. * Remove the IS annotation loading function bundled with the IS plugin in favor of the Jetpack-specific version in Jetpack_Infinite_Scroll_Extras::action_after_setup_theme();
  221. */
  222. remove_action( 'after_setup_theme', 'the_neverending_home_page_theme_support', 5 );