2016-12-08 18:59:07 +00:00
|
|
|
<?php
|
2018-03-06 12:07:33 +00:00
|
|
|
/**
|
|
|
|
* WP_CLI_Rest_Command class file.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Cli
|
|
|
|
*/
|
2017-05-23 14:40:19 +00:00
|
|
|
|
2020-01-29 05:21:29 +00:00
|
|
|
use Automattic\Jetpack\Constants;
|
|
|
|
|
2017-05-23 14:40:19 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:59:07 +00:00
|
|
|
/**
|
|
|
|
* Main Command for WooCommere CLI.
|
|
|
|
*
|
|
|
|
* Since a lot of WC operations can be handled via the REST API, we base our CLI
|
|
|
|
* off of Restful to generate commands for each WooCommerce REST API endpoint
|
|
|
|
* so most of the logic is shared.
|
|
|
|
*
|
2016-12-08 21:40:12 +00:00
|
|
|
* Forked from wp-cli/restful (by Daniel Bachhuber, released under the MIT license https://opensource.org/licenses/MIT).
|
|
|
|
* https://github.com/wp-cli/restful
|
2016-12-08 18:59:07 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @version 3.0.0
|
2016-12-08 18:59:07 +00:00
|
|
|
* @package WooCommerce
|
|
|
|
*/
|
|
|
|
class WC_CLI_REST_Command {
|
|
|
|
/**
|
|
|
|
* Endpoints that have a parent ID.
|
|
|
|
* Ex: Product reviews, which has a product ID and a review ID.
|
2018-03-06 12:07:33 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
protected $routes_with_parent_id = array(
|
|
|
|
'customer_download',
|
|
|
|
'product_review',
|
|
|
|
'order_note',
|
|
|
|
'shop_order_refund',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of command/endpoint object.
|
2018-03-06 12:07:33 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
private $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoint route.
|
2018-03-06 12:07:33 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
private $route;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main resource ID.
|
2018-03-06 12:07:33 +00:00
|
|
|
*
|
|
|
|
* @var int
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
private $resource_identifier;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schema for command.
|
2018-03-06 12:07:33 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
private $schema;
|
|
|
|
|
2017-02-09 18:29:24 +00:00
|
|
|
/**
|
|
|
|
* List of supported IDs and their description (name => desc).
|
2018-03-06 12:07:33 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2017-02-09 18:29:24 +00:00
|
|
|
*/
|
|
|
|
private $supported_ids = array();
|
|
|
|
|
2016-12-08 18:59:07 +00:00
|
|
|
/**
|
|
|
|
* Sets up REST Command.
|
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param string $name Name of endpoint object (comes from schema).
|
|
|
|
* @param string $route Path to route of this endpoint.
|
|
|
|
* @param array $schema Schema object.
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
public function __construct( $name, $route, $schema ) {
|
2018-03-05 19:46:54 +00:00
|
|
|
$this->name = $name;
|
2017-05-15 09:54:06 +00:00
|
|
|
|
|
|
|
preg_match_all( '#\([^\)]+\)#', $route, $matches );
|
2016-12-08 18:59:07 +00:00
|
|
|
$first_match = $matches[0];
|
|
|
|
$resource_id = ! empty( $matches[0] ) ? array_pop( $matches[0] ) : null;
|
|
|
|
$this->route = rtrim( $route );
|
|
|
|
$this->schema = $schema;
|
|
|
|
|
|
|
|
$this->resource_identifier = $resource_id;
|
2018-03-06 12:07:33 +00:00
|
|
|
if ( in_array( $name, $this->routes_with_parent_id, true ) ) {
|
2016-12-15 17:11:08 +00:00
|
|
|
$is_singular = substr( $this->route, - strlen( $resource_id ) ) === $resource_id;
|
2016-12-08 18:59:07 +00:00
|
|
|
if ( ! $is_singular ) {
|
|
|
|
$this->resource_identifier = $first_match[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 18:29:24 +00:00
|
|
|
/**
|
|
|
|
* Passes supported ID arguments (things like product_id, order_id, etc) that we should look for in addition to id.
|
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param array $supported_ids List of supported IDs.
|
2017-02-09 18:29:24 +00:00
|
|
|
*/
|
|
|
|
public function set_supported_ids( $supported_ids = array() ) {
|
|
|
|
$this->supported_ids = $supported_ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-17 10:10:52 +00:00
|
|
|
* Returns an ID of supported ID arguments (things like product_id, order_id, etc) that we should look for in addition to id.
|
2017-02-09 18:29:24 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_supported_ids() {
|
|
|
|
return $this->supported_ids;
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:59:07 +00:00
|
|
|
/**
|
|
|
|
* Create a new item.
|
|
|
|
*
|
|
|
|
* @subcommand create
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param array $args WP-CLI positional arguments.
|
|
|
|
* @param array $assoc_args WP-CLI associative arguments.
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
public function create_item( $args, $assoc_args ) {
|
2018-03-05 19:46:54 +00:00
|
|
|
$assoc_args = self::decode_json( $assoc_args );
|
2016-12-08 18:59:07 +00:00
|
|
|
list( $status, $body ) = $this->do_request( 'POST', $this->get_filled_route( $args ), $assoc_args );
|
|
|
|
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
|
|
|
|
WP_CLI::line( $body['id'] );
|
|
|
|
} else {
|
|
|
|
WP_CLI::success( "Created {$this->name} {$body['id']}." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an existing item.
|
|
|
|
*
|
|
|
|
* @subcommand delete
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param array $args WP-CLI positional arguments.
|
|
|
|
* @param array $assoc_args WP-CLI associative arguments.
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
public function delete_item( $args, $assoc_args ) {
|
|
|
|
list( $status, $body ) = $this->do_request( 'DELETE', $this->get_filled_route( $args ), $assoc_args );
|
|
|
|
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
|
|
|
|
WP_CLI::line( $body['id'] );
|
|
|
|
} else {
|
|
|
|
if ( empty( $assoc_args['force'] ) ) {
|
|
|
|
WP_CLI::success( __( 'Trashed', 'woocommerce' ) . " {$this->name} {$body['id']}" );
|
|
|
|
} else {
|
|
|
|
WP_CLI::success( __( 'Deleted', 'woocommerce' ) . " {$this->name} {$body['id']}." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a single item.
|
|
|
|
*
|
|
|
|
* @subcommand get
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param array $args WP-CLI positional arguments.
|
|
|
|
* @param array $assoc_args WP-CLI associative arguments.
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
public function get_item( $args, $assoc_args ) {
|
2018-03-05 19:46:54 +00:00
|
|
|
$route = $this->get_filled_route( $args );
|
2016-12-08 18:59:07 +00:00
|
|
|
list( $status, $body, $headers ) = $this->do_request( 'GET', $route, $assoc_args );
|
|
|
|
|
|
|
|
if ( ! empty( $assoc_args['fields'] ) ) {
|
2017-03-03 16:55:22 +00:00
|
|
|
$body = self::limit_item_to_fields( $body, $assoc_args['fields'] );
|
2016-12-08 18:59:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 04:41:41 +00:00
|
|
|
if ( empty( $assoc_args['format'] ) ) {
|
2018-01-11 00:32:40 +00:00
|
|
|
$assoc_args['format'] = 'table';
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:59:07 +00:00
|
|
|
if ( 'headers' === $assoc_args['format'] ) {
|
2018-03-06 11:18:31 +00:00
|
|
|
echo wp_json_encode( $headers );
|
2016-12-08 18:59:07 +00:00
|
|
|
} elseif ( 'body' === $assoc_args['format'] ) {
|
2018-03-06 11:18:31 +00:00
|
|
|
echo wp_json_encode( $body );
|
2016-12-08 18:59:07 +00:00
|
|
|
} elseif ( 'envelope' === $assoc_args['format'] ) {
|
2018-03-06 11:18:31 +00:00
|
|
|
echo wp_json_encode(
|
2018-03-05 19:46:54 +00:00
|
|
|
array(
|
|
|
|
'body' => $body,
|
|
|
|
'headers' => $headers,
|
|
|
|
'status' => $status,
|
|
|
|
)
|
|
|
|
);
|
2016-12-08 18:59:07 +00:00
|
|
|
} else {
|
|
|
|
$formatter = $this->get_formatter( $assoc_args );
|
|
|
|
$formatter->display_item( $body );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List all items.
|
|
|
|
*
|
|
|
|
* @subcommand list
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param array $args WP-CLI positional arguments.
|
|
|
|
* @param array $assoc_args WP-CLI associative arguments.
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
public function list_items( $args, $assoc_args ) {
|
|
|
|
if ( ! empty( $assoc_args['format'] ) && 'count' === $assoc_args['format'] ) {
|
|
|
|
$method = 'HEAD';
|
|
|
|
} else {
|
|
|
|
$method = 'GET';
|
|
|
|
}
|
|
|
|
|
2018-03-28 08:18:10 +00:00
|
|
|
if ( ! isset( $assoc_args['per_page'] ) || empty( $assoc_args['per_page'] ) ) {
|
|
|
|
$assoc_args['per_page'] = '100';
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:59:07 +00:00
|
|
|
list( $status, $body, $headers ) = $this->do_request( $method, $this->get_filled_route( $args ), $assoc_args );
|
|
|
|
if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
|
|
|
|
$items = array_column( $body, 'id' );
|
|
|
|
} else {
|
|
|
|
$items = $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $assoc_args['fields'] ) ) {
|
|
|
|
foreach ( $items as $key => $item ) {
|
2017-03-03 16:55:22 +00:00
|
|
|
$items[ $key ] = self::limit_item_to_fields( $item, $assoc_args['fields'] );
|
2016-12-08 18:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 15:02:54 +00:00
|
|
|
if ( empty( $assoc_args['format'] ) ) {
|
|
|
|
$assoc_args['format'] = 'table';
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:59:07 +00:00
|
|
|
if ( ! empty( $assoc_args['format'] ) && 'count' === $assoc_args['format'] ) {
|
|
|
|
echo (int) $headers['X-WP-Total'];
|
|
|
|
} elseif ( 'headers' === $assoc_args['format'] ) {
|
2018-03-06 11:18:31 +00:00
|
|
|
echo wp_json_encode( $headers );
|
2016-12-08 18:59:07 +00:00
|
|
|
} elseif ( 'body' === $assoc_args['format'] ) {
|
2018-03-06 11:18:31 +00:00
|
|
|
echo wp_json_encode( $body );
|
2016-12-08 18:59:07 +00:00
|
|
|
} elseif ( 'envelope' === $assoc_args['format'] ) {
|
2018-03-06 11:18:31 +00:00
|
|
|
echo wp_json_encode(
|
2018-03-05 19:46:54 +00:00
|
|
|
array(
|
|
|
|
'body' => $body,
|
|
|
|
'headers' => $headers,
|
|
|
|
'status' => $status,
|
|
|
|
'api_url' => $this->api_url,
|
|
|
|
)
|
|
|
|
);
|
2016-12-08 18:59:07 +00:00
|
|
|
} else {
|
|
|
|
$formatter = $this->get_formatter( $assoc_args );
|
|
|
|
$formatter->display_items( $items );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing item.
|
|
|
|
*
|
|
|
|
* @subcommand update
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param array $args WP-CLI positional arguments.
|
|
|
|
* @param array $assoc_args WP-CLI associative arguments.
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
public function update_item( $args, $assoc_args ) {
|
2018-03-05 19:46:54 +00:00
|
|
|
$assoc_args = self::decode_json( $assoc_args );
|
2016-12-08 18:59:07 +00:00
|
|
|
list( $status, $body ) = $this->do_request( 'POST', $this->get_filled_route( $args ), $assoc_args );
|
|
|
|
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
|
|
|
|
WP_CLI::line( $body['id'] );
|
|
|
|
} else {
|
|
|
|
WP_CLI::success( __( 'Updated', 'woocommerce' ) . " {$this->name} {$body['id']}." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do a REST Request
|
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param string $method Request method. Examples: 'POST', 'PUT', 'DELETE' or 'GET'.
|
|
|
|
* @param string $route Resource route.
|
|
|
|
* @param array $assoc_args Associative arguments passed to the originating WP-CLI command.
|
2016-12-08 18:59:07 +00:00
|
|
|
*
|
2017-05-12 08:48:46 +00:00
|
|
|
* @return array
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
private function do_request( $method, $route, $assoc_args ) {
|
2017-09-05 19:52:39 +00:00
|
|
|
wc_maybe_define_constant( 'REST_REQUEST', true );
|
|
|
|
|
2016-12-08 18:59:07 +00:00
|
|
|
$request = new WP_REST_Request( $method, $route );
|
2018-03-06 12:07:33 +00:00
|
|
|
if ( in_array( $method, array( 'POST', 'PUT' ), true ) ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$request->set_body_params( $assoc_args );
|
|
|
|
} else {
|
2016-12-15 17:11:08 +00:00
|
|
|
foreach ( $assoc_args as $key => $value ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$request->set_param( $key, $value );
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 05:21:29 +00:00
|
|
|
if ( Constants::is_true( 'SAVEQUERIES' ) ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$original_queries = is_array( $GLOBALS['wpdb']->queries ) ? array_keys( $GLOBALS['wpdb']->queries ) : array();
|
|
|
|
}
|
|
|
|
$response = rest_do_request( $request );
|
2020-01-29 05:21:29 +00:00
|
|
|
if ( Constants::is_true( 'SAVEQUERIES' ) ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$performed_queries = array();
|
2016-12-15 17:11:08 +00:00
|
|
|
foreach ( (array) $GLOBALS['wpdb']->queries as $key => $query ) {
|
2018-03-06 12:07:33 +00:00
|
|
|
if ( in_array( $key, $original_queries, true ) ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$performed_queries[] = $query;
|
|
|
|
}
|
2018-03-05 19:46:54 +00:00
|
|
|
usort(
|
2019-12-20 18:25:23 +00:00
|
|
|
$performed_queries,
|
|
|
|
function( $a, $b ) {
|
2018-03-05 19:46:54 +00:00
|
|
|
if ( $a[1] === $b[1] ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ( $a[1] > $b[1] ) ? -1 : 1;
|
2016-12-08 18:59:07 +00:00
|
|
|
}
|
2018-03-05 19:46:54 +00:00
|
|
|
);
|
2016-12-08 18:59:07 +00:00
|
|
|
|
2018-03-05 19:46:54 +00:00
|
|
|
$query_count = count( $performed_queries );
|
2016-12-08 18:59:07 +00:00
|
|
|
$query_total_time = 0;
|
2016-12-15 17:11:08 +00:00
|
|
|
foreach ( $performed_queries as $query ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$query_total_time += $query[1];
|
|
|
|
}
|
|
|
|
$slow_query_message = '';
|
|
|
|
if ( $performed_queries && 'wc' === WP_CLI::get_config( 'debug' ) ) {
|
|
|
|
$slow_query_message .= '. Ordered by slowness, the queries are:' . PHP_EOL;
|
2016-12-15 17:11:08 +00:00
|
|
|
foreach ( $performed_queries as $i => $query ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$i++;
|
2018-03-05 19:46:54 +00:00
|
|
|
$bits = explode( ', ', $query[2] );
|
|
|
|
$backtrace = implode( ', ', array_slice( $bits, 13 ) );
|
|
|
|
$seconds = round( $query[1], 6 );
|
2016-12-08 18:59:07 +00:00
|
|
|
$slow_query_message .= <<<EOT
|
|
|
|
{$i}:
|
|
|
|
- {$seconds} seconds
|
|
|
|
- {$backtrace}
|
|
|
|
- {$query[0]}
|
|
|
|
EOT;
|
|
|
|
$slow_query_message .= PHP_EOL;
|
|
|
|
}
|
|
|
|
} elseif ( 'wc' !== WP_CLI::get_config( 'debug' ) ) {
|
|
|
|
$slow_query_message = '. Use --debug=wc to see all queries.';
|
|
|
|
}
|
|
|
|
$query_total_time = round( $query_total_time, 6 );
|
|
|
|
WP_CLI::debug( "wc command executed {$query_count} queries in {$query_total_time} seconds{$slow_query_message}", 'wc' );
|
|
|
|
}
|
2017-03-06 19:34:06 +00:00
|
|
|
|
2018-03-06 12:07:33 +00:00
|
|
|
$error = $response->as_error();
|
|
|
|
|
|
|
|
if ( $error ) {
|
2017-03-06 19:34:06 +00:00
|
|
|
// For authentication errors (status 401), include a reminder to set the --user flag.
|
|
|
|
// WP_CLI::error will only return the first message from WP_Error, so we will pass a string containing both instead.
|
|
|
|
if ( 401 === $response->get_status() ) {
|
|
|
|
$errors = $error->get_error_messages();
|
|
|
|
$errors[] = __( 'Make sure to include the --user flag with an account that has permissions for this action.', 'woocommerce' ) . ' {"status":401}';
|
|
|
|
$error = implode( "\n", $errors );
|
|
|
|
}
|
2016-12-08 18:59:07 +00:00
|
|
|
WP_CLI::error( $error );
|
|
|
|
}
|
|
|
|
return array( $response->get_status(), $response->get_data(), $response->get_headers() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Formatter object based on supplied parameters.
|
|
|
|
*
|
|
|
|
* @param array $assoc_args Parameters passed to command. Determines formatting.
|
|
|
|
* @return \WP_CLI\Formatter
|
|
|
|
*/
|
|
|
|
protected function get_formatter( &$assoc_args ) {
|
|
|
|
if ( ! empty( $assoc_args['fields'] ) ) {
|
|
|
|
if ( is_string( $assoc_args['fields'] ) ) {
|
|
|
|
$fields = explode( ',', $assoc_args['fields'] );
|
|
|
|
} else {
|
|
|
|
$fields = $assoc_args['fields'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( ! empty( $assoc_args['context'] ) ) {
|
|
|
|
$fields = $this->get_context_fields( $assoc_args['context'] );
|
|
|
|
} else {
|
|
|
|
$fields = $this->get_context_fields( 'view' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new \WP_CLI\Formatter( $assoc_args, $fields );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of fields present in a given context
|
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param string $context Scope under which the request is made. Determines fields present in response.
|
2016-12-08 18:59:07 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function get_context_fields( $context ) {
|
|
|
|
$fields = array();
|
2016-12-15 17:11:08 +00:00
|
|
|
foreach ( $this->schema['properties'] as $key => $args ) {
|
2018-03-06 12:07:33 +00:00
|
|
|
if ( empty( $args['context'] ) || in_array( $context, $args['context'], true ) ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$fields[] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the route for this resource
|
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param array $args Positional arguments passed to the originating WP-CLI command.
|
2016-12-08 18:59:07 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function get_filled_route( $args = array() ) {
|
2017-03-01 21:22:02 +00:00
|
|
|
$supported_id_matched = false;
|
|
|
|
$route = $this->route;
|
2017-02-09 18:29:24 +00:00
|
|
|
|
|
|
|
foreach ( $this->get_supported_ids() as $id_name => $id_desc ) {
|
2018-02-25 19:18:22 +00:00
|
|
|
if ( 'id' !== $id_name && strpos( $route, '<' . $id_name . '>' ) !== false && ! empty( $args ) ) {
|
2017-03-01 21:22:02 +00:00
|
|
|
$route = str_replace( '(?P<' . $id_name . '>[\d]+)', $args[0], $route );
|
|
|
|
$supported_id_matched = true;
|
2017-02-09 18:29:24 +00:00
|
|
|
}
|
2016-12-08 18:59:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 21:22:02 +00:00
|
|
|
if ( ! empty( $args ) ) {
|
|
|
|
$id_replacement = $supported_id_matched && ! empty( $args[1] ) ? $args[1] : $args[0];
|
|
|
|
$route = str_replace( array( '(?P<id>[\d]+)', '(?P<id>[\w-]+)' ), $id_replacement, $route );
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:59:07 +00:00
|
|
|
return rtrim( $route );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduce an item to specific fields.
|
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param array $item Item to reduce.
|
|
|
|
* @param array $fields Fields to keep.
|
2016-12-08 18:59:07 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function limit_item_to_fields( $item, $fields ) {
|
|
|
|
if ( empty( $fields ) ) {
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
if ( is_string( $fields ) ) {
|
|
|
|
$fields = explode( ',', $fields );
|
|
|
|
}
|
|
|
|
foreach ( $item as $i => $field ) {
|
2018-03-06 12:07:33 +00:00
|
|
|
if ( ! in_array( $i, $fields, true ) ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
unset( $item[ $i ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JSON can be passed in some more complicated objects, like the payment gateway settings array.
|
|
|
|
* This function decodes the json (if present) and tries to get it's value.
|
|
|
|
*
|
2018-03-06 12:07:33 +00:00
|
|
|
* @param array $arr Array that will be scanned for JSON encoded values.
|
2017-05-12 08:48:46 +00:00
|
|
|
*
|
|
|
|
* @return array
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
protected function decode_json( $arr ) {
|
|
|
|
foreach ( $arr as $key => $value ) {
|
|
|
|
if ( '[' === substr( $value, 0, 1 ) || '{' === substr( $value, 0, 1 ) ) {
|
|
|
|
$arr[ $key ] = json_decode( $value, true );
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|