Plugin Directory

Changeset 619740

Timestamp:
11/01/2012 08:17:20 AM (12 years ago)
Author:
_ck_
Message:

0.0.4 updated to use internal WP 3.x api functions

Location:
wp-cache-users/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-cache-users/trunk/readme.txt

    r84127 r619740  
    22Tags: performance, speed, mysql, queries, cache, caching, faster, _ck_
    33Contributors: _ck_
    4 Requires at least: 2.3
    5 Tested up to: 2.5.1
     4Requires at least:
     5Tested up to:
    66Stable tag: trunk
    77Donate link: http://bbshowcase.org/donate/
     
    1313Caches multiple users automatically on post and comment pages to radically reduce mysql queries for plugins that get_userdata.
    1414If you find that your query count has become excessive (25+) on your post or comment pages, this plugin may help.
    15 Tested up to WordPress 2.5 (might be compatibile with newer versions). Feedback requested.
     15Tested up to WordPress (might be compatibile with newer versions). Feedback requested.
    1616
    1717== Installation ==
     
    2828* http://bbshowcase.org/donate/
    2929
    30 == History ==
     30== ==
    3131
    3232= Version 0.0.1 (2009-01-02) =
     
    3434* first public alpha release
    3535
     36
     37
     38
     39
  • wp-cache-users/trunk/wp-cache-users.php

    r84127 r619740  
    66Author: _ck_
    77Author URI: http://bbShowcase.org
    8 Version: 0.0.1
     8Version: 0.0.
    99*/
    1010
     
    1313
    1414function wp_cache_users_from_posts($posts) {
    15 if (!empty($posts)) {
    16     foreach ($posts as $post) {if (!empty($post->post_author)) {$ids[$post->post_author]=$post->post_author;}}
    17     wp_cache_users($ids);
    18 }
    19 return $posts;
     15if (!empty($posts)) {
     16    foreach ($posts as $post) {if (!empty($post->post_author)) {$ids[$post->post_author]=$post->post_author;}}
     17    wp_cache_users($ids);
     18}
     19return $posts;
    2020}
    2121
    2222function wp_cache_users_from_comments($comments) {
    23 if (!empty($comments)) {
    24     foreach ($comments as $comment) {if (!empty($comment->user_id)) {$ids[$comment->user_id]=$comment->user_id;}}
    25     wp_cache_users($ids);
    26 }
    27 return $comments;
     23if (!empty($comments)) {
     24    foreach ($comments as $comment) {if (!empty($comment->user_id)) {$ids[$comment->user_id]=$comment->user_id;}}
     25    wp_cache_users($ids);
     26}
     27return $comments;
    2828}
    2929
    30 function wp_cache_users($ids) {     // does the heavy lifting, load and cache as many users as you want
    31 if (empty($ids) || !is_array($ids)) {return;}
    32 global $wpdb,$wp_object_cache; 
    33 $ids=array_unique($ids); $ids = array_map('intval', $ids); 
    34 foreach ($ids as $key=>$id) {if (isset($wp_object_cache->cache['users'][$id])) {unset($ids[$key]);}}    // don't load what's already there
    35 if (empty($ids)) {return;}
    36 $sids = join(',', $ids);
    37 $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE ID IN ($sids)");
    38 if (empty($users)) {return;}   
    39 foreach ($users as $user) {$temp[$user->ID]=$user;} $users=$temp; unset($temp);   // make key user_id
    40 $metas = $wpdb->get_results("SELECT user_id,meta_key, meta_value FROM $wpdb->usermeta WHERE user_id IN ($sids)");
    41 if (!empty($metas)) {   
    42     foreach ( $metas as $meta ) {
    43         $value = maybe_unserialize($meta->meta_value);
    44         $users[$meta->user_id]->{$meta->meta_key} = $value;
    45     }   
     30function wp_cache_users($ids) {
     31    if (empty($ids)) {return;}
     32    global $wpdb, $wp_object_cache; $limit=0; $in='';
     33    foreach ($ids as $key=>$id) {if (!isset($wp_object_cache->cache['users'][$id])) {$limit++; $in.=$id.',';}}
     34    if ($limit) {
     35        $in=substr($in,0,-1);
     36        $users=$wpdb->get_results("SELECT * FROM $wpdb->users WHERE ID IN ($in) LIMIT $limit");
     37        if (!empty($users)) { _fill_many_users($users); }
     38    }
    4639}
    47 foreach ($users as $user) {
    48     $level = $wpdb->prefix . 'user_level';
    49     if ( isset( $user->{$level} ) ) {   $user->user_level = $user->{$level};}
    50     // For backwards compat.
    51     if ( isset($user->first_name) ) {$user->user_firstname = $user->first_name;}
    52     if ( isset($user->last_name) ) {$user->user_lastname = $user->last_name;}
    53     if ( isset($user->description) ) {$user->user_description = $user->description;}
    54     wp_cache_add($user->ID, $user, 'users');
    55     wp_cache_add($user->user_login, $user->ID, 'userlogins');
    56     wp_cache_add($user->user_email, $user->ID, 'useremail');
    57 }
    58 }
    59 ?>
Note: See TracChangeset for help on using the changeset viewer.