Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/StoreApi/Schemas
File: ExtendSchema.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\StoreApi\Schemas;
[1] Fix | Delete
[2] Fix | Delete
use Automattic\WooCommerce\StoreApi\Schemas\V1\CartItemSchema;
[3] Fix | Delete
use Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema;
[4] Fix | Delete
use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema;
[5] Fix | Delete
use Automattic\WooCommerce\StoreApi\Schemas\V1\ProductSchema;
[6] Fix | Delete
use Automattic\WooCommerce\StoreApi\Formatters;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Provides utility functions to extend Store API schemas.
[10] Fix | Delete
*
[11] Fix | Delete
* Note there are also helpers that map to these methods.
[12] Fix | Delete
*
[13] Fix | Delete
* @see woocommerce_store_api_register_endpoint_data()
[14] Fix | Delete
* @see woocommerce_store_api_register_update_callback()
[15] Fix | Delete
* @see woocommerce_store_api_register_payment_requirements()
[16] Fix | Delete
* @see woocommerce_store_api_get_formatter()
[17] Fix | Delete
*/
[18] Fix | Delete
final class ExtendSchema {
[19] Fix | Delete
/**
[20] Fix | Delete
* List of Store API schema that is allowed to be extended by extensions.
[21] Fix | Delete
*
[22] Fix | Delete
* @var string[]
[23] Fix | Delete
*/
[24] Fix | Delete
private $endpoints = [
[25] Fix | Delete
CartItemSchema::IDENTIFIER,
[26] Fix | Delete
CartSchema::IDENTIFIER,
[27] Fix | Delete
CheckoutSchema::IDENTIFIER,
[28] Fix | Delete
ProductSchema::IDENTIFIER,
[29] Fix | Delete
];
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Holds the formatters class instance.
[33] Fix | Delete
*
[34] Fix | Delete
* @var Formatters
[35] Fix | Delete
*/
[36] Fix | Delete
private $formatters;
[37] Fix | Delete
[38] Fix | Delete
/**
[39] Fix | Delete
* Data to be extended
[40] Fix | Delete
*
[41] Fix | Delete
* @var array
[42] Fix | Delete
*/
[43] Fix | Delete
private $extend_data = [];
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Data to be extended
[47] Fix | Delete
*
[48] Fix | Delete
* @var array
[49] Fix | Delete
*/
[50] Fix | Delete
private $callback_methods = [];
[51] Fix | Delete
[52] Fix | Delete
/**
[53] Fix | Delete
* Array of payment requirements
[54] Fix | Delete
*
[55] Fix | Delete
* @var array
[56] Fix | Delete
*/
[57] Fix | Delete
private $payment_requirements = [];
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* Constructor
[61] Fix | Delete
*
[62] Fix | Delete
* @param Formatters $formatters An instance of the formatters class.
[63] Fix | Delete
*/
[64] Fix | Delete
public function __construct( Formatters $formatters ) {
[65] Fix | Delete
$this->formatters = $formatters;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Register endpoint data under a specified namespace
[70] Fix | Delete
*
[71] Fix | Delete
* @param array $args {
[72] Fix | Delete
* An array of elements that make up a post to update or insert.
[73] Fix | Delete
*
[74] Fix | Delete
* @type string $endpoint Required. The endpoint to extend.
[75] Fix | Delete
* @type string $namespace Required. Plugin namespace.
[76] Fix | Delete
* @type callable $schema_callback Callback executed to add schema data.
[77] Fix | Delete
* @type callable $data_callback Callback executed to add endpoint data.
[78] Fix | Delete
* @type string $schema_type The type of data, object or array.
[79] Fix | Delete
* }
[80] Fix | Delete
*
[81] Fix | Delete
* @throws \Exception On failure to register.
[82] Fix | Delete
*/
[83] Fix | Delete
public function register_endpoint_data( $args ) {
[84] Fix | Delete
$args = wp_parse_args(
[85] Fix | Delete
$args,
[86] Fix | Delete
[
[87] Fix | Delete
'endpoint' => '',
[88] Fix | Delete
'namespace' => '',
[89] Fix | Delete
'schema_callback' => null,
[90] Fix | Delete
'data_callback' => null,
[91] Fix | Delete
'schema_type' => ARRAY_A,
[92] Fix | Delete
]
[93] Fix | Delete
);
[94] Fix | Delete
[95] Fix | Delete
if ( ! is_string( $args['namespace'] ) || empty( $args['namespace'] ) ) {
[96] Fix | Delete
$this->throw_exception( 'You must provide a plugin namespace when extending a Store REST endpoint.' );
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
if ( ! in_array( $args['endpoint'], $this->endpoints, true ) ) {
[100] Fix | Delete
$this->throw_exception(
[101] Fix | Delete
sprintf( 'You must provide a valid Store REST endpoint to extend, valid endpoints are: %1$s. You provided %2$s.', implode( ', ', $this->endpoints ), $args['endpoint'] )
[102] Fix | Delete
);
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
if ( ! is_null( $args['schema_callback'] ) && ! is_callable( $args['schema_callback'] ) ) {
[106] Fix | Delete
$this->throw_exception( '$schema_callback must be a callable function.' );
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
if ( ! is_null( $args['data_callback'] ) && ! is_callable( $args['data_callback'] ) ) {
[110] Fix | Delete
$this->throw_exception( '$data_callback must be a callable function.' );
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
if ( ! in_array( $args['schema_type'], [ ARRAY_N, ARRAY_A ], true ) ) {
[114] Fix | Delete
$this->throw_exception(
[115] Fix | Delete
sprintf( 'Data type must be either ARRAY_N for a numeric array or ARRAY_A for an object like array. You provided %1$s.', $args['schema_type'] )
[116] Fix | Delete
);
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
$this->extend_data[ $args['endpoint'] ][ $args['namespace'] ] = [
[120] Fix | Delete
'schema_callback' => $args['schema_callback'],
[121] Fix | Delete
'data_callback' => $args['data_callback'],
[122] Fix | Delete
'schema_type' => $args['schema_type'],
[123] Fix | Delete
];
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
/**
[127] Fix | Delete
* Add callback functions that can be executed by the cart/extensions endpoint.
[128] Fix | Delete
*
[129] Fix | Delete
* @param array $args {
[130] Fix | Delete
* An array of elements that make up the callback configuration.
[131] Fix | Delete
*
[132] Fix | Delete
* @type string $namespace Required. Plugin namespace.
[133] Fix | Delete
* @type callable $callback Required. The function/callable to execute.
[134] Fix | Delete
* }
[135] Fix | Delete
*
[136] Fix | Delete
* @throws \Exception On failure to register.
[137] Fix | Delete
*/
[138] Fix | Delete
public function register_update_callback( $args ) {
[139] Fix | Delete
$args = wp_parse_args(
[140] Fix | Delete
$args,
[141] Fix | Delete
[
[142] Fix | Delete
'namespace' => '',
[143] Fix | Delete
'callback' => null,
[144] Fix | Delete
]
[145] Fix | Delete
);
[146] Fix | Delete
[147] Fix | Delete
if ( ! is_string( $args['namespace'] ) || empty( $args['namespace'] ) ) {
[148] Fix | Delete
throw new \Exception( 'You must provide a plugin namespace when extending a Store REST endpoint.' );
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
if ( ! is_callable( $args['callback'] ) ) {
[152] Fix | Delete
throw new \Exception( 'There is no valid callback supplied to register_update_callback.' );
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
$this->callback_methods[ $args['namespace'] ] = $args;
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
/**
[159] Fix | Delete
* Registers and validates payment requirements callbacks.
[160] Fix | Delete
*
[161] Fix | Delete
* @param array $args {
[162] Fix | Delete
* Array of registration data.
[163] Fix | Delete
*
[164] Fix | Delete
* @type callable $data_callback Required. Callback executed to add payment requirements data.
[165] Fix | Delete
* }
[166] Fix | Delete
*
[167] Fix | Delete
* @throws \Exception On failure to register.
[168] Fix | Delete
*/
[169] Fix | Delete
public function register_payment_requirements( $args ) {
[170] Fix | Delete
if ( empty( $args['data_callback'] ) || ! is_callable( $args['data_callback'] ) ) {
[171] Fix | Delete
$this->throw_exception( '$data_callback must be a callable function.' );
[172] Fix | Delete
}
[173] Fix | Delete
$this->payment_requirements[] = $args['data_callback'];
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
/**
[177] Fix | Delete
* Returns a formatter instance.
[178] Fix | Delete
*
[179] Fix | Delete
* @param string $name Formatter name.
[180] Fix | Delete
* @return FormatterInterface
[181] Fix | Delete
*/
[182] Fix | Delete
public function get_formatter( $name ) {
[183] Fix | Delete
return $this->formatters->$name;
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
/**
[187] Fix | Delete
* Get callback for a specific endpoint and namespace.
[188] Fix | Delete
*
[189] Fix | Delete
* @param string $namespace The namespace to get callbacks for.
[190] Fix | Delete
*
[191] Fix | Delete
* @return callable The callback registered by the extension.
[192] Fix | Delete
* @throws \Exception When callback is not callable or parameters are incorrect.
[193] Fix | Delete
*/
[194] Fix | Delete
public function get_update_callback( $namespace ) {
[195] Fix | Delete
if ( ! is_string( $namespace ) ) {
[196] Fix | Delete
throw new \Exception( 'You must provide a plugin namespace when extending a Store REST endpoint.' );
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
if ( ! array_key_exists( $namespace, $this->callback_methods ) ) {
[200] Fix | Delete
throw new \Exception( sprintf( 'There is no such namespace registered: %1$s.', $namespace ) );
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
if ( ! array_key_exists( 'callback', $this->callback_methods[ $namespace ] ) || ! is_callable( $this->callback_methods[ $namespace ]['callback'] ) ) {
[204] Fix | Delete
throw new \Exception( sprintf( 'There is no valid callback registered for: %1$s.', $namespace ) );
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
return $this->callback_methods[ $namespace ]['callback'];
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
/**
[211] Fix | Delete
* Returns the registered endpoint data
[212] Fix | Delete
*
[213] Fix | Delete
* @param string $endpoint A valid identifier.
[214] Fix | Delete
* @param array $passed_args Passed arguments from the Schema class.
[215] Fix | Delete
* @return object Returns an casted object with registered endpoint data.
[216] Fix | Delete
* @throws \Exception If a registered callback throws an error, or silently logs it.
[217] Fix | Delete
*/
[218] Fix | Delete
public function get_endpoint_data( $endpoint, array $passed_args = [] ) {
[219] Fix | Delete
$registered_data = [];
[220] Fix | Delete
[221] Fix | Delete
if ( isset( $this->extend_data[ $endpoint ] ) ) {
[222] Fix | Delete
foreach ( $this->extend_data[ $endpoint ] as $namespace => $callbacks ) {
[223] Fix | Delete
if ( is_null( $callbacks['data_callback'] ) ) {
[224] Fix | Delete
continue;
[225] Fix | Delete
}
[226] Fix | Delete
try {
[227] Fix | Delete
$data = $callbacks['data_callback']( ...$passed_args );
[228] Fix | Delete
[229] Fix | Delete
if ( ! is_array( $data ) ) {
[230] Fix | Delete
$data = [];
[231] Fix | Delete
throw new \Exception( '$data_callback must return an array.' );
[232] Fix | Delete
}
[233] Fix | Delete
} catch ( \Throwable $e ) {
[234] Fix | Delete
$this->throw_exception( $e );
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
$registered_data[ $namespace ] = $data;
[238] Fix | Delete
}
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
return (object) $registered_data;
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
/**
[245] Fix | Delete
* Returns the registered endpoint schema
[246] Fix | Delete
*
[247] Fix | Delete
* @param string $endpoint A valid identifier.
[248] Fix | Delete
* @param array $passed_args Passed arguments from the Schema class.
[249] Fix | Delete
* @return object Returns an array with registered schema data.
[250] Fix | Delete
* @throws \Exception If a registered callback throws an error, or silently logs it.
[251] Fix | Delete
*/
[252] Fix | Delete
public function get_endpoint_schema( $endpoint, array $passed_args = [] ) {
[253] Fix | Delete
$registered_schema = [];
[254] Fix | Delete
[255] Fix | Delete
if ( isset( $this->extend_data[ $endpoint ] ) ) {
[256] Fix | Delete
foreach ( $this->extend_data[ $endpoint ] as $namespace => $callbacks ) {
[257] Fix | Delete
if ( is_null( $callbacks['schema_callback'] ) ) {
[258] Fix | Delete
continue;
[259] Fix | Delete
}
[260] Fix | Delete
try {
[261] Fix | Delete
$schema = $callbacks['schema_callback']( ...$passed_args );
[262] Fix | Delete
[263] Fix | Delete
if ( ! is_array( $schema ) ) {
[264] Fix | Delete
$schema = [];
[265] Fix | Delete
throw new \Exception( '$schema_callback must return an array.' );
[266] Fix | Delete
}
[267] Fix | Delete
} catch ( \Throwable $e ) {
[268] Fix | Delete
$this->throw_exception( $e );
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
$registered_schema[ $namespace ] = $this->format_extensions_properties( $namespace, $schema, $callbacks['schema_type'] );
[272] Fix | Delete
}
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
return (object) $registered_schema;
[276] Fix | Delete
}
[277] Fix | Delete
[278] Fix | Delete
/**
[279] Fix | Delete
* Returns the additional payment requirements for the cart which are required to make payments. Values listed here
[280] Fix | Delete
* are compared against each Payment Gateways "supports" flag.
[281] Fix | Delete
*
[282] Fix | Delete
* @param array $requirements list of requirements that should be added to the collected requirements.
[283] Fix | Delete
* @return array Returns a list of payment requirements.
[284] Fix | Delete
* @throws \Exception If a registered callback throws an error, or silently logs it.
[285] Fix | Delete
*/
[286] Fix | Delete
public function get_payment_requirements( array $requirements = [ 'products' ] ) {
[287] Fix | Delete
if ( ! empty( $this->payment_requirements ) ) {
[288] Fix | Delete
foreach ( $this->payment_requirements as $callback ) {
[289] Fix | Delete
try {
[290] Fix | Delete
$data = $callback();
[291] Fix | Delete
[292] Fix | Delete
if ( ! is_array( $data ) ) {
[293] Fix | Delete
throw new \Exception( '$data_callback must return an array.' );
[294] Fix | Delete
}
[295] Fix | Delete
[296] Fix | Delete
$requirements = array_unique( array_merge( $requirements, $data ) );
[297] Fix | Delete
} catch ( \Throwable $e ) {
[298] Fix | Delete
$this->throw_exception( $e );
[299] Fix | Delete
}
[300] Fix | Delete
}
[301] Fix | Delete
}
[302] Fix | Delete
return $requirements;
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
/**
[306] Fix | Delete
* Throws error and/or silently logs it.
[307] Fix | Delete
*
[308] Fix | Delete
* @param string|\Throwable $exception_or_error Error message or \Exception.
[309] Fix | Delete
* @throws \Exception An error to throw if we have debug enabled and user is admin.
[310] Fix | Delete
*/
[311] Fix | Delete
private function throw_exception( $exception_or_error ) {
[312] Fix | Delete
$exception = is_string( $exception_or_error ) ? new \Exception( $exception_or_error ) : $exception_or_error;
[313] Fix | Delete
[314] Fix | Delete
wc_caught_exception( $exception );
[315] Fix | Delete
[316] Fix | Delete
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && current_user_can( 'manage_woocommerce' ) ) {
[317] Fix | Delete
throw $exception;
[318] Fix | Delete
}
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
/**
[322] Fix | Delete
* Format schema for an extension.
[323] Fix | Delete
*
[324] Fix | Delete
* @param string $namespace Error message or \Exception.
[325] Fix | Delete
* @param array $schema An error to throw if we have debug enabled and user is admin.
[326] Fix | Delete
* @param string $schema_type How should data be shaped.
[327] Fix | Delete
* @return array Formatted schema.
[328] Fix | Delete
*/
[329] Fix | Delete
private function format_extensions_properties( $namespace, $schema, $schema_type ) {
[330] Fix | Delete
if ( ARRAY_N === $schema_type ) {
[331] Fix | Delete
return [
[332] Fix | Delete
/* translators: %s: extension namespace */
[333] Fix | Delete
'description' => sprintf( __( 'Extension data registered by %s', 'woocommerce' ), $namespace ),
[334] Fix | Delete
'type' => [ 'array', 'null' ],
[335] Fix | Delete
'context' => [ 'view', 'edit' ],
[336] Fix | Delete
'items' => $schema,
[337] Fix | Delete
];
[338] Fix | Delete
}
[339] Fix | Delete
return [
[340] Fix | Delete
/* translators: %s: extension namespace */
[341] Fix | Delete
'description' => sprintf( __( 'Extension data registered by %s', 'woocommerce' ), $namespace ),
[342] Fix | Delete
'type' => [ 'object', 'null' ],
[343] Fix | Delete
'context' => [ 'view', 'edit' ],
[344] Fix | Delete
'properties' => $schema,
[345] Fix | Delete
];
[346] Fix | Delete
}
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function