diff --git a/docs/snippets.md b/docs/snippets.md index 77565f3c1..922ea82c6 100644 --- a/docs/snippets.md +++ b/docs/snippets.md @@ -88,3 +88,28 @@ add_filter( 'dt_push_post_args', function( $post_body, $post ) { return $post_body; }, 10, 2 ); ``` + +### Set custom meta data on distributed content. + +Site owners may wish to modify the meta data on distributed content upon pushing or pulling. + +To set an item of meta data you can use the `dt_after_set_meta` hook. + +```php +/** + * Automatically store custom meta data on distributed posts. + */ +add_action( 'dt_after_set_meta', function( $meta, $existing_meta, $post_id ) { + update_post_meta( $post_id, 'myplugin_custom_meta', 'some_value', true ); +}, 10, 3 ); + +/** + * Automatically unlink a post once it is distributed. + * + * This prevents updates to the original content from modifying the copies + * distributed on other sites. + */ +add_action( 'dt_after_set_meta', function( $meta, $existing_meta, $post_id ) { + update_post_meta( $post_id, 'dt_unlinked', '1', true ); +}, 10, 3 ); +``` diff --git a/includes/classes/ExternalConnections/WordPressExternalConnection.php b/includes/classes/ExternalConnections/WordPressExternalConnection.php index 01c5d9389..6959d0b0d 100644 --- a/includes/classes/ExternalConnections/WordPressExternalConnection.php +++ b/includes/classes/ExternalConnections/WordPressExternalConnection.php @@ -428,7 +428,7 @@ public function pull( $items ) { update_post_meta( $new_post, 'dt_full_connection', true ); } - if ( ! empty( $post_array['meta'] ) ) { + if ( isset( $post_array['meta'] ) && is_array( $post_array['meta'] ) ) { // Filter documented in includes/classes/InternalConnections/NetworkSiteConnection.php. if ( apply_filters( 'dt_pull_post_meta', true, $new_post, $post_array['meta'], $item_array['remote_post_id'], $post_array, $this ) ) { \Distributor\Utils\set_meta( $new_post, $post_array['meta'] ); diff --git a/tests/php/WordPressExternalConnectionTest.php b/tests/php/WordPressExternalConnectionTest.php index 6c8b587fa..78c8e85ee 100644 --- a/tests/php/WordPressExternalConnectionTest.php +++ b/tests/php/WordPressExternalConnectionTest.php @@ -267,6 +267,15 @@ public function test_pull() { \WP_Mock::userFunction( 'get_current_user_id' ); \WP_Mock::userFunction( 'delete_post_meta' ); + \WP_Mock::userFunction( + 'apply_filters_deprecated', + [ + 'return' => function( $name, $args ) { + return $args[0]; + }, + ] + ); + \WP_Mock::userFunction( 'wp_remote_retrieve_headers', [ 'return' => [