woocommerce/includes/class-wc-cache-helper.php

114 lines
3.0 KiB
PHP
Raw Normal View History

2013-04-09 09:38:40 +00:00
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
2013-04-09 09:38:40 +00:00
/**
* WC_Cache_Helper class.
*
* @class WC_Cache_Helper
* @version 2.2.0
2013-04-09 09:38:40 +00:00
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
class WC_Cache_Helper {
/**
* Hook in methods
2013-04-09 09:38:40 +00:00
*/
public static function init() {
add_action( 'before_woocommerce_init', array( __CLASS__, 'prevent_caching' ) );
add_action( 'admin_notices', array( __CLASS__, 'notices' ) );
2013-04-09 09:38:40 +00:00
}
/**
* Prevent caching on dynamic pages.
*
* @access public
* @return void
*/
public static function prevent_caching() {
2013-04-09 09:38:40 +00:00
if ( false === ( $wc_page_uris = get_transient( 'woocommerce_cache_excluded_uris' ) ) ) {
if ( wc_get_page_id( 'cart' ) < 1 || wc_get_page_id( 'checkout' ) < 1 || wc_get_page_id( 'myaccount' ) < 1 )
2013-04-09 09:38:40 +00:00
return;
$wc_page_uris = array();
2013-05-23 07:59:09 +00:00
// Exclude querystring when using page ID
$wc_page_uris[] = 'p=' . wc_get_page_id( 'cart' );
$wc_page_uris[] = 'p=' . wc_get_page_id( 'checkout' );
$wc_page_uris[] = 'p=' . wc_get_page_id( 'myaccount' );
2013-05-23 07:59:09 +00:00
// Exclude permalinks
$cart_page = get_post( wc_get_page_id( 'cart' ) );
$checkout_page = get_post( wc_get_page_id( 'checkout' ) );
$account_page = get_post( wc_get_page_id( 'myaccount' ) );
2013-04-09 09:38:40 +00:00
2013-05-23 07:59:09 +00:00
if ( ! is_null( $cart_page ) )
$wc_page_uris[] = '/' . $cart_page->post_name;
if ( ! is_null( $checkout_page ) )
$wc_page_uris[] = '/' . $checkout_page->post_name;
if ( ! is_null( $account_page ) )
$wc_page_uris[] = '/' . $account_page->post_name;
2013-04-09 09:38:40 +00:00
set_transient( 'woocommerce_cache_excluded_uris', $wc_page_uris );
}
if ( is_array( $wc_page_uris ) ) {
2013-04-09 09:38:40 +00:00
foreach( $wc_page_uris as $uri )
if ( strstr( $_SERVER['REQUEST_URI'], $uri ) ) {
self::nocache();
2013-04-09 09:38:40 +00:00
break;
}
}
2013-04-09 09:38:40 +00:00
}
/**
* Set nocache constants and headers.
*
* @access private
* @return void
*/
private static function nocache() {
2013-04-09 09:38:40 +00:00
if ( ! defined( 'DONOTCACHEPAGE' ) )
define( "DONOTCACHEPAGE", "true" );
if ( ! defined( 'DONOTCACHEOBJECT' ) )
define( "DONOTCACHEOBJECT", "true" );
if ( ! defined( 'DONOTCACHEDB' ) )
define( "DONOTCACHEDB", "true" );
2013-04-09 09:38:40 +00:00
nocache_headers();
}
/**
* notices function.
*
* @access public
* @return void
*/
public static function notices() {
if ( ! function_exists( 'w3tc_pgcache_flush' ) || ! function_exists( 'w3_instance' ) ) {
2013-04-09 09:38:40 +00:00
return;
}
2013-04-09 09:38:40 +00:00
$config = w3_instance('W3_Config');
$enabled = $config->get_integer( 'dbcache.enabled' );
$settings = $config->get_array( 'dbcache.reject.sql' );
if ( $enabled && ! in_array( '_wc_session_', $settings ) ) {
?>
<div class="error">
<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>
</div>
<?php
}
}
}
WC_Cache_Helper::init();