Use new code style

This commit is contained in:
David Stone
2025-02-07 19:02:33 -07:00
parent 0181024ae1
commit 8433379d90
672 changed files with 37107 additions and 45249 deletions

View File

@ -9,11 +9,11 @@
namespace WP_Ultimo\API;
use \WP_Ultimo\Checkout\Cart;
use \WP_Ultimo\Database\Sites\Site_Type;
use \WP_Ultimo\Database\Payments\Payment_Status;
use \WP_Ultimo\Database\Memberships\Membership_Status;
use \WP_Ultimo\Objects\Billing_Address;
use WP_Ultimo\Checkout\Cart;
use WP_Ultimo\Database\Sites\Site_Type;
use WP_Ultimo\Database\Payments\Payment_Status;
use WP_Ultimo\Database\Memberships\Membership_Status;
use WP_Ultimo\Objects\Billing_Address;
// Exit if accessed directly
defined('ABSPATH') || exit;
@ -36,8 +36,7 @@ class Register_Endpoint {
public function init() {
add_action('wu_register_rest_routes', array($this, 'register_route'));
} // end init;
}
/**
* Adds a new route to the wu namespace, for the register endpoint.
@ -51,20 +50,27 @@ class Register_Endpoint {
$namespace = $api->get_namespace();
register_rest_route($namespace, '/register', array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'handle_get'),
'permission_callback' => \Closure::fromCallable([$api, 'check_authorization']),
));
register_rest_route(
$namespace,
'/register',
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'handle_get'),
'permission_callback' => \Closure::fromCallable(array($api, 'check_authorization')),
)
);
register_rest_route($namespace, '/register', array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array($this, 'handle_endpoint'),
'permission_callback' => \Closure::fromCallable([$api, 'check_authorization']),
'args' => $this->get_rest_args(),
));
} // end register_route;
register_rest_route(
$namespace,
'/register',
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array($this, 'handle_endpoint'),
'permission_callback' => \Closure::fromCallable(array($api, 'check_authorization')),
'args' => $this->get_rest_args(),
)
);
}
/**
* Handle the register endpoint get for zapier integration reasons.
@ -79,8 +85,7 @@ class Register_Endpoint {
return array(
'registration_status' => wu_get_setting('enable_registration', true) ? 'open' : 'closed',
);
} // end handle_get;
}
/**
* Handle the register endpoint logic.
@ -97,60 +102,63 @@ class Register_Endpoint {
$params = json_decode($request->get_body(), true);
if (\WP_Ultimo\API::get_instance()->should_log_api_calls()) {
wu_log_add('api-calls', json_encode($params, JSON_PRETTY_PRINT));
} // end if;
}
$validation_errors = $this->validate($params);
if (is_wp_error($validation_errors)) {
$validation_errors->add_data(array(
'status' => 400,
));
$validation_errors->add_data(
array(
'status' => 400,
)
);
return $validation_errors;
} // end if;
}
$wpdb->query('START TRANSACTION');
try {
$customer = $this->maybe_create_customer($params);
if (is_wp_error($customer)) {
return $this->rollback_and_return($customer);
} // end if;
}
$customer->update_last_login(true, true);
$customer->add_note(array(
'text' => __('Created via REST API', 'wp-ultimo'),
'author_id' => $customer->get_user_id(),
));
$customer->add_note(
array(
'text' => __('Created via REST API', 'wp-ultimo'),
'author_id' => $customer->get_user_id(),
)
);
/*
* Payment Method defaults
*/
$payment_method = wp_parse_args(wu_get_isset($params, 'payment_method'), array(
'gateway' => '',
'gateway_customer_id' => '',
'gateway_subscription_id' => '',
'gateway_payment_id' => '',
));
$payment_method = wp_parse_args(
wu_get_isset($params, 'payment_method'),
array(
'gateway' => '',
'gateway_customer_id' => '',
'gateway_subscription_id' => '',
'gateway_payment_id' => '',
)
);
/*
* Cart params and creation
*/
$cart_params = $params;
$cart_params = wp_parse_args($cart_params, array(
'type' => 'new',
));
$cart_params = wp_parse_args(
$cart_params,
array(
'type' => 'new',
)
);
$cart = new Cart($cart_params);
@ -158,21 +166,33 @@ class Register_Endpoint {
* Validates if the cart is valid.
*/
if ($cart->is_valid() && count($cart->get_line_items()) === 0) {
return new \WP_Error('invalid_cart', __('Products are required.', 'wp-ultimo'), array_merge((array) $cart->done(), array(
'status' => 400,
)));
} // end if;
return new \WP_Error(
'invalid_cart',
__('Products are required.', 'wp-ultimo'),
array_merge(
(array) $cart->done(),
array(
'status' => 400,
)
)
);
}
/*
* Get Membership data
*/
$membership_data = $cart->to_membership_data();
$membership_data = array_merge($membership_data, wu_get_isset($params, 'membership', array(
'status' => Membership_Status::PENDING,
)));
$membership_data = array_merge(
$membership_data,
wu_get_isset(
$params,
'membership',
array(
'status' => Membership_Status::PENDING,
)
)
);
$membership_data['customer_id'] = $customer->get_id();
$membership_data['gateway'] = wu_get_isset($payment_method, 'gateway');
@ -190,21 +210,28 @@ class Register_Endpoint {
$membership = wu_create_membership($membership_data);
if (is_wp_error($membership)) {
return $this->rollback_and_return($membership);
}
} // end if;
$membership->add_note(array(
'text' => __('Created via REST API', 'wp-ultimo'),
'author_id' => $customer->get_user_id(),
));
$membership->add_note(
array(
'text' => __('Created via REST API', 'wp-ultimo'),
'author_id' => $customer->get_user_id(),
)
);
$payment_data = $cart->to_payment_data();
$payment_data = array_merge($payment_data, wu_get_isset($params, 'payment', array(
'status' => Payment_Status::PENDING,
)));
$payment_data = array_merge(
$payment_data,
wu_get_isset(
$params,
'payment',
array(
'status' => Payment_Status::PENDING,
)
)
);
/*
* Unset the status because we are going to transition it later.
@ -221,15 +248,15 @@ class Register_Endpoint {
$payment = wu_create_payment($payment_data);
if (is_wp_error($payment)) {
return $this->rollback_and_return($payment);
}
} // end if;
$payment->add_note(array(
'text' => __('Created via REST API', 'wp-ultimo'),
'author_id' => $customer->get_user_id(),
));
$payment->add_note(
array(
'text' => __('Created via REST API', 'wp-ultimo'),
'author_id' => $customer->get_user_id(),
)
);
$site = false;
@ -237,22 +264,17 @@ class Register_Endpoint {
* Site creation.
*/
if (wu_get_isset($params, 'site')) {
$site = $this->maybe_create_site($params, $membership);
if (is_wp_error($site)) {
return $this->rollback_and_return($site);
} // end if;
} // end if;
}
}
/*
* Deal with status changes.
*/
if ($membership_status !== $membership->get_status()) {
$membership->set_status($membership_status);
$membership->save();
@ -263,34 +285,24 @@ class Register_Endpoint {
* again, this time as a WU Site object.
*/
if ($site) {
$wp_site = get_site_by_path($site['domain'], $site['path']);
if ($wp_site) {
$site['id'] = $wp_site->blog_id;
} // end if;
} // end if;
} // end if;
}
}
}
if ($payment_status !== $payment->get_status()) {
$payment->set_status($payment_status);
$payment->save();
} // end if;
}
} catch (\Throwable $e) {
$wpdb->query('ROLLBACK');
return new \WP_Error('registration_error', $e->getMessage(), array('status' => 500));
} // end try;
}
$wpdb->query('COMMIT');
@ -303,8 +315,7 @@ class Register_Endpoint {
'payment' => $payment->to_array(),
'site' => $site ? $site : array('id' => 0),
);
} // end handle_endpoint;
}
/**
* Returns the list of arguments allowed on to the endpoint.
@ -351,7 +362,7 @@ class Register_Endpoint {
'billing_address' => array(
'type' => 'object',
'properties' => $billing_address_fields,
)
),
),
),
);
@ -521,39 +532,31 @@ class Register_Endpoint {
$args = array_merge($customer_args, $membership_args, $cart_args, $payment_args, $site_args);
return apply_filters('wu_rest_register_endpoint_args', $args, $this);
} // end get_rest_args;
/**
* Maybe create a customer, if needed.
*
* @since 2.0.0
*
* @param array $p The request parameters.
* @return \WP_Ultimo\Models\Customer|\WP_Error
*/
public function maybe_create_customer($p) {
}
/**
* Maybe create a customer, if needed.
*
* @since 2.0.0
*
* @param array $p The request parameters.
* @return \WP_Ultimo\Models\Customer|\WP_Error
*/
public function maybe_create_customer($p) {
$customer_id = wu_get_isset($p, 'customer_id');
if ($customer_id) {
$customer = wu_get_customer($customer_id);
if (!$customer) {
if ( ! $customer) {
return new \WP_Error('customer_not_found', __('The customer id sent does not correspond to a valid customer.', 'wp-ultimo'));
} // end if;
}
} else {
$customer = wu_create_customer($p['customer']);
} // end if;
}
return $customer;
} // end maybe_create_customer;
}
/**
* Undocumented function
@ -585,15 +588,14 @@ class Register_Endpoint {
* The get_sites method already includes pending sites,
* so we can safely rely on it.
*/
if (!empty($sites)) {
if ( ! empty($sites)) {
/*
* Returns the first site on that list.
* This is not ideal, but since we'll usually only have
* one site here, it's ok. for now.
*/
return current($sites);
} // end if;
}
$site_url = wu_get_isset($site_data, 'site_url');
@ -605,10 +607,8 @@ class Register_Endpoint {
$results = wpmu_validate_blog_signup($site_url, wu_get_isset($site_data, 'site_title'), $membership->get_customer()->get_user());
if ($results['errors']->has_errors()) {
return $results['errors'];
} // end if;
}
/*
* Get the transient data to save with the site
@ -640,22 +640,17 @@ class Register_Endpoint {
$site_data['id'] = 0;
if (wu_get_isset($site_data, 'publish')) {
$membership->publish_pending_site();
$wp_site = get_site_by_path($site_data['domain'], $site_data['path']);
if ($wp_site) {
$site_data['id'] = $wp_site->blog_id;
} // end if;
} // end if;
}
}
return $site_data;
} // end maybe_create_site;
}
/**
* Set the validation rules for this particular model.
@ -679,30 +674,26 @@ class Register_Endpoint {
'site.site_url' => 'required_with:site|alpha_num|min:4|lowercase|unique_site',
'site.site_title' => 'required_with:site|min:4',
);
}
/**
* Validates the rules and make sure we only save models when necessary.
*
* @since 2.0.0
* @param array $args The params to validate.
* @return mixed[]|\WP_Error
*/
public function validate($args) {
} // end validation_rules;
/**
* Validates the rules and make sure we only save models when necessary.
*
* @since 2.0.0
* @param array $args The params to validate.
* @return mixed[]|\WP_Error
*/
public function validate($args) {
$validator = new \WP_Ultimo\Helpers\Validator;
$validator = new \WP_Ultimo\Helpers\Validator();
$validator->validate($args, $this->validation_rules());
if ($validator->fails()) {
return $validator->get_errors();
} // end if;
}
return true;
} // end validate;
}
/**
* Rolls back database changes and returns the error passed.
@ -719,7 +710,5 @@ class Register_Endpoint {
$wpdb->query('ROLLBACK');
return $error;
} // end rollback_and_return;
} // end class Register_Endpoint;
}
}

View File

@ -45,9 +45,8 @@ trait Rest_Api {
*/
public function get_rest_base() {
return (!empty($this->rest_base)) ? $this->rest_base : $this->slug;
} // end get_rest_base;
return (! empty($this->rest_base)) ? $this->rest_base : $this->slug;
}
/**
* Registers the routes. Should be called by the entity
@ -60,14 +59,11 @@ trait Rest_Api {
$is_enabled = \WP_Ultimo\API::get_instance()->is_api_enabled();
if ($is_enabled) {
add_action('rest_api_init', array($this, 'register_routes_general'));
add_action('rest_api_init', array($this, 'register_routes_with_id'));
} // end if;
} // end enable_rest_api;
}
}
/**
* Register the endpoints that don't need an ID,
@ -80,7 +76,6 @@ trait Rest_Api {
$routes = array();
if (in_array('get_items', $this->enabled_rest_endpoints, true)) {
$routes = array(
array(
'methods' => \WP_REST_Server::READABLE,
@ -88,34 +83,28 @@ trait Rest_Api {
'permission_callback' => array($this, 'get_items_permissions_check'),
),
);
} // end if;
}
if (in_array('create_item', $this->enabled_rest_endpoints, true)) {
$routes[] = array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item_rest'),
'permission_callback' => array($this, 'create_item_permissions_check'),
'args' => $this->get_arguments_schema()
'args' => $this->get_arguments_schema(),
);
}
} // end if;
if (!empty($routes)) {
if ( ! empty($routes)) {
register_rest_route(
\WP_Ultimo\API::get_instance()->get_namespace(),
'/' . $this->get_rest_base(),
$routes,
true
);
} // end if;
}
do_action('wu_rest_register_routes_general', $routes, $this->get_rest_base(), 'create', $this);
} // end register_routes_general;
}
/**
* Register the endpoints that need an ID,
@ -128,95 +117,82 @@ trait Rest_Api {
$routes = array();
if (in_array('get_item', $this->enabled_rest_endpoints, true)) {
$routes[] = array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_item_rest'),
'permission_callback' => array($this, 'get_item_permissions_check'),
);
} // end if;
}
if (in_array('update_item', $this->enabled_rest_endpoints, true)) {
$routes[] = array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item_rest'),
'permission_callback' => array($this, 'update_item_permissions_check'),
'args' => $this->get_arguments_schema(true)
'args' => $this->get_arguments_schema(true),
);
} // end if;
}
if (in_array('delete_item', $this->enabled_rest_endpoints, true)) {
$routes[] = array(
'methods' => \WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item_rest'),
'permission_callback' => array($this, 'delete_item_permissions_check'),
);
}
} // end if;
if (!empty($routes)) {
if ( ! empty($routes)) {
register_rest_route(
\WP_Ultimo\API::get_instance()->get_namespace(),
'/' . $this->get_rest_base() . '/(?P<id>[\d]+)',
$routes,
true
);
} // end if;
}
do_action('wu_rest_register_routes_with_id', $routes, $this->get_rest_base(), 'update', $this);
} // end register_routes_with_id;
/**
* Returns a specific item.
*
* @since 2.0.0
*
* @param WP_REST_Request $request The request sent.
* @return \WP_REST_Response|\WP_Error
*/
public function get_item_rest($request) {
}
/**
* Returns a specific item.
*
* @since 2.0.0
*
* @param WP_REST_Request $request The request sent.
* @return \WP_REST_Response|\WP_Error
*/
public function get_item_rest($request) {
$item = $this->model_class::get_by_id($request['id']);
if (empty($item)) {
return new \WP_Error("wu_rest_{$this->slug}_invalid_id", __('Item not found.', 'wp-ultimo'), array('status' => 404));
} // end if;
}
return rest_ensure_response($item);
} // end get_item_rest;
/**
* Returns a list of items.
*
* @since 2.0.0
*
* @param WP_REST_Request $request The request sent.
* @return \WP_REST_Response|\WP_Error
*/
public function get_items_rest($request) {
}
/**
* Returns a list of items.
*
* @since 2.0.0
*
* @param WP_REST_Request $request The request sent.
* @return \WP_REST_Response|\WP_Error
*/
public function get_items_rest($request) {
$items = $this->model_class::query($request->get_params());
return rest_ensure_response($items);
} // end get_items_rest;
/**
* Creates an item.
*
* @since 2.0.0
*
* @param WP_REST_Request $request The request sent.
* @return \WP_REST_Response|\WP_Error
*/
public function create_item_rest($request) {
}
/**
* Creates an item.
*
* @since 2.0.0
*
* @param WP_REST_Request $request The request sent.
* @return \WP_REST_Response|\WP_Error
*/
public function create_item_rest($request) {
$body = json_decode($request->get_body(), true);
@ -225,53 +201,42 @@ trait Rest_Api {
$saver_function = "wu_create_{$model_name}";
if (function_exists($saver_function)) {
$item = call_user_func($saver_function, $body);
$saved = is_wp_error($item) ? $item : true;
} else {
$item = new $this->model_class($body);
$saved = $item->save();
} // end if;
}
if (is_wp_error($saved)) {
return rest_ensure_response($saved);
}
} // end if;
if (!$saved) {
if ( ! $saved) {
return new \WP_Error("wu_rest_{$this->slug}", __('Something went wrong (Code 1).', 'wp-ultimo'), array('status' => 400));
} // end if;
}
return rest_ensure_response($item);
} // end create_item_rest;
/**
* Updates an item.
*
* @since 2.0.0
*
* @param WP_REST_Request $request The request sent.
* @return \WP_REST_Response|\WP_Error
*/
public function update_item_rest($request) {
}
/**
* Updates an item.
*
* @since 2.0.0
*
* @param WP_REST_Request $request The request sent.
* @return \WP_REST_Response|\WP_Error
*/
public function update_item_rest($request) {
$id = wu_get_isset($request->get_url_params(), 'id');
$item = $this->model_class::get_by_id($id);
if (empty($item)) {
return new \WP_Error("wu_rest_{$this->slug}_invalid_id", __('Item not found.', 'wp-ultimo'), array('status' => 404));
} // end if;
}
$params = array_filter(
json_decode($request->get_body(), true),
@ -280,19 +245,13 @@ trait Rest_Api {
);
foreach ($params as $param => $value) {
$set_method = "set_{$param}";
if ($param === 'meta') {
$item->update_meta_batch($value);
} elseif (method_exists($item, $set_method)) {
call_user_func(array($item, $set_method), $value);
} else {
$error_message = sprintf(
/* translators: 1. Object class name; 2. Set method name */
__('The %1$s object does not have a %2$s method', 'wp-ultimo'),
@ -305,51 +264,41 @@ trait Rest_Api {
$error_message,
array('status' => 400)
);
} // end if;
} // end foreach;
}
}
$saved = $item->save();
if (is_wp_error($saved)) {
return rest_ensure_response($saved);
}
} // end if;
if (!$saved) {
if ( ! $saved) {
return new \WP_Error("wu_rest_{$this->slug}", __('Something went wrong (Code 2).', 'wp-ultimo'));
} // end if;
}
return rest_ensure_response($item);
} // end update_item_rest;
/**
* Deletes an item.
*
* @since 2.0.0
*
* @param WP_REST_Request $request The request sent.
* @return \WP_REST_Response|\WP_Error
*/
public function delete_item_rest($request) {
}
/**
* Deletes an item.
*
* @since 2.0.0
*
* @param WP_REST_Request $request The request sent.
* @return \WP_REST_Response|\WP_Error
*/
public function delete_item_rest($request) {
$item = $this->model_class::get_by_id($request['id']);
if (empty($item)) {
return new \WP_Error("wu_rest_{$this->slug}_invalid_id", __('Item not found.', 'wp-ultimo'), array('status' => 404));
} // end if;
}
$result = $item->delete();
return rest_ensure_response($result);
} // end delete_item_rest;
}
/**
* Check permissions to list items.
@ -361,11 +310,9 @@ trait Rest_Api {
*/
public function get_items_permissions_check($request) {
if (!\WP_Ultimo\API::get_instance()->check_authorization($request)) {
if ( ! \WP_Ultimo\API::get_instance()->check_authorization($request)) {
return false;
} // end if;
}
/**
* Filters if it is allowed to proceed with the request or not.
@ -377,8 +324,7 @@ trait Rest_Api {
* @param Base_Manager $this The object instance.
*/
return apply_filters('wu_rest_get_items', true, $this->get_rest_base(), $this);
} // end get_items_permissions_check;
}
/**
* Check permissions to create an item.
@ -390,11 +336,9 @@ trait Rest_Api {
*/
public function create_item_permissions_check($request) {
if (!\WP_Ultimo\API::get_instance()->check_authorization($request)) {
if ( ! \WP_Ultimo\API::get_instance()->check_authorization($request)) {
return false;
} // end if;
}
/**
* Filters if it is allowed to proceed with the request or not.
@ -406,8 +350,7 @@ trait Rest_Api {
* @param Base_Manager $this The object instance.
*/
return apply_filters('wu_rest_create_item', true, $this->get_rest_base(), $this);
} // end create_item_permissions_check;
}
/**
* Check permissions to get an item.
@ -419,11 +362,9 @@ trait Rest_Api {
*/
public function get_item_permissions_check($request) {
if (!\WP_Ultimo\API::get_instance()->check_authorization($request)) {
if ( ! \WP_Ultimo\API::get_instance()->check_authorization($request)) {
return false;
} // end if;
}
/**
* Filters if it is allowed to proceed with the request or not.
@ -435,8 +376,7 @@ trait Rest_Api {
* @param Base_Manager $this The object instance.
*/
return apply_filters('wu_rest_get_item', true, $this->get_rest_base(), $this);
} // end get_item_permissions_check;
}
/**
* Check permissions to update an item.
@ -448,11 +388,9 @@ trait Rest_Api {
*/
public function update_item_permissions_check($request) {
if (!\WP_Ultimo\API::get_instance()->check_authorization($request)) {
if ( ! \WP_Ultimo\API::get_instance()->check_authorization($request)) {
return false;
} // end if;
}
/**
* Filters if it is allowed to proceed with the request or not.
@ -464,8 +402,7 @@ trait Rest_Api {
* @param Base_Manager $this The object instance.
*/
return apply_filters('wu_rest_update_item', true, $this->get_rest_base(), $this);
} // end update_item_permissions_check;
}
/**
* Check permissions to delete an item.
@ -477,11 +414,9 @@ trait Rest_Api {
*/
public function delete_item_permissions_check($request) {
if (!\WP_Ultimo\API::get_instance()->check_authorization($request)) {
if ( ! \WP_Ultimo\API::get_instance()->check_authorization($request)) {
return false;
} // end if;
}
/**
* Filters if it is allowed to proceed with the request or not.
@ -493,8 +428,7 @@ trait Rest_Api {
* @param Base_Manager $this The object instance.
*/
return apply_filters('wu_rest_delete_item', true, $this->get_rest_base(), $this);
} // end delete_item_permissions_check;
}
/**
* Checks if a value is not a credential key.
@ -513,9 +447,8 @@ trait Rest_Api {
'api-secret',
);
return !in_array($value, $credentials_keys, true);
} // end is_not_credential_key;
return ! in_array($value, $credentials_keys, true);
}
/**
* Checks if a value is not equal to "id".
@ -532,17 +465,14 @@ trait Rest_Api {
);
if ($this->slug === 'site') {
$arr = array(
'id',
'blog_id',
);
}
} // end if;
return !in_array($value, $arr, true);
} // end is_not_id_key;
return ! in_array($value, $arr, true);
}
/**
* Get the arguments for an endpoint
@ -559,8 +489,7 @@ trait Rest_Api {
$args = array_filter($schema, array($this, 'is_not_id_key'), ARRAY_FILTER_USE_KEY);
return $this->filter_schema_arguments($args);
} // end get_arguments_schema;
}
/**
* Remove some properties from the API schema.
@ -583,102 +512,78 @@ trait Rest_Api {
apply_filters('wu_before_' . $this->slug . '_api_arguments', $args, $this);
if ($this->slug !== 'broadcast' && isset($args['author_id'])) {
unset($args['author_id']);
} // end if;
}
if (isset($args['list_order'])) {
unset($args['list_order']);
}
} // end if;
$remove_status = apply_filters("wu_api_{$this->slug}_remove_status", array(
'broadcast',
'membership',
'product',
'payment',
));
if (!in_array($this->slug, $remove_status, true) && isset($args['status'])) {
$remove_status = apply_filters(
"wu_api_{$this->slug}_remove_status",
array(
'broadcast',
'membership',
'product',
'payment',
)
);
if ( ! in_array($this->slug, $remove_status, true) && isset($args['status'])) {
unset($args['status']);
}
} // end if;
$remove_slug = apply_filters("wu_api_{$this->slug}_remove_slug", array(
'broadcast',
'product',
'checkout_form',
'event',
));
if (!in_array($this->slug, $remove_slug, true) && isset($args['slug'])) {
$remove_slug = apply_filters(
"wu_api_{$this->slug}_remove_slug",
array(
'broadcast',
'product',
'checkout_form',
'event',
)
);
if ( ! in_array($this->slug, $remove_slug, true) && isset($args['slug'])) {
unset($args['slug']);
} // end if;
}
if ($this->slug === 'product' && isset($args['price_variations'])) {
unset($args['price_variations']);
} // end if;
}
if ($this->slug === 'payment' && isset($args['line_items'])) {
unset($args['line_items']);
} // end if;
}
if ($this->slug === 'site') {
if (isset($args['duplication_arguments'])) {
unset($args['duplication_arguments']);
} // end if;
}
if (isset($args['transient'])) {
unset($args['transient']);
} // end if;
} // end if;
}
}
if ($this->slug === 'email') {
if (isset($args['status'])) {
unset($args['status']);
} // end if;
}
if (isset($args['email_schedule'])) {
unset($args['email_schedule']);
} // end if;
} // end if;
}
}
if ($this->slug === 'broadcast') {
if (isset($args['message_targets'])) {
unset($args['message_targets']);
} // end if;
} // end if;
}
}
if (isset($args['billing_address'])) {
unset($args['billing_address']);
} // end if;
}
/**
* Filter after being changed.
@ -691,7 +596,5 @@ trait Rest_Api {
apply_filters('wu_after_' . $this->slug . '_api_arguments', $args, $this);
return $args;
} // end filter_schema_arguments;
} // end trait Rest_Api;
}
}

View File

@ -39,9 +39,8 @@ trait WP_CLI {
*/
public function get_wp_cli_command_base() {
return (!empty($this->wp_cli_command_base)) ? $this->wp_cli_command_base : $this->slug;
} // end get_wp_cli_command_base;
return (! empty($this->wp_cli_command_base)) ? $this->wp_cli_command_base : $this->slug;
}
/**
* Registers the routes. Should be called by the entity
@ -51,18 +50,15 @@ trait WP_CLI {
*/
public function enable_wp_cli() {
if (!defined('WP_CLI')) {
if ( ! defined('WP_CLI')) {
return;
} // end if;
}
$wp_cli_root = 'wu';
$this->set_wp_cli_enabled_sub_commands();
foreach ($this->wp_cli_enabled_sub_commands as $sub_command => $sub_command_data) {
\WP_CLI::add_command(
"{$wp_cli_root} {$this->get_wp_cli_command_base()} {$sub_command}",
$sub_command_data['callback'],
@ -70,10 +66,8 @@ trait WP_CLI {
'synopsis' => $sub_command_data['synopsis'],
)
);
} // end foreach;
} // end enable_wp_cli;
}
}
/**
* Set wP-CLI Sub-command enabled for this entity.
@ -105,34 +99,32 @@ trait WP_CLI {
/**
* Unset undesired Params.
*/
$params_to_remove = apply_filters('wu_cli_params_to_remove', array(
'id',
'model',
));
$params_to_remove = apply_filters(
'wu_cli_params_to_remove',
array(
'id',
'model',
)
);
$params = array_filter($params, fn($param) => !in_array($param, $params_to_remove, true));
$params = array_filter($params, fn($param) => ! in_array($param, $params_to_remove, true));
foreach ($sub_commands as $sub_command => &$sub_command_data) {
$sub_command_data['synopsis'] = array();
if (in_array($sub_command, array('get', 'update', 'delete'), true)) {
$sub_command_data['synopsis'][] = array(
'name' => 'id',
'type' => 'positional',
'description' => __('The id for the resource.', 'wp-ultimo'),
'optional' => false,
);
} // end if;
}
if (in_array($sub_command, array('list', 'update', 'create'), true)) {
$explanation_list = wu_rest_get_endpoint_schema($this->model_class, 'update');
foreach ($params as $name) {
$explanation = wu_get_isset($explanation_list, $name, array());
$type = wu_get_isset($explanation, 'type', 'assoc');
@ -140,37 +132,30 @@ trait WP_CLI {
$field = array(
'name' => $name,
'description' => wu_get_isset($explanation, 'description', __('No description found.', 'wp-ultimo')),
'optional' => !wu_get_isset($explanation, 'required'),
'optional' => ! wu_get_isset($explanation, 'required'),
'type' => 'assoc',
);
$options = wu_get_isset($explanation, 'options', array());
if ($options) {
$field['options'] = $options;
} // end if;
}
$sub_command_data['synopsis'][] = $field;
} // end foreach;
} // end if;
}
}
if (in_array($sub_command, array('create', 'update'), true)) {
$sub_command_data['synopsis'][] = array(
'name' => 'porcelain',
'type' => 'flag',
'description' => __('Output just the id when the operation is successful.', 'wp-ultimo'),
'optional' => true,
);
} // end if;
}
if (in_array($sub_command, array('list', 'get'), true)) {
$sub_command_data['synopsis'][] = array(
'name' => 'format',
'type' => 'assoc',
@ -194,10 +179,8 @@ trait WP_CLI {
'optional' => true,
'options' => array_merge(array('id'), $params),
);
} // end if;
} // end foreach;
}
}
$this->wp_cli_enabled_sub_commands = $sub_commands;
@ -216,8 +199,7 @@ trait WP_CLI {
$this->get_wp_cli_command_base(),
$this
);
} // end set_wp_cli_enabled_sub_commands;
}
/**
* Allows the additional of additional parameters.
*
@ -228,8 +210,7 @@ trait WP_CLI {
$model = new $this->model_class();
return array_keys($model->to_array());
} // end wp_cli_extra_parameters;
}
/**
* Returns the list of default fields, based on the table schema.
@ -242,8 +223,7 @@ trait WP_CLI {
$schema = $this->model_class::get_schema();
return array_column($schema, 'name');
} // end wp_cli_get_fields;
}
/**
* Returns a specific item.
@ -258,18 +238,15 @@ trait WP_CLI {
$item = $this->model_class::get_by_id($args[0]);
if (empty($item)) {
\WP_CLI::error('Invalid ID.');
}
} // end if;
$fields = (!empty($array_assoc['fields'])) ? $array_assoc['fields'] : $this->wp_cli_get_fields();
$fields = (! empty($array_assoc['fields'])) ? $array_assoc['fields'] : $this->wp_cli_get_fields();
$formatter = new \WP_CLI\Formatter($array_assoc, $fields);
$formatter->display_item($item->to_array());
} // end wp_cli_get_item;
}
/**
* Returns a list of items.
@ -281,7 +258,7 @@ trait WP_CLI {
*/
public function wp_cli_get_items($args, $array_assoc) {
$fields = (!empty($array_assoc['fields'])) ? $array_assoc['fields'] : $this->wp_cli_get_fields();
$fields = (! empty($array_assoc['fields'])) ? $array_assoc['fields'] : $this->wp_cli_get_fields();
unset($array_assoc['fields']);
@ -290,8 +267,7 @@ trait WP_CLI {
$items = array_map(fn($item) => $item->to_array(), $items);
\WP_CLI\Utils\format_items($array_assoc['format'], $items, $fields);
} // end wp_cli_get_items;
}
/**
* Creates an item.
@ -308,28 +284,19 @@ trait WP_CLI {
$success = $item->save();
if ($success === true) {
$item_id = $item->get_id();
if (!empty($array_assoc['porcelain'])) {
if ( ! empty($array_assoc['porcelain'])) {
\WP_CLI::line($item_id);
} else {
$message = sprintf('Item created with ID %d', $item_id);
\WP_CLI::success($message);
} // end if;
}
} else {
\WP_CLI::error($success);
} // end if;
} // end wp_cli_create_item;
}
}
/**
* Updates an item.
@ -344,37 +311,27 @@ trait WP_CLI {
$item = $this->model_class::get_by_id($args[0]);
if (empty($item)) {
\WP_CLI::error('Invalid ID.');
} // end if;
}
$porcelain = false;
if (!empty($array_assoc['porcelain'])) {
if ( ! empty($array_assoc['porcelain'])) {
$porcelain = true;
unset($array_assoc['porcelain']);
} // end if;
}
$params = $array_assoc;
foreach ($params as $param => $value) {
$set_method = "set_{$param}";
if ($param === 'meta') {
$item->update_meta_batch($value);
} elseif (method_exists($item, $set_method)) {
call_user_func(array($item, $set_method), $value);
} else {
$error_message = sprintf(
/* translators: 1. Object class name; 2. Set method name */
__('The %1$s object does not have a %2$s method', 'wp-ultimo'),
@ -383,36 +340,25 @@ trait WP_CLI {
);
\WP_CLI::error($error_message);
} // end if;
} // end foreach;
}
}
$success = $item->save();
if ($success) {
$item_id = $item->get_id();
if ($porcelain) {
\WP_CLI::line($item_id);
} else {
$message = sprintf('Item updated with ID %d', $item_id);
\WP_CLI::success($message);
} // end if;
}
} else {
\WP_CLI::error('Unexpected error. The item was not updated.');
} // end if;
} // end wp_cli_update_item;
}
}
/**
* Deletes an item.
@ -426,23 +372,15 @@ trait WP_CLI {
$item = $this->model_class::get_by_id($args[0]);
if (empty($item)) {
\WP_CLI::error('Invalid ID.');
} // end if;
}
$success = $item->delete();
if (is_wp_error($success) || !$success) {
if (is_wp_error($success) || ! $success) {
\WP_CLI::error('Unexpected error. The item was not deleted.');
} else {
\WP_CLI::success('Item deleted.');
} // end if;
} // end wp_cli_delete_item;
} // end trait WP_CLI;
}
}
}