/** * Loop over all the content in the main loop and do a request for all the items present so that we're only getting * a few API hits instead of all of them. Then, when anything else calls `easyazon_get_item` it will get the cached * data and not have to hit the API. */ public static function prefetch_items() { global $wp_query; $identifiers = array_fill_keys(array_keys(easyazon_get_locales()), array()); $preparsed = array(); $shortcodes = easyazon_get_shortcodes(); foreach ($wp_query->posts as $post) { preg_match_all('/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $shortcode = trim($match[2]); if (in_array($shortcode, $shortcodes)) { $attributes = shortcode_parse_atts($match[3]); $locale = isset($attributes['locale']) ? $attributes['locale'] : false; $identifier = isset($attributes['identifier']) ? $attributes['identifier'] : false; if (!is_array($preparsed[$shortcode])) { $preparsed[$shortcode] = array(); } $preparsed[$shortcode][] = $attributes; if ($locale && $identifier) { $identifiers[$locale][] = $identifier; } } } } foreach ($identifiers as $locale => $queryable) { $cached_items = easyazon_get_items($queryable, $locale); } self::$preparsed_shortcodes = $preparsed; }
function easyazon_get_preparsed_shortcodes() { return apply_filters(__FUNCTION__, EasyAzon_Components_Performance::get_preparsed_shortcodes()); }