PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.7
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.7
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / elementor / wp-one-package / src / Admin / Controllers / Settings.php

Settings.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.7, at vendor/elementor/wp-one-package/src/Admin/Controllers/Settings.php

93 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ElementorOne\Admin\Controllers;
4
5 use ElementorOne\Admin\Config;
6 use ElementorOne\Admin\Helpers\Utils;
7 use ElementorOne\Admin\Components\Fields;
8 use ElementorOne\Common\RestError;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly
12 }
13
14 /**
15 * Class Settings
16 * Extends WordPress's built-in REST Settings Controller
17 * Handles all settings-related REST API endpoints
18 */
19 class Settings extends \WP_REST_Settings_Controller {
20
21 /**
22 * Constructor
23 */
24 public function __construct() {
25 $this->namespace = Config::APP_REST_NAMESPACE;
26 $this->rest_base = 'settings';
27
28 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
29 }
30
31 /**
32 * Register all settings-related routes
33 * @return void
34 */
35 public function register_routes() {
36 // Register base route that handles both GET and POST
37 register_rest_route(
38 $this->namespace,
39 '/' . $this->rest_base,
40 [
41 [
42 'methods' => \WP_REST_Server::READABLE,
43 'callback' => [ $this, 'get_item' ],
44 'permission_callback' => [ $this, 'get_item_permissions_check' ],
45 ],
46 [
47 'methods' => \WP_REST_Server::EDITABLE,
48 'callback' => [ $this, 'update_item' ],
49 'permission_callback' => [ $this, 'get_item_permissions_check' ],
50 ],
51 ]
52 );
53 }
54
55 /**
56 * Get settings - Override parent to return custom format
57 * @param \WP_REST_Request $request
58 * @return \WP_REST_Response|\WP_Error
59 */
60 public function get_item( $request ) {
61 try {
62 $data = Fields::instance()->get_plugin_settings();
63
64 return new \WP_REST_Response( $data );
65 } catch ( \Throwable $th ) {
66 return RestError::internal_server_error( $th->getMessage() );
67 }
68 }
69
70 /**
71 * Update settings - Override parent to handle custom logic
72 * @param \WP_REST_Request $request
73 * @return \WP_REST_Response|\WP_Error
74 */
75 public function update_item( $request ) {
76 try {
77 foreach ( $request->get_params() as $key => $value ) {
78 $setting_name = Utils::camel_to_snake( $key );
79 if ( ! isset( Fields::get_settings()[ $setting_name ] ) ) {
80 continue;
81 }
82 update_option( Fields::SETTING_PREFIX . $setting_name, $value );
83 }
84
85 $data = Fields::instance()->get_plugin_settings();
86
87 return new \WP_REST_Response( $data );
88 } catch ( \Throwable $th ) {
89 return RestError::internal_server_error( $th->getMessage() );
90 }
91 }
92 }
93