From 91acf7455f0d32b05e13feefc66dc074494dc5ec Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Tue, 23 May 2023 15:54:56 +0530 Subject: [PATCH 01/13] add: update logic to attach terms correctely to post --- includes/Classifai/Watson/Linker.php | 62 +++++++++++++--------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/includes/Classifai/Watson/Linker.php b/includes/Classifai/Watson/Linker.php index 96bea57cf..50c5ad439 100644 --- a/includes/Classifai/Watson/Linker.php +++ b/includes/Classifai/Watson/Linker.php @@ -33,24 +33,36 @@ class Linker { * @param int $post_id The post to link to. * @param array $output The classification results from Watson NLU. * @param array $options Unused. - + * * @return void */ public function link( $post_id, $output, $options = [] ) { + $all_terms = []; + + $terms = $this->link_categories( $output['categories'] ) if ( ! empty( $output['categories'] ) ) { - $this->link_categories( $post_id, $output['categories'] ); + $all_terms = $terms; } + $terms = $this->link_keywords( $output['keywords'] ); if ( ! empty( $output['keywords'] ) ) { - $this->link_keywords( $post_id, $output['keywords'] ); + $all_terms = array_merge_recursive( $all_terms, $terms ); } + $terms = $this->link_concepts( $output['concepts'] ); if ( ! empty( $output['concepts'] ) ) { - $this->link_concepts( $post_id, $output['concepts'] ); + $all_terms = array_merge_recursive( $all_terms, $terms ); } + $terms = $this->link_entities( $output['entities'] ); if ( ! empty( $output['entities'] ) ) { - $this->link_entities( $post_id, $output['entities'] ); + $all_terms = array_merge_recursive( $all_terms, $terms ); + } + + if ( $all_terms ) { + foreach ( $all_terms as $taxonomy => $terms ) { + wp_set_object_terms( $post_id, $terms, $taxonomy, false ); + } } } @@ -71,11 +83,9 @@ public function link( $post_id, $output, $options = [] ) { * * Eg:- /animals/pets/cats * - * @param int $post_id The id of the post to link * @param array $categories The list of categories to link - * @return void */ - public function link_categories( $post_id, $categories ) { + protected function link_categories( array $categories ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'category' ); @@ -100,21 +110,19 @@ public function link_categories( $post_id, $categories ) { ); if ( ! is_wp_error( $term ) ) { - $parent = intval( $term['term_id'] ); - $terms_to_link[] = intval( $term['term_id'] ); + $parent = (int) $term['term_id']; + $terms_to_link[] = (int) $term['term_id']; } } else { - $parent = intval( $term->term_id ); - $terms_to_link[] = intval( $term->term_id ); + $parent = $term->term_id; + $terms_to_link[] = $term->term_id; } } } } } - if ( ! empty( $terms_to_link ) ) { - wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); - } + return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; } /** @@ -127,11 +135,9 @@ public function link_categories( $post_id, $categories ) { * ... * ] * - * @param int $post_id The post to link to * @param array $keywords NLU returned keywords - * @return void */ - public function link_keywords( $post_id, $keywords ) { + protected function link_keywords( $keywords ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'keyword' ); @@ -153,9 +159,7 @@ public function link_keywords( $post_id, $keywords ) { } } - if ( ! empty( $terms_to_link ) ) { - wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); - } + return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; } /** @@ -168,11 +172,9 @@ public function link_keywords( $post_id, $keywords ) { * ... * ] * - * @param int $post_id The post to link to. * @param array $concepts The NLU returned concepts. - * @return void */ - public function link_concepts( $post_id, $concepts ) { + protected function link_concepts( array $concepts ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'concept' ); @@ -201,9 +203,7 @@ public function link_concepts( $post_id, $concepts ) { } } - if ( ! empty( $terms_to_link ) ) { - wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); - } + return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; } /** @@ -216,11 +216,9 @@ public function link_concepts( $post_id, $concepts ) { * ... * ] * - * @param int $post_id The post to link to * @param array $entities The entities returned by the NLU api - * @return void */ - public function link_entities( $post_id, $entities ) { + protected function link_entities( array $entities ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'entity' ); @@ -260,9 +258,7 @@ public function link_entities( $post_id, $entities ) { } } - if ( ! empty( $terms_to_link ) ) { - wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); - } + return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; } /** From ef8f457499f0aef8b4ad4e47affd1b487a1f11b3 Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Tue, 23 May 2023 15:55:34 +0530 Subject: [PATCH 02/13] format: add missing semicolon --- includes/Classifai/Watson/Linker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Classifai/Watson/Linker.php b/includes/Classifai/Watson/Linker.php index 50c5ad439..7d9672883 100644 --- a/includes/Classifai/Watson/Linker.php +++ b/includes/Classifai/Watson/Linker.php @@ -39,7 +39,7 @@ class Linker { public function link( $post_id, $output, $options = [] ) { $all_terms = []; - $terms = $this->link_categories( $output['categories'] ) + $terms = $this->link_categories( $output['categories'] ); if ( ! empty( $output['categories'] ) ) { $all_terms = $terms; } From 71eaad99a9a2bad44ae0983554f27dcfdc48fe29 Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Tue, 23 May 2023 16:03:23 +0530 Subject: [PATCH 03/13] refactor: make function accessable public --- includes/Classifai/Watson/Linker.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/Classifai/Watson/Linker.php b/includes/Classifai/Watson/Linker.php index 7d9672883..b32fca76f 100644 --- a/includes/Classifai/Watson/Linker.php +++ b/includes/Classifai/Watson/Linker.php @@ -85,7 +85,7 @@ public function link( $post_id, $output, $options = [] ) { * * @param array $categories The list of categories to link */ - protected function link_categories( array $categories ): array { + public function link_categories( array $categories ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'category' ); @@ -137,7 +137,7 @@ protected function link_categories( array $categories ): array { * * @param array $keywords NLU returned keywords */ - protected function link_keywords( $keywords ): array { + public function link_keywords( $keywords ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'keyword' ); @@ -174,7 +174,7 @@ protected function link_keywords( $keywords ): array { * * @param array $concepts The NLU returned concepts. */ - protected function link_concepts( array $concepts ): array { + public function link_concepts( array $concepts ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'concept' ); @@ -218,7 +218,7 @@ protected function link_concepts( array $concepts ): array { * * @param array $entities The entities returned by the NLU api */ - protected function link_entities( array $entities ): array { + public function link_entities( array $entities ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'entity' ); From cb35c80d78dfd6f2880c7b6c6575277b2bedb60d Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Tue, 23 May 2023 16:04:16 +0530 Subject: [PATCH 04/13] fix: remove unnecessary type conversion --- includes/Classifai/Watson/Linker.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/includes/Classifai/Watson/Linker.php b/includes/Classifai/Watson/Linker.php index b32fca76f..6ac7be42f 100644 --- a/includes/Classifai/Watson/Linker.php +++ b/includes/Classifai/Watson/Linker.php @@ -151,10 +151,10 @@ public function link_keywords( $keywords ): array { $term = wp_insert_term( $name, $taxonomy, [] ); if ( ! is_wp_error( $term ) ) { - $terms_to_link[] = intval( $term['term_id'] ); + $terms_to_link[] = (int) $term['term_id']; } } else { - $terms_to_link[] = intval( $term->term_id ); + $terms_to_link[] = $term->term_id; } } } @@ -187,18 +187,18 @@ public function link_concepts( array $concepts ): array { $term = wp_insert_term( $name, $taxonomy, [] ); if ( ! is_wp_error( $term ) ) { - $terms_to_link[] = intval( $term['term_id'] ); + $terms_to_link[] = (int) $term['term_id']; if ( ! empty( $concept['dbpedia_resource'] ) ) { update_term_meta( - intval( $term['term_id'] ), + (int) $term['term_id'], 'dbpedia_resource', $concept['dbpedia_resource'] ); } } } else { - $terms_to_link[] = intval( $term->term_id ); + $terms_to_link[] = $term->term_id; } } } @@ -240,20 +240,20 @@ public function link_entities( array $entities ): array { if ( ! empty( $entity['disambiguation']['dbpedia_resource'] ) ) { update_term_meta( - intval( $term['term_id'] ), + (int) $term['term_id'], 'dbpedia_resource', $entity['disambiguation']['dbpedia_resource'] ); update_term_meta( - intval( $term['term_id'] ), + (int) $term['term_id'], 'type', $entity['type'] ); } } } else { - $terms_to_link[] = intval( $term->term_id ); + $terms_to_link[] = $term->term_id; } } } From de19c577196f7cccdeab918084ea80470d4e61b2 Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Tue, 23 May 2023 16:11:00 +0530 Subject: [PATCH 05/13] fix: allow to set or get terms from functions --- includes/Classifai/Watson/Linker.php | 52 ++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/includes/Classifai/Watson/Linker.php b/includes/Classifai/Watson/Linker.php index 6ac7be42f..275456e27 100644 --- a/includes/Classifai/Watson/Linker.php +++ b/includes/Classifai/Watson/Linker.php @@ -39,22 +39,22 @@ class Linker { public function link( $post_id, $output, $options = [] ) { $all_terms = []; - $terms = $this->link_categories( $output['categories'] ); + $terms = $this->link_categories( $post_id, $output['categories'], true ); if ( ! empty( $output['categories'] ) ) { $all_terms = $terms; } - $terms = $this->link_keywords( $output['keywords'] ); + $terms = $this->link_keywords( $post_id, $output['keywords'], true ); if ( ! empty( $output['keywords'] ) ) { $all_terms = array_merge_recursive( $all_terms, $terms ); } - $terms = $this->link_concepts( $output['concepts'] ); + $terms = $this->link_concepts( $post_id, $output['concepts'], true ); if ( ! empty( $output['concepts'] ) ) { $all_terms = array_merge_recursive( $all_terms, $terms ); } - $terms = $this->link_entities( $output['entities'] ); + $terms = $this->link_entities( $post_id, $output['entities'], true ); if ( ! empty( $output['entities'] ) ) { $all_terms = array_merge_recursive( $all_terms, $terms ); } @@ -83,9 +83,11 @@ public function link( $post_id, $output, $options = [] ) { * * Eg:- /animals/pets/cats * + * @param int $post_id The id of the post to link * @param array $categories The list of categories to link + * @param bool $return_terms Whether to return the terms to link or not */ - public function link_categories( array $categories ): array { + public function link_categories( int $post_id, array $categories, bool $return_terms = false ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'category' ); @@ -101,13 +103,7 @@ public function link_categories( array $categories ): array { $term = get_term_by( 'name', $part, $taxonomy ); if ( false === $term ) { - $term = wp_insert_term( - $part, - $taxonomy, - [ - 'parent' => $parent, - ] - ); + $term = wp_insert_term( $part, $taxonomy, [ 'parent' => $parent ] ); if ( ! is_wp_error( $term ) ) { $parent = (int) $term['term_id']; @@ -122,6 +118,11 @@ public function link_categories( array $categories ): array { } } + if ( ! $return_terms ) { + wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + return []; + } + return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; } @@ -135,9 +136,11 @@ public function link_categories( array $categories ): array { * ... * ] * + * @param int $post_id The id of the post to link * @param array $keywords NLU returned keywords + * @param bool $return_terms Whether to return the terms to link or not */ - public function link_keywords( $keywords ): array { + public function link_keywords( int $post_id, array $keywords, bool $return_terms = false ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'keyword' ); @@ -159,6 +162,11 @@ public function link_keywords( $keywords ): array { } } + if ( ! $return_terms ) { + wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + return []; + } + return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; } @@ -172,9 +180,11 @@ public function link_keywords( $keywords ): array { * ... * ] * + * @param int $post_id The id of the post to link * @param array $concepts The NLU returned concepts. + * @param bool $return_terms Whether to return the terms to link or not */ - public function link_concepts( array $concepts ): array { + public function link_concepts( int $post_id, array $concepts, bool $return_terms = false ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'concept' ); @@ -203,6 +213,11 @@ public function link_concepts( array $concepts ): array { } } + if ( ! $return_terms ) { + wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + return []; + } + return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; } @@ -216,9 +231,11 @@ public function link_concepts( array $concepts ): array { * ... * ] * + * @param int $post_id The id of the post to link * @param array $entities The entities returned by the NLU api + * @param bool $return_terms Whether to return the terms to link or not */ - public function link_entities( array $entities ): array { + public function link_entities( int $post_id, array $entities, bool $return_terms = false ): array { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'entity' ); @@ -258,6 +275,11 @@ public function link_entities( array $entities ): array { } } + if ( ! $return_terms ) { + wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + return []; + } + return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; } From df15edb744c29d0487cc38b727240c6de3859485 Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Tue, 23 May 2023 16:12:24 +0530 Subject: [PATCH 06/13] refactor: do not use intval --- includes/Classifai/Watson/Linker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Classifai/Watson/Linker.php b/includes/Classifai/Watson/Linker.php index 275456e27..78d7a9a5f 100644 --- a/includes/Classifai/Watson/Linker.php +++ b/includes/Classifai/Watson/Linker.php @@ -253,7 +253,7 @@ public function link_entities( int $post_id, array $entities, bool $return_terms $term = wp_insert_term( $name, $taxonomy, [] ); if ( ! is_wp_error( $term ) ) { - $terms_to_link[] = intval( $term['term_id'] ); + $terms_to_link[] = (int) $term['term_id']; if ( ! empty( $entity['disambiguation']['dbpedia_resource'] ) ) { update_term_meta( From 28ecf0fae6da1bf0a7c2a3fe6c437c7aa9266a14 Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Tue, 23 May 2023 16:16:23 +0530 Subject: [PATCH 07/13] refactor: get terms only if relative watson data exist --- includes/Classifai/Watson/Linker.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/Classifai/Watson/Linker.php b/includes/Classifai/Watson/Linker.php index 78d7a9a5f..75933c422 100644 --- a/includes/Classifai/Watson/Linker.php +++ b/includes/Classifai/Watson/Linker.php @@ -39,23 +39,23 @@ class Linker { public function link( $post_id, $output, $options = [] ) { $all_terms = []; - $terms = $this->link_categories( $post_id, $output['categories'], true ); if ( ! empty( $output['categories'] ) ) { + $terms = $this->link_categories( $post_id, $output['categories'], true ); $all_terms = $terms; } - $terms = $this->link_keywords( $post_id, $output['keywords'], true ); if ( ! empty( $output['keywords'] ) ) { + $terms = $this->link_keywords( $post_id, $output['keywords'], true ); $all_terms = array_merge_recursive( $all_terms, $terms ); } - $terms = $this->link_concepts( $post_id, $output['concepts'], true ); if ( ! empty( $output['concepts'] ) ) { + $terms = $this->link_concepts( $post_id, $output['concepts'], true ); $all_terms = array_merge_recursive( $all_terms, $terms ); } - $terms = $this->link_entities( $post_id, $output['entities'], true ); if ( ! empty( $output['entities'] ) ) { + $terms = $this->link_entities( $post_id, $output['entities'], true ); $all_terms = array_merge_recursive( $all_terms, $terms ); } From 8178b94005f35dc9573629d6cba35b75be6c11af Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Thu, 25 May 2023 09:43:52 +0530 Subject: [PATCH 08/13] format: improve code formatting --- includes/Classifai/Watson/Linker.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/includes/Classifai/Watson/Linker.php b/includes/Classifai/Watson/Linker.php index 75933c422..bb2e0d5d2 100644 --- a/includes/Classifai/Watson/Linker.php +++ b/includes/Classifai/Watson/Linker.php @@ -40,22 +40,22 @@ public function link( $post_id, $output, $options = [] ) { $all_terms = []; if ( ! empty( $output['categories'] ) ) { - $terms = $this->link_categories( $post_id, $output['categories'], true ); + $terms = $this->link_categories( $post_id, $output['categories'], true ); $all_terms = $terms; } if ( ! empty( $output['keywords'] ) ) { - $terms = $this->link_keywords( $post_id, $output['keywords'], true ); + $terms = $this->link_keywords( $post_id, $output['keywords'], true ); $all_terms = array_merge_recursive( $all_terms, $terms ); } if ( ! empty( $output['concepts'] ) ) { - $terms = $this->link_concepts( $post_id, $output['concepts'], true ); + $terms = $this->link_concepts( $post_id, $output['concepts'], true ); $all_terms = array_merge_recursive( $all_terms, $terms ); } if ( ! empty( $output['entities'] ) ) { - $terms = $this->link_entities( $post_id, $output['entities'], true ); + $terms = $this->link_entities( $post_id, $output['entities'], true ); $all_terms = array_merge_recursive( $all_terms, $terms ); } @@ -86,6 +86,8 @@ public function link( $post_id, $output, $options = [] ) { * @param int $post_id The id of the post to link * @param array $categories The list of categories to link * @param bool $return_terms Whether to return the terms to link or not + * + * @return array The terms to link */ public function link_categories( int $post_id, array $categories, bool $return_terms = false ): array { $terms_to_link = []; @@ -139,6 +141,8 @@ public function link_categories( int $post_id, array $categories, bool $return_t * @param int $post_id The id of the post to link * @param array $keywords NLU returned keywords * @param bool $return_terms Whether to return the terms to link or not + * + * @return array The terms to link */ public function link_keywords( int $post_id, array $keywords, bool $return_terms = false ): array { $terms_to_link = []; @@ -183,6 +187,8 @@ public function link_keywords( int $post_id, array $keywords, bool $return_terms * @param int $post_id The id of the post to link * @param array $concepts The NLU returned concepts. * @param bool $return_terms Whether to return the terms to link or not + * + * @return array The terms to link */ public function link_concepts( int $post_id, array $concepts, bool $return_terms = false ): array { $terms_to_link = []; @@ -234,6 +240,8 @@ public function link_concepts( int $post_id, array $concepts, bool $return_terms * @param int $post_id The id of the post to link * @param array $entities The entities returned by the NLU api * @param bool $return_terms Whether to return the terms to link or not + * + * @return array The terms to link */ public function link_entities( int $post_id, array $entities, bool $return_terms = false ): array { $terms_to_link = []; From f8467bbc67ca3f390608d142cc2c0392d8a432d7 Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Thu, 25 May 2023 09:58:58 +0530 Subject: [PATCH 09/13] refactor: update logic to whther link terms instead of return result. --- includes/Classifai/Watson/Linker.php | 96 ++++++++++++++++++---------- 1 file changed, 64 insertions(+), 32 deletions(-) diff --git a/includes/Classifai/Watson/Linker.php b/includes/Classifai/Watson/Linker.php index bb2e0d5d2..884aacb79 100644 --- a/includes/Classifai/Watson/Linker.php +++ b/includes/Classifai/Watson/Linker.php @@ -40,22 +40,22 @@ public function link( $post_id, $output, $options = [] ) { $all_terms = []; if ( ! empty( $output['categories'] ) ) { - $terms = $this->link_categories( $post_id, $output['categories'], true ); + $terms = $this->link_categories( $post_id, $output['categories'] ); $all_terms = $terms; } if ( ! empty( $output['keywords'] ) ) { - $terms = $this->link_keywords( $post_id, $output['keywords'], true ); + $terms = $this->link_keywords( $post_id, $output['keywords'] ); $all_terms = array_merge_recursive( $all_terms, $terms ); } if ( ! empty( $output['concepts'] ) ) { - $terms = $this->link_concepts( $post_id, $output['concepts'], true ); + $terms = $this->link_concepts( $post_id, $output['concepts'] ); $all_terms = array_merge_recursive( $all_terms, $terms ); } if ( ! empty( $output['entities'] ) ) { - $terms = $this->link_entities( $post_id, $output['entities'], true ); + $terms = $this->link_entities( $post_id, $output['entities'] ); $all_terms = array_merge_recursive( $all_terms, $terms ); } @@ -83,13 +83,13 @@ public function link( $post_id, $output, $options = [] ) { * * Eg:- /animals/pets/cats * - * @param int $post_id The id of the post to link + * @param int $post_id The id of the post to link. * @param array $categories The list of categories to link - * @param bool $return_terms Whether to return the terms to link or not + * @param bool $link_terms Whether link terms to post or return terms. * - * @return array The terms to link + * @return array|\WP_Error List of the terms to link. WP_Error class object on error. */ - public function link_categories( int $post_id, array $categories, bool $return_terms = false ): array { + public function link_categories( int $post_id, array $categories, bool $link_terms = false ) { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'category' ); @@ -120,12 +120,20 @@ public function link_categories( int $post_id, array $categories, bool $return_t } } - if ( ! $return_terms ) { - wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + // Exit if there are not any term to link. + if ( empty( $terms_to_link ) ) { return []; } - return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; + if ( $link_terms ) { + $result = wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + + if ( is_wp_error( $result ) ) { + return $result; + } + } + + return $terms_to_link; } /** @@ -138,13 +146,13 @@ public function link_categories( int $post_id, array $categories, bool $return_t * ... * ] * - * @param int $post_id The id of the post to link + * @param int $post_id The id of the post to link. * @param array $keywords NLU returned keywords - * @param bool $return_terms Whether to return the terms to link or not + * @param bool $link_terms Whether link terms to post or return terms. * - * @return array The terms to link + * @return array|\WP_Error List of the terms to link. WP_Error class object on error. */ - public function link_keywords( int $post_id, array $keywords, bool $return_terms = false ): array { + public function link_keywords( int $post_id, array $keywords, bool $link_terms = false ) { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'keyword' ); @@ -166,12 +174,20 @@ public function link_keywords( int $post_id, array $keywords, bool $return_terms } } - if ( ! $return_terms ) { - wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + // Exit if there are not any term to link. + if ( empty( $terms_to_link ) ) { return []; } - return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; + if ( $link_terms ) { + $result = wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + + if ( is_wp_error( $result ) ) { + return $result; + } + } + + return $terms_to_link; } /** @@ -184,13 +200,13 @@ public function link_keywords( int $post_id, array $keywords, bool $return_terms * ... * ] * - * @param int $post_id The id of the post to link + * @param int $post_id The id of the post to link. * @param array $concepts The NLU returned concepts. - * @param bool $return_terms Whether to return the terms to link or not + * @param bool $link_terms Whether link terms to post or return terms. * - * @return array The terms to link + * @return array|\WP_Error List of the terms to link. WP_Error class object on error. */ - public function link_concepts( int $post_id, array $concepts, bool $return_terms = false ): array { + public function link_concepts( int $post_id, array $concepts, bool $link_terms = false ) { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'concept' ); @@ -219,12 +235,20 @@ public function link_concepts( int $post_id, array $concepts, bool $return_terms } } - if ( ! $return_terms ) { - wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + // Exit if there are not any term to link. + if ( empty( $terms_to_link ) ) { return []; } - return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; + if ( $link_terms ) { + $result = wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + + if ( is_wp_error( $result ) ) { + return $result; + } + } + + return $terms_to_link; } /** @@ -237,13 +261,13 @@ public function link_concepts( int $post_id, array $concepts, bool $return_terms * ... * ] * - * @param int $post_id The id of the post to link + * @param int $post_id The id of the post to link. * @param array $entities The entities returned by the NLU api - * @param bool $return_terms Whether to return the terms to link or not + * @param bool $link_terms Whether link terms to post or return terms. * - * @return array The terms to link + * @return array|\WP_Error List of the terms to link. WP_Error class object on error. */ - public function link_entities( int $post_id, array $entities, bool $return_terms = false ): array { + public function link_entities( int $post_id, array $entities, bool $link_terms = false ) { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'entity' ); @@ -283,12 +307,20 @@ public function link_entities( int $post_id, array $entities, bool $return_terms } } - if ( ! $return_terms ) { - wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + // Exit if there are not any term to link. + if ( empty( $terms_to_link ) ) { return []; } - return $terms_to_link ? [ $taxonomy => $terms_to_link ] : []; + if ( $link_terms ) { + $result = wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); + + if ( is_wp_error( $result ) ) { + return $result; + } + } + + return $terms_to_link; } /** From 2e467ea00e4cf5cf0e51182856a92a31d7afa84e Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Thu, 25 May 2023 10:44:48 +0530 Subject: [PATCH 10/13] add: return terms with taxonomy --- includes/Classifai/Watson/Linker.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/includes/Classifai/Watson/Linker.php b/includes/Classifai/Watson/Linker.php index 884aacb79..d59712830 100644 --- a/includes/Classifai/Watson/Linker.php +++ b/includes/Classifai/Watson/Linker.php @@ -34,7 +34,7 @@ class Linker { * @param array $output The classification results from Watson NLU. * @param array $options Unused. * - * @return void + * @return array The terms that were linked. */ public function link( $post_id, $output, $options = [] ) { $all_terms = []; @@ -59,11 +59,13 @@ public function link( $post_id, $output, $options = [] ) { $all_terms = array_merge_recursive( $all_terms, $terms ); } - if ( $all_terms ) { + if ( ! empty( $all_terms ) ) { foreach ( $all_terms as $taxonomy => $terms ) { wp_set_object_terms( $post_id, $terms, $taxonomy, false ); } } + + return $all_terms; } /* helpers */ @@ -133,7 +135,7 @@ public function link_categories( int $post_id, array $categories, bool $link_ter } } - return $terms_to_link; + return [ $taxonomy => $terms_to_link ]; } /** @@ -187,7 +189,7 @@ public function link_keywords( int $post_id, array $keywords, bool $link_terms = } } - return $terms_to_link; + return [ $taxonomy => $terms_to_link ]; } /** @@ -248,7 +250,7 @@ public function link_concepts( int $post_id, array $concepts, bool $link_terms = } } - return $terms_to_link; + return [ $taxonomy => $terms_to_link ]; } /** @@ -320,7 +322,7 @@ public function link_entities( int $post_id, array $entities, bool $link_terms = } } - return $terms_to_link; + return [ $taxonomy => $terms_to_link ]; } /** From 4ec6c7e07cb26a8a638a3a7d34ba71dba464e98c Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Thu, 25 May 2023 10:45:13 +0530 Subject: [PATCH 11/13] tests: fix broken tests --- tests/Classifai/PostClassifierTest.php | 1 + tests/Classifai/Watson/LinkerTest.php | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/Classifai/PostClassifierTest.php b/tests/Classifai/PostClassifierTest.php index 424c0a1cb..9e1f420d6 100644 --- a/tests/Classifai/PostClassifierTest.php +++ b/tests/Classifai/PostClassifierTest.php @@ -6,6 +6,7 @@ class PostClassifierTest extends \WP_UnitTestCase { + /* @var PostClassifier $classifier */ public $classifier; function set_up() { diff --git a/tests/Classifai/Watson/LinkerTest.php b/tests/Classifai/Watson/LinkerTest.php index c26dbcbca..0b858b15e 100644 --- a/tests/Classifai/Watson/LinkerTest.php +++ b/tests/Classifai/Watson/LinkerTest.php @@ -4,6 +4,7 @@ class LinkerTest extends \WP_UnitTestCase { + /* @var Linker $linker */ public $linker; function set_up() { @@ -25,7 +26,7 @@ function test_it_can_link_nlu_categories() { ]; $post_id = $this->factory->post->create(); - $this->linker->link_categories( $post_id, $categories ); + $this->linker->link_categories( $post_id, $categories, true ); $actual = wp_get_object_terms( $post_id, [ WATSON_CATEGORY_TAXONOMY ] ); $actual = array_map( function( $term ) { @@ -48,7 +49,7 @@ function test_it_can_link_nlu_keywords() { ]; $post_id = $this->factory->post->create(); - $this->linker->link_keywords( $post_id, $keywords ); + $this->linker->link_keywords( $post_id, $keywords, true ); $actual = wp_get_object_terms( $post_id, [ WATSON_KEYWORD_TAXONOMY ] ); $actual = array_map( function( $term ) { @@ -73,7 +74,7 @@ function test_it_can_link_nlu_concepts() { ]; $post_id = $this->factory->post->create(); - $this->linker->link_concepts( $post_id, $concepts ); + $this->linker->link_concepts( $post_id, $concepts, true ); $actual = wp_get_object_terms( $post_id, [ WATSON_CONCEPT_TAXONOMY ] ); $actual = array_map( function( $term ) { @@ -106,7 +107,7 @@ function test_it_can_link_nlu_entities() { ]; $post_id = $this->factory->post->create(); - $this->linker->link_entities( $post_id, $entities ); + $this->linker->link_entities( $post_id, $entities, true ); $actual = wp_get_object_terms( $post_id, [ WATSON_ENTITY_TAXONOMY ] ); $actual = array_map( function( $term ) { @@ -175,7 +176,7 @@ function test_it_can_link_nlu_features() { $post_id = $this->factory->post->create(); - $this->linker->link( $post_id, $output ); + $result = $this->linker->link( $post_id, $output ); // categories $actual = wp_get_object_terms( $post_id, [ WATSON_CATEGORY_TAXONOMY ] ); From 83daef42733ef67b14786f2057771e7be498f041 Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Thu, 25 May 2023 11:35:28 +0530 Subject: [PATCH 12/13] tests: remove unused param --- tests/Classifai/Watson/LinkerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Classifai/Watson/LinkerTest.php b/tests/Classifai/Watson/LinkerTest.php index 0b858b15e..0380028de 100644 --- a/tests/Classifai/Watson/LinkerTest.php +++ b/tests/Classifai/Watson/LinkerTest.php @@ -176,7 +176,7 @@ function test_it_can_link_nlu_features() { $post_id = $this->factory->post->create(); - $result = $this->linker->link( $post_id, $output ); + $this->linker->link( $post_id, $output ); // categories $actual = wp_get_object_terms( $post_id, [ WATSON_CATEGORY_TAXONOMY ] ); From 5b34d69e9bcbed87f6fa8279c6a96238bd02eee9 Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Fri, 26 May 2023 17:45:39 +0530 Subject: [PATCH 13/13] refactor: link terms by default --- includes/Classifai/Watson/Linker.php | 32 +++++++++++++-------------- tests/Classifai/Watson/LinkerTest.php | 8 +++---- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/includes/Classifai/Watson/Linker.php b/includes/Classifai/Watson/Linker.php index d59712830..c1b6dc5a7 100644 --- a/includes/Classifai/Watson/Linker.php +++ b/includes/Classifai/Watson/Linker.php @@ -40,22 +40,22 @@ public function link( $post_id, $output, $options = [] ) { $all_terms = []; if ( ! empty( $output['categories'] ) ) { - $terms = $this->link_categories( $post_id, $output['categories'] ); + $terms = $this->link_categories( $post_id, $output['categories'], false ); $all_terms = $terms; } if ( ! empty( $output['keywords'] ) ) { - $terms = $this->link_keywords( $post_id, $output['keywords'] ); + $terms = $this->link_keywords( $post_id, $output['keywords'], false ); $all_terms = array_merge_recursive( $all_terms, $terms ); } if ( ! empty( $output['concepts'] ) ) { - $terms = $this->link_concepts( $post_id, $output['concepts'] ); + $terms = $this->link_concepts( $post_id, $output['concepts'], false ); $all_terms = array_merge_recursive( $all_terms, $terms ); } if ( ! empty( $output['entities'] ) ) { - $terms = $this->link_entities( $post_id, $output['entities'] ); + $terms = $this->link_entities( $post_id, $output['entities'], false ); $all_terms = array_merge_recursive( $all_terms, $terms ); } @@ -87,11 +87,11 @@ public function link( $post_id, $output, $options = [] ) { * * @param int $post_id The id of the post to link. * @param array $categories The list of categories to link - * @param bool $link_terms Whether link terms to post or return terms. + * @param bool $link_categories Whether link categories to post or return array of term ids. * * @return array|\WP_Error List of the terms to link. WP_Error class object on error. */ - public function link_categories( int $post_id, array $categories, bool $link_terms = false ) { + public function link_categories( int $post_id, array $categories, bool $link_categories = true ) { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'category' ); @@ -127,7 +127,7 @@ public function link_categories( int $post_id, array $categories, bool $link_ter return []; } - if ( $link_terms ) { + if ( $link_categories ) { $result = wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); if ( is_wp_error( $result ) ) { @@ -150,11 +150,11 @@ public function link_categories( int $post_id, array $categories, bool $link_ter * * @param int $post_id The id of the post to link. * @param array $keywords NLU returned keywords - * @param bool $link_terms Whether link terms to post or return terms. + * @param bool $link_keywords Whether link keywords to post or return array of term ids. * * @return array|\WP_Error List of the terms to link. WP_Error class object on error. */ - public function link_keywords( int $post_id, array $keywords, bool $link_terms = false ) { + public function link_keywords( int $post_id, array $keywords, bool $link_keywords = true ) { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'keyword' ); @@ -181,7 +181,7 @@ public function link_keywords( int $post_id, array $keywords, bool $link_terms = return []; } - if ( $link_terms ) { + if ( $link_keywords ) { $result = wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); if ( is_wp_error( $result ) ) { @@ -204,11 +204,11 @@ public function link_keywords( int $post_id, array $keywords, bool $link_terms = * * @param int $post_id The id of the post to link. * @param array $concepts The NLU returned concepts. - * @param bool $link_terms Whether link terms to post or return terms. + * @param bool $link_concepts Whether link concepts to post or return array of term ids. * * @return array|\WP_Error List of the terms to link. WP_Error class object on error. */ - public function link_concepts( int $post_id, array $concepts, bool $link_terms = false ) { + public function link_concepts( int $post_id, array $concepts, bool $link_concepts = true ) { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'concept' ); @@ -242,7 +242,7 @@ public function link_concepts( int $post_id, array $concepts, bool $link_terms = return []; } - if ( $link_terms ) { + if ( $link_concepts ) { $result = wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); if ( is_wp_error( $result ) ) { @@ -265,11 +265,11 @@ public function link_concepts( int $post_id, array $concepts, bool $link_terms = * * @param int $post_id The id of the post to link. * @param array $entities The entities returned by the NLU api - * @param bool $link_terms Whether link terms to post or return terms. + * @param bool $link_entities Whether link entities to post or return array of term ids. * * @return array|\WP_Error List of the terms to link. WP_Error class object on error. */ - public function link_entities( int $post_id, array $entities, bool $link_terms = false ) { + public function link_entities( int $post_id, array $entities, bool $link_entities = true ) { $terms_to_link = []; $taxonomy = \Classifai\get_feature_taxonomy( 'entity' ); @@ -314,7 +314,7 @@ public function link_entities( int $post_id, array $entities, bool $link_terms = return []; } - if ( $link_terms ) { + if ( $link_entities ) { $result = wp_set_object_terms( $post_id, $terms_to_link, $taxonomy, false ); if ( is_wp_error( $result ) ) { diff --git a/tests/Classifai/Watson/LinkerTest.php b/tests/Classifai/Watson/LinkerTest.php index 0380028de..4c3a168eb 100644 --- a/tests/Classifai/Watson/LinkerTest.php +++ b/tests/Classifai/Watson/LinkerTest.php @@ -26,7 +26,7 @@ function test_it_can_link_nlu_categories() { ]; $post_id = $this->factory->post->create(); - $this->linker->link_categories( $post_id, $categories, true ); + $this->linker->link_categories( $post_id, $categories ); $actual = wp_get_object_terms( $post_id, [ WATSON_CATEGORY_TAXONOMY ] ); $actual = array_map( function( $term ) { @@ -49,7 +49,7 @@ function test_it_can_link_nlu_keywords() { ]; $post_id = $this->factory->post->create(); - $this->linker->link_keywords( $post_id, $keywords, true ); + $this->linker->link_keywords( $post_id, $keywords ); $actual = wp_get_object_terms( $post_id, [ WATSON_KEYWORD_TAXONOMY ] ); $actual = array_map( function( $term ) { @@ -74,7 +74,7 @@ function test_it_can_link_nlu_concepts() { ]; $post_id = $this->factory->post->create(); - $this->linker->link_concepts( $post_id, $concepts, true ); + $this->linker->link_concepts( $post_id, $concepts ); $actual = wp_get_object_terms( $post_id, [ WATSON_CONCEPT_TAXONOMY ] ); $actual = array_map( function( $term ) { @@ -107,7 +107,7 @@ function test_it_can_link_nlu_entities() { ]; $post_id = $this->factory->post->create(); - $this->linker->link_entities( $post_id, $entities, true ); + $this->linker->link_entities( $post_id, $entities ); $actual = wp_get_object_terms( $post_id, [ WATSON_ENTITY_TAXONOMY ] ); $actual = array_map( function( $term ) {