WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/17/2009 08:17:33 PM (10 years ago)
Author:
westi
Message:

First pass commentmeta implementation. See #2659 props scribu.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r11930 r11943  
    512512 */
    513513function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) {
    514     if ( !$meta_key )
    515         return false;
    516 
    517     global $wpdb;
    518 
    519514    // make sure meta is added to the post, not a revision
    520515    if ( $the_post = wp_is_post_revision($post_id) )
    521516        $post_id = $the_post;
    522517
    523     // expected_slashed ($meta_key)
    524     $meta_key = stripslashes($meta_key);
    525 
    526     if ( $unique && $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) )
    527         return false;
    528 
    529     $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
    530 
    531     $wpdb->insert( $wpdb->postmeta, compact( 'post_id', 'meta_key', 'meta_value' ) );
    532 
    533     wp_cache_delete($post_id, 'post_meta');
    534 
    535     do_action( 'added_post_meta', $wpdb->insert_id, $post_id, $meta_key, $meta_value );
    536 
    537     return true;
     518    return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
    538519}
    539520
     
    555536 */
    556537function delete_post_meta($post_id, $meta_key, $meta_value = '') {
    557     global $wpdb;
    558 
    559538    // make sure meta is added to the post, not a revision
    560539    if ( $the_post = wp_is_post_revision($post_id) )
    561540        $post_id = $the_post;
    562541
    563     // expected_slashed ($meta_key, $meta_value)
    564     $meta_key = stripslashes( $meta_key );
    565     $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
    566 
    567     if ( !$meta_key )
    568         return false;
    569 
    570     if ( empty( $meta_value ) )
    571         $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $meta_key ) );
    572     else
    573         $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $meta_key, $meta_value ) );
    574 
    575     if ( !$meta_id )
    576         return false;
    577 
    578     do_action( 'delete_post_meta', $meta_id, $post_id, $meta_key, $meta_value );
    579 
    580     if ( empty( $meta_value ) )
    581         $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $meta_key ) );
    582     else
    583         $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $meta_key, $meta_value ) );
    584 
    585     wp_cache_delete($post_id, 'post_meta');
    586 
    587     do_action( 'deleted_post_meta', $meta_id, $post_id, $meta_key, $meta_value );
    588 
    589     return true;
     542    return delete_metadata('post', $post_id, $meta_key, $meta_value);
    590543}
    591544
     
    604557 */
    605558function get_post_meta($post_id, $key, $single = false) {
    606     if ( !$key )
    607         return '';
    608 
    609     $post_id = (int) $post_id;
    610 
    611     $meta_cache = wp_cache_get($post_id, 'post_meta');
    612 
    613     if ( !$meta_cache ) {
    614         update_postmeta_cache($post_id);
    615         $meta_cache = wp_cache_get($post_id, 'post_meta');
    616     }
    617 
    618     if ( isset($meta_cache[$key]) ) {
    619         if ( $single ) {
    620             return maybe_unserialize( $meta_cache[$key][0] );
    621         } else {
    622             return array_map('maybe_unserialize', $meta_cache[$key]);
    623         }
    624     }
    625 
    626     return '';
     559    return get_metadata('post', $post_id, $key, $single);
    627560}
    628561
     
    646579 */
    647580function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
    648     global $wpdb;
    649 
    650581    // make sure meta is added to the post, not a revision
    651582    if ( $the_post = wp_is_post_revision($post_id) )
    652583        $post_id = $the_post;
    653584
    654     // expected_slashed ($meta_key)
    655     $meta_key = stripslashes($meta_key);
    656 
    657     if ( !$meta_key )
    658         return false;
    659 
    660     $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) );
    661     if ( ! $meta_id )
    662         return add_post_meta($post_id, $meta_key, $meta_value);
    663 
    664     $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
    665 
    666     $data  = compact( 'meta_value' );
    667     $where = compact( 'meta_key', 'post_id' );
    668 
    669     if ( !empty( $prev_value ) ) {
    670         $prev_value = maybe_serialize($prev_value);
    671         $where['meta_value'] = $prev_value;
    672     }
    673 
    674     do_action( 'update_post_meta', $meta_id, $post_id, $meta_key, $meta_value );
    675 
    676     $wpdb->update( $wpdb->postmeta, $data, $where );
    677     wp_cache_delete($post_id, 'post_meta');
    678 
    679     do_action( 'updated_post_meta', $meta_id, $post_id, $meta_key, $meta_value );
    680 
    681     return true;
     585    return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);
    682586}
    683587
     
    33053209 */
    33063210function update_postmeta_cache($post_ids) {
    3307     global $wpdb;
    3308 
    3309     if ( empty( $post_ids ) )
    3310         return false;
    3311 
    3312     if ( !is_array($post_ids) ) {
    3313         $post_ids = preg_replace('|[^0-9,]|', '', $post_ids);
    3314         $post_ids = explode(',', $post_ids);
    3315     }
    3316 
    3317     $post_ids = array_map('intval', $post_ids);
    3318 
    3319     $ids = array();
    3320     foreach ( (array) $post_ids as $id ) {
    3321         if ( false === wp_cache_get($id, 'post_meta') )
    3322             $ids[] = $id;
    3323     }
    3324 
    3325     if ( empty( $ids ) )
    3326         return false;
    3327 
    3328     // Get post-meta info
    3329     $id_list = join(',', $ids);
    3330     $cache = array();
    3331     if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN ($id_list)", ARRAY_A) ) {
    3332         foreach ( (array) $meta_list as $metarow) {
    3333             $mpid = (int) $metarow['post_id'];
    3334             $mkey = $metarow['meta_key'];
    3335             $mval = $metarow['meta_value'];
    3336 
    3337             // Force subkeys to be array type:
    3338             if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
    3339                 $cache[$mpid] = array();
    3340             if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
    3341                 $cache[$mpid][$mkey] = array();
    3342 
    3343             // Add a value to the current pid/key:
    3344             $cache[$mpid][$mkey][] = $mval;
    3345         }
    3346     }
    3347 
    3348     foreach ( (array) $ids as $id ) {
    3349         if ( ! isset($cache[$id]) )
    3350             $cache[$id] = array();
    3351     }
    3352 
    3353     foreach ( (array) array_keys($cache) as $post)
    3354         wp_cache_set($post, $cache[$post], 'post_meta');
    3355 
    3356     return $cache;
     3211    return update_meta_cache('post', $post_ids);
    33573212}
    33583213
Note: See TracChangeset for help on using the changeset viewer.