From 1191f19a0486efb9d449b0d961b6f80367b9f432 Mon Sep 17 00:00:00 2001 From: avag Date: Wed, 19 Jun 2019 18:03:14 +0400 Subject: [PATCH 01/14] Make possible to send 'notification' in background also --- includes/subscriptions.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/includes/subscriptions.php b/includes/subscriptions.php index 1e02d9441..e3f82ea4a 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -238,6 +238,18 @@ function send_notifications( $post_id ) { return; } + /** + * Add possibility to send notification in background + * + * @param bool true Whether send notification in background or not + * @param int $post_id The post id + */ + $dt_send_notification_in_background = apply_filters( 'dt_send_notification_in_background', false, $post_id ); + + if ( true === $dt_send_notification_in_background ) { + return; + } + $subscriptions = get_post_meta( $post_id, 'dt_subscriptions', true ); if ( empty( $subscriptions ) ) { From f0eeb0bb35ca96154d13a15ce976d87d61c12c61 Mon Sep 17 00:00:00 2001 From: avag Date: Thu, 20 Jun 2019 17:15:41 +0400 Subject: [PATCH 02/14] Rename filter 'dt_send_notification_in_background' to 'dt_send_notification_allow_in_background' --- includes/subscriptions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/subscriptions.php b/includes/subscriptions.php index e3f82ea4a..03a1e147c 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -244,7 +244,7 @@ function send_notifications( $post_id ) { * @param bool true Whether send notification in background or not * @param int $post_id The post id */ - $dt_send_notification_in_background = apply_filters( 'dt_send_notification_in_background', false, $post_id ); + $dt_send_notification_in_background = apply_filters( 'dt_send_notification_allow_in_background', false, $post_id ); if ( true === $dt_send_notification_in_background ) { return; From b00178499615ae68854967d59eb371b5139fdf2f Mon Sep 17 00:00:00 2001 From: avag Date: Thu, 20 Jun 2019 18:03:09 +0400 Subject: [PATCH 03/14] Refactor - 'send notification' 'current_user_can' condition implement after the filter --- includes/subscriptions.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/includes/subscriptions.php b/includes/subscriptions.php index 03a1e147c..47711e56c 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -234,7 +234,7 @@ function delete_subscriptions( $post_id ) { * @since 1.0 */ function send_notifications( $post_id ) { - if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { + if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id ) ) { return; } @@ -244,9 +244,13 @@ function send_notifications( $post_id ) { * @param bool true Whether send notification in background or not * @param int $post_id The post id */ - $dt_send_notification_in_background = apply_filters( 'dt_send_notification_allow_in_background', false, $post_id ); + $send_notification_in_background = apply_filters( 'dt_send_notification_allow_in_background', false, $post_id ); - if ( true === $dt_send_notification_in_background ) { + if ( true === $send_notification_in_background ) { + return; + } + + if( ! current_user_can( 'edit_post', $post_id ) ) { return; } From bed14b613fd18c04bebcd905b3564d2cb926ba74 Mon Sep 17 00:00:00 2001 From: avag Date: Fri, 21 Jun 2019 19:09:07 +0400 Subject: [PATCH 04/14] do not apply 'send notifiaction allow in background' filter when 'doing cron' --- includes/subscriptions.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/includes/subscriptions.php b/includes/subscriptions.php index 47711e56c..c427661cd 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -238,16 +238,18 @@ function send_notifications( $post_id ) { return; } - /** - * Add possibility to send notification in background - * - * @param bool true Whether send notification in background or not - * @param int $post_id The post id - */ - $send_notification_in_background = apply_filters( 'dt_send_notification_allow_in_background', false, $post_id ); - - if ( true === $send_notification_in_background ) { - return; + if( ! wp_doing_cron() ) { + /** + * Add possibility to send notification in background + * + * @param bool true Whether send notification in background or not + * @param int $post_id The post id + */ + $send_notification_in_background = apply_filters( 'dt_send_notification_allow_in_background', false, $post_id ); + + if ( true === $send_notification_in_background ) { + return; + } } if( ! current_user_can( 'edit_post', $post_id ) ) { From e7a5cbb41c49de57025f3778403d4c245c47638a Mon Sep 17 00:00:00 2001 From: avag Date: Mon, 24 Jun 2019 20:10:34 +0400 Subject: [PATCH 05/14] Add hook to implement 'push' action in background --- includes/push-ui.php | 80 ++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/includes/push-ui.php b/includes/push-ui.php index 51bd6de7e..e47e6ddb1 100644 --- a/includes/push-ui.php +++ b/includes/push-ui.php @@ -88,7 +88,52 @@ function ajax_push() { exit; } - $connection_map = get_post_meta( intval( $_POST['postId'] ), 'dt_connection_map', true ); + // Maybe we need $_POST stored for background task + $params = $_POST; + + if( ! wp_doing_cron() ) { + /** + * Add possibility to send notification in background + * + * @param bool true Whether send notification in background or not + * @param int $post_id The post id + */ + $push_in_background = apply_filters( 'dt_push_allow_in_background', false, $params ); + + if ( true === $push_in_background ) { + wp_send_json_success( + array( + 'results' => 'Success!!', + ) + ); + + exit; + } + } + + $result = push( $params ); + + wp_send_json_success( + array( + 'results' => array( + 'internal' => $result['internal_push_results'], + 'external' => $result['external_push_results'], + ), + ) + ); + + exit; +} + +/** + * Performs 'push' action + * + * @param array $params + * + * @return array + */ +function push( $params ) { + $connection_map = get_post_meta( intval( $params['postId'] ), 'dt_connection_map', true ); if ( empty( $connection_map ) ) { $connection_map = array(); } @@ -104,7 +149,7 @@ function ajax_push() { $external_push_results = array(); $internal_push_results = array(); - foreach ( $_POST['connections'] as $connection ) { + foreach ( $params['connections'] as $connection ) { if ( 'external' === $connection['type'] ) { $external_connection_type = get_post_meta( $connection['id'], 'dt_external_connection_type', true ); $external_connection_url = get_post_meta( $connection['id'], 'dt_external_connection_url', true ); @@ -127,11 +172,11 @@ function ajax_push() { $push_args['remote_post_id'] = (int) $connection_map['external'][ (int) $connection['id'] ]['post_id']; } - if ( ! empty( $_POST['postStatus'] ) ) { - $push_args['post_status'] = $_POST['postStatus']; + if ( ! empty( $params['postStatus'] ) ) { + $push_args['post_status'] = $params['postStatus']; } - $remote_id = $external_connection->push( intval( $_POST['postId'] ), $push_args ); + $remote_id = $external_connection->push( intval( $params['postId'] ), $push_args ); /** * Record the external connection id's remote post id for this local post @@ -149,7 +194,7 @@ function ajax_push() { 'status' => 'success', ); - $external_connection->log_sync( array( $remote_id => $_POST['postId'] ) ); + $external_connection->log_sync( array( $remote_id => $params['postId'] ) ); } else { $external_push_results[ (int) $connection['id'] ] = array( 'post_id' => (int) $remote_id, @@ -166,11 +211,11 @@ function ajax_push() { $push_args['remote_post_id'] = (int) $connection_map['internal'][ (int) $connection['id'] ]['post_id']; } - if ( ! empty( $_POST['postStatus'] ) ) { - $push_args['post_status'] = esc_attr( $_POST['postStatus'] ); + if ( ! empty( $params['postStatus'] ) ) { + $push_args['post_status'] = esc_attr( $params['postStatus'] ); } - $remote_id = $internal_connection->push( intval( $_POST['postId'] ), $push_args ); + $remote_id = $internal_connection->push( intval( $params['postId'] ), $push_args ); /** * Record the internal connection id's remote post id for this local post @@ -179,7 +224,7 @@ function ajax_push() { $origin_site = get_current_blog_id(); switch_to_blog( intval( $connection['id'] ) ); $remote_url = get_permalink( $remote_id ); - $internal_connection->log_sync( array( $_POST['postId'] => $remote_id ), $origin_site ); + $internal_connection->log_sync( array( $params['postId'] => $remote_id ), $origin_site ); restore_current_blog(); $connection_map['internal'][ (int) $connection['id'] ] = array( @@ -203,18 +248,9 @@ function ajax_push() { } } - update_post_meta( intval( $_POST['postId'] ), 'dt_connection_map', $connection_map ); + update_post_meta( intval( $params['postId'] ), 'dt_connection_map', $connection_map ); - wp_send_json_success( - array( - 'results' => array( - 'internal' => $internal_push_results, - 'external' => $external_push_results, - ), - ) - ); - - exit; + return array( 'internal_push_results' => $internal_push_results, 'external_push_results' => $external_push_results ); } /** @@ -504,7 +540,7 @@ function menu_content() {
- +
From b3ba59f59c770f79c469391cdb0c6eb7b82bb1da Mon Sep 17 00:00:00 2001 From: avag Date: Mon, 24 Jun 2019 20:23:42 +0400 Subject: [PATCH 06/14] Fix linter errors --- includes/push-ui.php | 13 ++++++++++--- includes/subscriptions.php | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/includes/push-ui.php b/includes/push-ui.php index e47e6ddb1..be563c9a5 100644 --- a/includes/push-ui.php +++ b/includes/push-ui.php @@ -91,7 +91,7 @@ function ajax_push() { // Maybe we need $_POST stored for background task $params = $_POST; - if( ! wp_doing_cron() ) { + if ( ! wp_doing_cron() ) { /** * Add possibility to send notification in background * @@ -250,7 +250,10 @@ function push( $params ) { update_post_meta( intval( $params['postId'] ), 'dt_connection_map', $connection_map ); - return array( 'internal_push_results' => $internal_push_results, 'external_push_results' => $external_push_results ); + return array( + 'internal_push_results' => $internal_push_results, + 'external_push_results' => $external_push_results, + ); } /** @@ -534,7 +537,11 @@ function menu_content() { */ $as_draft = apply_filters( 'dt_allow_as_draft_distribute', $as_draft, $connection, $post ); ?> - + + +
diff --git a/includes/subscriptions.php b/includes/subscriptions.php index c427661cd..a067b9cf4 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -238,7 +238,7 @@ function send_notifications( $post_id ) { return; } - if( ! wp_doing_cron() ) { + if ( ! wp_doing_cron() ) { /** * Add possibility to send notification in background * @@ -252,7 +252,7 @@ function send_notifications( $post_id ) { } } - if( ! current_user_can( 'edit_post', $post_id ) ) { + if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } From f35b08bdbb7e7ece0b7cca146aa5d7a1d386a498 Mon Sep 17 00:00:00 2001 From: avag Date: Mon, 24 Jun 2019 20:36:55 +0400 Subject: [PATCH 07/14] Fix linter errors --- includes/push-ui.php | 15 +++++++-------- includes/subscriptions.php | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/includes/push-ui.php b/includes/push-ui.php index be563c9a5..b37ee45be 100644 --- a/includes/push-ui.php +++ b/includes/push-ui.php @@ -128,7 +128,7 @@ function ajax_push() { /** * Performs 'push' action * - * @param array $params + * @param array $params Parameters from 'post' request * * @return array */ @@ -406,7 +406,7 @@ function menu_content() { $current_post_type = get_post_type(); if ( ! empty( $_GET['post_type'] ) ) { // @codingStandardsIgnoreLine nonce not required - $current_post_type = sanitize_key( $_GET['post_type'] ); + $current_post_type = sanitize_key( $_GET['post_type'] ); // @codingStandardsIgnoreLine nonce not required } if ( empty( $current_post_type ) ) { @@ -537,17 +537,16 @@ function menu_content() { */ $as_draft = apply_filters( 'dt_allow_as_draft_distribute', $as_draft, $connection, $post ); ?> - - - + + + +
- +
diff --git a/includes/subscriptions.php b/includes/subscriptions.php index a067b9cf4..e84bb4771 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -238,7 +238,7 @@ function send_notifications( $post_id ) { return; } - if ( ! wp_doing_cron() ) { + if ( ! \wp_doing_cron() ) { /** * Add possibility to send notification in background * From 9297c1f55a34e32ca8f0ab195a361a04d075f5cd Mon Sep 17 00:00:00 2001 From: avag Date: Mon, 24 Jun 2019 20:49:31 +0400 Subject: [PATCH 08/14] Workaround to avoid linter errors --- includes/subscriptions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/subscriptions.php b/includes/subscriptions.php index e84bb4771..e0f991e9d 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -238,7 +238,7 @@ function send_notifications( $post_id ) { return; } - if ( ! \wp_doing_cron() ) { + if ( ! wp_doing_cron() ) { // @codingStandardsIgnoreLine `wp_doing_cron(..)` is a WP function /** * Add possibility to send notification in background * From 4141f7ced62924c523586e40a11a115dc3b82a29 Mon Sep 17 00:00:00 2001 From: avag Date: Mon, 8 Jul 2019 18:02:24 +0400 Subject: [PATCH 09/14] Fix comments --- includes/push-ui.php | 4 ++-- includes/subscriptions.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/push-ui.php b/includes/push-ui.php index b37ee45be..f09b3d3e0 100644 --- a/includes/push-ui.php +++ b/includes/push-ui.php @@ -95,8 +95,8 @@ function ajax_push() { /** * Add possibility to send notification in background * - * @param bool true Whether send notification in background or not - * @param int $post_id The post id + * @param bool false Whether run 'push' action in background or not, default 'false' + * @param array $params request data */ $push_in_background = apply_filters( 'dt_push_allow_in_background', false, $params ); diff --git a/includes/subscriptions.php b/includes/subscriptions.php index e0f991e9d..492235ab7 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -242,8 +242,8 @@ function send_notifications( $post_id ) { /** * Add possibility to send notification in background * - * @param bool true Whether send notification in background or not - * @param int $post_id The post id + * @param bool false Whether run 'send notification' in background or not, default 'false' + * @param array $params request data */ $send_notification_in_background = apply_filters( 'dt_send_notification_allow_in_background', false, $post_id ); From 3c8f2b4e54335a4fe136a24ad475b3847c1d144c Mon Sep 17 00:00:00 2001 From: avag Date: Tue, 9 Jul 2019 11:59:33 +0400 Subject: [PATCH 10/14] Eliminate check 'current_user_can' when 'wp_doing_cron' --- includes/subscriptions.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/subscriptions.php b/includes/subscriptions.php index 492235ab7..48276285f 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -238,7 +238,11 @@ function send_notifications( $post_id ) { return; } - if ( ! wp_doing_cron() ) { // @codingStandardsIgnoreLine `wp_doing_cron(..)` is a WP function + if ( ! wp_doing_cron() ) { //phpcs:ignore + if ( ! current_user_can( 'edit_post', $post_id ) ) { + return; + } + /** * Add possibility to send notification in background * @@ -252,10 +256,6 @@ function send_notifications( $post_id ) { } } - if ( ! current_user_can( 'edit_post', $post_id ) ) { - return; - } - $subscriptions = get_post_meta( $post_id, 'dt_subscriptions', true ); if ( empty( $subscriptions ) ) { From b9a7517afdcb2cf34211557860d84cae2cccf1c9 Mon Sep 17 00:00:00 2001 From: avag Date: Tue, 9 Jul 2019 12:46:36 +0400 Subject: [PATCH 11/14] return push return array from 'ajax_push()' --- includes/push-ui.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/includes/push-ui.php b/includes/push-ui.php index f09b3d3e0..3ef0ec25b 100644 --- a/includes/push-ui.php +++ b/includes/push-ui.php @@ -111,15 +111,9 @@ function ajax_push() { } } - $result = push( $params ); wp_send_json_success( - array( - 'results' => array( - 'internal' => $result['internal_push_results'], - 'external' => $result['external_push_results'], - ), - ) + push( $params ) ); exit; @@ -251,8 +245,10 @@ function push( $params ) { update_post_meta( intval( $params['postId'] ), 'dt_connection_map', $connection_map ); return array( - 'internal_push_results' => $internal_push_results, - 'external_push_results' => $external_push_results, + 'results' => array( + 'internal' => $internal_push_results, + 'external' => $external_push_results, + ), ); } From 710d4ca63b1a3da656998ac43c8bb547beb58272 Mon Sep 17 00:00:00 2001 From: avag Date: Tue, 9 Jul 2019 15:10:36 +0400 Subject: [PATCH 12/14] Document the 'dt_successfully_distributed_message' filter --- includes/push-ui.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/includes/push-ui.php b/includes/push-ui.php index 3ef0ec25b..61cf401bb 100644 --- a/includes/push-ui.php +++ b/includes/push-ui.php @@ -542,7 +542,14 @@ function menu_content() {
- +
From 023416316a8854a7f08599fab2d16d90d673488a Mon Sep 17 00:00:00 2001 From: avag Date: Fri, 12 Jul 2019 19:42:02 +0400 Subject: [PATCH 13/14] Refactor filter names --- includes/push-ui.php | 7 +++---- includes/subscriptions.php | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/includes/push-ui.php b/includes/push-ui.php index 61cf401bb..ae5b49615 100644 --- a/includes/push-ui.php +++ b/includes/push-ui.php @@ -95,12 +95,12 @@ function ajax_push() { /** * Add possibility to send notification in background * - * @param bool false Whether run 'push' action in background or not, default 'false' + * @param bool true Whether run 'push' action in background or not, default 'false' * @param array $params request data */ - $push_in_background = apply_filters( 'dt_push_allow_in_background', false, $params ); + $push_in_background = apply_filters( 'dt_allow_push', true, $params ); - if ( true === $push_in_background ) { + if ( false === $push_in_background ) { wp_send_json_success( array( 'results' => 'Success!!', @@ -111,7 +111,6 @@ function ajax_push() { } } - wp_send_json_success( push( $params ) ); diff --git a/includes/subscriptions.php b/includes/subscriptions.php index 48276285f..1db9fcfcf 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -246,12 +246,12 @@ function send_notifications( $post_id ) { /** * Add possibility to send notification in background * - * @param bool false Whether run 'send notification' in background or not, default 'false' + * @param bool true Whether run 'send notification' in background or not, default 'false' * @param array $params request data */ - $send_notification_in_background = apply_filters( 'dt_send_notification_allow_in_background', false, $post_id ); + $send_notification_in_background = apply_filters( 'dt_allow_send_notifications', true, $post_id ); - if ( true === $send_notification_in_background ) { + if ( false === $send_notification_in_background ) { return; } } From e8dcd984941b5f19d20773cfd222bf8b24ab6711 Mon Sep 17 00:00:00 2001 From: avag Date: Mon, 15 Jul 2019 14:10:59 +0400 Subject: [PATCH 14/14] Rename variables in push actions --- includes/push-ui.php | 4 ++-- includes/subscriptions.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/push-ui.php b/includes/push-ui.php index ae5b49615..21176433d 100644 --- a/includes/push-ui.php +++ b/includes/push-ui.php @@ -98,9 +98,9 @@ function ajax_push() { * @param bool true Whether run 'push' action in background or not, default 'false' * @param array $params request data */ - $push_in_background = apply_filters( 'dt_allow_push', true, $params ); + $allow_push = apply_filters( 'dt_allow_push', true, $params ); - if ( false === $push_in_background ) { + if ( false === $allow_push ) { wp_send_json_success( array( 'results' => 'Success!!', diff --git a/includes/subscriptions.php b/includes/subscriptions.php index 1db9fcfcf..3a94f2b56 100644 --- a/includes/subscriptions.php +++ b/includes/subscriptions.php @@ -249,9 +249,9 @@ function send_notifications( $post_id ) { * @param bool true Whether run 'send notification' in background or not, default 'false' * @param array $params request data */ - $send_notification_in_background = apply_filters( 'dt_allow_send_notifications', true, $post_id ); + $allow_send_notification = apply_filters( 'dt_allow_send_notifications', true, $post_id ); - if ( false === $send_notification_in_background ) { + if ( false === $allow_send_notification ) { return; } }