Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
41.86% |
18 / 43 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| LinkPrefetchController | |
41.86% |
18 / 43 |
|
0.00% |
0 / 3 |
9.91 | |
0.00% |
0 / 1 |
| register_routes | |
90.00% |
18 / 20 |
|
0.00% |
0 / 1 |
1.00 | |||
| get_settings | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| update_settings | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\RestApi; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Performance\LinkPrefetch\LinkPrefetch; |
| 6 | |
| 7 | /** |
| 8 | * Class LinkPrefetchController |
| 9 | */ |
| 10 | class LinkPrefetchController { |
| 11 | |
| 12 | /** |
| 13 | * REST namespace |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $namespace = 'newfold-performance/v1'; |
| 18 | |
| 19 | /** |
| 20 | * REST base |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $rest_base = '/link-prefetch'; |
| 25 | |
| 26 | /** |
| 27 | * Registers rest routes for PluginsController class. |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public function register_routes() { |
| 32 | register_rest_route( |
| 33 | $this->namespace, |
| 34 | $this->rest_base . '/settings', |
| 35 | array( |
| 36 | array( |
| 37 | 'methods' => \WP_REST_Server::READABLE, |
| 38 | 'callback' => array( $this, 'get_settings' ), |
| 39 | 'permission_callback' => function () { |
| 40 | return current_user_can( 'manage_options' ); |
| 41 | }, |
| 42 | ), |
| 43 | array( |
| 44 | 'methods' => \WP_REST_Server::CREATABLE, |
| 45 | 'callback' => array( $this, 'update_settings' ), |
| 46 | 'permission_callback' => function () { |
| 47 | return current_user_can( 'manage_options' ); |
| 48 | }, |
| 49 | ), |
| 50 | ) |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the settings |
| 56 | * |
| 57 | * @return \WP_REST_Response |
| 58 | */ |
| 59 | public function get_settings() { |
| 60 | return new \WP_REST_Response( |
| 61 | array( |
| 62 | 'settings' => LinkPrefetch::get_settings(), |
| 63 | ), |
| 64 | 200 |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Update the settings |
| 70 | * |
| 71 | * @param \WP_REST_Request $request the request. |
| 72 | * @return \WP_REST_Response |
| 73 | */ |
| 74 | public function update_settings( \WP_REST_Request $request ) { |
| 75 | $settings = $request->get_param( 'settings' ); |
| 76 | if ( is_array( $settings ) ) { |
| 77 | $settings['ignoreKeywords'] = sanitize_text_field( $settings['ignoreKeywords'] ); |
| 78 | $updated = LinkPrefetch::update_settings( $settings ); |
| 79 | return new \WP_REST_Response( |
| 80 | array( |
| 81 | 'result' => $updated, |
| 82 | ), |
| 83 | $updated ? 200 : 400 |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | return new \WP_REST_Response( |
| 88 | array( |
| 89 | 'result' => false, |
| 90 | 'message' => 'Invalid settings format', |
| 91 | ), |
| 92 | 400 |
| 93 | ); |
| 94 | } |
| 95 | } |