$customer->get_billing_country(), 'state' => $customer->get_billing_state(), 'postcode' => $customer->get_billing_postcode(), 'city' => $customer->get_billing_city(), ]; $location_hash = substr( md5( strtolower( implode( '', $location ) ) ), 0, 12 ); /** * Controls the location hash used in geolocation-based caching. * * @param string $location_hash The hash used for geolocation. * @param array $location The location/address data. * @param Customer $customer The current customer object. */ return apply_filters( 'storeengine/geolocation_ajax_get_location_hash', $location_hash, $location, $customer ); } /** * Prevent caching on certain pages */ public static function prevent_caching() { if ( ! is_blog_installed() ) { return; } $page_ids = array_filter( [ (int) Helper::get_settings( 'cart_page' ), (int) Helper::get_settings( 'checkout_page' ), (int) Helper::get_settings( 'dashboard_page' ), ] ); if ( is_page( $page_ids ) ) { self::nocache_headers(); } } /** * Wrapper for nocache_headers which also disables page caching. */ public static function nocache_headers() { self::set_nocache_constants(); nocache_headers(); } /** * When using geolocation via ajax, to bust cache, redirect if the location hash does not equal the querystring. * * This prevents caching of the wrong data for this request. */ public static function geolocation_ajax_redirect() { if ( Geolocation::is_geolocation_enabled() && Helper::get_settings( 'enable_caching_support' ) && ! Helper::is_checkout() && ! Helper::is_cart() && ! Helper::is_account_page() && ! wp_doing_ajax() && empty( $_POST ) // phpcs:ignore WordPress.Security.NonceVerification.Missing ) { $location_hash = self::geolocation_ajax_get_location_hash(); $current_hash = isset( $_GET['se-v'] ) ? sanitize_text_field( wp_unslash( $_GET['se-v'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( empty( $current_hash ) || $current_hash !== $location_hash ) { global $wp; $redirect_url = trailingslashit( home_url( $wp->request ) ); if ( ! empty( $_SERVER['QUERY_STRING'] ) ) { $redirect_url = add_query_arg( wp_unslash( $_SERVER['QUERY_STRING'] ), '', $redirect_url ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized } if ( ! get_option( 'permalink_structure' ) ) { $redirect_url = add_query_arg( $wp->query_string, '', $redirect_url ); } $redirect_url = remove_query_arg( [ 'se-v', 'add-to-cart' ], $redirect_url ); $redirect_url = add_query_arg( 'se-v', $location_hash, $redirect_url ); wp_safe_redirect( esc_url_raw( $redirect_url ), 307 ); exit; } } } /** * Updates the `storeengine_geo_hash` cookie, which is used to help ensure we display * the correct pricing etc. to customers, according to their billing country. * * Note that: * * A) This only sets the cookie if the default customer address is set to "GeoLocate (with * Page Caching Support)". * * B) It is hooked into the `wc_ajax_update_order_review` action, which has the benefit of * ensuring we update the cookie any time the billing country is changed. */ public static function update_geolocation_hash() { if ( Geolocation::is_geolocation_enabled() && Helper::get_settings( 'enable_caching_support' ) ) { Helper::setcookie( 'storeengine_geo_hash', static::geolocation_ajax_get_location_hash(), time() + HOUR_IN_SECONDS ); } } /** * Get transient version. * * When using transients with unpredictable names, e.g. those containing a md5 * hash in the name, we need a way to invalidate them all at once. * * When using default WP transients we're able to do this with a DB query to * delete transients manually. * * With external cache however, this isn't possible. Instead, this function is used * to append a unique string (based on time()) to each transient. When transients * are invalidated, the transient version will increment and data will be regenerated. * * Raised in issue https://github.com/woocommerce/woocommerce/issues/5777. * Adapted from ideas in http://tollmanz.com/invalidation-schemes/. * * @param string $group Name for the group of transients we need to invalidate. * @param boolean $refresh true to force a new version. * * @return string transient version based on time(), 10 digits. */ public static function get_transient_version( string $group, bool $refresh = false ): string { $transient_name = $group . '-transient-version'; $transient_value = get_transient( $transient_name ); if ( false === $transient_value || true === $refresh ) { $transient_value = (string) time(); set_transient( $transient_name, $transient_value ); } return $transient_value; } /** * Set constants to prevent caching by some plugins. * * @param mixed $return Value to return. Previously hooked into a filter. * * @return mixed */ public static function set_nocache_constants( $return = true ) { if ( ! defined( 'DONOTCACHEPAGE' ) ) { // Play nice with WP-Super-Cache. define( 'DONOTCACHEPAGE', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound } if ( ! defined( 'DONOTCACHEOBJECT' ) ) { define( 'DONOTCACHEOBJECT', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound } if ( ! defined( 'DONOTCACHEDB' ) ) { define( 'DONOTCACHEDB', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound } return $return; } /** * Notices function. */ public static function notices() { if ( ! function_exists( 'w3tc_pgcache_flush' ) || ! function_exists( 'w3_instance' ) ) { return; } /** @noinspection PhpUndefinedFunctionInspection */ $config = w3_instance( 'W3_Config' ); $enabled = $config->get_integer( 'dbcache.enabled' ); $settings = array_map( 'trim', $config->get_array( 'dbcache.reject.sql' ) ); if ( $enabled && ! in_array( '_storeengine_session_', $settings, true ) ) { ?>

database caching to work with StoreEngine you must add %1$s to the "Ignored Query Strings" option in W3 Total Cache settings.', 'storeengine' ), '_storeengine_session_', esc_url( admin_url( 'admin.php?page=w3tc_dbcache' ) ) ) ); ?>

query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name LIKE %s LIMIT %d;", '\_transient\_%' . $version, $limit ) ); // : cache ok, db call ok. // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching // If affected rows is equal to limit, there are more rows to delete. Delete in 30 secs. if ( $affected === $limit ) { wp_schedule_single_event( time() + 30, 'delete_version_transients', array( $version ) ); } } } /** * Get prefix for use with wp_cache_set. Allows all cache in a group to be invalidated at once. * * @param string $group Group of cache to get. * * @return string Prefix. */ public static function get_cache_prefix( string $group ): string { // Get cache key - uses cache key storeengine_orders_cache_prefix to invalidate when needed. $prefix = wp_cache_get( 'storeengine_' . $group . '_cache_prefix', $group ); if ( false === $prefix ) { $prefix = microtime(); wp_cache_set( 'storeengine_' . $group . '_cache_prefix', $prefix, $group ); } return 'storeengine_cache_' . $prefix . '_'; } /** * Invalidate cache group. * * @param string $group Group of cache to clear. */ public static function invalidate_cache_group( string $group ): bool { return wp_cache_set( 'storeengine_' . $group . '_cache_prefix', microtime(), $group ); } /** * Helper method to get prefixed key. * * @param string|int $key Key to prefix. * @param string $group Group of cache to get. * * @return string Prefixed key. */ public static function get_prefixed_key( $key, string $group ): string { return self::get_cache_prefix( $group ) . $key; } public static function get_query_cache_key( $group, $sql, ...$args): string { $key = md5( maybe_serialize( $args ) . $sql ); $last_changed = wp_cache_get_last_changed( $group ); return ":$key:$last_changed"; } } // End of file caching.php.