/**
* Update taxonomies cache for the custom post types for the current query.
*/
function gdtt_custom_post_types_cache()
{
global $wp_query;
$post_ids = array();
for ($i = 0; $i < count($wp_query->posts); $i++) {
$post_ids[] = $wp_query->posts[$i]->ID;
}
if (!empty($post_ids)) {
update_object_term_cache($post_ids, get_query_var("post_type"));
}
}
public function _prime_terms_cache($terms, $taxonomies)
{
if ($this->is_translated_taxonomy($taxonomies)) {
foreach ($terms as $term) {
$term_ids[] = is_object($term) ? $term->term_id : $term;
}
}
if (!empty($term_ids)) {
update_object_term_cache(array_unique($term_ids), 'term');
}
// adds language and translation of terms to cache
return $terms;
}
/**
* update_post_caches() - Call major cache updating functions for list of Post objects.
*
* @package WordPress
* @subpackage Cache
* @since 1.5
*
* @uses $wpdb
* @uses update_post_cache()
* @uses update_object_term_cache()
* @uses update_postmeta_cache()
*
* @param array $posts Array of Post objects
*/
function update_post_caches(&$posts)
{
// No point in doing all this work if we didn't match any posts.
if (!$posts) {
return;
}
update_post_cache($posts);
$post_ids = array();
for ($i = 0; $i < count($posts); $i++) {
$post_ids[] = $posts[$i]->ID;
}
update_object_term_cache($post_ids, 'post');
update_postmeta_cache($post_ids);
}
//.........这里部分代码省略.........
$view_settings_defaults = array('post_type' => 'any', 'orderby' => 'post-date', 'order' => 'DESC', 'paged' => '1', 'posts_per_page' => -1);
extract($view_settings_defaults);
$view_settings['view_id'] = $id;
extract($view_settings, EXTR_OVERWRITE);
$query = array('posts_per_page' => $posts_per_page, 'paged' => $paged, 'post_type' => $post_type, 'order' => $order, 'suppress_filters' => false, 'ignore_sticky_posts' => true);
// Add special check for media (attachments) as their default status in not usually published
if (sizeof($post_type) == 1 && $post_type[0] == 'attachment') {
$query['post_status'] = 'any';
// Note this can be overriden by adding a status filter.
}
$query = apply_filters('wpv_filter_query', $query, $view_settings, $id);
// Now we have the $query as in the original one
// We now need to overwrite the limit, offset, paged and pagination options
// Also, we set it to just return the IDs
$query['posts_per_page'] = -1;
$query['ĺimit'] = -1;
$query['paged'] = 1;
$query['offset'] = 0;
$query['fields'] = 'ids';
if ($cache_exclude_queried_posts) {
// do not query again already queried and cached posts
$already = array();
if (isset($post_query->posts) && !empty($post_query->posts)) {
foreach ((array) $post_query->posts as $post_object) {
$already[] = $post_object->ID;
}
}
$WP_Views->returned_ids_for_parametric_search = $already;
if (isset($query['pr_filter_post__in'])) {
$query['post__in'] = $query['pr_filter_post__in'];
} else {
// If just for the missing ones, generate the post__not_in argument
if (isset($query['post__not_in'])) {
$query['post__not_in'] = array_merge((array) $query['post__not_in'], (array) $already);
} else {
$query['post__not_in'] = (array) $already;
}
// And adjust on the post__in argument
if (isset($query['post__in'])) {
$query['post__in'] = array_diff((array) $query['post__in'], (array) $query['post__not_in']);
//unset( $query['post__in'] );
}
}
}
// Perform the query
$aux_cache_query = new WP_Query($query);
// In case we need to recreate our own cache object, we do not need to load there all the postmeta and taxonomy data, just for the elements involved in parametric search controls
$filter_c_mode = isset($view_settings['filter_controls_mode']) && is_array($view_settings['filter_controls_mode']) ? $view_settings['filter_controls_mode'] : array();
$filter_c_name = isset($view_settings['filter_controls_field_name']) && is_array($view_settings['filter_controls_field_name']) ? $view_settings['filter_controls_field_name'] : array();
$f_taxes = array();
$f_fields = array();
foreach ($filter_c_mode as $f_index => $f_mode) {
if (isset($filter_c_name[$f_index])) {
switch ($f_mode) {
case 'slug':
$f_taxes[] = $filter_c_name[$f_index];
break;
case 'cf':
$f_fields[] = $filter_c_name[$f_index];
break;
case 'rel':
if (function_exists('wpcf_pr_get_belongs')) {
$returned_post_types = $view_settings['post_type'];
$returned_post_type_parents = array();
if (empty($returned_post_types)) {
$returned_post_types = array('any');
}
foreach ($returned_post_types as $returned_post_type_slug) {
$parent_parents_array = wpcf_pr_get_belongs($returned_post_type_slug);
if ($parent_parents_array != false && is_array($parent_parents_array)) {
$returned_post_type_parents = array_merge($returned_post_type_parents, array_values(array_keys($parent_parents_array)));
}
}
foreach ($returned_post_type_parents as $parent_to_cache) {
$f_fields[] = '_wpcf_belongs_' . $parent_to_cache . '_id';
}
}
break;
default:
break;
}
}
}
// If we are using the native caching, update the cache for the posts returned by the aux query
if (is_array($aux_cache_query->posts) && !empty($aux_cache_query->posts)) {
$WP_Views->returned_ids_for_parametric_search = array_merge($WP_Views->returned_ids_for_parametric_search, $aux_cache_query->posts);
$WP_Views->returned_ids_for_parametric_search = array_unique($WP_Views->returned_ids_for_parametric_search);
if ($cache_use_native) {
// If we are using the native caching, update the cache for the posts returned by the aux query
update_postmeta_cache($aux_cache_query->posts);
update_object_term_cache($aux_cache_query->posts, $view_settings['post_type']);
} else {
// Else, we need to fake an $wp_object_cache->cache
$f_data = array('cf' => $f_fields, 'tax' => $f_taxes);
$cache_combined = wpv_custom_cache_metadata($aux_cache_query->posts, $f_data);
$wp_object_cache->cache = $cache_combined;
}
}
return $post_query;
}
/**
* @group cache
*/
public function test_taxonomy_classes_hit_cache()
{
global $wpdb;
register_taxonomy('wptests_tax', 'post');
wp_set_post_terms($this->post_id, array('foo', 'bar'), 'wptests_tax');
wp_set_post_terms($this->post_id, array('footag', 'bartag'), 'post_tag');
// Prime cache, including meta cache, which is used by get_post_class().
update_object_term_cache($this->post_id, 'post');
update_meta_cache('post', $this->post_id);
$num_queries = $wpdb->num_queries;
$found = get_post_class('', $this->post_id);
$this->assertSame($num_queries, $wpdb->num_queries);
}
function update_post_caches(&$posts)
{
global $post_cache;
global $wpdb, $blog_id;
// No point in doing all this work if we didn't match any posts.
if (!$posts) {
return;
}
// Get the categories for all the posts
for ($i = 0; $i < count($posts); $i++) {
$post_id_array[] = $posts[$i]->ID;
$post_cache[$blog_id][$posts[$i]->ID] =& $posts[$i];
}
$post_id_list = implode(',', $post_id_array);
update_object_term_cache($post_id_list, 'post');
update_postmeta_cache($post_id_list);
}
/**
* @group cache
*/
public function test_taxonomy_classes_hit_cache()
{
global $wpdb;
register_taxonomy('wptests_tax', 'post');
wp_set_post_terms($this->post_id, array('foo', 'bar'), 'wptests_tax');
wp_set_post_terms($this->post_id, array('footag', 'bartag'), 'post_tag');
// Prime cache, including meta cache, which is used by get_post_class().
update_object_term_cache($this->post_id, 'post');
update_meta_cache('post', $this->post_id);
$num_queries = $wpdb->num_queries;
$found = get_post_class('', $this->post_id);
// The 'site_icon' option check adds a query during unit tests. See {@see WP_Site_Icon::get_post_metadata()}.
$expected_num_queries = $num_queries + 1;
$this->assertSame($expected_num_queries, $wpdb->num_queries);
}
/**
* Call major cache updating functions for list of Post objects.
*
* @package WordPress
* @subpackage Cache
* @since 1.5.0
*
* @uses $wpdb
* @uses update_post_cache()
* @uses update_object_term_cache()
* @uses update_postmeta_cache()
*
* @param array $posts Array of Post objects
* @param string $post_type The post type of the posts in $posts. Default is 'post'.
* @param bool $update_term_cache Whether to update the term cache. Default is true.
* @param bool $update_meta_cache Whether to update the meta cache. Default is true.
*/
function update_post_caches(&$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true)
{
// No point in doing all this work if we didn't match any posts.
if (!$posts) {
return;
}
update_post_cache($posts);
$post_ids = array();
foreach ($posts as $post) {
$post_ids[] = $post->ID;
}
if (empty($post_type)) {
$post_type = 'post';
}
if (!is_array($post_type) && 'any' != $post_type && $update_term_cache) {
update_object_term_cache($post_ids, $post_type);
}
if ($update_meta_cache) {
update_postmeta_cache($post_ids);
}
}
/**
* Get all the events in the month by directly querying the postmeta table
* Also caches the postmeta and terms for the found events
*/
protected function set_events_in_month()
{
global $wpdb;
$grid_start_datetime = tribe_beginning_of_day($this->first_grid_date);
$grid_end_datetime = tribe_end_of_day($this->final_grid_date);
$cache = new Tribe__Cache();
$cache_key = 'events_in_month' . $grid_start_datetime . '-' . $grid_end_datetime;
// if we have a cached result, use that
$cached_events = $cache->get($cache_key, 'save_post');
if ($cached_events !== false) {
$this->events_in_month = $cached_events;
return;
}
$post_stati = array('publish');
if (is_user_logged_in()) {
$post_stati[] = 'private';
}
$post_stati = implode("','", $post_stati);
$ignore_hidden_events_AND = $this->hidden_events_fragment();
$events_request = $wpdb->prepare("SELECT tribe_event_start.post_id as ID,\n\t\t\t\t\t\ttribe_event_start.meta_value as EventStartDate,\n\t\t\t\t\t\ttribe_event_end_date.meta_value as EventEndDate\n\t\t\t\tFROM {$wpdb->postmeta} AS tribe_event_start\n\t\t\t\tLEFT JOIN {$wpdb->posts} ON tribe_event_start.post_id = {$wpdb->posts}.ID\n\t\t\t\tLEFT JOIN {$wpdb->postmeta} as tribe_event_end_date ON ( tribe_event_start.post_id = tribe_event_end_date.post_id AND tribe_event_end_date.meta_key = '_EventEndDate' )\n\t\t\t\tWHERE {$ignore_hidden_events_AND} tribe_event_start.meta_key = '_EventStartDate'\n\t\t\t\tAND ( (tribe_event_start.meta_value >= '%1\$s' AND tribe_event_start.meta_value <= '%2\$s')\n\t\t\t\t\tOR (tribe_event_start.meta_value <= '%1\$s' AND tribe_event_end_date.meta_value >= '%1\$s')\n\t\t\t\t\tOR ( tribe_event_start.meta_value >= '%1\$s' AND tribe_event_start.meta_value <= '%2\$s')\n\t\t\t\t)\n\t\t\t\tAND {$wpdb->posts}.post_status IN('{$post_stati}')\n\t\t\t\tORDER BY {$wpdb->posts}.menu_order ASC, DATE(tribe_event_start.meta_value) ASC, TIME(tribe_event_start.meta_value) ASC;\n\t\t\t\t", $grid_start_datetime, $grid_end_datetime);
$this->events_in_month = $wpdb->get_results($events_request);
// cache the postmeta and terms for all these posts in one go
$event_ids_in_month = wp_list_pluck($this->events_in_month, 'ID');
update_object_term_cache($event_ids_in_month, Tribe__Events__Main::POSTTYPE);
update_postmeta_cache($event_ids_in_month);
// cache the found events in the object cache
$cache->set($cache_key, $this->events_in_month, 0, 'save_post');
}
/**
* copy or synchronize terms
*
* @since 1.8
*
* @param int $from id of the post from which we copy informations
* @param int $to id of the post to which we paste informations
* @param string $lang language slug
* @param bool $sync true if it is synchronization, false if it is a copy, defaults to false
*/
public function copy_taxonomies($from, $to, $lang, $sync = false)
{
// get taxonomies to sync for this post type
$taxonomies = array_intersect(get_post_taxonomies($from), $this->get_taxonomies_to_copy($sync));
// update the term cache to reduce the number of queries in the loop
update_object_term_cache($sync ? array($from, $to) : $from, get_post_type($from));
// copy or synchronize terms
// FIXME quite a lot of query in foreach
foreach ($taxonomies as $tax) {
$terms = get_the_terms($from, $tax);
// translated taxonomy
if ($this->model->is_translated_taxonomy($tax)) {
$newterms = array();
if (is_array($terms)) {
foreach ($terms as $term) {
if ($term_id = $this->model->term->get_translation($term->term_id, $lang)) {
$newterms[] = (int) $term_id;
// cast is important otherwise we get 'numeric' tags
}
}
}
// for some reasons, the user may have untranslated terms in the translation. don't forget them.
if ($sync) {
$tr_terms = get_the_terms($to, $tax);
if (is_array($tr_terms)) {
foreach ($tr_terms as $term) {
if (!$this->model->term->get_translation($term->term_id, $this->model->post->get_language($from))) {
$newterms[] = (int) $term->term_id;
}
}
}
}
if (!empty($newterms) || $sync) {
wp_set_object_terms($to, $newterms, $tax);
// replace terms in translation
}
} else {
wp_set_object_terms($to, is_array($terms) ? array_map('intval', wp_list_pluck($terms, 'term_id')) : null, $tax);
}
}
}
请发表评论