Make WordPress Core

Changeset 43729 for branches/5.0


Ignore:
Timestamp:
10/15/2018 11:45:16 AM (8 years ago)
Author:
flixos90
Message:

REST API: Move object type-specific metadata integrations from the wrapper functions to the low-level Meta API functions.

Object type-specific actions that should happen before or after modification of metadata have so far been part of the respective wrapper functions. By using action and filter hooks, this changeset ensures they are always executed, even when calling the lower-level Meta API functions directly, which the REST API does as a prime example.

Props flixos90, spacedmonkey.
Fixes #44467.

Location:
branches/5.0
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-includes/comment.php

    r43468 r43729  
    423423 * @return int|bool Meta ID on success, false on failure.
    424424 */
    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;
     425function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) {
     426        return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
    431427}
    432428
     
    446442 * @return bool True on success, false on failure.
    447443 */
    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;
     444function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) {
     445        return delete_metadata( 'comment', $comment_id, $meta_key, $meta_value );
    454446}
    455447
     
    466458 *  is true.
    467459 */
    468 function get_comment_meta($comment_id, $key = '', $single = false) {
    469         return get_metadata('comment', $comment_id, $key, $single);
     460function get_comment_meta( $comment_id, $key = '', $single = false ) {
     461        return get_metadata( 'comment', $comment_id, $key, $single );
    470462}
    471463
     
    487479 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
    488480 */
    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;
     481function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) {
     482        return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value );
    495483}
    496484
     
    30623050                 */
    30633051                do_action( 'comment_on_draft', $comment_post_ID );
    3064                
     3052
    30653053                if ( current_user_can( 'read_post', $comment_post_ID ) ) {
    30663054                        return new WP_Error( 'comment_on_draft', __( 'Sorry, comments are not allowed for this item.' ), 403 );
     
    33883376        );
    33893377}
     3378
     3379/**
     3380 * Sets the last changed time for the 'comment' cache group.
     3381 *
     3382 * @since 5.0.0
     3383 */
     3384function wp_cache_set_comments_last_changed() {
     3385        wp_cache_set( 'last_changed', microtime(), 'comment' );
     3386}
  • branches/5.0/src/wp-includes/default-filters.php

    r43502 r43729  
    8989// Meta
    9090add_filter( 'register_meta_args', '_wp_register_meta_args_whitelist', 10, 2 );
     91
     92// Post meta
     93add_action( 'added_post_meta', 'wp_cache_set_posts_last_changed' );
     94add_action( 'updated_post_meta', 'wp_cache_set_posts_last_changed' );
     95add_action( 'deleted_post_meta', 'wp_cache_set_posts_last_changed' );
     96
     97// Term meta
     98add_action( 'added_term_meta', 'wp_cache_set_terms_last_changed' );
     99add_action( 'updated_term_meta', 'wp_cache_set_terms_last_changed' );
     100add_action( 'deleted_term_meta', 'wp_cache_set_terms_last_changed' );
     101add_filter( 'get_term_metadata', 'wp_check_term_meta_support_prefilter' );
     102add_filter( 'add_term_metadata', 'wp_check_term_meta_support_prefilter' );
     103add_filter( 'update_term_metadata', 'wp_check_term_meta_support_prefilter' );
     104add_filter( 'delete_term_metadata', 'wp_check_term_meta_support_prefilter' );
     105add_filter( 'get_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' );
     106add_filter( 'update_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' );
     107add_filter( 'delete_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' );
     108add_filter( 'update_term_metadata_cache', 'wp_check_term_meta_support_prefilter' );
     109
     110// Comment meta
     111add_action( 'added_comment_meta', 'wp_cache_set_comments_last_changed' );
     112add_action( 'updated_comment_meta', 'wp_cache_set_comments_last_changed' );
     113add_action( 'deleted_comment_meta', 'wp_cache_set_comments_last_changed' );
    91114
    92115// Places to balance tags on input
  • branches/5.0/src/wp-includes/meta.php

    r43706 r43729  
    587587        $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
    588588
     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
    589606        $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
    590607
     
    631648        $column = sanitize_key($meta_type . '_id');
    632649        $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
     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        }
    633669
    634670        // Fetch the meta and go on if it's found.
     
    725761        $column = sanitize_key($meta_type . '_id');
    726762        $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
     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        }
    727780
    728781        // Fetch the meta and go on if it's found.
     
    811864
    812865        $object_ids = array_map('intval', $object_ids);
     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        }
    813883
    814884        $cache_key = $meta_type . '_meta';
  • branches/5.0/src/wp-includes/post.php

    r43710 r43729  
    17851785function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
    17861786        // Make sure meta is added to the post, not a revision.
    1787         if ( $the_post = wp_is_post_revision($post_id) )
     1787        $the_post = wp_is_post_revision( $post_id );
     1788        if ( $the_post ) {
    17881789                $post_id = $the_post;
    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        }
     1791
     1792        return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
    17951793}
    17961794
     
    18121810function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
    18131811        // Make sure meta is added to the post, not a revision.
    1814         if ( $the_post = wp_is_post_revision($post_id) )
     1812        $the_post = wp_is_post_revision( $post_id );
     1813        if ( $the_post ) {
    18151814                $post_id = $the_post;
    1816 
    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;
     1815        }
     1816
     1817        return delete_metadata( 'post', $post_id, $meta_key, $meta_value );
    18221818}
    18231819
     
    18581854function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
    18591855        // Make sure meta is added to the post, not a revision.
    1860         if ( $the_post = wp_is_post_revision($post_id) )
     1856        $the_post = wp_is_post_revision( $post_id );
     1857        if ( $the_post ) {
    18611858                $post_id = $the_post;
    1862 
    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;
     1859        }
     1860
     1861        return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
    18681862}
    18691863
     
    18771871 */
    18781872function 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;
     1873        return delete_metadata( 'post', null, $post_meta_key, '', true );
    18841874}
    18851875
     
    64766466        return $clauses;
    64776467}
     6468
     6469/**
     6470 * Sets the last changed time for the 'posts' cache group.
     6471 *
     6472 * @since 5.0.0
     6473 */
     6474function wp_cache_set_posts_last_changed() {
     6475        wp_cache_set( 'last_changed', microtime(), 'posts' );
     6476}
  • branches/5.0/src/wp-includes/taxonomy.php

    r43510 r43729  
    11361136 */
    11371137function 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 
    11431138        if ( wp_term_is_shared( $term_id ) ) {
    11441139                return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id );
    11451140        }
    11461141
    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 );
    11551143}
    11561144
     
    11661154 */
    11671155function 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 );
    11811157}
    11821158
     
    11931169 */
    11941170function 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 
    12001171        return get_metadata( 'term', $term_id, $key, $single );
    12011172}
     
    12181189 */
    12191190function 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 
    12251191        if ( wp_term_is_shared( $term_id ) ) {
    12261192                return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id );
    12271193        }
    12281194
    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 );
    12371196}
    12381197
     
    12491208 */
    12501209function 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 
    12561210        return update_meta_cache( 'term', $term_ids );
    12571211}
     
    12681222 */
    12691223function 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;
    12731227        }
    12741228
     
    43424296        return $parent;
    43434297}
     4298
     4299/**
     4300 * Sets the last changed time for the 'terms' cache group.
     4301 *
     4302 * @since 5.0.0
     4303 */
     4304function 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 */
     4316function wp_check_term_meta_support_prefilter( $check ) {
     4317        if ( get_option( 'db_version' ) < 34370 ) {
     4318                return false;
     4319        }
     4320
     4321        return $check;
     4322}
  • branches/5.0/tests/phpunit/tests/comment/metaCache.php

    r37954 r43729  
    211211                $this->assertSame( $num_queries, $wpdb->num_queries );
    212212        }
     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        }
    213250}
  • branches/5.0/tests/phpunit/tests/post/meta.php

    r43510 r43729  
    306306                );
    307307        }
     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        }
    308345}
  • branches/5.0/tests/phpunit/tests/term/meta.php

    r43510 r43729  
    510510                );
    511511        }
     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        }
    512570}
Note: See TracChangeset for help on using the changeset viewer.