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.

Built from https://develop.svn.wordpress.org/trunk@43982


git-svn-id: http://core.svn.wordpress.org/trunk@43814 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Jeremy Felt 2018-12-12 03:02:24 +00:00
parent a0309e80b6
commit 2dce718157
6 changed files with 156 additions and 91 deletions

View File

@ -442,11 +442,7 @@ function get_comment_count( $post_id = 0 ) {
* @return int|bool Meta ID on success, false on failure.
*/
function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) {
$added = add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
if ( $added ) {
wp_cache_set( 'last_changed', microtime(), 'comment' );
}
return $added;
return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
}
/**
@ -465,11 +461,7 @@ function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false
* @return bool True on success, false on failure.
*/
function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) {
$deleted = delete_metadata( 'comment', $comment_id, $meta_key, $meta_value );
if ( $deleted ) {
wp_cache_set( 'last_changed', microtime(), 'comment' );
}
return $deleted;
return delete_metadata( 'comment', $comment_id, $meta_key, $meta_value );
}
/**
@ -506,11 +498,7 @@ function get_comment_meta( $comment_id, $key = '', $single = false ) {
* @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
*/
function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) {
$updated = update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value );
if ( $updated ) {
wp_cache_set( 'last_changed', microtime(), 'comment' );
}
return $updated;
return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value );
}
/**
@ -3512,3 +3500,12 @@ function wp_comments_personal_data_eraser( $email_address, $page = 1 ) {
'done' => $done,
);
}
/**
* Sets the last changed time for the 'comment' cache group.
*
* @since 5.0.0
*/
function wp_cache_set_comments_last_changed() {
wp_cache_set( 'last_changed', microtime(), 'comment' );
}

View File

