Make WordPress Core

Changeset 11943


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

First pass commentmeta implementation. See #2659 props scribu.

Location:
trunk
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/schema.php

    r11883 r11943  
    5454 PRIMARY KEY  (object_id,term_taxonomy_id),
    5555 KEY term_taxonomy_id (term_taxonomy_id)
     56) $charset_collate;
     57CREATE TABLE $wpdb->commentmeta (
     58  meta_id bigint(20) unsigned NOT NULL auto_increment,
     59  comment_id bigint(20) unsigned NOT NULL default '0',
     60  meta_key varchar(255) default NULL,
     61  meta_value longtext,
     62  PRIMARY KEY  (meta_id),
     63  KEY post_id (comment_id),
     64  KEY meta_key (meta_key)
    5665) $charset_collate;
    5766CREATE TABLE $wpdb->comments (
  • trunk/wp-includes/comment.php

    r11930 r11943  
    362362}
    363363
     364//
     365// Comment meta functions
     366//
     367
     368/**
     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
    364446/**
    365447 * Sanitizes the cookies sent to the user already.
  • 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
  • trunk/wp-includes/version.php

    r11883 r11943  
    1616 * @global int $wp_db_version
    1717 */
    18 $wp_db_version = 11557;
     18$wp_db_version = 11943;
    1919
    2020/**
  • trunk/wp-includes/wp-db.php

    r11930 r11943  
    210210
    211211    /**
     212     * WordPress Comment Metadata table
     213     *
     214     * @since 2.9
     215     * @access public
     216     * @var string
     217     */
     218    var $commentmeta;
     219
     220    /**
    212221     * WordPress User Metadata table
    213222     *
     
    253262     */
    254263    var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
    255             'postmeta', 'terms', 'term_taxonomy', 'term_relationships');
     264            'postmeta', 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta');
    256265
    257266    /**
  • trunk/wp-settings.php

    r11820 r11943  
    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');
Note: See TracChangeset for help on using the changeset viewer.