PluginProbe
Hostinger Tools / 3.0.0
Hostinger Tools v3.0.0
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 2.0.1 2.0.4 All 108 releases
hostinger / includes / Rest / Routes.php

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

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