Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Domain/Services
File: DraftOrders.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\Blocks\Domain\Services;
[1] Fix | Delete
[2] Fix | Delete
use Automattic\WooCommerce\Blocks\Domain\Package;
[3] Fix | Delete
use Exception;
[4] Fix | Delete
use WC_Order;
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Service class for adding DraftOrder functionality to WooCommerce core.
[8] Fix | Delete
*
[9] Fix | Delete
* Sets up all logic related to the Checkout Draft Orders service
[10] Fix | Delete
*
[11] Fix | Delete
* @internal
[12] Fix | Delete
*/
[13] Fix | Delete
class DraftOrders {
[14] Fix | Delete
[15] Fix | Delete
const DB_STATUS = 'wc-checkout-draft';
[16] Fix | Delete
const STATUS = 'checkout-draft';
[17] Fix | Delete
[18] Fix | Delete
const DRAFT_CLEANUP_EVENT_HOOK = 'woocommerce_cleanup_draft_orders';
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Holds the Package instance
[22] Fix | Delete
*
[23] Fix | Delete
* @var Package
[24] Fix | Delete
*/
[25] Fix | Delete
private $package;
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Constructor
[29] Fix | Delete
*
[30] Fix | Delete
* @param Package $package An instance of the package class.
[31] Fix | Delete
*/
[32] Fix | Delete
public function __construct( Package $package ) {
[33] Fix | Delete
$this->package = $package;
[34] Fix | Delete
}
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* Set all hooks related to adding Checkout Draft order functionality to Woo Core.
[38] Fix | Delete
*/
[39] Fix | Delete
public function init() {
[40] Fix | Delete
add_filter( 'wc_order_statuses', [ $this, 'register_draft_order_status' ] );
[41] Fix | Delete
add_filter( 'woocommerce_register_shop_order_post_statuses', [ $this, 'register_draft_order_post_status' ] );
[42] Fix | Delete
add_filter( 'woocommerce_analytics_excluded_order_statuses', [ $this, 'append_draft_order_post_status' ] );
[43] Fix | Delete
add_filter( 'woocommerce_valid_order_statuses_for_payment', [ $this, 'append_draft_order_post_status' ], 999 );
[44] Fix | Delete
add_filter( 'woocommerce_valid_order_statuses_for_payment_complete', [ $this, 'append_draft_order_post_status' ], 999 );
[45] Fix | Delete
// Hook into the query to retrieve My Account orders so draft status is excluded.
[46] Fix | Delete
add_action( 'woocommerce_my_account_my_orders_query', [ $this, 'delete_draft_order_post_status_from_args' ] );
[47] Fix | Delete
add_action( self::DRAFT_CLEANUP_EVENT_HOOK, [ $this, 'delete_expired_draft_orders' ] );
[48] Fix | Delete
add_action( 'admin_init', [ $this, 'install' ] );
[49] Fix | Delete
[50] Fix | Delete
if ( defined( 'WC_PLUGIN_BASENAME' ) ) {
[51] Fix | Delete
add_action( 'deactivate_' . WC_PLUGIN_BASENAME, [ $this, 'unschedule_cronjobs' ] );
[52] Fix | Delete
}
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* Installation related logic for Draft order functionality.
[57] Fix | Delete
*
[58] Fix | Delete
* @internal
[59] Fix | Delete
*/
[60] Fix | Delete
public function install() {
[61] Fix | Delete
$this->maybe_create_cronjobs();
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
/**
[65] Fix | Delete
* Unschedule recurring actions when plugin is deactivated.
[66] Fix | Delete
*
[67] Fix | Delete
* @since 10.0.0
[68] Fix | Delete
* @internal
[69] Fix | Delete
*/
[70] Fix | Delete
public function unschedule_cronjobs() {
[71] Fix | Delete
WC()->queue()->cancel_all( self::DRAFT_CLEANUP_EVENT_HOOK );
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
/**
[75] Fix | Delete
* Maybe create cron events.
[76] Fix | Delete
*/
[77] Fix | Delete
protected function maybe_create_cronjobs() {
[78] Fix | Delete
$has_scheduled_action = function_exists( 'as_has_scheduled_action' ) ? 'as_has_scheduled_action' : 'as_next_scheduled_action';
[79] Fix | Delete
if ( false === call_user_func( $has_scheduled_action, self::DRAFT_CLEANUP_EVENT_HOOK ) ) {
[80] Fix | Delete
as_schedule_recurring_action( strtotime( 'midnight tonight' ), DAY_IN_SECONDS, self::DRAFT_CLEANUP_EVENT_HOOK );
[81] Fix | Delete
}
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
/**
[85] Fix | Delete
* Register custom order status for orders created via the API during checkout.
[86] Fix | Delete
*
[87] Fix | Delete
* Draft order status is used before payment is attempted, during checkout, when a cart is converted to an order.
[88] Fix | Delete
*
[89] Fix | Delete
* @param array $statuses Array of statuses.
[90] Fix | Delete
* @internal
[91] Fix | Delete
* @return array
[92] Fix | Delete
*/
[93] Fix | Delete
public function register_draft_order_status( array $statuses ) {
[94] Fix | Delete
$statuses[ self::DB_STATUS ] = _x( 'Draft', 'Order status', 'woocommerce' );
[95] Fix | Delete
return $statuses;
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/**
[99] Fix | Delete
* Register custom order post status for orders created via the API during checkout.
[100] Fix | Delete
*
[101] Fix | Delete
* @param array $statuses Array of statuses.
[102] Fix | Delete
* @internal
[103] Fix | Delete
[104] Fix | Delete
* @return array
[105] Fix | Delete
*/
[106] Fix | Delete
public function register_draft_order_post_status( array $statuses ) {
[107] Fix | Delete
$statuses[ self::DB_STATUS ] = $this->get_post_status_properties();
[108] Fix | Delete
return $statuses;
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Returns the properties of this post status for registration.
[113] Fix | Delete
*
[114] Fix | Delete
* @return array
[115] Fix | Delete
*/
[116] Fix | Delete
private function get_post_status_properties() {
[117] Fix | Delete
return [
[118] Fix | Delete
'label' => _x( 'Draft', 'Order status', 'woocommerce' ),
[119] Fix | Delete
'public' => false,
[120] Fix | Delete
'exclude_from_search' => false,
[121] Fix | Delete
'show_in_admin_all_list' => false,
[122] Fix | Delete
'show_in_admin_status_list' => true,
[123] Fix | Delete
/* translators: %s: number of orders */
[124] Fix | Delete
'label_count' => _n_noop( 'Drafts <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>', 'woocommerce' ),
[125] Fix | Delete
];
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
/**
[129] Fix | Delete
* Remove draft status from the 'status' argument of an $args array.
[130] Fix | Delete
*
[131] Fix | Delete
* @param array $args Array of arguments containing statuses in the status key.
[132] Fix | Delete
* @internal
[133] Fix | Delete
* @return array
[134] Fix | Delete
*/
[135] Fix | Delete
public function delete_draft_order_post_status_from_args( $args ) {
[136] Fix | Delete
if ( ! array_key_exists( 'status', $args ) ) {
[137] Fix | Delete
$statuses = [];
[138] Fix | Delete
foreach ( wc_get_order_statuses() as $key => $label ) {
[139] Fix | Delete
if ( self::DB_STATUS !== $key ) {
[140] Fix | Delete
$statuses[] = str_replace( 'wc-', '', $key );
[141] Fix | Delete
}
[142] Fix | Delete
}
[143] Fix | Delete
$args['status'] = $statuses;
[144] Fix | Delete
} elseif ( self::DB_STATUS === $args['status'] ) {
[145] Fix | Delete
$args['status'] = '';
[146] Fix | Delete
} elseif ( is_array( $args['status'] ) ) {
[147] Fix | Delete
$args['status'] = array_diff_key( $args['status'], array( self::STATUS => null ) );
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
return $args;
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* Append draft status to a list of statuses.
[155] Fix | Delete
*
[156] Fix | Delete
* @param array $statuses Array of statuses.
[157] Fix | Delete
* @internal
[158] Fix | Delete
[159] Fix | Delete
* @return array
[160] Fix | Delete
*/
[161] Fix | Delete
public function append_draft_order_post_status( $statuses ) {
[162] Fix | Delete
$statuses[] = self::STATUS;
[163] Fix | Delete
return $statuses;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
/**
[167] Fix | Delete
* Delete draft orders older than a day in batches of 20.
[168] Fix | Delete
*
[169] Fix | Delete
* Ran on a daily cron schedule.
[170] Fix | Delete
*
[171] Fix | Delete
* @internal
[172] Fix | Delete
*/
[173] Fix | Delete
public function delete_expired_draft_orders() {
[174] Fix | Delete
$count = 0;
[175] Fix | Delete
$batch_size = 20;
[176] Fix | Delete
$this->ensure_draft_status_registered();
[177] Fix | Delete
$orders = wc_get_orders(
[178] Fix | Delete
[
[179] Fix | Delete
'date_modified' => '<=' . strtotime( '-1 DAY' ),
[180] Fix | Delete
'limit' => $batch_size,
[181] Fix | Delete
'status' => self::DB_STATUS,
[182] Fix | Delete
'type' => 'shop_order',
[183] Fix | Delete
]
[184] Fix | Delete
);
[185] Fix | Delete
[186] Fix | Delete
// do we bail because the query results are unexpected?
[187] Fix | Delete
try {
[188] Fix | Delete
$this->assert_order_results( $orders, $batch_size );
[189] Fix | Delete
if ( $orders ) {
[190] Fix | Delete
foreach ( $orders as $order ) {
[191] Fix | Delete
$order->delete( true );
[192] Fix | Delete
++$count;
[193] Fix | Delete
}
[194] Fix | Delete
}
[195] Fix | Delete
if ( $batch_size === $count && function_exists( 'as_enqueue_async_action' ) ) {
[196] Fix | Delete
as_enqueue_async_action( self::DRAFT_CLEANUP_EVENT_HOOK );
[197] Fix | Delete
}
[198] Fix | Delete
} catch ( Exception $error ) {
[199] Fix | Delete
wc_caught_exception( $error, __METHOD__ );
[200] Fix | Delete
}
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Since it's possible for third party code to clobber the `$wp_post_statuses` global,
[205] Fix | Delete
* we need to do a final check here to make sure the draft post status is
[206] Fix | Delete
* registered with the global so that it is not removed by WP_Query status
[207] Fix | Delete
* validation checks.
[208] Fix | Delete
*/
[209] Fix | Delete
private function ensure_draft_status_registered() {
[210] Fix | Delete
$is_registered = get_post_stati( [ 'name' => self::DB_STATUS ] );
[211] Fix | Delete
if ( empty( $is_registered ) ) {
[212] Fix | Delete
register_post_status(
[213] Fix | Delete
self::DB_STATUS,
[214] Fix | Delete
$this->get_post_status_properties()
[215] Fix | Delete
);
[216] Fix | Delete
}
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
/**
[220] Fix | Delete
* Asserts whether incoming order results are expected given the query
[221] Fix | Delete
* this service class executes.
[222] Fix | Delete
*
[223] Fix | Delete
* @param WC_Order[] $order_results The order results being asserted.
[224] Fix | Delete
* @param int $expected_batch_size The expected batch size for the results.
[225] Fix | Delete
* @throws Exception If any assertions fail, an exception is thrown.
[226] Fix | Delete
*/
[227] Fix | Delete
private function assert_order_results( $order_results, $expected_batch_size ) {
[228] Fix | Delete
// if not an array, then just return because it won't get handled
[229] Fix | Delete
// anyways.
[230] Fix | Delete
if ( ! is_array( $order_results ) ) {
[231] Fix | Delete
return;
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
$suffix = ' This is an indicator that something is filtering WooCommerce or WordPress queries and modifying the query parameters.';
[235] Fix | Delete
[236] Fix | Delete
// if count is greater than our expected batch size, then that's a problem.
[237] Fix | Delete
if ( count( $order_results ) > 20 ) {
[238] Fix | Delete
throw new Exception( 'There are an unexpected number of results returned from the query.' . $suffix );
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
// if any of the returned orders are not draft (or not a WC_Order), then that's a problem.
[242] Fix | Delete
foreach ( $order_results as $order ) {
[243] Fix | Delete
if ( ! ( $order instanceof WC_Order ) ) {
[244] Fix | Delete
throw new Exception( 'The returned results contain a value that is not a WC_Order.' . $suffix );
[245] Fix | Delete
}
[246] Fix | Delete
if ( ! $order->has_status( self::STATUS ) ) {
[247] Fix | Delete
throw new Exception( 'The results contain an order that is not a `wc-checkout-draft` status in the results.' . $suffix );
[248] Fix | Delete
}
[249] Fix | Delete
}
[250] Fix | Delete
}
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function