| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Controllers; |
| 4 |
|
| 5 |
use YayMail\Abstracts\BaseController; |
| 6 |
use YayMail\Models\SettingModel; |
| 7 |
use YayMail\Utils\SingletonTrait; |
| 8 |
|
| 9 |
/** |
| 10 |
* Settings Controller |
| 11 |
* * @method static SettingController get_instance() |
| 12 |
*/ |
| 13 |
class SettingController extends BaseController { |
| 14 |
use SingletonTrait; |
| 15 |
|
| 16 |
private $model = null; |
| 17 |
|
| 18 |
protected function __construct() { |
| 19 |
$this->model = SettingModel::get_instance(); |
| 20 |
$this->init_hooks(); |
| 21 |
} |
| 22 |
|
| 23 |
protected function init_hooks() { |
| 24 |
register_rest_route( |
| 25 |
YAYMAIL_REST_NAMESPACE, |
| 26 |
'/settings', |
| 27 |
[ |
| 28 |
[ |
| 29 |
'methods' => \WP_REST_Server::READABLE, |
| 30 |
'callback' => [ $this, 'exec_get_settings' ], |
| 31 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 32 |
], |
| 33 |
[ |
| 34 |
'methods' => \WP_REST_Server::EDITABLE, |
| 35 |
'callback' => [ $this, 'exec_update_settings' ], |
| 36 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 37 |
], |
| 38 |
] |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
public function exec_get_settings( \WP_REST_Request $request ) { |
| 43 |
return $this->exec( [ $this, 'get_settings' ], $request ); |
| 44 |
} |
| 45 |
|
| 46 |
public function get_settings() { |
| 47 |
$settings = $this->model::find_all(); |
| 48 |
return $settings; |
| 49 |
} |
| 50 |
|
| 51 |
public function exec_update_settings( \WP_REST_Request $request ) { |
| 52 |
return $this->exec( [ $this, 'update_settings' ], $request ); |
| 53 |
} |
| 54 |
|
| 55 |
public function update_settings( \WP_REST_Request $request ) { |
| 56 |
$data = json_decode( $request->get_param( 'data' ), true ); |
| 57 |
$settings = is_array( $data ) ? array_map( 'sanitize_text_field', wp_unslash( $data ) ) : []; |
| 58 |
$settings['custom_css'] = wp_strip_all_tags( isset( $data['custom_css'] ) ? $data['custom_css'] : '' ); |
| 59 |
$this->model::update( $settings ); |
| 60 |
return [ |
| 61 |
'success' => true, |
| 62 |
'data' => $settings, |
| 63 |
]; |
| 64 |
} |
| 65 |
} |
| 66 |
|