11<?php
22/**
3- *
3+ * This allows non-admin users to read Stackable Options for Global Settings in the Editor
44 */
55
66// Exit if accessed directly.
@@ -22,82 +22,38 @@ public function __construct() {
2222 add_action ( 'rest_api_init ' , array ( $ this , 'register_routes ' ) );
2323 }
2424
25- public function get_item_permissions_check ( $ request ) {
26- return current_user_can ( 'edit_posts ' );
25+ public function register_routes () {
26+ register_rest_route (
27+ $ this ->namespace ,
28+ '/ ' . $ this ->rest_base ,
29+ array (
30+ 'methods ' => WP_REST_Server::READABLE ,
31+ 'callback ' => array ( $ this , 'get_item ' ),
32+ 'args ' => array (),
33+ 'permission_callback ' => array ( $ this , 'retrieve_item_permissions_check ' ),
34+ )
35+ );
2736 }
2837
29- /**
30- * Updates settings for the settings object.
31- */
32- public function update_item ( $ request ) {
33-
34- if ( $ request ->has_param ( '_locale ' ) ) {
35- unset( $ request [ '_locale ' ] );
36- }
37-
38- $ params = $ request ->get_params ();
39-
40- return parent ::update_item ( $ request );
38+ public function retrieve_item_permissions_check ( $ request ) {
39+ return current_user_can ( 'edit_posts ' );
4140 }
4241
4342 /**
44- * Retrieves all of the registered stackable options.
43+ * Retrieves only the Stackable registered options
4544 *
4645 * @return array Array of registered options.
4746 */
4847 protected function get_registered_options () {
49- $ rest_options = array ();
50-
51- foreach ( get_registered_settings () as $ name => $ args ) {
52- if ( empty ( $ args ['show_in_rest ' ] ) ) {
53- continue ;
54- }
55-
56- $ rest_args = array ();
57-
58- if ( is_array ( $ args ['show_in_rest ' ] ) ) {
59- $ rest_args = $ args ['show_in_rest ' ];
60- }
61-
62- $ defaults = array (
63- 'name ' => ! empty ( $ rest_args ['name ' ] ) ? $ rest_args ['name ' ] : $ name ,
64- 'schema ' => array (),
65- );
66-
67- $ rest_args = array_merge ( $ defaults , $ rest_args );
68-
69- // Skip over settings not from Stackable
70- if ( strpos ( $ rest_args [ 'name ' ], 'stackable ' ) !== 0 ) {
71- continue ;
72- }
73-
74- $ default_schema = array (
75- 'type ' => empty ( $ args ['type ' ] ) ? null : $ args ['type ' ],
76- 'title ' => empty ( $ args ['label ' ] ) ? '' : $ args ['label ' ],
77- 'description ' => empty ( $ args ['description ' ] ) ? '' : $ args ['description ' ],
78- 'default ' => isset ( $ args ['default ' ] ) ? $ args ['default ' ] : null ,
79- );
80-
81- $ rest_args ['schema ' ] = array_merge ( $ default_schema , $ rest_args ['schema ' ] );
82- $ rest_args ['option_name ' ] = $ name ;
83-
84- // Skip over settings that don't have a defined type in the schema.
85- if ( empty ( $ rest_args ['schema ' ]['type ' ] ) ) {
86- continue ;
87- }
88-
89- /*
90- * Allow the supported types for settings, as we don't want invalid types
91- * to be updated with arbitrary values that we can't do decent sanitizing for.
92- */
93- if ( ! in_array ( $ rest_args ['schema ' ]['type ' ], array ( 'number ' , 'integer ' , 'string ' , 'boolean ' , 'array ' , 'object ' ), true ) ) {
94- continue ;
95- }
96-
97- $ rest_args ['schema ' ] = rest_default_additional_properties_to_false ( $ rest_args ['schema ' ] );
98-
99- $ rest_options [ $ rest_args ['name ' ] ] = $ rest_args ;
100- }
48+ $ rest_options = parent ::get_registered_options ();
49+
50+ $ rest_options = array_filter (
51+ $ rest_options ,
52+ function ( $ key ) {
53+ return strpos ( $ key , 'stackable ' ) === 0 ;
54+ },
55+ ARRAY_FILTER_USE_KEY
56+ );
10157
10258 return $ rest_options ;
10359 }
0 commit comments