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

Add snippet for setting meta data automatically. #1063

Merged
merged 4 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
```
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty check was preventing the hook from running if there was no meta because empty( [] ) === true. My understanding is that the hook should fire regardless of whether there is or is not meta so I've changed it to fire when the array is set.

// 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'] );
Expand Down
9 changes: 9 additions & 0 deletions tests/php/WordPressExternalConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down