Skip to content

Instantly share code, notes, and snippets.

@tott
Last active April 24, 2019 16:39
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tott/e9e8e177966b478977f2129ae32aec10 to your computer and use it in GitHub Desktop.
WP-Minions ( https://github.com/tott/WP-Minions/tree/alter-job-data ) for multisite syndication to one site.
<?php
/**
* Plugin Name: WP Minions Post Pusher
* Description: Pushes all published posts to one blog using wp-minions
* Version: 1.0
* Author: Thorsten ott
* Author URI: http://thorsten-ott.de/
* License: GPLv2 or later
*/
if ( ! defined( 'WP_MINIONS_POST_PUSHER_TARGET' ) ) {
// nothing to do for us
return;
}
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
// don't run during import
return;
}
if ( get_current_blog_id() != WP_MINIONS_POST_PUSHER_TARGET ) {
// if we're not on the main site then we hook into post transitions to run our async calls
add_action( 'transition_post_status', 'post_pusher_handle_post', 1000, 3 );
}
function post_pusher_handle_post( $new_status, $old_status, $post ) {
if ( 'publish' != $new_status && 'publish' != $old_status ) {
// we only act on published posts
return;
}
// inject our post data and blog id
add_filter( 'wp_async_task_add_job_data', 'post_pusher_adjust_job_data', 10 );
// schedule async tasks
wp_async_task_add( 'post_pusher_add_post', array( 'new_status' => $new_status, 'old_status' => $old_status, 'post' => $post ) );
// remove injections
remove_filter( 'wp_async_task_add_job_data', 'post_pusher_adjust_job_data', 10 );
}
// manipulate job data before we send it to the queue, eg inject blog id
function post_pusher_adjust_job_data( $job_data ) {
$job_data['args']['original_blog_id'] = $job_data['blog_id'];
$job_data['args']['original_post_id'] = $job_data['args']['post']->ID;
unset( $job_data['args']['post']->ID );
unset( $job_data['args']['post']->guid );
$job_data['args']['post']->post_name .= '-' . pusher_id2alpha( $job_data['blog_id'] ); // we do this to ensure post-name uniqueness
$job_data['blog_id'] = WP_MINIONS_POST_PUSHER_TARGET;
//error_log( var_export( $job_data, true ) );
return $job_data;
}
// run our callback for the worker that creates / updates the posts
add_action( 'post_pusher_add_post', 'post_pusher_add_post_callback' );
function post_pusher_add_post_callback( $args ) {
global $wpdb;
//error_log( 'Executing post callback' . var_export( $args, true ) );
// $args = array( 'new_status' => $new_status, 'old_status' => $old_status, 'post' => $post )
if ( ! isset( $args['post'] ) || ! isset( $args['new_status'] ) || ! isset( $args['old_status'] ) ) {
return 'Missing data ' . var_export( $args, true );
}
$update = false;
$post = $args['post'];
$post = (array) $post;
// currently just checking for the post name, might be more elegant to check for original_*_id postmeta
$check_sql = "SELECT ID FROM $wpdb->posts WHERE post_name = %s LIMIT 1";
$post_ID = $wpdb->get_var( $wpdb->prepare( $check_sql, $post['post_name'] ) );
if ( ! is_wp_error( $post_ID ) ) {
// re-inject post_id to force post updates if a post exists
$post['ID'] = (int) $post_ID;
if ( $args['blog_id'] != get_post_meta( $post_ID, 'original_blog_id', true ) || $args['post_id'] != get_post_meta( $post_ID, 'original_post_id', true ) ) {
error_log( 'Invalid post push : ' . var_export( $args, true ) );
return false;
}
$update = true;
}
// insert / update post
$post_id = wp_insert_post( $post );
// inject post meta to store reference to source
if ( $post_id && false === $update ) {
update_post_meta( $post_id, 'original_blog_id', $args['blog_id'] );
update_post_meta( $post_id, 'original_post_id', $args['post_id'] );
}
}
function pusher_id2alpha( $id ) {
$dictionary = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$dictionary = str_split( $dictionary );
if ( 0 == $id ) {
return $dictionary[0];
}
$result = [];
$base = count( $dictionary );
while ( $id > 0 ) {
$result[] = $dictionary[ ($id % $base) ];
$id = floor( $id / $base );
}
$result = array_reverse( $result );
$string = implode( '', $result );
return str_pad( $string, 4, '0', STR_PAD_LEFT );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment