-
Notifications
You must be signed in to change notification settings - Fork 156
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?
Add hooks in push actions #417
Conversation
…cation_allow_in_background'
… after the filter
Hi @avag-novembit, thank you for your contribution. Looking at the code, if the user returns true from If we are going to enable async push requests, we probably want to include full support for the actual pushing as well, and proper UI to show the (background) progress. |
includes/push-ui.php
Outdated
$result = push( $params ); | ||
|
||
wp_send_json_success( | ||
array( |
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.
can we have push
return the exact array we need and return it here directly?
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.
Changed accordingly.
includes/push-ui.php
Outdated
</div> | ||
</div> | ||
|
||
<div class="messages"> | ||
<div class="dt-success"> | ||
<?php esc_html_e( 'Post successfully distributed.', 'distributor' ); ?> | ||
<?php echo esc_html( apply_filters( 'dt_successfully_distributed_message', esc_html__( 'Post successfully distributed.', 'distributor' ) ) ); ?> |
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.
Is this a new filter? if so, needs documenting.
The inner esc_html__
can change to __
- escaping should happen late, right before output.
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.
Changed accordingly.
includes/subscriptions.php
Outdated
* @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 ); |
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.
this will affect all subscription updates for updated posts, these 'notifications' are required for updating pulled posts. This filter seems unrelated to the overall purpose of the PR, why is it included here?
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.
I'll take a look at this.
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.
@adamsilverstein basically, we are trying to prevent sending any REST API request during post update and move them into background. What if we will just add a filter, somewhere on the top and prevent send notifications, maybe near ?
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id ) ) {
return;
}
includes/subscriptions.php
Outdated
} | ||
} | ||
|
||
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
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.
can this stay as part of the logic check on L:237 https://github.com/10up/distributor/pull/417/files#diff-12f37ea7c389dfd5a6a70cc9992d8935R237 ?
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.
We don't need this check when running in background as in cron there is no current in user.
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.
Left some feedback; if we enable this it should fully support background processing.
Hi @adamsilverstein, thanks for the comments. Yes, 'Push in background' implementation is planned to be a separate add-on, and it will have its UI. Currently working on it. |
@@ -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 comment
The 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 comment
The 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 current_user_can(..)
check when 'is not doing cron'.
* @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 ); |
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:
add_filter( 'dt_allow_push', 'schedule_push_action', 10, 2 );
function schedule_push_action( $allow_push, $params ) {
if ( ! wp_next_scheduled( 'dt_push_posts_hook' ) ) {
wp_schedule_single_event( time(), 'dt_push_posts_hook', [ $params ] );
}
return false;
}
And then simply we can run it by hooking to scheduled event:
add_action( 'dt_push_posts_hook', 'push_action', 10, 1 );
function push_action( $params ) {
\Distributor\PushUI\push( $params );
}
Or we can call custom function depending on use case.
* | ||
* @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 comment
The 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 comment
The 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.
* @param bool true Whether run 'send notification' in background or not, default 'false' | ||
* @param array $params request data | ||
*/ | ||
$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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we can change it to dt_allow_subscription_update
, which is more descriptive perhaps.
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:
add_filter( 'dt_allow_subscription_update', 'schedule_send_notifications', 10, 2 );
function schedule_send_notifications( $allow_send_notifications, $post_id ) {
if ( ! wp_next_scheduled( 'dt_redistribute_posts_hook' ) ) {
wp_schedule_single_event( time(), 'dt_redistribute_posts_hook', [ $post_id ] );
}
return false;
}
Here we are scheduling an event to run in background, and by returning false
terminating post redistribution function execution. Then we can hook the post redistribution function to scheduled action or write a custom function to redistribute post.
add_action( 'dt_redistribute_posts_hook', 'redistribute_post', 10, 1 );
function redistribute_posts( $post_id ) {
\Distributor\Subscriptions\send_notifications( $post_id );
}
@avag-novembit Thanks for your updates here. I left some additional feedback. I'm still not 100% clear on your use case and whether its something the code plugin should support directly, enable via filters or perhaps help you figure out how to implement using existing extensibility. When you have code ready that is close or complete, can you link to your project so we can better evaluate the value of these changes. Thanks! |
@adamsilverstein thanks. Hope my last comments gives some idea how filters can be used. |
@avag-novembit we're looking to solve a similar issue related to reports on ACF issues as part of our |
Unassigning reviewers here while we focus on the v2 refactoring and can then reassess how best to handle the root issue here. |
@jeffpaul we are maintaining this repo anymor, however, if you will decide to move forward, please let me know if we will review it on our end. |
@avag-novembit thanks for the PR! Could you please rebase your PR on top of the latest changes in the base branch? |
Makes possible to run post distribution and re-distribution in background, closes #416.