From d77a0dde30fa0aec233e7c14346720f9d7ea395c Mon Sep 17 00:00:00 2001 From: Kader Ibrahim S Date: Fri, 18 Aug 2023 20:01:06 +0530 Subject: [PATCH 1/3] Fixes Knowledge base API (#39808) --- .../Admin/Marketing/MarketingSpecs.php | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/plugins/woocommerce/src/Internal/Admin/Marketing/MarketingSpecs.php b/plugins/woocommerce/src/Internal/Admin/Marketing/MarketingSpecs.php index b80b93a16a4..b61398bb618 100644 --- a/plugins/woocommerce/src/Internal/Admin/Marketing/MarketingSpecs.php +++ b/plugins/woocommerce/src/Internal/Admin/Marketing/MarketingSpecs.php @@ -136,34 +136,41 @@ class MarketingSpecs { /** * Load knowledge base posts from WooCommerce.com * - * @param string|null $category Category of posts to retrieve. + * @param string|null $term Term of posts to retrieve. * @return array */ - public function get_knowledge_base_posts( ?string $category ): array { - $kb_transient = self::KNOWLEDGE_BASE_TRANSIENT; - - $categories = array( - 'marketing' => 1744, - 'coupons' => 25202, + public function get_knowledge_base_posts( ?string $term ): array { + $terms = array( + 'marketing' => array( + 'taxonomy' => 'category', + 'term_id' => 1744, + 'argument' => 'categories', + ), + 'coupons' => array( + 'taxonomy' => 'post_tag', + 'term_id' => 1377, + 'argument' => 'tags', + ) ); - // Default to marketing category (if no category set on the kb component). - if ( ! empty( $category ) && array_key_exists( $category, $categories ) ) { - $category_id = $categories[ $category ]; - $kb_transient = $kb_transient . '_' . strtolower( $category ); - } else { - $category_id = $categories['marketing']; + // Default to the marketing category (if no term is set on the kb component). + if ( empty( $term ) || ! array_key_exists( $term, $terms ) ) { + $term = 'marketing'; } + $term_id = $terms[ $term ]['term_id']; + $argument = $terms[ $term ]['argument']; + $kb_transient = self::KNOWLEDGE_BASE_TRANSIENT . '_' . strtolower( $term ); + $posts = get_transient( $kb_transient ); if ( false === $posts ) { $request_url = add_query_arg( array( - 'categories' => $category_id, - 'page' => 1, - 'per_page' => 8, - '_embed' => 1, + $argument => $term_id, + 'page' => 1, + 'per_page' => 8, + '_embed' => 1, ), 'https://woocommerce.com/wp-json/wp/v2/posts?utm_medium=product' ); From 520bbe4f9c9660b3825a03c2d4641c95f0aa9863 Mon Sep 17 00:00:00 2001 From: Kader Ibrahim S Date: Fri, 18 Aug 2023 20:02:53 +0530 Subject: [PATCH 2/3] Fixes lint errors (#39808) --- .../woocommerce/src/Internal/Admin/Marketing/MarketingSpecs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/woocommerce/src/Internal/Admin/Marketing/MarketingSpecs.php b/plugins/woocommerce/src/Internal/Admin/Marketing/MarketingSpecs.php index b61398bb618..c1938c6acb1 100644 --- a/plugins/woocommerce/src/Internal/Admin/Marketing/MarketingSpecs.php +++ b/plugins/woocommerce/src/Internal/Admin/Marketing/MarketingSpecs.php @@ -150,7 +150,7 @@ class MarketingSpecs { 'taxonomy' => 'post_tag', 'term_id' => 1377, 'argument' => 'tags', - ) + ), ); // Default to the marketing category (if no term is set on the kb component). From 657971e6cad322e310ae47dd2cc5a0141d2694e2 Mon Sep 17 00:00:00 2001 From: github-actions Date: Fri, 18 Aug 2023 15:59:56 +0000 Subject: [PATCH 3/3] Add changefile(s) from automation for the following project(s): woocommerce --- plugins/woocommerce/changelog/fix-knowledge-base-posts-api | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/fix-knowledge-base-posts-api diff --git a/plugins/woocommerce/changelog/fix-knowledge-base-posts-api b/plugins/woocommerce/changelog/fix-knowledge-base-posts-api new file mode 100644 index 00000000000..d651d37f158 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-knowledge-base-posts-api @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Fixes WooCommerce knowledge base API returning empty posts.