Skip to content

Commit 0b5d50f

Browse files
Merge pull request #202 from Automattic/trunk
Alpha release Jan 23
2 parents dc5551c + 06ec18a commit 0b5d50f

27 files changed

+1545
-242
lines changed

includes/class-accepted-actions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Accepted_Actions {
4242
'newspack_network_membership_plan_updated' => 'Membership_Plan_Updated',
4343
'network_post_updated' => 'Network_Post_Updated',
4444
'network_post_deleted' => 'Network_Post_Deleted',
45+
'newspack_network_distributor_migrate_incoming_posts' => 'Distributor_Migrate_Incoming_Posts',
4546
];
4647

4748
/**
@@ -65,5 +66,6 @@ class Accepted_Actions {
6566
'newspack_network_membership_plan_updated',
6667
'network_post_updated',
6768
'network_post_deleted',
69+
'newspack_network_distributor_migrate_incoming_posts',
6870
];
6971
}

includes/class-content-distribution.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
use Newspack_Network\Content_Distribution\Canonical_Url;
1616
use Newspack_Network\Content_Distribution\Incoming_Post;
1717
use Newspack_Network\Content_Distribution\Outgoing_Post;
18+
use Newspack_Network\Content_Distribution\Distributor_Migrator;
1819
use WP_Post;
1920

2021
/**
2122
* Main class for content distribution
2223
*/
2324
class Content_Distribution {
25+
26+
const PAYLOAD_HASH_META = '_newspack_network_payload_hash';
27+
2428
/**
2529
* Queued network post updates.
2630
*
@@ -55,8 +59,8 @@ public static function init() {
5559
CLI::init();
5660
API::init();
5761
Editor::init();
58-
5962
Canonical_Url::init();
63+
Distributor_Migrator::init();
6064
}
6165

6266
/**
@@ -77,6 +81,9 @@ public static function register_data_event_actions() {
7781
* Distribute queued posts.
7882
*/
7983
public static function distribute_queued_posts() {
84+
if ( empty( self::$queued_post_updates ) ) {
85+
return;
86+
}
8087
$post_ids = array_unique( self::$queued_post_updates );
8188
foreach ( $post_ids as $post_id ) {
8289
$post = get_post( $post_id );
@@ -85,6 +92,7 @@ public static function distribute_queued_posts() {
8592
}
8693
self::distribute_post( $post );
8794
}
95+
self::$queued_post_updates = [];
8896
}
8997

9098
/**
@@ -177,6 +185,9 @@ public static function handle_post_updated( $post ) {
177185
if ( ! $post ) {
178186
return;
179187
}
188+
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post->ID ) ) {
189+
return;
190+
}
180191
if ( ! self::is_post_distributed( $post ) ) {
181192
return;
182193
}
@@ -268,6 +279,7 @@ public static function get_reserved_post_meta_keys() {
268279
return array_merge(
269280
$reserved_keys,
270281
[
282+
self::PAYLOAD_HASH_META,
271283
Outgoing_Post::DISTRIBUTED_POST_META,
272284
Incoming_Post::NETWORK_POST_ID_META,
273285
Incoming_Post::PAYLOAD_META,
@@ -345,11 +357,12 @@ public static function get_distributed_post( $post ) {
345357
/**
346358
* Trigger post distribution.
347359
*
348-
* @param WP_Post|Outgoing_Post|int $post The post object or ID.
360+
* @param WP_Post|Outgoing_Post|int $post The post object or ID.
361+
* @param string $status_on_create The post status on create. Default is draft.
349362
*
350363
* @return void
351364
*/
352-
public static function distribute_post( $post ) {
365+
public static function distribute_post( $post, $status_on_create = 'draft' ) {
353366
if ( ! class_exists( 'Newspack\Data_Events' ) ) {
354367
return;
355368
}
@@ -359,7 +372,14 @@ public static function distribute_post( $post ) {
359372
$distributed_post = self::get_distributed_post( $post );
360373
}
361374
if ( $distributed_post ) {
362-
Data_Events::dispatch( 'network_post_updated', $distributed_post->get_payload() );
375+
$payload = $distributed_post->get_payload( $status_on_create );
376+
$payload_hash = $distributed_post->get_payload_hash( $payload );
377+
$post = $distributed_post->get_post();
378+
if ( get_post_meta( $post->ID, self::PAYLOAD_HASH_META, true ) === $payload_hash ) {
379+
return;
380+
}
381+
Data_Events::dispatch( 'network_post_updated', $payload );
382+
update_post_meta( $post->ID, self::PAYLOAD_HASH_META, $payload_hash );
363383
}
364384
}
365385
}

includes/class-user-manual-sync.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static function manual_sync_user( $user_data ) {
5959

6060
return [
6161
'email' => $user_data->user_email,
62-
'role' => array_shift( $user_data->roles ),
62+
'role' => $user_data->roles,
6363
'user_id' => $user_data->ID,
6464
'meta' => $synced_metadata,
6565
'prop' => $synced_props,

includes/content-distribution/class-api.php

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
namespace Newspack_Network\Content_Distribution;
99

10+
use InvalidArgumentException;
1011
use Newspack_Network\Content_Distribution;
1112
use WP_Error;
13+
use WP_REST_Response;
14+
use WP_REST_Server;
1215

1316
/**
1417
* API Class.
@@ -17,50 +20,101 @@ class API {
1720
/**
1821
* Initialize hooks.
1922
*/
20-
public static function init() {
23+
public static function init(): void {
2124
add_action( 'rest_api_init', [ __CLASS__, 'register_routes' ] );
2225
}
2326

2427
/**
2528
* Register the REST API routes.
2629
*/
27-
public static function register_routes() {
30+
public static function register_routes(): void {
2831
register_rest_route(
2932
'newspack-network/v1',
3033
'/content-distribution/distribute/(?P<post_id>\d+)',
3134
[
3235
'methods' => 'POST',
3336
'callback' => [ __CLASS__, 'distribute' ],
3437
'args' => [
35-
'urls' => [
38+
'urls' => [
3639
'type' => 'array',
3740
'required' => true,
3841
'items' => [
3942
'type' => 'string',
4043
],
4144
],
45+
'status_on_create' => [
46+
'type' => 'string',
47+
'enum' => [ 'draft', 'publish' ],
48+
'default' => 'draft',
49+
],
50+
],
51+
'permission_callback' => function () {
52+
return current_user_can( Admin::CAPABILITY );
53+
},
54+
]
55+
);
56+
57+
register_rest_route(
58+
'newspack-network/v1',
59+
'/content-distribution/unlink/(?P<post_id>\d+)',
60+
[
61+
'methods' => WP_REST_Server::EDITABLE,
62+
'callback' => [ __CLASS__, 'toggle_unlink' ],
63+
'args' => [
64+
'unlinked' => [
65+
'required' => true,
66+
'type' => 'boolean',
67+
],
4268
],
43-
'permission_callback' => function() {
69+
'permission_callback' => function () {
4470
return current_user_can( Admin::CAPABILITY );
4571
},
4672
]
4773
);
4874
}
4975

76+
/**
77+
* Toggle the unlinked status of an incoming post.
78+
*
79+
* @param \WP_REST_Request $request The REST request object.
80+
*
81+
* @return WP_REST_Response|WP_Error The REST response or error.
82+
*/
83+
public static function toggle_unlink( $request ): WP_REST_Response|WP_Error {
84+
$post_id = $request->get_param( 'post_id' );
85+
$unlinked = $request->get_param( 'unlinked' );
86+
87+
try {
88+
$incoming_post = new Incoming_Post( $post_id );
89+
$incoming_post->set_unlinked( $unlinked );
90+
} catch ( InvalidArgumentException $e ) {
91+
return new WP_Error( 'newspack_network_content_distribution_error', $e->getMessage(), [ 'status' => 400 ] );
92+
}
93+
94+
return rest_ensure_response(
95+
[
96+
'post_id' => $post_id,
97+
'unlinked' => ! $incoming_post->is_linked(),
98+
'status' => 'success',
99+
]
100+
);
101+
}
102+
50103
/**
51104
* Distribute a post to the network.
52105
*
53106
* @param \WP_REST_Request $request The REST request object.
54107
*
55-
* @return \WP_REST_Response|WP_Error The REST response or error.
108+
* @return WP_REST_Response|WP_Error The REST response or error.
56109
*/
57110
public static function distribute( $request ) {
58-
$post_id = $request->get_param( 'post_id' );
59-
$urls = $request->get_param( 'urls' );
111+
$post_id = $request->get_param( 'post_id' );
112+
$urls = $request->get_param( 'urls' );
113+
$status_on_create = $request->get_param( 'status_on_create' );
60114

61115
try {
62116
$outgoing_post = new Outgoing_Post( $post_id );
63-
} catch ( \InvalidArgumentException $e ) {
117+
} catch ( InvalidArgumentException $e ) {
64118
return new WP_Error( 'newspack_network_content_distribution_error', $e->getMessage(), [ 'status' => 400 ] );
65119
}
66120

@@ -70,7 +124,7 @@ public static function distribute( $request ) {
70124
return new WP_Error( 'newspack_network_content_distribution_error', $distribution->get_error_message(), [ 'status' => 400 ] );
71125
}
72126

73-
Content_Distribution::distribute_post( $outgoing_post );
127+
Content_Distribution::distribute_post( $outgoing_post, $status_on_create );
74128

75129
return rest_ensure_response( $distribution );
76130
}

0 commit comments

Comments
 (0)