Skip to content

Commit

Permalink
Use post abstraction for pulling network posts.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwilsoncc committed Feb 20, 2023
1 parent d5ea65f commit aea7570
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions includes/classes/InternalConnections/NetworkSiteConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,24 @@ public function pull( $items ) {
$created_posts = array();

foreach ( $items as $item_array ) {
$post = $this->remote_get( [ 'id' => $item_array['remote_post_id'] ] );
$update = false;
$insert_args = array();
if ( ! empty( $item_array['post_status'] ) ) {
$insert_args['post_status'] = $item_array['post_status'];
}
if ( ! empty( $item_array['post_id'] ) ) {
$insert_args['remote_post_id'] = $item_array['post_id'];
$update = true;
}

$post = $this->remote_get( [ 'id' => $item_array['remote_post_id'] ], $insert_args );

if ( is_wp_error( $post ) ) {
$created_posts[] = $post;
continue;
}

$post_props = get_object_vars( $post );
$post_props = $post;
$post_array = array();
$current_blog_id = get_current_blog_id();

Expand All @@ -241,41 +251,20 @@ public function pull( $items ) {
}
}

foreach ( $post_props as $key => $value ) {
$post_array[ $key ] = $value;
}

if ( ! empty( $item_array['post_id'] ) ) {
$post_array['ID'] = $item_array['post_id'];
} else {
unset( $post_array['ID'] );
}

if ( isset( $post_array['post_parent'] ) ) {
unset( $post_array['post_parent'] );
}

if ( ! empty( $item_array['post_status'] ) ) {
$post_array['post_status'] = $item_array['post_status'];
}

add_filter( 'wp_insert_post_data', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'maybe_set_modified_date' ), 10, 2 );

// Filter documented in includes/classes/ExternalConnections/WordPressExternalConnection.php
$new_post_args = Utils\post_args_allow_list( apply_filters( 'dt_pull_post_args', $post_array, $item_array['remote_post_id'], $post, $this ) );
$new_post_id = wp_insert_post( wp_slash( $new_post_args ) );

if ( $update ) {
$new_post_id = wp_update_post( wp_slash( $new_post_args ) );
} else {
$new_post_id = wp_insert_post( wp_slash( $new_post_args ) );
}
remove_filter( 'wp_insert_post_data', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'maybe_set_modified_date' ), 10, 2 );

if ( ! is_wp_error( $new_post_id ) ) {
update_post_meta( $new_post_id, 'dt_original_post_id', (int) $item_array['remote_post_id'] );
update_post_meta( $new_post_id, 'dt_original_blog_id', (int) $this->site->blog_id );
update_post_meta( $new_post_id, 'dt_syndicate_time', time() );
update_post_meta( $new_post_id, 'dt_original_post_url', esc_url_raw( $post->link ) );

if ( ! empty( $post->post_parent ) ) {
update_post_meta( $new_post_id, 'dt_original_post_parent', (int) $post->post_parent );
}

/**
* Allow bypassing of all meta processing.
Expand All @@ -284,15 +273,15 @@ public function pull( $items ) {
*
* @param {bool} true If Distributor should set the post meta.
* @param {int} $new_post_id The newly created post ID.
* @param {array} $post->meta List of meta items attached to the post, formatted by {@link \Distributor\Utils\prepare_meta()}.
* @param {array} $post_meta List of meta items attached to the post, formatted by {@link \Distributor\Utils\prepare_meta()}.
* @param {int} $remote_post_id The original post ID.
* @param {array} $post_array The arguments passed into wp_insert_post.
* @param {NetworkSiteConnection} $this The Distributor connection being pulled from.
*
* @return {bool} If Distributor should set the post meta.
*/
if ( apply_filters( 'dt_pull_post_meta', true, $new_post_id, $post->meta, $item_array['remote_post_id'], $post_array, $this ) ) {
\Distributor\Utils\set_meta( $new_post_id, $post->meta );
if ( apply_filters( 'dt_pull_post_meta', true, $new_post_id, $post['meta'], $item_array['remote_post_id'], $post_array, $this ) ) {
\Distributor\Utils\set_meta( $new_post_id, $post['meta'] );
}

/**
Expand All @@ -302,15 +291,15 @@ public function pull( $items ) {
*
* @param {bool} true If Distributor should set the post terms.
* @param {int} $new_post_id The newly created post ID.
* @param {array} $post->terms List of terms items attached to the post, formatted by {@link \Distributor\Utils\prepare_taxonomy_terms()}.
* @param {array} $post_terms List of terms items attached to the post, formatted by {@link \Distributor\Utils\prepare_taxonomy_terms()}.
* @param {int} $remote_post_id The original post ID.
* @param {array} $post_array The arguments passed into wp_insert_post.
* @param {NetworkSiteConnection} $this The Distributor connection being pulled from.
*
* @return {bool} If Distributor should set the post terms.
*/
if ( apply_filters( 'dt_pull_post_terms', true, $new_post_id, $post->terms, $item_array['remote_post_id'], $post_array, $this ) ) {
\Distributor\Utils\set_taxonomy_terms( $new_post_id, $post->terms );
if ( apply_filters( 'dt_pull_post_terms', true, $new_post_id, $post['terms'], $item_array['remote_post_id'], $post_array, $this ) ) {
\Distributor\Utils\set_taxonomy_terms( $new_post_id, $post['terms'] );
}

/**
Expand All @@ -320,15 +309,15 @@ public function pull( $items ) {
*
* @param {bool} true If Distributor should set the post media.
* @param {int} $new_post_id The newly created post ID.
* @param {array} $post->media List of media items attached to the post, formatted by {@link \Distributor\Utils\prepare_media()}.
* @param {array} $post_media List of media items attached to the post, formatted by {@link \Distributor\Utils\prepare_media()}.
* @param {int} $remote_post_id The original post ID.
* @param {array} $post_array The arguments passed into wp_insert_post.
* @param {NetworkSiteConnection} $this The Distributor connection being pulled from.
*
* @return {bool} If Distributor should set the post media.
*/
if ( apply_filters( 'dt_pull_post_media', true, $new_post_id, $post->media, $item_array['remote_post_id'], $post_array, $this ) ) {
\Distributor\Utils\set_media( $new_post_id, $post->media, [ 'use_filesystem' => true ] );
if ( apply_filters( 'dt_pull_post_media', true, $new_post_id, $post['media'], $item_array['remote_post_id'], $post_array, $this ) ) {
\Distributor\Utils\set_media( $new_post_id, $post['media'], [ 'use_filesystem' => true ] );
};
}

Expand Down Expand Up @@ -467,11 +456,18 @@ public function get_post_types() {
/**
* Remotely get posts so we can list them for pulling
*
* @param array $args Array of args for getting.
* @since 0.8
* @since 2.0.0 Added $new_post_args parameter.
*
* @param array $args Array of args for getting.
* @param array $new_post_args {
* Array of args for creating new post.
*
* @type string $post_status Post status for new post.
* }
* @return array|WP_Post|bool
*/
public function remote_get( $args = array() ) {
public function remote_get( $args = array(), $new_post_args = array() ) {

$id = ( empty( $args['id'] ) ) ? false : $args['id'];

Expand Down Expand Up @@ -556,7 +552,8 @@ public function remote_get( $args = array() ) {
if ( empty( $post ) ) {
$formatted_post = false;
} else {
$formatted_post = Utils\prepare_post( $post );
$dt_post = new DistributorPost( $post );
$formatted_post = $dt_post->to_insert( $new_post_args );
}

restore_current_blog();
Expand Down

0 comments on commit aea7570

Please sign in to comment.