Make WordPress Core

Ticket #44467: 44467.4.diff

File 44467.4.diff, 19.4 KB (added by flixos90, 7 years ago)
  • src/wp-includes/comment.php

     
    422422 * @param bool $unique Optional, default is false. Whether the same key should not be added.
    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
    433429/**
     
    445441 * @param mixed $meta_value Optional. Metadata value.
    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
    456448/**
     
    465457 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
    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
    472464/**
     
    486478 * @param mixed $prev_value Optional. Previous value to check before removing.
    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
    497485/**
     
    30613049                 * @param int $comment_post_ID Post ID.
    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 );
    30673055                } else {
     
    33873375                'done'           => $done,
    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}
  • src/wp-includes/default-filters.php

     
    8989// Meta
    9090add_filter( 'register_meta_args', '_wp_register_meta_args_whitelist', 10, 2 );
    9191
     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' );
     114
    92115// Places to balance tags on input
    93116foreach ( array( 'content_save_pre', 'excerpt_save_pre', 'comment_save_pre', 'pre_comment_content' ) as $filter ) {
    94117        add_filter( $filter, 'convert_invalid_entities' );
  • src/wp-includes/meta.php

     
    586586
    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
    591608        if ( empty( $meta ) )
     
    631648        $column = sanitize_key($meta_type . '_id');
    632649        $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
    633650
     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
    634670        // Fetch the meta and go on if it's found.
    635671        if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
    636672                $original_key = $meta->meta_key;
     
    725761        $column = sanitize_key($meta_type . '_id');
    726762        $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
    727763
     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
    728781        // Fetch the meta and go on if it's found.
    729782        if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
    730783                $object_id = $meta->{$column};
     
    811864
    812865        $object_ids = array_map('intval', $object_ids);
    813866
     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
    814884        $cache_key = $meta_type . '_meta';
    815885        $ids = array();
    816886        $cache = array();
  • src/wp-includes/post.php

     
    17871787        if ( $the_post = wp_is_post_revision($post_id) )
    17881788                $post_id = $the_post;
    17891789
    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 );
    17951791}
    17961792
    17971793/**
     
    18141810        if ( $the_post = wp_is_post_revision($post_id) )
    18151811                $post_id = $the_post;
    18161812
    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 );
    18221814}
    18231815
    18241816/**
     
    18601852        if ( $the_post = wp_is_post_revision($post_id) )
    18611853                $post_id = $the_post;
    18621854
    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 );
    18681856}
    18691857
    18701858/**
     
    18761864 * @return bool Whether the post meta key was deleted from the database.
    18771865 */
    18781866function 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 );
    18841868}
    18851869
    18861870/**
     
    64756459
    64766460        return $clauses;
    64776461}
     6462
     6463/**
     6464 * Sets the last changed time for the 'posts' cache group.
     6465 *
     6466 * @since 5.0.0
     6467 */
     6468function wp_cache_set_posts_last_changed() {
     6469        wp_cache_set( 'last_changed', microtime(), 'posts' );
     6470}
  • src/wp-includes/taxonomy.php

     
    11351135 *                           False on failure.
    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
    11571145/**
     
    11651153 * @return bool True on success, false on failure.
    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
    11831159/**
     
    11921168 * @return mixed If `$single` is false, an array of metadata values. If `$single` is true, a single metadata value.
    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}
    12021173
     
    12171188 *                           WP_Error when term_id is ambiguous between taxonomies. False on failure.
    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
    12391198/**
     
    12481207 * @return array|false Returns false if there is nothing to update. Returns an array of metadata on success.
    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}
    12581212
     
    12671221 * @return array|false Array with meta data, or false when the meta table is not installed.
    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
    12751229        global $wpdb;
     
    43414295
    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}
  • tests/phpunit/tests/comment/metaCache.php

     
    210210                $num_queries++;
    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}
  • tests/phpunit/tests/post/meta.php

     
    305305                        array( '', 'registered_key3' ),
    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}
  • tests/phpunit/tests/term/meta.php

     
    509509                        array( '', 'registered_key3' ),
    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}