PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Wordpress-Project/wp-content/plugins/foogallery/includes/class-foogallery-cache.php

https://bitbucket.org/hinzanhilmy/hinzan-sample-works
PHP | 232 lines | 122 code | 43 blank | 67 comment | 18 complexity | 3a3c4b52331f8be128cee80bd7056b26 MD5 | raw file
Possible License(s): GPL-3.0, 0BSD, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1, GPL-2.0, BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Class used to cache gallery HTML output to save requests to the database
  4. * Date: 20/03/2017
  5. */
  6. if ( ! class_exists( 'FooGallery_Cache' ) ) {
  7. class FooGallery_Cache {
  8. function __construct() {
  9. if ( is_admin() ) {
  10. //intercept the gallery save and save the html output to post meta
  11. add_action( 'foogallery_after_save_gallery', array( $this, 'cache_gallery_html_output_after_save' ), 10, 2 );
  12. //add some settings to allow the clearing and disabling of the cache
  13. add_filter( 'foogallery_admin_settings_override', array( $this, 'add_cache_settings' ) );
  14. //render the clear HTML cache button
  15. add_action( 'foogallery_admin_settings_custom_type_render_setting', array( $this, 'render_settings' ) );
  16. // Ajax call for clearing HTML cache
  17. add_action( 'wp_ajax_foogallery_clear_html_cache', array( $this, 'ajax_clear_all_caches' ) );
  18. add_action( 'foogallery_admin_new_version_detected', array( $this, 'clear_cache_on_update' ) );
  19. //clear the gallery caches when settings are updated or reset
  20. add_action( 'foogallery_settings_updated', array( $this, 'clear_all_gallery_caches' ) );
  21. add_action( 'foogallery_settings_reset', array( $this, 'clear_all_gallery_caches' ) );
  22. }
  23. add_filter( 'foogallery_load_gallery_template', array( $this, 'fetch_gallery_html_from_cache' ), 10, 3 );
  24. add_filter( 'foogallery_html_cache_disabled', array( $this, 'disable_html_cache' ), 10, 3 );
  25. }
  26. /**
  27. * Override if the gallery html cache is disabled
  28. *
  29. * @param $disabled bool
  30. * @param $gallery FooGallery
  31. * @return bool
  32. */
  33. function disable_html_cache( $disabled, $gallery ) {
  34. //check if the gallery sorting is random
  35. if ( 'rand' === $gallery->sorting ) {
  36. $disabled = true;
  37. }
  38. return $disabled;
  39. }
  40. /**
  41. * Save the HTML output of the gallery after the gallery has been saved
  42. *
  43. * @param $post_id
  44. * @param $form_post
  45. */
  46. function cache_gallery_html_output_after_save( $post_id, $form_post ) {
  47. $this->cache_gallery_html_output( $post_id );
  48. }
  49. /**
  50. * Save the HTML output of the gallery to post meta so that it can be used in future requests
  51. *
  52. * @param $foogallery_id
  53. */
  54. function cache_gallery_html_output( $foogallery_id ) {
  55. $caching_enabled = $this->is_caching_enabled();
  56. //check if caching is disabled and quit early
  57. if ( !$caching_enabled ) {
  58. return;
  59. }
  60. //we need a way to force the gallery to render it's output every time it is saved
  61. global $foogallery_force_gallery_cache;
  62. $foogallery_force_gallery_cache = true;
  63. //capture the html output
  64. ob_start();
  65. foogallery_render_gallery( $foogallery_id );
  66. $gallery_html = ob_get_contents();
  67. ob_end_clean();
  68. if ( $caching_enabled ) {
  69. //save the output to post meta for later use
  70. update_post_meta( $foogallery_id, FOOGALLERY_META_CACHE, $gallery_html );
  71. }
  72. $foogallery_force_gallery_cache = false;
  73. }
  74. function is_caching_enabled() {
  75. global $current_foogallery_arguments;
  76. //do some checks if we are using arguments
  77. if ( isset( $current_foogallery_arguments ) ) {
  78. //never cache if showing a preview
  79. if ( array_key_exists( 'preview', $current_foogallery_arguments ) &&
  80. true === $current_foogallery_arguments['preview'] ) {
  81. return false;
  82. }
  83. //never cache if we are passing in extra arguments via the shortcode
  84. $array_keys = array_keys( $current_foogallery_arguments );
  85. if ( $array_keys != array( 'id', 'gallery' ) ) {
  86. return false;
  87. }
  88. }
  89. //next, check the settings
  90. if ( 'on' === foogallery_get_setting( 'enable_html_cache' ) ) {
  91. return true;
  92. }
  93. return false;
  94. }
  95. /**
  96. * Override the template output
  97. *
  98. * @param $override
  99. * @param $gallery
  100. * @param $template_location
  101. *
  102. * @return bool
  103. */
  104. function fetch_gallery_html_from_cache( $override, $gallery, $template_location ) {
  105. global $foogallery_force_gallery_cache;
  106. if ( $foogallery_force_gallery_cache ) {
  107. return false;
  108. }
  109. //check if caching is disabled and quit early
  110. if ( !$this->is_caching_enabled() ) {
  111. return false;
  112. }
  113. //allow extensions of others disable the html cache
  114. if ( apply_filters( 'foogallery_html_cache_disabled', false, $gallery ) ) {
  115. return false;
  116. }
  117. $gallery_cache = get_post_meta( $gallery->ID, FOOGALLERY_META_CACHE, true );
  118. if ( !empty( $gallery_cache ) && is_string( $gallery_cache ) ) {
  119. //output the cached gallery html
  120. echo $gallery_cache;
  121. return true; //return that we will override
  122. } else {
  123. //we should cache the result for next time
  124. $this->cache_gallery_html_output( $gallery->ID );
  125. }
  126. return false;
  127. }
  128. /**
  129. * Add some caching settings
  130. * @param $settings
  131. *
  132. * @return array
  133. */
  134. function add_cache_settings( $settings ) {
  135. $cache_settings[] = array(
  136. 'id' => 'enable_html_cache',
  137. 'title' => __( 'Enable HTML Cache', 'foogallery' ),
  138. 'desc' => __( 'The gallery HTML that is generated can be cached. This can reduce the number of calls to the database when displaying a gallery and can increase site performance.', 'foogallery' ),
  139. 'type' => 'checkbox',
  140. 'tab' => 'general',
  141. 'section' => __( 'Cache', 'foogallery' )
  142. );
  143. $cache_settings[] = array(
  144. 'id' => 'clear_html_cache',
  145. 'title' => __( 'Clear HTML Cache', 'foogallery' ),
  146. 'desc' => __( 'If you enable the HTML cache, then you can use this button to clear the gallery HTML that has been cached for all galleries.', 'foogallery' ),
  147. 'type' => 'clear_gallery_cache_button',
  148. 'tab' => 'general',
  149. 'section' => __( 'Cache', 'foogallery' )
  150. );
  151. $new_settings = array_merge( $cache_settings, $settings['settings'] );
  152. $settings['settings'] = $new_settings;
  153. return $settings;
  154. }
  155. /**
  156. * Render any custom setting types to the settings page
  157. */
  158. function render_settings( $args ) {
  159. if ('clear_gallery_cache_button' === $args['type'] ) { ?>
  160. <div id="foogallery_clear_html_cache_container">
  161. <input type="button" data-nonce="<?php echo esc_attr( wp_create_nonce( 'foogallery_clear_html_cache' ) ); ?>" class="button-primary foogallery_clear_html_cache" value="<?php _e( 'Clear Gallery HTML Cache', 'foogallery' ); ?>">
  162. <span id="foogallery_clear_html_cache_spinner" style="position: absolute" class="spinner"></span>
  163. </div>
  164. <?php }
  165. }
  166. /**
  167. * AJAX endpoint for clearing all gallery caches
  168. */
  169. function ajax_clear_all_caches() {
  170. if ( check_admin_referer( 'foogallery_clear_html_cache' ) ) {
  171. $this->clear_all_gallery_caches();
  172. _e('The cache for all galleries has been cleared!', 'foogallery' );
  173. die();
  174. }
  175. }
  176. /**
  177. * Clears all caches for all galleries
  178. */
  179. function clear_all_gallery_caches() {
  180. delete_post_meta_by_key( FOOGALLERY_META_CACHE );
  181. }
  182. /**
  183. * Clear all caches when the plugin has been updated. This is to account for changes in the HTML when a new version is released.
  184. */
  185. function clear_cache_on_update() {
  186. $this->clear_all_gallery_caches();
  187. }
  188. }
  189. }