Last active
January 18, 2019 14:30
-
-
Save gthayer/c839231888fe5de7ed0243f496737a82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Automatically distribute posts using Distributor's external connections. | |
* Only applies to published posts. | |
* | |
* @param [int] $post_id The post's ID. | |
* @return void | |
*/ | |
function auto_distribute( $post_id ) { | |
// Do not run this on unless a post is being updated. | |
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id ) ) { | |
return; | |
} | |
// Do not run if Distributor Connection class is not available. | |
if ( ! class_exists( '\Distributor\Connections' ) ) { | |
return; | |
} | |
// Only run this on published posts. | |
$status = get_post_status( $post_id ); | |
if ( 'publish' !== $status || 'post' !== get_post_type( $post_id ) ) { | |
return; | |
} | |
// If the connection map has already been built, no need to rebuild. | |
$connection_map = get_post_meta( $post_id, 'dt_connection_map', true ); | |
if ( ! empty( $connection_map['external'] ) ) { | |
return; | |
} | |
// Get all available external connections. | |
$external_connections = new \WP_Query( | |
array( | |
'post_type' => 'dt_ext_connection', | |
'fields' => 'ids', | |
'no_found_rows' => true, | |
'posts_per_page' => 100, | |
) | |
); | |
$connection_map = [ | |
'internal' => [], | |
'external' => [], | |
]; | |
// Much of this is heavily borrowed from plugins/distributor/includes/push-ui.php. | |
foreach ( $external_connections->posts as $external_connection_id ) { | |
$external_connection_type = get_post_meta( $external_connection_id, 'dt_external_connection_type', true ); | |
$external_connection_url = get_post_meta( $external_connection_id, 'dt_external_connection_url', true ); | |
$external_connection_auth = get_post_meta( $external_connection_id, 'dt_external_connection_auth', true ); | |
if ( ! empty( $external_connection_type ) && ! empty( $external_connection_url ) ) { | |
$external_connection_class = \Distributor\Connections::factory()->get_registered()[ $external_connection_type ]; | |
$auth_handler = new $external_connection_class::$auth_handler_class( $external_connection_auth ); | |
$external_connection = new $external_connection_class( get_the_title( $external_connection_id ), $external_connection_url, $external_connection_id, $auth_handler ); | |
$push_args = array(); | |
if ( ! empty( $connection_map['external'][ (int) $external_connection_id ] ) && ! empty( $connection_map['external'][ (int) $external_connection_id ]['post_id'] ) ) { | |
$push_args['remote_post_id'] = (int) $connection_map['external'][ (int) $external_connection_id ]['post_id']; | |
} | |
if ( ! empty( $status ) ) { | |
$push_args['post_status'] = $status; | |
} | |
$remote_id = $external_connection->push( intval( $post_id ), $push_args ); | |
/** | |
* Record the external connection id's remote post id for this local post | |
*/ | |
if ( ! is_wp_error( $remote_id ) ) { | |
$connection_map['external'][ (int) $external_connection_id ] = array( | |
'post_id' => (int) $remote_id, | |
'time' => time(), | |
); | |
$external_push_results[ (int) $external_connection_id ] = array( | |
'post_id' => (int) $remote_id, | |
'date' => date( 'F j, Y g:i a' ), | |
'status' => 'success', | |
); | |
} else { | |
$external_push_results[ (int) $external_connection_id ] = array( | |
'post_id' => (int) $remote_id, | |
'date' => date( 'F j, Y g:i a' ), | |
'status' => 'fail', | |
); | |
} | |
} | |
} | |
$connection_map = update_post_meta( $post_id, 'dt_connection_map', $connection_map ); | |
} | |
// Set the priorty later in the process so the syncronize script does not also attempt the distribute. | |
add_action( 'save_post', 'auto_distribute', 25 ); | |
/** | |
* Set the published date on newly distributed posts to the original publish date. | |
* | |
* @param array $post_body args being sent to the REST API. | |
* @param object $post The original post being sent to the external site. | |
* @param object $connection The remote connection's details. | |
* @return array | |
*/ | |
function published_date( $post_body, $post, $connection ) { | |
$post_body['date'] = $post->post_date; | |
return $post_body; | |
} | |
add_filter( 'dt_push_post_args', 'published_date', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment