add-to-cart
4 years ago
additional-information
4 years ago
advanced-search
4 years ago
archive-description
5 years ago
archive-products
4 years ago
archive-result-count
4 years ago
archive-title
4 years ago
archive-view-mode
4 years ago
breadcrumbs
4 years ago
cart-table
4 years ago
cart-totals
4 years ago
checkout-coupon-form
5 years ago
checkout-form-additional
4 years ago
checkout-form-billing
4 years ago
checkout-form-shipping
4 years ago
checkout-payment
4 years ago
checkout-review-order
4 years ago
checkout-shipping-methods
4 years ago
cross-sells
4 years ago
deal-products
4 years ago
empty-cart-message
4 years ago
filter-orderby
4 years ago
filter-products-per-page
4 years ago
filterable-product-list
4 years ago
init
4 years ago
notice
4 years ago
product-categories
5 years ago
product-category-lists
4 years ago
product-description
4 years ago
product-excerpt
4 years ago
product-image
4 years ago
product-list
4 years ago
product-meta
4 years ago
product-price
4 years ago
product-rating
4 years ago
product-review
4 years ago
product-share
4 years ago
product-sku
5 years ago
product-stock
4 years ago
product-tabs
4 years ago
product-tags
5 years ago
product-title
4 years ago
recently-viewed-products
4 years ago
related
4 years ago
return-to-shop
4 years ago
up-sells
4 years ago
view-single-product
4 years ago
lazy-cache.php
5 years ago
manifest.php
5 years ago
prod-short-code.php
5 years ago
products.php
4 years ago
shop.php
5 years ago
widget-helper.php
4 years ago
lazy-cache.php
115 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Widgets; |
| 4 | |
| 5 | use ShopEngine\Modules\Swatches\Swatches; |
| 6 | |
| 7 | defined('ABSPATH') || exit; |
| 8 | |
| 9 | trait Lazy_Cache { |
| 10 | |
| 11 | private $cache_objects; |
| 12 | |
| 13 | private function cache($query_type, $query_arg) { |
| 14 | |
| 15 | $cache_key = $this->generate_key_hash($query_type, $query_arg); |
| 16 | |
| 17 | if(isset($this->cache_objects[$cache_key])) { |
| 18 | return $this->cache_objects[$cache_key]; |
| 19 | } |
| 20 | |
| 21 | switch($query_type) { |
| 22 | default: |
| 23 | case 'wc_get_products': |
| 24 | $query_object = wc_get_products($query_arg); |
| 25 | break; |
| 26 | |
| 27 | case 'wc_get_orders-woocommerce_my_account_my_orders_query': |
| 28 | $query_object = wc_get_orders( |
| 29 | apply_filters( |
| 30 | 'woocommerce_my_account_my_orders_query', |
| 31 | $query_arg |
| 32 | ) |
| 33 | ); |
| 34 | break; |
| 35 | |
| 36 | case 'wc_get_orders': |
| 37 | $query_object = wc_get_orders($query_arg); |
| 38 | break; |
| 39 | |
| 40 | case 'get_posts': |
| 41 | $query_object = get_posts($query_arg); |
| 42 | break; |
| 43 | |
| 44 | case 'wc_get_product': |
| 45 | $query_object = wc_get_product($query_arg); |
| 46 | break; |
| 47 | |
| 48 | case 'get_post_meta': |
| 49 | $query_object = get_post_meta($query_arg[0], $query_arg[1], $query_arg[2]); |
| 50 | break; |
| 51 | |
| 52 | case 'get_all_color_terms': |
| 53 | $attribute_taxonomies = wc_get_attribute_taxonomies(); |
| 54 | $taxonomy_terms = []; |
| 55 | |
| 56 | if(!empty($attribute_taxonomies)) { |
| 57 | foreach($attribute_taxonomies as $tax) { |
| 58 | if(Swatches::PA_COLOR === $tax->attribute_type) { |
| 59 | $attribute_name = wc_attribute_taxonomy_name($tax->attribute_name); |
| 60 | if(taxonomy_exists($attribute_name)) { |
| 61 | $taxonomy_terms[$tax->attribute_name] = get_terms($attribute_name, 'orderby=name&hide_empty=0'); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $all_color_terms = $this->array_flatten($taxonomy_terms); |
| 68 | $query_object = $this->add_color_meta($all_color_terms); |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | $this->cache_objects[$cache_key] = $query_object; |
| 73 | |
| 74 | return $query_object; |
| 75 | |
| 76 | } |
| 77 | |
| 78 | public function add_color_meta($all_color_terms = []) { |
| 79 | if(is_array($all_color_terms) && !empty($all_color_terms)) { |
| 80 | foreach($all_color_terms as $term) { |
| 81 | $term_id = $term->term_id; |
| 82 | $color_mete_value = get_term_meta($term_id, Swatches::PA_COLOR, true); |
| 83 | $term->color = $color_mete_value; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return $all_color_terms; |
| 88 | } |
| 89 | |
| 90 | public function array_flatten($array) { |
| 91 | if(!is_array($array)) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | $result = []; |
| 96 | foreach($array as $key => $value) { |
| 97 | if(is_array($value)) { |
| 98 | $result = array_merge($result, $this->array_flatten($value)); |
| 99 | } else { |
| 100 | $result[$key] = $value; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return $result; |
| 105 | } |
| 106 | |
| 107 | public function get_cache_objects() { |
| 108 | return $this->cache_objects; |
| 109 | } |
| 110 | |
| 111 | private function generate_key_hash($string, $array) { |
| 112 | return $string . md5(serialize($array)); |
| 113 | } |
| 114 | } |
| 115 |