From 291ed888d23e19bf578e730295e12c39bf5d49e7 Mon Sep 17 00:00:00 2001 From: Nestor Vera Date: Fri, 23 May 2025 12:32:58 +0200 Subject: [PATCH 1/3] fix: `downloadsRemaining` returns `null` when is of type `string` --- includes/type/object/class-downloadable-item-type.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/type/object/class-downloadable-item-type.php b/includes/type/object/class-downloadable-item-type.php index 3b9583b7..d1ecd630 100644 --- a/includes/type/object/class-downloadable-item-type.php +++ b/includes/type/object/class-downloadable-item-type.php @@ -63,8 +63,8 @@ public static function register() { 'type' => 'Int', 'description' => __( 'Number of times the item can be downloaded.', 'wp-graphql-woocommerce' ), 'resolve' => static function ( $source ) { - return isset( $source['downloads_remaining'] ) && 'integer' === gettype( $source['downloads_remaining'] ) - ? $source['downloads_remaining'] + return isset( $source['downloads_remaining'] ) && is_numeric( $source['downloads_remaining'] ) + ? intval( $source['downloads_remaining'] ) : null; }, ], From 030f949b8c2ca6149f798d26a805b2f85b77f9b0 Mon Sep 17 00:00:00 2001 From: Nestor Vera Date: Fri, 23 May 2025 12:35:16 +0200 Subject: [PATCH 2/3] test: update tests to match parsing of `downloadsRemaining` --- tests/_support/Helper/crud-helpers/customer.php | 4 ++-- tests/_support/Helper/crud-helpers/order.php | 4 ++-- tests/wpunit/DownloadableItemQueriesTest.php | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/_support/Helper/crud-helpers/customer.php b/tests/_support/Helper/crud-helpers/customer.php index ae8f05b7..81ca8d02 100644 --- a/tests/_support/Helper/crud-helpers/customer.php +++ b/tests/_support/Helper/crud-helpers/customer.php @@ -219,8 +219,8 @@ public function print_downloadables( $id ) { 'url' => $item['download_url'], 'accessExpires' => $item['access_expires'], 'downloadId' => $item['download_id'], - 'downloadsRemaining' => isset( $item['downloads_remaining'] ) && 'integer' === gettype( $item['downloads_remaining'] ) - ? $item['downloads_remaining'] + 'downloadsRemaining' => isset( $item['downloads_remaining'] ) && is_numeric( $item['downloads_remaining'] ) + ? intval( $item['downloads_remaining'] ) : null, 'name' => $item['download_name'], 'product' => array( 'databaseId' => $item['product_id'] ), diff --git a/tests/_support/Helper/crud-helpers/order.php b/tests/_support/Helper/crud-helpers/order.php index 01b0fd9c..08e65715 100644 --- a/tests/_support/Helper/crud-helpers/order.php +++ b/tests/_support/Helper/crud-helpers/order.php @@ -456,8 +456,8 @@ public function print_downloadables( $id ) { 'url' => $item['download_url'], 'accessExpires' => $item['access_expires'], 'downloadId' => $item['download_id'], - 'downloadsRemaining' => isset( $item['downloads_remaining'] ) && 'integer' === gettype( $item['downloads_remaining'] ) - ? $item['downloads_remaining'] + 'downloadsRemaining' => isset( $item['downloads_remaining'] ) && is_numeric( $item['downloads_remaining'] ) + ? intval( $item['downloads_remaining'] ) : null, 'name' => $item['download_name'], 'product' => array( 'databaseId' => $item['product_id'] ), diff --git a/tests/wpunit/DownloadableItemQueriesTest.php b/tests/wpunit/DownloadableItemQueriesTest.php index 76bd11b1..992635ed 100644 --- a/tests/wpunit/DownloadableItemQueriesTest.php +++ b/tests/wpunit/DownloadableItemQueriesTest.php @@ -83,8 +83,8 @@ function ( $item ) { $this->expectedField( 'downloadId', $item['download_id'] ), $this->expectedField( 'downloadsRemaining', - isset( $item['downloads_remaining'] ) && 'integer' === gettype( $item['downloads_remaining'] ) - ? $item['downloads_remaining'] + isset( $item['downloads_remaining'] ) && is_numeric( $item['downloads_remaining'] ) + ? intval( $item['downloads_remaining'] ) : static::IS_NULL ), $this->expectedField( 'name', $item['download_name'] ), @@ -360,8 +360,8 @@ function ( $item ) { $this->expectedField( 'downloadId', $item['download_id'] ), $this->expectedField( 'downloadsRemaining', - isset( $item['downloads_remaining'] ) && 'integer' === gettype( $item['downloads_remaining'] ) - ? $item['downloads_remaining'] + isset( $item['downloads_remaining'] ) && is_numeric( $item['downloads_remaining'] ) + ? intval( $item['downloads_remaining'] ) : static::IS_NULL ), $this->expectedField( 'name', $item['download_name'] ), From 1b422627ed0d8e01dea649f4b143e4aeaf261d0e Mon Sep 17 00:00:00 2001 From: Nestor Vera Date: Fri, 23 May 2025 12:36:45 +0200 Subject: [PATCH 3/3] fix: ensure `get_query_args` parses `downloads_remaining` correctly when is type `string` --- .../class-downloadable-item-connection-resolver.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/data/connection/class-downloadable-item-connection-resolver.php b/includes/data/connection/class-downloadable-item-connection-resolver.php index a2149602..54983482 100644 --- a/includes/data/connection/class-downloadable-item-connection-resolver.php +++ b/includes/data/connection/class-downloadable-item-connection-resolver.php @@ -55,8 +55,8 @@ public function get_query_args() { $is_expired = isset( $downloadable_item['access_expires'] ) ? time() > $downloadable_item['access_expires']->getTimestamp() : false; - $downloads_remaining = ( 'integer' === gettype( $downloadable_item['downloads_remaining'] ) ) - ? 0 < $downloadable_item['downloads_remaining'] + $downloads_remaining = ( is_numeric( $downloadable_item['downloads_remaining'] ) ) + ? 0 < intval( $downloadable_item['downloads_remaining'] ) : true; return $active ? ( ! $is_expired && $downloads_remaining ) : ( $is_expired || ! $downloads_remaining ); @@ -79,8 +79,8 @@ public function get_query_args() { $has_downloads_remaining = $where_args['hasDownloadsRemaining']; $query_args['filters'][] = static function ( $downloadable_item ) use ( $has_downloads_remaining ) { - $downloads_remaining = ( 'integer' === gettype( $downloadable_item['downloads_remaining'] ) ) - ? 0 < $downloadable_item['downloads_remaining'] + $downloads_remaining = ( is_numeric( $downloadable_item['downloads_remaining'] ) ) + ? 0 < intval( $downloadable_item['downloads_remaining'] ) : true; return $has_downloads_remaining === $downloads_remaining;