RestAudioPresetsController.php
3 years ago
RestPresetsController.php
3 years ago
RestSettingsController.php
3 years ago
RestVideosController.php
2 years ago
RestSettingsController.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Services\API; |
| 4 | |
| 5 | class RestSettingsController extends \WP_REST_Settings_Controller |
| 6 | { |
| 7 | |
| 8 | /** |
| 9 | * Constructor. |
| 10 | * |
| 11 | * @since 4.7.0 |
| 12 | */ |
| 13 | public function __construct() |
| 14 | { |
| 15 | $this->namespace = 'presto-player/v1'; |
| 16 | $this->rest_base = 'settings'; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Register controller |
| 21 | * |
| 22 | * @return void |
| 23 | */ |
| 24 | public function register() |
| 25 | { |
| 26 | add_action('rest_api_init', [$this, 'register_routes']); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Checks if a given request has access to read and manage settings. |
| 31 | * |
| 32 | * @since 4.7.0 |
| 33 | * |
| 34 | * @param WP_REST_Request $request Full details about the request. |
| 35 | * @return bool True if the request has read access for the item, otherwise false. |
| 36 | */ |
| 37 | public function get_item_permissions_check($request) |
| 38 | { |
| 39 | return current_user_can('edit_posts'); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Retrieves all of the registered options for the Settings API. |
| 44 | * |
| 45 | * @since 4.7.0 |
| 46 | * |
| 47 | * @return array Array of registered options. |
| 48 | */ |
| 49 | protected function get_registered_options() |
| 50 | { |
| 51 | $rest_options = array(); |
| 52 | |
| 53 | foreach (get_registered_settings() as $name => $args) { |
| 54 | if (!in_array($name, ['presto_player_branding', 'presto_player_youtube', 'presto_player_presets', 'presto_player_audio_presets'])) { |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | if (empty($args['show_in_rest'])) { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | $rest_args = array(); |
| 63 | |
| 64 | if (is_array($args['show_in_rest'])) { |
| 65 | $rest_args = $args['show_in_rest']; |
| 66 | } |
| 67 | |
| 68 | $defaults = array( |
| 69 | 'name' => !empty($rest_args['name']) ? $rest_args['name'] : $name, |
| 70 | 'schema' => array(), |
| 71 | ); |
| 72 | |
| 73 | $rest_args = array_merge($defaults, $rest_args); |
| 74 | |
| 75 | $default_schema = array( |
| 76 | 'type' => empty($args['type']) ? null : $args['type'], |
| 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 | } |
| 101 | |
| 102 | return $rest_options; |
| 103 | } |
| 104 | } |
| 105 |