Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync empty taxonomies to distributed posts. #951

Merged
merged 9 commits into from
Oct 20, 2022
5 changes: 5 additions & 0 deletions includes/classes/API/SubscriptionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ public function receive_item( $request ) {
'media' => ( isset( $request['post_data']['distributor_media'] ) ) ? $request['post_data']['distributor_media'] : [],
];

// Limit taxonomy updates to those shown in the REST API.
$rest_taxonomies = get_taxonomies( [ 'show_in_rest' => true ] );
$rest_taxonomies = array_fill_keys( $rest_taxonomies, true );
$update['terms'] = array_intersect_key( $update['terms'], $rest_taxonomies );

update_post_meta( (int) $request['post_id'], 'dt_subscription_update', $update );

$unlinked = (bool) get_post_meta( $request['post_id'], 'dt_unlinked', true );
Expand Down
61 changes: 35 additions & 26 deletions includes/subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ function send_notifications( $post ) {
'content' => Utils\get_processed_content( $post->post_content ),
'excerpt' => $post->post_excerpt,
'distributor_media' => \Distributor\Utils\prepare_media( $post_id ),
'distributor_terms' => \Distributor\Utils\prepare_taxonomy_terms( $post_id ),
'distributor_terms' => \Distributor\Utils\prepare_taxonomy_terms( $post_id, array( 'show_in_rest' => true ) ),
'distributor_meta' => \Distributor\Utils\prepare_meta( $post_id ),
],
];
Expand All @@ -284,33 +284,42 @@ function send_notifications( $post ) {
}
}

/**
* Filter the timeout used when calling `\Distributor\Subscriptions\send_notifications`
*
* @hook dt_subscription_post_timeout
*
* @param {int} $timeout The timeout to use for the remote post. Default `5`.
* @param {WP_Post} $post The post object
*
* @return {int} The timeout to use for the remote post.
*/
$request_timeout = apply_filters( 'dt_subscription_post_timeout', 5, $post );

/**
* Filter the arguments sent to the remote server during a subscription update.
*
* @since 1.3.0
* @hook dt_subscription_post_args
*
* @param {array} $post_body The request body to send.
* @param {WP_Post} $post The WP_Post that is being pushed.
*
* @return {array} The request body to send.
*/
$post_body = apply_filters( 'dt_subscription_post_args', $post_body, $post );

$post_arguments = [
'timeout' => $request_timeout,
'body' => wp_json_encode( $post_body ),
'headers' => [
'Content-Type' => 'application/json',
],
];

$request = wp_remote_post(
untrailingslashit( $target_url ) . '/wp/v2/dt_subscription/receive',
[
/**
* Filter the timeout used when calling `\Distributor\Subscriptions\send_notifications`
*
* @hook dt_subscription_post_timeout
*
* @param {int} $timeout The timeout to use for the remote post. Default `5`.
* @param {WP_Post} $post The post object
*
* @return {int} The timeout to use for the remote post.
*/
'timeout' => apply_filters( 'dt_subscription_post_timeout', 5, $post ),
/**
* Filter the arguments sent to the remote server during a subscription update.
*
* @since 1.3.0
* @hook dt_subscription_post_args
*
* @param {array} $post_body The request body to send.
* @param {WP_Post} $post The WP_Post that is being pushed.
*
* @return {array} The request body to send.
*/
'body' => apply_filters( 'dt_subscription_post_args', $post_body, $post ),
]
$post_arguments
);

if ( ! is_wp_error( $request ) ) {
Expand Down
14 changes: 10 additions & 4 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,15 +438,21 @@ function prepare_media( $post_id ) {
/**
* Format taxonomy terms for consumption
*
* @param int $post_id Post ID.
* @since 1.0
* @return array
*
* @param int $post_id Post ID.
* @param array $args Taxonomy query arguments. See get_taxonomies().
* @return array[] Array of taxonomy terms.
*/
function prepare_taxonomy_terms( $post_id ) {
function prepare_taxonomy_terms( $post_id, $args = array() ) {
$post = get_post( $post_id );

if ( empty( $args ) ) {
$args = array( 'publicly_queryable' => true );
}

$taxonomy_terms = [];
$taxonomies = get_object_taxonomies( $post );
$taxonomies = get_taxonomies( $args );

/**
* Filters the taxonomies that should be synced.
Expand Down
32 changes: 19 additions & 13 deletions tests/php/SubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,19 +359,22 @@ public function test_send_notifications_no_remote_post() {
$target_url . '/wp/v2/dt_subscription/receive',
[
'timeout' => 5,
'body' => [
'body' => wp_json_encode( [
'post_id' => $remote_post_id,
'signature' => $signature,
'post_data' => [
'title' => 'title',
'slug' => 'slug',
'post_type' => 'post',
'content' => 'content',
'excerpt' => 'excerpt',
'post_type' => 'post',
'slug' => 'slug',
'distributor_media' => [],
'distributor_terms' => [],
'distributor_meta' => [],
],
'distributor_media' => null, // Accounts for https://github.com/10up/wp_mock/issues/173
'distributor_terms' => null, // Accounts for https://github.com/10up/wp_mock/issues/173
'distributor_meta' => null, // Accounts for https://github.com/10up/wp_mock/issues/173
]
] ),
'headers' => [
'Content-Type' => 'application/json',
],
],
],
Expand Down Expand Up @@ -547,20 +550,23 @@ public function test_send_notifications_remote_post_exists() {
$target_url . '/wp/v2/dt_subscription/receive',
[
'timeout' => 5,
'body' => [
'body' => wp_json_encode( [
'post_id' => $remote_post_id,
'signature' => $signature,
'post_data' => [
'title' => 'title',
'slug' => 'slug',
'post_type' => 'post',
'content' => 'content',
'excerpt' => 'excerpt',
'post_type' => 'post',
'distributor_media' => [],
'distributor_terms' => [],
'distributor_meta' => [],
'distributor_media' => null, // Accounts for https://github.com/10up/wp_mock/issues/173
'distributor_terms' => null, // Accounts for https://github.com/10up/wp_mock/issues/173
'distributor_meta' => null, // Accounts for https://github.com/10up/wp_mock/issues/173
],
],
] ),
'headers' => [
'Content-Type' => 'application/json',
]
],
],
]
Expand Down
14 changes: 14 additions & 0 deletions tests/php/includes/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ function get_allowed_mime_types() {
];
}

/**
* Mock wp_json_encode() function.
*
* @since x.x.x
*
* @param mixed $data Data to encode.
* @param int $options Optional. Options to be passed to json_encode(). Default 0.
* @param int $depth Optional. Maximum depth to walk through $data.
* @return string|false The JSON encoded string, or false if it cannot be encoded.
*/
function wp_json_encode( $data, $options = 0, $depth = 512 ) {
return json_encode( $data, $options, $depth );
}

/**
* Stub for remove_filter to avoid failure in test_remote_get()
*
Expand Down