| 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 |
/** |
| 19 |
* @param SettingsRoutes $settings_routes Settings route class. |
| 20 |
*/ |
| 21 |
public function __construct( SettingsRoutes $settings_routes ) { |
| 22 |
$this->settings_routes = $settings_routes; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Init rest routes |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function init(): void { |
| 31 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function register_routes() { |
| 38 |
// Register Settings Rest API Routes. |
| 39 |
$this->register_settings_routes(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
private function register_settings_routes(): void { |
| 47 |
// Return settings. |
| 48 |
register_rest_route( |
| 49 |
HOSTINGER_PLUGIN_REST_API_BASE, |
| 50 |
'get-settings', |
| 51 |
array( |
| 52 |
'methods' => 'GET', |
| 53 |
'callback' => array( $this->settings_routes, 'get_settings' ), |
| 54 |
'permission_callback' => array( $this, 'permission_check' ), |
| 55 |
) |
| 56 |
); |
| 57 |
|
| 58 |
// Update settings. |
| 59 |
register_rest_route( |
| 60 |
HOSTINGER_PLUGIN_REST_API_BASE, |
| 61 |
'update-settings', |
| 62 |
array( |
| 63 |
'methods' => 'POST', |
| 64 |
'callback' => array( $this->settings_routes, 'update_settings' ), |
| 65 |
'permission_callback' => array( $this, 'permission_check' ), |
| 66 |
) |
| 67 |
); |
| 68 |
|
| 69 |
// Regenerate bypass link. |
| 70 |
register_rest_route( |
| 71 |
HOSTINGER_PLUGIN_REST_API_BASE, |
| 72 |
'regenerate-bypass-code', |
| 73 |
array( |
| 74 |
'methods' => 'GET', |
| 75 |
'callback' => array( $this->settings_routes, 'regenerate_bypass_code' ), |
| 76 |
'permission_callback' => array( $this, 'permission_check' ), |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @param WP_REST_Request $request WordPress rest request. |
| 83 |
* |
| 84 |
* @return bool |
| 85 |
*/ |
| 86 |
/** PHPCS:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found */ |
| 87 |
public function permission_check( $request ): bool { |
| 88 |
// Workaround if Rest Api endpoint cache is enabled. |
| 89 |
// We don't want to cache these requests. |
| 90 |
if ( has_action( 'litespeed_control_set_nocache' ) ) { |
| 91 |
do_action( |
| 92 |
'litespeed_control_set_nocache', |
| 93 |
'Custom Rest API endpoint, not cacheable.' |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
if ( empty( is_user_logged_in() ) ) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
// Implement custom capabilities when needed. |
| 102 |
return current_user_can( 'manage_options' ); |
| 103 |
} |
| 104 |
} |
| 105 |
/** PHPCS:enable */ |
| 106 |
|