<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[43378] trunk: REST API: Support meta registration for specific object subtypes.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/43378">43378</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/43378","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>kadamwhite</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-06-21 21:06:50 +0000 (Thu, 21 Jun 2018)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>REST API: Support meta registration for specific object subtypes.
Introduce an `object_subtype` argument to the args array for `register_meta()` which can be used to limit meta registration to a single subtype (e.g. a custom post type or taxonomy, vs all posts or taxonomies).
Introduce `register_post_meta()` and `register_term_meta()` wrapper methods for `register_meta` to provide a convenient interface for the common case of registering meta for a specific taxonomy or post type. These methods work the way plugin developers have often expected `register_meta` to function, and should be used in place of direct `register_meta` where possible.
Props flixos90, tharsheblows, spacedmonkey.
Fixes <a href="https://core.trac.wordpress.org/ticket/38323">#38323</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescapabilitiesphp">trunk/src/wp-includes/capabilities.php</a></li>
<li><a href="#trunksrcwpincludesmetaphp">trunk/src/wp-includes/meta.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunksrcwpincludesrestapifieldsclasswprestcommentmetafieldsphp">trunk/src/wp-includes/rest-api/fields/class-wp-rest-comment-meta-fields.php</a></li>
<li><a href="#trunksrcwpincludesrestapifieldsclasswprestmetafieldsphp">trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php</a></li>
<li><a href="#trunksrcwpincludesrestapifieldsclasswprestpostmetafieldsphp">trunk/src/wp-includes/rest-api/fields/class-wp-rest-post-meta-fields.php</a></li>
<li><a href="#trunksrcwpincludesrestapifieldsclasswpresttermmetafieldsphp">trunk/src/wp-includes/rest-api/fields/class-wp-rest-term-meta-fields.php</a></li>
<li><a href="#trunksrcwpincludesrestapifieldsclasswprestusermetafieldsphp">trunk/src/wp-includes/rest-api/fields/class-wp-rest-user-meta-fields.php</a></li>
<li><a href="#trunksrcwpincludestaxonomyphp">trunk/src/wp-includes/taxonomy.php</a></li>
<li><a href="#trunktestsphpunittestsmetaregisterMetaphp">trunk/tests/phpunit/tests/meta/registerMeta.php</a></li>
<li><a href="#trunktestsphpunittestspostmetaphp">trunk/tests/phpunit/tests/post/meta.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestpostmetafieldsphp">trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiresttermmetafieldsphp">trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php</a></li>
<li><a href="#trunktestsphpunitteststermmetaphp">trunk/tests/phpunit/tests/term/meta.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescapabilitiesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/capabilities.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/capabilities.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/src/wp-includes/capabilities.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -281,45 +281,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> list( $_, $object_type, $_ ) = explode( '_', $cap );
</span><span class="cx" style="display: block; padding: 0 10px"> $object_id = (int) $args[0];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- switch ( $object_type ) {
- case 'post':
- $post = get_post( $object_id );
- if ( ! $post ) {
- break;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $object_subtype = get_object_subtype( $object_type, $object_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $sub_type = get_post_type( $post );
- break;
-
- case 'comment':
- $comment = get_comment( $object_id );
- if ( ! $comment ) {
- break;
- }
-
- $sub_type = empty( $comment->comment_type ) ? 'comment' : $comment->comment_type;
- break;
-
- case 'term':
- $term = get_term( $object_id );
- if ( ! $term instanceof WP_Term ) {
- break;
- }
-
- $sub_type = $term->taxonomy;
- break;
-
- case 'user':
- $user = get_user_by( 'id', $object_id );
- if ( ! $user ) {
- break;
- }
-
- $sub_type = 'user';
- break;
- }
-
- if ( empty( $sub_type ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $object_subtype ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $caps[] = 'do_not_allow';
</span><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -328,55 +292,79 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_key = isset( $args[1] ) ? $args[1] : false;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $has_filter = has_filter( "auth_{$object_type}_meta_{$meta_key}" ) || has_filter( "auth_{$object_type}_{$sub_type}_meta_{$meta_key}" );
- if ( $meta_key && $has_filter ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $meta_key ) {
+ $allowed = ! is_protected_meta( $meta_key, $object_type );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * Filters whether the user is allowed to edit meta for specific object types.
- *
- * Return true to have the mapped meta caps from `edit_{$object_type}` apply.
- *
- * The dynamic portion of the hook name, `$object_type` refers to the object type being filtered.
- * The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap().
- *
- * @since 3.3.0 As `auth_post_meta_{$meta_key}`.
- * @since 4.6.0
- *
- * @param bool $allowed Whether the user can add the object meta. Default false.
- * @param string $meta_key The meta key.
- * @param int $object_id Object ID.
- * @param int $user_id User ID.
- * @param string $cap Capability name.
- * @param string[] $caps Array of the user's capabilities.
- */
- $allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}", false, $meta_key, $object_id, $user_id, $cap, $caps );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $object_subtype ) && has_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * Filters whether the user is allowed to edit meta for specific object types/subtypes.
- *
- * Return true to have the mapped meta caps from `edit_{$object_type}` apply.
- *
- * The dynamic portion of the hook name, `$object_type` refers to the object type being filtered.
- * The dynamic portion of the hook name, `$sub_type` refers to the object subtype being filtered.
- * The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap().
- *
- * @since 4.6.0 As `auth_post_{$post_type}_meta_{$meta_key}`.
- * @since 4.7.0
- *
- * @param bool $allowed Whether the user can add the object meta. Default false.
- * @param string $meta_key The meta key.
- * @param int $object_id Object ID.
- * @param int $user_id User ID.
- * @param string $cap Capability name.
- * @param string[] $caps Array of the user's capabilities.
- */
- $allowed = apply_filters( "auth_{$object_type}_{$sub_type}_meta_{$meta_key}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters whether the user is allowed to edit a specific meta key of a specific object type and subtype.
+ *
+ * The dynamic portions of the hook name, `$object_type`, `$meta_key`,
+ * and `$object_subtype`, refer to the metadata object type (comment, post, term or user),
+ * the meta key value, and the object subtype respectively.
+ *
+ * @since 5.0.0
+ *
+ * @param bool $allowed Whether the user can add the object meta. Default false.
+ * @param string $meta_key The meta key.
+ * @param int $object_id Object ID.
+ * @param int $user_id User ID.
+ * @param string $cap Capability name.
+ * @param string[] $caps Array of the user's capabilities.
+ */
+ $allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps );
+ } else {
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters whether the user is allowed to edit a specific meta key of a specific object type.
+ *
+ * Return true to have the mapped meta caps from `edit_{$object_type}` apply.
+ *
+ * The dynamic portion of the hook name, `$object_type` refers to the object type being filtered.
+ * The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap().
+ *
+ * @since 3.3.0 As `auth_post_meta_{$meta_key}`.
+ * @since 4.6.0
+ *
+ * @param bool $allowed Whether the user can add the object meta. Default false.
+ * @param string $meta_key The meta key.
+ * @param int $object_id Object ID.
+ * @param int $user_id User ID.
+ * @param string $cap Capability name.
+ * @param string[] $caps Array of the user's capabilities.
+ */
+ $allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps );
+ }
+
+ if ( ! empty( $object_subtype ) ) {
+
+ /**
+ * Filters whether the user is allowed to edit meta for specific object types/subtypes.
+ *
+ * Return true to have the mapped meta caps from `edit_{$object_type}` apply.
+ *
+ * The dynamic portion of the hook name, `$object_type` refers to the object type being filtered.
+ * The dynamic portion of the hook name, `$object_subtype` refers to the object subtype being filtered.
+ * The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap().
+ *
+ * @since 4.6.0 As `auth_post_{$post_type}_meta_{$meta_key}`.
+ * @since 4.7.0
+ * @deprecated 5.0.0 Use `auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}`
+ *
+ * @param bool $allowed Whether the user can add the object meta. Default false.
+ * @param string $meta_key The meta key.
+ * @param int $object_id Object ID.
+ * @param int $user_id User ID.
+ * @param string $cap Capability name.
+ * @param string[] $caps Array of the user's capabilities.
+ */
+ $allowed = apply_filters_deprecated( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", array( $allowed, $meta_key, $object_id, $user_id, $cap, $caps ), '5.0.0', "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $allowed ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $caps[] = $cap;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } elseif ( $meta_key && is_protected_meta( $meta_key, $object_type ) ) {
- $caps[] = $cap;
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> case 'edit_comment':
</span></span></pre></div>
<a id="trunksrcwpincludesmetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/meta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/meta.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/src/wp-includes/meta.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -44,12 +44,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $meta_subtype = get_object_subtype( $meta_type, $object_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $column = sanitize_key( $meta_type . '_id' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // expected_slashed ($meta_key)
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_key = wp_unslash( $meta_key );
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_value = wp_unslash( $meta_value );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters whether to add metadata of a specific type.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -165,6 +167,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $meta_subtype = get_object_subtype( $meta_type, $object_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $column = sanitize_key( $meta_type . '_id' );
</span><span class="cx" style="display: block; padding: 0 10px"> $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -173,7 +177,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_key = wp_unslash( $meta_key );
</span><span class="cx" style="display: block; padding: 0 10px"> $passed_value = $meta_value;
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_value = wp_unslash( $meta_value );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters whether to update metadata of a specific type.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -666,9 +670,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $meta_subtype = get_object_subtype( $meta_type, $object_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Sanitize the meta
</span><span class="cx" style="display: block; padding: 0 10px"> $_meta_value = $meta_value;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
</ins><span class="cx" style="display: block; padding: 0 10px"> $meta_value = maybe_serialize( $meta_value );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Format the data query arguments.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -968,6 +974,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Sanitize meta value.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.1.3
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.0.0 The `$object_subtype` parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $meta_key Meta key.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param mixed $meta_value Meta value to sanitize.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -975,7 +982,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return mixed Sanitized $meta_value.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function sanitize_meta( $meta_key, $meta_value, $object_type ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) {
+ if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) {
+
+ /**
+ * Filters the sanitization of a specific meta key of a specific meta type and subtype.
+ *
+ * The dynamic portions of the hook name, `$object_type`, `$meta_key`,
+ * and `$object_subtype`, refer to the metadata object type (comment, post, term or user),
+ * the meta key value, and the object subtype respectively.
+ *
+ * @since 5.0.0
+ *
+ * @param mixed $meta_value Meta value to sanitize.
+ * @param string $meta_key Meta key.
+ * @param string $object_type Object type.
+ * @param string $object_subtype Object subtype.
+ */
+ return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $meta_value, $meta_key, $object_type, $object_subtype );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the sanitization of a specific meta key of a specific meta type.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -995,10 +1021,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Registers a meta key.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * It is recommended to register meta keys for a specific combination of object type and object subtype. If passing
+ * an object subtype is omitted, the meta key will be registered for the entire object type, however it can be partly
+ * overridden in case a more specific meta key of the same name exists for the same object type and a subtype.
+ *
+ * If an object type does not support any subtypes, such as users or comments, you should commonly call this function
+ * without passing a subtype.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 3.3.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified
</span><span class="cx" style="display: block; padding: 0 10px"> * to support an array of data to attach to registered meta keys}. Previous arguments for
</span><span class="cx" style="display: block; padding: 0 10px"> * `$sanitize_callback` and `$auth_callback` have been folded into this array.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.0.0 The `$object_subtype` argument was added to the arguments array.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $object_type Type of object this meta is registered to.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $meta_key Meta key to register.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1005,6 +1039,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $args {
</span><span class="cx" style="display: block; padding: 0 10px"> * Data used to describe the meta key when registered.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty,
+ * the meta key will be registered on the entire object type. Default empty.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @type string $type The type of data associated with this meta key.
</span><span class="cx" style="display: block; padding: 0 10px"> * Valid values are 'string', 'boolean', 'integer', and 'number'.
</span><span class="cx" style="display: block; padding: 0 10px"> * @type string $description A description of the data attached to this meta key.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1027,6 +1063,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $defaults = array(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'object_subtype' => '',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'single' => false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1067,6 +1104,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
</span><span class="cx" style="display: block; padding: 0 10px"> $args = wp_parse_args( $args, $defaults );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // If `auth_callback` is not provided, fall back to `is_protected_meta()`.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $args['auth_callback'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_protected_meta( $meta_key, $object_type ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1078,17 +1117,27 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Back-compat: old sanitize and auth callbacks are applied to all of an object type.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_callable( $args['sanitize_callback'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $object_subtype ) ) {
+ add_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'], 10, 4 );
+ } else {
+ add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_callable( $args['auth_callback'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $object_subtype ) ) {
+ add_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'], 10, 6 );
+ } else {
+ add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $wp_meta_keys[ $object_type ][ $meta_key ] = $args;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ unset( $args['object_subtype'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1099,28 +1148,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Checks if a meta key is registered.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.6.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.0.0 The `$object_subtype` parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $object_type The type of object.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $meta_key The meta key.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $object_subtype Optional. The subtype of the object type.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return bool True if the meta key is registered to the object type. False if not.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return bool True if the meta key is registered to the object type and, if provided,
+ * the object subtype. False if not.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function registered_meta_key_exists( $object_type, $meta_key ) {
- global $wp_meta_keys;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) {
+ $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! is_array( $wp_meta_keys ) ) {
- return false;
- }
-
- if ( ! isset( $wp_meta_keys[ $object_type ] ) ) {
- return false;
- }
-
- if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) {
- return true;
- }
-
- return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return isset( $meta_keys[ $meta_key ] );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1127,31 +1167,44 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Unregisters a meta key from the list of registered keys.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.6.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.0.0 The `$object_subtype` parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $object_type The type of object.
- * @param string $meta_key The meta key.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $object_type The type of object.
+ * @param string $meta_key The meta key.
+ * @param string $object_subtype Optional. The subtype of the object type.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return bool True if successful. False if the meta key was not registered.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function unregister_meta_key( $object_type, $meta_key ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> global $wp_meta_keys;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $args = $wp_meta_keys[ $object_type ][ $meta_key ];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ];
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $object_subtype ) ) {
+ remove_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'] );
+ } else {
+ remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $object_subtype ) ) {
+ remove_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'] );
+ } else {
+ remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- unset( $wp_meta_keys[ $object_type ][ $meta_key ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Do some clean up
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
+ unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $wp_meta_keys[ $object_type ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $wp_meta_keys[ $object_type ] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1163,23 +1216,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieves a list of registered meta keys for an object type.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.6.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.0.0 The `$object_subtype` parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $object_type The type of object. Post, comment, user, term.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $object_type The type of object. Post, comment, user, term.
+ * @param string $object_subtype Optional. The subtype of the object type.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return array List of registered meta keys.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function get_registered_meta_keys( $object_type ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> global $wp_meta_keys;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return array();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $wp_meta_keys[ $object_type ];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $wp_meta_keys[ $object_type ][ $object_subtype ];
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieves registered metadata for a specified object.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * The results include both meta that is registered specifically for the
+ * object's subtype and meta that is registered for the entire object type.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 4.6.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1187,14 +1245,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered
</span><span class="cx" style="display: block; padding: 0 10px"> * metadata for the specified object.
</span><span class="cx" style="display: block; padding: 0 10px"> * @return mixed A single value or array of values for a key if specified. An array of all registered keys
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * and values for an object ID if not.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * and values for an object ID if not. False if a given $meta_key is not registered.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $object_subtype = get_object_subtype( $object_type, $object_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $meta_key ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $object_subtype ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
+ $object_subtype = '';
+ }
+
+ if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $meta_keys = get_registered_meta_keys( $object_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
</ins><span class="cx" style="display: block; padding: 0 10px"> $meta_key_data = $meta_keys[ $meta_key ];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1203,18 +1268,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $data = get_metadata( $object_type, $object_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $data ) {
+ return array();
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $meta_keys = get_registered_meta_keys( $object_type );
- $registered_data = array();
-
- // Someday, array_filter()
- foreach ( $meta_keys as $k => $v ) {
- if ( isset( $data[ $k ] ) ) {
- $registered_data[ $k ] = $data[ $k ];
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $meta_keys = get_registered_meta_keys( $object_type );
+ if ( ! empty( $object_subtype ) ) {
+ $meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $registered_data;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return array_intersect_key( $data, $meta_keys );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1223,22 +1286,77 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * to be explicitly turned off is a warranty seal of sorts.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 4.6.0
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.6.0
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param array $args Arguments from `register_meta()`.
- * @param array $default_args Default arguments for `register_meta()`.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $args Arguments from `register_meta()`.
+ * @param array $default_args Default arguments for `register_meta()`.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array Filtered arguments.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function _wp_register_meta_args_whitelist( $args, $default_args ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $whitelist = array_keys( $default_args );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return array_intersect_key( $args, $default_args );
+}
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // In an anonymous function world, this would be better as an array_filter()
- foreach ( $args as $key => $value ) {
- if ( ! in_array( $key, $whitelist ) ) {
- unset( $args[ $key ] );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Returns the object subtype for a given object ID of a specific type.
+ *
+ * @since 5.0.0
+ *
+ * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
+ * @param int $object_id ID of the object to retrieve its subtype.
+ * @return string The object subtype or an empty string if unspecified subtype.
+ */
+function get_object_subtype( $object_type, $object_id ) {
+ $object_id = (int) $object_id;
+ $object_subtype = '';
+
+ switch ( $object_type ) {
+ case 'post':
+ $post_type = get_post_type( $object_id );
+
+ if ( ! empty( $post_type ) ) {
+ $object_subtype = $post_type;
+ }
+ break;
+
+ case 'term':
+ $term = get_term( $object_id );
+ if ( ! $term instanceof WP_Term ) {
+ break;
+ }
+
+ $object_subtype = $term->taxonomy;
+ break;
+
+ case 'comment':
+ $comment = get_comment( $object_id );
+ if ( ! $comment ) {
+ break;
+ }
+
+ $object_subtype = 'comment';
+ break;
+
+ case 'user':
+ $user = get_user_by( 'id', $object_id );
+ if ( ! $user ) {
+ break;
+ }
+
+ $object_subtype = 'user';
+ break;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $args;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters the object subtype identifier for a non standard object type.
+ *
+ * The dynamic portion of the hook, `$object_type`, refers to the object
+ * type (post, comment, term, or user).
+ *
+ * @since 5.0.0
+ *
+ * @param string $object_subtype Empty string to override.
+ * @param int $object_id ID of the object to get the subtype for.
+ */
+ return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/src/wp-includes/post.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1990,6 +1990,39 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Registers a meta key for posts.
+ *
+ * @since 5.0.0
+ *
+ * @param string $post_type Post type to register a meta key for. Pass an empty string
+ * to register the meta key across all existing post types.
+ * @param string $meta_key The meta key to register.
+ * @param array $args Data used to describe the meta key when registered. See
+ * {@see register_meta()} for a list of supported arguments.
+ * @return bool True if the meta key was successfully registered, false if not.
+ */
+function register_post_meta( $post_type, $meta_key, array $args ) {
+ $args['object_subtype'] = $post_type;
+
+ return register_meta( 'post', $meta_key, $args );
+}
+
+/**
+ * Unregisters a meta key for posts.
+ *
+ * @since 5.0.0
+ *
+ * @param string $post_type Post type the meta key is currently registered for. Pass
+ * an empty string if the meta key is registered across all
+ * existing post types.
+ * @param string $meta_key The meta key to unregister.
+ * @return bool True on success, false if the meta key was not previously registered.
+ */
+function unregister_post_meta( $post_type, $meta_key ) {
+ return unregister_meta_key( 'post', $meta_key, $post_type );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Retrieve post meta fields, based on post ID.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * The post meta fields are retrieved from the cache where possible,
</span></span></pre></div>
<a id="trunksrcwpincludesrestapifieldsclasswprestcommentmetafieldsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/fields/class-wp-rest-comment-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/fields/class-wp-rest-comment-meta-fields.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/src/wp-includes/rest-api/fields/class-wp-rest-comment-meta-fields.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -28,6 +28,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves the object meta subtype.
+ *
+ * @since 5.0.0
+ *
+ * @return string 'comment' There are no subtypes.
+ */
+ protected function get_meta_subtype() {
+ return 'comment';
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the type for register_rest_field() in the context of comments.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span></span></pre></div>
<a id="trunksrcwpincludesrestapifieldsclasswprestmetafieldsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -25,6 +25,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> abstract protected function get_meta_type();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves the object meta subtype.
+ *
+ * @since 5.0.0
+ *
+ * @return string Subtype for the meta type, or empty string if no specific subtype.
+ */
+ protected function get_meta_subtype() {
+ return '';
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the object type for register_rest_field().
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -340,7 +351,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> protected function get_registered_fields() {
</span><span class="cx" style="display: block; padding: 0 10px"> $registered = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( get_registered_meta_keys( $this->get_meta_type() ) as $name => $args ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $meta_type = $this->get_meta_type();
+ $meta_subtype = $this->get_meta_subtype();
+
+ $meta_keys = get_registered_meta_keys( $meta_type );
+ if ( ! empty( $meta_subtype ) ) {
+ $meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $meta_type, $meta_subtype ) );
+ }
+
+ foreach ( $meta_keys as $name => $args ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $args['show_in_rest'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesrestapifieldsclasswprestpostmetafieldsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/fields/class-wp-rest-post-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/fields/class-wp-rest-post-meta-fields.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/src/wp-includes/rest-api/fields/class-wp-rest-post-meta-fields.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -47,6 +47,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves the object meta subtype.
+ *
+ * @since 5.0.0
+ *
+ * @return string Subtype for the meta type, or empty string if no specific subtype.
+ */
+ protected function get_meta_subtype() {
+ return $this->post_type;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the type for register_rest_field().
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span></span></pre></div>
<a id="trunksrcwpincludesrestapifieldsclasswpresttermmetafieldsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/fields/class-wp-rest-term-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/fields/class-wp-rest-term-meta-fields.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/src/wp-includes/rest-api/fields/class-wp-rest-term-meta-fields.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -47,6 +47,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves the object meta subtype.
+ *
+ * @since 5.0.0
+ *
+ * @return string Subtype for the meta type, or empty string if no specific subtype.
+ */
+ protected function get_meta_subtype() {
+ return $this->taxonomy;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the type for register_rest_field().
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span></span></pre></div>
<a id="trunksrcwpincludesrestapifieldsclasswprestusermetafieldsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/fields/class-wp-rest-user-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/fields/class-wp-rest-user-meta-fields.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/src/wp-includes/rest-api/fields/class-wp-rest-user-meta-fields.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -28,6 +28,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves the object meta subtype.
+ *
+ * @since 5.0.0
+ *
+ * @return string 'user' There are no subtypes.
+ */
+ protected function get_meta_subtype() {
+ return 'user';
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the type for register_rest_field().
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span></span></pre></div>
<a id="trunksrcwpincludestaxonomyphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/src/wp-includes/taxonomy.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1323,6 +1323,39 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Registers a meta key for terms.
+ *
+ * @since 5.0.0
+ *
+ * @param string $taxonomy Taxonomy to register a meta key for. Pass an empty string
+ * to register the meta key across all existing taxonomies.
+ * @param string $meta_key The meta key to register.
+ * @param array $args Data used to describe the meta key when registered. See
+ * {@see register_meta()} for a list of supported arguments.
+ * @return bool True if the meta key was successfully registered, false if not.
+ */
+function register_term_meta( $taxonomy, $meta_key, array $args ) {
+ $args['object_subtype'] = $taxonomy;
+
+ return register_meta( 'term', $meta_key, $args );
+}
+
+/**
+ * Unregisters a meta key for terms.
+ *
+ * @since 5.0.0
+ *
+ * @param string $taxonomy Taxonomy the meta key is currently registered for. Pass
+ * an empty string if the meta key is registered across all
+ * existing taxonomies.
+ * @param string $meta_key The meta key to unregister.
+ * @return bool True on success, false if the meta key was not previously registered.
+ */
+function unregister_term_meta( $taxonomy, $meta_key ) {
+ return unregister_meta_key( 'term', $meta_key, $taxonomy );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Determines whether a term exists.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Formerly is_term(), introduced in 2.3.0.
</span></span></pre></div>
<a id="trunktestsphpunittestsmetaregisterMetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/meta/registerMeta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/meta/registerMeta.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/tests/phpunit/tests/meta/registerMeta.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3,12 +3,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @group meta
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Meta_Register_Meta extends WP_UnitTestCase {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> protected static $post_id;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected static $term_id;
+ protected static $comment_id;
+ protected static $user_id;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpSetUpBeforeClass( $factory ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$post_id = $factory->post->create();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$post_id = $factory->post->create( array( 'post_type' => 'page' ) );
+ self::$term_id = $factory->term->create( array( 'taxonomy' => 'category' ) );
+ self::$comment_id = $factory->comment->create();
+ self::$user_id = $factory->user->create();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static function wpTearDownAfterClass() {
+ wp_delete_post( self::$post_id, true );
+ wp_delete_term( self::$term_id, 'category' );
+ wp_delete_comment( self::$comment_id, true );
+ self::delete_user( self::$user_id );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function _old_sanitize_meta_cb( $meta_value, $meta_key, $meta_type ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $meta_key . ' old sanitized';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -74,18 +88,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'post' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'flight_number' => array(
- 'type' => 'string',
- 'description' => '',
- 'single' => false,
- 'sanitize_callback' => null,
- 'auth_callback' => '__return_true',
- 'show_in_rest' => false,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '' => array(
+ 'flight_number' => array(
+ 'type' => 'string',
+ 'description' => '',
+ 'single' => false,
+ 'sanitize_callback' => null,
+ 'auth_callback' => '__return_true',
+ 'show_in_rest' => false,
+ ),
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( $actual, $expected );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( $expected, $actual );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_register_meta_with_term_object_type_populates_wp_meta_keys() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -96,18 +112,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'term' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'category_icon' => array(
- 'type' => 'string',
- 'description' => '',
- 'single' => false,
- 'sanitize_callback' => null,
- 'auth_callback' => '__return_true',
- 'show_in_rest' => false,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '' => array(
+ 'category_icon' => array(
+ 'type' => 'string',
+ 'description' => '',
+ 'single' => false,
+ 'sanitize_callback' => null,
+ 'auth_callback' => '__return_true',
+ 'show_in_rest' => false,
+ ),
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( $actual, $expected );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( $expected, $actual );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_register_meta_with_deprecated_sanitize_callback_does_not_populate_wp_meta_keys() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -148,13 +166,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'post' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'flight_number' => array(
- 'type' => 'string',
- 'description' => '',
- 'single' => false,
- 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ),
- 'auth_callback' => '__return_true',
- 'show_in_rest' => false,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '' => array(
+ 'flight_number' => array(
+ 'type' => 'string',
+ 'description' => '',
+ 'single' => false,
+ 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ),
+ 'auth_callback' => '__return_true',
+ 'show_in_rest' => false,
+ ),
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -301,4 +321,190 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEmpty( $meta );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @ticket 38323
+ * @dataProvider data_get_types_and_subtypes
+ */
+ public function test_register_meta_with_subtype_populates_wp_meta_keys( $type, $subtype ) {
+ global $wp_meta_keys;
+
+ register_meta( $type, 'flight_number', array( 'object_subtype' => $subtype ) );
+
+ $expected = array(
+ $type => array(
+ $subtype => array(
+ 'flight_number' => array(
+ 'type' => 'string',
+ 'description' => '',
+ 'single' => false,
+ 'sanitize_callback' => null,
+ 'auth_callback' => '__return_true',
+ 'show_in_rest' => false,
+ ),
+ ),
+ ),
+ );
+
+ $actual = $wp_meta_keys;
+
+ // Reset global so subsequent data tests do not get polluted.
+ $wp_meta_keys = array();
+
+ $this->assertEquals( $expected, $actual );
+ }
+
+ /**
+ * @ticket 38323
+ * @dataProvider data_get_types_and_subtypes
+ */
+ public function test_unregister_meta_with_subtype_unpopulates_wp_meta_keys( $type, $subtype ) {
+ global $wp_meta_keys;
+
+ register_meta( $type, 'flight_number', array( 'object_subtype' => $subtype ) );
+ unregister_meta_key( $type, 'flight_number', $subtype );
+
+ $actual = $wp_meta_keys;
+
+ // Reset global so subsequent data tests do not get polluted.
+ $wp_meta_keys = array();
+
+ $this->assertEmpty( $actual );
+ }
+
+ /**
+ * @ticket 38323
+ * @dataProvider data_get_types_and_subtypes
+ */
+ public function test_unregister_meta_without_subtype_keeps_subtype_meta_key( $type, $subtype ) {
+ global $wp_meta_keys;
+
+ register_meta( $type, 'flight_number', array( 'object_subtype' => $subtype ) );
+
+ // Unregister meta key without subtype.
+ unregister_meta_key( $type, 'flight_number' );
+
+ $expected = array(
+ $type => array(
+ $subtype => array(
+ 'flight_number' => array(
+ 'type' => 'string',
+ 'description' => '',
+ 'single' => false,
+ 'sanitize_callback' => null,
+ 'auth_callback' => '__return_true',
+ 'show_in_rest' => false,
+ ),
+ ),
+ ),
+ );
+
+ $actual = $wp_meta_keys;
+
+ // Reset global so subsequent data tests do not get polluted.
+ $wp_meta_keys = array();
+
+ $this->assertEquals( $expected, $actual );
+ }
+
+ /**
+ * @ticket 38323
+ * @dataProvider data_get_types_and_subtypes
+ */
+ public function test_get_registered_meta_keys_with_subtype( $type, $subtype ) {
+ register_meta( $type, 'registered_key1', array( 'object_subtype' => $subtype ) );
+ register_meta( $type, 'registered_key2', array( 'object_subtype' => $subtype ) );
+
+ $meta_keys = get_registered_meta_keys( $type, $subtype );
+
+ $this->assertArrayHasKey( 'registered_key1', $meta_keys );
+ $this->assertArrayHasKey( 'registered_key2', $meta_keys );
+ $this->assertEmpty( get_registered_meta_keys( $type ) );
+ }
+
+ /**
+ * @ticket 38323
+ * @dataProvider data_get_types_and_subtypes
+ */
+ public function test_get_registered_metadata_with_subtype( $type, $subtype ) {
+ register_meta( $type, 'registered_key1', array() );
+
+ // This will override the above registration for objects of $subtype.
+ register_meta( $type, 'registered_key1', array(
+ 'object_subtype' => $subtype,
+ 'single' => true,
+ ) );
+
+ // For testing with $single => false.
+ register_meta( $type, 'registered_key2', array(
+ 'object_subtype' => $subtype,
+ ) );
+
+ // Register another meta key for a different subtype.
+ register_meta( $type, 'registered_key3', array(
+ 'object_subtype' => 'other_subtype',
+ ) );
+
+ $object_property_name = $type . '_id';
+ $object_id = self::$$object_property_name;
+
+ add_metadata( $type, $object_id, 'registered_key1', 'value1' );
+ add_metadata( $type, $object_id, 'registered_key2', 'value2' );
+ add_metadata( $type, $object_id, 'registered_key3', 'value3' );
+
+ $meta = get_registered_metadata( $type, $object_id );
+
+ $key1 = get_registered_metadata( $type, $object_id, 'registered_key1' );
+ $key2 = get_registered_metadata( $type, $object_id, 'registered_key2' );
+ $key3 = get_registered_metadata( $type, $object_id, 'registered_key3' );
+
+ $this->assertSame( array( 'registered_key1', 'registered_key2' ), array_keys( $meta ) );
+ $this->assertSame( 'value1', $meta['registered_key1'][0] );
+ $this->assertSame( 'value2', $meta['registered_key2'][0] );
+
+ $this->assertSame( 'value1', $key1 );
+ $this->assertSame( array( 'value2' ), $key2 );
+ $this->assertFalse( $key3 );
+ }
+
+ /**
+ * @ticket 38323
+ * @dataProvider data_get_types_and_subtypes
+ */
+ public function test_get_object_subtype( $type, $expected_subtype ) {
+ $object_property_name = $type . '_id';
+ $object_id = self::$$object_property_name;
+
+ $this->assertSame( $expected_subtype, get_object_subtype( $type, $object_id ) );
+ }
+
+ /**
+ * @ticket 38323
+ */
+ public function test_get_object_subtype_custom() {
+ add_filter( 'get_object_subtype_customtype', array( $this, 'filter_get_object_subtype_for_customtype' ), 10, 2 );
+
+ $subtype_for_3 = get_object_subtype( 'customtype', 3 );
+ $subtype_for_4 = get_object_subtype( 'customtype', 4 );
+
+ $this->assertSame( 'odd', $subtype_for_3 );
+ $this->assertSame( 'even', $subtype_for_4 );
+ }
+
+ public function filter_get_object_subtype_for_customtype( $subtype, $object_id ) {
+ if ( $object_id % 2 === 1 ) {
+ return 'odd';
+ }
+
+ return 'even';
+ }
+
+ public function data_get_types_and_subtypes() {
+ return array(
+ array( 'post', 'page' ),
+ array( 'term', 'category' ),
+ array( 'comment', 'comment' ),
+ array( 'user', 'user' ),
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestspostmetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/post/meta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/meta.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/tests/phpunit/tests/post/meta.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,6 +6,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Post_Meta extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private $last_register_meta_call = array(
+ 'object_type' => '',
+ 'meta_key' => '',
+ 'args' => array(),
+ );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> protected static $author;
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $post_id;
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $post_id_2;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -238,4 +244,65 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $funky_meta, get_post_meta( self::$post_id, 'test_funky_post_meta', true ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @ticket 38323
+ * @dataProvider data_register_post_meta
+ */
+ public function test_register_post_meta( $post_type, $meta_key, $args ) {
+ add_filter( 'register_meta_args', array( $this, 'filter_register_meta_args_set_last_register_meta_call' ), 10, 4 );
+
+ register_post_meta( $post_type, $meta_key, $args );
+
+ $args['object_subtype'] = $post_type;
+
+ // Reset global so subsequent data tests do not get polluted.
+ $GLOBALS['wp_meta_keys'] = array();
+
+ $this->assertEquals( 'post', $this->last_register_meta_call['object_type'] );
+ $this->assertEquals( $meta_key, $this->last_register_meta_call['meta_key'] );
+ $this->assertEquals( $args, $this->last_register_meta_call['args'] );
+ }
+
+ public function data_register_post_meta() {
+ return array(
+ array( 'post', 'registered_key1', array( 'single' => true ) ),
+ array( 'page', 'registered_key2', array() ),
+ array( '', 'registered_key3', array( 'sanitize_callback' => 'absint' ) ),
+ );
+ }
+
+ public function filter_register_meta_args_set_last_register_meta_call( $args, $defaults, $object_type, $meta_key ) {
+ $this->last_register_meta_call['object_type'] = $object_type;
+ $this->last_register_meta_call['meta_key'] = $meta_key;
+ $this->last_register_meta_call['args'] = $args;
+
+ return $args;
+ }
+
+ /**
+ * @ticket 38323
+ * @dataProvider data_unregister_post_meta
+ */
+ public function test_unregister_post_meta( $post_type, $meta_key ) {
+ global $wp_meta_keys;
+
+ register_post_meta( $post_type, $meta_key, array() );
+ unregister_post_meta( $post_type, $meta_key );
+
+ $actual = $wp_meta_keys;
+
+ // Reset global so subsequent data tests do not get polluted.
+ $wp_meta_keys = array();
+
+ $this->assertEmpty( $actual );
+ }
+
+ public function data_unregister_post_meta() {
+ return array(
+ array( 'post', 'registered_key1' ),
+ array( 'page', 'registered_key2' ),
+ array( '', 'registered_key3' ),
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestpostmetafieldsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,15 +12,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $wp_meta_keys_saved;
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $post_id;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected static $cpt_post_id;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpSetUpBeforeClass( $factory ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ register_post_type( 'cpt', array(
+ 'show_in_rest' => true,
+ 'supports' => array( 'custom-fields' ),
+ ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> self::$wp_meta_keys_saved = isset( $GLOBALS['wp_meta_keys'] ) ? $GLOBALS['wp_meta_keys'] : array();
</span><span class="cx" style="display: block; padding: 0 10px"> self::$post_id = $factory->post->create();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$cpt_post_id = $factory->post->create( array( 'post_type' => 'cpt' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpTearDownAfterClass() {
</span><span class="cx" style="display: block; padding: 0 10px"> $GLOBALS['wp_meta_keys'] = self::$wp_meta_keys_saved;
</span><span class="cx" style="display: block; padding: 0 10px"> wp_delete_post( self::$post_id, true );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_delete_post( self::$cpt_post_id, true );
+
+ unregister_post_type( 'cpt' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function setUp() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -120,6 +130,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ register_post_type( 'cpt', array(
+ 'show_in_rest' => true,
+ 'supports' => array( 'custom-fields' ),
+ ) );
+
+ register_post_meta( 'cpt', 'test_cpt_single', array(
+ 'show_in_rest' => true,
+ 'single' => true,
+ ) );
+
+ register_post_meta( 'cpt', 'test_cpt_multi', array(
+ 'show_in_rest' => true,
+ 'single' => false,
+ ) );
+
+ // Register 'test_single' on subtype to override for bad auth.
+ register_post_meta( 'cpt', 'test_single', array(
+ 'show_in_rest' => true,
+ 'single' => true,
+ 'auth_callback' => '__return_false',
+ ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /** @var WP_REST_Server $wp_rest_server */
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_rest_server;
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_rest_server = new Spy_REST_Server;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1048,6 +1080,126 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 38323
+ * @dataProvider data_get_subtype_meta_value
+ */
+ public function test_get_subtype_meta_value( $post_type, $meta_key, $single, $in_post_type ) {
+ $post_id = self::$post_id;
+ $endpoint = 'posts';
+ if ( 'cpt' === $post_type ) {
+ $post_id = self::$cpt_post_id;
+ $endpoint = 'cpt';
+ }
+
+ $meta_value = 'testvalue';
+
+ add_post_meta( $post_id, $meta_key, $meta_value );
+
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/%s/%d', $endpoint, $post_id ) );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+
+ $this->assertArrayHasKey( 'meta', $data );
+ $this->assertInternalType( 'array', $data['meta'] );
+
+ if ( $in_post_type ) {
+ $expected_value = $meta_value;
+ if ( ! $single ) {
+ $expected_value = array( $expected_value );
+ }
+
+ $this->assertArrayHasKey( $meta_key, $data['meta'] );
+ $this->assertEquals( $expected_value, $data['meta'][ $meta_key ] );
+ } else {
+ $this->assertArrayNotHasKey( $meta_key, $data['meta'] );
+ }
+ }
+
+ public function data_get_subtype_meta_value() {
+ return array(
+ array( 'cpt', 'test_cpt_single', true, true ),
+ array( 'cpt', 'test_cpt_multi', false, true ),
+ array( 'cpt', 'test_single', true, true ),
+ array( 'cpt', 'test_multi', false, true ),
+ array( 'post', 'test_cpt_single', true, false ),
+ array( 'post', 'test_cpt_multi', false, false ),
+ array( 'post', 'test_single', true, true ),
+ array( 'post', 'test_multi', false, true ),
+ );
+ }
+
+ /**
+ * @ticket 38323
+ * @dataProvider data_set_subtype_meta_value
+ */
+ public function test_set_subtype_meta_value( $post_type, $meta_key, $single, $in_post_type, $can_write ) {
+ $post_id = self::$post_id;
+ $endpoint = 'posts';
+ if ( 'cpt' === $post_type ) {
+ $post_id = self::$cpt_post_id;
+ $endpoint = 'cpt';
+ }
+
+ $meta_value = 'value_to_set';
+
+ $this->grant_write_permission();
+
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/%s/%d', $endpoint, $post_id ) );
+ $request->set_body_params( array(
+ 'meta' => array(
+ $meta_key => $meta_value,
+ ),
+ ) );
+
+ $response = rest_get_server()->dispatch( $request );
+ if ( ! $can_write ) {
+ $this->assertEquals( 403, $response->get_status() );
+ $this->assertEmpty( get_post_meta( $post_id, $meta_key, $single ) );
+ return;
+ }
+
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $this->assertArrayHasKey( 'meta', $data );
+ $this->assertInternalType( 'array', $data['meta'] );
+
+ if ( $in_post_type ) {
+ $expected_value = $meta_value;
+ if ( ! $single ) {
+ $expected_value = array( $expected_value );
+ }
+
+ $this->assertEquals( $expected_value, get_post_meta( $post_id, $meta_key, $single ) );
+ $this->assertArrayHasKey( $meta_key, $data['meta'] );
+ $this->assertEquals( $expected_value, $data['meta'][ $meta_key ] );
+ } else {
+ $this->assertEmpty( get_post_meta( $post_id, $meta_key, $single ) );
+ $this->assertArrayNotHasKey( $meta_key, $data['meta'] );
+ }
+ }
+
+ public function data_set_subtype_meta_value() {
+ $data = $this->data_get_subtype_meta_value();
+
+ foreach ( $data as $index => $dataset ) {
+ $can_write = true;
+
+ // This combination is not writable because of an auth callback of '__return_false'.
+ if ( 'cpt' === $dataset[0] && 'test_single' === $dataset[1] ) {
+ $can_write = false;
+ }
+
+ $data[ $index ][] = $can_write;
+ }
+
+ return $data;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Internal function used to disable an insert query which
</span><span class="cx" style="display: block; padding: 0 10px"> * will trigger a wpdb error for testing purposes.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiresttermmetafieldsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,15 +12,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class WP_Test_REST_Term_Meta_Fields extends WP_Test_REST_TestCase {
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $wp_meta_keys_saved;
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $category_id;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected static $customtax_term_id;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpSetUpBeforeClass( $factory ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ register_taxonomy( 'customtax', 'post', array(
+ 'show_in_rest' => true,
+ ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> self::$wp_meta_keys_saved = isset( $GLOBALS['wp_meta_keys'] ) ? $GLOBALS['wp_meta_keys'] : array();
</span><span class="cx" style="display: block; padding: 0 10px"> self::$category_id = $factory->category->create();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$customtax_term_id = $factory->term->create( array( 'taxonomy' => 'customtax' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpTearDownAfterClass() {
</span><span class="cx" style="display: block; padding: 0 10px"> $GLOBALS['wp_meta_keys'] = self::$wp_meta_keys_saved;
</span><span class="cx" style="display: block; padding: 0 10px"> wp_delete_term( self::$category_id, 'category' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_delete_term( self::$customtax_term_id, 'customtax' );
+
+ unregister_taxonomy( 'customtax' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function setUp() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -120,6 +129,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ register_taxonomy( 'customtax', 'post', array(
+ 'show_in_rest' => true,
+ ) );
+
+ register_term_meta( 'customtax', 'test_customtax_single', array(
+ 'show_in_rest' => true,
+ 'single' => true,
+ ) );
+
+ register_term_meta( 'customtax', 'test_customtax_multi', array(
+ 'show_in_rest' => true,
+ 'single' => false,
+ ) );
+
+ // Register 'test_single' on subtype to override for bad auth.
+ register_term_meta( 'customtax', 'test_single', array(
+ 'show_in_rest' => true,
+ 'single' => true,
+ 'auth_callback' => '__return_false',
+ ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /** @var WP_REST_Server $wp_rest_server */
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_rest_server;
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_rest_server = new Spy_REST_Server;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1048,6 +1078,126 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 38323
+ * @dataProvider data_get_subtype_meta_value
+ */
+ public function test_get_subtype_meta_value( $taxonomy, $meta_key, $single, $in_taxonomy ) {
+ $term_id = self::$category_id;
+ $endpoint = 'categories';
+ if ( 'customtax' === $taxonomy ) {
+ $term_id = self::$customtax_term_id;
+ $endpoint = 'customtax';
+ }
+
+ $meta_value = 'testvalue';
+
+ add_term_meta( $term_id, $meta_key, $meta_value );
+
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/%s/%d', $endpoint, $term_id ) );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+
+ $this->assertArrayHasKey( 'meta', $data );
+ $this->assertInternalType( 'array', $data['meta'] );
+
+ if ( $in_taxonomy ) {
+ $expected_value = $meta_value;
+ if ( ! $single ) {
+ $expected_value = array( $expected_value );
+ }
+
+ $this->assertArrayHasKey( $meta_key, $data['meta'] );
+ $this->assertEquals( $expected_value, $data['meta'][ $meta_key ] );
+ } else {
+ $this->assertArrayNotHasKey( $meta_key, $data['meta'] );
+ }
+ }
+
+ public function data_get_subtype_meta_value() {
+ return array(
+ array( 'customtax', 'test_customtax_single', true, true ),
+ array( 'customtax', 'test_customtax_multi', false, true ),
+ array( 'customtax', 'test_single', true, true ),
+ array( 'customtax', 'test_multi', false, true ),
+ array( 'category', 'test_customtax_single', true, false ),
+ array( 'category', 'test_customtax_multi', false, false ),
+ array( 'category', 'test_single', true, true ),
+ array( 'category', 'test_multi', false, true ),
+ );
+ }
+
+ /**
+ * @ticket 38323
+ * @dataProvider data_set_subtype_meta_value
+ */
+ public function test_set_subtype_meta_value( $taxonomy, $meta_key, $single, $in_taxonomy, $can_write ) {
+ $term_id = self::$category_id;
+ $endpoint = 'categories';
+ if ( 'customtax' === $taxonomy ) {
+ $term_id = self::$customtax_term_id;
+ $endpoint = 'customtax';
+ }
+
+ $meta_value = 'value_to_set';
+
+ $this->grant_write_permission();
+
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/%s/%d', $endpoint, $term_id ) );
+ $request->set_body_params( array(
+ 'meta' => array(
+ $meta_key => $meta_value,
+ ),
+ ) );
+
+ $response = rest_get_server()->dispatch( $request );
+ if ( ! $can_write ) {
+ $this->assertEquals( 403, $response->get_status() );
+ $this->assertEmpty( get_term_meta( $term_id, $meta_key, $single ) );
+ return;
+ }
+
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $this->assertArrayHasKey( 'meta', $data );
+ $this->assertInternalType( 'array', $data['meta'] );
+
+ if ( $in_taxonomy ) {
+ $expected_value = $meta_value;
+ if ( ! $single ) {
+ $expected_value = array( $expected_value );
+ }
+
+ $this->assertEquals( $expected_value, get_term_meta( $term_id, $meta_key, $single ) );
+ $this->assertArrayHasKey( $meta_key, $data['meta'] );
+ $this->assertEquals( $expected_value, $data['meta'][ $meta_key ] );
+ } else {
+ $this->assertEmpty( get_term_meta( $term_id, $meta_key, $single ) );
+ $this->assertArrayNotHasKey( $meta_key, $data['meta'] );
+ }
+ }
+
+ public function data_set_subtype_meta_value() {
+ $data = $this->data_get_subtype_meta_value();
+
+ foreach ( $data as $index => $dataset ) {
+ $can_write = true;
+
+ // This combination is not writable because of an auth callback of '__return_false'.
+ if ( 'customtax' === $dataset[0] && 'test_single' === $dataset[1] ) {
+ $can_write = false;
+ }
+
+ $data[ $index ][] = $can_write;
+ }
+
+ return $data;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Internal function used to disable an insert query which
</span><span class="cx" style="display: block; padding: 0 10px"> * will trigger a wpdb error for testing purposes.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span></span></pre></div>
<a id="trunktestsphpunitteststermmetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/term/meta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/meta.php 2018-06-20 04:20:32 UTC (rev 43377)
+++ trunk/tests/phpunit/tests/term/meta.php 2018-06-21 21:06:50 UTC (rev 43378)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,6 +6,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 10142
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Term_Meta extends WP_UnitTestCase {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ private $last_register_meta_call = array(
+ 'object_type' => '',
+ 'meta_key' => '',
+ 'args' => array(),
+ );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function setUp() {
</span><span class="cx" style="display: block; padding: 0 10px"> parent::setUp();
</span><span class="cx" style="display: block; padding: 0 10px"> register_taxonomy( 'wptests_tax', 'post' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -463,4 +470,65 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public static function set_cache_results( $q ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $q->set( 'cache_results', true );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @ticket 38323
+ * @dataProvider data_register_term_meta
+ */
+ public function test_register_term_meta( $taxonomy, $meta_key, $args ) {
+ add_filter( 'register_meta_args', array( $this, 'filter_register_meta_args_set_last_register_meta_call' ), 10, 4 );
+
+ register_term_meta( $taxonomy, $meta_key, $args );
+
+ $args['object_subtype'] = $taxonomy;
+
+ // Reset global so subsequent data tests do not get polluted.
+ $GLOBALS['wp_meta_keys'] = array();
+
+ $this->assertEquals( 'term', $this->last_register_meta_call['object_type'] );
+ $this->assertEquals( $meta_key, $this->last_register_meta_call['meta_key'] );
+ $this->assertEquals( $args, $this->last_register_meta_call['args'] );
+ }
+
+ public function data_register_term_meta() {
+ return array(
+ array( 'wptests_tax', 'registered_key1', array( 'single' => true ) ),
+ array( 'category', 'registered_key2', array() ),
+ array( '', 'registered_key3', array( 'sanitize_callback' => 'absint' ) ),
+ );
+ }
+
+ public function filter_register_meta_args_set_last_register_meta_call( $args, $defaults, $object_type, $meta_key ) {
+ $this->last_register_meta_call['object_type'] = $object_type;
+ $this->last_register_meta_call['meta_key'] = $meta_key;
+ $this->last_register_meta_call['args'] = $args;
+
+ return $args;
+ }
+
+ /**
+ * @ticket 38323
+ * @dataProvider data_unregister_term_meta
+ */
+ public function test_unregister_term_meta( $taxonomy, $meta_key ) {
+ global $wp_meta_keys;
+
+ register_term_meta( $taxonomy, $meta_key, array() );
+ unregister_term_meta( $taxonomy, $meta_key );
+
+ $actual = $wp_meta_keys;
+
+ // Reset global so subsequent data tests do not get polluted.
+ $wp_meta_keys = array();
+
+ $this->assertEmpty( $actual );
+ }
+
+ public function data_unregister_term_meta() {
+ return array(
+ array( 'wptests_tax', 'registered_key1' ),
+ array( 'category', 'registered_key2' ),
+ array( '', 'registered_key3' ),
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>