Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal
File: RestApiControllerBase.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Automattic\WooCommerce\Internal;
[2] Fix | Delete
[3] Fix | Delete
use Automattic\WooCommerce\Internal\RegisterHooksInterface;
[4] Fix | Delete
use Automattic\WooCommerce\Utilities\StringUtil;
[5] Fix | Delete
use WP_HTTP_Response;
[6] Fix | Delete
use WP_REST_Request;
[7] Fix | Delete
use WP_REST_Response;
[8] Fix | Delete
use WP_Error;
[9] Fix | Delete
use InvalidArgumentException;
[10] Fix | Delete
use Exception;
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Base class for REST API controllers defined inside the 'src' directory.
[14] Fix | Delete
*
[15] Fix | Delete
* The following must be added at the end of the 'init_hooks' method in the 'WooCommerce' class,
[16] Fix | Delete
* otherwise the routes won't be registered:
[17] Fix | Delete
* $container->get( <full class name>::class )->register();
[18] Fix | Delete
*
[19] Fix | Delete
* Minimal controller example:
[20] Fix | Delete
*
[21] Fix | Delete
* class FoobarsController extends RestApiControllerBase {
[22] Fix | Delete
*
[23] Fix | Delete
* protected function get_rest_api_namespace(): string {
[24] Fix | Delete
* return 'foobars';
[25] Fix | Delete
* }
[26] Fix | Delete
*
[27] Fix | Delete
* public function register_routes() {
[28] Fix | Delete
* register_rest_route(
[29] Fix | Delete
* $this->route_namespace,
[30] Fix | Delete
* '/foobars/(?P<id>[\d]+)',
[31] Fix | Delete
* array(
[32] Fix | Delete
* array(
[33] Fix | Delete
* 'methods' => \WP_REST_Server::READABLE,
[34] Fix | Delete
* 'callback' => fn( $request ) => $this->run( $request, 'get_foobar' ),
[35] Fix | Delete
* 'permission_callback' => fn( $request ) => $this->check_permission( $request, 'read_foobars', $request->get_param( 'id' ) ),
[36] Fix | Delete
* 'args' => $this->get_args_for_get_foobar(),
[37] Fix | Delete
* 'schema' => $this->get_schema_for_get_foobar(),
[38] Fix | Delete
* ),
[39] Fix | Delete
* )
[40] Fix | Delete
* );
[41] Fix | Delete
* }
[42] Fix | Delete
*
[43] Fix | Delete
* protected function get_foobar( \WP_REST_Request $request ) {
[44] Fix | Delete
* return array( 'message' => 'Get foobar with id ' . $request->get_param(' id' ) );
[45] Fix | Delete
* }
[46] Fix | Delete
*
[47] Fix | Delete
* private function get_args_for_get_foobar(): array {
[48] Fix | Delete
* return array(
[49] Fix | Delete
* 'id' => array(
[50] Fix | Delete
* 'description' => __( 'Unique identifier of the foobar.', 'woocommerce' ),
[51] Fix | Delete
* 'type' => 'integer',
[52] Fix | Delete
* 'context' => array( 'view', 'edit' ),
[53] Fix | Delete
* 'readonly' => true,
[54] Fix | Delete
* ),
[55] Fix | Delete
* );
[56] Fix | Delete
* }
[57] Fix | Delete
*
[58] Fix | Delete
* private function get_schema_for_get_foobar(): array {
[59] Fix | Delete
* $schema = $this->get_base_schema();
[60] Fix | Delete
* $schema['properties'] = array(
[61] Fix | Delete
* 'message' => array(
[62] Fix | Delete
* 'description' => __( 'A message.', 'woocommerce' ),
[63] Fix | Delete
* 'type' => 'string',
[64] Fix | Delete
* 'context' => array( 'view', 'edit' ),
[65] Fix | Delete
* 'readonly' => true,
[66] Fix | Delete
* ),
[67] Fix | Delete
* );
[68] Fix | Delete
* return $schema;
[69] Fix | Delete
* }
[70] Fix | Delete
*
[71] Fix | Delete
* }
[72] Fix | Delete
*/
[73] Fix | Delete
abstract class RestApiControllerBase implements RegisterHooksInterface {
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* The root namespace for the JSON REST API endpoints.
[77] Fix | Delete
*
[78] Fix | Delete
* @var string
[79] Fix | Delete
*/
[80] Fix | Delete
protected string $route_namespace = 'wc/v3';
[81] Fix | Delete
[82] Fix | Delete
/**
[83] Fix | Delete
* Register the hooks used by the class.
[84] Fix | Delete
*/
[85] Fix | Delete
public function register() {
[86] Fix | Delete
add_filter( 'woocommerce_rest_api_get_rest_namespaces', array( $this, 'handle_woocommerce_rest_api_get_rest_namespaces' ) );
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
/**
[90] Fix | Delete
* Handle the woocommerce_rest_api_get_rest_namespaces filter
[91] Fix | Delete
* to add ourselves to the list of REST API controllers registered by WooCommerce.
[92] Fix | Delete
*
[93] Fix | Delete
* @param array $namespaces The original list of WooCommerce REST API namespaces/controllers.
[94] Fix | Delete
* @return array The updated list of WooCommerce REST API namespaces/controllers.
[95] Fix | Delete
*
[96] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[97] Fix | Delete
*/
[98] Fix | Delete
public function handle_woocommerce_rest_api_get_rest_namespaces( array $namespaces ): array {
[99] Fix | Delete
$namespaces['wc/v3'][ $this->get_rest_api_namespace() ] = static::class;
[100] Fix | Delete
return $namespaces;
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* Get the WooCommerce REST API namespace for the class. It must be unique across all other derived classes
[105] Fix | Delete
* and the keys returned by the 'get_vX_controllers' methods in includes/rest-api/Server.php.
[106] Fix | Delete
* Note that this value is NOT related to the route namespace.
[107] Fix | Delete
*
[108] Fix | Delete
* @return string
[109] Fix | Delete
*/
[110] Fix | Delete
abstract protected function get_rest_api_namespace(): string;
[111] Fix | Delete
[112] Fix | Delete
/**
[113] Fix | Delete
* Register the REST API endpoints handled by this controller.
[114] Fix | Delete
*
[115] Fix | Delete
* Use 'register_rest_route' in the usual way, it's recommended to use the 'run' method for 'callback'
[116] Fix | Delete
* and the 'check_permission' method for 'permission_check', see the example in the class comment.
[117] Fix | Delete
*/
[118] Fix | Delete
abstract public function register_routes();
[119] Fix | Delete
[120] Fix | Delete
/**
[121] Fix | Delete
* Handle a request for one of the provided REST API endpoints.
[122] Fix | Delete
*
[123] Fix | Delete
* If an exception is thrown, the exception message will be returned as part of the response
[124] Fix | Delete
* if the user has the 'manage_woocommerce' capability.
[125] Fix | Delete
*
[126] Fix | Delete
* Note that the method specified in $method_name must have a 'protected' visibility and accept one argument of type 'WP_REST_Request'.
[127] Fix | Delete
*
[128] Fix | Delete
* @param WP_REST_Request $request The incoming HTTP REST request.
[129] Fix | Delete
* @param string $method_name The name of the class method to execute. It must be protected and accept one argument of type 'WP_REST_Request'.
[130] Fix | Delete
* @return WP_Error|WP_HTTP_Response|WP_REST_Response The response to send back to the client.
[131] Fix | Delete
*/
[132] Fix | Delete
protected function run( WP_REST_Request $request, string $method_name ) {
[133] Fix | Delete
try {
[134] Fix | Delete
return rest_ensure_response( $this->$method_name( $request ) );
[135] Fix | Delete
} catch ( InvalidArgumentException $ex ) {
[136] Fix | Delete
$message = $ex->getMessage();
[137] Fix | Delete
return new WP_Error( 'woocommerce_rest_invalid_argument', $message ? $message : __( 'Internal server error', 'woocommerce' ), array( 'status' => 400 ) );
[138] Fix | Delete
} catch ( Exception $ex ) {
[139] Fix | Delete
wc_get_logger()->error( StringUtil::class_name_without_namespace( static::class ) . ": when executing method $method_name: {$ex->getMessage()}" );
[140] Fix | Delete
return $this->internal_wp_error( $ex );
[141] Fix | Delete
}
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
/**
[145] Fix | Delete
* Return an WP_Error object for an internal server error, with exception information if the current user is an admin.
[146] Fix | Delete
*
[147] Fix | Delete
* @param Exception $exception The exception to maybe include information from.
[148] Fix | Delete
* @return WP_Error
[149] Fix | Delete
*/
[150] Fix | Delete
protected function internal_wp_error( Exception $exception ): WP_Error {
[151] Fix | Delete
$data = array( 'status' => 500 );
[152] Fix | Delete
if ( current_user_can( 'manage_woocommerce' ) ) {
[153] Fix | Delete
$data['exception_class'] = get_class( $exception );
[154] Fix | Delete
$data['exception_message'] = $exception->getMessage();
[155] Fix | Delete
$data['exception_trace'] = (array) $exception->getTrace();
[156] Fix | Delete
}
[157] Fix | Delete
$data['exception_message'] = $exception->getMessage();
[158] Fix | Delete
[159] Fix | Delete
return new WP_Error( 'woocommerce_rest_internal_error', __( 'Internal server error', 'woocommerce' ), $data );
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/**
[163] Fix | Delete
* Returns an authentication error message for a given HTTP verb.
[164] Fix | Delete
*
[165] Fix | Delete
* @param string $method HTTP method.
[166] Fix | Delete
* @return array|null Error information on success, null otherwise.
[167] Fix | Delete
*/
[168] Fix | Delete
protected function get_authentication_error_by_method( string $method ) {
[169] Fix | Delete
$errors = array(
[170] Fix | Delete
'GET' => array(
[171] Fix | Delete
'code' => 'woocommerce_rest_cannot_view',
[172] Fix | Delete
'message' => __( 'Sorry, you cannot view resources.', 'woocommerce' ),
[173] Fix | Delete
),
[174] Fix | Delete
'POST' => array(
[175] Fix | Delete
'code' => 'woocommerce_rest_cannot_create',
[176] Fix | Delete
'message' => __( 'Sorry, you cannot create resources.', 'woocommerce' ),
[177] Fix | Delete
),
[178] Fix | Delete
'DELETE' => array(
[179] Fix | Delete
'code' => 'woocommerce_rest_cannot_delete',
[180] Fix | Delete
'message' => __( 'Sorry, you cannot delete resources.', 'woocommerce' ),
[181] Fix | Delete
),
[182] Fix | Delete
);
[183] Fix | Delete
[184] Fix | Delete
return $errors[ $method ] ?? null;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* Permission check for REST API endpoints, given the request method.
[189] Fix | Delete
*
[190] Fix | Delete
* @param WP_REST_Request $request The request for which the permission is checked.
[191] Fix | Delete
* @param string $required_capability_name The name of the required capability.
[192] Fix | Delete
* @param mixed ...$extra_args Extra arguments to be used for the permission check.
[193] Fix | Delete
* @return bool|WP_Error True if the current user has the capability, otherwise an "Unauthorized" error or False if no error is available for the request method.
[194] Fix | Delete
*/
[195] Fix | Delete
protected function check_permission( WP_REST_Request $request, string $required_capability_name, ...$extra_args ) {
[196] Fix | Delete
if ( current_user_can( $required_capability_name, ...$extra_args ) ) {
[197] Fix | Delete
return true;
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
$error_information = $this->get_authentication_error_by_method( $request->get_method() );
[201] Fix | Delete
if ( is_null( $error_information ) ) {
[202] Fix | Delete
return false;
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
return new WP_Error(
[206] Fix | Delete
$error_information['code'],
[207] Fix | Delete
$error_information['message'],
[208] Fix | Delete
array( 'status' => rest_authorization_required_code() )
[209] Fix | Delete
);
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
/**
[213] Fix | Delete
* Get the base schema for the REST API endpoints.
[214] Fix | Delete
*
[215] Fix | Delete
* @return array
[216] Fix | Delete
*/
[217] Fix | Delete
protected function get_base_schema(): array {
[218] Fix | Delete
return array(
[219] Fix | Delete
'$schema' => 'http://json-schema.org/draft-04/schema#',
[220] Fix | Delete
'title' => 'order receipts',
[221] Fix | Delete
'type' => 'object',
[222] Fix | Delete
);
[223] Fix | Delete
}
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function