PluginProbe
Hostinger Tools / 3.0.79
Hostinger Tools v3.0.79
3.0.79 3.0.78 3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 All 110 releases
hostinger / includes / Rest / Routes.php

Routes.php in Hostinger Tools 3.0.79, at includes/Rest/Routes.php

123 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hostinger\Rest;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 die;
7 }
8
9 /**
10 * Class for handling Rest Api Routes
11 */
12 class Routes {
13 /**
14 * @var Settings
15 */
16 private SettingsRoutes $settings_routes;
17
18 private CacheRoutes $cache_routes;
19
20 /**
21 * @param SettingsRoutes $settings_routes Settings route class.
22 * @param CacheRoutes $cache_routes Cache route class.
23 */
24 public function __construct( SettingsRoutes $settings_routes, CacheRoutes $cache_routes ) {
25 $this->settings_routes = $settings_routes;
26 $this->cache_routes = $cache_routes;
27 }
28
29 /**
30 * Init rest routes
31 *
32 * @return void
33 */
34 public function init(): void {
35 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
36 }
37
38 /**
39 * @return void
40 */
41 public function register_routes() {
42 // Register Settings Rest API Routes.
43 $this->register_settings_routes();
44 $this->register_cache_routes();
45 }
46
47 private function register_cache_routes(): void {
48 register_rest_route(
49 HOSTINGER_PLUGIN_REST_API_BASE,
50 'clear-cache',
51 array(
52 'methods' => \WP_REST_Server::DELETABLE,
53 'callback' => array( $this->cache_routes, 'clear_cache' ),
54 'permission_callback' => array( $this, 'permission_check' ),
55 )
56 );
57 }
58
59 /**
60 *
61 * @return void
62 */
63 private function register_settings_routes(): void {
64 // Return settings.
65 register_rest_route(
66 HOSTINGER_PLUGIN_REST_API_BASE,
67 'get-settings',
68 array(
69 'methods' => 'GET',
70 'callback' => array( $this->settings_routes, 'get_settings' ),
71 'permission_callback' => array( $this, 'permission_check' ),
72 )
73 );
74
75 // Update settings.
76 register_rest_route(
77 HOSTINGER_PLUGIN_REST_API_BASE,
78 'update-settings',
79 array(
80 'methods' => 'POST',
81 'callback' => array( $this->settings_routes, 'update_settings' ),
82 'permission_callback' => array( $this, 'permission_check' ),
83 )
84 );
85
86 // Regenerate bypass link.
87 register_rest_route(
88 HOSTINGER_PLUGIN_REST_API_BASE,
89 'regenerate-bypass-code',
90 array(
91 'methods' => 'GET',
92 'callback' => array( $this->settings_routes, 'regenerate_bypass_code' ),
93 'permission_callback' => array( $this, 'permission_check' ),
94 )
95 );
96 }
97
98 /**
99 * @param WP_REST_Request $request WordPress rest request.
100 *
101 * @return bool
102 */
103 /** PHPCS:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found */
104 public function permission_check( $request ): bool {
105 // Workaround if Rest Api endpoint cache is enabled.
106 // We don't want to cache these requests.
107 if ( has_action( 'litespeed_control_set_nocache' ) ) {
108 do_action(
109 'litespeed_control_set_nocache',
110 'Custom Rest API endpoint, not cacheable.'
111 );
112 }
113
114 if ( empty( is_user_logged_in() ) ) {
115 return false;
116 }
117
118 // Implement custom capabilities when needed.
119 return current_user_can( 'manage_options' );
120 }
121 }
122 /** PHPCS:enable */
123