Skip to content

Commit

Permalink
Internal distribution should prepare posts the same for push or pull
Browse files Browse the repository at this point in the history
  • Loading branch information
rmarscher committed Aug 4, 2018
1 parent 1023600 commit ad7f3c1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
26 changes: 7 additions & 19 deletions includes/classes/InternalConnections/NetworkSiteConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ public function push( $post_id, $args = array() ) {
'post_name' => $post->post_name,
);

$media = \Distributor\Utils\prepare_media( $post_id );
$terms = \Distributor\Utils\prepare_taxonomy_terms( $post_id );
$meta = \Distributor\Utils\prepare_meta( $post_id );
$post = \Distributor\Utils\prepare_post( $post );

switch_to_blog( $this->site->blog_id );

Expand Down Expand Up @@ -109,8 +107,8 @@ public function push( $post_id, $args = array() ) {
update_post_meta( $new_post_id, 'dt_syndicate_time', time() );
update_post_meta( $new_post_id, 'dt_original_post_url', esc_url_raw( $original_post_url ) );

\Distributor\Utils\set_meta( $new_post_id, $meta );
\Distributor\Utils\set_taxonomy_terms( $new_post_id, $terms );
\Distributor\Utils\set_meta( $new_post_id, $post->meta );
\Distributor\Utils\set_taxonomy_terms( $new_post_id, $post->terms );

/**
* Allow plugins to override the default {@see \Distributor\Utils\set_media()} function.
Expand All @@ -122,8 +120,8 @@ public function push( $post_id, $args = array() ) {
* @param array $args The arguments passed into wp_insert_post.
* @param ExternalConnection $this The distributor connection being pushed to.
*/
if ( apply_filters( 'dt_push_post_media', true, $new_post_id, $media, $post_id, $args, $this ) ) {
\Distributor\Utils\set_media( $new_post_id, $media );
if ( apply_filters( 'dt_push_post_media', true, $new_post_id, $post->media, $post_id, $args, $this ) ) {
\Distributor\Utils\set_media( $new_post_id, $post->media );
};
}

Expand Down Expand Up @@ -352,12 +350,7 @@ public function remote_get( $args = array() ) {
$formatted_posts = [];

foreach ( $posts as $post ) {
$post->link = get_permalink( $post->ID );
$post->meta = \Distributor\Utils\prepare_meta( $post->ID );
$post->terms = \Distributor\Utils\prepare_taxonomy_terms( $post->ID );
$post->media = \Distributor\Utils\prepare_media( $post->ID );

$formatted_posts[] = $post;
$formatted_posts[] = \Distributor\Utils\prepare_post( $post );
}

restore_current_blog();
Expand All @@ -375,12 +368,7 @@ public function remote_get( $args = array() ) {
if ( empty( $post ) ) {
$formatted_post = false;
} else {
$post->link = get_permalink( $id );
$post->meta = \Distributor\Utils\prepare_meta( $id );
$post->terms = \Distributor\Utils\prepare_taxonomy_terms( $id );
$post->media = \Distributor\Utils\prepare_media( $id );

$formatted_post = $post;
$formatted_post = \Distributor\Utils\prepare_post( $post );
}

restore_current_blog();
Expand Down
16 changes: 16 additions & 0 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,19 @@ function process_media( $url, $post_id ) {
// Do the validation and storage stuff.
return media_handle_sideload( $file_array, $post_id );
}

/**
* Setup additional properties on a post object to enable them to be
* fetched once and manipulated by filters.
*
* @param WP_Post $post WP_Post object.
* @since 1.2.2
* @return WP_Post
*/
function prepare_post($post) {
$post->link = get_permalink( $post->ID );
$post->meta = prepare_meta( $post->ID );
$post->terms = prepare_taxonomy_terms( $post->ID );
$post->media = prepare_media( $post->ID );
return $post;
}
3 changes: 3 additions & 0 deletions tests/php/NetworkSiteConnectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function test_push() {
\WP_Mock::userFunction(
'get_post', [
'return' => (object) [
'ID' => 111,
'post_content' => '',
'post_excerpt' => '',
'post_type' => '',
Expand Down Expand Up @@ -141,6 +142,7 @@ public function test_pull() {
\WP_Mock::userFunction(
'get_post', [
'return' => (object) [
'ID' => 111,
'post_tite' => 'My post title',
'meta' => [],
],
Expand Down Expand Up @@ -267,6 +269,7 @@ public function test_remote_get() {
\WP_Mock::userFunction(
'get_post', [
'return' => (object) [
'ID' => 111,
'post_title' => 'my title',
],
]
Expand Down

0 comments on commit ad7f3c1

Please sign in to comment.