tenweb-speed-optimizer
/
vendor
/
10web-utils
/
10web-wordpress-benchmark
/
src
/
TenWebWpBenchmark
/
Rest.php
tenweb-speed-optimizer
/
vendor
/
10web-utils
/
10web-wordpress-benchmark
/
src
/
TenWebWpBenchmark
Last commit date
Init.php
3 years ago
OptimizerBenchmark.php
3 years ago
Rest.php
3 years ago
Rest.php
120 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TenWebWpBenchmark; |
| 4 | |
| 5 | use \WP_REST_Request; |
| 6 | use \WP_Error; |
| 7 | use \Exception; |
| 8 | use \WP_REST_Response; |
| 9 | use \WP_REST_Controller; |
| 10 | |
| 11 | class Rest extends WP_REST_Controller |
| 12 | { |
| 13 | |
| 14 | public const TWO_BENCHMARK_API_NAMESPACE = 'tenweb_so/v1'; |
| 15 | public function registerRoutes() |
| 16 | { |
| 17 | register_rest_route(self::TWO_BENCHMARK_API_NAMESPACE, 'get_benchmark_data', |
| 18 | array( |
| 19 | 'methods' => 'GET', |
| 20 | 'permission_callback' => array($this, 'check_authorization'), |
| 21 | 'callback' => array($this, "get_benchmark_data"), |
| 22 | ) |
| 23 | ); |
| 24 | |
| 25 | register_rest_route(self::TWO_BENCHMARK_API_NAMESPACE, 'rerun_benchmark', |
| 26 | array( |
| 27 | 'methods' => 'POST', |
| 28 | 'permission_callback' => array($this, 'check_authorization'), |
| 29 | 'callback' => array($this, "rerun_benchmark"), |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | private function clear_response() { |
| 35 | // Clear all unexpected output. We don't want to see a warning in rest response. |
| 36 | while (ob_get_level() !== 0) { |
| 37 | ob_end_clean(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param WP_REST_Request $request |
| 43 | * |
| 44 | * @return WP_REST_Response |
| 45 | */ |
| 46 | public function get_benchmark_data(WP_REST_Request $request) { |
| 47 | $this->clear_response(); |
| 48 | $data_for_response = array( |
| 49 | 'success'=>false, |
| 50 | 'message'=>"No Benchmark Data", |
| 51 | ); |
| 52 | try { |
| 53 | $benchmark = \TenWebWpBenchmark\OptimizerBenchmark::get_instance(); |
| 54 | if(isset($benchmark) && $benchmarkData = $benchmark->getData()){ |
| 55 | $data_for_response = [ |
| 56 | 'success' => true, |
| 57 | 'message' => 'Success', |
| 58 | 'data' => $benchmarkData, |
| 59 | ]; |
| 60 | } |
| 61 | } catch (Exception $exception) { |
| 62 | $data_for_response['message'] = 'Error in getting benchmark data'; |
| 63 | $data_for_response['error'] = $exception->getMessage().' in '.$exception->getFile().' on '.$exception->getLine(); |
| 64 | |
| 65 | return new WP_REST_Response($data_for_response, 500); |
| 66 | } |
| 67 | |
| 68 | return new WP_REST_Response($data_for_response, 200); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param WP_REST_Request $request |
| 73 | * |
| 74 | * @return WP_REST_Response |
| 75 | */ |
| 76 | public function rerun_benchmark(WP_REST_Request $request) { |
| 77 | $this->clear_response(); |
| 78 | $data_for_response = array( |
| 79 | 'success'=>false, |
| 80 | 'message'=>"Benchmark failed", |
| 81 | ); |
| 82 | try { |
| 83 | $benchmark = \TenWebWpBenchmark\OptimizerBenchmark::get_instance(); |
| 84 | if(isset($benchmark) && $benchmarkData = $benchmark->test()){ |
| 85 | $data_for_response = [ |
| 86 | 'success' => true, |
| 87 | 'message' => 'Success', |
| 88 | 'data' => $benchmarkData, |
| 89 | ]; |
| 90 | } |
| 91 | } catch (Exception $exception) { |
| 92 | $data_for_response['message'] = 'Error in rerunning benchmark'; |
| 93 | $data_for_response['error'] = $exception->getMessage().' in '.$exception->getFile().' on '.$exception->getLine(); |
| 94 | |
| 95 | return new WP_REST_Response($data_for_response, 500); |
| 96 | } |
| 97 | |
| 98 | return new WP_REST_Response($data_for_response, 200); |
| 99 | } |
| 100 | |
| 101 | public function check_authorization(WP_REST_Request $request){ |
| 102 | if (!\Tenweb_Authorization\Login::get_instance()->check_logged_in()){ |
| 103 | $data_for_response = array( |
| 104 | "code" => "unauthorized", |
| 105 | "message" => "unauthorized", |
| 106 | "data" => array( |
| 107 | "status" => 401 |
| 108 | ) |
| 109 | ); |
| 110 | return new WP_Error('rest_forbidden', $data_for_response, 401); |
| 111 | } |
| 112 | $authorize = \Tenweb_Authorization\Login::get_instance()->authorize($request); |
| 113 | if (is_array($authorize)) { |
| 114 | return new WP_Error('rest_forbidden', $authorize, 401); |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 |