PluginProbe
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more / 1.1
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more v1.1
2.0.2 trunk 0.2 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 2.0.0 2.0.1
texty / includes / Api / Settings.php

Settings.php in Texty – SMS Notification for WordPress, WooCommerce, Dokan and more 1.1, at includes/Api/Settings.php

161 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Texty\Api;
4
5 use Texty\Settings as TextySettings;
6 use WP_REST_Server;
7
8 class Settings extends Base {
9
10 /**
11 * Initialize
12 *
13 * @return void
14 */
15 public function __construct() {
16 $this->namespace = 'texty/v1';
17 $this->rest_base = 'settings';
18 }
19
20 /**
21 * Registers the routes for the objects of the controller.
22 *
23 * @return void
24 */
25 public function register_routes() {
26 register_rest_route(
27 $this->namespace,
28 '/' . $this->rest_base,
29 [
30 [
31 'methods' => WP_REST_Server::READABLE,
32 'callback' => [ $this, 'get_items' ],
33 'permission_callback' => [ $this, 'admin_permissions_check' ],
34 'args' => [],
35 ],
36 [
37 'methods' => WP_REST_Server::EDITABLE,
38 'callback' => [ $this, 'update_items' ],
39 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
40 'permission_callback' => [ $this, 'admin_permissions_check' ],
41 ],
42 'schema' => [ $this, 'get_item_schema' ],
43 ]
44 );
45 }
46
47 /**
48 * Retrieves a list of items.
49 *
50 * @param WP_Rest_Request $request
51 *
52 * @return WP_Rest_Response|WP_Error
53 */
54 public function get_items( $request ) {
55 if ( isset( $request['context'] ) && $request['context'] === 'edit' ) {
56 $settings = texty()->settings()->all();
57 $gateways = texty()->gateways()->all();
58
59 $response = [
60 'gateway' => $settings['gateway'],
61 'gateways' => array_map( function ( $gateway ) { // phpcs:ignore
62 $obj = new $gateway();
63
64 return [
65 'name' => $obj->name(),
66 'logo' => $obj->logo(),
67 'description' => $obj->description(),
68 ];
69 }, $gateways ), // phpcs:ignore
70 ];
71
72 foreach ( $gateways as $key => $gateway ) {
73 $response[ $key ] = ( new $gateway() )->get_settings();
74 }
75 } else {
76 $response = texty()->settings()->all();
77 }
78
79 return rest_ensure_response( $response );
80 }
81
82 /**
83 * Updates item from the collection.
84 *
85 * @param WP_REST_Request $request
86 *
87 * @return WP_Error|WP_REST_Response
88 */
89 public function update_items( $request ) {
90 $settings = texty()->settings()->all();
91 $registered = texty()->gateways()->all();
92 $gateway = $request->get_param( 'gateway' );
93
94 $value = [
95 'gateway' => $gateway,
96 ];
97
98 // create a new gateway instance and validate from the gateway
99 $object = new $registered[ $gateway ]();
100 $response = $object->validate( $request );
101
102 // check if the credentials are correct
103 if ( is_wp_error( $response ) ) {
104 return $response;
105 }
106
107 // set the credentials with the gateway key
108 $value[ $gateway ] = $response;
109
110 $settings = array_merge( $settings, $value );
111
112 update_option( TextySettings::OPTION_KEY, $settings, false );
113
114 $request->set_param( 'context', 'edit' );
115
116 return $this->get_items( $request );
117 }
118
119 /**
120 * Get registered items
121 *
122 * @return array
123 */
124 public function get_registered_items() {
125 return [
126 'gateway' => [
127 'description' => __( 'The selected gateway', 'texty' ),
128 'type' => 'enum',
129 'enum' => array_keys( texty()->gateways()->all() ),
130 'required' => true,
131 ],
132 ];
133 }
134
135 /**
136 * Retrieves the settings schema, conforming to JSON Schema.
137 *
138 * @return array
139 */
140 public function get_item_schema() {
141 if ( $this->schema ) {
142 return $this->add_additional_fields_schema( $this->schema );
143 }
144
145 $schema = [
146 '$schema' => 'http://json-schema.org/draft-04/schema#',
147 'title' => 'settings',
148 'type' => 'object',
149 'properties' => [],
150 ];
151
152 foreach ( $this->get_registered_items() as $option => $args ) {
153 $schema['properties'][ $option ] = $args;
154 }
155
156 $this->schema = $schema;
157
158 return $this->add_additional_fields_schema( $this->schema );
159 }
160 }
161