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