| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\WpRest\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
class Elementor_Settings { |
| 10 |
|
| 11 |
public function register(): void { |
| 12 |
register_rest_route('elementor/v1', '/settings/(?P<key>[\w_-]+)', [ |
| 13 |
[ |
| 14 |
'methods' => \WP_REST_Server::READABLE, |
| 15 |
'permission_callback' => function (): bool { |
| 16 |
return current_user_can( 'manage_options' ); |
| 17 |
}, |
| 18 |
'sanitize_callback' => function ( string $param ): string { |
| 19 |
return esc_attr( $param ); |
| 20 |
}, |
| 21 |
'validate_callback' => function ( \WP_REST_Request $request ): bool { |
| 22 |
$params = $request->get_params(); |
| 23 |
|
| 24 |
return 0 === strpos( $params['key'], 'elementor' ); |
| 25 |
}, |
| 26 |
'callback' => function ( $request ): \WP_REST_Response { |
| 27 |
try { |
| 28 |
$key = $request->get_param( 'key' ); |
| 29 |
$current_value = get_option( $key ); |
| 30 |
|
| 31 |
return new \WP_REST_Response([ |
| 32 |
'success' => true, |
| 33 |
// Nest in order to allow extending the response with more details. |
| 34 |
'data' => [ |
| 35 |
'value' => $current_value, |
| 36 |
], |
| 37 |
], 200); |
| 38 |
} catch ( \Exception $e ) { |
| 39 |
return new \WP_REST_Response([ |
| 40 |
'success' => false, |
| 41 |
'data' => [ |
| 42 |
'message' => $e->getMessage(), |
| 43 |
], |
| 44 |
], 500); |
| 45 |
} |
| 46 |
}, |
| 47 |
], |
| 48 |
]); |
| 49 |
|
| 50 |
register_rest_route('elementor/v1', '/settings/(?P<key>[\w_-]+)', [ |
| 51 |
[ |
| 52 |
'methods' => \WP_REST_Server::EDITABLE, |
| 53 |
'permission_callback' => function (): bool { |
| 54 |
return current_user_can( 'manage_options' ); |
| 55 |
}, |
| 56 |
'sanitize_callback' => function ( string $param ): string { |
| 57 |
return esc_attr( $param ); |
| 58 |
}, |
| 59 |
'validate_callback' => function ( \WP_REST_Request $request ): bool { |
| 60 |
$params = $request->get_params(); |
| 61 |
return 0 === strpos( $params['key'], 'elementor' ) && isset( $params['value'] ); |
| 62 |
}, |
| 63 |
'callback' => function ( \WP_REST_Request $request ): \WP_REST_Response { |
| 64 |
$key = $request->get_param( 'key' ); |
| 65 |
$new_value = $request->get_param( 'value' ); |
| 66 |
$current_value = get_option( $key ); |
| 67 |
|
| 68 |
if ( $new_value === $current_value ) { |
| 69 |
return new \WP_REST_Response([ |
| 70 |
'success' => true, |
| 71 |
], 200); |
| 72 |
} |
| 73 |
|
| 74 |
$success = update_option( $key, $new_value ); |
| 75 |
if ( $success ) { |
| 76 |
return new \WP_REST_Response([ |
| 77 |
'success' => true, |
| 78 |
'data' => [ |
| 79 |
'message' => 'Setting updated successfully.', |
| 80 |
], |
| 81 |
], 200); |
| 82 |
} else { |
| 83 |
return new \WP_REST_Response([ |
| 84 |
'success' => false, |
| 85 |
'data' => [ |
| 86 |
'message' => 'Failed to update setting.', |
| 87 |
], |
| 88 |
], 500); |
| 89 |
} |
| 90 |
}, |
| 91 |
], |
| 92 |
]); |
| 93 |
} |
| 94 |
} |
| 95 |
|