api-base.php
5 years ago
api-plugin.php
6 years ago
api-preset.php
5 years ago
api-replace.php
6 years ago
api-search.php
5 years ago
api-settings.php
6 years ago
api-source.php
5 years ago
api.php
5 years ago
api-settings.php
92 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @api {get} /search-regex/v1/setting Get settings |
| 4 | * @apiVersion 1.0.0 |
| 5 | * @apiName GetSettings |
| 6 | * @apiDescription Get all settings for Search Regex. This includes user-configurable settings, as well as necessary WordPress settings. |
| 7 | * @apiGroup Settings |
| 8 | * |
| 9 | * @apiUse SettingItem |
| 10 | * @apiUse 401Error |
| 11 | * @apiUse 404Error |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * @api {post} /search-regex/v1/setting Update settings |
| 16 | * @apiVersion 1.0.0 |
| 17 | * @apiName UpdateSettings |
| 18 | * @apiDescription Update Search Regex settings. Note you can do partial updates, and only the values specified will be changed. |
| 19 | * @apiGroup Settings |
| 20 | * |
| 21 | * @apiParam {Object} settings An object containing all the settings to update |
| 22 | * @apiParamExample {json} settings: |
| 23 | * { |
| 24 | * "expire_redirect": 14, |
| 25 | * "https": false |
| 26 | * } |
| 27 | * |
| 28 | * @apiUse SettingItem |
| 29 | * @apiUse 401Error |
| 30 | * @apiUse 404Error |
| 31 | */ |
| 32 | |
| 33 | /** |
| 34 | * @apiDefine SettingItem Settings |
| 35 | * Search Regex settings |
| 36 | * |
| 37 | * @apiSuccess {Object[]} settings An object containing all settings |
| 38 | * @apiSuccess {String} settings.support |
| 39 | * @apiSuccess {String} settings.rest_api |
| 40 | * |
| 41 | * @apiSuccessExample {json} Success-Response: |
| 42 | * HTTP/1.1 200 OK |
| 43 | * { |
| 44 | * "settings": { |
| 45 | * "support": false, |
| 46 | * } |
| 47 | * } |
| 48 | */ |
| 49 | |
| 50 | /** |
| 51 | * Settings API endpoint |
| 52 | */ |
| 53 | class Search_Regex_Api_Settings extends Search_Regex_Api_Route { |
| 54 | /** |
| 55 | * Create API endpoints with the given namespace |
| 56 | * |
| 57 | * @param String $namespace Namespace. |
| 58 | */ |
| 59 | public function __construct( $namespace ) { |
| 60 | register_rest_route( $namespace, '/setting', array( |
| 61 | $this->get_route( WP_REST_Server::READABLE, 'route_settings', [ $this, 'permission_callback' ] ), |
| 62 | $this->get_route( WP_REST_Server::EDITABLE, 'route_save_settings', [ $this, 'permission_callback' ] ), |
| 63 | ) ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get settings |
| 68 | * |
| 69 | * @param WP_REST_Request $request Request. |
| 70 | * @return Array Settings |
| 71 | */ |
| 72 | public function route_settings( WP_REST_Request $request ) { |
| 73 | return [ |
| 74 | 'settings' => searchregex_get_options(), |
| 75 | ]; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Set settings |
| 80 | * |
| 81 | * @param WP_REST_Request $request Request. |
| 82 | * @return Array Settings |
| 83 | */ |
| 84 | public function route_save_settings( WP_REST_Request $request ) { |
| 85 | $params = $request->get_params(); |
| 86 | |
| 87 | searchregex_set_options( $params ); |
| 88 | |
| 89 | return $this->route_settings( $request ); |
| 90 | } |
| 91 | } |
| 92 |