Skip to content

Instantly share code, notes, and snippets.

@Biont
Last active October 3, 2017 09:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Biont/8684a055c945f07295fa to your computer and use it in GitHub Desktop.
Save Biont/8684a055c945f07295fa to your computer and use it in GitHub Desktop.
MLP Autosync Terms
<?php
/**
* Plugin Name: MLP Autosync Terms
* Description: Automatically sync all linked terms when a post is saved
* Author: Biont
* Author URI: https://github.com/Biont
* License: GPLv3
*/
/**
* Class MLP_Sync_Terms
*/
class MLP_Sync_Terms {
/**
* @var array All taxonomies that should be handled by this class
*/
private $allowed_taxonomies = array(
'category'
);
/**
* @var array
*/
private $excluded_post_types = array(
'attachment',
'revision'
);
/**
* Class constructor. Set class variables
*/
public function __construct() {
$this->current_blog_id = get_current_blog_id();
}
/**
* Register hooks
*/
public function register() {
add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), 10, 3 );
}
/**
* Contains the post saving logic to transfer post meta and categories
*
* @wp-hook wp_insert_post
*
* @param $post_ID
* @param \WP_Post $post
* @param $update
*/
public function wp_insert_post( $post_ID, \WP_Post $post, $update ) {
if ( in_array( $post->post_type, $this->excluded_post_types ) ) {
return;
}
/**
* MLP switches blogs and saves remote posts. Check if we're still on the source blog (and dealing with the source post)
*/
if ( $this->current_blog_id !== get_current_blog_id() ) {
return;
}
/**
* Now grab all linked posts. Bail if none are found
*/
$linked_posts = mlp_get_linked_elements( $post_ID, 'post', $this->current_blog_id );
if ( empty( $linked_posts ) ) {
return;
}
foreach ( $linked_posts as $blog_ID => $remote_post_ID ) {
/**
* No need to sync with itself
*/
if ( $blog_ID == $this->current_blog_id ) {
continue;
}
$this->sync_terms(
$this->current_blog_id,
$post_ID,
$blog_ID,
$remote_post_ID
);
}
}
/**
* Transfer terms from one post/blog to another
*
* @param $source_blog_id
* @param $source_post_id
* @param $target_blog_id
* @param $target_post_id
*/
private function sync_terms( $source_blog_id, $source_post_id, $target_blog_id, $target_post_id ) {
$original_blog_id = get_current_blog_id();
if ( $original_blog_id !== $source_blog_id ) {
switch_to_blog( $source_blog_id );
}
$tax_and_terms = array();
foreach ( $this->allowed_taxonomies as $tax ) {
$tax_and_terms[ $tax ] = $this->get_linked_terms( $source_post_id, $tax );
}
switch_to_blog( $target_blog_id );
foreach ( $tax_and_terms as $tax => $linked_terms ) {
if ( empty( $linked_terms ) ) {
continue;
}
$remote_terms = array();
foreach ( $linked_terms as $source_TTID => $remote_TTIDs ) {
if ( empty( $remote_TTIDs[ $target_blog_id ] ) ) {
continue;
}
$remote_terms[] = $remote_TTIDs[ $target_blog_id ];
}
wp_set_object_terms( $target_post_id, $remote_terms, $tax );
}
switch_to_blog( $original_blog_id );
}
/**
* Collects all linked terms for a given taxonomy. The resulting array has the following structure:
*
* array(
* $term_taxonomy_id => array( $blog_id => $remote_term_taxonomy_id )
* )
*
* @param $post_id
* @param $taxonomy
*
* @return array
*/
private function get_linked_terms( $post_id, $taxonomy ) {
$result = array();
$categories = wp_get_post_terms( $post_id, $taxonomy );
foreach ( $categories as $cat ) {
$temp = array();
$temp[ $this->current_blog_id ] = $cat->term_taxonomy_id;
$linked_terms = mlp_get_linked_elements( $cat->term_taxonomy_id, 'term' );
unset( $linked_terms[ $this->current_blog_id ] );
foreach ( $linked_terms as $blog_id => $tt_id ) {
$temp[ $blog_id ] = $tt_id;
}
$result[ $cat->term_taxonomy_id ] = $temp;
}
return $result;
}
}
if ( is_admin() ) {
add_action( 'inpsyde_mlp_loaded', 'mlp_sync_terms_init' );
}
/**
* Initialize this plugin
*/
function mlp_sync_terms_init() {
$mlp_sync_terms = new MLP_Sync_Terms();
$mlp_sync_terms->register();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment