Skip to content

Instantly share code, notes, and snippets.

@mishterk
Created December 17, 2020 00:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mishterk/f42b1b57845c56392baa384e2c8b3360 to your computer and use it in GitHub Desktop.
Save mishterk/f42b1b57845c56392baa384e2c8b3360 to your computer and use it in GitHub Desktop.
Post type/taxonomy mirror.
<?php
$mirror = new PostTypeTaxonomyMirror( 'my_post_type', 'my_taxonomy' );
$mirror->init();
<?php
class PostTypeTaxonomyMirror {
private $post_type;
private $taxonomy;
/**
* @param $post_type
* @param $taxonomy
*/
public function __construct( $post_type, $taxonomy ) {
$this->post_type = $post_type;
$this->taxonomy = $taxonomy;
}
public function init() {
$this->update_terms_on_post_update();
$this->delete_terms_on_post_delete();
}
// NOTE: this doesn't appear to be relevant as the posts are 'updated' when they are created in the admin. This might be something we need to investigate for when creating posts via PHP
//private function create_terms_on_post_create() {
// add_action( "save_post_{$this->post_type}", function ( $post_id, WP_Post $post, $updated ) {
// PDS_Debug::log( $updated );
// if ( ! $updated ) {
// $result = wp_insert_term( $post->post_name, $this->taxonomy, [ 'name' => $post->post_title ] );
// //wp_add_object_terms($post_id, $result->)
// PDS_Debug::log( $result );
// }
// }, 10, 3 );
//}
private function update_terms_on_post_update() {
add_action( 'post_updated', function ( $post_id, WP_Post $after, WP_Post $before ) {
if ( get_post_type( $post_id ) !== $this->post_type ) {
return;
}
if ( $this->new_post_created( $after, $before ) ) {
$this->create_new_term_for_post( $after );
} elseif ( $this->post_has_changed( $before, $after ) ) {
if ( $term = $this->get_existing_post_term( $before ) ) {
$this->update_post_term( $term, $after );
} else {
$this->create_new_term_for_post( $after );
}
} elseif ( ! $term = $this->get_existing_post_term( $after ) ) {
$this->create_new_term_for_post( $after );
}
}, 10, 3 );
}
private function delete_terms_on_post_delete() {
add_action( 'delete_post', function ( $post_id ) {
if ( get_post_type( $post_id ) !== $this->post_type ) {
return;
}
if ( $post = get_post( $post_id ) and $term = $this->get_existing_post_term( $post ) ) {
wp_delete_term( $term->term_id, $this->taxonomy );
}
} );
}
private function new_post_created( WP_Post $after, WP_Post $before ) {
return $before and $before->post_status === 'auto-draft';
}
private function create_new_term_for_post( WP_Post $post ) {
$result = wp_insert_term( $post->post_title, $this->taxonomy, [ 'slug' => $post->post_name ] );
if ( ! is_wp_error( $result ) ) {
wp_add_object_terms( $post->ID, $result['term_id'], $this->taxonomy );
}
}
private function post_has_changed( WP_Post $after, WP_Post $before ) {
return ! (
$before->post_name === $after->post_name and
$before->post_title === $after->post_title
);
}
private function get_existing_post_term( WP_Post $post ) {
$term = get_term_by( 'slug', $post->post_name, $this->taxonomy, OBJECT );
return $term instanceof WP_Term
? $term
: false;
}
private function update_post_term( WP_Term $term, WP_Post $post ) {
wp_update_term( $term->term_id, $this->taxonomy, [
'slug' => $post->post_name,
'name' => $post->post_title,
] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment