PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.13
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.13
2.2.13 2.2.12 2.2.11 2.2.10 2.2.9 2.2.8 2.2.7 2.2.6 2.2.5 2.2.4 2.2.3 trunk 1.0.0 1.0.1 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2
table-builder-block / includes / Admin / Api / SettingsData.php

SettingsData.php in TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables 2.2.13, at includes/Admin/Api/SettingsData.php

142 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST endpoints for the plugin's settings
4 *
5 * @package TableKit
6 */
7
8 namespace TableBuilder\Admin\Api;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Registers and serves the tablebuilder/v1/settings REST routes.
14 */
15 class SettingsData {
16 /**
17 * Unused option-name prefix, reserved for future use.
18 *
19 * @var string
20 */
21 public $prefix = '';
22
23 /**
24 * Unused request-param key, reserved for future use.
25 *
26 * @var string
27 */
28 public $param = '';
29
30 /**
31 * Unused cached request reference, reserved for future use.
32 *
33 * @var \WP_REST_Request|null
34 */
35 public $request = null;
36
37 /**
38 * Registers the tablebuilder/v1/settings GET/POST REST routes.
39 */
40 public function __construct() {
41 add_action(
42 'rest_api_init',
43 function () {
44 register_rest_route(
45 'tablebuilder/v1',
46 'settings',
47 array(
48 'methods' => \WP_REST_Server::READABLE,
49 'callback' => array( $this, 'action_get_settings' ),
50 'permission_callback' => array( $this, 'check_permission' ),
51 )
52 );
53 }
54 );
55
56 add_action(
57 'rest_api_init',
58 function () {
59 register_rest_route(
60 'tablebuilder/v1',
61 'settings',
62 array(
63 'methods' => \WP_REST_Server::EDITABLE,
64 'callback' => array( $this, 'action_edit_settings' ),
65 'permission_callback' => array( $this, 'check_permission' ),
66 )
67 );
68 }
69 );
70 }
71
72 /**
73 * Verifies nonce and capability before either callback runs.
74 * Doing this in permission_callback (rather than inside the action)
75 * ensures WordPress returns a proper 401/403 status on failure.
76 *
77 * @param \WP_REST_Request $request The incoming request.
78 * @return true|\WP_Error True if allowed, otherwise a 403 WP_Error.
79 */
80 public function check_permission( $request ) {
81 if ( ! wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' ) ) {
82 return new \WP_Error(
83 'rest_cookie_invalid_nonce',
84 __( 'Nonce mismatch.', 'table-builder-block' ),
85 array( 'status' => 403 )
86 );
87 }
88
89 if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
90 return new \WP_Error(
91 'rest_forbidden',
92 __( 'Access denied.', 'table-builder-block' ),
93 array( 'status' => 403 )
94 );
95 }
96
97 return true;
98 }
99
100 /**
101 * REST callback: fetch the stored "gutenkit_settings_list" option.
102 *
103 * @param \WP_REST_Request $request Unused; no params required.
104 * @return array{status:string,settings:mixed,message:string[]}
105 */
106 public function action_get_settings( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- required by the register_rest_route() "callback" signature; not needed in the body.
107 $result_data = get_option( 'gutenkit_settings_list' );
108
109 return array(
110 'status' => 'success',
111 'settings' => $result_data,
112 'message' => array( __( 'Settings list has been fetched successfully.', 'table-builder-block' ) ),
113 );
114 }
115
116 /**
117 * REST callback: overwrite the "gutenkit_settings_list" option.
118 *
119 * @param \WP_REST_Request $request Request with a "settings" param.
120 * @return array{status:string,settings?:mixed,message:string[]}
121 */
122 public function action_edit_settings( $request ) {
123 $req_data = $request->get_params();
124
125 if ( array_key_exists( 'settings', $req_data ) ) {
126 $data = $req_data['settings'];
127 $array_get = update_option( 'gutenkit_settings_list', $data );
128
129 return array(
130 'status' => 'success',
131 'settings' => $array_get,
132 'message' => array( __( 'Settings list has been updated successfully.', 'table-builder-block' ) ),
133 );
134 } else {
135 return array(
136 'status' => 'fail',
137 'message' => array( __( 'Something went wrong.', 'table-builder-block' ) ),
138 );
139 }
140 }
141 }
142