Skip to content

Commit ffec109

Browse files
committed
feat(distributor): synchronize post status
1 parent af03b03 commit ffec109

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

includes/distributor-customizations/class-base.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public static function init() {
2121
add_filter( 'dt_push_post_args', [ __CLASS__, 'filter_push_post_args' ], 10, 2 );
2222
add_filter( 'dt_pull_post_args', [ __CLASS__, 'filter_pull_post_args' ], 10, 3 );
2323
add_filter( 'dt_subscription_post_args', [ __CLASS__, 'filter_subscription_post_args' ], 10, 2 );
24-
add_action( 'dt_process_subscription_attributes', [ __CLASS__, 'fix_primary_category' ], 10, 2 );
25-
add_action( 'dt_process_distributor_attributes', [ __CLASS__, 'fix_primary_category' ], 10, 2 );
24+
add_action( 'dt_process_subscription_attributes', [ __CLASS__, 'process_attributes' ], 10, 2 );
25+
add_action( 'dt_process_distributor_attributes', [ __CLASS__, 'process_attributes' ], 10, 2 );
2626

2727
/**
2828
* This is a workaround the bug fixed in https://github.com/10up/distributor/pull/1185
@@ -54,12 +54,13 @@ private static function get_primary_category_slug( $post_body, $post ) {
5454
}
5555

5656
/**
57-
* Fix primary category on the Node.
57+
* Process distributed post attributes after the distribution has completed.
5858
*
5959
* @param WP_Post $post The post object.
6060
* @param WP_REST_Request $request The request data.
6161
*/
62-
public static function fix_primary_category( $post, $request ) {
62+
public static function process_attributes( $post, $request ) {
63+
// Fix the primary category.
6364
$primary_category_id = get_post_meta( $post->ID, '_yoast_wpseo_primary_category', true );
6465
$primary_category_slug = get_post_meta( $post->ID, 'yoast_primary_category_slug', true );
6566
$hub_primary_category = get_term( $primary_category_id );
@@ -72,6 +73,18 @@ public static function fix_primary_category( $post, $request ) {
7273
} elseif ( class_exists( '\Newspack\Logger' ) ) {
7374
\Newspack\Logger::error( __( 'No matching category found on the Hub site.', 'newspack-network' ) );
7475
}
76+
77+
// Synchronize the post status.
78+
$hub_post_status = get_post_meta( $post->ID, 'post_status', true );
79+
$current_post_status = get_post_status( $post->ID );
80+
if ( $hub_post_status && $hub_post_status !== $current_post_status ) {
81+
wp_update_post(
82+
[
83+
'ID' => $post->ID,
84+
'post_status' => $hub_post_status,
85+
]
86+
);
87+
}
7588
}
7689

7790
/**
@@ -116,6 +129,9 @@ public static function filter_pull_post_args( $post_array, $remote_id, $post ) {
116129
public static function filter_subscription_post_args( $post_body, $post ) {
117130
$slug = self::get_primary_category_slug( $post_body, $post );
118131
$post_body['post_data']['distributor_meta']['yoast_primary_category_slug'] = $slug;
132+
// Attaching the post status only on updates (so not in filter_push_post_args).
133+
// By default, only published posts are distributable, so there's no need to attach the post status on new posts.
134+
$post_body['post_data']['distributor_meta']['post_status'] = $post->post_status;
119135
return $post_body;
120136
}
121137

0 commit comments

Comments
 (0)