-
Notifications
You must be signed in to change notification settings - Fork 154
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 hooks in push actions #417
base: develop
Are you sure you want to change the base?
Changes from 5 commits
1191f19
f0eeb0b
b001784
bed14b6
e7a5cbb
b3ba59f
f35b08b
9297c1f
4141f7c
3c8f2b4
b9a7517
710d4ca
0234163
e8dcd98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ); | ||
$allow_push = apply_filters( 'dt_allow_push', true, $params ); | ||
|
||
if ( true === $push_in_background ) { | ||
if ( false === $allow_push ) { | ||
wp_send_json_success( | ||
array( | ||
'results' => 'Success!!', | ||
|
@@ -111,15 +111,8 @@ 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 +244,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, | ||
), | ||
); | ||
} | ||
|
||
|
@@ -546,7 +541,14 @@ function menu_content() { | |
|
||
<div class="messages"> | ||
<div class="dt-success"> | ||
<?php echo esc_html( apply_filters( 'dt_successfully_distributed_message', esc_html__( 'Post successfully distributed.', 'distributor' ) ) ); ?> | ||
<?php | ||
/** | ||
* Filter distribution success message | ||
* | ||
* @param string Success message | ||
*/ | ||
echo esc_html( apply_filters( 'dt_successfully_distributed_message', __( 'Post successfully distributed.', 'distributor' ) ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this filter is related to the success message, can you add a sentence describing how it can be used/your use case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case when we are terminating 'push' function execution, it would be more descriptive to show another status message instead of "Post successfully distributed". For example: "Scheduled action to distribute post". Then show scheduled action status in a separate UI. |
||
?> | ||
</div> | ||
<div class="dt-error"> | ||
<?php esc_html_e( 'There was an issue distributing the post.', 'distributor' ); ?> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -238,24 +238,24 @@ 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 ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Subscriptions are run in the cron context, will this user check work in cron? I suspect not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what do you mean, there is no 'current user' in cron, so we need the |
||
return; | ||
} | ||
|
||
/** | ||
* 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 ); | ||
$allow_send_notification = apply_filters( 'dt_allow_send_notifications', true, $post_id ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs a description and better naming for the filter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, we can change it to By using this filter we can terminate function redistribution process and run it in background. It can be helpful in cases when resource consumption is high for that process. Here is a simple example how it can be implemented:
Here we are scheduling an event to run in background, and by returning
|
||
|
||
if ( true === $send_notification_in_background ) { | ||
if ( false === $allow_send_notification ) { | ||
return; | ||
} | ||
} | ||
|
||
if ( ! current_user_can( 'edit_post', $post_id ) ) { | ||
return; | ||
} | ||
|
||
$subscriptions = get_post_meta( $post_id, 'dt_subscriptions', true ); | ||
|
||
if ( empty( $subscriptions ) ) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a sentence describing how this filter could be used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case the 'push' action is consuming a lot of resources, we can use the
dt_allow_push
filter to terminate function execution, and run the function in background. Here is simple use case example:And then simply we can run it by hooking to scheduled event:
Or we can call custom function depending on use case.