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 hooks in push actions #417

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from

Conversation

avag-novembit
Copy link
Contributor

Makes possible to run post distribution and re-distribution in background, closes #416.

@jeffpaul jeffpaul added the type:enhancement New feature or request. label Jun 24, 2019
@jeffpaul jeffpaul added this to the 1.5.0 milestone Jun 24, 2019
@adamsilverstein adamsilverstein self-requested a review July 8, 2019 15:38
@adamsilverstein
Copy link

adamsilverstein commented Jul 8, 2019

Hi @avag-novembit, thank you for your contribution.

Looking at the code, if the user returns true from dt_push_allow_in_background, normal 'push' behavior is bypassed. I don't see code to handle actually pushing the content here, are you working on that separately?

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.

$result = push( $params );

wp_send_json_success(
array(

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed accordingly.

</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' ) ) ); ?>

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed accordingly.

* @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 );

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?

Copy link
Contributor Author

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.

Copy link
Contributor

@arsendovlatyan arsendovlatyan Jul 10, 2019

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;
	}

}
}

if ( ! current_user_can( 'edit_post', $post_id ) ) {
Copy link

@adamsilverstein adamsilverstein Jul 8, 2019

Choose a reason for hiding this comment

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

Copy link
Contributor Author

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.

Copy link

@adamsilverstein adamsilverstein left a 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.

@avag-novembit
Copy link
Contributor Author

Hi @avag-novembit, thank you for your contribution.

Looking at the code, if the user returns true from dt_push_allow_in_background, normal 'push' behavior is bypassed. I don't see code to handle actually pushing the content here, are you working on that separately?

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.

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 ) ) {

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.

Copy link
Contributor Author

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 );

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

Copy link
Contributor Author

@avag-novembit avag-novembit Jul 17, 2019

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' ) ) );

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

Copy link
Contributor Author

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 );

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.

Copy link
Contributor Author

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 );
}

@adamsilverstein
Copy link

@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!

@jeffpaul jeffpaul added the needs:feedback This requires reporter feedback to better understand the request. label Jul 16, 2019
@avag-novembit
Copy link
Contributor Author

@adamsilverstein thanks. Hope my last comments gives some idea how filters can be used.

@helen helen modified the milestones: 1.5.0, 1.6.0 Jul 17, 2019
@jeffpaul jeffpaul requested a review from dkotter January 27, 2020 21:48
@jeffpaul jeffpaul modified the milestones: 2.0.0, 2.1.0 Mar 26, 2020
@jeffpaul
Copy link
Member

@avag-novembit we're looking to solve a similar issue related to reports on ACF issues as part of our 2.1.0 release so I'm moving this PR and related issue to that release as well.

@dkotter dkotter removed the request for review from dinhtungdu August 23, 2022 22:12
@jeffpaul jeffpaul removed the request for review from dkotter January 16, 2023 18:04
@jeffpaul
Copy link
Member

Unassigning reviewers here while we focus on the v2 refactoring and can then reassess how best to handle the root issue here.

@arsendovlatyan
Copy link
Contributor

@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.

@github-actions github-actions bot added the needs:refresh This requires a refreshed PR to resolve. label Feb 9, 2024
Copy link

github-actions bot commented Feb 9, 2024

@avag-novembit thanks for the PR! Could you please rebase your PR on top of the latest changes in the base branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs:feedback This requires reporter feedback to better understand the request. needs:refresh This requires a refreshed PR to resolve. type:enhancement New feature or request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add possibility to run 'push' actions in background
5 participants