Ticket #10487: meta_api.diff
| File meta_api.diff, 14.0 KB (added by , 17 years ago) |
|---|
-
wp-includes/post.php
511 511 * @return bool False for failure. True for success. 512 512 */ 513 513 function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) { 514 if ( !$meta_key )515 return false;516 517 global $wpdb;518 519 514 // make sure meta is added to the post, not a revision 520 515 if ( $the_post = wp_is_post_revision($post_id) ) 521 516 $post_id = $the_post; 522 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 return true; 517 518 return add_metadata('post', $post_id, $meta_key, $meta_value, $unique); 536 519 } 537 520 538 521 /** … … 552 535 * @return bool False for failure. True for success. 553 536 */ 554 537 function delete_post_meta($post_id, $meta_key, $meta_value = '') { 555 global $wpdb;556 557 538 // make sure meta is added to the post, not a revision 558 539 if ( $the_post = wp_is_post_revision($post_id) ) 559 540 $post_id = $the_post; 560 541 561 // expected_slashed ($meta_key, $meta_value) 562 $meta_key = stripslashes( $meta_key ); 563 $meta_value = maybe_serialize( stripslashes_deep($meta_value) ); 564 565 if ( !$meta_key ) 566 return false; 567 568 if ( empty( $meta_value ) ) 569 $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 ) ); 570 else 571 $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 ) ); 572 573 if ( !$meta_id ) 574 return false; 575 576 if ( empty( $meta_value ) ) 577 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $meta_key ) ); 578 else 579 $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 ) ); 580 581 wp_cache_delete($post_id, 'post_meta'); 582 583 return true; 542 return delete_metadata('post', $post_id, $meta_key, $meta_value); 584 543 } 585 544 586 545 /** … … 597 556 * is true. 598 557 */ 599 558 function get_post_meta($post_id, $key, $single = false) { 600 if ( !$key ) 601 return ''; 602 603 $post_id = (int) $post_id; 604 605 $meta_cache = wp_cache_get($post_id, 'post_meta'); 606 607 if ( !$meta_cache ) { 608 update_postmeta_cache($post_id); 609 $meta_cache = wp_cache_get($post_id, 'post_meta'); 610 } 611 612 if ( isset($meta_cache[$key]) ) { 613 if ( $single ) { 614 return maybe_unserialize( $meta_cache[$key][0] ); 615 } else { 616 return array_map('maybe_unserialize', $meta_cache[$key]); 617 } 618 } 619 620 return ''; 559 return get_metadata('post', $post_id, $key, $single); 621 560 } 622 561 623 562 /** … … 639 578 * @return bool False on failure, true if success. 640 579 */ 641 580 function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') { 642 global $wpdb;643 644 581 // make sure meta is added to the post, not a revision 645 582 if ( $the_post = wp_is_post_revision($post_id) ) 646 583 $post_id = $the_post; 647 584 648 // expected_slashed ($meta_key) 649 $meta_key = stripslashes($meta_key); 650 651 if ( !$meta_key ) 652 return false; 653 654 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) ) { 655 return add_post_meta($post_id, $meta_key, $meta_value); 656 } 657 658 $meta_value = maybe_serialize( stripslashes_deep($meta_value) ); 659 660 $data = compact( 'meta_value' ); 661 $where = compact( 'meta_key', 'post_id' ); 662 663 if ( !empty( $prev_value ) ) { 664 $prev_value = maybe_serialize($prev_value); 665 $where['meta_value'] = $prev_value; 666 } 667 668 $wpdb->update( $wpdb->postmeta, $data, $where ); 669 wp_cache_delete($post_id, 'post_meta'); 670 return true; 585 return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value); 671 586 } 672 587 673 588 /** … … 717 632 718 633 $post_id = (int) $post_id; 719 634 720 if ( ! wp_cache_get($post_id, 'post_meta') ) 721 update_postmeta_cache($post_id); 722 723 return wp_cache_get($post_id, 'post_meta'); 635 return get_metadata('post', $post_id); 724 636 } 725 637 726 638 /** … … 3197 3109 * @return bool|array Returns false if there is nothing to update or an array of metadata. 3198 3110 */ 3199 3111 function update_postmeta_cache($post_ids) { 3200 global $wpdb; 3201 3202 if ( empty( $post_ids ) ) 3203 return false; 3204 3205 if ( !is_array($post_ids) ) { 3206 $post_ids = preg_replace('|[^0-9,]|', '', $post_ids); 3207 $post_ids = explode(',', $post_ids); 3208 } 3209 3210 $post_ids = array_map('intval', $post_ids); 3211 3212 $ids = array(); 3213 foreach ( (array) $post_ids as $id ) { 3214 if ( false === wp_cache_get($id, 'post_meta') ) 3215 $ids[] = $id; 3216 } 3217 3218 if ( empty( $ids ) ) 3219 return false; 3220 3221 // Get post-meta info 3222 $id_list = join(',', $ids); 3223 $cache = array(); 3224 if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN ($id_list)", ARRAY_A) ) { 3225 foreach ( (array) $meta_list as $metarow) { 3226 $mpid = (int) $metarow['post_id']; 3227 $mkey = $metarow['meta_key']; 3228 $mval = $metarow['meta_value']; 3229 3230 // Force subkeys to be array type: 3231 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) ) 3232 $cache[$mpid] = array(); 3233 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) ) 3234 $cache[$mpid][$mkey] = array(); 3235 3236 // Add a value to the current pid/key: 3237 $cache[$mpid][$mkey][] = $mval; 3238 } 3239 } 3240 3241 foreach ( (array) $ids as $id ) { 3242 if ( ! isset($cache[$id]) ) 3243 $cache[$id] = array(); 3244 } 3245 3246 foreach ( (array) array_keys($cache) as $post) 3247 wp_cache_set($post, $cache[$post], 'post_meta'); 3248 3249 return $cache; 3112 update_meta_cache('post', $post_ids); 3250 3113 } 3251 3114 3252 3115 // -
wp-includes/version.php
15 15 * 16 16 * @global int $wp_db_version 17 17 */ 18 $wp_db_version = 1154 8;18 $wp_db_version = 11549; 19 19 20 20 /** 21 21 * Holds the TinyMCE version -
wp-includes/wp-db.php
200 200 var $options; 201 201 202 202 /** 203 * WordPress PostMetadata table203 * WordPress Metadata table 204 204 * 205 * @since {@internal Version Unknown}}205 * @since 2.9.0 206 206 * @access public 207 207 * @var string 208 208 */ 209 var $ postmeta;209 var $meta; 210 210 211 211 /** 212 212 * WordPress User Metadata table … … 252 252 * @var array 253 253 */ 254 254 var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options', 255 ' postmeta', 'terms', 'term_taxonomy', 'term_relationships');255 'meta', 'terms', 'term_taxonomy', 'term_relationships'); 256 256 257 257 /** 258 258 * Format specifiers for DB columns. Columns not listed here default to %s. Initialized in wp-settings.php. -
wp-includes/meta.php
1 <?php 2 /** 3 * Meta API 4 * 5 * Functions for retrieving and manipulating metadata 6 * 7 * @package WordPress 8 */ 9 10 function register_meta_type($meta_type) { 11 if ( ! $meta_type ) 12 return false; 13 14 global $wpdb; 15 16 $data = $wpdb->get_row("SHOW COLUMNS FROM $wpdb->meta like 'meta_type'"); 17 18 $types = explode(',', substr($data->Type, 5, -1)); 19 foreach ( $types as $i => $type ) 20 $types[$i] = trim($type, "'"); 21 22 if ( in_array($meta_type, $types) ) 23 return true; 24 25 array_push($types, $meta_type); 26 sort($types); 27 28 foreach ( $types as $i => $type ) 29 $types[$i] = "'$type'"; 30 $types = implode(',', $types); 31 32 return $wpdb->query("ALTER TABLE $wpdb->meta CHANGE meta_type meta_type ENUM($types) NOT NULL"); 33 } 34 35 function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) { 36 if ( !$meta_type ) 37 return false; 38 39 if ( !$meta_key ) 40 return false; 41 42 global $wpdb; 43 44 // expected_slashed ($meta_key) 45 $meta_key = stripslashes($meta_key); 46 47 if ( $unique && $wpdb->get_var( $wpdb->prepare( 48 "SELECT meta_key FROM $wpdb->meta WHERE meta_type = %s AND meta_key = %s AND object_id = %d", 49 $meta_type, $meta_key, $object_id ) ) ) 50 return false; 51 52 $meta_value = maybe_serialize( stripslashes_deep($meta_value) ); 53 54 $wpdb->insert( $wpdb->meta, compact( 'meta_type', 'object_id', 'meta_key', 'meta_value' ) ); 55 56 wp_cache_delete($object_id, $meta_type . '_meta'); 57 58 return true; 59 } 60 61 function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') { 62 if ( !$meta_type ) 63 return false; 64 65 if ( !$meta_key ) 66 return false; 67 68 global $wpdb; 69 70 // expected_slashed ($meta_key) 71 $meta_key = stripslashes($meta_key); 72 73 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->meta WHERE meta_type = %s AND meta_key = %s AND object_id = %d", $meta_key, $object_id ) ) ) { 74 return add_metadata($meta_type, $object_id, $meta_key, $meta_value); 75 } 76 77 $meta_value = maybe_serialize( stripslashes_deep($meta_value) ); 78 79 $data = compact( 'meta_value' ); 80 $where = compact( 'meta_type', 'meta_key', 'object_id' ); 81 82 if ( !empty( $prev_value ) ) { 83 $prev_value = maybe_serialize($prev_value); 84 $where['meta_value'] = $prev_value; 85 } 86 87 $wpdb->update( $wpdb->meta, $data, $where ); 88 wp_cache_delete($object_id, $meta_type . '_meta'); 89 return true; 90 } 91 92 function delete_metadata($meta_type, $object_id, $meta_key = '', $meta_value = '') { 93 if ( !$meta_type ) 94 return false; 95 96 global $wpdb; 97 98 // expected_slashed ($meta_key, $meta_value) 99 $meta_key = stripslashes( $meta_key ); 100 $meta_value = maybe_serialize( stripslashes_deep($meta_value) ); 101 102 if ( empty( $meta_value ) ) 103 $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->meta WHERE meta_type = %s AND object_id = %d AND meta_key = %s", $meta_type, $object_id, $meta_key ) ); 104 else 105 $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->meta WHERE meta_type = %s AND object_id = %d AND meta_key = %s AND meta_value = %s", $meta_type, $object_id, $meta_key, $meta_value ) ); 106 107 if ( !$meta_id ) 108 return false; 109 110 $query = $wpdb->prepare( "DELETE FROM $wpdb->meta WHERE meta_type = %s", $meta_type ); 111 112 if ( $meta_key ) 113 $query .= $wpdb->prepare("AND meta_key = %s", $meta_key ); 114 115 if ( $meta_value ) 116 $query .= $wpdb->prepare("AND meta_value = %s", $meta_value ); 117 118 $wpdb->query($query); 119 120 wp_cache_delete($object_id, $meta_type . '_meta'); 121 122 return true; 123 } 124 125 function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) { 126 if ( !$meta_type ) 127 return ''; 128 129 $meta_cache = wp_cache_get($object_id, $meta_type . '_meta'); 130 131 if ( !$meta_cache ) { 132 update_meta_cache($meta_type, $object_id); 133 $meta_cache = wp_cache_get($object_id, $meta_type . '_meta'); 134 } 135 136 if ( ! $meta_key ) 137 return $meta_cache; 138 139 if ( isset($meta_cache[$meta_key]) ) { 140 if ( $single ) { 141 return maybe_unserialize( $meta_cache[$meta_key][0] ); 142 } else { 143 return array_map('maybe_unserialize', $meta_cache[$meta_key]); 144 } 145 } 146 147 return ''; 148 } 149 150 function update_meta_cache($meta_type, $object_ids) { 151 global $wpdb; 152 153 if ( empty( $meta_type ) ) 154 return false; 155 156 if ( empty( $object_ids ) ) 157 return false; 158 159 if ( !is_array($object_ids) ) { 160 $object_ids = preg_replace('|[^0-9,]|', '', $object_ids); 161 $object_ids = explode(',', $object_ids); 162 } 163 164 $object_ids = array_map('intval', $object_ids); 165 166 $cache_key = $meta_type . '_meta'; 167 $ids = array(); 168 foreach ( (array) $object_ids as $id ) { 169 if ( false === wp_cache_get($id, $cache_key) ) 170 $ids[] = $id; 171 } 172 173 if ( empty( $ids ) ) 174 return false; 175 176 // Get meta info 177 $id_list = join(',', $ids); 178 $cache = array(); 179 if ( $meta_list = $wpdb->get_results( $wpdb->prepare( 180 "SELECT object_id, meta_key, meta_value FROM $wpdb->meta WHERE meta_type = %s AND object_id IN ($id_list)", 181 $meta_type), ARRAY_A ) ) { 182 foreach ( (array) $meta_list as $metarow) { 183 $mpid = (int) $metarow['object_id']; 184 $mkey = $metarow['meta_key']; 185 $mval = $metarow['meta_value']; 186 187 // Force subkeys to be array type: 188 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) ) 189 $cache[$mpid] = array(); 190 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) ) 191 $cache[$mpid][$mkey] = array(); 192 193 // Add a value to the current pid/key: 194 $cache[$mpid][$mkey][] = $mval; 195 } 196 } 197 198 foreach ( (array) $ids as $id ) { 199 if ( ! isset($cache[$id]) ) 200 $cache[$id] = array(); 201 } 202 203 foreach ( (array) array_keys($cache) as $object) 204 wp_cache_set($object, $cache[$object], $cache_key); 205 206 return $cache; 207 } 208 -
wp-settings.php
334 334 require (ABSPATH . WPINC . '/query.php'); 335 335 require (ABSPATH . WPINC . '/theme.php'); 336 336 require (ABSPATH . WPINC . '/user.php'); 337 require (ABSPATH . WPINC . '/meta.php'); 337 338 require (ABSPATH . WPINC . '/general-template.php'); 338 339 require (ABSPATH . WPINC . '/link-template.php'); 339 340 require (ABSPATH . WPINC . '/author-template.php'); -
wp-admin/includes/schema.php
102 102 PRIMARY KEY (option_id,blog_id,option_name), 103 103 KEY option_name (option_name) 104 104 ) $charset_collate; 105 CREATE TABLE $wpdb->postmeta ( 105 CREATE TABLE $wpdb->meta ( 106 object_id bigint(20) unsigned NOT NULL default '0', 106 107 meta_id bigint(20) unsigned NOT NULL auto_increment, 107 post_id bigint(20) unsigned NOT NULL default '0',108 meta_type ENUM('post') NOT NULL, 108 109 meta_key varchar(255) default NULL, 109 110 meta_value longtext, 110 111 PRIMARY KEY (meta_id), 111 KEY post_id (post_id),112 KEY (meta_type,object_id), 112 113 KEY meta_key (meta_key) 113 114 ) $charset_collate; 114 115 CREATE TABLE $wpdb->posts (
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)