Skip to content

Commit

Permalink
Merge pull request #202 from Automattic/trunk
Browse files Browse the repository at this point in the history
Alpha release Jan 23
  • Loading branch information
chickenn00dle authored Jan 23, 2025
2 parents dc5551c + 06ec18a commit 0b5d50f
Show file tree
Hide file tree
Showing 27 changed files with 1,545 additions and 242 deletions.
2 changes: 2 additions & 0 deletions includes/class-accepted-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Accepted_Actions {
'newspack_network_membership_plan_updated' => 'Membership_Plan_Updated',
'network_post_updated' => 'Network_Post_Updated',
'network_post_deleted' => 'Network_Post_Deleted',
'newspack_network_distributor_migrate_incoming_posts' => 'Distributor_Migrate_Incoming_Posts',
];

/**
Expand All @@ -65,5 +66,6 @@ class Accepted_Actions {
'newspack_network_membership_plan_updated',
'network_post_updated',
'network_post_deleted',
'newspack_network_distributor_migrate_incoming_posts',
];
}
28 changes: 24 additions & 4 deletions includes/class-content-distribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
use Newspack_Network\Content_Distribution\Canonical_Url;
use Newspack_Network\Content_Distribution\Incoming_Post;
use Newspack_Network\Content_Distribution\Outgoing_Post;
use Newspack_Network\Content_Distribution\Distributor_Migrator;
use WP_Post;

/**
* Main class for content distribution
*/
class Content_Distribution {

const PAYLOAD_HASH_META = '_newspack_network_payload_hash';

/**
* Queued network post updates.
*
Expand Down Expand Up @@ -55,8 +59,8 @@ public static function init() {
CLI::init();
API::init();
Editor::init();

Canonical_Url::init();
Distributor_Migrator::init();
}

/**
Expand All @@ -77,6 +81,9 @@ public static function register_data_event_actions() {
* Distribute queued posts.
*/
public static function distribute_queued_posts() {
if ( empty( self::$queued_post_updates ) ) {
return;
}
$post_ids = array_unique( self::$queued_post_updates );
foreach ( $post_ids as $post_id ) {
$post = get_post( $post_id );
Expand All @@ -85,6 +92,7 @@ public static function distribute_queued_posts() {
}
self::distribute_post( $post );
}
self::$queued_post_updates = [];
}

/**
Expand Down Expand Up @@ -177,6 +185,9 @@ public static function handle_post_updated( $post ) {
if ( ! $post ) {
return;
}
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post->ID ) ) {
return;
}
if ( ! self::is_post_distributed( $post ) ) {
return;
}
Expand Down Expand Up @@ -268,6 +279,7 @@ public static function get_reserved_post_meta_keys() {
return array_merge(
$reserved_keys,
[
self::PAYLOAD_HASH_META,
Outgoing_Post::DISTRIBUTED_POST_META,
Incoming_Post::NETWORK_POST_ID_META,
Incoming_Post::PAYLOAD_META,
Expand Down Expand Up @@ -345,11 +357,12 @@ public static function get_distributed_post( $post ) {
/**
* Trigger post distribution.
*
* @param WP_Post|Outgoing_Post|int $post The post object or ID.
* @param WP_Post|Outgoing_Post|int $post The post object or ID.
* @param string $status_on_create The post status on create. Default is draft.
*
* @return void
*/
public static function distribute_post( $post ) {
public static function distribute_post( $post, $status_on_create = 'draft' ) {
if ( ! class_exists( 'Newspack\Data_Events' ) ) {
return;
}
Expand All @@ -359,7 +372,14 @@ public static function distribute_post( $post ) {
$distributed_post = self::get_distributed_post( $post );
}
if ( $distributed_post ) {
Data_Events::dispatch( 'network_post_updated', $distributed_post->get_payload() );
$payload = $distributed_post->get_payload( $status_on_create );
$payload_hash = $distributed_post->get_payload_hash( $payload );
$post = $distributed_post->get_post();
if ( get_post_meta( $post->ID, self::PAYLOAD_HASH_META, true ) === $payload_hash ) {
return;
}
Data_Events::dispatch( 'network_post_updated', $payload );
update_post_meta( $post->ID, self::PAYLOAD_HASH_META, $payload_hash );
}
}
}
2 changes: 1 addition & 1 deletion includes/class-user-manual-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function manual_sync_user( $user_data ) {

return [
'email' => $user_data->user_email,
'role' => array_shift( $user_data->roles ),
'role' => $user_data->roles,
'user_id' => $user_data->ID,
'meta' => $synced_metadata,
'prop' => $synced_props,
Expand Down
72 changes: 63 additions & 9 deletions includes/content-distribution/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

namespace Newspack_Network\Content_Distribution;

use InvalidArgumentException;
use Newspack_Network\Content_Distribution;
use WP_Error;
use WP_REST_Response;
use WP_REST_Server;

/**
* API Class.
Expand All @@ -17,50 +20,101 @@ class API {
/**
* Initialize hooks.
*/
public static function init() {
public static function init(): void {
add_action( 'rest_api_init', [ __CLASS__, 'register_routes' ] );
}

/**
* Register the REST API routes.
*/
public static function register_routes() {
public static function register_routes(): void {
register_rest_route(
'newspack-network/v1',
'/content-distribution/distribute/(?P<post_id>\d+)',
[
'methods' => 'POST',
'callback' => [ __CLASS__, 'distribute' ],
'args' => [
'urls' => [
'urls' => [
'type' => 'array',
'required' => true,
'items' => [
'type' => 'string',
],
],
'status_on_create' => [
'type' => 'string',
'enum' => [ 'draft', 'publish' ],
'default' => 'draft',
],
],
'permission_callback' => function () {
return current_user_can( Admin::CAPABILITY );
},
]
);

register_rest_route(
'newspack-network/v1',
'/content-distribution/unlink/(?P<post_id>\d+)',
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ __CLASS__, 'toggle_unlink' ],
'args' => [
'unlinked' => [
'required' => true,
'type' => 'boolean',
],
],
'permission_callback' => function() {
'permission_callback' => function () {
return current_user_can( Admin::CAPABILITY );
},
]
);
}

/**
* Toggle the unlinked status of an incoming post.
*
* @param \WP_REST_Request $request The REST request object.
*
* @return WP_REST_Response|WP_Error The REST response or error.
*/
public static function toggle_unlink( $request ): WP_REST_Response|WP_Error {
$post_id = $request->get_param( 'post_id' );
$unlinked = $request->get_param( 'unlinked' );

try {
$incoming_post = new Incoming_Post( $post_id );
$incoming_post->set_unlinked( $unlinked );
} catch ( InvalidArgumentException $e ) {
return new WP_Error( 'newspack_network_content_distribution_error', $e->getMessage(), [ 'status' => 400 ] );
}

return rest_ensure_response(
[
'post_id' => $post_id,
'unlinked' => ! $incoming_post->is_linked(),
'status' => 'success',
]
);
}

/**
* Distribute a post to the network.
*
* @param \WP_REST_Request $request The REST request object.
*
* @return \WP_REST_Response|WP_Error The REST response or error.
* @return WP_REST_Response|WP_Error The REST response or error.
*/
public static function distribute( $request ) {
$post_id = $request->get_param( 'post_id' );
$urls = $request->get_param( 'urls' );
$post_id = $request->get_param( 'post_id' );
$urls = $request->get_param( 'urls' );
$status_on_create = $request->get_param( 'status_on_create' );

try {
$outgoing_post = new Outgoing_Post( $post_id );
} catch ( \InvalidArgumentException $e ) {
} catch ( InvalidArgumentException $e ) {
return new WP_Error( 'newspack_network_content_distribution_error', $e->getMessage(), [ 'status' => 400 ] );
}

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

Content_Distribution::distribute_post( $outgoing_post );
Content_Distribution::distribute_post( $outgoing_post, $status_on_create );

return rest_ensure_response( $distribution );
}
Expand Down
Loading

0 comments on commit 0b5d50f

Please sign in to comment.