Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/ReceiptR...
File: ReceiptRenderingRestController.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Automattic\WooCommerce\Internal\ReceiptRendering;
[2] Fix | Delete
[3] Fix | Delete
use Automattic\WooCommerce\Internal\TransientFiles\TransientFilesEngine;
[4] Fix | Delete
use \WP_REST_Server;
[5] Fix | Delete
use \WP_REST_Request;
[6] Fix | Delete
use \WP_Error;
[7] Fix | Delete
use Automattic\WooCommerce\Internal\RestApiControllerBase;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Controller for the REST endpoints associated to the receipt rendering engine.
[11] Fix | Delete
* The endpoints require the read_shop_order capability for the order at hand.
[12] Fix | Delete
*/
[13] Fix | Delete
class ReceiptRenderingRestController extends RestApiControllerBase {
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Get the WooCommerce REST API namespace for the class.
[17] Fix | Delete
*
[18] Fix | Delete
* @return string
[19] Fix | Delete
*/
[20] Fix | Delete
protected function get_rest_api_namespace(): string {
[21] Fix | Delete
return 'order-receipts';
[22] Fix | Delete
}
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* Register the REST API endpoints handled by this controller.
[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
'/orders/(?P<id>[\d]+)/receipt',
[31] Fix | Delete
array(
[32] Fix | Delete
array(
[33] Fix | Delete
'methods' => WP_REST_Server::CREATABLE,
[34] Fix | Delete
'callback' => fn( $request ) => $this->run( $request, 'create_order_receipt' ),
[35] Fix | Delete
'permission_callback' => fn( $request ) => $this->check_permission( $request, 'read_shop_order', $request->get_param( 'id' ) ),
[36] Fix | Delete
'args' => $this->get_args_for_create_order_receipt(),
[37] Fix | Delete
'schema' => $this->get_schema_for_get_and_post_order_receipt(),
[38] Fix | Delete
),
[39] Fix | Delete
)
[40] Fix | Delete
);
[41] Fix | Delete
[42] Fix | Delete
register_rest_route(
[43] Fix | Delete
$this->route_namespace,
[44] Fix | Delete
'/orders/(?P<id>[\d]+)/receipt',
[45] Fix | Delete
array(
[46] Fix | Delete
array(
[47] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[48] Fix | Delete
'callback' => fn( $request ) => $this->run( $request, 'get_order_receipt' ),
[49] Fix | Delete
'permission_callback' => fn( $request ) => $this->check_permission( $request, 'read_shop_order', $request->get_param( 'id' ) ),
[50] Fix | Delete
'args' => $this->get_args_for_get_order_receipt(),
[51] Fix | Delete
'schema' => $this->get_schema_for_get_and_post_order_receipt(),
[52] Fix | Delete
),
[53] Fix | Delete
)
[54] Fix | Delete
);
[55] Fix | Delete
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Handle the GET /orders/id/receipt:
[60] Fix | Delete
*
[61] Fix | Delete
* Return the data for a receipt if it exists, or a 404 error if it doesn't.
[62] Fix | Delete
*
[63] Fix | Delete
* @param WP_REST_Request $request The received request.
[64] Fix | Delete
* @return array|WP_Error
[65] Fix | Delete
*/
[66] Fix | Delete
public function get_order_receipt( WP_REST_Request $request ) {
[67] Fix | Delete
$order_id = $request->get_param( 'id' );
[68] Fix | Delete
$filename = wc_get_container()->get( ReceiptRenderingEngine::class )->get_existing_receipt( $order_id );
[69] Fix | Delete
[70] Fix | Delete
return is_null( $filename ) ?
[71] Fix | Delete
new WP_Error( 'woocommerce_rest_not_found', __( 'Receipt not found', 'woocommerce' ), array( 'status' => 404 ) ) :
[72] Fix | Delete
$this->get_response_for_file( $filename );
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Handle the POST /orders/id/receipt:
[77] Fix | Delete
*
[78] Fix | Delete
* Return the data for a receipt if it exists, or create a new receipt and return its data otherwise.
[79] Fix | Delete
*
[80] Fix | Delete
* Optional query string arguments:
[81] Fix | Delete
*
[82] Fix | Delete
* expiration_date: formatted as yyyy-mm-dd.
[83] Fix | Delete
* expiration_days: a number, 0 is today, 1 is tomorrow, etc.
[84] Fix | Delete
* force_new: defaults to false, if true, create a new receipt even if one already exists for the order.
[85] Fix | Delete
*
[86] Fix | Delete
* If neither expiration_date nor expiration_days are supplied, the default is expiration_days = 1.
[87] Fix | Delete
*
[88] Fix | Delete
* @param WP_REST_Request $request The received request.
[89] Fix | Delete
* @return array|WP_Error Request response or an error.
[90] Fix | Delete
*/
[91] Fix | Delete
public function create_order_receipt( WP_REST_Request $request ) {
[92] Fix | Delete
$expiration_date =
[93] Fix | Delete
$request->get_param( 'expiration_date' ) ??
[94] Fix | Delete
gmdate( 'Y-m-d', strtotime( "+{$request->get_param('expiration_days')} days" ) );
[95] Fix | Delete
[96] Fix | Delete
$order_id = $request->get_param( 'id' );
[97] Fix | Delete
[98] Fix | Delete
$filename = wc_get_container()->get( ReceiptRenderingEngine::class )->generate_receipt( $order_id, $expiration_date, $request->get_param( 'force_new' ) );
[99] Fix | Delete
[100] Fix | Delete
return is_null( $filename ) ?
[101] Fix | Delete
new WP_Error( 'woocommerce_rest_not_found', __( 'Order not found', 'woocommerce' ), array( 'status' => 404 ) ) :
[102] Fix | Delete
$this->get_response_for_file( $filename );
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Formats the response for both the GET and POST endpoints.
[107] Fix | Delete
*
[108] Fix | Delete
* @param string $filename The filename to return the information for.
[109] Fix | Delete
* @return array The data for the actual response to be returned.
[110] Fix | Delete
*/
[111] Fix | Delete
private function get_response_for_file( string $filename ): array {
[112] Fix | Delete
$expiration_date = TransientFilesEngine::get_expiration_date( $filename );
[113] Fix | Delete
$public_url = wc_get_container()->get( TransientFilesEngine::class )->get_public_url( $filename );
[114] Fix | Delete
[115] Fix | Delete
return array(
[116] Fix | Delete
'receipt_url' => $public_url,
[117] Fix | Delete
'expiration_date' => $expiration_date,
[118] Fix | Delete
);
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
/**
[122] Fix | Delete
* Get the accepted arguments for the GET request.
[123] Fix | Delete
*
[124] Fix | Delete
* @return array[] The accepted arguments for the GET request.
[125] Fix | Delete
*/
[126] Fix | Delete
private function get_args_for_get_order_receipt(): array {
[127] Fix | Delete
return array(
[128] Fix | Delete
'id' => array(
[129] Fix | Delete
'description' => __( 'Unique identifier of the order.', 'woocommerce' ),
[130] Fix | Delete
'type' => 'integer',
[131] Fix | Delete
'context' => array( 'view', 'edit' ),
[132] Fix | Delete
'readonly' => true,
[133] Fix | Delete
),
[134] Fix | Delete
);
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Get the schema for both the GET and the POST requests.
[139] Fix | Delete
*
[140] Fix | Delete
* @return array[]
[141] Fix | Delete
*/
[142] Fix | Delete
private function get_schema_for_get_and_post_order_receipt(): array {
[143] Fix | Delete
$schema = $this->get_base_schema();
[144] Fix | Delete
$schema['properties'] = array(
[145] Fix | Delete
'receipt_url' => array(
[146] Fix | Delete
'description' => __( 'Public url of the receipt.', 'woocommerce' ),
[147] Fix | Delete
'type' => 'string',
[148] Fix | Delete
'context' => array( 'view', 'edit' ),
[149] Fix | Delete
'readonly' => true,
[150] Fix | Delete
),
[151] Fix | Delete
'expiration_date' => array(
[152] Fix | Delete
'description' => __( 'Expiration date of the receipt, formatted as yyyy-mm-dd.', 'woocommerce' ),
[153] Fix | Delete
'type' => 'string',
[154] Fix | Delete
'context' => array( 'view', 'edit' ),
[155] Fix | Delete
'readonly' => true,
[156] Fix | Delete
),
[157] Fix | Delete
);
[158] Fix | Delete
[159] Fix | Delete
return $schema;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/**
[163] Fix | Delete
* Get the accepted arguments for the POST request.
[164] Fix | Delete
*
[165] Fix | Delete
* @return array[]
[166] Fix | Delete
*/
[167] Fix | Delete
private function get_args_for_create_order_receipt(): array {
[168] Fix | Delete
return array(
[169] Fix | Delete
'id' => array(
[170] Fix | Delete
'description' => __( 'Unique identifier of the order.', 'woocommerce' ),
[171] Fix | Delete
'type' => 'integer',
[172] Fix | Delete
'context' => array( 'view', 'edit' ),
[173] Fix | Delete
'readonly' => true,
[174] Fix | Delete
),
[175] Fix | Delete
'expiration_date' => array(
[176] Fix | Delete
'description' => __( 'Expiration date formatted as yyyy-mm-dd.', 'woocommerce' ),
[177] Fix | Delete
'type' => 'string',
[178] Fix | Delete
'context' => array( 'view', 'edit' ),
[179] Fix | Delete
'readonly' => true,
[180] Fix | Delete
'default' => null,
[181] Fix | Delete
),
[182] Fix | Delete
'expiration_days' => array(
[183] Fix | Delete
'description' => __( 'Number of days to be added to the current date to get the expiration date.', 'woocommerce' ),
[184] Fix | Delete
'type' => 'integer',
[185] Fix | Delete
'context' => array( 'view', 'edit' ),
[186] Fix | Delete
'readonly' => true,
[187] Fix | Delete
'default' => 1,
[188] Fix | Delete
),
[189] Fix | Delete
'force_new' => array(
[190] Fix | Delete
'description' => __( 'True to force the creation of a new receipt even if one already exists and has not expired yet.', 'woocommerce' ),
[191] Fix | Delete
'type' => 'boolean',
[192] Fix | Delete
'required' => false,
[193] Fix | Delete
'context' => array( 'view', 'edit' ),
[194] Fix | Delete
'readonly' => true,
[195] Fix | Delete
'default' => false,
[196] Fix | Delete
),
[197] Fix | Delete
);
[198] Fix | Delete
}
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function