Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| CacheController | |
0.00% |
0 / 52 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| register_routes | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
2 | |||
| get_settings | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| update_settings | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
20 | |||
| purge_all | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\RestApi; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\ECommerce\Permissions; |
| 6 | use NewfoldLabs\WP\Module\Performance\Cache\CacheExclusion; |
| 7 | use NewfoldLabs\WP\Module\Performance\Cache\CacheManager; |
| 8 | use NewfoldLabs\WP\Module\Performance\Cache\CachePurgingService; |
| 9 | |
| 10 | use function NewfoldLabs\WP\ModuleLoader\container; |
| 11 | use function NewfoldLabs\WP\Module\Performance\get_cache_level; |
| 12 | use function NewfoldLabs\WP\Module\Performance\get_cache_exclusion; |
| 13 | |
| 14 | /** |
| 15 | * Class CacheExclusionController |
| 16 | */ |
| 17 | class CacheController { |
| 18 | /** |
| 19 | * REST namespace |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $namespace = 'newfold-performance/v1'; |
| 24 | |
| 25 | /** |
| 26 | * REST base |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $rest_base = '/cache'; |
| 31 | |
| 32 | /** |
| 33 | * Registers rest routes for PluginsController class. |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public function register_routes() { |
| 38 | |
| 39 | \register_rest_route( |
| 40 | $this->namespace, |
| 41 | $this->rest_base . '/settings', |
| 42 | array( |
| 43 | array( |
| 44 | 'methods' => \WP_REST_Server::READABLE, |
| 45 | 'callback' => array( $this, 'get_settings' ), |
| 46 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 47 | ), |
| 48 | array( |
| 49 | 'methods' => \WP_REST_Server::CREATABLE, |
| 50 | 'callback' => array( $this, 'update_settings' ), |
| 51 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 52 | ), |
| 53 | array( |
| 54 | 'methods' => \WP_REST_Server::DELETABLE, |
| 55 | 'callback' => array( $this, 'purge_all' ), |
| 56 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 57 | ), |
| 58 | ) |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the settings |
| 64 | * |
| 65 | * @return \WP_REST_Response |
| 66 | */ |
| 67 | public function get_settings() { |
| 68 | return new \WP_REST_Response( |
| 69 | array( |
| 70 | 'cacheExclusion' => get_cache_exclusion(), |
| 71 | 'cacheLevel' => get_cache_level(), |
| 72 | ), |
| 73 | 200 |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Update the settings |
| 79 | * |
| 80 | * @param \WP_REST_Request $request the request. |
| 81 | * @return \WP_REST_Response |
| 82 | */ |
| 83 | public function update_settings( \WP_REST_Request $request ) { |
| 84 | |
| 85 | if ( $request->has_param( 'cacheExclusion' ) ) { |
| 86 | $cache_exclusion = $request->get_param( 'cacheExclusion' ); |
| 87 | $result = update_option( CacheExclusion::OPTION_CACHE_EXCLUSION, $cache_exclusion ); |
| 88 | } elseif ( $request->has_param( 'cacheLevel' ) ) { |
| 89 | $cache_level = $request->get_param( 'cacheLevel' ); |
| 90 | $result = update_option( CacheManager::OPTION_CACHE_LEVEL, $cache_level ); |
| 91 | } |
| 92 | |
| 93 | if ( $result ) { |
| 94 | return new \WP_REST_Response( |
| 95 | array( |
| 96 | 'result' => true, |
| 97 | ), |
| 98 | 200 |
| 99 | ); |
| 100 | } else { |
| 101 | return new \WP_REST_Response( |
| 102 | array( |
| 103 | 'result' => false, |
| 104 | ), |
| 105 | 400 |
| 106 | ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Clears the entire cache |
| 112 | */ |
| 113 | public function purge_all() { |
| 114 | |
| 115 | container()->get( 'cachePurger' )->purge_all(); |
| 116 | |
| 117 | return array( |
| 118 | 'status' => 'success', |
| 119 | 'message' => 'Cache purged', |
| 120 | ); |
| 121 | } |
| 122 | } |