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-options-endpoints.php
324 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | /** |
| 8 | * ADBC Options Endpoints. |
| 9 | * |
| 10 | * This class provides the endpoints (controllers) for the options routes. |
| 11 | */ |
| 12 | class ADBC_Options_Endpoints { |
| 13 | |
| 14 | /** |
| 15 | * Get the options list. |
| 16 | * |
| 17 | * @param WP_REST_Request $filters_request The request with the filters. |
| 18 | * @return WP_REST_Response The list of options. |
| 19 | */ |
| 20 | public static function get_options_list( WP_REST_Request $filters_request ) { |
| 21 | |
| 22 | try { |
| 23 | |
| 24 | $filters = ADBC_Common_Validator::sanitize_filters( $filters_request ); |
| 25 | $rest_response = ADBC_Options::get_options_list( $filters ); |
| 26 | return $rest_response; |
| 27 | |
| 28 | } catch (Throwable $e) { |
| 29 | |
| 30 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 31 | |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Edit scan results of options. |
| 37 | * |
| 38 | * @param WP_REST_Request $request_data The request with the options to edit. |
| 39 | * @return WP_REST_Response The response. |
| 40 | */ |
| 41 | public static function edit_scan_results_options( WP_REST_Request $request_data ) { |
| 42 | |
| 43 | try { |
| 44 | |
| 45 | return ADBC_Scan_Utils::edit_scan_results( $request_data, 'edit_scan_results_options', 'options' ); |
| 46 | |
| 47 | } catch (Throwable $e) { |
| 48 | |
| 49 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 50 | |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Set autoload to "yes" for options. |
| 56 | * |
| 57 | * @param WP_REST_Request $request_data The request with the options to set autoload to "yes". |
| 58 | * @return WP_REST_Response The response. |
| 59 | */ |
| 60 | public static function set_autoload_to_yes_options( WP_REST_Request $request_data ) { |
| 61 | |
| 62 | try { |
| 63 | |
| 64 | $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "set_autoload_to_yes_options", "options", $request_data ); |
| 65 | |
| 66 | // If $validation_answer is not an array, it means that the validation failed and we have an error message. |
| 67 | if ( ! is_array( $validation_answer ) ) |
| 68 | return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST ); |
| 69 | |
| 70 | $cleaned_options = $validation_answer; |
| 71 | |
| 72 | if ( ADBC_Settings::instance()->get_setting( 'prevent_taking_action_on_wp_items' ) === '1' ) { |
| 73 | |
| 74 | $cleaned_options = ADBC_Hardcoded_Items::instance()->exclude_hardcoded_items_from_selected_items( $validation_answer, 'options', "wp" ); |
| 75 | |
| 76 | if ( ADBC_VERSION_TYPE === 'PREMIUM' ) |
| 77 | $cleaned_options = ADBC_Scan_Utils::exclude_r_wp_items_from_selected_items( $cleaned_options, 'options' ); |
| 78 | |
| 79 | if ( empty( $cleaned_options ) ) |
| 80 | return ADBC_Rest::error( |
| 81 | __( 'The selected options could not have their autoload edited because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), |
| 82 | ADBC_Rest::BAD_REQUEST, |
| 83 | 0, |
| 84 | [ |
| 85 | "message_links" => [ |
| 86 | [ |
| 87 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 88 | "tab_id" => "settings", |
| 89 | "anchor_id" => "other_settings", |
| 90 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 91 | ] |
| 92 | ] |
| 93 | ] |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | $grouped = ADBC_Selected_Items_Validator::group_selected_items_by_site_id( $cleaned_options ); |
| 98 | $not_processed = ADBC_Options::set_autoload_to_yes( $grouped ); |
| 99 | |
| 100 | if ( count( $cleaned_options ) < count( $validation_answer ) ) { |
| 101 | return ADBC_Rest::success( |
| 102 | __( 'Some options were updated successfully; others were skipped because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), |
| 103 | count( $not_processed ), |
| 104 | [ |
| 105 | "message_links" => [ |
| 106 | [ |
| 107 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 108 | "tab_id" => "settings", |
| 109 | "anchor_id" => "other_settings", |
| 110 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 111 | ] |
| 112 | ] |
| 113 | ] |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | return ADBC_Rest::success( "", count( $not_processed ) ); |
| 118 | |
| 119 | } catch (Throwable $e) { |
| 120 | |
| 121 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 122 | |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Set autoload to "no" for options. |
| 128 | * |
| 129 | * @param WP_REST_Request $request_data The request with the options to set autoload to "no". |
| 130 | * @return WP_REST_Response The response. |
| 131 | */ |
| 132 | public static function set_autoload_to_no_options( WP_REST_Request $request_data ) { |
| 133 | |
| 134 | try { |
| 135 | |
| 136 | $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "set_autoload_to_no_options", "options", $request_data ); |
| 137 | |
| 138 | // If $validation_answer is not an array, it means that the validation failed and we have an error message. |
| 139 | if ( ! is_array( $validation_answer ) ) |
| 140 | return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST ); |
| 141 | |
| 142 | $cleaned_options = $validation_answer; |
| 143 | |
| 144 | if ( ADBC_Settings::instance()->get_setting( 'prevent_taking_action_on_wp_items' ) === '1' ) { |
| 145 | |
| 146 | $cleaned_options = ADBC_Hardcoded_Items::instance()->exclude_hardcoded_items_from_selected_items( $validation_answer, 'options', "wp" ); |
| 147 | |
| 148 | if ( ADBC_VERSION_TYPE === 'PREMIUM' ) |
| 149 | $cleaned_options = ADBC_Scan_Utils::exclude_r_wp_items_from_selected_items( $cleaned_options, 'options' ); |
| 150 | |
| 151 | if ( empty( $cleaned_options ) ) |
| 152 | return ADBC_Rest::error( |
| 153 | __( 'The selected options could not have their autoload edited because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), ADBC_Rest::BAD_REQUEST, |
| 154 | 0, |
| 155 | [ |
| 156 | "message_links" => [ |
| 157 | [ |
| 158 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 159 | "tab_id" => "settings", |
| 160 | "anchor_id" => "other_settings", |
| 161 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 162 | ] |
| 163 | ] |
| 164 | ] |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | $grouped = ADBC_Selected_Items_Validator::group_selected_items_by_site_id( $cleaned_options ); |
| 169 | $not_processed = ADBC_Options::set_autoload_to_no( $grouped ); |
| 170 | |
| 171 | if ( count( $cleaned_options ) < count( $validation_answer ) ) { |
| 172 | return ADBC_Rest::success( |
| 173 | __( 'Some options were updated successfully; others were skipped because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), |
| 174 | count( $not_processed ), |
| 175 | [ |
| 176 | "message_links" => [ |
| 177 | [ |
| 178 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 179 | "tab_id" => "settings", |
| 180 | "anchor_id" => "other_settings", |
| 181 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 182 | ] |
| 183 | ] |
| 184 | ] |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | return ADBC_Rest::success( "", count( $not_processed ) ); |
| 189 | |
| 190 | } catch (Throwable $e) { |
| 191 | |
| 192 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 193 | |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Delete options. |
| 199 | * |
| 200 | * @param WP_REST_Request $request_data The request with the options to delete. |
| 201 | * @return WP_REST_Response The response. |
| 202 | */ |
| 203 | public static function delete_options( WP_REST_Request $request_data ) { |
| 204 | |
| 205 | try { |
| 206 | |
| 207 | // Verify if there is a scan in progress. If there is, return an error to prevent conflicts. |
| 208 | if ( ADBC_VERSION_TYPE === 'PREMIUM' && ADBC_Scan_Utils::is_scan_exists( 'options' ) ) |
| 209 | return ADBC_Rest::error( __( 'A scan is in progress. Please wait until it finishes before performing this action.', 'advanced-database-cleaner' ), ADBC_Rest::BAD_REQUEST ); |
| 210 | |
| 211 | $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "delete_options", "options", $request_data ); |
| 212 | |
| 213 | // If $validation_answer is not an array, it means that the validation failed and we have an error message. |
| 214 | if ( ! is_array( $validation_answer ) ) |
| 215 | return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST ); |
| 216 | |
| 217 | $cleaned_options = $validation_answer; |
| 218 | |
| 219 | if ( ADBC_Settings::instance()->get_setting( 'prevent_taking_action_on_wp_items' ) === '1' ) { |
| 220 | |
| 221 | $cleaned_options = ADBC_Hardcoded_Items::instance()->exclude_hardcoded_items_from_selected_items( $validation_answer, 'options', "wp" ); |
| 222 | |
| 223 | if ( ADBC_VERSION_TYPE === 'PREMIUM' ) |
| 224 | $cleaned_options = ADBC_Scan_Utils::exclude_r_wp_items_from_selected_items( $cleaned_options, 'options' ); |
| 225 | |
| 226 | if ( empty( $cleaned_options ) ) |
| 227 | return ADBC_Rest::error( |
| 228 | __( 'The selected options could not be deleted because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), |
| 229 | ADBC_Rest::BAD_REQUEST, |
| 230 | 0, |
| 231 | [ |
| 232 | "message_links" => [ |
| 233 | [ |
| 234 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 235 | "tab_id" => "settings", |
| 236 | "anchor_id" => "other_settings", |
| 237 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 238 | ] |
| 239 | ] |
| 240 | ] |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | $grouped = ADBC_Selected_Items_Validator::group_selected_items_by_site_id( $cleaned_options ); |
| 245 | $not_processed = ADBC_Options::delete_options( $grouped ); |
| 246 | |
| 247 | // Delete the options from the scan results |
| 248 | if ( ADBC_VERSION_TYPE === 'PREMIUM' ) { |
| 249 | $options_names = array_column( $cleaned_options, 'name' ); // Create an array containing only the options names. |
| 250 | ADBC_Scan_Utils::update_scan_results_file_after_deletion( 'options', $options_names, $not_processed ); |
| 251 | } |
| 252 | |
| 253 | if ( count( $cleaned_options ) < count( $validation_answer ) ) { |
| 254 | return ADBC_Rest::success( |
| 255 | __( 'Some options were deleted successfully; others were skipped because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), |
| 256 | count( $not_processed ), |
| 257 | [ |
| 258 | "message_links" => [ |
| 259 | [ |
| 260 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 261 | "tab_id" => "settings", |
| 262 | "anchor_id" => "other_settings", |
| 263 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 264 | ] |
| 265 | ] |
| 266 | ] |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | return ADBC_Rest::success( "", count( $not_processed ) ); |
| 271 | |
| 272 | } catch (Throwable $e) { |
| 273 | |
| 274 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 275 | |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Count the size of all autoloaded options in the wp_options table. |
| 281 | * |
| 282 | * @return WP_REST_Response The response. |
| 283 | */ |
| 284 | public static function count_big_options() { |
| 285 | try { |
| 286 | return ADBC_Rest::success( "", ADBC_Options::count_big_options() ); |
| 287 | } catch (Throwable $e) { |
| 288 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Count the total number of options that are not scanned. |
| 294 | * |
| 295 | * @return WP_REST_Response The response. |
| 296 | */ |
| 297 | public static function count_total_not_scanned_options() { |
| 298 | |
| 299 | try { |
| 300 | return ADBC_Rest::success( "", ADBC_Options::count_total_not_scanned_options() ); |
| 301 | } catch (Throwable $e) { |
| 302 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Count the health of the autoloaded options. |
| 308 | * |
| 309 | * @return WP_REST_Response The response. |
| 310 | */ |
| 311 | public static function get_autoload_health() { |
| 312 | try { |
| 313 | return ADBC_Rest::success( "", ADBC_Options::count_autoload_size_using_sql() ); |
| 314 | } catch (Throwable $e) { |
| 315 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | } |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 |