Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
16.67% |
12 / 72 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Skip404Controller | |
16.67% |
12 / 72 |
|
0.00% |
0 / 2 |
95.33 | |
0.00% |
0 / 1 |
| register_routes | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
1.00 | |||
| set_options | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
132 | |||
| 1 | <?php |
| 2 | namespace NewfoldLabs\WP\Module\Performance\RestApi; |
| 3 | |
| 4 | use NewfoldLabs\WP\Module\Performance\Skip404\Skip404; |
| 5 | |
| 6 | /** |
| 7 | * Class Settings |
| 8 | * |
| 9 | * @package NewfoldLabs\WP\Module\Performance |
| 10 | */ |
| 11 | class Skip404Controller { |
| 12 | |
| 13 | /** |
| 14 | * The REST route namespace. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $namespace = 'newfold-performance/v1'; |
| 19 | |
| 20 | /** |
| 21 | * The REST route base. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $rest_base = '/skip404'; |
| 26 | |
| 27 | /** |
| 28 | * Register API routes. |
| 29 | * |
| 30 | * @return void |
| 31 | */ |
| 32 | public function register_routes() { |
| 33 | register_rest_route( |
| 34 | $this->namespace, |
| 35 | $this->rest_base, |
| 36 | array( |
| 37 | array( |
| 38 | 'methods' => \WP_REST_Server::CREATABLE, |
| 39 | 'callback' => array( $this, 'set_options' ), |
| 40 | 'permission_callback' => function () { |
| 41 | return current_user_can( 'manage_options' ); |
| 42 | }, |
| 43 | ), |
| 44 | ) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Set Jetpack options. |
| 50 | * |
| 51 | * @param WP_REST_Request $request The request object. |
| 52 | * @return WP_REST_Response |
| 53 | */ |
| 54 | public function set_options( $request ) { |
| 55 | try { |
| 56 | $params = $request->get_params(); |
| 57 | |
| 58 | if ( ! isset( $params['field'] ) || ! is_array( $params['field'] ) ) { |
| 59 | return new \WP_REST_Response( |
| 60 | array( |
| 61 | 'success' => false, |
| 62 | 'error' => __( "The parameter 'field' is missing or invalid.", 'wp-module-performance' ), |
| 63 | ), |
| 64 | 400 |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | $field = $params['field']; |
| 69 | |
| 70 | if ( ! isset( $field['id'], $field['value'] ) ) { |
| 71 | return new \WP_REST_Response( |
| 72 | array( |
| 73 | 'success' => false, |
| 74 | 'error' => __( "The fields 'id' and 'value' are required.", 'wp-module-performance' ), |
| 75 | ), |
| 76 | 400 |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | switch ( $field['id'] ) { |
| 81 | case 'skip404': |
| 82 | $bool_value = filter_var( $field['value'], FILTER_VALIDATE_BOOLEAN ); |
| 83 | $current_value = get_option( Skip404::OPTION_NAME, 'not_set' ); |
| 84 | if ( 'not_set' === $current_value ) { |
| 85 | add_option( Skip404::OPTION_NAME, $bool_value ); |
| 86 | break; |
| 87 | } |
| 88 | if ( $current_value !== $bool_value ) { |
| 89 | $result = update_option( Skip404::OPTION_NAME, $bool_value ); |
| 90 | if ( false === $result ) { |
| 91 | return new \WP_REST_Response( |
| 92 | array( |
| 93 | 'success' => false, |
| 94 | 'error' => __( 'An error occurred while updating the option.', 'wp-module-performance' ), |
| 95 | ), |
| 96 | 500 |
| 97 | ); |
| 98 | } |
| 99 | } |
| 100 | break; |
| 101 | default: |
| 102 | return new \WP_REST_Response( |
| 103 | array( |
| 104 | 'success' => false, |
| 105 | 'error' => __( 'Invalid field ID provided.', 'wp-module-performance' ), |
| 106 | ), |
| 107 | 400 |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | return new \WP_REST_Response( |
| 112 | array( |
| 113 | 'success' => true, |
| 114 | 'updated_option' => $field['id'], |
| 115 | 'updated_value' => isset( $bool_value ) ? $bool_value : $field['value'], |
| 116 | ), |
| 117 | 200 |
| 118 | ); |
| 119 | } catch ( \Exception $e ) { |
| 120 | return new \WP_REST_Response( |
| 121 | array( |
| 122 | 'success' => false, |
| 123 | 'error' => __( 'An error occurred while updating the option.', 'wp-module-performance' ) . $e->getMessage(), |
| 124 | ), |
| 125 | 500 |
| 126 | ); |
| 127 | } |
| 128 | } |
| 129 | } |