add-to-cart
7 months ago
additional-information
3 years ago
advanced-search
1 year ago
archive-description
3 years ago
archive-products
1 day ago
archive-result-count
2 years ago
archive-title
1 year ago
archive-view-mode
2 years ago
breadcrumbs
4 years ago
call-for-price
9 months ago
cart-table
3 weeks ago
cart-totals
7 months ago
checkout-coupon-form
3 weeks ago
checkout-form-additional
3 years ago
checkout-form-billing
2 years ago
checkout-form-login
3 years ago
checkout-form-shipping
4 months ago
checkout-payment
3 years ago
checkout-review-order
2 years ago
checkout-shipping-methods
2 years ago
cross-sells
7 months ago
deal-products
2 years ago
empty-cart-message
2 years ago
filter-orderby
2 years ago
filter-products-per-page
3 years ago
filterable-product-list
1 year ago
init
1 day ago
notice
2 years ago
product-categories
3 years ago
product-category-lists
1 year ago
product-description
2 years ago
product-excerpt
3 years ago
product-image
1 year ago
product-list
4 months ago
product-meta
2 years ago
product-price
3 years ago
product-rating
7 months ago
product-review
2 years ago
product-share
2 years ago
product-sku
3 years ago
product-stock
9 months ago
product-tabs
7 months ago
product-tags
3 years ago
product-title
2 years ago
qr-code
9 months ago
recently-viewed-products
11 months ago
related
7 months ago
return-to-shop
2 years ago
up-sells
7 months ago
view-single-product
3 years ago
lazy-cache.php
3 years ago
manifest.php
1 day ago
prod-short-code.php
2 years ago
products.php
3 years ago
shop.php
3 years ago
widget-helper.php
1 day ago
lazy-cache.php
124 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 | $query_object = $this->get_custom_terms(Swatches::PA_COLOR, 'color'); |
| 54 | break; |
| 55 | |
| 56 | case 'get_all_image_terms': |
| 57 | $query_object = $this->get_custom_terms(Swatches::PA_IMAGE, 'image'); |
| 58 | break; |
| 59 | |
| 60 | case 'get_all_label_terms': |
| 61 | $query_object = $this->get_custom_terms(Swatches::PA_LABEL, 'label'); |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | $this->cache_objects[$cache_key] = $query_object; |
| 66 | |
| 67 | return $query_object; |
| 68 | } |
| 69 | |
| 70 | public function get_custom_terms($option_key, $key) { |
| 71 | |
| 72 | $attribute_taxonomies = wc_get_attribute_taxonomies(); |
| 73 | $taxonomy_terms = []; |
| 74 | |
| 75 | if(!empty($attribute_taxonomies)) { |
| 76 | foreach($attribute_taxonomies as $tax) { |
| 77 | if($option_key === $tax->attribute_type) { |
| 78 | $attribute_name = wc_attribute_taxonomy_name($tax->attribute_name); |
| 79 | if(taxonomy_exists($attribute_name)) { |
| 80 | $taxonomy_terms[$tax->attribute_name] = get_terms($attribute_name, 'hide_empty=0'); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | $terms = $this->array_flatten($taxonomy_terms); |
| 87 | |
| 88 | if(is_array($terms) && !empty($terms)) { |
| 89 | foreach($terms as $term) { |
| 90 | $term_id = $term->term_id; |
| 91 | $meta_value = get_term_meta($term_id, $option_key, true); |
| 92 | $term->{$key} = $meta_value; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return $terms; |
| 97 | } |
| 98 | |
| 99 | public function array_flatten($array) { |
| 100 | if(!is_array($array)) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | $result = []; |
| 105 | foreach($array as $key => $value) { |
| 106 | if(is_array($value)) { |
| 107 | $result = array_merge($result, $this->array_flatten($value)); |
| 108 | } else { |
| 109 | $result[$key] = $value; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return $result; |
| 114 | } |
| 115 | |
| 116 | public function get_cache_objects() { |
| 117 | return $this->cache_objects; |
| 118 | } |
| 119 | |
| 120 | private function generate_key_hash($string, $array) { |
| 121 | return $string . md5(serialize($array)); |
| 122 | } |
| 123 | } |
| 124 |