@ -98,6 +98,29 @@ add_filter( 'post_mime_type', 'sanitize_mime_type' );
// Meta
add_filter( 'register_meta_args', '_wp_register_meta_args_whitelist', 10, 2 );
// Post meta
add_action( 'added_post_meta', 'wp_cache_set_posts_last_changed' );
add_action( 'updated_post_meta', 'wp_cache_set_posts_last_changed' );
add_action( 'deleted_post_meta', 'wp_cache_set_posts_last_changed' );
// Term meta
add_action( 'added_term_meta', 'wp_cache_set_terms_last_changed' );
add_action( 'updated_term_meta', 'wp_cache_set_terms_last_changed' );
add_action( 'deleted_term_meta', 'wp_cache_set_terms_last_changed' );
add_filter( 'get_term_metadata', 'wp_check_term_meta_support_prefilter' );
add_filter( 'add_term_metadata', 'wp_check_term_meta_support_prefilter' );
add_filter( 'update_term_metadata', 'wp_check_term_meta_support_prefilter' );
add_filter( 'delete_term_metadata', 'wp_check_term_meta_support_prefilter' );
add_filter( 'get_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' );
add_filter( 'update_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' );
add_filter( 'delete_term_metadata_by_mid', 'wp_check_term_meta_support_prefilter' );
add_filter( 'update_term_metadata_cache', 'wp_check_term_meta_support_prefilter' );
// Comment meta
add_action( 'added_comment_meta', 'wp_cache_set_comments_last_changed' );
add_action( 'updated_comment_meta', 'wp_cache_set_comments_last_changed' );
add_action( 'deleted_comment_meta', 'wp_cache_set_comments_last_changed' );
// Places to balance tags on input
foreach ( array( 'content_save_pre', 'excerpt_save_pre', 'comment_save_pre', 'pre_comment_content' ) as $filter ) {
add_filter( $filter, 'convert_invalid_entities' );

View File

@ -613,6 +613,23 @@ function get_metadata_by_mid( $meta_type, $meta_id ) {
$id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
/**
* Filters whether to retrieve metadata of a specific type by meta ID.
*
* The dynamic portion of the hook, `$meta_type`, refers to the meta
* object type (comment, post, term, or user). Returning a non-null value
* will effectively short-circuit the function.
*
* @since 5.0.0
*
* @param mixed $value The value get_metadata_by_mid() should return.
* @param int $meta_id Meta ID.
*/
$check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id );
if ( null !== $check ) {
return $check;
}
$meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
if ( empty( $meta ) ) {
@ -660,6 +677,25 @@ function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key =
$column = sanitize_key( $meta_type . '_id' );
$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
/**
* Filters whether to update metadata of a specific type by meta ID.
*
* The dynamic portion of the hook, `$meta_type`, refers to the meta
* object type (comment, post, term, or user). Returning a non-null value
* will effectively short-circuit the function.
*
* @since 5.0.0
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $meta_id Meta ID.
* @param mixed $meta_value Meta value. Must be serializable if non-scalar.
* @param string|bool $meta_key Meta key, if provided.
*/
$check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key );
if ( null !== $check ) {
return (bool) $check;
}
// Fetch the meta and go on if it's found.
if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
$original_key = $meta->meta_key;
@ -755,6 +791,23 @@ function delete_metadata_by_mid( $meta_type, $meta_id ) {
$column = sanitize_key( $meta_type . '_id' );
$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
/**
* Filters whether to delete metadata of a specific type by meta ID.
*
* The dynamic portion of the hook, `$meta_type`, refers to the meta
* object type (comment, post, term, or user). Returning a non-null value
* will effectively short-circuit the function.
*
* @since 5.0.0
*
* @param null|bool $delete Whether to allow metadata deletion of the given type.
* @param int $meta_id Meta ID.
*/
$check = apply_filters( "delete_{$meta_type}_metadata_by_mid", null, $meta_id );
if ( null !== $check ) {
return (bool) $check;
}
// Fetch the meta and go on if it's found.
if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
$object_id = $meta->{$column};
@ -841,6 +894,23 @@ function update_meta_cache( $meta_type, $object_ids ) {
$object_ids = array_map( 'intval', $object_ids );
/**
* Filters whether to update the metadata cache of a specific type.
*
* The dynamic portion of the hook, `$meta_type`, refers to the meta
* object type (comment, post, term, or user). Returning a non-null value
* will effectively short-circuit the function.
*
* @since 5.0.0
*
* @param mixed $check Whether to allow updating the meta cache of the given type.
* @param array $object_ids Array of object IDs to update the meta cache for.
*/
$check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids );
if ( null !== $check ) {
return (bool) $check;
}
$cache_key = $meta_type . '_meta';
$ids = array();
$cache = array();

View File

@ -1906,15 +1906,12 @@ function get_posts( $args = null ) {
*/
function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
// Make sure meta is added to the post, not a revision.
if ( $the_post = wp_is_post_revision( $post_id ) ) {
$the_post = wp_is_post_revision( $post_id );
if ( $the_post ) {
$post_id = $the_post;
}
$added = add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
if ( $added ) {
wp_cache_set( 'last_changed', microtime(), 'posts' );
}
return $added;
return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
}
/**
@ -1934,15 +1931,12 @@ function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
*/
function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
// Make sure meta is added to the post, not a revision.
if ( $the_post = wp_is_post_revision( $post_id ) ) {
$the_post = wp_is_post_revision( $post_id );
if ( $the_post ) {
$post_id = $the_post;
}
$deleted = delete_metadata( 'post', $post_id, $meta_key, $meta_value );
if ( $deleted ) {
wp_cache_set( 'last_changed', microtime(), 'posts' );
}
return $deleted;
return delete_metadata( 'post', $post_id, $meta_key, $meta_value );
}
/**
@ -1981,15 +1975,12 @@ function get_post_meta( $post_id, $key = '', $single = false ) {
*/
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
// Make sure meta is added to the post, not a revision.
if ( $the_post = wp_is_post_revision( $post_id ) ) {
$the_post = wp_is_post_revision( $post_id );
if ( $the_post ) {
$post_id = $the_post;
}
$updated = update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
if ( $updated ) {
wp_cache_set( 'last_changed', microtime(), 'posts' );
}
return $updated;
return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
}
/**
@ -2001,11 +1992,7 @@ function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' )
* @return bool Whether the post meta key was deleted from the database.
*/
function delete_post_meta_by_key( $post_meta_key ) {
$deleted = delete_metadata( 'post', null, $post_meta_key, '', true );
if ( $deleted ) {
wp_cache_set( 'last_changed', microtime(), 'posts' );
}
return $deleted;
return delete_metadata( 'post', null, $post_meta_key, '', true );
}
/**
@ -6785,3 +6772,12 @@ function _filter_query_attachment_filenames( $clauses ) {
return $clauses;
}
/**
* Sets the last changed time for the 'posts' cache group.
*
* @since 5.0.0
*/
function wp_cache_set_posts_last_changed() {
wp_cache_set( 'last_changed', microtime(), 'posts' );
}

View File

@ -1211,23 +1211,11 @@ function get_terms( $args = array(), $deprecated = '' ) {
* False on failure.
*/
function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
// Bail if term meta table is not installed.
if ( get_option( 'db_version' ) < 34370 ) {
return false;
}
if ( wp_term_is_shared( $term_id ) ) {
return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );
}
$added = add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
// Bust term query cache.
if ( $added ) {
wp_cache_set( 'last_changed', microtime(), 'terms' );
}
return $added;
return add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
}
/**
@ -1241,19 +1229,7 @@ function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
* @return bool True on success, false on failure.
*/
function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) {
// Bail if term meta table is not installed.
if ( get_option( 'db_version' ) < 34370 ) {
return false;
}
$deleted = delete_metadata( 'term', $term_id, $meta_key, $meta_value );
// Bust term query cache.
if ( $deleted ) {
wp_cache_set( 'last_changed', microtime(), 'terms' );
}
return $deleted;
return delete_metadata( 'term', $term_id, $meta_key, $meta_value );
}
/**
@ -1268,11 +1244,6 @@ function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) {
* @return mixed If `$single` is false, an array of metadata values. If `$single` is true, a single metadata value.
*/
function get_term_meta( $term_id, $key = '', $single = false ) {
// Bail if term meta table is not installed.
if ( get_option( 'db_version' ) < 34370 ) {
return false;
}
return get_metadata( 'term', $term_id, $key, $single );
}
@ -1293,23 +1264,11 @@ function get_term_meta( $term_id, $key = '', $single = false ) {
* WP_Error when term_id is ambiguous between taxonomies. False on failure.
*/
function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
// Bail if term meta table is not installed.
if ( get_option( 'db_version' ) < 34370 ) {
return false;
}
if ( wp_term_is_shared( $term_id ) ) {
return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );
}
$updated = update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
// Bust term query cache.
if ( $updated ) {
wp_cache_set( 'last_changed', microtime(), 'terms' );
}
return $updated;
return update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
}
/**
@ -1324,11 +1283,6 @@ function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' )
* @return array|false Returns false if there is nothing to update. Returns an array of metadata on success.
*/
function update_termmeta_cache( $term_ids ) {
// Bail if term meta table is not installed.
if ( get_option( 'db_version' ) < 34370 ) {
return;
}
return update_meta_cache( 'term', $term_ids );
}
@ -1343,9 +1297,9 @@ function update_termmeta_cache( $term_ids ) {
* @return array|false Array with meta data, or false when the meta table is not installed.
*/
function has_term_meta( $term_id ) {
// Bail if term meta table is not installed.
if ( get_option( 'db_version' ) < 34370 ) {
return false;
$check = wp_check_term_meta_support_prefilter( null );
if ( null !== $check ) {
return $check;
}
global $wpdb;
@ -4632,3 +4586,28 @@ function is_taxonomy_viewable( $taxonomy ) {
return $taxonomy->publicly_queryable;
}
/**
* Sets the last changed time for the 'terms' cache group.
*
* @since 5.0.0
*/
function wp_cache_set_terms_last_changed() {
wp_cache_set( 'last_changed', microtime(), 'terms' );
}
/**
* Aborts calls to term meta if it is not supported.
*
* @since 5.0.0
*
* @param mixed $check Skip-value for whether to proceed term meta function execution.
* @return mixed Original value of $check, or false if term meta is not supported.
*/
function wp_check_term_meta_support_prefilter( $check ) {
if ( get_option( 'db_version' ) < 34370 ) {
return false;
}
return $check;
}

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.1-alpha-43981';
$wp_version = '5.1-alpha-43982';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.