Last active
April 8, 2022 06:52
-
-
Save MrSwed/effdc1d5076bc0ac11f4c60a9558f719 to your computer and use it in GitHub Desktop.
Some Object caching of repeated sql queries for Woocommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Some Object caching of repeated sql queries for Woocommerce | |
* usage: `require_once( 'ext-caches.php' );` | |
* | |
* Author: MrSwed | |
* Author URI: https://gitlab.com/MrSwed | |
* | |
* */ | |
/** | |
* @param $store | |
* | |
* @return string | |
*/ | |
function extend_cache_class( $store ) { | |
if ( class_exists( $store . "_Cached" ) ) { | |
$store = $store . "_Cached"; | |
} | |
return $store; | |
} | |
add_action( "woocommerce_loaded", function () { | |
/** | |
* WC Shipping Zone Data Store Cached. | |
*/ | |
class WC_Shipping_Zone_Data_Store_Cached extends WC_Shipping_Zone_Data_Store { | |
/** | |
* Stores meta in cache for future reads. | |
* A group must be set to to enable caching. | |
* | |
* @var string | |
*/ | |
protected $cache_group = 'shipping_zone'; | |
/** | |
* Return an ordered list of zones. | |
* | |
* @return array An array of objects containing a zone_id, zone_name, and zone_order. | |
*/ | |
public function get_zones() { | |
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . "wc_zones"; | |
$cached = wp_cache_get( $cache_key, $this->cache_group ); | |
if ( $cached === false ) { | |
$cached = parent::{__FUNCTION__}(); | |
wp_cache_set( $cache_key, $cached, $this->cache_group ); | |
} | |
return $cached; | |
} | |
/** | |
* Get a list of shipping methods for a specific zone. | |
* | |
* @param int $zone_id Zone ID. | |
* @param bool $enabled_only True to request enabled methods only. | |
* | |
* @return array Array of objects containing method_id, method_order, instance_id, is_enabled | |
*/ | |
public function get_methods( $zone_id, $enabled_only ) { | |
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . "wc_zone_methods_" . md5( sprintf( "%s+%s", $zone_id, $enabled_only ) ); | |
$cached = wp_cache_get( $cache_key, $this->cache_group ); | |
if ( $cached === false ) { | |
$cached = parent::{__FUNCTION__}( $zone_id, $enabled_only ); | |
wp_cache_set( $cache_key, $cached, $this->cache_group ); | |
} | |
return $cached; | |
} | |
/** | |
* Method to read a shipping zone. | |
* | |
* @param WC_Shipping_Zone $zone Shipping zone object. | |
*/ | |
public function read( &$zone ) { | |
$key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . "wc_zone_loaded_" . $zone->get_id(); | |
$cached = wp_cache_get( $key, $this->cache_group ); | |
if ( $cached === false ) { | |
parent::{__FUNCTION__}( $zone ); | |
wp_cache_set( $key, $zone, $this->cache_group ); | |
} else { | |
$zone->set_props( $cached->get_data() ); | |
} | |
} | |
/** | |
* Returns an array of meta for an object. | |
* | |
* @param WC_Data $object WC_Data object. | |
* | |
* @return array | |
*/ | |
public function read_meta( &$object ) { | |
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . "wc_zone_read_meta_" . $object->get_id(); | |
$cached = wp_cache_get( $cache_key, $this->cache_group ); | |
if ( $cached === false ) { | |
$cached = parent::{__FUNCTION__}( $object ); | |
wp_cache_set( $cache_key, $cached, $this->cache_group ); | |
} | |
return $cached; | |
} | |
} | |
/** | |
* WC Product Data Store CPT Cached. | |
*/ | |
class WC_Product_Data_Store_CPT_Cached extends WC_Product_Data_Store_CPT { | |
/** | |
* Stores meta in cache for future reads. | |
* A group must be set to to enable caching. | |
* | |
* @var string | |
*/ | |
protected $cache_group = 'progucts'; | |
public function query( $args ) { | |
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . "query" . serialize( $args ); | |
$cached = wp_cache_get( $cache_key, $this->cache_group ); | |
if ( $cached === false ) { | |
$cached = parent::{__FUNCTION__}( $args ); | |
wp_cache_set( $cache_key, $cached, $this->cache_group ); | |
} | |
return $cached; | |
} | |
} | |
/** | |
* WC Customer Data Store Cached | |
*/ | |
class WC_Customer_Data_Store_Cached extends WC_Customer_Data_Store { | |
/** | |
* Stores meta in cache for future reads. | |
* A group must be set to to enable caching. | |
* | |
* @var string | |
*/ | |
protected $cache_group = 'customer'; | |
/** | |
* Method to read a Customer. | |
* | |
* @param WC_Customer $customer Customer object. | |
*/ | |
public function read( &$customer ) { | |
$key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . "wc_customer_loaded_" . $customer->get_id(); | |
$cached = wp_cache_get( $key, $this->cache_group ); | |
if ( $cached === false ) { | |
parent::{__FUNCTION__}( $customer ); | |
wp_cache_set( $key, $customer, $this->cache_group ); | |
} else { | |
$customer->set_props( $cached->get_data() ); | |
foreach ( explode( ",", "billing,shipping" ) as $address_type ) { | |
foreach ( array_filter( $cached->{"get_$address_type"}() ) as $key => $value ) { | |
if ( method_exists( $customer, "set_{$address_type}_{$key}" ) ) { | |
$customer->{"set_{$address_type}_{$key}"}( $value ); | |
} | |
} | |
} | |
// needs to renew meta cache | |
$customer->get_data(); | |
} | |
} | |
/** | |
* Returns an array of meta for an object. | |
* | |
* @param WC_Data $object WC_Data object. | |
* | |
* @return array | |
*/ | |
public function read_meta( &$object ) { | |
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . "wc_customer_read_meta_" . $object->get_id(); | |
$cached = wp_cache_get( $cache_key, $this->cache_group ); | |
if ( $cached === false ) { | |
$cached = parent::{__FUNCTION__}( $object ); | |
wp_cache_set( $cache_key, $cached, $this->cache_group ); | |
} | |
return $cached; | |
} | |
} | |
add_filter( 'woocommerce_shipping-zone_data_store', "extend_cache_class" ); | |
add_filter( 'woocommerce_product_data_store', "extend_cache_class" ); | |
add_filter( 'woocommerce_customer_data_store', "extend_cache_class" ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment