PluginProbe ʕ •ᴥ•ʔ
10Web Booster – Website speed optimization, Cache & Page Speed optimizer / trunk
10Web Booster – Website speed optimization, Cache & Page Speed optimizer vtrunk
2.33.0 2.30.5 2.30.7 2.30.9 2.31.10 2.31.8 2.32.11 2.32.21 2.32.3 2.32.4 2.32.7 2.6.31 2.6.40 2.6.42 2.6.7 2.7.37 2.7.44 2.7.47 2.8.18 2.8.19 2.8.32 2.8.34 2.8.35 2.9.23 2.9.24 2.9.25 2.9.27 v2.27.4 trunk 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.17 2.0.18 2.0.21 2.0.22 2.0.25 2.0.26 2.0.27 2.0.3 2.0.7 2.0.9 2.10.46 2.10.65 2.10.66 2.10.68 2.11.41 2.11.42 2.11.43 2.12.15 2.12.21 2.12.22 2.12.23 2.12.26 2.13.37 2.13.40 2.13.41 2.13.42 2.13.44 2.13.45 2.13.47 2.14.49 2.14.50 2.15.18 2.17.21 2.17.23 2.18.17 2.19.44 2.19.45 2.19.46 2.19.49 2.2.12 2.2.15 2.2.16 2.2.18 2.2.8 2.20.31 2.20.32 2.20.33 2.21.11 2.21.12 2.21.16 2.21.25 2.22.32 2.23.13 2.23.15 2.23.16 2.23.18 2.24.12 2.24.14 2.24.18 2.25.14 2.26.6 2.28.10 2.28.13 2.28.14 2.28.7 2.29.1 2.29.2 2.29.3 2.3.0 2.3.1 2.3.2 2.3.3 2.30.18
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