PluginProbe
Enable CORS / trunk
Enable CORS vtrunk
2.1.0 2.0.4 trunk 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3
enable-cors / src / SettingsApi.php

SettingsApi.php in Enable CORS trunk, at src/SettingsApi.php

98 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Enable\Cors;
4
5 /*
6 |--------------------------------------------------------------------------
7 | If this file is called directly, abort.
8 |--------------------------------------------------------------------------
9 */
10 if ( ! defined( 'Enable\Cors\SLUG' ) ) {
11 exit;
12 }
13
14 use Enable\Cors\Helpers\Htaccess;
15 use Enable\Cors\Helpers\Option;
16 use Enable\Cors\Traits\Api;
17 use WP_Error;
18 use WP_REST_Request;
19 use WP_REST_Response;
20 use WP_REST_Server;
21
22
23 final class SettingsApi {
24 use Api;
25
26 /**
27 * Settings object.
28 *
29 * @var Option
30 */
31 private $option;
32
33 /**
34 * Initialize settings API
35 */
36 public function __construct() {
37 register_rest_route(
38 $this->namespace,
39 '/settings',
40 array(
41 array(
42 'methods' => WP_REST_Server::READABLE,
43 'callback' => array( $this, 'get' ),
44 'permission_callback' => array( $this, 'permissions_check' ),
45 'args' => array(),
46 ),
47 array(
48 'methods' => WP_REST_Server::CREATABLE,
49 'callback' => array( $this, 'set' ),
50 'permission_callback' => array( $this, 'permissions_check' ),
51 'args' => array(),
52 ),
53 )
54 );
55
56 $this->option = new Option();
57 }
58
59 /**
60 * Update settings data in database
61 *
62 * @param WP_REST_Request $request Request object.
63 *
64 * @return WP_REST_Response
65 */
66 public function set( WP_REST_Request $request ) {
67 $json_params = $request->get_json_params();
68 $params = is_array( $json_params ) ? $json_params : $request->get_body_params();
69 $saved = $this->option->save( $params );
70 if ( is_wp_error( $saved ) ) {
71 $this->response['message'] = $saved->get_error_message();
72 $this->response['success'] = false;
73 } else {
74 $writer = new Htaccess();
75 $writer->modify();
76 wp_cache_flush();
77 $this->response['message'] = __( 'Settings Updated!', 'enable-cors' );
78 $this->response['success'] = true;
79 }
80 $this->response['data'] = $this->option->get();
81
82 return rest_ensure_response( $this->response );
83 }
84
85 /**
86 * Get settings data from database
87 *
88 * @return WP_REST_Response
89 */
90 public function get() {
91 $this->response['data'] = $this->option->get();
92
93 $this->response['success'] = true;
94
95 return rest_ensure_response( $this->response );
96 }
97 }
98