From 9abec351d11863ede34d506586a96dfad95b6072 Mon Sep 17 00:00:00 2001 From: James Pham Date: Fri, 28 Oct 2022 14:05:26 -0700 Subject: [PATCH 1/2] Update class PHPdocs & remove limit from pending --- classes/VTACosEmailManager.php | 7 +++---- classes/VTACosSettingsManager.php | 7 +++---- classes/VTACustomEmail.php | 10 +++++----- classes/VTACustomOrderStatuses.php | 13 ++++++------- classes/VTAWooCommerce.php | 13 +++++++------ models/VTACustomOrderStatus.php | 1 - 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/classes/VTACosEmailManager.php b/classes/VTACosEmailManager.php index 8e1d649..f70cf53 100644 --- a/classes/VTACosEmailManager.php +++ b/classes/VTACosEmailManager.php @@ -4,7 +4,6 @@ } /** - * @class VTACosEmailManager * Custom class to provide email functionality to all custom classes without default WooCommerce email templates. */ class VTACosEmailManager { @@ -17,7 +16,7 @@ class VTACosEmailManager { /** @var VTACustomOrderStatus[] */ private array $no_email_statuses; - /** @var string[] hardcoded default statuses. Emails that we want to override with our custom class & templates */ + /** @var string[] hardcoded default statuses. Emails that we want to override with our custom class & templates */ private array $default_order_statuses = [ 'processing', 'on-hold', @@ -113,8 +112,8 @@ public function send_email( int $order_id, WC_Order $order, bool $is_reminder = try { $order_status = VTACustomOrderStatus::get_cos_by_key($order->get_status()); $this->wc_emails = WC_Emails::instance(); // must re-initiate class to trigger custom email classes - $this->wc_emails->init(); // initiate only for custom emails... - do_action($order_status->get_email_action() . ($is_reminder ? '_reminder' : ''), $order); + $this->wc_emails->init(); // initiate only for custom emails... + do_action($order_status->get_email_action() . ( $is_reminder ? '_reminder' : '' ), $order); } catch ( Exception $e ) { error_log("VTACosEmailManager::send_email() error. Could not send email for Order #$order_id - $e"); diff --git a/classes/VTACosSettingsManager.php b/classes/VTACosSettingsManager.php index 8ab0e74..5f0bce7 100644 --- a/classes/VTACosSettingsManager.php +++ b/classes/VTACosSettingsManager.php @@ -1,7 +1,6 @@ $query_params) = get_query_params(); + [ 'query_params' => $query_params ] = get_query_params(); // Settings page $is_settings_page = in_array($this->post_type, $query_params) && in_array($this->settings_page, $query_params); @@ -199,8 +198,8 @@ public function default_settings(): void { */ public function render_settings_page(): void { $reorderable_statuses = $this->settings->get_reorderable_statuses(); - $reminder_statuses = $this->settings->get_reminder_statuses(); - include_once(plugin_dir_path(__DIR__) . '/admin/views/settings-page.php'); + $reminder_statuses = $this->settings->get_reminder_statuses(); + include_once( plugin_dir_path(__DIR__) . '/admin/views/settings-page.php' ); } /** diff --git a/classes/VTACustomEmail.php b/classes/VTACustomEmail.php index ba3592a..a9e4155 100644 --- a/classes/VTACustomEmail.php +++ b/classes/VTACustomEmail.php @@ -1,9 +1,9 @@ is_reminder = $is_reminder; // Add email ID, title, description, heading, subject - $this->id = "custom_email_{$order_status->get_cos_key()}" . ($is_reminder ? '_reminder' : ''); + $this->id = "custom_email_{$order_status->get_cos_key()}" . ( $is_reminder ? '_reminder' : '' ); $this->customer_email = true; - $this->title = "{$order_status->get_cos_name()} Email" . ($is_reminder ? ' (Reminder)' : ''); + $this->title = "{$order_status->get_cos_name()} Email" . ( $is_reminder ? ' (Reminder)' : '' ); $this->description = $is_reminder ? "This is a reminder email for Order Status \"{$order_status->get_cos_name()}\"" : "This email is received when an order status is changed to \"{$order_status->get_cos_name()}\"."; - $this->heading = "{$order_status->get_cos_name()}" . ($is_reminder ? ' (Reminder)' : ''); - $this->subject = "{$order_status->get_cos_name()} (Order #{order_number}) - {order_date}" . ($is_reminder ? ' (Reminder)' : ''); + $this->heading = "{$order_status->get_cos_name()}" . ( $is_reminder ? ' (Reminder)' : '' ); + $this->subject = "{$order_status->get_cos_name()} (Order #{order_number}) - {order_date}" . ( $is_reminder ? ' (Reminder)' : '' ); // email template path $this->template_html = $is_reminder ? 'templates/custom-reminder-email-html.php' : 'templates/custom-email-html.php'; diff --git a/classes/VTACustomOrderStatuses.php b/classes/VTACustomOrderStatuses.php index e8a8edf..ef5d7a1 100644 --- a/classes/VTACustomOrderStatuses.php +++ b/classes/VTACustomOrderStatuses.php @@ -1,26 +1,25 @@ Pending Orders (%d)', - ($query_params['post_status'] ?? null) === 'pending-orders' ? 'class="current" aria-current="page"' : '', + ( $query_params['post_status'] ?? null ) === 'pending-orders' ? 'class="current" aria-current="page"' : '', $this->get_pending_orders_count() ); @@ -310,7 +309,7 @@ private function sort_order_statuses( array $order_statuses ): array { $arrangement_ids = $this->settings->get_arrangement(); try { - $arrangement_cos_keys = array_map(fn( int $post_id ) => (new VTACustomOrderStatus($post_id))->get_cos_key(true), $arrangement_ids); + $arrangement_cos_keys = array_map(fn( int $post_id ) => ( new VTACustomOrderStatus($post_id) )->get_cos_key(true), $arrangement_ids); return array_replace(array_flip($arrangement_cos_keys), $order_statuses); } catch ( Exception $e ) { error_log("VTAWooCommerce::sort_order_status error. Could not sort custom order statuses - $e"); @@ -324,7 +323,10 @@ private function sort_order_statuses( array $order_statuses ): array { */ private function get_pending_orders_count(): int { $non_reorderable_cos = $this->get_pending_cos_keys(); - $orders = wc_get_orders([ 'status' => $non_reorderable_cos ]); + $orders = wc_get_orders([ + 'status' => $non_reorderable_cos, + 'limit' => -1 + ]); // manually filter orders since pre_get_posts interferes with count $filtered_orders = array_filter( @@ -349,9 +351,8 @@ private function get_pending_cos_keys( bool $with_prefix = false ): array { 'failed' ]; - $vta_cos = $this->vta_cos; $filtered_cos = array_filter( - $vta_cos, + $this->vta_cos, fn( VTACustomOrderStatus $order_status ) => !$order_status->get_cos_reorderable() && !in_array($order_status->get_cos_key(), $other_non_pending_statuses) ); diff --git a/models/VTACustomOrderStatus.php b/models/VTACustomOrderStatus.php index cd19ea2..47ecb28 100644 --- a/models/VTACustomOrderStatus.php +++ b/models/VTACustomOrderStatus.php @@ -1,7 +1,6 @@ Date: Fri, 28 Oct 2022 14:56:35 -0700 Subject: [PATCH 2/2] Remove unused settings from old custom emails --- classes/VTAWooCommerce.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/classes/VTAWooCommerce.php b/classes/VTAWooCommerce.php index f1ef507..36785b9 100644 --- a/classes/VTAWooCommerce.php +++ b/classes/VTAWooCommerce.php @@ -45,9 +45,10 @@ public function __construct( string $plugin_name, string $plugin_version, VTACos $this->vta_cos = $this->get_cos(); $this->pending_status_keys = $this->get_pending_cos_keys(true); - /** - * Need to run this hook first before other filter methods are ran in the Orders page - */ + // clean up options from old custom emails + $this->deprecated_options_cleanup(); + + // Need to run this hook first before other filter methods are ran in the Orders page add_action('pre_get_posts', [ $this, 'query_include_deprecated' ], 10, 1); add_action('pre_get_posts', [ $this, 'query_pending_orders' ], 10, 1); @@ -359,4 +360,17 @@ private function get_pending_cos_keys( bool $with_prefix = false ): array { return array_values(array_map(fn( VTACustomOrderStatus $order_status ) => $order_status->get_cos_key($with_prefix), $filtered_cos)); } + /** + * Cleans up old Options API from past custom emails and others (no longer used) + * @return void + */ + private function deprecated_options_cleanup(): void { + delete_option('woocommerce_finishing_email_settings'); + delete_option('woocommerce_special_email_settings'); + delete_option('woocommerce_ready_for_pickup_email_settings'); + delete_option('woocommerce_proof_ready_email_settings'); + delete_option('woocommerce_proof_email_settings'); + delete_option('woocommerce_ready_reminder_email_settings'); + } + }