<!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>[37924] trunk/src/wp-includes: Introduce an expanded meta registration API.</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 { 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/37924">37924</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/37924","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>helen</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-06-30 01:01:35 +0000 (Thu, 30 Jun 2016)</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'>Introduce an expanded meta registration API.

`register_meta()` has been altered to accept an array of arguments as the third parameter in order to support its usage beyond XML-RPC, notably in the REST API and other projects that may build on top of meta, such as a potential Fields API. Arguments are whitelisted to reserve the right for core to add more later.

New functions added to complement this expansion are:
* `registered_meta_key_exists()`
* `unregister_meta_key()`
* `get_registered_meta_keys()`
* `get_registered_metadata()`
* A "private" function for the aforementioned whitelisting.

There still need to be lots of tests written for previous and new behaviors, and many things are subject to change. Maybe things will explode. #yolo

props jeremyfelt, ericlewis, sc0ttkclark, helen, rmccue, ocean90, voldemortensen.
see <a href="https://core.trac.wordpress.org/ticket/35658">#35658</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescapabilitiesphp">trunk/src/wp-includes/capabilities.php</a></li>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludesmetaphp">trunk/src/wp-includes/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    2016-06-29 22:07:17 UTC (rev 37923)
+++ trunk/src/wp-includes/capabilities.php      2016-06-30 01:01:35 UTC (rev 37924)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -243,11 +243,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        break;
</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">+                $post_type = get_post_type( $post );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
</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">-                if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $meta_key && ( has_filter( "auth_post_meta_{$meta_key}" ) || has_filter( "auth_post_{$post_type}_meta_{$meta_key}" ) ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         /**
</span><span class="cx" style="display: block; padding: 0 10px">                         * Filters whether the user is allowed to add post meta to a post.
</span><span class="cx" style="display: block; padding: 0 10px">                         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -264,6 +266,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         * @param array  $caps     User capabilities.
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><span class="cx" style="display: block; padding: 0 10px">                        $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       /**
+                        * Filters whether the user is allowed to add post meta to a post of a given type.
+                        *
+                        * The dynamic portions of the hook name, `$meta_key` and `$post_type`,
+                        * refer to the meta key passed to map_meta_cap() and the post type, respectively.
+                        *
+                        * @since 4.6.0
+                        *
+                        * @param bool   $allowed  Whether the user can add the post meta. Default false.
+                        * @param string $meta_key The meta key.
+                        * @param int    $post_id  Post ID.
+                        * @param int    $user_id  User ID.
+                        * @param string $cap      Capability name.
+                        * @param array  $caps     User capabilities.
+                        */
+                       $allowed = apply_filters( "auth_post_{$post_type}_meta_{$meta_key}", $allowed, $meta_key, $post->ID, $user_id, $cap, $caps );
+
</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">                } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
</span></span></pre></div>
<a id="trunksrcwpincludesdefaultfiltersphp"></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/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2016-06-29 22:07:17 UTC (rev 37923)
+++ trunk/src/wp-includes/default-filters.php   2016-06-30 01:01:35 UTC (rev 37924)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -85,6 +85,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'pre_post_mime_type', 'sanitize_mime_type' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'post_mime_type', 'sanitize_mime_type' );
</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
+add_filter( 'register_meta_args', '_wp_register_meta_args_whitelist', 10, 2 );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Places to balance tags on input
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( array( 'content_save_pre', 'excerpt_save_pre', 'comment_save_pre', 'pre_comment_content' ) as $filter ) {
</span><span class="cx" style="display: block; padding: 0 10px">        add_filter( $filter, 'convert_invalid_entities' );
</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    2016-06-29 22:07:17 UTC (rev 37923)
+++ trunk/src/wp-includes/meta.php      2016-06-30 01:01:35 UTC (rev 37924)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -936,52 +936,341 @@
</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 4.6.0 Added the `$object_subtype` parameter.
</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 $meta_key   Meta key
- * @param mixed  $meta_value Meta value to sanitize
- * @param string $meta_type  Type of meta
- * @return mixed Sanitized $meta_value
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $meta_key       Meta key.
+ * @param mixed  $meta_value     Meta value to sanitize.
+ * @param string $object_type    Type of object the meta is registered to.
+ * @param string $object_subtype Optional. Subtype of object. Will inherit the object type by default.
+ *
+ * @return mixed Sanitized $meta_value.
</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 sanitize_meta( $meta_key, $meta_value, $meta_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 ) ) {
+               /**
+                * Filters the sanitization of a specific meta key of a specific meta type and subtype.
+                *
+                * The dynamic portions of the hook name, `$meta_type`, `$meta_subtype`,
+                * and `$meta_key`, refer to the metadata object type (comment, post, or user)
+                * the object subtype, and the meta key value, respectively.
+                *
+                * @since 4.6.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.
+                */
+               $meta_value = apply_filters( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $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">        /**
</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="cx" style="display: block; padding: 0 10px">         * The dynamic portions of the hook name, `$meta_type`, and `$meta_key`,
</span><span class="cx" style="display: block; padding: 0 10px">         * refer to the metadata object type (comment, post, or user) and the meta
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * key value,
-        * respectively.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * key value, respectively.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.3.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 4.6.0 Added the `$object_subtype` parameter.
</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 mixed  $meta_value Meta value to sanitize.
-        * @param string $meta_key   Meta key.
-        * @param string $meta_type  Meta type.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @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.
</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 apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $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"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Register meta key
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Registers a meta key.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 3.3.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.6.0 Modified to support an array of data to attach to registered meta keys. Previous arguments for
+ *              `$sanitize_callback` and `$auth_callback` have been folded into this 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">- * @param string       $meta_type         Type of meta
- * @param string       $meta_key          Meta key
- * @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key.
- * @param string|array $auth_callback     Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $object_type    Type of object this meta is registered to.
+ * @param string $meta_key       Meta key to register.
+ * @param array  $args {
+ *     Data used to describe the meta key when registered.
+ *
+ *     @type string $object_subtype    A subtype; e.g. if the object type is "post", the post type.
+ *     @type string $type              The type of data associated with this meta key.
+ *     @type string $description       A description of the data attached to this meta key.
+ *     @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
+ *     @type string $auth_callback     Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.
+ *     @type bool   $show_in_rest      Whether data associated with this meta key can be considered public.
+ * }
+ * @param string|array $auth_callback Deprecated. Use `$args` instead.
+ *
+ * @return bool True if the meta key was successfully registered in the global array, false if not.
+ *              Registering a meta key with distinct sanitize and auth callbacks will fire those
+ *              callbacks, but will not add to the global registry as it requires a 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">-function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) {
-       if ( is_callable( $sanitize_callback ) )
-               add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function register_meta( $object_type, $meta_key, $args, $auth_callback = null ) {
+       global $wp_meta_keys;
</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 ( empty( $auth_callback ) ) {
-               if ( is_protected_meta( $meta_key, $meta_type ) )
-                       $auth_callback = '__return_false';
-               else
-                       $auth_callback = '__return_true';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! is_array( $wp_meta_keys ) ) {
+               $wp_meta_keys = array();
</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">-        if ( is_callable( $auth_callback ) )
-               add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /* translators: object type name */
+       if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) {
+               _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid object type: %s.' ), $object_type ), '4.6.0' );
+       }
+
+       $defaults = array(
+               'object_subtype'    => '',
+               'type'              => 'string',
+               'description'       => '',
+               'sanitize_callback' => null,
+               'auth_callback'     => null,
+               'show_in_rest'      => false,
+       );
+
+       $passed_args = array_slice( func_get_args(), 2 );
+
+       // There used to be individual args for sanitize and auth callbacks     
+       $has_old_sanitize_cb = $has_old_auth_cb = false;
+
+       if ( is_callable( $passed_args[0] ) ) {
+               $args['sanitize_callback'] = $passed_args[0];
+               $has_old_sanitize_cb = true;
+       } else {
+               $args = $passed_args[0];
+       }
+
+       if ( isset( $passed_args[1] ) && is_callable( $passed_args[1] ) ) {
+               $args['auth_callback'] = $passed_args[1];
+               $has_old_auth_cb = true;
+       }
+
+       $args = wp_parse_args( $args, $defaults );
+
+       /**
+        * Filters the registration arguments when registering meta.
+        *
+        * @since 4.6.0
+        *
+        * @param array  $args        Array of meta registration arguments.
+        * @param array  $defaults    Array of default arguments.
+        * @param string $object_type Object type.
+        * @param string $meta_key    Meta key.
+        */
+       $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
+
+       // Object subtype is required if using the args style of registration
+       if ( ! $has_old_sanitize_cb && empty( $args['object_subtype'] ) ) {
+               return false;
+       }
+
+       // Back-compat: old sanitize and auth callbacks applied to all of an object type
+       if ( $has_old_sanitize_cb ) {
+               add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 );
+       } elseif ( is_callable( $args['sanitize_callback'] ) && ! empty( $object_subtype ) ) {
+               add_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 );
+       }
+
+       // If `auth_callback` is not provided, fall back to `is_protected_meta()`.
+       if ( empty( $args['auth_callback'] ) ) {
+               if ( is_protected_meta( $meta_key, $object_type ) ) {
+                       $args['auth_callback'] = '__return_false';
+               } else {
+                       $args['auth_callback'] = '__return_true';
+               }
+       }
+
+       if ( $has_old_auth_cb ) {
+               add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
+       } elseif ( is_callable( $args['auth_callback'] ) && ! empty( $object_subtype ) ) {
+               add_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
+       }
+
+       $object_subtype = $args['object_subtype'];
+
+       // Global registry only contains meta keys registered in the new way with a subtype.
+       if ( ! empty( $object_subtype ) ) {
+               $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
+
+               return true;
+       }
+
+       return false;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Checks if a meta key is registered.
+ *
+ * @since 4.6.0
+ *
+ * @param string $object_type    The type of object.
+ * @param string $object_subtype The subtype of the object type.
+ * @param string $meta_key       The meta key.
+ *
+ * @return bool True if the meta key is registered to the object type and subtype. False if not.
+ */
+function registered_meta_key_exists( $object_type, $object_subtype, $meta_key ) {
+       global $wp_meta_keys;
+
+       if ( ! is_array( $wp_meta_keys ) ) {
+               return false;
+       }
+
+       // Only specific core object types are supported.
+       if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) {
+               return false;
+       }
+
+       if ( ! isset( $wp_meta_keys[ $object_type ] ) ) {
+               return false;
+       }
+
+       if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
+               return false;
+       }
+
+       if ( isset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ) ) {
+               return true;
+       }
+
+       return false;
+}
+
+/**
+ * Unregisters a meta key from the list of registered keys.
+ *
+ * @since 4.6.0
+ *
+ * @param string $object_type    The type of object.
+ * @param string $object_subtype The subtype of the object type.
+ * @param string $meta_key       The meta key.
+ *
+ * @return bool|WP_Error True if successful. WP_Error if the meta key is invalid.
+ */
+function unregister_meta_key( $object_type, $object_subtype, $meta_key ) {
+       global $wp_meta_keys;
+
+       if ( ! registered_meta_key_exists( $object_type, $object_subtype, $meta_key ) ) {
+               return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key' ) );
+       }
+
+       unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
+
+       // Do some clean up
+       if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
+               unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );
+       }
+
+       if ( empty( $wp_meta_keys[ $object_type ] ) ) {
+               unset( $wp_meta_keys[ $object_type ] );
+       }
+
+       return true;
+}
+
+/**
+ * Retrieves a list of registered meta keys for an object type and optionally subtype.
+ *
+ * @since 4.6.0
+ *
+ * @param string $object_type    The type of object. Post, comment, user, term.
+ * @param string $object_subtype Optional. A subtype of the object (e.g. custom post type).
+ *
+ * @return array List of registered meta keys.
+ */
+function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
+       global $wp_meta_keys;
+
+       if ( ! isset( $wp_meta_keys[ $object_type ] ) ) {
+               return array();
+       }
+
+       if ( empty( $object_subtype ) && isset( $wp_meta_keys[ $object_type ] ) ) {
+               return $wp_meta_keys[ $object_type ];
+       }
+
+       if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
+               return array();
+       }
+
+       return $wp_meta_keys[ $object_type ][ $object_subtype ];
+}
+
+/**
+ * Retrieves registered metadata for a specified object.
+ *
+ * @since 4.6.0
+ *
+ * @param string $object_type    Type of object to request metadata for. (e.g. comment, post, term, user)
+ * @param string $object_subtype The subtype of the object's type to request metadata for. (e.g. custom post type)
+ * @param int    $object_id      ID of the object the metadata is for.
+ * @param string $meta_key       Optional. Registered metadata key. If not specified, retrieve all registered
+ *                               metadata for the specified object.
+ *
+ * @return mixed|WP_Error
+ */
+function get_registered_metadata( $object_type, $object_subtype, $object_id, $meta_key = '' ) {
+       global $wp_meta_keys;
+
+       if ( ! is_array( $wp_meta_keys ) ) {
+               return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) );
+       }
+
+       if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) {
+               return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) );
+       }
+
+       if ( ! empty( $meta_key ) ) {
+               if ( ! registered_meta_key_exists( $object_type, $object_subtype, $meta_key ) ) {
+                       return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) );
+               }
+               $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
+               $meta_key_data = $meta_keys[ $object_type ][ $object_subtype ][ $meta_key ];
+
+               $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data->single );
+
+               return $data;
+       }
+
+       $data = get_metadata( $object_type, $object_id, $meta_key );
+
+       $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
+       $registered_data = array();
+
+       // Someday, array_filter()
+       foreach ( $meta_keys as $k => $v ) {
+               if ( isset( $data[ $k ] ) ) {
+                       $registered_data[ $k ] = $data[ $k ];
+               }
+       }
+
+       return $registered_data;
+}
+
+/**
+ * Filter out `register_meta()` args based on a whitelist.
+ * `register_meta()` args may change over time, so requiring the whitelist
+ * to be explicitly turned off is a warranty seal of sorts.
+ *
+ * @access private
+ * @since  4.6.0
+ *
+ * @param  array $args         Arguments from `register_meta()`.
+ * @param  array $default_args Default arguments for `register_meta()`.
+ *
+ * @return array Filtered arguments.
+ */
+function _wp_register_meta_args_whitelist( $args, $default_args ) {
+       $whitelist = array_keys( $default_args );
+
+       // 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 ] );
+               }
+       }
+
+       return $args;
+}
</ins></span></pre>
</div>
</div>

</body>
</html>