/**
  * Retrive counts using wp_cache
  *
  * @param {String} $post_type
  * @param {Number} [$id]
  */
 private function retrieveCounts($post_type, $id = null)
 {
     if (!isset($id)) {
         $counts = array();
         foreach ((array) wp_count_posts($post_type) as $status => $count) {
             if (in_array($status, $this->whitelist) && $count > 0) {
                 $counts[$status] = (int) $count;
             }
         }
         return $counts;
     }
     global $wpdb;
     $key = 'rest-api-' . $id . '-' . _count_posts_cache_key($post_type);
     $counts = wp_cache_get($key, 'counts');
     if (false === $counts) {
         $results = $wpdb->get_results($this->buildCountsQuery($post_type, $id));
         $counts = $this->filterStatusesByWhiteslist($results);
         wp_cache_set($key, $counts, 'counts');
     }
     return $counts;
 }
Example #2
0
/**
 * Hook for managing future post transitions to published.
 *
 * @since 2.3.0
 * @access private
 *
 * @see wp_clear_scheduled_hook()
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string  $new_status New post status.
 * @param string  $old_status Previous post status.
 * @param WP_Post $post       Post object.
 */
function _transition_post_status($new_status, $old_status, $post)
{
    global $wpdb;
    if ($old_status != 'publish' && $new_status == 'publish') {
        // Reset GUID if transitioning to publish and it is empty.
        if ('' == get_the_guid($post->ID)) {
            $wpdb->update($wpdb->posts, array('guid' => get_permalink($post->ID)), array('ID' => $post->ID));
        }
        /**
         * Fires when a post's status is transitioned from private to published.
         *
         * @since 1.5.0
         * @deprecated 2.3.0 Use 'private_to_publish' instead.
         *
         * @param int $post_id Post ID.
         */
        do_action('private_to_published', $post->ID);
    }
    // If published posts changed clear the lastpostmodified cache.
    if ('publish' == $new_status || 'publish' == $old_status) {
        foreach (array('server', 'gmt', 'blog') as $timezone) {
            wp_cache_delete("lastpostmodified:{$timezone}", 'timeinfo');
            wp_cache_delete("lastpostdate:{$timezone}", 'timeinfo');
            wp_cache_delete("lastpostdate:{$timezone}:{$post->post_type}", 'timeinfo');
        }
    }
    if ($new_status !== $old_status) {
        wp_cache_delete(_count_posts_cache_key($post->post_type), 'counts');
        wp_cache_delete(_count_posts_cache_key($post->post_type, 'readable'), 'counts');
    }
    // Always clears the hook in case the post status bounced from future to draft.
    wp_clear_scheduled_hook('publish_future_post', array($post->ID));
}
 function wp_count_posts($type = 'post', $perm = '')
 {
     global $wpdb;
     if (!post_type_exists($type)) {
         return new stdClass();
     }
     $cache_key = _count_posts_cache_key($type . '_donation', $perm);
     $counts = wp_cache_get($cache_key, 'counts');
     if (false !== $counts) {
         return $counts;
     }
     $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} ";
     $query .= "LEFT JOIN {$wpdb->postmeta} ON wp_postmeta.post_id = wp_posts.ID ";
     $query .= 'WHERE wp_postmeta.meta_key = "_is_donation" AND wp_postmeta.meta_value = 1 AND post_type = %s ';
     $query .= 'GROUP BY post_status ';
     $results = (array) $wpdb->get_results($wpdb->prepare($query, $type), ARRAY_A);
     $counts = array_fill_keys(get_post_stati(), 0);
     foreach ($results as $row) {
         $counts[$row['post_status']] = $row['num_posts'];
     }
     $counts = (object) $counts;
     wp_cache_set($cache_key . '_donation', $counts, 'counts');
     return $counts;
 }
 public function modify_wp_count_posts($old_status, $type, $perm = '')
 {
     global $wpdb;
     if (!post_type_exists($type)) {
         return new stdClass();
     }
     $cache_key = _count_posts_cache_key($type, $perm);
     $counts = wp_cache_get($cache_key, 'wc_qd_modified_wp_count_posts');
     if (false !== $counts) {
         return apply_filters('wc_qd_modified_wp_count_posts', $counts, $type, $perm);
     }
     $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE  ";
     $query .= " ID NOT IN (SELECT donationid FROM `" . WC_QD_TB . "`) ";
     $query .= " AND post_type = %s ";
     if ('readable' == $perm && is_user_logged_in()) {
         $post_type_object = get_post_type_object($type);
         if (!current_user_can($post_type_object->cap->read_private_posts)) {
             $query .= $wpdb->prepare(" AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))", get_current_user_id());
         }
     }
     $query .= ' GROUP BY post_status';
     $results = (array) $wpdb->get_results($wpdb->prepare($query, $type), ARRAY_A);
     $counts = array_fill_keys(get_post_stati(), 0);
     foreach ($results as $row) {
         $counts[$row['post_status']] = $row['num_posts'];
     }
     $counts = (object) $counts;
     wp_cache_set($cache_key, $counts, 'wc_qd_modified_wp_count_posts');
     return apply_filters('wc_qd_modified_wp_count_posts', $counts, $type, $perm);
 }