Ticket #44467: 44467.4.diff
File 44467.4.diff, 19.4 KB (added by , 7 years ago) |
---|
-
src/wp-includes/comment.php
422 422 * @param bool $unique Optional, default is false. Whether the same key should not be added. 423 423 * @return int|bool Meta ID on success, false on failure. 424 424 */ 425 function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) { 426 $added = add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique ); 427 if ( $added ) { 428 wp_cache_set( 'last_changed', microtime(), 'comment' ); 429 } 430 return $added; 425 function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) { 426 return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique ); 431 427 } 432 428 433 429 /** … … 445 441 * @param mixed $meta_value Optional. Metadata value. 446 442 * @return bool True on success, false on failure. 447 443 */ 448 function delete_comment_meta($comment_id, $meta_key, $meta_value = '') { 449 $deleted = delete_metadata( 'comment', $comment_id, $meta_key, $meta_value ); 450 if ( $deleted ) { 451 wp_cache_set( 'last_changed', microtime(), 'comment' ); 452 } 453 return $deleted; 444 function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) { 445 return delete_metadata( 'comment', $comment_id, $meta_key, $meta_value ); 454 446 } 455 447 456 448 /** … … 465 457 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single 466 458 * is true. 467 459 */ 468 function get_comment_meta( $comment_id, $key = '', $single = false) {469 return get_metadata( 'comment', $comment_id, $key, $single);460 function get_comment_meta( $comment_id, $key = '', $single = false ) { 461 return get_metadata( 'comment', $comment_id, $key, $single ); 470 462 } 471 463 472 464 /** … … 486 478 * @param mixed $prev_value Optional. Previous value to check before removing. 487 479 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. 488 480 */ 489 function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') { 490 $updated = update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value ); 491 if ( $updated ) { 492 wp_cache_set( 'last_changed', microtime(), 'comment' ); 493 } 494 return $updated; 481 function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) { 482 return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value ); 495 483 } 496 484 497 485 /** … … 3061 3049 * @param int $comment_post_ID Post ID. 3062 3050 */ 3063 3051 do_action( 'comment_on_draft', $comment_post_ID ); 3064 3052 3065 3053 if ( current_user_can( 'read_post', $comment_post_ID ) ) { 3066 3054 return new WP_Error( 'comment_on_draft', __( 'Sorry, comments are not allowed for this item.' ), 403 ); 3067 3055 } else { … … 3387 3375 'done' => $done, 3388 3376 ); 3389 3377 } 3378 3379 /** 3380 * Sets the last changed time for the 'comment' cache group. 3381 * 3382 * @since 5.0.0 3383 */ 3384 function wp_cache_set_comments_last_changed() { 3385 wp_cache_set( 'last_changed', microtime(), 'comment' ); 3386 } -
src/wp-includes/default-filters.php
89 89 // Meta 90 90 add_filter( 'register_meta_args', '_wp_register_meta_args_whitelist', 10, 2 ); 91 91 92 // Post meta 93 add_action( 'added_post_meta', 'wp_cache_set_posts_last_changed' ); 94 add_action( 'updated_post_meta', 'wp_cache_set_posts_last_changed' ); 95 add_action( 'deleted_post_meta', 'wp_cache_set_posts_last_changed' ); 96 97 // Term meta 98 add_action( 'added_term_meta', 'wp_cache_set_terms_last_changed' ); 99 add_action( 'updated_term_meta', 'wp_cache_set_terms_last_changed' ); 100 add_action( 'deleted_term_meta', 'wp_cache_set_terms_last_changed' ); 101 add_filter( 'get_term_metadata', 'wp_check_term_meta_support_prefilter' ); 102 add_filter( 'add_term_metadata', 'wp_check_term_meta_support_prefilter' ); 103 add_filter( 'update_term_metadata', 'wp_check_term_meta_support_prefilter' ); 104 add_filter( 'delete_term_metadata', 'wp_check_term_meta_support_prefilter' ); 105 add_filter( 'get_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' ); 106 add_filter( 'update_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' ); 107 add_filter( 'delete_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' ); 108 add_filter( 'update_term_metadata_cache', 'wp_check_term_meta_support_prefilter' ); 109 110 // Comment meta 111 add_action( 'added_comment_meta', 'wp_cache_set_comments_last_changed' ); 112 add_action( 'updated_comment_meta', 'wp_cache_set_comments_last_changed' ); 113 add_action( 'deleted_comment_meta', 'wp_cache_set_comments_last_changed' ); 114 92 115 // Places to balance tags on input 93 116 foreach ( array( 'content_save_pre', 'excerpt_save_pre', 'comment_save_pre', 'pre_comment_content' ) as $filter ) { 94 117 add_filter( $filter, 'convert_invalid_entities' ); -
src/wp-includes/meta.php
586 586 587 587 $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id'; 588 588 589 /** 590 * Filters whether to retrieve metadata of a specific type by meta ID. 591 * 592 * The dynamic portion of the hook, `$meta_type`, refers to the meta 593 * object type (comment, post, term, or user). Returning a non-null value 594 * will effectively short-circuit the function. 595 * 596 * @since 5.0.0 597 * 598 * @param mixed $value The value get_metadata_by_mid() should return. 599 * @param int $meta_id Meta ID. 600 */ 601 $check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id ); 602 if ( null !== $check ) { 603 return $check; 604 } 605 589 606 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); 590 607 591 608 if ( empty( $meta ) ) … … 631 648 $column = sanitize_key($meta_type . '_id'); 632 649 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 633 650 651 /** 652 * Filters whether to update metadata of a specific type by meta ID. 653 * 654 * The dynamic portion of the hook, `$meta_type`, refers to the meta 655 * object type (comment, post, term, or user). Returning a non-null value 656 * will effectively short-circuit the function. 657 * 658 * @since 5.0.0 659 * 660 * @param null|bool $check Whether to allow updating metadata for the given type. 661 * @param int $meta_id Meta ID. 662 * @param mixed $meta_value Meta value. Must be serializable if non-scalar. 663 * @param string|bool $meta_key Meta key, if provided. 664 */ 665 $check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key ); 666 if ( null !== $check ) { 667 return (bool) $check; 668 } 669 634 670 // Fetch the meta and go on if it's found. 635 671 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { 636 672 $original_key = $meta->meta_key; … … 725 761 $column = sanitize_key($meta_type . '_id'); 726 762 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 727 763 764 /** 765 * Filters whether to delete metadata of a specific type by meta ID. 766 * 767 * The dynamic portion of the hook, `$meta_type`, refers to the meta 768 * object type (comment, post, term, or user). Returning a non-null value 769 * will effectively short-circuit the function. 770 * 771 * @since 5.0.0 772 * 773 * @param null|bool $delete Whether to allow metadata deletion of the given type. 774 * @param int $meta_id Meta ID. 775 */ 776 $check = apply_filters( "delete_{$meta_type}_metadata_by_mid", null, $meta_id ); 777 if ( null !== $check ) { 778 return (bool) $check; 779 } 780 728 781 // Fetch the meta and go on if it's found. 729 782 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { 730 783 $object_id = $meta->{$column}; … … 811 864 812 865 $object_ids = array_map('intval', $object_ids); 813 866 867 /** 868 * Filters whether to update the metadata cache of a specific type. 869 * 870 * The dynamic portion of the hook, `$meta_type`, refers to the meta 871 * object type (comment, post, term, or user). Returning a non-null value 872 * will effectively short-circuit the function. 873 * 874 * @since 5.0.0 875 * 876 * @param mixed $check Whether to allow updating the meta cache of the given type. 877 * @param array $object_ids Array of object IDs to update the meta cache for. 878 */ 879 $check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids ); 880 if ( null !== $check ) { 881 return (bool) $check; 882 } 883 814 884 $cache_key = $meta_type . '_meta'; 815 885 $ids = array(); 816 886 $cache = array(); -
src/wp-includes/post.php
1787 1787 if ( $the_post = wp_is_post_revision($post_id) ) 1788 1788 $post_id = $the_post; 1789 1789 1790 $added = add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique ); 1791 if ( $added ) { 1792 wp_cache_set( 'last_changed', microtime(), 'posts' ); 1793 } 1794 return $added; 1790 return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique ); 1795 1791 } 1796 1792 1797 1793 /** … … 1814 1810 if ( $the_post = wp_is_post_revision($post_id) ) 1815 1811 $post_id = $the_post; 1816 1812 1817 $deleted = delete_metadata( 'post', $post_id, $meta_key, $meta_value ); 1818 if ( $deleted ) { 1819 wp_cache_set( 'last_changed', microtime(), 'posts' ); 1820 } 1821 return $deleted; 1813 return delete_metadata( 'post', $post_id, $meta_key, $meta_value ); 1822 1814 } 1823 1815 1824 1816 /** … … 1860 1852 if ( $the_post = wp_is_post_revision($post_id) ) 1861 1853 $post_id = $the_post; 1862 1854 1863 $updated = update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value ); 1864 if ( $updated ) { 1865 wp_cache_set( 'last_changed', microtime(), 'posts' ); 1866 } 1867 return $updated; 1855 return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value ); 1868 1856 } 1869 1857 1870 1858 /** … … 1876 1864 * @return bool Whether the post meta key was deleted from the database. 1877 1865 */ 1878 1866 function delete_post_meta_by_key( $post_meta_key ) { 1879 $deleted = delete_metadata( 'post', null, $post_meta_key, '', true ); 1880 if ( $deleted ) { 1881 wp_cache_set( 'last_changed', microtime(), 'posts' ); 1882 } 1883 return $deleted; 1867 return delete_metadata( 'post', null, $post_meta_key, '', true ); 1884 1868 } 1885 1869 1886 1870 /** … … 6475 6459 6476 6460 return $clauses; 6477 6461 } 6462 6463 /** 6464 * Sets the last changed time for the 'posts' cache group. 6465 * 6466 * @since 5.0.0 6467 */ 6468 function wp_cache_set_posts_last_changed() { 6469 wp_cache_set( 'last_changed', microtime(), 'posts' ); 6470 } -
src/wp-includes/taxonomy.php
1135 1135 * False on failure. 1136 1136 */ 1137 1137 function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) { 1138 // Bail if term meta table is not installed.1139 if ( get_option( 'db_version' ) < 34370 ) {1140 return false;1141 }1142 1143 1138 if ( wp_term_is_shared( $term_id ) ) { 1144 1139 return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id ); 1145 1140 } 1146 1141 1147 $added = add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique ); 1148 1149 // Bust term query cache. 1150 if ( $added ) { 1151 wp_cache_set( 'last_changed', microtime(), 'terms' ); 1152 } 1153 1154 return $added; 1142 return add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique ); 1155 1143 } 1156 1144 1157 1145 /** … … 1165 1153 * @return bool True on success, false on failure. 1166 1154 */ 1167 1155 function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) { 1168 // Bail if term meta table is not installed. 1169 if ( get_option( 'db_version' ) < 34370 ) { 1170 return false; 1171 } 1172 1173 $deleted = delete_metadata( 'term', $term_id, $meta_key, $meta_value ); 1174 1175 // Bust term query cache. 1176 if ( $deleted ) { 1177 wp_cache_set( 'last_changed', microtime(), 'terms' ); 1178 } 1179 1180 return $deleted; 1156 return delete_metadata( 'term', $term_id, $meta_key, $meta_value ); 1181 1157 } 1182 1158 1183 1159 /** … … 1192 1168 * @return mixed If `$single` is false, an array of metadata values. If `$single` is true, a single metadata value. 1193 1169 */ 1194 1170 function get_term_meta( $term_id, $key = '', $single = false ) { 1195 // Bail if term meta table is not installed.1196 if ( get_option( 'db_version' ) < 34370 ) {1197 return false;1198 }1199 1200 1171 return get_metadata( 'term', $term_id, $key, $single ); 1201 1172 } 1202 1173 … … 1217 1188 * WP_Error when term_id is ambiguous between taxonomies. False on failure. 1218 1189 */ 1219 1190 function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) { 1220 // Bail if term meta table is not installed.1221 if ( get_option( 'db_version' ) < 34370 ) {1222 return false;1223 }1224 1225 1191 if ( wp_term_is_shared( $term_id ) ) { 1226 1192 return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id ); 1227 1193 } 1228 1194 1229 $updated = update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value ); 1230 1231 // Bust term query cache. 1232 if ( $updated ) { 1233 wp_cache_set( 'last_changed', microtime(), 'terms' ); 1234 } 1235 1236 return $updated; 1195 return update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value ); 1237 1196 } 1238 1197 1239 1198 /** … … 1248 1207 * @return array|false Returns false if there is nothing to update. Returns an array of metadata on success. 1249 1208 */ 1250 1209 function update_termmeta_cache( $term_ids ) { 1251 // Bail if term meta table is not installed.1252 if ( get_option( 'db_version' ) < 34370 ) {1253 return;1254 }1255 1256 1210 return update_meta_cache( 'term', $term_ids ); 1257 1211 } 1258 1212 … … 1267 1221 * @return array|false Array with meta data, or false when the meta table is not installed. 1268 1222 */ 1269 1223 function has_term_meta( $term_id ) { 1270 // Bail if term meta table is not installed.1271 if ( get_option( 'db_version' ) < 34370) {1272 return false;1224 $check = wp_check_term_meta_support_prefilter( null ); 1225 if ( null !== $check ) { 1226 return $check; 1273 1227 } 1274 1228 1275 1229 global $wpdb; … … 4341 4295 4342 4296 return $parent; 4343 4297 } 4298 4299 /** 4300 * Sets the last changed time for the 'terms' cache group. 4301 * 4302 * @since 5.0.0 4303 */ 4304 function wp_cache_set_terms_last_changed() { 4305 wp_cache_set( 'last_changed', microtime(), 'terms' ); 4306 } 4307 4308 /** 4309 * Aborts calls to term meta if it is not supported. 4310 * 4311 * @since 5.0.0 4312 * 4313 * @param mixed $check Skip-value for whether to proceed term meta function execution. 4314 * @return mixed Original value of $check, or false if term meta is not supported. 4315 */ 4316 function wp_check_term_meta_support_prefilter( $check ) { 4317 if ( get_option( 'db_version' ) < 34370 ) { 4318 return false; 4319 } 4320 4321 return $check; 4322 } -
tests/phpunit/tests/comment/metaCache.php
210 210 $num_queries++; 211 211 $this->assertSame( $num_queries, $wpdb->num_queries ); 212 212 } 213 214 /** 215 * @ticket 44467 216 */ 217 public function test_add_metadata_sets_comments_last_changed() { 218 $comment_id = self::factory()->comment->create(); 219 220 wp_cache_delete( 'last_changed', 'comment' ); 221 222 $this->assertInternalType( 'integer', add_metadata( 'comment', $comment_id, 'foo', 'bar' ) ); 223 $this->assertNotFalse( wp_cache_get_last_changed( 'comment' ) ); 224 } 225 226 /** 227 * @ticket 44467 228 */ 229 public function test_update_metadata_sets_comments_last_changed() { 230 $comment_id = self::factory()->comment->create(); 231 232 wp_cache_delete( 'last_changed', 'comment' ); 233 234 $this->assertInternalType( 'integer', update_metadata( 'comment', $comment_id, 'foo', 'bar' ) ); 235 $this->assertNotFalse( wp_cache_get_last_changed( 'comment' ) ); 236 } 237 238 /** 239 * @ticket 44467 240 */ 241 public function test_delete_metadata_sets_comments_last_changed() { 242 $comment_id = self::factory()->comment->create(); 243 244 update_metadata( 'comment', $comment_id, 'foo', 'bar' ); 245 wp_cache_delete( 'last_changed', 'comment' ); 246 247 $this->assertTrue( delete_metadata( 'comment', $comment_id, 'foo' ) ); 248 $this->assertNotFalse( wp_cache_get_last_changed( 'comment' ) ); 249 } 213 250 } -
tests/phpunit/tests/post/meta.php
305 305 array( '', 'registered_key3' ), 306 306 ); 307 307 } 308 309 /** 310 * @ticket 44467 311 */ 312 public function test_add_metadata_sets_posts_last_changed() { 313 $post_id = self::factory()->post->create(); 314 315 wp_cache_delete( 'last_changed', 'posts' ); 316 317 $this->assertInternalType( 'integer', add_metadata( 'post', $post_id, 'foo', 'bar' ) ); 318 $this->assertNotFalse( wp_cache_get_last_changed( 'posts' ) ); 319 } 320 321 /** 322 * @ticket 44467 323 */ 324 public function test_update_metadata_sets_posts_last_changed() { 325 $post_id = self::factory()->post->create(); 326 327 wp_cache_delete( 'last_changed', 'posts' ); 328 329 $this->assertInternalType( 'integer', update_metadata( 'post', $post_id, 'foo', 'bar' ) ); 330 $this->assertNotFalse( wp_cache_get_last_changed( 'posts' ) ); 331 } 332 333 /** 334 * @ticket 44467 335 */ 336 public function test_delete_metadata_sets_posts_last_changed() { 337 $post_id = self::factory()->post->create(); 338 339 update_metadata( 'post', $post_id, 'foo', 'bar' ); 340 wp_cache_delete( 'last_changed', 'posts' ); 341 342 $this->assertTrue( delete_metadata( 'post', $post_id, 'foo' ) ); 343 $this->assertNotFalse( wp_cache_get_last_changed( 'posts' ) ); 344 } 308 345 } -
tests/phpunit/tests/term/meta.php
509 509 array( '', 'registered_key3' ), 510 510 ); 511 511 } 512 513 /** 514 * @ticket 44467 515 */ 516 public function test_add_metadata_sets_terms_last_changed() { 517 $term_id = self::factory()->term->create(); 518 519 wp_cache_delete( 'last_changed', 'terms' ); 520 521 $this->assertInternalType( 'integer', add_metadata( 'term', $term_id, 'foo', 'bar' ) ); 522 $this->assertNotFalse( wp_cache_get_last_changed( 'terms' ) ); 523 } 524 525 /** 526 * @ticket 44467 527 */ 528 public function test_update_metadata_sets_terms_last_changed() { 529 $term_id = self::factory()->term->create(); 530 531 wp_cache_delete( 'last_changed', 'terms' ); 532 533 $this->assertInternalType( 'integer', update_metadata( 'term', $term_id, 'foo', 'bar' ) ); 534 $this->assertNotFalse( wp_cache_get_last_changed( 'terms' ) ); 535 } 536 537 /** 538 * @ticket 44467 539 */ 540 public function test_delete_metadata_sets_terms_last_changed() { 541 $term_id = self::factory()->term->create(); 542 543 update_metadata( 'term', $term_id, 'foo', 'bar' ); 544 wp_cache_delete( 'last_changed', 'terms' ); 545 546 $this->assertTrue( delete_metadata( 'term', $term_id, 'foo' ) ); 547 $this->assertNotFalse( wp_cache_get_last_changed( 'terms' ) ); 548 } 549 550 /** 551 * @ticket 44467 552 */ 553 public function test_metadata_functions_respect_term_meta_support() { 554 $term_id = self::factory()->term->create(); 555 556 $meta_id = add_metadata( 'term', $term_id, 'foo', 'bar' ); 557 558 // Set database version to last version before term meta support. 559 update_option( 'db_version', 34369 ); 560 561 $this->assertFalse( get_metadata( 'term', $term_id, 'foo', true ) ); 562 $this->assertFalse( add_metadata( 'term', $term_id, 'foo', 'bar' ) ); 563 $this->assertFalse( update_metadata( 'term', $term_id, 'foo', 'bar' ) ); 564 $this->assertFalse( delete_metadata( 'term', $term_id, 'foo' ) ); 565 $this->assertFalse( get_metadata_by_mid( 'term', $meta_id ) ); 566 $this->assertFalse( update_metadata_by_mid( 'term', $meta_id, 'baz' ) ); 567 $this->assertFalse( delete_metadata_by_mid( 'term', $meta_id ) ); 568 $this->assertFalse( update_meta_cache( 'term', array( $term_id ) ) ); 569 } 512 570 }