Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/338: add Util function to fetch primary category #350

Merged
merged 5 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/functions/content-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function get_post_data( $post ) {
'plainText' => wp_strip_all_tags( $content ),
'size' => str_word_count( wp_strip_all_tags( $content ) ),
'allSections' => Utils\get_post_categories_paths( $post->ID ),
'sectionNames' => Utils\get_post_categories( $post->ID ),
'sectionNames' => array( Utils\get_primary_category( $post->ID ) ),
'modifiedAt' => gmdate( \DateTime::RFC3339, strtotime( $post->post_modified_gmt ) ),
'tags' => Utils\get_post_tags( $post ),
'url' => $permalink,
Expand Down
25 changes: 25 additions & 0 deletions includes/functions/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,28 @@ function get_post_categories( $post_id ) {
function get_wp_sophi_versions() {
return 'wp-' . get_bloginfo( 'version' ) . ':plugin-' . SOPHI_WP_VERSION;
}

/**
* Get the primary term name.
*
* @param string $taxonomy Optional. The taxonomy to get the primary term ID for. Defaults to category.
* @param int $post_id Optional. Post to get the primary term ID for.
*
* @return string
*/
function get_primary_category( $post_id = 0, $taxonomy = 'category' ) {
if ( ! function_exists( 'yoast_get_primary_term_id' ) ) {
$post_terms = wp_get_post_terms( $post_id, $taxonomy );

if ( is_array( $post_terms ) && count( $post_terms ) > 0 ) {
return $post_terms[0]->name;
} else {
return '';
}
}

$primary_term_id = yoast_get_primary_term_id( $taxonomy, $post_id );
$primary_category = get_term( $primary_term_id );

return $primary_category->name;
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
bootstrap="tests/bootstrap.php"
backupGlobals="false"
processIsolation="false"
colors="false">
colors="true">
<testsuites>
<testsuite name="Default">
<directory suffix="Tests.php">./tests/phpunit</directory>
Expand Down
63 changes: 63 additions & 0 deletions tests/phpunit/test-tools/Utils_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,67 @@ public function data_provider_embedded_images() {
),
);
}

public function test_get_primary_category__default_category__yoast_deactivated() {
$term_array = array(
(object) array(
'term_id' => 10,
'name' => 'Uncategorized',
)
);

\WP_Mock::userFunction( 'wp_get_post_terms', array(
'times' => 1,
'args' => array( null, 'category' ),
'return' => $term_array,
) );

$term_name = get_primary_category();

$this->assertEquals( $term_name, 'Uncategorized' );
}

public function test_get_primary_category__multiple_categories__yoast_deactivated() {
$term_array = array(
(object) array(
'term_id' => 10,
'name' => 'Science',
),
(object) array(
'term_id' => 10,
'name' => 'Uncategorized',
)
);

\WP_Mock::userFunction( 'wp_get_post_terms', array(
'times' => 1,
'args' => array( null, 'category' ),
'return' => $term_array,
) );

$term_name = get_primary_category();

$this->assertEquals( $term_name, 'Science' );
}

public function test_get_primary_category__default_category__yoast_activated() {
\WP_Mock::userFunction( 'yoast_get_primary_term_id', array(
'times' => 1,
'args' => array( 'category', null ),
'return' => 123,
) );

\WP_Mock::userFunction( 'get_term', array(
'times' => 1,
'args' => array( 123 ),
'return' => (object) array(
'term_id' => 123,
'name' => 'Uncategorized',
),
) );

$term_name = get_primary_category();

$this->assertEquals( $term_name, 'Uncategorized' );
}
}