Make WordPress Core

Ticket #2659: comment_meta.diff

File comment_meta.diff, 17.8 KB (added by scribu, 14 years ago)

Patch for 2.9 (uses common api for posts and comments)

  • wp-includes/post.php

     
    511511 * @return bool False for failure. True for success.
    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
    540521/**
     
    554535 * @return bool False for failure. True for success.
    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
    592545/**
     
    603556 *  is true.
    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
    629562/**
     
    645578 * @return bool False on failure, true if success.
    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
    684588/**
     
    33043208 * @return bool|array Returns false if there is nothing to update or an array of metadata.
    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
    33593214//
  • wp-includes/version.php

     
    1515 *
    1616 * @global int $wp_db_version
    1717 */
    18 $wp_db_version = 11557;
     18$wp_db_version = 11558;
    1919
    2020/**
    2121 * Holds the TinyMCE version
  • wp-includes/comment.php

     
    361361        return $comment_count;
    362362}
    363363
     364//
     365// Comment meta functions
     366//
     367
    364368/**
     369 * Add meta data field to a comment.
     370 *
     371 * Post meta data is called "Custom Fields" on the Administration Panels.
     372 *
     373 * @since 2.9
     374 * @uses add_metadata
     375 * @link http://codex.wordpress.org/Function_Reference/add_comment_meta
     376 *
     377 * @param int $comment_id Post ID.
     378 * @param string $key Metadata name.
     379 * @param mixed $value Metadata value.
     380 * @param bool $unique Optional, default is false. Whether the same key should not be added.
     381 * @return bool False for failure. True for success.
     382 */
     383function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
     384        return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
     385}
     386
     387/**
     388 * Remove metadata matching criteria from a comment.
     389 *
     390 * You can match based on the key, or key and value. Removing based on key and
     391 * value, will keep from removing duplicate metadata with the same key. It also
     392 * allows removing all metadata matching key, if needed.
     393 *
     394 * @since 2.9
     395 * @uses delete_metadata
     396 * @link http://codex.wordpress.org/Function_Reference/delete_comment_meta
     397 *
     398 * @param int $comment_id comment ID
     399 * @param string $meta_key Metadata name.
     400 * @param mixed $meta_value Optional. Metadata value.
     401 * @return bool False for failure. True for success.
     402 */
     403function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
     404        return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
     405}
     406
     407/**
     408 * Retrieve comment meta field for a comment.
     409 *
     410 * @since 2.9
     411 * @uses get_metadata
     412 * @link http://codex.wordpress.org/Function_Reference/get_comment_meta
     413 *
     414 * @param int $comment_id Post ID.
     415 * @param string $key The meta key to retrieve.
     416 * @param bool $single Whether to return a single value.
     417 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
     418 *  is true.
     419 */
     420function get_comment_meta($comment_id, $key, $single = false) {
     421        return get_metadata('comment', $comment_id, $key, $single);
     422}
     423
     424/**
     425 * Update comment meta field based on comment ID.
     426 *
     427 * Use the $prev_value parameter to differentiate between meta fields with the
     428 * same key and comment ID.
     429 *
     430 * If the meta field for the comment does not exist, it will be added.
     431 *
     432 * @since 2.9
     433 * @uses update_metadata
     434 * @link http://codex.wordpress.org/Function_Reference/update_comment_meta
     435 *
     436 * @param int $comment_id Post ID.
     437 * @param string $key Metadata key.
     438 * @param mixed $value Metadata value.
     439 * @param mixed $prev_value Optional. Previous value to check before removing.
     440 * @return bool False on failure, true if success.
     441 */
     442function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
     443        return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
     444}
     445
     446/**
    365447 * Sanitizes the cookies sent to the user already.
    366448 *
    367449 * Will only do anything if the cookies have already been created for the user.
  • wp-includes/wp-db.php

     
    155155        var $users;
    156156
    157157        /**
    158          * WordPress Categories table
    159          *
    160          * @since 1.5.0
    161          * @access public
    162          * @var string
    163          */
    164         var $categories;
    165 
    166         /**
    167          * WordPress Post to Category table
    168          *
    169          * @since 1.5.0
    170          * @access public
    171          * @var string
    172          */
    173         var $post2cat;
    174 
    175         /**
    176158         * WordPress Comments table
    177159         *
    178160         * @since 1.5.0
     
    209191        var $postmeta;
    210192
    211193        /**
     194         * WordPress Comment Metadata table
     195         *
     196         * @since 2.9
     197         * @access public
     198         * @var string
     199         */
     200        var $commentmeta;
     201
     202        /**
    212203         * WordPress User Metadata table
    213204         *
    214205         * @since 2.3.0
     
    251242         * @access private
    252243         * @var array
    253244         */
    254         var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
    255                         'postmeta', 'terms', 'term_taxonomy', 'term_relationships');
     245        var $tables = array('users', 'usermeta', 'posts', 'postmeta', 'comments', 'commentmeta',
     246                'links', 'options',     'terms', 'term_taxonomy', 'term_relationships');
    256247
    257248        /**
    258249         * List of deprecated WordPress tables
  • wp-includes/meta.php

     
     1<?php
     2/**
     3 * Meta API
     4 *
     5 * Functions for retrieving and manipulating metadata
     6 *
     7 * @package WordPress
     8 */
     9
     10function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {
     11        if ( !$meta_type || !$meta_key )
     12                return false;
     13
     14        if ( ! $table = _get_meta_table($meta_type) )
     15                return false;
     16
     17        global $wpdb;
     18
     19        $column = esc_sql($meta_type . '_id');
     20
     21        // expected_slashed ($meta_key)
     22        $meta_key = stripslashes($meta_key);
     23
     24        if ( $unique && $wpdb->get_var( $wpdb->prepare(
     25                "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
     26                $meta_key, $object_id ) ) )
     27                return false;
     28
     29        $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
     30
     31        $wpdb->insert( $table, array(
     32                $column => $object_id,
     33                'meta_key' => $meta_key,
     34                'meta_value' => $meta_value
     35        ) );
     36
     37        wp_cache_delete($object_id, $meta_type . '_meta');
     38
     39        do_action( "added_{$meta_type}_meta", $wpdb->insert_id, $object_id, $meta_key, $meta_value );
     40
     41        return true;
     42}
     43
     44function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {
     45        if ( !$meta_type || !$meta_key )
     46                return false;
     47
     48        if ( ! $table = _get_meta_table($meta_type) )
     49                return false;
     50
     51        global $wpdb;
     52
     53        $column = esc_sql($meta_type . '_id');
     54
     55        // expected_slashed ($meta_key)
     56        $meta_key = stripslashes($meta_key);
     57
     58        if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
     59                return add_metadata($meta_type, $object_id, $meta_key, $meta_value);
     60
     61        $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
     62
     63        $data  = compact( 'meta_value' );
     64        $where = array( $column => $object_id, 'meta_key' => $meta_key );
     65
     66        if ( !empty( $prev_value ) ) {
     67                $prev_value = maybe_serialize($prev_value);
     68                $where['meta_value'] = $prev_value;
     69        }
     70
     71        do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $meta_value );
     72
     73        $wpdb->update( $table, $data, $where );
     74        wp_cache_delete($object_id, $meta_type . '_meta');
     75
     76        do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $meta_value );
     77
     78        return true;
     79}
     80
     81function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '') {
     82        if ( !$meta_type || !$meta_key )
     83                return false;
     84
     85        if ( ! $table = _get_meta_table($meta_type) )
     86                return false;
     87
     88        global $wpdb;
     89
     90        $column = esc_sql($meta_type . '_id');
     91
     92        // expected_slashed ($meta_key)
     93        $meta_key = stripslashes($meta_key);
     94        $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
     95
     96        $query = $wpdb->prepare( "DELETE FROM $table WHERE meta_key = %s", $meta_key );
     97
     98        if ( $meta_value )
     99                $query .= $wpdb->prepare("AND meta_value = %s", $meta_value );
     100       
     101        $count = $wpdb->query($query);
     102       
     103        if ( !$count )
     104                return false;
     105
     106        wp_cache_delete($object_id, $meta_type . '_meta');
     107
     108        do_action( "deleted_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $meta_value );
     109
     110        return true;
     111}
     112
     113function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
     114        if ( !$meta_type )
     115                return false;
     116
     117        $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
     118
     119        if ( !$meta_cache ) {
     120                update_meta_cache($meta_type, $object_id);
     121                $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
     122        }
     123
     124        if ( ! $meta_key )
     125                return $meta_cache;
     126
     127        if ( isset($meta_cache[$meta_key]) ) {
     128                if ( $single ) {
     129                        return maybe_unserialize( $meta_cache[$meta_key][0] );
     130                } else {
     131                        return array_map('maybe_unserialize', $meta_cache[$meta_key]);
     132                }
     133        }
     134
     135        return false;
     136}
     137
     138function update_meta_cache($meta_type, $object_ids) {
     139        if ( empty( $meta_type ) || empty( $object_ids ) )
     140                return false;
     141
     142        if ( ! $table = _get_meta_table($meta_type) )
     143                return false;
     144
     145        $column = esc_sql($meta_type . '_id');
     146
     147        global $wpdb;
     148
     149        if ( !is_array($object_ids) ) {
     150                $object_ids = preg_replace('|[^0-9,]|', '', $object_ids);
     151                $object_ids = explode(',', $object_ids);
     152        }
     153
     154        $object_ids = array_map('intval', $object_ids);
     155
     156        $cache_key = $meta_type . '_meta';
     157        $ids = array();
     158        foreach ( $object_ids as $id ) {
     159                if ( false === wp_cache_get($id, $cache_key) )
     160                        $ids[] = $id;
     161        }
     162
     163        if ( empty( $ids ) )
     164                return false;
     165
     166        // Get meta info
     167        $id_list = join(',', $ids);
     168        $cache = array();
     169        $meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)",
     170                $meta_type), ARRAY_A );
     171
     172        if ( !empty($meta_list) ) {
     173                foreach ( $meta_list as $metarow) {
     174                        $mpid = intval($metarow[$column]);
     175                        $mkey = $metarow['meta_key'];
     176                        $mval = $metarow['meta_value'];
     177
     178                        // Force subkeys to be array type:
     179                        if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
     180                                $cache[$mpid] = array();
     181                        if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
     182                                $cache[$mpid][$mkey] = array();
     183
     184                        // Add a value to the current pid/key:
     185                        $cache[$mpid][$mkey][] = $mval;
     186                }
     187        }
     188
     189        foreach ( $ids as $id ) {
     190                if ( ! isset($cache[$id]) )
     191                        $cache[$id] = array();
     192        }
     193
     194        foreach ( array_keys($cache) as $object)
     195                wp_cache_set($object, $cache[$object], $cache_key);
     196
     197        return $cache;
     198}
     199
     200function _get_meta_table($type) {
     201        global $wpdb;
     202
     203        $table_name = $type . 'meta';
     204
     205        if ( empty($wpdb->$table_name) )
     206                return false;
     207
     208        return $wpdb->$table_name;
     209}
     210
  • wp-settings.php

     
    332332require (ABSPATH . WPINC . '/query.php');
    333333require (ABSPATH . WPINC . '/theme.php');
    334334require (ABSPATH . WPINC . '/user.php');
     335require (ABSPATH . WPINC . '/meta.php');
    335336require (ABSPATH . WPINC . '/general-template.php');
    336337require (ABSPATH . WPINC . '/link-template.php');
    337338require (ABSPATH . WPINC . '/author-template.php');
  • wp-admin/includes/schema.php

     
    111111  KEY post_id (post_id),
    112112  KEY meta_key (meta_key)
    113113) $charset_collate;
     114CREATE TABLE $wpdb->commentmeta (
     115  meta_id bigint(20) unsigned NOT NULL auto_increment,
     116  comment_id bigint(20) unsigned NOT NULL default '0',
     117  meta_key varchar(255) default NULL,
     118  meta_value longtext,
     119  PRIMARY KEY  (meta_id),
     120  KEY post_id (comment_id),
     121  KEY meta_key (meta_key)
     122) $charset_collate;
    114123CREATE TABLE $wpdb->posts (
    115124  ID bigint(20) unsigned NOT NULL auto_increment,
    116125  post_author bigint(20) unsigned NOT NULL default '0',
  • wp-admin/includes/comment.php

     
    130130        return $pending_keyed;
    131131}
    132132
     133
    133134/**
    134135 * Add avatars to relevant places in admin, or try to.
    135136 *
     
    164165                add_filter( 'comment_author', 'floated_admin_avatar' );
    165166}
    166167
    167 ?>