distributor
distributor copied to clipboard
Support for cloning sites with distributed posts.
Is your enhancement related to a problem? Please describe. In a multisite network when cloning a site that contains distributed posts the original post has no idea a new connection has been made, and as such updates are not being made in the new cloned post.
Describe the solution you'd like I created a plugin that fixes this problem. Basically anytime a new multisite is created it will look for distributed posts in the database, find their original blog ID and post ID, and finally create and insert the new connection in each of the original posts' 'dt_connection_map' meta. It also adds a page in which you can select your new clone and fix its connections.
Additional context I understand this is not a common use of the Distributor plugin. Our client has a main site clone for almost every state, and needed to push and update posts and pages. At the moment this plugin only works while cloning with Multisite Cloner and not with other cloning plugins (such as NS Cloner or MultiSite Clone Duplicator) but the manual fix works fine. Its page is located in Network > Settings > Distributor Map Fixer. At the moment I'm working on adding support for the other cloning plugins which shouldn't take much time.
@ulkish sounds like a good solution to this problem, let us know as you update broader support for cloning plugins, I'm tagging this for review during our 2.0 release cycle for now, thanks!
Distributor is really really good, I love it. But the problem with lost linking when cloning the sites is a rule out criterion for me. I really need a solution for WP Ultimo. I tried to customize your plugin, but unfortunately unsuccessful so far. But I stay tuned...
@ulkish In my opinion, there is a mistake in it. In if ( isset( $dt_connection ) ) { $dt_connection[0]['internal'][$blog_id] = array( the original blog-id must be inserted.
I have slightly modified it with another hook (for cloning via WP Ultimo) into my function and it works so wonderfully.
add_action('mucd_after_copy_data', 'p4u_add_dt_connections', 10, 2 );
function p4u_add_dt_connections( $template_id, $blog_id ) { global $wpdb, $p4u_globals;
switch_to_blog( $blog_id );
// Search through site for distributed posts
$args = array(
'meta_key' => 'dt_original_blog_id',
'post_type' => 'any',
'posts_per_page' => -1,
);
$posts_query = new WP_Query( $args );
$distributed_posts = $posts_query->posts;
// Getting the original blog ID and post ID from every distributed post.
$og_blog_and_post_ids = array();
foreach ( $distributed_posts as $post ) {
$original_blog_id = get_post_meta( $post->ID, 'dt_original_blog_id' )[0];
$original_post_id = get_post_meta( $post->ID, 'dt_original_post_id' )[0];
if ( !isset( $og_blog_and_post_ids[$original_blog_id] ) ) {
$og_blog_and_post_ids[$original_blog_id] = array();
}
array_push( $og_blog_and_post_ids[$original_blog_id],
array(
'og_post_id' => $original_post_id,
'post_id' => $post->ID,
));
}
foreach ( $og_blog_and_post_ids as $blogid => $og_post_id) {
// Switching to original blog to create and add new site connection.
switch_to_blog( $blogid );
foreach ( $og_post_id as $post ) {
$dt_connection = get_post_meta( $post['og_post_id'], 'dt_connection_map' );
if ( isset( $dt_connection ) ) {
$dt_connection[0]['internal'][$blog_id] = array(
'post_id' => $post['post_id'],
'time' => time(),
);
update_post_meta( $post['og_post_id'], 'dt_connection_map', $dt_connection[0] );
}
}
}
restore_current_blog();
return true;
}
I created a plugin with parts of code from ulkish and otte24 which adds support for:
- NS Cloner - Site Copier (https://de.wordpress.org/plugins/ns-cloner-site-copier/)
- MultiSite Clone Duplicator (https://de.wordpress.org/plugins/multisite-clone-duplicator/)
- WP Ultimo
You can find it here: https://github.com/milkycode/mc_wp_mu-dt-clone
But after playing a bit with distributor I found out, that distributor is not working good in all ways. So I am using "Broadcast" WP Plugin now. It has some addons to support 3rd party cloning tools aswell as an own cloning solution (both paid). This woks much better for my cases.