map_plugin_to_options = new Map_Plugin_To_Options(); add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); } /** * Register the REST API routes. * * @return void */ public function register_rest_routes() { \register_rest_route( 'aaa-option-optimizer/v1', '/update-autoload', [ 'methods' => 'POST', 'callback' => [ $this, 'update_option_autoload' ], 'permission_callback' => function () { return current_user_can( 'manage_options' ); }, 'args' => [ 'option_name' => [ 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ], 'autoload' => [ 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ], ], ] ); \register_rest_route( 'aaa-option-optimizer/v1', '/delete-option', [ 'methods' => 'POST', 'callback' => [ $this, 'delete_option' ], 'permission_callback' => function () { return current_user_can( 'manage_options' ); }, 'args' => [ 'option_name' => [ 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ], ], ] ); \register_rest_route( 'aaa-option-optimizer/v1', '/create-option-false', [ 'methods' => 'POST', 'callback' => [ $this, 'create_option_false' ], 'permission_callback' => function () { return current_user_can( 'manage_options' ); }, 'args' => [ 'option_name' => [ 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ], ], ] ); \register_rest_route( 'aaa-option-optimizer/v1', '/all-options', [ 'methods' => 'GET', 'callback' => [ $this, 'get_all_options' ], 'permission_callback' => function () { return current_user_can( 'manage_options' ); }, ] ); \register_rest_route( 'aaa-option-optimizer/v1', '/reset', [ 'methods' => 'POST', 'callback' => [ $this, 'reset_stats' ], 'permission_callback' => function () { return current_user_can( 'manage_options' ); }, ] ); } /** * Update autoload status of an option. * * @return \WP_Error|\WP_REST_Response */ public function reset_stats() { Plugin::get_instance()->reset(); return new \WP_REST_Response( [ 'success' => true ], 200 ); } /** * Update autoload status of an option. * * @return \WP_Error|\WP_REST_Response */ public function get_all_options() { global $wpdb; $output = []; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- We need to query all options. $options = $wpdb->get_results( "SELECT option_name, option_value, autoload FROM $wpdb->options" ); foreach ( $options as $option ) { $output[] = [ 'name' => $option->option_name, 'plugin' => $this->map_plugin_to_options->get_plugin_name( $option->option_name ), 'value' => htmlentities( $option->option_value, ENT_QUOTES | ENT_SUBSTITUTE ), 'size' => number_format( strlen( $option->option_value ) / 1024, 2 ), 'autoload' => $option->autoload, 'row_id' => 'option_' . $option->option_name, ]; } return new \WP_REST_Response( [ 'data' => $output ], 200 ); } /** * Update autoload status of an option. * * @param \WP_REST_Request $request The REST request object. * * @return \WP_Error|\WP_REST_Response */ public function update_option_autoload( $request ) { $option_name = $request['option_name']; $autoload = $request['autoload']; $option_value = get_option( $option_name ); if ( ! in_array( $autoload, [ 'yes', 'on', 'no', 'off','auto', 'auto-on', 'auto-off' ], true ) ) { return new \WP_Error( 'invalid_autoload_value', 'Invalid autoload value', [ 'status' => 400 ] ); } if ( false === $option_value ) { return new \WP_Error( 'option_not_found', 'Option does not exist', [ 'status' => 404 ] ); } delete_option( $option_name ); $autoload_values = \wp_autoload_values_to_autoload(); $bool_autoload = false; if ( in_array( $autoload, $autoload_values, true ) ) { $bool_autoload = true; } $succeeded = add_option( $option_name, $option_value, '', $bool_autoload ); if ( ! $succeeded ) { return new \WP_Error( 'update_failed', 'Updating the option failed', [ 'status' => 400 ] ); } return new \WP_REST_Response( [ 'success' => true ], 200 ); } /** * Delete an option. * * @param \WP_REST_Request $request The REST request object. * * @return \WP_REST_Response|\WP_Error */ public function delete_option( $request ) { $option_name = $request['option_name']; if ( delete_option( $option_name ) ) { return new \WP_REST_Response( [ 'success' => true ], 200 ); } return new \WP_Error( 'option_not_found_or_deleted', 'Option does not exist or could not be deleted', [ 'status' => 404 ] ); } /** * Create an option with a false value. * * @param \WP_REST_Request $request The REST request object. * * @return \WP_Error|\WP_REST_Response */ public function create_option_false( $request ) { $option_name = $request['option_name']; if ( add_option( $option_name, false, '', false ) ) { return new \WP_REST_Response( [ 'success' => true ], 200 ); } return new \WP_Error( 'option_not_created', 'Option could not be created', [ 'status' => 400 ] ); } }