diff --git a/includes/classes/API/SubscriptionsController.php b/includes/classes/API/SubscriptionsController.php index 766f9f45e..15cc484ee 100644 --- a/includes/classes/API/SubscriptionsController.php +++ b/includes/classes/API/SubscriptionsController.php @@ -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 ); diff --git a/includes/subscriptions.php b/includes/subscriptions.php index fe7fc3ca1..2fd92df1d 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -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 ), ], ]; @@ -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 ) ) { diff --git a/includes/utils.php b/includes/utils.php index 024490723..8f5549c07 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -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. diff --git a/tests/php/SubscriptionsTest.php b/tests/php/SubscriptionsTest.php index 0453d57b8..e46f25e37 100644 --- a/tests/php/SubscriptionsTest.php +++ b/tests/php/SubscriptionsTest.php @@ -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', ], ], ], @@ -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', + ] ], ], ] diff --git a/tests/php/includes/common.php b/tests/php/includes/common.php index 17e9b58cb..1fc4b1b0d 100644 --- a/tests/php/includes/common.php +++ b/tests/php/includes/common.php @@ -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() *