class-adbc-automation-endpoints.php
3 months ago
class-adbc-common-endpoints.php
4 months ago
class-adbc-cron-jobs-endpoints.php
3 months ago
class-adbc-general-cleanup-endpoints.php
3 months ago
class-adbc-info-endpoints.php
3 months ago
class-adbc-logs-endpoints.php
3 months ago
class-adbc-options-endpoints.php
3 months ago
class-adbc-post-types-endpoints.php
3 months ago
class-adbc-posts-meta-endpoints.php
3 months ago
class-adbc-settings-endpoints.php
3 months ago
class-adbc-tables-endpoints.php
3 months ago
class-adbc-transients-endpoints.php
3 months ago
class-adbc-users-meta-endpoints.php
3 months ago
class-adbc-settings-endpoints.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | /** |
| 8 | * ADBC settings endpoints class. |
| 9 | * |
| 10 | * This class provides the endpoints (controllers) for the settings routes. |
| 11 | */ |
| 12 | class ADBC_Settings_Endpoints { |
| 13 | |
| 14 | /** |
| 15 | * Update one or multiple ADBC settings in database by keys. |
| 16 | * |
| 17 | * @param WP_REST_Request $setting Setting keys and their values. |
| 18 | * @return WP_REST_Response Response. |
| 19 | */ |
| 20 | public static function update_settings( WP_REST_Request $settings ) { |
| 21 | |
| 22 | try { |
| 23 | |
| 24 | $settings = $settings->get_json_params(); |
| 25 | |
| 26 | // Iterate through the settings array and validate each key and value. |
| 27 | foreach ( $settings as $key => $value ) { |
| 28 | |
| 29 | $sanitized_key = sanitize_key( $key ); |
| 30 | |
| 31 | // Validate key. |
| 32 | if ( ! ADBC_Settings_Validator::is_valid_setting_key( $sanitized_key ) ) |
| 33 | return ADBC_Rest::error( "Invalid setting key.", ADBC_Rest::BAD_REQUEST ); |
| 34 | |
| 35 | // Validate the value case by case. |
| 36 | switch ( $sanitized_key ) { |
| 37 | case 'left_menu': |
| 38 | case 'tools_menu': |
| 39 | case 'network_menu': |
| 40 | case 'send_corrections_to_server': |
| 41 | case 'reduce_cpu_usage': |
| 42 | case 'addons_activity_enabled': |
| 43 | case 'analytics_enabled': |
| 44 | case 'show_tables_with_invalid_prefix': |
| 45 | case 'sidebar_is_expanded': |
| 46 | case 'prevent_taking_action_on_wp_items': |
| 47 | case 'show_confirmation_on_dangerous_actions': |
| 48 | if ( ! ADBC_Common_Validator::is_string_equals_0_or_1( "", $value ) ) |
| 49 | return ADBC_Rest::error( "Invalid setting value.", ADBC_Rest::BAD_REQUEST ); |
| 50 | break; |
| 51 | case 'hidden_tabs': |
| 52 | if ( ! ADBC_Settings_Validator::are_valid_hiddenable_tabs( "", $value ) ) |
| 53 | return ADBC_Rest::error( "Invalid setting value.", ADBC_Rest::BAD_REQUEST ); |
| 54 | break; |
| 55 | case 'file_lines_batch': |
| 56 | case 'file_content_chunks': |
| 57 | case 'scan_max_execution_time': |
| 58 | if ( ! ADBC_Settings_Validator::is_scan_setting_valid( $sanitized_key, $value ) ) |
| 59 | return ADBC_Rest::error( "Please ensure that the scan settings respect the indicated min and max acceptable values.", ADBC_Rest::BAD_REQUEST ); |
| 60 | break; |
| 61 | case 'cpu_work_time_ms': |
| 62 | case 'cpu_rest_time_ms': |
| 63 | if ( ! ADBC_Settings_Validator::is_cpu_usage_time_valid( $sanitized_key, $value ) ) |
| 64 | return ADBC_Rest::error( "Please ensure that the CPU usage settings respect the indicated min and max acceptable values.", ADBC_Rest::BAD_REQUEST ); |
| 65 | break; |
| 66 | case 'database_rows_batch': |
| 67 | case 'sql_or_native_cleanup_method': |
| 68 | if ( ! ADBC_Settings_Validator::is_performance_settings_valid( $sanitized_key, $value ) ) |
| 69 | return ADBC_Rest::error( "Please ensure that the performance settings respect the acceptable values.", ADBC_Rest::BAD_REQUEST ); |
| 70 | break; |
| 71 | default: |
| 72 | return ADBC_Rest::error( "This setting cannot be changed.", ADBC_Rest::BAD_REQUEST ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | ADBC_Settings::instance()->update_settings( $settings ); // Update setting in database if all keys and values are valid. |
| 77 | return ADBC_Rest::success( "" ); |
| 78 | |
| 79 | } catch (Throwable $e) { |
| 80 | |
| 81 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 82 | |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get specific ADBC setting by key. |
| 88 | * |
| 89 | * @param WP_REST_Request $setting_key Setting key. |
| 90 | * @return WP_REST_Response Response. |
| 91 | */ |
| 92 | public static function get_setting( WP_REST_Request $setting_key ) { |
| 93 | |
| 94 | try { |
| 95 | |
| 96 | $key = $setting_key->get_param( 'key' ); |
| 97 | $sanitized_key = sanitize_key( $key ); |
| 98 | |
| 99 | // Validate key. |
| 100 | if ( ! ADBC_Settings_Validator::is_valid_setting_key( $sanitized_key ) ) |
| 101 | return ADBC_Rest::error( "Invalid setting key.", ADBC_Rest::BAD_REQUEST ); |
| 102 | |
| 103 | // Do not return sensitive settings like the security code. |
| 104 | if ( $sanitized_key === 'security_code' ) |
| 105 | return ADBC_Rest::error( "Cannot get content of this setting!", ADBC_Rest::BAD_REQUEST ); |
| 106 | |
| 107 | $value = ADBC_Settings::instance()->get_setting( $sanitized_key ); |
| 108 | return ADBC_Rest::success( "", [ 'value' => $value ] ); |
| 109 | |
| 110 | } catch (Exception $e) { |
| 111 | |
| 112 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 113 | |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } |