| @@ -1,56 +1,97 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * REST endpoints for the plugin's settings | |
| 4 | + * | |
| 5 | + * @package TableKit | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | namespace TableBuilder\Admin\Api; |
| 4 | 9 | |
| 5 | -defined('ABSPATH') || exit; | |
| 10 | +defined( 'ABSPATH' ) || exit; | |
| 6 | 11 | |
| 12 | +/** | |
| 13 | + * Registers and serves the tablebuilder/v1/settings REST routes. | |
| 14 | + */ | |
| 7 | 15 | class SettingsData { |
| 8 | - public $prefix = ''; | |
| 9 | - public $param = ''; | |
| 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 | + */ | |
| 10 | 35 | public $request = null; |
| 11 | 36 | |
| 37 | + /** | |
| 38 | + * Registers the tablebuilder/v1/settings GET/POST REST routes. | |
| 39 | + */ | |
| 12 | 40 | public function __construct() { |
| 13 | - add_action('rest_api_init', function() { | |
| 14 | - register_rest_route('tablebuilder/v1', 'settings', | |
| 15 | - array( | |
| 16 | - 'methods' => \WP_REST_Server::READABLE, | |
| 17 | - 'callback' => array( $this, 'action_get_settings' ), | |
| 18 | - 'permission_callback' => array( $this, 'check_permission' ), | |
| 19 | - ) | |
| 20 | - ); | |
| 21 | - }); | |
| 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 | + ); | |
| 22 | 55 | |
| 23 | - add_action('rest_api_init', function() { | |
| 24 | - register_rest_route('tablebuilder/v1', 'settings', | |
| 25 | - array( | |
| 26 | - 'methods' => \WP_REST_Server::EDITABLE, | |
| 27 | - 'callback' => array( $this, 'action_edit_settings' ), | |
| 28 | - 'permission_callback' => array( $this, 'check_permission' ), | |
| 29 | - ) | |
| 30 | - ); | |
| 31 | - }); | |
| 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 | + ); | |
| 32 | 70 | } |
| 33 | 71 | |
| 34 | 72 | /** |
| 35 | - * Verify nonce and capability before either callback runs. | |
| 73 | + * Verifies nonce and capability before either callback runs. | |
| 36 | 74 | * Doing this in permission_callback (rather than inside the action) |
| 37 | 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. | |
| 38 | 79 | */ |
| 39 | - public function check_permission($request) { | |
| 40 | - if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) { | |
| 80 | + public function check_permission( $request ) { | |
| 81 | + if ( ! wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' ) ) { | |
| 41 | 82 | return new \WP_Error( |
| 42 | 83 | 'rest_cookie_invalid_nonce', |
| 43 | - __('Nonce mismatch.', 'table-builder-block'), | |
| 44 | - array('status' => 403) | |
| 84 | + __( 'Nonce mismatch.', 'table-builder-block' ), | |
| 85 | + array( 'status' => 403 ) | |
| 45 | 86 | ); |
| 46 | 87 | } |
| 47 | 88 | |
| 48 | - if (!is_user_logged_in() || !current_user_can('manage_options')) { | |
| 89 | + if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) { | |
| 49 | 90 | return new \WP_Error( |
| 50 | 91 | 'rest_forbidden', |
| 51 | - __('Access denied.', 'table-builder-block'), | |
| 52 | - array('status' => 403) | |
| 92 | + __( 'Access denied.', 'table-builder-block' ), | |
| 93 | + array( 'status' => 403 ) | |
| 53 | 94 | ); |
| 54 | 95 | } |
| 55 | 96 | |
| 56 | 97 | return true; |
| @@ -55,34 +96,46 @@ | ||
| 55 | 96 | |
| 56 | 97 | return true; |
| 57 | 98 | } |
| 58 | 99 | |
| 59 | - public function action_get_settings($request) { | |
| 60 | - $result_data = get_option('gutenkit_settings_list'); | |
| 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' ); | |
| 61 | 108 | |
| 62 | 109 | return array( |
| 63 | 110 | 'status' => 'success', |
| 64 | 111 | 'settings' => $result_data, |
| 65 | - 'message' => array(__('Settings list has been fetched successfully.', 'table-builder-block')), | |
| 112 | + 'message' => array( __( 'Settings list has been fetched successfully.', 'table-builder-block' ) ), | |
| 66 | 113 | ); |
| 67 | 114 | } |
| 68 | 115 | |
| 69 | - public function action_edit_settings($request) { | |
| 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 ) { | |
| 70 | 123 | $req_data = $request->get_params(); |
| 71 | 124 | |
| 72 | - if (array_key_exists('settings', $req_data)) { | |
| 125 | + if ( array_key_exists( 'settings', $req_data ) ) { | |
| 73 | 126 | $data = $req_data['settings']; |
| 74 | - $array_get = update_option('gutenkit_settings_list', $data); | |
| 127 | + $array_get = update_option( 'gutenkit_settings_list', $data ); | |
| 75 | 128 | |
| 76 | 129 | return array( |
| 77 | 130 | 'status' => 'success', |
| 78 | 131 | 'settings' => $array_get, |
| 79 | - 'message' => array(__('Settings list has been updated successfully.', 'table-builder-block')), | |
| 132 | + 'message' => array( __( 'Settings list has been updated successfully.', 'table-builder-block' ) ), | |
| 80 | 133 | ); |
| 81 | 134 | } else { |
| 82 | 135 | return array( |
| 83 | 136 | 'status' => 'fail', |
| 84 | - 'message' => array(__('Something went wrong.', 'table-builder-block')), | |
| 137 | + 'message' => array( __( 'Something went wrong.', 'table-builder-block' ) ), | |
| 85 | 138 | ); |
| 86 | 139 | } |
| 87 | 140 | } |
| 88 | 141 | } |