Get prefix for use with wp_cache_set. Allows all cache in a group to be invalidated at once.
public static get_cache_prefix ( string $group ) : string | ||
$group | string | |
return | string |
/** * Find a matching zone for a given package. * @since 2.6.0 * @uses wc_make_numeric_postcode() * @param object $package * @return WC_Shipping_Zone */ public static function get_zone_matching_package($package) { global $wpdb; $country = strtoupper(wc_clean($package['destination']['country'])); $state = strtoupper(wc_clean($package['destination']['state'])); $continent = strtoupper(wc_clean(WC()->countries->get_continent_code_for_country($country))); $postcode = wc_normalize_postcode(wc_clean($package['destination']['postcode'])); $cache_key = WC_Cache_Helper::get_cache_prefix('shipping_zones') . 'wc_shipping_zone_' . md5(sprintf('%s+%s+%s', $country, $state, $postcode)); $matching_zone_id = wp_cache_get($cache_key, 'shipping_zones'); if (false === $matching_zone_id) { // Work out criteria for our zone search $criteria = array(); $criteria[] = $wpdb->prepare("( ( location_type = 'country' AND location_code = %s )", $country); $criteria[] = $wpdb->prepare("OR ( location_type = 'state' AND location_code = %s )", $country . ':' . $state); $criteria[] = $wpdb->prepare("OR ( location_type = 'continent' AND location_code = %s ) )", $continent); // Postcode range and wildcard matching $postcode_locations = $wpdb->get_results("SELECT zone_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_type = 'postcode';"); if ($postcode_locations) { $zone_ids_with_postcode_rules = array_map('absint', wp_list_pluck($postcode_locations, 'zone_id')); $matches = wc_postcode_location_matcher($postcode, $postcode_locations, 'zone_id', 'location_code'); $do_not_match = array_unique(array_diff($zone_ids_with_postcode_rules, array_keys($matches))); if (!empty($do_not_match)) { $criteria[] = "AND zones.zone_id NOT IN (" . implode(',', $do_not_match) . ")"; } } // Get matching zones $matching_zone_id = $wpdb->get_var("\n\t\t\t\tSELECT zones.zone_id FROM {$wpdb->prefix}woocommerce_shipping_zones as zones\n\t\t\t\tLEFT OUTER JOIN {$wpdb->prefix}woocommerce_shipping_zone_locations as locations ON zones.zone_id = locations.zone_id\n\t\t\t\tWHERE " . implode(' ', $criteria) . "\n\t\t\t\tORDER BY zone_order ASC LIMIT 1\n\t\t\t"); wp_cache_set($cache_key, $matching_zone_id, 'shipping_zones'); } return new WC_Shipping_Zone($matching_zone_id ? $matching_zone_id : 0); }
/** * Get all item meta data in array format in the order it was saved. Does not group meta by key like get_item_meta(). * * @param mixed $order_item_id * @return array of objects */ public function get_item_meta_array($order_item_id) { global $wpdb; // Get cache key - uses get_cache_prefix to invalidate when needed $cache_key = WC_Cache_Helper::get_cache_prefix('orders') . 'item_meta_array_' . $order_item_id; $item_meta_array = wp_cache_get($cache_key, 'orders'); if (false === $item_meta_array) { $item_meta_array = array(); $metadata = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value, meta_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d ORDER BY meta_id", absint($order_item_id))); foreach ($metadata as $metadata_row) { $item_meta_array[$metadata_row->meta_id] = (object) array('key' => $metadata_row->meta_key, 'value' => $metadata_row->meta_value); } wp_cache_set($cache_key, $item_meta_array, 'orders'); } return $item_meta_array; }
/** * This function updates the database for the delivery details and adds delivery fields on the Order Received page, * WooCommerce->Orders when an order is placed for WooCommerce version greater than 2.0. */ function prdd_lite_order_item_meta($item_meta, $cart_item) { if (version_compare(WOOCOMMERCE_VERSION, "2.0.0") < 0) { return; } // Add the fields global $wpdb, $woocommerce; foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { $_product = $values['data']; if (isset($values['prdd_lite_delivery'])) { $delivery = $values['prdd_lite_delivery']; } $quantity = $values['quantity']; $post_id = $this->prdd_lite_get_product_id($values['product_id']); $post_title = $_product->get_title(); $query = "SELECT order_item_id,order_id FROM `" . $wpdb->prefix . "woocommerce_order_items`\n\t\t\t\t\t\tWHERE order_id = %s AND order_item_name = %s"; $results = $wpdb->get_results($wpdb->prepare($query, $item_meta, $post_title)); $order_item_ids[] = $results[0]->order_item_id; $order_id = $results[0]->order_id; $order_obj = new WC_order($order_id); $details = $product_ids = array(); $order_items = $order_obj->get_items(); if (isset($values['prdd_lite_delivery'])) { $prdd_settings = get_post_meta($post_id, '_woo_prdd_lite_enable_delivery_date', true); $details = array(); if (isset($delivery[0]['delivery_date']) && $delivery[0]['delivery_date'] != "") { $name = "Delivery Date"; $date_select = $delivery[0]['delivery_date']; wc_add_order_item_meta($results[0]->order_item_id, $name, sanitize_text_field($date_select, true)); } if (array_key_exists('delivery_hidden_date', $delivery[0]) && $delivery[0]['delivery_hidden_date'] != "") { $date_booking = date('Y-m-d', strtotime($delivery[0]['delivery_hidden_date'])); wc_add_order_item_meta($results[0]->order_item_id, '_prdd_lite_date', sanitize_text_field($date_booking, true)); } } if (version_compare(WOOCOMMERCE_VERSION, "2.5") < 0) { continue; } else { // Code where the Delivery dates are not displayed in the customer new order email from WooCommerce version 2.5 $cache_key = WC_Cache_Helper::get_cache_prefix('orders') . 'item_meta_array_' . $results[0]->order_item_id; $item_meta_array = wp_cache_get($cache_key, 'orders'); if (false !== $item_meta_array) { $metadata = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value, meta_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d AND meta_key IN (%s,%s) ORDER BY meta_id", absint($results[0]->order_item_id), "Delivery Date", '_prdd_lite_date')); foreach ($metadata as $metadata_row) { $item_meta_array[$metadata_row->meta_id] = (object) array('key' => $metadata_row->meta_key, 'value' => $metadata_row->meta_value); } wp_cache_set($cache_key, $item_meta_array, 'orders'); } } } }
/** * Get coupon code by ID. * * @since 2.7.0 * @param string $code * @param int $exclude Used to exclude an ID from the check if you're checking existance. * @return int */ function wc_get_coupon_id_by_code($code, $exclude = 0) { global $wpdb; $ids = wp_cache_get(WC_Cache_Helper::get_cache_prefix('coupons') . 'coupon_id_from_code_' . $code, 'coupons'); if (false === $ids) { $sql = $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' ORDER BY post_date DESC;", $code); $ids = $wpdb->get_col($sql); if ($ids) { wp_cache_set(WC_Cache_Helper::get_cache_prefix('coupons') . 'coupon_id_from_code_' . $code, $ids, 'coupons'); } } $ids = array_diff(array_filter(array_map('absint', (array) $ids)), array($exclude)); return apply_filters('woocommerce_get_coupon_id_from_code', absint(current($ids)), $code, $exclude); }
/** * Return the orders count of a specific order status. * * @param string $status * @return int */ function wc_orders_count($status) { $count = 0; $status = 'wc-' . $status; $order_statuses = array_keys(wc_get_order_statuses()); if (!in_array($status, $order_statuses)) { return 0; } $cache_key = WC_Cache_Helper::get_cache_prefix('orders') . $status; $cached_count = wp_cache_get($cache_key, 'counts'); if (false !== $cached_count) { return $cached_count; } foreach (wc_get_order_types('order-count') as $type) { $data_store = WC_Data_Store::load('shop_order' === $type ? 'order' : $type); if ($data_store) { $count += $data_store->get_order_count($status); } } wp_cache_set($cache_key, $count, 'counts'); return $count; }
/** * Searches for all matching country/state/postcode tax rates. * * @param array $args * @return array */ public static function find_rates($args = array()) { $args = wp_parse_args($args, array('country' => '', 'state' => '', 'city' => '', 'postcode' => '', 'tax_class' => '')); extract($args, EXTR_SKIP); if (!$country) { return array(); } $postcode = wc_normalize_postcode(wc_clean($postcode)); $cache_key = WC_Cache_Helper::get_cache_prefix('taxes') . 'wc_tax_rates_' . md5(sprintf('%s+%s+%s+%s+%s', $country, $state, $city, $postcode, $tax_class)); $matched_tax_rates = wp_cache_get($cache_key, 'taxes'); if (false === $matched_tax_rates) { $matched_tax_rates = self::get_matched_tax_rates($country, $state, $postcode, $city, $tax_class); wp_cache_set($cache_key, $matched_tax_rates, 'taxes'); } return apply_filters('woocommerce_find_rates', $matched_tax_rates, $args); }
/** * Get coupon code by ID. * * @since 2.7.0 * @param string $code * @param int $exclude Used to exclude an ID from the check if you're checking existance. * @return int */ function wc_get_coupon_id_by_code($code, $exclude = 0) { $data_store = WC_Data_Store::load('coupon'); $ids = wp_cache_get(WC_Cache_Helper::get_cache_prefix('coupons') . 'coupon_id_from_code_' . $code, 'coupons'); if (false === $ids) { $ids = $data_store->get_ids_by_code($code); if ($ids) { wp_cache_set(WC_Cache_Helper::get_cache_prefix('coupons') . 'coupon_id_from_code_' . $code, $ids, 'coupons'); } } $ids = array_diff(array_filter(array_map('absint', (array) $ids)), array($exclude)); return apply_filters('woocommerce_get_coupon_id_from_code', absint(current($ids)), $code, $exclude); }
/** * WooCommerce Order Item Meta API - Delete term meta. * * @access public * @param mixed $item_id * @param mixed $meta_key * @param string $meta_value (default: '') * @param bool $delete_all (default: false) * @return bool */ function wc_delete_order_item_meta($item_id, $meta_key, $meta_value = '', $delete_all = false) { if (delete_metadata('order_item', $item_id, $meta_key, $meta_value, $delete_all)) { $cache_key = WC_Cache_Helper::get_cache_prefix('orders') . 'item_meta_array_' . $item_id; wp_cache_delete($cache_key, 'orders'); return true; } return false; }
/** * Return the orders count of a specific order status. * * @access public * @param string $status * @return int */ function wc_orders_count($status) { global $wpdb; $count = 0; $status = 'wc-' . $status; $order_statuses = array_keys(wc_get_order_statuses()); if (!in_array($status, $order_statuses)) { return 0; } $cache_key = WC_Cache_Helper::get_cache_prefix('orders') . $status; $cached_count = wp_cache_get($cache_key, 'counts'); if (false !== $cached_count) { return $cached_count; } foreach (wc_get_order_types('order-count') as $type) { $query = "SELECT COUNT( * ) FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s"; $count += $wpdb->get_var($wpdb->prepare($query, $type, $status)); } wp_cache_set($cache_key, $count, 'counts'); return $count; }
/** * Find a matching zone for a given package. * @since 2.6.0 * @uses wc_make_numeric_postcode() * @param object $package * @return WC_Shipping_Zone */ public static function get_zone_matching_package($package) { global $wpdb; $country = strtoupper(wc_clean($package['destination']['country'])); $state = strtoupper(wc_clean($package['destination']['state'])); $continent = strtoupper(wc_clean(WC()->countries->get_continent_code_for_country($country))); $postcode = strtoupper(wc_clean($package['destination']['postcode'])); $valid_postcodes = array_map('wc_clean', self::_get_wildcard_postcodes($postcode)); $cache_key = WC_Cache_Helper::get_cache_prefix('shipping_zones') . 'wc_shipping_zone_' . md5(sprintf('%s+%s+%s', $country, $state, $postcode)); $matching_zone_id = wp_cache_get($cache_key, 'shipping_zones'); if (false === $matching_zone_id) { // Work out criteria for our zone search $criteria = array(); $criteria[] = $wpdb->prepare("( ( location_type = 'country' AND location_code = %s )", $country); $criteria[] = $wpdb->prepare("OR ( location_type = 'state' AND location_code = %s )", $country . ':' . $state); $criteria[] = $wpdb->prepare("OR ( location_type = 'continent' AND location_code = %s ) )", $continent); // Postcode range and wildcard matching $postcode_locations = $wpdb->get_results("SELECT zone_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_type = 'postcode';"); if ($postcode_locations) { $zone_ids_with_postcode_rules = array_map('absint', wp_list_pluck($postcode_locations, 'zone_id')); $zone_id_matches = array(); foreach ($postcode_locations as $postcode_location) { $postcode_to_match = trim(strtoupper($postcode_location->location_code)); // Ranges if (strstr('-', $postcode_to_match)) { $range = array_map('trim', explode('-', $postcode_to_match)); if (sizeof($range) != 2) { continue; } if (is_numeric($range[0]) && is_numeric($range[1])) { $encoded_postcode = $postcode; $min = $range[0]; $max = $range[1]; } else { $min = wc_make_numeric_postcode($range[0]); $max = wc_make_numeric_postcode($range[1]); $min = str_pad($min, $encoded_postcode_len, '0'); $max = str_pad($max, $encoded_postcode_len, '9'); } if ($encoded_postcode >= $min && $encoded_postcode <= $max) { $zone_id_matches[] = absint($postcode_location->zone_id); } // Wildcard/standard } elseif (in_array($postcode_to_match, $valid_postcodes)) { $zone_id_matches[] = absint($postcode_location->zone_id); } } $do_not_match = array_unique(array_diff($zone_ids_with_postcode_rules, $zone_id_matches)); if ($do_not_match) { $criteria[] = "AND zones.zone_id NOT IN (" . implode(',', $do_not_match) . ")"; } } // Get matching zones $matching_zone_id = $wpdb->get_var("\n\t\t\t\tSELECT zones.zone_id FROM {$wpdb->prefix}woocommerce_shipping_zones as zones\n\t\t\t\tLEFT OUTER JOIN {$wpdb->prefix}woocommerce_shipping_zone_locations as locations ON zones.zone_id = locations.zone_id\n\t\t\t\tWHERE " . implode(' ', $criteria) . "\n\t\t\t\tORDER BY zone_order ASC LIMIT 1\n\t\t\t"); wp_cache_set($cache_key, $matching_zone_id, 'shipping_zones'); } return new WC_Shipping_Zone($matching_zone_id ? $matching_zone_id : 0); }
public function clear_wc_tax_cache($to_country, $to_state, $source_city, $source_zip) { global $woocommerce; if (version_compare($woocommerce->version, '2.5.0', '>=')) { $cache_key = WC_Cache_Helper::get_cache_prefix('taxes') . 'wc_tax_rates_' . md5(sprintf('%s+%s+%s+%s+%s', $to_country, $to_state, $source_city, wc_clean($source_zip), '')); wp_cache_delete($cache_key, 'taxes'); } else { $valid_postcodes = $this->_get_wildcard_postcodes(wc_clean($source_zip)); $rates_transient_key = 'wc_tax_rates_' . md5(sprintf('%s+%s+%s+%s+%s', $to_country, $to_state, $source_city, implode(',', $valid_postcodes), '')); delete_transient($rates_transient_key); } }
/** * Read Meta Data from the database. Ignore any internal properties. * Uses it's own caches because get_metadata does not provide meta_ids. * * @since 2.6.0 * @param bool $force_read True to force a new DB read (and update cache). */ public function read_meta_data($force_read = false) { $this->meta_data = array(); $cache_loaded = false; if (!$this->get_id()) { return; } if (!$this->data_store) { return; } if (!empty($this->cache_group)) { $cache_key = WC_Cache_Helper::get_cache_prefix($this->cache_group) . 'object_meta_' . $this->get_id(); } if (!$force_read) { if (!empty($this->cache_group)) { $cached_meta = wp_cache_get($cache_key, $this->cache_group); if (false !== $cached_meta) { $this->meta_data = $cached_meta; $cache_loaded = true; } } } if (!$cache_loaded) { $raw_meta_data = $this->data_store->read_meta($this); if ($raw_meta_data) { foreach ($raw_meta_data as $meta) { $this->meta_data[] = (object) array('id' => (int) $meta->meta_id, 'key' => $meta->meta_key, 'value' => maybe_unserialize($meta->meta_value)); } if (!empty($this->cache_group)) { wp_cache_set($cache_key, $this->meta_data, $this->cache_group); } } } }
/** * WooCommerce Order Item Meta API - Delete term meta. * * @access public * @param mixed $item_id * @param mixed $meta_key * @param string $meta_value (default: '') * @param bool $delete_all (default: false) * @return bool */ function wc_delete_order_item_meta($item_id, $meta_key, $meta_value = '', $delete_all = false) { $data_store = WC_Data_Store::load('order-item'); if ($data_store->delete_metadata($item_id, $meta_key, $meta_value, $delete_all)) { $cache_key = WC_Cache_Helper::get_cache_prefix('orders') . 'item_meta_array_' . $item_id; wp_cache_delete($cache_key, 'orders'); return true; } return false; }
/** * Get a coupon ID from it's code. * @since 2.5.0 woocommerce_coupon_code_query was removed in favour of woocommerce_get_coupon_id_from_code filter on the return. wp_cache was also implemented. * @param string $code * @return int */ private function get_coupon_id_from_code($code) { global $wpdb; $coupon_id = wp_cache_get(WC_Cache_Helper::get_cache_prefix('coupons') . 'coupon_id_from_code_' . $code, 'coupons'); if (false === $coupon_id) { $sql = $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $this->code); $coupon_id = apply_filters('woocommerce_get_coupon_id_from_code', $wpdb->get_var($sql), $this->code); wp_cache_set(WC_Cache_Helper::get_cache_prefix('coupons') . 'coupon_id_from_code_' . $code, $coupon_id, 'coupons'); } return absint($coupon_id); }
/** * Read Meta Data from the database. Ignore any internal properties. * @since 2.6.0 */ protected function read_meta_data() { $this->_meta_data = array(); $cache_loaded = false; if (!$this->get_id()) { return; } if (!empty($this->_cache_group)) { $cache_key = WC_Cache_Helper::get_cache_prefix($this->_cache_group) . $this->get_id(); $cached_meta = wp_cache_get($cache_key, $this->_cache_group); if (false !== $cached_meta) { $this->_meta_data = $cached_meta; $cache_loaded = true; } } if (!$cache_loaded) { global $wpdb; $db_info = $this->_get_db_info(); $raw_meta_data = $wpdb->get_results($wpdb->prepare("\n\t\t\t\tSELECT " . $db_info['meta_id_field'] . ", meta_key, meta_value\n\t\t\t\tFROM " . $db_info['table'] . "\n\t\t\t\tWHERE " . $db_info['object_id_field'] . "=%d AND meta_key NOT LIKE 'wp\\_%%' ORDER BY " . $db_info['meta_id_field'] . "\n\t\t\t", $this->get_id())); if ($raw_meta_data) { foreach ($raw_meta_data as $meta) { if (in_array($meta->meta_key, $this->get_internal_meta_keys())) { continue; } $this->_meta_data[] = (object) array('id' => (int) $meta->{$db_info['meta_id_field']}, 'key' => $meta->meta_key, 'value' => maybe_unserialize($meta->meta_value)); } } if (!empty($this->_cache_group)) { wp_cache_set($cache_key, $this->_meta_data, $this->_cache_group); } } }
/** * Gets a cache prefix. This is used in session names so the entire cache can be invalidated with 1 function call. * * @return string */ private function get_cache_prefix() { return WC_Cache_Helper::get_cache_prefix(WC_SESSION_CACHE_GROUP); }
/** * Find a matching zone for a given package. * @since 2.6.0 * @uses wc_make_numeric_postcode() * @param object $package * @return WC_Shipping_Zone */ public static function get_zone_matching_package($package) { $country = strtoupper(wc_clean($package['destination']['country'])); $state = strtoupper(wc_clean($package['destination']['state'])); $continent = strtoupper(wc_clean(WC()->countries->get_continent_code_for_country($country))); $postcode = wc_normalize_postcode(wc_clean($package['destination']['postcode'])); $cache_key = WC_Cache_Helper::get_cache_prefix('shipping_zones') . 'wc_shipping_zone_' . md5(sprintf('%s+%s+%s', $country, $state, $postcode)); $matching_zone_id = wp_cache_get($cache_key, 'shipping_zones'); if (false === $matching_zone_id) { $data_store = WC_Data_Store::load('shipping-zone'); $matching_zone_id = $data_store->get_zone_id_from_package($package); wp_cache_set($cache_key, $matching_zone_id, 'shipping_zones'); } return new WC_Shipping_Zone($matching_zone_id ? $matching_zone_id : 0); }