From 7767f5ea93409f757bdb8d3ee25971977b0edfee Mon Sep 17 00:00:00 2001 From: Barry Kooij Date: Wed, 14 Oct 2026 07:59:36 +0200 Subject: [PATCH 1/7] Removed: CLAUDE.md from the repository and the plugin package --- .gitignore | 1 + CLAUDE.md | 5 ----- 2 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 CLAUDE.md diff --git a/.gitignore b/.gitignore index d6fba88..18c79b4 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ /tmp/ /dev/ phpcs.txt +CLAUDE.md # OS / Editor .DS_Store diff --git a/CLAUDE.md b/CLAUDE.md deleted file mode 100644 index d92e756..0000000 --- a/CLAUDE.md +++ /dev/null @@ -1,5 +0,0 @@ -# scanfully-plugin - -Repository instructions, shared with VS Code Copilot: - -@.github/copilot-instructions.md From e57b195595fdd78704f2e6ecbdc499437c72a398 Mon Sep 17 00:00:00 2001 From: Barry Kooij Date: Thu, 15 Oct 2026 13:15:40 +0200 Subject: [PATCH 2/7] Added: WooCommerce checkout tests can buy subscription and variable subscription products; the subscriptions a test order creates are cancelled at once without emails and deleted by the test order cleanup --- src/WooCheckout/Cleanup.php | 41 ++++++++--- src/WooCheckout/Controller.php | 56 +++++++++++---- src/WooCheckout/ProbeGateway.php | 76 +++++++++++++++++++-- tests/Unit/WooCheckout/ProbeOrderTest.php | 71 ++++++++++++++++++- tests/Unit/WooCheckout/ProductTypesTest.php | 47 +++++++++++++ 5 files changed, 262 insertions(+), 29 deletions(-) create mode 100644 tests/Unit/WooCheckout/ProductTypesTest.php diff --git a/src/WooCheckout/Cleanup.php b/src/WooCheckout/Cleanup.php index 820ffe9..4b7f9ac 100644 --- a/src/WooCheckout/Cleanup.php +++ b/src/WooCheckout/Cleanup.php @@ -1,7 +1,8 @@ update_status( 'cancelled', __( 'Scanfully probe order. Cancelled by the probe order cleanup.', 'scanfully' ) ); @@ -80,9 +96,11 @@ private static function cancel_stale_pending_orders(): void { * Permanently delete cancelled probe orders older than the retention * period. * + * @param string $type Order type: shop_order or shop_subscription. + * * @return void */ - private static function delete_old_cancelled_orders(): void { + private static function delete_old_cancelled_orders( string $type ): void { $retention_days = (int) apply_filters( 'scanfully_woocheckout_probe_order_retention_days', self::RETENTION_DAYS ); if ( $retention_days < 1 ) { $retention_days = 1; @@ -90,7 +108,7 @@ private static function delete_old_cancelled_orders(): void { $cutoff = time() - ( $retention_days * DAY_IN_SECONDS ); for ( $batch = 0; $batch < self::MAX_BATCHES; $batch++ ) { - $orders = self::find_probe_orders( 'cancelled', $cutoff ); + $orders = self::find_probe_orders( $type, 'cancelled', $cutoff ); foreach ( $orders as $order ) { $order->delete( true ); @@ -103,19 +121,22 @@ private static function delete_old_cancelled_orders(): void { } /** - * Find probe orders with a status, created before a cutoff. + * Find probe orders (or probe subscriptions) with a status, created + * before a cutoff. * * Every result is checked again in PHP (see is_probe_order()), so a meta * filter that is ignored, for example by a custom order data store, can * never hand a real order to the caller. * + * @param string $type Order type: shop_order or shop_subscription. * @param string $status Order status without the `wc-` prefix. * @param int $cutoff Only orders created before this Unix timestamp. * * @return \WC_Order[] */ - private static function find_probe_orders( string $status, int $cutoff ): array { + private static function find_probe_orders( string $type, string $status, int $cutoff ): array { $args = [ + 'type' => $type, 'limit' => self::BATCH_SIZE, 'date_created' => '<' . $cutoff, 'status' => [ $status ], diff --git a/src/WooCheckout/Controller.php b/src/WooCheckout/Controller.php index 9bf7a71..04e3168 100644 --- a/src/WooCheckout/Controller.php +++ b/src/WooCheckout/Controller.php @@ -302,11 +302,38 @@ public static function current_scan_id(): string { return isset( $parts[0] ) ? (string) $parts[0] : ''; } + /** + * Product types a test can buy, in the order the automatic pick tries + * them. Subscription products come last, and only while WooCommerce + * Subscriptions is active: a test order for one also creates a + * subscription, which the plugin then has to cancel and clean up. + * + * @return string[] + */ + private static function product_types(): array { + $types = [ 'simple', 'variable' ]; + if ( class_exists( '\WC_Subscriptions_Product' ) ) { + $types[] = 'subscription'; + $types[] = 'variable-subscription'; + } + return $types; + } + + /** + * Whether products of the type are bought through a variation. + * + * @param string $type Product type. + * @return bool + */ + private static function is_variable_type( string $type ): bool { + return 'variable' === $type || 'variable-subscription' === $type; + } + /** * Pick the default product URL the orchestrator should start the scan * at: in stock, paid (`price > 0`), published; simple products first, - * variable products as fallback. Used when the user has not chosen a - * product in the Scanfully dashboard. + * then variable and subscription products (see product_types()). Used + * when the user has not chosen a product in the Scanfully dashboard. * * @return string Empty string when no eligible product exists. */ @@ -319,8 +346,8 @@ public static function pick_product_url(): string { } /** - * Pick the eligible product object: simple products first, falling back - * to variable products when no eligible simple product exists. + * Pick the eligible product object: simple products first, then the + * other types in the order of product_types(). * * @return \WC_Product|null */ @@ -331,8 +358,8 @@ private static function pick_product(): ?\WC_Product { // Simple products first; variable products only when no eligible // simple product exists (their variation resolution adds moving - // parts to the scan). - foreach ( [ 'simple', 'variable' ] as $type ) { + // parts to the scan), subscription products last. + foreach ( self::product_types() as $type ) { $product = self::pick_product_of_type( $type ); if ( null !== $product ) { return $product; @@ -347,7 +374,7 @@ private static function pick_product(): ?\WC_Product { * ascending so the same product is picked deterministically across * repeated scans on the same shop. * - * @param string $type Product type ('simple' or 'variable'). + * @param string $type Product type, one of product_types(). * @return \WC_Product|null */ private static function pick_product_of_type( string $type ): ?\WC_Product { @@ -386,10 +413,10 @@ private static function pick_product_of_type( string $type ): ?\WC_Product { /** * Whether the product is probe-eligible: in stock, paid (`price > 0`), - * has a permalink, purchasable (variable). + * has a permalink, purchasable (variable types). * * @param \WC_Product $product Product to check. - * @param string $type Product type ('simple' or 'variable'). + * @param string $type Product type, one of product_types(). * @return bool */ private static function is_eligible_product( \WC_Product $product, string $type ): bool { @@ -402,7 +429,7 @@ private static function is_eligible_product( \WC_Product $product, string $type if ( '' === (string) $product->get_permalink() ) { return false; } - if ( 'variable' === $type && ( ! $product->is_purchasable() || ! $product->is_in_stock() ) ) { + if ( self::is_variable_type( $type ) && ( ! $product->is_purchasable() || ! $product->is_in_stock() ) ) { return false; } return true; @@ -410,8 +437,9 @@ private static function is_eligible_product( \WC_Product $product, string $type /** * Search probe-eligible products for the dashboard product select. - * Empty term returns the first eligible products (simple before - * variable). Uses WooCommerce's own product search for term matching. + * Empty term returns the first eligible products, by type in the order + * of product_types(). Uses WooCommerce's own product search for term + * matching. * * @param string $term Search term (may be empty). * @param int $limit Maximum number of results. @@ -445,7 +473,7 @@ public static function search_products( string $term, int $limit = 20 ): array { continue; } $type = $product->get_type(); - if ( ! in_array( $type, [ 'simple', 'variable' ], true ) ) { + if ( ! in_array( $type, self::product_types(), true ) ) { continue; } if ( ! $product->is_in_stock() || ! self::is_eligible_product( $product, $type ) ) { @@ -456,7 +484,7 @@ public static function search_products( string $term, int $limit = 20 ): array { return $results; } - foreach ( [ 'simple', 'variable' ] as $type ) { + foreach ( self::product_types() as $type ) { if ( count( $results ) >= $limit ) { break; } diff --git a/src/WooCheckout/ProbeGateway.php b/src/WooCheckout/ProbeGateway.php index dc9b70f..242f57a 100644 --- a/src/WooCheckout/ProbeGateway.php +++ b/src/WooCheckout/ProbeGateway.php @@ -51,6 +51,11 @@ public static function setup(): void { add_filter( 'woocommerce_email_recipient_new_order', [ self::class, 'suppress_email_for_probe' ], 10, 2 ); add_filter( 'woocommerce_email_recipient_customer_processing_order', [ self::class, 'suppress_email_for_probe' ], 10, 2 ); add_filter( 'woocommerce_email_recipient_customer_on_hold_order', [ self::class, 'suppress_email_for_probe' ], 10, 2 ); + // WooCommerce Subscriptions: the subscription a probe order creates + // is cancelled right away, which would otherwise email the shop. + add_filter( 'woocommerce_email_recipient_cancelled_subscription', [ self::class, 'suppress_email_for_probe' ], 10, 2 ); + add_filter( 'woocommerce_email_recipient_expired_subscription', [ self::class, 'suppress_email_for_probe' ], 10, 2 ); + add_filter( 'woocommerce_email_recipient_suspended_subscription', [ self::class, 'suppress_email_for_probe' ], 10, 2 ); add_filter( 'woocommerce_can_reduce_order_stock', [ self::class, 'skip_stock_reduction_for_probe' ], 10, 2 ); add_filter( 'woocommerce_webhook_should_deliver', [ self::class, 'skip_webhooks_for_probe' ], 10, 3 ); @@ -58,6 +63,9 @@ public static function setup(): void { // checkout), so they can be found even if payment never runs. add_action( 'woocommerce_checkout_create_order', [ self::class, 'tag_new_probe_order' ] ); add_action( 'woocommerce_store_api_checkout_update_order_meta', [ self::class, 'tag_new_probe_order' ] ); + // WooCommerce Subscriptions creates the subscriptions of an order at + // checkout, before payment; tag them the same way. + add_action( 'woocommerce_checkout_subscription_created', [ self::class, 'tag_new_probe_order' ] ); } /** @@ -158,7 +166,9 @@ private static function probe_only_gateways( array $gateways ): array { * * The scan ends at the stub PSP, so the order is cancelled right away. * Cancelling releases the stock WooCommerce reserved at checkout, and - * pending -> cancelled sends no email. + * pending -> cancelled sends no email. Subscriptions the order created + * (WooCommerce Subscriptions) are tagged before and cancelled after, so + * their emails stay suppressed and the cleanup deletes them too. * * @param int $order_id Order id. * @@ -174,9 +184,19 @@ public function process_payment( $order_id ) { } self::tag_probe_order( $order ); + $subscriptions = self::order_subscriptions( $order ); + foreach ( $subscriptions as $subscription ) { + self::tag_probe_order( $subscription ); + $subscription->save_meta_data(); + } + $order->set_status( 'cancelled', __( 'Scanfully probe order. Cancelled automatically at the end of the checkout check.', 'scanfully' ) ); $order->save(); + foreach ( $subscriptions as $subscription ) { + self::cancel_subscription( $subscription ); + } + $redirect = add_query_arg( [ 'scanfully_probe_psp' => '1', @@ -191,6 +211,53 @@ public function process_payment( $order_id ) { ]; } + /** + * The subscriptions WooCommerce Subscriptions created for an order at + * checkout; none when the plugin is not active. + * + * @param \WC_Order $order The parent order. + * + * @return \WC_Order[] + */ + private static function order_subscriptions( \WC_Order $order ): array { + if ( ! function_exists( 'wcs_get_subscriptions_for_order' ) ) { + return []; + } + $subscriptions = wcs_get_subscriptions_for_order( $order, [ 'order_type' => 'parent' ] ); + if ( ! is_array( $subscriptions ) ) { + return []; + } + return array_values( + array_filter( + $subscriptions, + static function ( $subscription ) { + return $subscription instanceof \WC_Order; + } + ) + ); + } + + /** + * Cancel a probe subscription, unless WooCommerce Subscriptions already + * did when the parent order was cancelled. + * + * @param \WC_Order $subscription The subscription. + * + * @return void + */ + private static function cancel_subscription( \WC_Order $subscription ): void { + if ( $subscription->has_status( 'cancelled' ) ) { + return; + } + try { + $subscription->update_status( 'cancelled', __( 'Scanfully probe subscription. Cancelled automatically at the end of the checkout check.', 'scanfully' ) ); + } catch ( \Exception $e ) { + // A status Subscriptions will not move to cancelled; the cleanup + // cancels it later. + return; + } + } + /** * Suppress WC email recipients for probe orders. * @@ -222,8 +289,9 @@ public static function skip_stock_reduction_for_probe( $can_reduce, $order ) { } /** - * Keep probe orders out of WooCommerce webhooks (order.created and the - * like), so ERP, marketing and accounting integrations never see them. + * Keep probe orders and their subscriptions out of WooCommerce webhooks + * (order.created, subscription.created and the like), so ERP, marketing + * and accounting integrations never see them. * * @param bool $should_deliver Whether the webhook should be delivered. * @param \WC_Webhook $webhook The webhook. @@ -232,7 +300,7 @@ public static function skip_stock_reduction_for_probe( $can_reduce, $order ) { * @return bool */ public static function skip_webhooks_for_probe( $should_deliver, $webhook, $arg ) { - if ( ! $should_deliver || ! $webhook instanceof \WC_Webhook || 'order' !== $webhook->get_resource() || ! is_numeric( $arg ) ) { + if ( ! $should_deliver || ! $webhook instanceof \WC_Webhook || ! in_array( $webhook->get_resource(), [ 'order', 'subscription' ], true ) || ! is_numeric( $arg ) ) { return $should_deliver; } diff --git a/tests/Unit/WooCheckout/ProbeOrderTest.php b/tests/Unit/WooCheckout/ProbeOrderTest.php index e906ad8..2794478 100644 --- a/tests/Unit/WooCheckout/ProbeOrderTest.php +++ b/tests/Unit/WooCheckout/ProbeOrderTest.php @@ -25,6 +25,9 @@ protected function setUp(): void { parent::setUp(); $this->stubTranslationFunctions(); $this->set_probe_request( false ); + // WooCommerce Subscriptions is "active" in every test, so the + // subscription paths always run; orders have none unless a test says so. + Functions\when( 'wcs_get_subscriptions_for_order' )->justReturn( [] ); } protected function tearDown(): void { @@ -55,7 +58,7 @@ private function set_probe_request( bool $is_probe ): void { * @param bool $tagged Whether the probe meta is set. * @param string $payment_method Payment method ID. * - * @return \Mockery\MockInterface + * @return \Mockery\MockInterface&\WC_Order */ private function order( bool $tagged = true, string $payment_method = Controller::PROBE_GATEWAY_ID ) { $order = Mockery::mock( 'WC_Order' ); @@ -79,6 +82,47 @@ public function test_a_probe_payment_cancels_the_order(): void { $this->assertStringContainsString( 'scanfully_probe_psp=1', $result['redirect'] ); } + public function test_a_probe_payment_tags_and_cancels_the_subscriptions_it_created(): void { + $order = Mockery::mock( 'WC_Order' ); + $order->shouldReceive( 'update_meta_data' )->once()->with( '_scanfully_probe_order', 'true' ); + $order->shouldReceive( 'set_status' )->once()->with( 'cancelled', Mockery::type( 'string' ) ); + $order->shouldReceive( 'save' )->once(); + + $pending = Mockery::mock( 'WC_Order' ); + $pending->shouldReceive( 'update_meta_data' )->once()->with( '_scanfully_probe_order', 'true' ); + $pending->shouldReceive( 'save_meta_data' )->once(); + $pending->shouldReceive( 'has_status' )->with( 'cancelled' )->andReturn( false ); + $pending->shouldReceive( 'update_status' )->once()->with( 'cancelled', Mockery::type( 'string' ) ); + + // Subscriptions already cancelled it along with the parent order. + $already = Mockery::mock( 'WC_Order' ); + $already->shouldReceive( 'update_meta_data' )->once()->with( '_scanfully_probe_order', 'true' ); + $already->shouldReceive( 'save_meta_data' )->once(); + $already->shouldReceive( 'has_status' )->with( 'cancelled' )->andReturn( true ); + $already->shouldNotReceive( 'update_status' ); + + Functions\when( 'wcs_get_subscriptions_for_order' )->justReturn( + [ + 101 => $pending, + 102 => $already, + ] + ); + Functions\when( 'wc_get_order' )->justReturn( $order ); + Functions\when( 'home_url' )->justReturn( 'https://shop.test/' ); + Functions\when( 'add_query_arg' )->alias( static fn( $args, $url ) => $url . '?' . http_build_query( $args ) ); + + $result = ( new ProbeGateway() )->process_payment( 12 ); + + $this->assertSame( 'success', $result['result'] ); + } + + public function test_emails_about_probe_subscriptions_are_suppressed(): void { + $subscription = $this->order(); + + $this->assertSame( '', ProbeGateway::suppress_email_for_probe( 'shop@scanfully.com', $subscription ) ); + $this->assertSame( 'shop@scanfully.com', ProbeGateway::suppress_email_for_probe( 'shop@scanfully.com', $this->order( false ) ) ); + } + public function test_orders_created_during_a_probe_are_tagged(): void { $this->set_probe_request( true ); $_SERVER['HTTP_X_SCANFULLY_PROBE'] = 'sf-1-abcd:sig'; @@ -113,6 +157,9 @@ public function test_cleanup_cancels_stale_pending_probe_orders_and_deletes_only $queried_statuses = []; Functions\when( 'wc_get_orders' )->alias( function ( array $args ) use ( $pending, $cancelled, &$queried_statuses ) { + if ( 'shop_order' !== $args['type'] ) { + return []; + } $queried_statuses[] = $args['status']; return [ 'pending' ] === $args['status'] ? [ $pending ] : [ $cancelled ]; } @@ -123,6 +170,28 @@ function ( array $args ) use ( $pending, $cancelled, &$queried_statuses ) { $this->assertSame( [ [ 'pending' ], [ 'cancelled' ] ], $queried_statuses ); } + public function test_cleanup_handles_probe_subscriptions_before_probe_orders(): void { + $pending = $this->order(); + $cancelled = $this->order(); + $pending->shouldReceive( 'update_status' )->once()->with( 'cancelled', Mockery::type( 'string' ) ); + $cancelled->shouldReceive( 'delete' )->once()->with( true ); + + $queries = []; + Functions\when( 'wc_get_orders' )->alias( + function ( array $args ) use ( $pending, $cancelled, &$queries ) { + $queries[] = $args['type'] . ':' . $args['status'][0]; + if ( 'shop_subscription' !== $args['type'] ) { + return []; + } + return [ 'pending' ] === $args['status'] ? [ $pending ] : [ $cancelled ]; + } + ); + + Cleanup::run(); + + $this->assertSame( [ 'shop_subscription:pending', 'shop_subscription:cancelled', 'shop_order:pending', 'shop_order:cancelled' ], $queries ); + } + public function test_cleanup_never_touches_orders_that_are_not_probe_orders(): void { $untagged = $this->order( false ); $real_method = $this->order( true, 'stripe' ); diff --git a/tests/Unit/WooCheckout/ProductTypesTest.php b/tests/Unit/WooCheckout/ProductTypesTest.php new file mode 100644 index 0000000..cf436b4 --- /dev/null +++ b/tests/Unit/WooCheckout/ProductTypesTest.php @@ -0,0 +1,47 @@ +setAccessible( true ); + return $reflection->invoke( null, ...$args ); + } + + public function test_subscription_products_are_tried_last_when_subscriptions_is_active(): void { + // Declares the class WooCommerce Subscriptions is recognised by. + Mockery::mock( 'WC_Subscriptions_Product' ); + + $this->assertSame( [ 'simple', 'variable', 'subscription', 'variable-subscription' ], $this->call( 'product_types' ) ); + } + + public function test_variable_subscriptions_are_bought_through_a_variation(): void { + $this->assertTrue( $this->call( 'is_variable_type', 'variable' ) ); + $this->assertTrue( $this->call( 'is_variable_type', 'variable-subscription' ) ); + $this->assertFalse( $this->call( 'is_variable_type', 'subscription' ) ); + $this->assertFalse( $this->call( 'is_variable_type', 'simple' ) ); + } +} From caaf1ca0014711be155279058c28885350f1bbfb Mon Sep 17 00:00:00 2001 From: Barry Kooij Date: Thu, 15 Oct 2026 19:02:05 +0200 Subject: [PATCH 3/7] Fixed: subscriptions created by WooCommerce checkout tests no longer show in the Subscriptions admin list and its status counts --- src/WooCheckout/AdminFilter.php | 141 ++++++++-- tests/Unit/WooCheckout/AdminFilterTest.php | 283 +++++++++++++++++++++ 2 files changed, 404 insertions(+), 20 deletions(-) create mode 100644 tests/Unit/WooCheckout/AdminFilterTest.php diff --git a/src/WooCheckout/AdminFilter.php b/src/WooCheckout/AdminFilter.php index 48f8048..26dade3 100644 --- a/src/WooCheckout/AdminFilter.php +++ b/src/WooCheckout/AdminFilter.php @@ -1,7 +1,8 @@ > + */ + private static array $probe_counts_cache = []; + /** * Boot hooks. * * @return void */ public static function setup(): void { - // HPOS order list (WC 8.0+). + // HPOS order list (WC 8.0+). This generic filter runs for every order type's + // list table, so it covers the WooCommerce Subscriptions list too. add_filter( 'woocommerce_order_list_table_prepare_items_query_args', [ self::class, 'filter_hpos_query_args' ] ); - // Legacy CPT order list. + // Legacy CPT order and subscription lists. add_action( 'pre_get_posts', [ self::class, 'filter_legacy_orders' ] ); // Status view counts, e.g. "Pending payment (1)". These bypass the list query. add_filter( 'woocommerce_shop_order_list_table_order_count', [ self::class, 'filter_hpos_status_counts' ], 10, 2 ); + add_filter( 'woocommerce_shop_subscription_list_table_order_count', [ self::class, 'filter_hpos_subscription_status_counts' ], 10, 2 ); add_filter( 'wp_count_posts', [ self::class, 'filter_legacy_status_counts' ], 10, 2 ); + + // WooCommerce Subscriptions replaces the HPOS subscription views at priority 10 + // with counts from its own unfiltered query, so correct them afterwards. + add_filter( 'views_woocommerce_page_wc-orders--shop_subscription', [ self::class, 'filter_hpos_subscription_views' ], 20 ); } /** @@ -67,7 +86,7 @@ public static function filter_hpos_query_args( $args ) { } /** - * Filter the legacy CPT order-list query to exclude probe orders. + * Filter the legacy CPT order- and subscription-list query to exclude probes. * * @param \WP_Query $query The query. * @@ -81,7 +100,7 @@ public static function filter_legacy_orders( $query ): void { return; } $post_type = $query->get( 'post_type' ); - if ( 'shop_order' !== $post_type ) { + if ( ! in_array( $post_type, self::ORDER_TYPES, true ) ) { return; } if ( self::should_show_probe_orders() ) { @@ -107,10 +126,35 @@ public static function filter_legacy_orders( $query ): void { * @return int */ public static function filter_hpos_status_counts( $count, $status ) { + return self::subtract_hpos_probe_counts( $count, $status, 'shop_order' ); + } + + /** + * Subtract probe subscriptions from the HPOS subscription status counts. + * + * @param int $count Subscription count for the status(es). + * @param string|string[] $status Status slug(s) being counted. + * + * @return int + */ + public static function filter_hpos_subscription_status_counts( $count, $status ) { + return self::subtract_hpos_probe_counts( $count, $status, 'shop_subscription' ); + } + + /** + * Subtract probes of one order type from an HPOS status count. + * + * @param int $count Count for the status(es). + * @param string|string[] $status Status slug(s) being counted. + * @param string $type Order type. + * + * @return int + */ + private static function subtract_hpos_probe_counts( $count, $status, string $type ) { if ( self::should_show_probe_orders() ) { return $count; } - $probe_counts = self::get_probe_counts_by_status( 'hpos' ); + $probe_counts = self::get_probe_counts_by_status( 'hpos', $type ); foreach ( (array) $status as $slug ) { if ( isset( $probe_counts[ $slug ] ) ) { $count -= $probe_counts[ $slug ]; @@ -120,7 +164,61 @@ public static function filter_hpos_status_counts( $count, $status ) { } /** - * Subtract probe orders from the legacy CPT status view counts (wp_count_posts). + * Subtract probe subscriptions from the HPOS subscription status views. + * + * WooCommerce Subscriptions builds these links itself, each as + * `Label (N)`, and leaves out statuses without + * subscriptions; a status view left with only probes is removed the same way. + * + * @param array $views View links keyed by status slug. + * + * @return array + */ + public static function filter_hpos_subscription_views( $views ) { + if ( ! is_array( $views ) || self::should_show_probe_orders() ) { + return $views; + } + $probe_counts = self::get_probe_counts_by_status( 'hpos', 'shop_subscription' ); + if ( empty( $probe_counts ) ) { + return $views; + } + + foreach ( $views as $slug => $link ) { + if ( 'all' === $slug ) { + // "All" sums the statuses that show in the all list (not trash). + $probe_count = 0; + foreach ( $probe_counts as $status => $status_count ) { + $status_object = get_post_status_object( $status ); + if ( $status_object && $status_object->show_in_admin_all_list ) { + $probe_count += $status_count; + } + } + } else { + $probe_count = $probe_counts[ $slug ] ?? 0; + } + if ( 0 === $probe_count || ! is_string( $link ) ) { + continue; + } + + $remaining = null; + $views[ $slug ] = (string) preg_replace_callback( + '/\(([\d.,\s]+)\)<\/span>/', + static function ( array $matches ) use ( $probe_count, &$remaining ): string { + $remaining = max( 0, (int) preg_replace( '/\D/', '', $matches[1] ) - $probe_count ); + return '(' . $remaining . ')'; + }, + $link, + 1 + ); + if ( 0 === $remaining && 'all' !== $slug ) { + unset( $views[ $slug ] ); + } + } + return $views; + } + + /** + * Subtract probes from the legacy CPT order and subscription status counts (wp_count_posts). * * @param \stdClass $counts Counts keyed by post status. * @param string $type Post type being counted. @@ -128,10 +226,10 @@ public static function filter_hpos_status_counts( $count, $status ) { * @return \stdClass */ public static function filter_legacy_status_counts( $counts, $type ) { - if ( 'shop_order' !== $type || self::should_show_probe_orders() ) { + if ( ! in_array( $type, self::ORDER_TYPES, true ) || self::should_show_probe_orders() ) { return $counts; } - foreach ( self::get_probe_counts_by_status( 'legacy' ) as $slug => $probe_count ) { + foreach ( self::get_probe_counts_by_status( 'legacy', $type ) as $slug => $probe_count ) { if ( isset( $counts->$slug ) ) { $counts->$slug = max( 0, (int) $counts->$slug - $probe_count ); } @@ -140,16 +238,17 @@ public static function filter_legacy_status_counts( $counts, $type ) { } /** - * Count probe orders per status, cached per request. + * Count probes of one order type per status, cached per request. * * @param string $backend Either 'hpos' or 'legacy'. + * @param string $type Order type, 'shop_order' or 'shop_subscription'. * - * @return array Status slug => probe order count. + * @return array Status slug => probe count. */ - private static function get_probe_counts_by_status( string $backend ): array { - static $cache = []; - if ( isset( $cache[ $backend ] ) ) { - return $cache[ $backend ]; + private static function get_probe_counts_by_status( string $backend, string $type ): array { + $cache_key = $backend . ':' . $type; + if ( isset( self::$probe_counts_cache[ $cache_key ] ) ) { + return self::$probe_counts_cache[ $cache_key ]; } global $wpdb; @@ -161,8 +260,9 @@ private static function get_probe_counts_by_status( string $backend ): array { "SELECT o.status AS status, COUNT(*) AS cnt FROM {$wpdb->prefix}wc_orders o INNER JOIN {$wpdb->prefix}wc_orders_meta m ON m.order_id = o.id - WHERE o.type = 'shop_order' AND m.meta_key = %s + WHERE o.type = %s AND m.meta_key = %s GROUP BY o.status", + $type, self::META_KEY ) ); @@ -173,8 +273,9 @@ private static function get_probe_counts_by_status( string $backend ): array { "SELECT p.post_status AS status, COUNT(*) AS cnt FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} m ON m.post_id = p.ID - WHERE p.post_type = 'shop_order' AND m.meta_key = %s + WHERE p.post_type = %s AND m.meta_key = %s GROUP BY p.post_status", + $type, self::META_KEY ) ); @@ -185,7 +286,7 @@ private static function get_probe_counts_by_status( string $backend ): array { $counts[ (string) $row->status ] = (int) $row->cnt; } - $cache[ $backend ] = $counts; + self::$probe_counts_cache[ $cache_key ] = $counts; return $counts; } } diff --git a/tests/Unit/WooCheckout/AdminFilterTest.php b/tests/Unit/WooCheckout/AdminFilterTest.php new file mode 100644 index 0000000..40173ee --- /dev/null +++ b/tests/Unit/WooCheckout/AdminFilterTest.php @@ -0,0 +1,283 @@ +reset_cache(); + unset( $_GET[ AdminFilter::TOGGLE_PARAM ] ); + Functions\when( 'wp_unslash' )->returnArg(); + Functions\when( 'sanitize_text_field' )->returnArg(); + Functions\when( 'is_admin' )->justReturn( true ); + Functions\when( 'get_post_status_object' )->alias( + static fn( $status ) => (object) [ 'show_in_admin_all_list' => 'trash' !== $status ] + ); + } + + protected function tearDown(): void { + // Mockery expectations (once/shouldNotReceive) are the assertions in the + // legacy list query tests; count them so PHPUnit sees them. + $this->addToAssertionCount( Mockery::getContainer()->mockery_getExpectationCount() ); + + $this->reset_cache(); + unset( $_GET[ AdminFilter::TOGGLE_PARAM ], $GLOBALS['wpdb'] ); + parent::tearDown(); + } + + /** + * Clear the per-request probe count cache. + */ + private function reset_cache(): void { + $property = new ReflectionProperty( AdminFilter::class, 'probe_counts_cache' ); + $property->setAccessible( true ); + $property->setValue( null, [] ); + } + + /** + * Install a wpdb double whose probe count query returns the given counts. + * + * @param array $counts Status slug => probe count. + */ + private function probe_counts( array $counts ): void { + $rows = []; + foreach ( $counts as $status => $count ) { + $rows[] = (object) [ + 'status' => $status, + 'cnt' => (string) $count, + ]; + } + + $wpdb = Mockery::mock( 'wpdb' ); + $wpdb->prefix = 'wp_'; + $wpdb->posts = 'wp_posts'; + $wpdb->postmeta = 'wp_postmeta'; + $wpdb->shouldReceive( 'prepare' )->andReturnUsing( + function ( string $sql, string $type, string $meta_key ): string { + $this->counted_types[] = $type; + $this->assertSame( AdminFilter::META_KEY, $meta_key ); + return $sql; + } + ); + $wpdb->shouldReceive( 'get_results' )->andReturn( $rows ); + $GLOBALS['wpdb'] = $wpdb; + } + + /** + * A main admin WP_Query double for a post type. + * + * @param string $post_type Post type being listed. + * + * @return \Mockery\MockInterface&\WP_Query + */ + private function list_query( string $post_type ) { + $query = Mockery::mock( 'WP_Query' ); + $query->shouldReceive( 'is_main_query' )->andReturn( true ); + $query->shouldReceive( 'get' )->with( 'post_type' )->andReturn( $post_type ); + $query->shouldReceive( 'get' )->with( 'meta_query' )->andReturn( '' ); + return $query; + } + + /** + * A subscription status view link as WooCommerce Subscriptions renders it. + * + * @param string $label Status label. + * @param int $count Count shown. + * + * @return string + */ + private function view( string $label, int $count ): string { + return '' . $label . ' (' . $count . ')'; + } + + public function test_setup_hooks_the_subscription_list_and_counts(): void { + AdminFilter::setup(); + + $this->assertNotFalse( has_filter( 'woocommerce_order_list_table_prepare_items_query_args', [ AdminFilter::class, 'filter_hpos_query_args' ] ) ); + $this->assertNotFalse( has_filter( 'woocommerce_shop_subscription_list_table_order_count', [ AdminFilter::class, 'filter_hpos_subscription_status_counts' ] ) ); + $this->assertSame( 20, has_filter( 'views_woocommerce_page_wc-orders--shop_subscription', [ AdminFilter::class, 'filter_hpos_subscription_views' ] ) ); + $this->assertNotFalse( has_action( 'pre_get_posts', [ AdminFilter::class, 'filter_legacy_orders' ] ) ); + $this->assertNotFalse( has_filter( 'wp_count_posts', [ AdminFilter::class, 'filter_legacy_status_counts' ] ) ); + } + + public function test_the_hpos_list_query_excludes_probes(): void { + $args = AdminFilter::filter_hpos_query_args( [ 'type' => 'shop_subscription' ] ); + + $this->assertSame( + [ + [ + 'key' => AdminFilter::META_KEY, + 'compare' => 'NOT EXISTS', + ], + ], + $args['meta_query'] + ); + } + + public function test_the_toggle_keeps_probes_in_the_hpos_list_query(): void { + $_GET[ AdminFilter::TOGGLE_PARAM ] = '1'; + + $this->assertSame( [ 'type' => 'shop_subscription' ], AdminFilter::filter_hpos_query_args( [ 'type' => 'shop_subscription' ] ) ); + } + + /** + * @dataProvider data_order_types + * + * @param string $post_type Post type being listed. + */ + public function test_the_legacy_list_query_excludes_probes( string $post_type ): void { + $query = $this->list_query( $post_type ); + $query->shouldReceive( 'set' )->once()->with( + 'meta_query', + [ + [ + 'key' => AdminFilter::META_KEY, + 'compare' => 'NOT EXISTS', + ], + ] + ); + + AdminFilter::filter_legacy_orders( $query ); + } + + /** + * Order types whose admin lists hide probes. + * + * @return array + */ + public function data_order_types(): array { + return [ + 'orders' => [ 'shop_order' ], + 'subscriptions' => [ 'shop_subscription' ], + ]; + } + + public function test_the_legacy_list_query_leaves_other_post_types_alone(): void { + $query = $this->list_query( 'product' ); + $query->shouldNotReceive( 'set' ); + + AdminFilter::filter_legacy_orders( $query ); + } + + public function test_the_toggle_keeps_probes_in_the_legacy_subscription_list(): void { + $_GET[ AdminFilter::TOGGLE_PARAM ] = '1'; + $query = $this->list_query( 'shop_subscription' ); + $query->shouldNotReceive( 'set' ); + + AdminFilter::filter_legacy_orders( $query ); + } + + public function test_legacy_subscription_counts_leave_out_probes(): void { + $this->probe_counts( [ 'wc-cancelled' => 2 ] ); + $counts = (object) [ + 'wc-active' => 4, + 'wc-cancelled' => 3, + ]; + + $counts = AdminFilter::filter_legacy_status_counts( $counts, 'shop_subscription' ); + + $this->assertSame( 4, $counts->{'wc-active'} ); + $this->assertSame( 1, $counts->{'wc-cancelled'} ); + $this->assertSame( [ 'shop_subscription' ], $this->counted_types ); + } + + public function test_legacy_counts_of_other_post_types_are_untouched(): void { + $counts = (object) [ 'publish' => 5 ]; + + $this->assertSame( 5, AdminFilter::filter_legacy_status_counts( $counts, 'product' )->publish ); + } + + public function test_the_toggle_keeps_probes_in_legacy_subscription_counts(): void { + $_GET[ AdminFilter::TOGGLE_PARAM ] = '1'; + $counts = (object) [ 'wc-cancelled' => 3 ]; + + $this->assertSame( 3, AdminFilter::filter_legacy_status_counts( $counts, 'shop_subscription' )->{'wc-cancelled'} ); + } + + public function test_hpos_subscription_counts_leave_out_probes(): void { + $this->probe_counts( [ 'wc-cancelled' => 2 ] ); + + $this->assertSame( 1, AdminFilter::filter_hpos_subscription_status_counts( 3, [ 'wc-cancelled' ] ) ); + $this->assertSame( 4, AdminFilter::filter_hpos_subscription_status_counts( 4, 'wc-active' ) ); + $this->assertSame( [ 'shop_subscription' ], $this->counted_types ); + } + + public function test_probe_counts_are_kept_apart_per_order_type(): void { + $this->probe_counts( [ 'wc-cancelled' => 1 ] ); + + AdminFilter::filter_hpos_status_counts( 5, 'wc-cancelled' ); + AdminFilter::filter_hpos_subscription_status_counts( 5, 'wc-cancelled' ); + AdminFilter::filter_hpos_subscription_status_counts( 5, 'wc-cancelled' ); + + $this->assertSame( [ 'shop_order', 'shop_subscription' ], $this->counted_types ); + } + + public function test_hpos_subscription_views_leave_out_probes(): void { + $this->probe_counts( + [ + 'wc-cancelled' => 2, + 'wc-on-hold' => 1, + 'trash' => 1, + ] + ); + $views = [ + 'all' => $this->view( 'All', 6 ), + 'wc-active' => $this->view( 'Active', 2 ), + 'wc-cancelled' => $this->view( 'Cancelled', 3 ), + 'wc-on-hold' => $this->view( 'On hold', 1 ), + 'trash' => $this->view( 'Trash', 2 ), + ]; + + $views = AdminFilter::filter_hpos_subscription_views( $views ); + + $this->assertSame( + [ + 'all' => $this->view( 'All', 3 ), + 'wc-active' => $this->view( 'Active', 2 ), + 'wc-cancelled' => $this->view( 'Cancelled', 1 ), + 'trash' => $this->view( 'Trash', 1 ), + ], + $views + ); + } + + public function test_hpos_subscription_views_keep_all_when_only_probes_exist(): void { + $this->probe_counts( [ 'wc-cancelled' => 2 ] ); + $views = [ + 'all' => $this->view( 'All', 2 ), + 'wc-cancelled' => $this->view( 'Cancelled', 2 ), + ]; + + $this->assertSame( [ 'all' => $this->view( 'All', 0 ) ], AdminFilter::filter_hpos_subscription_views( $views ) ); + } + + public function test_the_toggle_keeps_probes_in_hpos_subscription_views(): void { + $_GET[ AdminFilter::TOGGLE_PARAM ] = '1'; + $views = [ 'wc-cancelled' => $this->view( 'Cancelled', 2 ) ]; + + $this->assertSame( $views, AdminFilter::filter_hpos_subscription_views( $views ) ); + } +} From a5458bb21dcc01e93f3fda7df0f1918c0f9e7bfc Mon Sep 17 00:00:00 2001 From: Barry Kooij Date: Thu, 15 Oct 2026 22:54:10 +0200 Subject: [PATCH 4/7] Removed: the test environment configuration from the plugin package --- .distignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.distignore b/.distignore index 9989a46..e8e1e28 100644 --- a/.distignore +++ b/.distignore @@ -10,6 +10,7 @@ /.vscode /.idea /.wp-env.json +/.wp-env.test.json # Tooling and tests /tests From ef4cbd2c8bdbb33e0a884f0e4bf26f9a2de6df9a Mon Sep 17 00:00:00 2001 From: Barry Kooij Date: Fri, 16 Oct 2026 13:45:09 +0200 Subject: [PATCH 5/7] versiom bump to 1.8.1 --- readme.txt | 2 +- scanfully.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/readme.txt b/readme.txt index a0c322b..41faa7d 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://scanfully.com Tags: monitoring, site health, broken links, broken media, activity log Requires at least: 6.0 Tested up to: 7.1.0 -Stable tag: 1.8.0 +Stable tag: 1.8.1 License: GPLv3 or later License URI: http://www.gnu.org/licenses/gpl-3.0.html Requires PHP: 7.4 diff --git a/scanfully.php b/scanfully.php index 4ff45c7..72f804f 100644 --- a/scanfully.php +++ b/scanfully.php @@ -8,7 +8,7 @@ * * @wordpress-plugin * Plugin Name: Scanfully - * Version: 1.8.0 + * Version: 1.8.1 * Plugin URI: https://scanfully.com/wp-plugin * Description: Scanfully is your favorite WordPress performance and health monitoring tool. * Author: Scanfully @@ -68,7 +68,7 @@ function Scanfully(): \Scanfully\Main { // phpcs:ignore WordPress.NamingConventi function () { // meta. define( 'SCANFULLY_PLUGIN_FILE', __FILE__ ); - define( 'SCANFULLY_VERSION', '1.8.0' ); + define( 'SCANFULLY_VERSION', '1.8.1' ); // boot. require __DIR__ . '/vendor/autoload.php'; From f9f2588edf771bb10d06d1a95a7da988f5819a80 Mon Sep 17 00:00:00 2001 From: Barry Kooij Date: Fri, 16 Oct 2026 13:46:18 +0200 Subject: [PATCH 6/7] Added: changelog for 1.8.1 --- readme.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/readme.txt b/readme.txt index 41faa7d..3fd3181 100644 --- a/readme.txt +++ b/readme.txt @@ -206,6 +206,11 @@ Scanfully is designed to keep monitoring work away from frontend page loads. The == Changelog == += 1.8.1: October 16, 2026 = +* Added: WooCommerce checkout monitoring can buy subscription and variable subscription products; subscriptions created by a test order are cancelled right away without sending emails and removed with the test order +* Removed: development-only files from the plugin package +* Fixed: subscriptions created by WooCommerce checkout tests showing in the Subscriptions admin list and its status counts + = 1.8.0: October 13, 2026 = * Added: site data includes each plugin's latest version from WordPress's own update check, so Scanfully can show which plugins have an update * Changed: help center and notification channels links now point to docs.scanfully.com From b535467d3bda098bea0924758e002d3f1af2b6a5 Mon Sep 17 00:00:00 2001 From: Barry Kooij Date: Fri, 16 Oct 2026 14:01:13 +0200 Subject: [PATCH 7/7] Fixed: PHPStan baseline drift check failing on pull requests that add no baseline entries --- .github/workflows/qa.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/qa.yml b/.github/workflows/qa.yml index ee3a50b..3cfb482 100644 --- a/.github/workflows/qa.yml +++ b/.github/workflows/qa.yml @@ -25,13 +25,14 @@ jobs: shell: bash run: | set -euo pipefail + # The full checkout (fetch-depth: 0) already has the base branch. BASE="origin/${{ github.base_ref }}" - git fetch origin "${{ github.base_ref }}" --depth=1 # Count lines added to the baseline that look like a new ignore entry. # Commented lines (such as the example in the file header) are skipped. + # grep exits 1 when nothing matches, which pipefail would turn into a failure. ADDED=$(git diff "$BASE"...HEAD -- phpstan-baseline.neon \ - | grep -E '^\+[[:space:]]*message:' \ + | { grep -E '^\+[[:space:]]*message:' || true; } \ | wc -l \ | tr -d ' ') @@ -43,7 +44,7 @@ jobs: echo "Detected $ADDED new baseline entries; checking for matching BASELINE: blocks..." JUSTIFIED=$(git diff "$BASE"...HEAD -- phpstan-baseline.neon \ - | grep -E '^\+\s*#\s*BASELINE:' \ + | { grep -E '^\+\s*#\s*BASELINE:' || true; } \ | wc -l \ | tr -d ' ')