Make WordPress Core

Changeset 43982


Ignore:
Timestamp:
12/12/2018 03:02:00 AM (6 years ago)
Author:
jeremyfelt
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.

Merges [43729] to trunk.

Props flixos90, spacedmonkey.
Fixes #44467.

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/comment.php

    r43571 r43982  
    443443 */
    444444function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) {
    445     $added = add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
    446     if ( $added ) {
    447         wp_cache_set( 'last_changed', microtime(), 'comment' );
    448     }
    449     return $added;
     445    return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
    450446}
    451447
     
    466462 */
    467463function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) {
    468     $deleted = delete_metadata( 'comment', $comment_id, $meta_key, $meta_value );
    469     if ( $deleted ) {
    470         wp_cache_set( 'last_changed', microtime(), 'comment' );
    471     }
    472     return $deleted;
     464    return delete_metadata( 'comment', $comment_id, $meta_key, $meta_value );
    473465}
    474466
     
    507499 */
    508500function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) {
    509     $updated = update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value );
    510     if ( $updated ) {
    511         wp_cache_set( 'last_changed', microtime(), 'comment' );
    512     }
    513     return $updated;
     501    return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value );
    514502}
    515503
     
    35133501    );
    35143502}
     3503
     3504/**
     3505 * Sets the last changed time for the 'comment' cache group.
     3506 *
     3507 * @since 5.0.0
     3508 */
     3509function wp_cache_set_comments_last_changed() {
     3510    wp_cache_set( 'last_changed', microtime(), 'comment' );
     3511}
  • trunk/src/wp-includes/default-filters.php

    r43571 r43982  
    9898// Meta
    9999add_filter( 'register_meta_args', '_wp_register_meta_args_whitelist', 10, 2 );
     100
     101// Post meta
     102add_action( 'added_post_meta', 'wp_cache_set_posts_last_changed' );
     103add_action( 'updated_post_meta', 'wp_cache_set_posts_last_changed' );
     104add_action( 'deleted_post_meta', 'wp_cache_set_posts_last_changed' );
     105
     106// Term meta
     107add_action( 'added_term_meta', 'wp_cache_set_terms_last_changed' );
     108add_action( 'updated_term_meta', 'wp_cache_set_terms_last_changed' );
     109add_action( 'deleted_term_meta', 'wp_cache_set_terms_last_changed' );
     110add_filter( 'get_term_metadata', 'wp_check_term_meta_support_prefilter' );
     111add_filter( 'add_term_metadata', 'wp_check_term_meta_support_prefilter' );
     112add_filter( 'update_term_metadata', 'wp_check_term_meta_support_prefilter' );
     113add_filter( 'delete_term_metadata', 'wp_check_term_meta_support_prefilter' );
     114add_filter( 'get_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' );
     115add_filter( 'update_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' );
     116add_filter( 'delete_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' );
     117add_filter( 'update_term_metadata_cache', 'wp_check_term_meta_support_prefilter' );
     118
     119// Comment meta
     120add_action( 'added_comment_meta', 'wp_cache_set_comments_last_changed' );
     121add_action( 'updated_comment_meta', 'wp_cache_set_comments_last_changed' );
     122add_action( 'deleted_comment_meta', 'wp_cache_set_comments_last_changed' );
    100123
    101124// Places to balance tags on input
  • trunk/src/wp-includes/meta.php

    r43582 r43982  
    614614    $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
    615615
     616    /**
     617     * Filters whether to retrieve metadata of a specific type by meta ID.
     618     *
     619     * The dynamic portion of the hook, `$meta_type`, refers to the meta
     620     * object type (comment, post, term, or user). Returning a non-null value
     621     * will effectively short-circuit the function.
     622     *
     623     * @since 5.0.0
     624     *
     625     * @param mixed $value    The value get_metadata_by_mid() should return.
     626     * @param int   $meta_id  Meta ID.
     627     */
     628    $check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id );
     629    if ( null !== $check ) {
     630        return $check;
     631    }
     632
    616633    $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
    617634
     
    660677    $column    = sanitize_key( $meta_type . '_id' );
    661678    $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
     679
     680    /**
     681     * Filters whether to update metadata of a specific type by meta ID.
     682     *
     683     * The dynamic portion of the hook, `$meta_type`, refers to the meta
     684     * object type (comment, post, term, or user). Returning a non-null value
     685     * will effectively short-circuit the function.
     686     *
     687     * @since 5.0.0
     688     *
     689     * @param null|bool   $check      Whether to allow updating metadata for the given type.
     690     * @param int         $meta_id    Meta ID.
     691     * @param mixed       $meta_value Meta value. Must be serializable if non-scalar.
     692     * @param string|bool $meta_key   Meta key, if provided.
     693     */
     694    $check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key );
     695    if ( null !== $check ) {
     696        return (bool) $check;
     697    }
    662698
    663699    // Fetch the meta and go on if it's found.
     
    755791    $column    = sanitize_key( $meta_type . '_id' );
    756792    $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
     793
     794    /**
     795     * Filters whether to delete metadata of a specific type by meta ID.
     796     *
     797     * The dynamic portion of the hook, `$meta_type`, refers to the meta
     798     * object type (comment, post, term, or user). Returning a non-null value
     799     * will effectively short-circuit the function.
     800     *
     801     * @since 5.0.0
     802     *
     803     * @param null|bool $delete  Whether to allow metadata deletion of the given type.
     804     * @param int       $meta_id Meta ID.
     805     */
     806    $check = apply_filters( "delete_{$meta_type}_metadata_by_mid", null, $meta_id );
     807    if ( null !== $check ) {
     808        return (bool) $check;
     809    }
    757810
    758811    // Fetch the meta and go on if it's found.
     
    841894
    842895    $object_ids = array_map( 'intval', $object_ids );
     896
     897    /**
     898     * Filters whether to update the metadata cache of a specific type.
     899     *
     900     * The dynamic portion of the hook, `$meta_type`, refers to the meta
     901     * object type (comment, post, term, or user). Returning a non-null value
     902     * will effectively short-circuit the function.
     903     *
     904     * @since 5.0.0
     905     *
     906     * @param mixed $check      Whether to allow updating the meta cache of the given type.
     907     * @param array $object_ids Array of object IDs to update the meta cache for.
     908     */
     909    $check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids );
     910    if ( null !== $check ) {
     911        return (bool) $check;
     912    }
    843913
    844914    $cache_key = $meta_type . '_meta';
  • trunk/src/wp-includes/post.php

    r43638 r43982  
    19071907function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
    19081908    // Make sure meta is added to the post, not a revision.
    1909     if ( $the_post = wp_is_post_revision( $post_id ) ) {
     1909    $the_post = wp_is_post_revision( $post_id );
     1910    if ( $the_post ) {
    19101911        $post_id = $the_post;
    19111912    }
    19121913
    1913     $added = add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
    1914     if ( $added ) {
    1915         wp_cache_set( 'last_changed', microtime(), 'posts' );
    1916     }
    1917     return $added;
     1914    return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
    19181915}
    19191916
     
    19351932function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
    19361933    // Make sure meta is added to the post, not a revision.
    1937     if ( $the_post = wp_is_post_revision( $post_id ) ) {
     1934    $the_post = wp_is_post_revision( $post_id );
     1935    if ( $the_post ) {
    19381936        $post_id = $the_post;
    19391937    }
    19401938
    1941     $deleted = delete_metadata( 'post', $post_id, $meta_key, $meta_value );
    1942     if ( $deleted ) {
    1943         wp_cache_set( 'last_changed', microtime(), 'posts' );
    1944     }
    1945     return $deleted;
     1939    return delete_metadata( 'post', $post_id, $meta_key, $meta_value );
    19461940}
    19471941
     
    19821976function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
    19831977    // Make sure meta is added to the post, not a revision.
    1984     if ( $the_post = wp_is_post_revision( $post_id ) ) {
     1978    $the_post = wp_is_post_revision( $post_id );
     1979    if ( $the_post ) {
    19851980        $post_id = $the_post;
    19861981    }
    19871982
    1988     $updated = update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
    1989     if ( $updated ) {
    1990         wp_cache_set( 'last_changed', microtime(), 'posts' );
    1991     }
    1992     return $updated;
     1983    return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
    19931984}
    19941985
     
    20021993 */
    20031994function delete_post_meta_by_key( $post_meta_key ) {
    2004     $deleted = delete_metadata( 'post', null, $post_meta_key, '', true );
    2005     if ( $deleted ) {
    2006         wp_cache_set( 'last_changed', microtime(), 'posts' );
    2007     }
    2008     return $deleted;
     1995    return delete_metadata( 'post', null, $post_meta_key, '', true );
    20091996}
    20101997
     
    67866773    return $clauses;
    67876774}
     6775
     6776/**
     6777 * Sets the last changed time for the 'posts' cache group.
     6778 *
     6779 * @since 5.0.0
     6780 */
     6781function wp_cache_set_posts_last_changed() {
     6782    wp_cache_set( 'last_changed', microtime(), 'posts' );
     6783}
  • trunk/src/wp-includes/taxonomy.php

    r43631 r43982  
    12121212 */
    12131213function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
    1214     // Bail if term meta table is not installed.
    1215     if ( get_option( 'db_version' ) < 34370 ) {
    1216         return false;
    1217     }
    1218 
    12191214    if ( wp_term_is_shared( $term_id ) ) {
    12201215        return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );
    12211216    }
    12221217
    1223     $added = add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
    1224 
    1225     // Bust term query cache.
    1226     if ( $added ) {
    1227         wp_cache_set( 'last_changed', microtime(), 'terms' );
    1228     }
    1229 
    1230     return $added;
     1218    return add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
    12311219}
    12321220
     
    12421230 */
    12431231function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) {
    1244     // Bail if term meta table is not installed.
    1245     if ( get_option( 'db_version' ) < 34370 ) {
    1246         return false;
    1247     }
    1248 
    1249     $deleted = delete_metadata( 'term', $term_id, $meta_key, $meta_value );
    1250 
    1251     // Bust term query cache.
    1252     if ( $deleted ) {
    1253         wp_cache_set( 'last_changed', microtime(), 'terms' );
    1254     }
    1255 
    1256     return $deleted;
     1232    return delete_metadata( 'term', $term_id, $meta_key, $meta_value );
    12571233}
    12581234
     
    12691245 */
    12701246function get_term_meta( $term_id, $key = '', $single = false ) {
    1271     // Bail if term meta table is not installed.
    1272     if ( get_option( 'db_version' ) < 34370 ) {
    1273         return false;
    1274     }
    1275 
    12761247    return get_metadata( 'term', $term_id, $key, $single );
    12771248}
     
    12941265 */
    12951266function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
    1296     // Bail if term meta table is not installed.
    1297     if ( get_option( 'db_version' ) < 34370 ) {
    1298         return false;
    1299     }
    1300 
    13011267    if ( wp_term_is_shared( $term_id ) ) {
    13021268        return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );
    13031269    }
    13041270
    1305     $updated = update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
    1306 
    1307     // Bust term query cache.
    1308     if ( $updated ) {
    1309         wp_cache_set( 'last_changed', microtime(), 'terms' );
    1310     }
    1311 
    1312     return $updated;
     1271    return update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
    13131272}
    13141273
     
    13251284 */
    13261285function update_termmeta_cache( $term_ids ) {
    1327     // Bail if term meta table is not installed.
    1328     if ( get_option( 'db_version' ) < 34370 ) {
    1329         return;
    1330     }
    1331 
    13321286    return update_meta_cache( 'term', $term_ids );
    13331287}
     
    13441298 */
    13451299function has_term_meta( $term_id ) {
    1346     // Bail if term meta table is not installed.
    1347     if ( get_option( 'db_version' ) < 34370 ) {
    1348         return false;
     1300    $check = wp_check_term_meta_support_prefilter( null );
     1301    if ( null !== $check ) {
     1302        return $check;
    13491303    }
    13501304
     
    46334587    return $taxonomy->publicly_queryable;
    46344588}
     4589
     4590/**
     4591 * Sets the last changed time for the 'terms' cache group.
     4592 *
     4593 * @since 5.0.0
     4594 */
     4595function wp_cache_set_terms_last_changed() {
     4596    wp_cache_set( 'last_changed', microtime(), 'terms' );
     4597}
     4598
     4599/**
     4600 * Aborts calls to term meta if it is not supported.
     4601 *
     4602 * @since 5.0.0
     4603 *
     4604 * @param mixed $check Skip-value for whether to proceed term meta function execution.
     4605 * @return mixed Original value of $check, or false if term meta is not supported.
     4606 */
     4607function wp_check_term_meta_support_prefilter( $check ) {
     4608    if ( get_option( 'db_version' ) < 34370 ) {
     4609        return false;
     4610    }
     4611
     4612    return $check;
     4613}
  • trunk/tests/phpunit/tests/comment/metaCache.php

    r42343 r43982  
    225225        $this->assertSame( $num_queries, $wpdb->num_queries );
    226226    }
     227
     228    /**
     229     * @ticket 44467
     230     */
     231    public function test_add_metadata_sets_comments_last_changed() {
     232        $comment_id = self::factory()->comment->create();
     233
     234        wp_cache_delete( 'last_changed', 'comment' );
     235
     236        $this->assertInternalType( 'integer', add_metadata( 'comment', $comment_id, 'foo', 'bar' ) );
     237        $this->assertNotFalse( wp_cache_get_last_changed( 'comment' ) );
     238    }
     239
     240    /**
     241     * @ticket 44467
     242     */
     243    public function test_update_metadata_sets_comments_last_changed() {
     244        $comment_id = self::factory()->comment->create();
     245
     246        wp_cache_delete( 'last_changed', 'comment' );
     247
     248        $this->assertInternalType( 'integer', update_metadata( 'comment', $comment_id, 'foo', 'bar' ) );
     249        $this->assertNotFalse( wp_cache_get_last_changed( 'comment' ) );
     250    }
     251
     252    /**
     253     * @ticket 44467
     254     */
     255    public function test_delete_metadata_sets_comments_last_changed() {
     256        $comment_id = self::factory()->comment->create();
     257
     258        update_metadata( 'comment', $comment_id, 'foo', 'bar' );
     259        wp_cache_delete( 'last_changed', 'comment' );
     260
     261        $this->assertTrue( delete_metadata( 'comment', $comment_id, 'foo' ) );
     262        $this->assertNotFalse( wp_cache_get_last_changed( 'comment' ) );
     263    }
    227264}
  • trunk/tests/phpunit/tests/post/meta.php

    r43571 r43982  
    310310        );
    311311    }
     312
     313    /**
     314     * @ticket 44467
     315     */
     316    public function test_add_metadata_sets_posts_last_changed() {
     317        $post_id = self::factory()->post->create();
     318
     319        wp_cache_delete( 'last_changed', 'posts' );
     320
     321        $this->assertInternalType( 'integer', add_metadata( 'post', $post_id, 'foo', 'bar' ) );
     322        $this->assertNotFalse( wp_cache_get_last_changed( 'posts' ) );
     323    }
     324
     325    /**
     326     * @ticket 44467
     327     */
     328    public function test_update_metadata_sets_posts_last_changed() {
     329        $post_id = self::factory()->post->create();
     330
     331        wp_cache_delete( 'last_changed', 'posts' );
     332
     333        $this->assertInternalType( 'integer', update_metadata( 'post', $post_id, 'foo', 'bar' ) );
     334        $this->assertNotFalse( wp_cache_get_last_changed( 'posts' ) );
     335    }
     336
     337    /**
     338     * @ticket 44467
     339     */
     340    public function test_delete_metadata_sets_posts_last_changed() {
     341        $post_id = self::factory()->post->create();
     342
     343        update_metadata( 'post', $post_id, 'foo', 'bar' );
     344        wp_cache_delete( 'last_changed', 'posts' );
     345
     346        $this->assertTrue( delete_metadata( 'post', $post_id, 'foo' ) );
     347        $this->assertNotFalse( wp_cache_get_last_changed( 'posts' ) );
     348    }
    312349}
  • trunk/tests/phpunit/tests/term/meta.php

    r43571 r43982  
    538538        );
    539539    }
     540
     541    /**
     542     * @ticket 44467
     543     */
     544    public function test_add_metadata_sets_terms_last_changed() {
     545        $term_id = self::factory()->term->create();
     546
     547        wp_cache_delete( 'last_changed', 'terms' );
     548
     549        $this->assertInternalType( 'integer', add_metadata( 'term', $term_id, 'foo', 'bar' ) );
     550        $this->assertNotFalse( wp_cache_get_last_changed( 'terms' ) );
     551    }
     552
     553    /**
     554     * @ticket 44467
     555     */
     556    public function test_update_metadata_sets_terms_last_changed() {
     557        $term_id = self::factory()->term->create();
     558
     559        wp_cache_delete( 'last_changed', 'terms' );
     560
     561        $this->assertInternalType( 'integer', update_metadata( 'term', $term_id, 'foo', 'bar' ) );
     562        $this->assertNotFalse( wp_cache_get_last_changed( 'terms' ) );
     563    }
     564
     565    /**
     566     * @ticket 44467
     567     */
     568    public function test_delete_metadata_sets_terms_last_changed() {
     569        $term_id = self::factory()->term->create();
     570
     571        update_metadata( 'term', $term_id, 'foo', 'bar' );
     572        wp_cache_delete( 'last_changed', 'terms' );
     573
     574        $this->assertTrue( delete_metadata( 'term', $term_id, 'foo' ) );
     575        $this->assertNotFalse( wp_cache_get_last_changed( 'terms' ) );
     576    }
     577
     578    /**
     579     * @ticket 44467
     580     */
     581    public function test_metadata_functions_respect_term_meta_support() {
     582        $term_id = self::factory()->term->create();
     583
     584        $meta_id = add_metadata( 'term', $term_id, 'foo', 'bar' );
     585
     586        // Set database version to last version before term meta support.
     587        update_option( 'db_version', 34369 );
     588
     589        $this->assertFalse( get_metadata( 'term', $term_id, 'foo', true ) );
     590        $this->assertFalse( add_metadata( 'term', $term_id, 'foo', 'bar' ) );
     591        $this->assertFalse( update_metadata( 'term', $term_id, 'foo', 'bar' ) );
     592        $this->assertFalse( delete_metadata( 'term', $term_id, 'foo' ) );
     593        $this->assertFalse( get_metadata_by_mid( 'term', $meta_id ) );
     594        $this->assertFalse( update_metadata_by_mid( 'term', $meta_id, 'baz' ) );
     595        $this->assertFalse( delete_metadata_by_mid( 'term', $meta_id ) );
     596        $this->assertFalse( update_meta_cache( 'term', array( $term_id ) ) );
     597    }
    540598}
Note: See TracChangeset for help on using the changeset viewer.