From a40c592b433983434a6621aa9149b987732c8f25 Mon Sep 17 00:00:00 2001 From: leogermani Date: Fri, 24 Nov 2023 12:02:40 -0300 Subject: [PATCH 1/4] Update snippets.md Updating the docs for the publication date snippet. The argument expected by the core API is `date` and not `post_date`. Also adding `date_gmt` for completion --- docs/snippets.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/snippets.md b/docs/snippets.md index 5627f6eff..5be0ceba0 100644 --- a/docs/snippets.md +++ b/docs/snippets.md @@ -95,7 +95,8 @@ add_action( 'plugins_loaded', function() { * This filter is used to filter the arguments sent to the remote server during a push. The below code snippet passes the original published date to the new pushed post and sets the same published date instead of setting it as per the current time. */ add_filter( 'dt_push_post_args', function( $post_body, $post ) { - $post_body['post_date'] = $post->post_date; + $post_body['date'] = $post->post_date; + $post_body['date_gmt'] = $post->post_date_gmt; return $post_body; }, 10, 2 ); From 9964b7ae8e984d53c4dd66e4dcad2c11e170243e Mon Sep 17 00:00:00 2001 From: leogermani Date: Fri, 24 Nov 2023 12:56:40 -0300 Subject: [PATCH 2/4] update snippet with filter for pulls --- docs/snippets.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/snippets.md b/docs/snippets.md index 5be0ceba0..dc8f8dad8 100644 --- a/docs/snippets.md +++ b/docs/snippets.md @@ -100,6 +100,16 @@ add_filter( 'dt_push_post_args', function( $post_body, $post ) { return $post_body; }, 10, 2 ); + +/** + * This filters the the arguments passed into wp_insert_post during a pull + */ +add_filter( 'dt_pull_post_args', function( $post_array, $remote_id, $post ) { + $post_array['post_date'] = $post->post_date; + $post_array['post_date_gmt'] = $post->post_date_gmt; + + return $post_array; +}, 10, 3 ); ``` ### Automatically unlink posts From 1ad018ea93b23aaf0edb5c8e1b3dccde7799e3b1 Mon Sep 17 00:00:00 2001 From: leogermani Date: Wed, 13 Dec 2023 15:00:07 -0300 Subject: [PATCH 3/4] Update snippets.md Update snippet to account for the two types of connections --- docs/snippets.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/snippets.md b/docs/snippets.md index dc8f8dad8..0a8e6be5a 100644 --- a/docs/snippets.md +++ b/docs/snippets.md @@ -94,12 +94,17 @@ add_action( 'plugins_loaded', function() { * * This filter is used to filter the arguments sent to the remote server during a push. The below code snippet passes the original published date to the new pushed post and sets the same published date instead of setting it as per the current time. */ -add_filter( 'dt_push_post_args', function( $post_body, $post ) { - $post_body['date'] = $post->post_date; - $post_body['date_gmt'] = $post->post_date_gmt; +add_filter( 'dt_push_post_args', function( $post_body, $post, $args, $connection ) { + + // When pushing to an external connection, we use the REST API, so the name of the field is `date`. + // But when pushing to an internal connection, the attributes are sent to wp_insert_post, which expects `post_date`. + $field_prefix = is_a( $connection, 'Distributor\ExternalConnections\WordPressExternalConnection' ) ? '' : 'post_'; + + $post_body[ $field_prefix . 'date'] = $post->post_date; + $post_body[ $field_prefix . 'date_gmt'] = $post->post_date_gmt; return $post_body; -}, 10, 2 ); +}, 10, 4 ); /** * This filters the the arguments passed into wp_insert_post during a pull From 9f258ccf3e9ff2b5cc2c7cbbf037cef585a651f0 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Thu, 14 Dec 2023 10:13:33 +1100 Subject: [PATCH 4/4] Use `instanceof` in place of `is_a()`. --- docs/snippets.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/snippets.md b/docs/snippets.md index 0a8e6be5a..1c2a438aa 100644 --- a/docs/snippets.md +++ b/docs/snippets.md @@ -98,7 +98,7 @@ add_filter( 'dt_push_post_args', function( $post_body, $post, $args, $connection // When pushing to an external connection, we use the REST API, so the name of the field is `date`. // But when pushing to an internal connection, the attributes are sent to wp_insert_post, which expects `post_date`. - $field_prefix = is_a( $connection, 'Distributor\ExternalConnections\WordPressExternalConnection' ) ? '' : 'post_'; + $field_prefix =( $connection instanceof \Distributor\ExternalConnections\WordPressExternalConnection ) ? '' : 'post_'; $post_body[ $field_prefix . 'date'] = $post->post_date; $post_body[ $field_prefix . 'date_gmt'] = $post->post_date_gmt;