| 1 |
<?php |
| 2 |
|
| 3 |
namespace TableBuilder\Admin\Api; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
class SettingsData { |
| 8 |
public $prefix = ''; |
| 9 |
public $param = ''; |
| 10 |
public $request = null; |
| 11 |
|
| 12 |
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' => '__return_true', |
| 19 |
) |
| 20 |
); |
| 21 |
}); |
| 22 |
|
| 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' => '__return_true', |
| 29 |
) |
| 30 |
); |
| 31 |
}); |
| 32 |
} |
| 33 |
|
| 34 |
public function action_get_settings($request) { |
| 35 |
/** |
| 36 |
* Enable this section when fully functional from frontend and need Nonce & Permission check |
| 37 |
*/ |
| 38 |
if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) { |
| 39 |
return array( |
| 40 |
'status' => 'fail', |
| 41 |
'message' => array(__('Nonce mismatch.', 'table-builder-block')), |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
if (!is_user_logged_in() || !current_user_can('manage_options')) { |
| 46 |
return array( |
| 47 |
'status' => 'fail', |
| 48 |
'message' => array(__('Access denied.', 'table-builder-block')), |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
$result_data = get_option('gutenkit_settings_list'); |
| 53 |
|
| 54 |
return array( |
| 55 |
'status' => 'success', |
| 56 |
'settings' => $result_data, |
| 57 |
'message' => array(__('Settings list has been fetched successfully.', 'table-builder-block')), |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
public function action_edit_settings($request) { |
| 62 |
/** |
| 63 |
* Enable this section when fully functional from frontend and need Nonce & Permission check |
| 64 |
*/ |
| 65 |
if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) { |
| 66 |
return array( |
| 67 |
'status' => 'fail', |
| 68 |
'message' => array(__('Nonce mismatch.', 'table-builder-block')), |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
if (!is_user_logged_in() || !current_user_can('manage_options')) { |
| 73 |
return array( |
| 74 |
'status' => 'fail', |
| 75 |
'message' => array(__('Access denied.', 'table-builder-block')), |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
$req_data = $request->get_params(); |
| 80 |
|
| 81 |
if (array_key_exists('settings', $req_data)) { |
| 82 |
$data = $req_data['settings']; |
| 83 |
$array_get = update_option('gutenkit_settings_list', $data); |
| 84 |
|
| 85 |
return array( |
| 86 |
'status' => 'success', |
| 87 |
'settings' => $array_get, |
| 88 |
'message' => array(__('Settings list has been updated successfully.', 'table-builder-block')), |
| 89 |
); |
| 90 |
} else { |
| 91 |
return array( |
| 92 |
'status' => 'fail', |
| 93 |
'message' => array(__('Something went wrong.', 'table-builder-block')), |
| 94 |
); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|