PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/class-wc-cache-helper.php

https://bitbucket.org/theshipswakecreative/psw
PHP | 150 lines | 79 code | 20 blank | 51 comment | 22 complexity | 420aa61c1df477dfa2cf939155ed2348 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * WC_Cache_Helper class.
  7. *
  8. * @class WC_Cache_Helper
  9. * @version 2.2.0
  10. * @package WooCommerce/Classes
  11. * @category Class
  12. * @author WooThemes
  13. */
  14. class WC_Cache_Helper {
  15. /**
  16. * Hook in methods
  17. */
  18. public static function init() {
  19. add_action( 'before_woocommerce_init', array( __CLASS__, 'prevent_caching' ) );
  20. add_action( 'admin_notices', array( __CLASS__, 'notices' ) );
  21. }
  22. /**
  23. * Get transient version
  24. *
  25. * When using transients with unpredictable names, e.g. those containing an md5
  26. * hash in the name, we need a way to invalidate them all at once.
  27. *
  28. * When using default WP transients we're able to do this with a DB query to
  29. * delete transients manually.
  30. *
  31. * With external cache however, this isn't possible. Instead, this function is used
  32. * to append a unique string (based on time()) to each transient. When transients
  33. * are invalidated, the transient version will increment and data will be regenerated.
  34. *
  35. * Raised in issue https://github.com/woothemes/woocommerce/issues/5777
  36. * Adapted from ideas in http://tollmanz.com/invalidation-schemes/
  37. *
  38. * @param string $group Name for the group of transients we need to invalidate
  39. * @param boolean $refresh true to force a new version
  40. * @return string transient version based on time(), 10 digits
  41. */
  42. public static function get_transient_version( $group, $refresh = false ) {
  43. $transient_name = $group . '-transient-version';
  44. $transient_value = get_transient( $transient_name );
  45. if ( false === $transient_value || true === $refresh ) {
  46. $transient_value = time();
  47. set_transient( $transient_name, $transient_value );
  48. }
  49. return $transient_value;
  50. }
  51. /**
  52. * Prevent caching on dynamic pages.
  53. *
  54. * @access public
  55. * @return void
  56. */
  57. public static function prevent_caching() {
  58. if ( false === ( $wc_page_uris = get_transient( 'woocommerce_cache_excluded_uris' ) ) ) {
  59. $wc_page_uris = array();
  60. // Exclude querystring when using page ID and permalinks
  61. if ( ( $cart_page_id = wc_get_page_id( 'cart' ) ) && $cart_page_id > 0 ) {
  62. $wc_page_uris[] = 'p=' . $cart_page_id;
  63. $page = get_post( $cart_page_id );
  64. if ( ! is_null( $page ) ) {
  65. $wc_page_uris[] = '/' . $page->post_name;
  66. }
  67. }
  68. if ( ( $checkout_page_id = wc_get_page_id( 'checkout' ) ) && $checkout_page_id > 0 ) {
  69. $wc_page_uris[] = 'p=' . $checkout_page_id;
  70. $page = get_post( $checkout_page_id );
  71. if ( ! is_null( $page ) ) {
  72. $wc_page_uris[] = '/' . $page->post_name;
  73. }
  74. }
  75. if ( ( $myaccount_page_id = wc_get_page_id( 'myaccount' ) ) && $myaccount_page_id > 0 ) {
  76. $wc_page_uris[] = 'p=' . $myaccount_page_id;
  77. $page = get_post( $myaccount_page_id );
  78. if ( ! is_null( $page ) ) {
  79. $wc_page_uris[] = '/' . $page->post_name;
  80. }
  81. }
  82. set_transient( 'woocommerce_cache_excluded_uris', $wc_page_uris );
  83. }
  84. if ( is_array( $wc_page_uris ) ) {
  85. foreach( $wc_page_uris as $uri ) {
  86. if ( strstr( $_SERVER['REQUEST_URI'], $uri ) ) {
  87. self::nocache();
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. /**
  94. * Set nocache constants and headers.
  95. *
  96. * @access private
  97. * @return void
  98. */
  99. private static function nocache() {
  100. if ( ! defined( 'DONOTCACHEPAGE' ) )
  101. define( "DONOTCACHEPAGE", "true" );
  102. if ( ! defined( 'DONOTCACHEOBJECT' ) )
  103. define( "DONOTCACHEOBJECT", "true" );
  104. if ( ! defined( 'DONOTCACHEDB' ) )
  105. define( "DONOTCACHEDB", "true" );
  106. nocache_headers();
  107. }
  108. /**
  109. * notices function.
  110. *
  111. * @access public
  112. * @return void
  113. */
  114. public static function notices() {
  115. if ( ! function_exists( 'w3tc_pgcache_flush' ) || ! function_exists( 'w3_instance' ) ) {
  116. return;
  117. }
  118. $config = w3_instance('W3_Config');
  119. $enabled = $config->get_integer( 'dbcache.enabled' );
  120. $settings = array_map( 'trim', $config->get_array( 'dbcache.reject.sql' ) );
  121. if ( $enabled && ! in_array( '_wc_session_', $settings ) ) {
  122. ?>
  123. <div class="error">
  124. <p><?php printf( __( 'In order for <strong>database caching</strong> to work with WooCommerce you must add <code>_wc_session_</code> to the "Ignored Query Strings" option in W3 Total Cache settings <a href="%s">here</a>.', 'woocommerce' ), admin_url( 'admin.php?page=w3tc_dbcache' ) ); ?></p>
  125. </div>
  126. <?php
  127. }
  128. }
  129. }
  130. WC_Cache_Helper::init();