/** * Get Terms Category Sort Filter * * This sorts the categories when a call to get_terms is made. * * If $args requests that term objects (default) are returned then * we are able to pre-fetch the term meta in bulk. * * @param object|array $terms Array of term objects or field values. * @param array $taxonomies Taxonomies. * @param array $args Args passed to get_terms(). * @return object|array $terms */ function wpsc_get_terms_category_sort_filter($terms, $taxonomies, $args) { if (in_array('wpsc_product_category', $taxonomies)) { $new_terms = array(); $unsorted = array(); $term_ids = array(); // If something other than term objects are requested, don't sort. if (isset($args['fields']) && 'all' != $args['fields']) { return $terms; } else { $term_ids = wp_list_pluck($terms, 'term_id'); } // Pre-fetch category meta in bulk if we've been able to grab the IDs. if (!empty($term_ids)) { wpsc_update_meta_cache('wpsc_category', $term_ids); } foreach ($terms as $term) { if (!is_object($term)) { return $terms; } $term_order = $term->taxonomy == 'wpsc_product_category' ? wpsc_get_meta($term->term_id, 'sort_order', 'wpsc_category') : null; $term_order = (int) $term_order; // unsorted categories should go to the top of the list if ($term_order == 0) { $term->sort_order = $term_order; $unsorted[] = $term; continue; } while (isset($new_terms[$term_order])) { $term_order++; } $term->sort_order = $term_order; $new_terms[$term_order] = $term; } if (!empty($new_terms)) { ksort($new_terms); } for ($i = count($unsorted) - 1; $i >= 0; $i--) { array_unshift($new_terms, $unsorted[$i]); } return array_values($new_terms); } return $terms; }
/** * Get meta data from the database * * Gets and caches an object's meta using the WordPress Object Cache API * and returns meta for a specific key. * * @internal * * @param integer $object_id Object ID. * @param string $meta_key Meta key. * @param string $object_type Object type. * @return mixed Meta value. */ function wpsc_get_meta($object_id = 0, $meta_key, $object_type) { global $wpdb; $cache_object_id = $object_id = (int) $object_id; $meta_key = wpsc_sanitize_meta_key($meta_key); $meta_tuple = compact('object_type', 'object_id', 'meta_key'); $meta_tuple = apply_filters('wpsc_get_meta', $meta_tuple); // Get cached meta $meta_value = wp_cache_get($cache_object_id, $meta_tuple['object_type']); // If not cached, get and cache all object meta if ($meta_value === false) { $meta_values = wpsc_update_meta_cache($meta_tuple['object_type'], $meta_tuple['object_id']); $meta_value = $meta_values[$meta_tuple['object_id']]; } if (isset($meta_value[$meta_tuple['meta_key']])) { return maybe_unserialize($meta_value[$meta_tuple['meta_key']]); } return ''; }
/** * Get Countries Meta * * Returns and caches meta data for all countries to eliminate * multiple database request for each meta value later. * * @return array Countries meta */ public static function get_countries_meta() { $ids = array(); foreach (self::$active_wpsc_country_by_country_id->data() as $c) { $ids[] = $c->get_id(); } return wpsc_update_meta_cache('WPSC_Country', $ids); }