Make WordPress Core

Ticket #35658: 35658-objects.2.diff

File 35658-objects.2.diff, 20.0 KB (added by sc0ttkclark, 8 years ago)

Updated to remove 'schema'

Line 
1diff --git a/src/wp-includes/class-wp-meta-manager.php b/src/wp-includes/class-wp-meta-manager.php
2new file mode 100644
3index 0000000..b5f33d9
4--- /dev/null
5+++ b/src/wp-includes/class-wp-meta-manager.php
6@@ -0,0 +1,284 @@
7+<?php
8+/**
9+ * A registry for meta data.
10+ *
11+ * @package WordPress
12+ * @since   4.6.0
13+ */
14+final class WP_Meta_Manager {
15+
16+       /**
17+        * Registered meta of the post meta type (including pages and CPTs).
18+        *
19+        * @since  4.6.0
20+        * @access private
21+        * @var array
22+        */
23+       private $post = array(
24+               'all' => array()
25+       );
26+
27+       /**
28+        * Registered meta of the taxonomy meta type.
29+        *
30+        * @since  4.6.0
31+        * @access private
32+        * @var array
33+        */
34+       private $taxonomy = array(
35+               'all' => array()
36+       );
37+
38+       /**
39+        * Registered meta of the user meta type.
40+        *
41+        * @since  4.6.0
42+        * @access private
43+        * @var array
44+        */
45+       private $user = array(
46+               'all' => array()
47+       );
48+
49+       /**
50+        * Registered meta of the comment meta type.
51+        *
52+        * @since  4.6.0
53+        * @access private
54+        * @var array
55+        */
56+       private $comment = array(
57+               'all' => array()
58+       );
59+
60+       /**
61+        * @since 4.6.0
62+        */
63+       public function __construct() {
64+
65+               $post_types_without_meta = array( 'revision', 'nav_menu_item' );
66+
67+               foreach ( get_post_types( null, 'names' ) as $post_type ) {
68+                       if ( in_array( $post_type, $post_types_without_meta ) ) {
69+                               continue;
70+                       }
71+
72+                       $this->post[ $post_type ] = array();
73+               }
74+
75+               foreach ( get_taxonomies() as $taxonomy ) {
76+                       $this->taxonomy[ $taxonomy ] = array();
77+               }
78+
79+               foreach ( array( 'comment', 'ping' ) as $comment_type ) {
80+                       $this->comment[ $comment_type ] = array();
81+               }
82+
83+       }
84+
85+       /**
86+        * Register a meta key.
87+        *
88+        * @since 4.6.0
89+        *
90+        * @param array|object $query          {
91+        *                                     Array or object of meta data properties.
92+        *
93+        * @type string        $object_type    The meta type this key belongs to, e.g. `post`.
94+        * @type string        $object_subtype The meta subtype this key belongs to, e.g. `page`.
95+        * @type string        $key            The meta key.
96+        * @type array         $public         Whether the meta key should be publicly queryable.
97+        * @return true|WP_Error
98+        */
99+       public function register( $args ) {
100+
101+               $defaults = array(
102+                       'object_type'           => null,
103+                       'object_subtype'        => null,
104+                       'key'                   => null,
105+                       'public'                => false,
106+                       'show_in_rest'          => false,
107+                       'sanitize_callback'     => null,
108+                       'old_sanitize_callback' => null,
109+                       'auth_callback'         => null,
110+                       'old_auth_callback'     => null,
111+               );
112+
113+               if ( is_array( $args ) ) {
114+                       // Merge passed $args array with defaults
115+                       $args = array_merge( $defaults, $args );
116+               } elseif ( ! is_object( $args ) ) {
117+                       // No $args passed, use defaults
118+                       $args = $defaults;
119+               }
120+
121+               // Force object
122+               if ( ! is_object( $args ) ) {
123+                       $args = (object) $args;
124+               }
125+
126+               // @todo validate object sub types to only those objects that support custom object types?
127+               if ( empty( $args->object_subtype ) ) {
128+                       $args->object_subtype = 'all';
129+               }
130+
131+               // @todo validate object type as post, comment, user, term?
132+
133+               if ( empty( $args->object_type ) ) {
134+                       return new WP_Error( 'missing_object_type', __( 'The object type is required to register meta.' ) );
135+               }
136+
137+               if ( ! property_exists( $this, $args->object_type ) ) {
138+                       return new WP_Error( 'object_type_does_not_exist', __( sprintf( 'The object type "%s" does not exist.', esc_html( $args['object_type'] ) ) ) );
139+               }
140+
141+               // @todo This may not be necessary, what if post types are registered later?
142+               if ( ! empty( $args->object_subtype ) && ! isset( $this->{$args->object_type}[ $args->object_subtype ] ) ) {
143+                       return new WP_Error( 'meta_subtype_does_not_exist', __( 'The meta subtype does not exist in the registry.' ) );
144+               }
145+
146+               if ( empty( $args->key ) ) {
147+                       return new WP_Error( 'meta_key_required', __( 'The meta key is required.' ) );
148+               }
149+
150+               if ( ! isset( $args->public ) ) {
151+                       return new WP_Error( 'public_required', __( 'The public argument is required to register meta.' ) );
152+               }
153+
154+               if ( empty( $args->object_subtype ) ) {
155+                       $args->object_subtype = 'all';
156+               }
157+
158+               // If neither new or legacy `auth_callback` is provided, fallback to `is_protected_meta()`.
159+               if ( empty( $args->auth_callback ) && empty( $args->old_auth_callback ) ) {
160+                       if ( is_protected_meta( $args->key, $args->object_type ) ) {
161+                               $args->auth_callback = '__return_false';
162+                       } else {
163+                               $args->auth_callback = '__return_true';
164+                       }
165+               }
166+
167+               $this->setup_hooks( $args );
168+
169+               $this->{$args->object_type}[ $args->object_subtype ][ $args->key ] = $args;
170+
171+               return true;
172+
173+       }
174+
175+       /**
176+        * Setup hooks for sanitize and authentication checks
177+        *
178+        * @param object $args
179+        */
180+       private function setup_hooks( $args ) {
181+
182+               $object_type    = $args->object_type;
183+               $object_subtype = $args->object_subtype;
184+               $meta_key       = $args->key;
185+
186+               // This legacy filter will fire for all subtypes of an object type.
187+               if ( is_callable( $args->old_sanitize_callback ) ) {
188+                       add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args->old_sanitize_callback, 10, 3 );
189+               }
190+
191+               if ( is_callable( $args->sanitize_callback ) ) {
192+                       add_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args->sanitize_callback, 10, 4 );
193+               }
194+
195+               // @todo the only core application of this filter is for the `post` object type. What about users, comments, terms?
196+               // The auth here is to edit or add meta, not to view.
197+               if ( is_callable( $args->old_auth_callback ) ) {
198+                       add_filter( "auth_{$object_type}_meta_{$meta_key}", $args->old_auth_callback, 10, 6 );
199+               }
200+
201+               // @todo sort above out before going further
202+               if ( is_callable( $args->auth_callback ) ) {
203+                       add_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args->auth_callback, 10, 7 );
204+               }
205+
206+       }
207+
208+       /**
209+        * Retrieve a list of registered meta keys for an object type and subtype.
210+        *
211+        * @since 4.6.0
212+        *
213+        * @param string $object_type    The type of object. Post, comment, user, term.
214+        * @param string $object_subtype A subtype of the object.
215+        *
216+        * @return array List of registered meta keys.
217+        */
218+       public function get_registered_meta_keys( $object_type = 'post', $object_subtype = 'all' ) {
219+
220+               if ( ! property_exists( $this, $object_type ) ) {
221+                       return array();
222+               }
223+
224+               if ( ! isset( $this->{$object_type}[ $object_subtype ] ) ) {
225+                       return array();
226+               }
227+
228+               return $this->{$object_type}[ $object_subtype ];
229+
230+       }
231+
232+       /**
233+        * Check if a meta key is registered.
234+        *
235+        * @since 4.6.0
236+        *
237+        * @param string $object_type
238+        * @param string $meta_key
239+        * @param string $object_subtype
240+        *
241+        * @return bool True if the meta key is registered to the object type and subtype. False if not.
242+        */
243+       public function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = 'all' ) {
244+
245+               if ( ! property_exists( $this, $object_type ) ) {
246+                       return false;
247+               }
248+
249+               if ( ! isset( $this->{$object_type}[ $object_subtype ] ) ) {
250+                       return false;
251+               }
252+
253+               if ( ! isset( $this->{$object_type}[ $object_subtype ][ $meta_key ] ) ) {
254+                       return false;
255+               }
256+
257+               return true;
258+
259+       }
260+
261+       /**
262+        * Unregister a meta key from the list of registered keys.
263+        *
264+        * @since 4.6.0
265+        *
266+        * @param string $object_type    The type of object.
267+        * @param string $meta_key       The meta key.
268+        * @param string $object_subtype Optional. The subtype of the object type.
269+        *
270+        * @return bool|WP_Error True if successful. WP_Error if the meta key is invalid.
271+        */
272+       public function unregister_meta_key( $object_type, $meta_key, $object_subtype = 'all' ) {
273+
274+               if ( ! $this->registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
275+                       return new WP_Error( 'meta_key_is_not_registered', __( 'Meta key is not registered.' ) );
276+               }
277+
278+               unset( $this->{$object_type}[ $object_subtype ][ $meta_key ] );
279+
280+               return true;
281+
282+       }
283+
284+}
285\ No newline at end of file
286diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
287index 2ae86d8..a38222d 100644
288--- a/src/wp-includes/default-filters.php
289+++ b/src/wp-includes/default-filters.php
290@@ -477,4 +477,7 @@
291 add_filter( 'oembed_dataparse',       'wp_filter_oembed_result',        10, 3 );
292 add_filter( 'oembed_response_data',   'get_oembed_response_data_rich',  10, 4 );
293 
294+// Meta
295+add_action( 'plugins_loaded', '_wp_initialize_meta_manager', 0 );
296+
297 unset( $filter, $action );
298diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php
299index bfdd595..89063a7 100644
300--- a/src/wp-includes/meta.php
301+++ b/src/wp-includes/meta.php
302@@ -936,14 +936,16 @@ function is_protected_meta( $meta_key, $meta_type = null ) {
303  * Sanitize meta value.
304  *
305  * @since 3.1.3
306+ * @since 4.6.0 Added the `$object_subtype` parameter.
307+ *
308+ * @param string $meta_key       Meta key.
309+ * @param mixed  $meta_value     Meta value to sanitize.
310+ * @param string $object_type    Type of object the meta is registered to.
311+ * @param string $object_subtype Optional. Subtype of object. Will inherit the object type by default.
312  *
313- * @param string $meta_key   Meta key
314- * @param mixed  $meta_value Meta value to sanitize
315- * @param string $meta_type  Type of meta
316  * @return mixed Sanitized $meta_value
317  */
318-function sanitize_meta( $meta_key, $meta_value, $meta_type ) {
319-
320+function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = 'all' ) {
321        /**
322         * Filter the sanitization of a specific meta key of a specific meta type.
323         *
324@@ -958,30 +960,163 @@ function sanitize_meta( $meta_key, $meta_value, $meta_type ) {
325         * @param string $meta_key   Meta key.
326         * @param string $meta_type  Meta type.
327         */
328-       return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type );
329+       $meta_value = apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );
330+
331+       /**
332+        * Filter the sanitization of a specific meta key of a specific meta object type and subtype.
333+        *
334+        * The dynamic portions of the hook name, `$object_type`, `$object_subtype`, and `$meta_key`
335+        * refer to the metadata object type (comment, post, or user), the subtype of that object type
336+        * and the meta key value respectively.
337+        *
338+        * @since 4.5.0
339+        *
340+        * @param mixed  $meta_value     Meta value to sanitize.
341+        * @param string $meta_key       Meta key.
342+        * @param string $object_type    Meta type.
343+        * @param string $object_subtype Subtype of the object type.
344+        */
345+       return apply_filters( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $meta_value, $meta_key, $object_type, $object_subtype );
346 }
347 
348 /**
349- * Register meta key
350+ * Register a meta key.
351+ *
352+ * @todo should we wrap `register_meta()` with `register_meta_key()` to be more explicit?
353  *
354  * @since 3.3.0
355+ * @since 4.6.0 Modified to support an array of data to attach to registered meta keys. Previous arguments for
356+ *              `$sanitize_callback` and `$auth_callback` have been folded into this array.
357+ *
358+ * @param string $object_type    Type of object this meta is registered to.
359+ * @param string $meta_key       Meta key to register.
360+ * @param array  $args {
361+ *     Data used to describe the meta key when registered.
362  *
363- * @param string       $meta_type         Type of meta
364- * @param string       $meta_key          Meta key
365- * @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key.
366- * @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.
367+ *     @type string   $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
368+ *     @type string   $auth_callback     Optional. A function or method to call when performing edit_post_meta,
369+ *                                       add_post_meta, and delete_post_meta capability checks.
370+ *     @type bool     $public            Whether data associated with this meta key can be considered public.
371+ * }
372+ * @return true|WP_Error
373  */
374-function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) {
375-       if ( is_callable( $sanitize_callback ) )
376-               add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 );
377+function register_meta( $object_type, $meta_key, $args ) {
378 
379-       if ( empty( $auth_callback ) ) {
380-               if ( is_protected_meta( $meta_key, $meta_type ) )
381-                       $auth_callback = '__return_false';
382-               else
383-                       $auth_callback = '__return_true';
384+       global $wp_meta_manager;
385+
386+       $defaults = array(
387+               'object_type' => $object_type,
388+               'key'         => $meta_key,
389+       );
390+
391+       $passed_args = func_get_args();
392+
393+       // Backwards compatibility for sanitize_callback
394+       if ( is_callable( $passed_args[2] ) ) {
395+               // Handle the old sanitize_callback callable argument
396+               $defaults['old_sanitize_callback'] = $passed_args[2];
397+
398+               // No args passed
399+               $args = array();
400        }
401 
402-       if ( is_callable( $auth_callback ) )
403-               add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );
404+       // Backwards compatibility for auth_callback
405+       if ( isset( $passed_args[3] ) && is_callable( $passed_args[3] ) ) {
406+               $defaults['old_auth_callback'] = $passed_args[3];
407+       }
408+
409+       if ( $args && is_array( $args ) ) {
410+               // Merge passed $args array with defaults
411+               $args = array_merge( $defaults, $args );
412+       } else {
413+               // No $args passed, use defaults
414+               $args = $defaults;
415+       }
416+
417+       return $wp_meta_manager->register( $args );
418+
419+}
420+
421+/**
422+ * Check if a meta key is registered.
423+ *
424+ * @since 4.6.0
425+ *
426+ * @param string $object_type
427+ * @param string $meta_key
428+ * @param string $object_subtype
429+ *
430+ * @return bool True if the meta key is registered to the object type and subtype. False if not.
431+ */
432+function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = 'all' ) {
433+       global $wp_meta_manager;
434+       return $wp_meta_manager->registered_meta_key_exists( $object_type, $meta_key, $object_subtype );
435+}
436+
437+/**
438+ * Unregister a meta key from the list of registered keys.
439+ *
440+ * @since 4.6.0
441+ *
442+ * @param string $object_type    The type of object.
443+ * @param string $meta_key       The meta key.
444+ * @param string $object_subtype Optional. The subtype of the object type.
445+ *
446+ * @return bool|WP_Error True if successful. WP_Error if the meta key is invalid.
447+ */
448+function unregister_meta_key( $object_type, $meta_key, $object_subtype = 'all' ) {
449+       global $wp_meta_manager;
450+       return $wp_meta_manager->unregister_meta_key( $object_type, $meta_key, $object_subtype );
451+}
452+
453+/**
454+ * Retrieve registered metadata for a specified object.
455+ *
456+ * @since 4.6.0
457+ *
458+ * @param string $object_type    Type of object to request metadata for. (e.g. comment, post, term, user)
459+ * @param int    $object_id      ID of the object the metadata is for.
460+ * @param string $meta_key       Optional. Registered metadata key. If not specified, retrieve all registered
461+ *                               metadata for the specified object.
462+ * @param bool   $single         Optional. If true, return only the first value associated
463+ *                               with the specified meta key. If false, return an array of values. Default false.
464+ * @param string $object_subtype The subtype of the object's type to request metadata for. (e.g. custom post type)
465+ *
466+ * @return mixed|WP_Error
467+ */
468+function get_registered_metadata( $object_type = 'post', $object_id, $meta_key = '', $single = false, $object_subtype = 'all' ) {
469+       global $wp_meta_manager;
470+       if ( ! empty( $meta_key ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
471+               return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) );
472+       }
473+
474+       if ( 'post' === $object_type && $object_subtype !== get_post_type( $object_id ) ) {
475+               return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered for this subtype.' ) );
476+       }
477+
478+       $data = get_metadata( $object_type, $object_id, $meta_key, $single );
479+
480+       // If a meta key was specified, return the value associated with that key.
481+       if ( ! empty( $meta_key ) ) {
482+               return $data;
483+       }
484+
485+       $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
486+    $registered_data = array();
487+
488+       foreach( $meta_keys as $k => $v ) {
489+               if ( isset( $data[ $k ] ) ) {
490+                       $registered_data[ $k ] = $data[ $k ];
491+               }
492+       }
493+
494+       return $registered_data;
495+}
496+
497+
498+function _wp_initialize_meta_manager() {
499+       if ( empty( $GLOBALS['wp_meta_manager'] ) ) {
500+               $GLOBALS['wp_meta_manager'] = new WP_Meta_Manager();
501+       }
502 }
503diff --git a/src/wp-settings.php b/src/wp-settings.php
504index a4bb370..768362a 100644
505--- a/src/wp-settings.php
506+++ b/src/wp-settings.php
507@@ -133,6 +133,7 @@
508 require( ABSPATH . WPINC . '/user.php' );
509 require( ABSPATH . WPINC . '/class-wp-user-query.php' );
510 require( ABSPATH . WPINC . '/session.php' );
511+require( ABSPATH . WPINC . '/class-wp-meta-manager.php' );
512 require( ABSPATH . WPINC . '/meta.php' );
513 require( ABSPATH . WPINC . '/class-wp-meta-query.php' );
514 require( ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php' );
515diff --git a/tests/phpunit/tests/meta/registerMeta.php b/tests/phpunit/tests/meta/registerMeta.php
516new file mode 100644
517index 0000000..1601041
518--- /dev/null
519+++ b/tests/phpunit/tests/meta/registerMeta.php
520@@ -0,0 +1,79 @@
521+<?php
522+
523+/**
524+ * @group meta
525+ */
526+class Tests_Meta_Register_Meta extends WP_UnitTestCase {
527+       protected static $editor_id;
528+       protected static $post_id;
529+       protected static $comment_id;
530+
531+       public static function wpSetUpBeforeClass( $factory ) {
532+               self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
533+               self::$post_id = $factory->post->create();
534+               self::$comment_id = $factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
535+       }
536+
537+       public static function wpTearDownAfterClass() {
538+               self::delete_user( self::$editor_id );
539+               wp_delete_comment( self::$comment_id, true );
540+               wp_delete_post( self::$post_id, true );
541+       }
542+
543+       function setUp() {
544+               parent::setUp();
545+               wp_set_current_user( self::$editor_id );
546+       }
547+
548+       public function _old_sanitize_meta_cb( $meta_value, $meta_key, $meta_type ) {
549+               return $meta_key . ' sanitized';
550+       }
551+
552+       public function _new_sanitize_meta_cb( $meta_value, $meta_key, $object_type, $object_subtype ) {
553+               return $meta_key . ' sanitized';
554+       }
555+
556+       public function test_register_meta_without_sanitize_callback_registers_meta_key() {
557+               register_meta( 'post', 'flight_number', array() );
558+               $this->assertTrue( registered_meta_key_exists( 'post', 'flight_number' ) );
559+       }
560+
561+       public function test_register_meta_for_a_category() {
562+               register_meta( 'taxonomy', 'category_icon', array( 'object_subtype' => 'category' ) );
563+               $this->assertTrue( registered_meta_key_exists( 'taxonomy', 'category_icon', 'category' ) );
564+       }
565+
566+       public function test_register_meta_for_a_comment() {
567+               register_meta( 'comment', 'comment_rating', array( 'object_subtype' => 'comment' ) );
568+               $this->assertTrue( registered_meta_key_exists( 'comment', 'comment_rating', 'comment' ) );
569+       }
570+
571+       public function test_register_meta_with_sanitize_callback_registers_meta_key() {
572+               register_meta( 'post', 'flight_number', array( 'sanitize_callback' => 'esc_html' ) );
573+               $this->assertTrue( registered_meta_key_exists( 'post', 'flight_number' ) );
574+       }
575+
576+       public function test_register_meta_valid_object_type_with_valid_subtype() {
577+               register_meta( 'post', 'longitude', array( 'object_subtype' => 'post' ) );
578+               $this->assertTrue( registered_meta_key_exists( 'post', 'longitude', 'post' ) );
579+       }
580+
581+       public function test_register_meta_valid_object_type_with_invalid_subtype() {
582+               register_meta( 'post', 'latitude', array( 'object_subtype' => 'invalid-type' ) );
583+               $this->assertFalse( registered_meta_key_exists( 'post', 'latitude', 'invalid-type' ) );
584+       }
585+
586+       public function test_register_meta_with_old_sanitize_callback_parameter() {
587+               register_meta( 'post', 'old_sanitized_key', array( $this, '_old_sanitize_meta_cb' ) );
588+               $meta = sanitize_meta( 'old_sanitized_key', 'unsanitized', 'post' );
589+
590+               $this->assertEquals( 'old_sanitized_key sanitized', $meta );
591+       }
592+
593+       public function test_register_meta_with_new_sanitize_callback_parameter() {
594+               register_meta( 'post', 'new_sanitized_key', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
595+               $meta = sanitize_meta( 'new_sanitized_key', 'unsanitized', 'post' );
596+
597+               $this->assertEquals( 'new_sanitized_key sanitized', $meta );
598+       }
599+}