Skip to content

Commit db1a8f8

Browse files
authored
Merge pull request #63 from happyprime/fix/add-missing-connect-term
Recreate missing connect terms if post is published
2 parents 5d64a05 + e7291f2 commit db1a8f8

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

includes/sync.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ function sync_shadow_taxonomies( int $post_id, \WP_Post $post_after, bool $updat
9191
return;
9292
}
9393

94+
// If a post is already published and not undergoing a status change, but does not
95+
// have an associated term, create one.
96+
if ( 'publish' === $status_before && 'publish' === $status_after && false === $term_after ) {
97+
// In the very unlikely condition that a term _was_ associated, but now is
98+
// lost to the ether, attempt to restore previous post associations.
99+
$existing_associations = (array) get_post_meta( $post_id, "{$taxonomy}_associated_posts", true );
100+
$existing_associations = array_filter( $existing_associations );
101+
102+
$new_term = wp_insert_term( $title_after, $taxonomy );
103+
104+
if ( is_wp_error( $new_term ) ) {
105+
return;
106+
}
107+
108+
foreach ( $existing_associations as $association ) {
109+
wp_set_object_terms( $association, $new_term['term_id'], $taxonomy );
110+
}
111+
112+
return;
113+
}
114+
94115
// If the post transitioned from published to not published, remove the associated term.
95116
if ( 'publish' !== $status_after && $term_before ) {
96117
$associated_posts = get_objects_in_term( $term_before->term_id, $taxonomy );

tests/test-term-sync.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,44 @@ public function test_post_trash_to_publish_creates_term_and_restores_relationshi
539539
$this->assertEquals( array( $term->slug ), $associated_terms, 'Existing shadow term relationships should be restored when its connected post is moved from trash to publish.' );
540540
}
541541

542+
/**
543+
* A published post without a shadow term should have one created on update.
544+
*/
545+
public function test_post_publish_to_publish_creates_missing_term(): void {
546+
$post = $this->factory()->post->create(
547+
array(
548+
'post_type' => 'example',
549+
'post_title' => 'Hazelnut',
550+
'post_status' => 'publish',
551+
)
552+
);
553+
554+
if ( is_wp_error( $post ) ) {
555+
$this->fail( 'Failed to create post.' );
556+
}
557+
558+
$post = get_post( $post );
559+
$term = get_term_by( 'slug', 'hazelnut', 'example_connect', 'OBJECT' );
560+
561+
if ( ! $term ) {
562+
$this->fail( 'Expected term not available.' );
563+
}
564+
565+
// Delete the shadow term to simulate the missing term condition.
566+
wp_delete_term( $term->term_id, 'example_connect' );
567+
568+
$term = get_term_by( 'slug', 'hazelnut', 'example_connect', 'OBJECT' );
569+
$this->assertFalse( $term, 'Shadow term should have been deleted.' );
570+
571+
// Update the post without changing its status.
572+
wp_update_post( $post );
573+
574+
$term = get_term_by( 'name', 'Hazelnut', 'example_connect', 'OBJECT' );
575+
$term_name = ! $term ? '' : $term->name;
576+
577+
$this->assertEquals( 'Hazelnut', $term_name, 'A published post without a shadow term should have one created on update.' );
578+
}
579+
542580
/**
543581
* An existing published post that has its title changed should change the
544582
* title of its shadow term.

0 commit comments

Comments
 (0)