Edit File by line
/home/zeestwma/ajeebong.../wp-conte.../plugins/image-op.../classes/rest
File: route.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace ImageOptimization\Classes\Rest;
[2] Fix | Delete
[3] Fix | Delete
use ReflectionClass;
[4] Fix | Delete
use WP_Error;
[5] Fix | Delete
use WP_REST_Request;
[6] Fix | Delete
use WP_REST_Response;
[7] Fix | Delete
use WP_User;
[8] Fix | Delete
[9] Fix | Delete
abstract class Route {
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Should the endpoint be validated for user authentication?
[13] Fix | Delete
* If set to TRUE, the default permission callback will make sure the user is logged in and has a valid user id
[14] Fix | Delete
* @var bool
[15] Fix | Delete
*/
[16] Fix | Delete
protected $auth = true;
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* holds current authenticated user id
[20] Fix | Delete
* @var int
[21] Fix | Delete
*/
[22] Fix | Delete
protected $current_user_id;
[23] Fix | Delete
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Should the endpoint override an existing one?
[27] Fix | Delete
* @var bool
[28] Fix | Delete
*/
[29] Fix | Delete
protected bool $override = false;
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Rest Endpoint namespace
[33] Fix | Delete
* @var string
[34] Fix | Delete
*/
[35] Fix | Delete
protected string $namespace = 'image-optimizer/v1';
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* @var array The valid HTTP methods. The list represents the general REST methods. Do not modify.
[39] Fix | Delete
*/
[40] Fix | Delete
private array $valid_http_methods = [
[41] Fix | Delete
'GET',
[42] Fix | Delete
'PATCH',
[43] Fix | Delete
'POST',
[44] Fix | Delete
'PUT',
[45] Fix | Delete
'DELETE',
[46] Fix | Delete
'HEAD',
[47] Fix | Delete
];
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* Route_Base constructor.
[51] Fix | Delete
*/
[52] Fix | Delete
public function __construct() {
[53] Fix | Delete
add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
/**
[57] Fix | Delete
* rest_api_init
[58] Fix | Delete
*
[59] Fix | Delete
* Registers REST endpoints.
[60] Fix | Delete
* Loops through the REST methods for this route, creates an endpoint configuration for
[61] Fix | Delete
* each of them and registers all the endpoints with the WordPress system.
[62] Fix | Delete
*/
[63] Fix | Delete
public function rest_api_init(): void {
[64] Fix | Delete
$methods = $this->get_methods();
[65] Fix | Delete
if ( empty( $methods ) ) {
[66] Fix | Delete
return;
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
$callbacks = false;
[70] Fix | Delete
foreach ( (array) $methods as $method ) {
[71] Fix | Delete
if ( ! in_array( $method, $this->valid_http_methods ) ) {
[72] Fix | Delete
continue;
[73] Fix | Delete
}
[74] Fix | Delete
if ( $method && ! $callbacks ) {
[75] Fix | Delete
$callbacks = [];
[76] Fix | Delete
}
[77] Fix | Delete
$callbacks[] = $this->build_endpoint_method_config( $method );
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
$arguments = $this->get_arguments();
[81] Fix | Delete
[82] Fix | Delete
if ( ! $callbacks && empty( $arguments ) ) {
[83] Fix | Delete
return;
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
$arguments = array_merge( $arguments, (array) $callbacks );
[87] Fix | Delete
register_rest_route( $this->namespace, '/' . $this->get_endpoint() . '/', $arguments, $this->override );
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
/**
[91] Fix | Delete
* get_methods
[92] Fix | Delete
* Rest Endpoint methods
[93] Fix | Delete
*
[94] Fix | Delete
* Returns an array of the supported REST methods for this route
[95] Fix | Delete
* @return array<string> REST methods being configured for this route.
[96] Fix | Delete
*/
[97] Fix | Delete
abstract public function get_methods(): array;
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* get_callback
[101] Fix | Delete
*
[102] Fix | Delete
* Returns a reference to the callback function to handle the REST method specified by the /method/ parameter.
[103] Fix | Delete
* @param string $method The REST method name
[104] Fix | Delete
*
[105] Fix | Delete
* @return callable A reference to a member function with the same name as the REST method being passed as a parameter,
[106] Fix | Delete
* or a reference to the default function /callback/.
[107] Fix | Delete
*/
[108] Fix | Delete
public function get_callback_method( string $method ): callable {
[109] Fix | Delete
$method_name = strtolower( $method );
[110] Fix | Delete
$callback = $this->method_exists_in_current_class( $method_name ) ? $method_name : 'callback';
[111] Fix | Delete
return [ $this, $callback ];
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
/**
[115] Fix | Delete
* get_permission_callback_method
[116] Fix | Delete
*
[117] Fix | Delete
* Returns a reference to the permission callback for the method if exists or the default one if it doesn't.
[118] Fix | Delete
* @param string $method The REST method name
[119] Fix | Delete
*
[120] Fix | Delete
* @return callable If a method called (rest-method)_permission_callback exists, returns a reference to it, otherwise
[121] Fix | Delete
* returns a reference to the default member method /permission_callback/.
[122] Fix | Delete
*/
[123] Fix | Delete
public function get_permission_callback_method( string $method ): callable {
[124] Fix | Delete
$method_name = strtolower( $method );
[125] Fix | Delete
$permission_callback_method = $method_name . '_permission_callback';
[126] Fix | Delete
$permission_callback = $this->method_exists_in_current_class( $permission_callback_method ) ? $permission_callback_method : 'permission_callback';
[127] Fix | Delete
return [ $this, $permission_callback ];
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
/**
[131] Fix | Delete
* maybe_add_args_to_config
[132] Fix | Delete
*
[133] Fix | Delete
* Checks if the class has a method call (rest-method)_args.
[134] Fix | Delete
* If it does, the function calls it and adds its response to the config object passed to the function, under the /args/ key.
[135] Fix | Delete
* if the function (rest-method)_consumes exists, it will add the response to the config object under the /consumes/ key.
[136] Fix | Delete
* if the function (rest-method)_produces exists, it will add the response to the config object under the /produces/ key.
[137] Fix | Delete
* if the function (rest-method)_summary exists, it will add the response to the config object under the /summary/ key.
[138] Fix | Delete
* if the function (rest-method)_description exists, it will add the response to the config object under the /description/ key.
[139] Fix | Delete
* @param string $method The REST method name being configured
[140] Fix | Delete
* @param array $config The configuration object for the method
[141] Fix | Delete
*
[142] Fix | Delete
* @return array The configuration object for the method, possibly after being amended
[143] Fix | Delete
*/
[144] Fix | Delete
public function maybe_add_args_to_config( string $method, array $config ): array {
[145] Fix | Delete
$method_name = strtolower( $method );
[146] Fix | Delete
$method_args = $method_name . '_args';
[147] Fix | Delete
if ( $this->method_exists_in_current_class( $method_args ) ) {
[148] Fix | Delete
$config['args'] = $this->{$method_args}();
[149] Fix | Delete
}
[150] Fix | Delete
$config['consumes'] = [ 'application/json' ];
[151] Fix | Delete
if ( $this->method_exists_in_current_class( $method . '_consumes' ) ) {
[152] Fix | Delete
$config['consumes'] = $this->{$method . '_consumes'}();
[153] Fix | Delete
}
[154] Fix | Delete
$config['produces'] = [ 'application/json' ];
[155] Fix | Delete
if ( $this->method_exists_in_current_class( $method . '_produces' ) ) {
[156] Fix | Delete
$config['produces'] = $this->{$method . '_produces'}();
[157] Fix | Delete
}
[158] Fix | Delete
if ( $this->method_exists_in_current_class( $method . '_summary' ) ) {
[159] Fix | Delete
$config['summary'] = $this->{$method . '_summary'}();
[160] Fix | Delete
}
[161] Fix | Delete
if ( $this->method_exists_in_current_class( $method . '_description' ) ) {
[162] Fix | Delete
$config['description'] = $this->{$method . '_description'}();
[163] Fix | Delete
}
[164] Fix | Delete
return $config;
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
/**
[168] Fix | Delete
* maybe_add_response_to_swagger
[169] Fix | Delete
*
[170] Fix | Delete
* If the function method /(rest-method)_response_callback/ exists, adds the filter
[171] Fix | Delete
* /swagger_api_response_(namespace with slashes replaced with underscores)_(endpoint with slashes replaced with underscores)/
[172] Fix | Delete
* with the aforementioned function method.
[173] Fix | Delete
* This filter is used with the WP API Swagger UI plugin to create documentation for the API.
[174] Fix | Delete
* The value being passed is an array: [
[175] Fix | Delete
'200' => ['description' => 'OK'],
[176] Fix | Delete
'404' => ['description' => 'Not Found'],
[177] Fix | Delete
'400' => ['description' => 'Bad Request']
[178] Fix | Delete
]
[179] Fix | Delete
* @param string $method REST method name
[180] Fix | Delete
*/
[181] Fix | Delete
public function maybe_add_response_to_swagger( string $method ): void {
[182] Fix | Delete
$method_name = strtolower( $method );
[183] Fix | Delete
$method_response_callback = $method_name . '_response_callback';
[184] Fix | Delete
if ( $this->method_exists_in_current_class( $method_response_callback ) ) {
[185] Fix | Delete
$response_filter = $method_name . '_' . str_replace(
[186] Fix | Delete
'/',
[187] Fix | Delete
'_',
[188] Fix | Delete
$this->namespace . '/' . $this->get_endpoint()
[189] Fix | Delete
);
[190] Fix | Delete
add_filter( 'swagger_api_responses_' . $response_filter, [ $this, $method_response_callback ] );
[191] Fix | Delete
}
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
/**
[195] Fix | Delete
* build_endpoint_method_config
[196] Fix | Delete
*
[197] Fix | Delete
* Builds a configuration array for the endpoint based on the presence of the callback, permission, additional parameters,
[198] Fix | Delete
* and response to Swagger member functions.
[199] Fix | Delete
* @param string $method The REST method for the endpoint
[200] Fix | Delete
*
[201] Fix | Delete
* @return array The endpoint configuration for the method specified by the parameter
[202] Fix | Delete
*/
[203] Fix | Delete
private function build_endpoint_method_config( string $method ): array {
[204] Fix | Delete
$config = [
[205] Fix | Delete
'methods' => $method,
[206] Fix | Delete
'callback' => $this->get_callback_method( $method ),
[207] Fix | Delete
'permission_callback' => $this->get_permission_callback_method( $method ),
[208] Fix | Delete
];
[209] Fix | Delete
$this->maybe_add_response_to_swagger( $method );
[210] Fix | Delete
return $this->maybe_add_args_to_config( $method, $config );
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
/**
[214] Fix | Delete
* method_exists_in_current_class
[215] Fix | Delete
*
[216] Fix | Delete
* Uses reflection to check if this class has the /method/ method.
[217] Fix | Delete
* @param string $method The name of the method being checked.
[218] Fix | Delete
*
[219] Fix | Delete
* @return bool TRUE if the class has the /method/ method, FALSE otherwise.
[220] Fix | Delete
*/
[221] Fix | Delete
private function method_exists_in_current_class( string $method ): bool {
[222] Fix | Delete
$class_name = get_class( $this );
[223] Fix | Delete
try {
[224] Fix | Delete
$reflection = new ReflectionClass( $class_name );
[225] Fix | Delete
} catch ( \ReflectionException $e ) {
[226] Fix | Delete
return false;
[227] Fix | Delete
}
[228] Fix | Delete
if ( ! $reflection->hasMethod( $method ) ) {
[229] Fix | Delete
return false;
[230] Fix | Delete
}
[231] Fix | Delete
$method_ref = $reflection->getMethod( $method );
[232] Fix | Delete
[233] Fix | Delete
return ( $method_ref && $class_name === $method_ref->class );
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
/**
[237] Fix | Delete
* permission_callback
[238] Fix | Delete
* Permissions callback fallback for the endpoint
[239] Fix | Delete
* Gets the current user ID and sets the /current_user_id/ property.
[240] Fix | Delete
* If the /auth/ property is set to /true/ will make sure that the user is logged in (has an id greater than 0)
[241] Fix | Delete
*
[242] Fix | Delete
* @param WP_REST_Request $request unused
[243] Fix | Delete
*
[244] Fix | Delete
* @return bool TRUE, if permission granted, FALSE otherwise
[245] Fix | Delete
*/
[246] Fix | Delete
public function permission_callback( WP_REST_Request $request ): bool {
[247] Fix | Delete
// try to get current user
[248] Fix | Delete
$this->current_user_id = get_current_user_id();
[249] Fix | Delete
if ( $this->auth ) {
[250] Fix | Delete
return $this->current_user_id > 0;
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
return true;
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
/**
[257] Fix | Delete
* callback
[258] Fix | Delete
* Fallback callback function, returns a response consisting of the string /ok/.
[259] Fix | Delete
*
[260] Fix | Delete
* @param WP_REST_Request $request unused
[261] Fix | Delete
*
[262] Fix | Delete
* @return WP_REST_Response Default Response of the string 'ok'.
[263] Fix | Delete
*/
[264] Fix | Delete
public function callback( WP_REST_Request $request ): WP_REST_Response {
[265] Fix | Delete
return rest_ensure_response( [ 'OK' ] );
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
/**
[269] Fix | Delete
* respond_wrong_method
[270] Fix | Delete
*
[271] Fix | Delete
* Creates a WordPress error object with the /rest_no_route/ code and the message and code supplied or the defaults.
[272] Fix | Delete
* @param null $message The error message for the wrong method.
[273] Fix | Delete
* Optional.
[274] Fix | Delete
* Defaults to null, which makes sets the message to /No route was found matching the URL and request method/
[275] Fix | Delete
* @param int $code The HTTP status code.
[276] Fix | Delete
* Optional.
[277] Fix | Delete
* Defaults to 404 (Not found).
[278] Fix | Delete
*
[279] Fix | Delete
* @return WP_Error The WordPress error object with the error message and status code supplied
[280] Fix | Delete
*/
[281] Fix | Delete
public function respond_wrong_method( $message = null, int $code = 404 ): WP_Error {
[282] Fix | Delete
if ( null === $message ) {
[283] Fix | Delete
$message = __( 'No route was found matching the URL and request method', 'image-optimization' );
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
return new WP_Error( 'rest_no_route', $message, [ 'status' => $code ] );
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
/**
[290] Fix | Delete
* respond_with_code
[291] Fix | Delete
* Create a new /WP_REST_Response/ object with the specified data and HTTP response code.
[292] Fix | Delete
*
[293] Fix | Delete
* @param array|null $data The data to return in this response
[294] Fix | Delete
* @param int $code The HTTP response code.
[295] Fix | Delete
* Optional.
[296] Fix | Delete
* Defaults to 200 (OK).
[297] Fix | Delete
*
[298] Fix | Delete
* @return WP_REST_Response The WordPress response object loaded with the data and the response code.
[299] Fix | Delete
*/
[300] Fix | Delete
public function respond_with_code( ?array $data = null, int $code = 200 ): WP_REST_Response {
[301] Fix | Delete
return new WP_REST_Response( $data, $code );
[302] Fix | Delete
}
[303] Fix | Delete
[304] Fix | Delete
/**
[305] Fix | Delete
* get_user_from_request
[306] Fix | Delete
*
[307] Fix | Delete
* Returns the current user object.
[308] Fix | Delete
* Depends on the property /current_user_id/ to be set.
[309] Fix | Delete
* @return WP_User|false The user object or false if not found or on error.
[310] Fix | Delete
*/
[311] Fix | Delete
public function get_user_from_request() {
[312] Fix | Delete
return get_user_by( 'id', $this->current_user_id );
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
/**
[316] Fix | Delete
* get_arguments
[317] Fix | Delete
* Rest Endpoint extra arguments
[318] Fix | Delete
* @return array Additional arguments for the route configuration
[319] Fix | Delete
*/
[320] Fix | Delete
public function get_arguments(): array {
[321] Fix | Delete
return [];
[322] Fix | Delete
}
[323] Fix | Delete
[324] Fix | Delete
/**
[325] Fix | Delete
* get_endpoint
[326] Fix | Delete
* Rest route Endpoint
[327] Fix | Delete
* @return string Endpoint uri component (comes after the route namespace)
[328] Fix | Delete
*/
[329] Fix | Delete
abstract public function get_endpoint(): string;
[330] Fix | Delete
[331] Fix | Delete
/**
[332] Fix | Delete
* get_name
[333] Fix | Delete
* @return string The name of the route
[334] Fix | Delete
*/
[335] Fix | Delete
abstract public function get_name(): string;
[336] Fix | Delete
[337] Fix | Delete
/**
[338] Fix | Delete
* get_self_url
[339] Fix | Delete
*
[340] Fix | Delete
* @param string $endpoint
[341] Fix | Delete
*
[342] Fix | Delete
* @return string
[343] Fix | Delete
*/
[344] Fix | Delete
public function get_self_url( string $endpoint = '' ): string {
[345] Fix | Delete
return rest_url( $this->namespace . '/' . $endpoint );
[346] Fix | Delete
}
[347] Fix | Delete
[348] Fix | Delete
public function respond_success_json( $data = [] ): WP_REST_Response {
[349] Fix | Delete
return new WP_REST_Response([
[350] Fix | Delete
'success' => true,
[351] Fix | Delete
'data' => $data,
[352] Fix | Delete
]);
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
/**
[356] Fix | Delete
* @param array{message: string, code: string} $data
[357] Fix | Delete
*
[358] Fix | Delete
* @return WP_Error
[359] Fix | Delete
*/
[360] Fix | Delete
public function respond_error_json( array $data ): WP_Error {
[361] Fix | Delete
if ( ! isset( $data['message'] ) || ! isset( $data['code'] ) ) {
[362] Fix | Delete
_doing_it_wrong(
[363] Fix | Delete
__FUNCTION__,
[364] Fix | Delete
esc_html__( 'Both `message` and `code` keys must be provided', 'image-optimization' ),
[365] Fix | Delete
'1.0.0'
[366] Fix | Delete
); // @codeCoverageIgnore
[367] Fix | Delete
}
[368] Fix | Delete
[369] Fix | Delete
return new WP_Error(
[370] Fix | Delete
$data['code'] ?? 'internal_server_error',
[371] Fix | Delete
$data['message'] ?? esc_html__( 'Internal server error', 'image-optimization' ),
[372] Fix | Delete
);
[373] Fix | Delete
}
[374] Fix | Delete
[375] Fix | Delete
public function verify_nonce( $nonce = '', $name = '' ) {
[376] Fix | Delete
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $nonce ) ), $name ) ) {
[377] Fix | Delete
return $this->respond_error_json([
[378] Fix | Delete
'message' => esc_html__( 'Invalid nonce', 'image-optimization' ),
[379] Fix | Delete
'code' => 'bad_request',
[380] Fix | Delete
]);
[381] Fix | Delete
}
[382] Fix | Delete
}
[383] Fix | Delete
[384] Fix | Delete
public function verify_capability( $capability = 'manage_options' ) {
[385] Fix | Delete
if ( ! current_user_can( $capability ) ) {
[386] Fix | Delete
return $this->respond_error_json([
[387] Fix | Delete
'message' => esc_html__( 'You do not have sufficient permissions to access this data.', 'image-optimization' ),
[388] Fix | Delete
'code' => 'bad_request',
[389] Fix | Delete
]);
[390] Fix | Delete
}
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
public function verify_nonce_and_capability( $nonce = '', $name = '', $capability = 'manage_options' ) {
[394] Fix | Delete
$valid = $this->verify_nonce( $nonce, $name );
[395] Fix | Delete
[396] Fix | Delete
if ( is_wp_error( $valid ) ) {
[397] Fix | Delete
return $valid;
[398] Fix | Delete
}
[399] Fix | Delete
[400] Fix | Delete
if ( ! current_user_can( $capability ) ) {
[401] Fix | Delete
return $this->respond_error_json([
[402] Fix | Delete
'message' => esc_html__( 'You do not have sufficient permissions to access this data.', 'image-optimization' ),
[403] Fix | Delete
'code' => 'bad_request',
[404] Fix | Delete
]);
[405] Fix | Delete
}
[406] Fix | Delete
}
[407] Fix | Delete
[408] Fix | Delete
public function trigger_internal( $method, $endpoint, $args = [] ): array {
[409] Fix | Delete
$request = new WP_REST_Request( $method, $this->get_self_url( $endpoint ) );
[410] Fix | Delete
if ( ! empty( $args['body'] ) ) {
[411] Fix | Delete
$request->set_body_params( $args['body'] );
[412] Fix | Delete
}
[413] Fix | Delete
if ( ! empty( $args['headers'] ) ) {
[414] Fix | Delete
$request->set_headers( $args['headers'] );
[415] Fix | Delete
}
[416] Fix | Delete
if ( ! empty( $args['params'] ) ) {
[417] Fix | Delete
$request->set_query_params( $args['params'] );
[418] Fix | Delete
}
[419] Fix | Delete
[420] Fix | Delete
$response = rest_do_request( $request );
[421] Fix | Delete
$server = rest_get_server();
[422] Fix | Delete
return $server->response_to_data( $response, false );
[423] Fix | Delete
}
[424] Fix | Delete
}
[425] Fix | Delete
[426] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function