-
Notifications
You must be signed in to change notification settings - Fork 57
feat(integrations): add ActionScheduler group handling #4559
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
Open
miguelpeixe
wants to merge
21
commits into
trunk
Choose a base branch
from
feat/integrations-action-groups
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+339
−11
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
83fdde5
feat: add ActionScheduler group helper methods
miguelpeixe b75e46c
feat: use per-integration AS group for handler retries
miguelpeixe 9be6dec
feat: use per-integration AS group for sync retries
miguelpeixe a5bbe44
feat: use per-integration AS group for async pulls
miguelpeixe 27ba501
feat: use dedicated AS group for webhook actions
miguelpeixe c579f10
fix: explicitly set AS group for bulk sync actions
miguelpeixe dc11ece
feat: add get_scheduled_actions query helper for UI
miguelpeixe e3bd456
test: update sync tests to use per-integration AS groups
miguelpeixe 95cff00
feat: add action group shortcuts to Integration base class
miguelpeixe 81e22b2
fix: make action methods final
miguelpeixe 30b7366
feat: decouple integrations from data events handler action group name
miguelpeixe be1aef1
chore: remove default action group constant and update fallback behavior
miguelpeixe bdff70e
docs: update return type
miguelpeixe 2be5e66
feat: introduce ActionScheduler class and integrate with existing com…
miguelpeixe f7a7c1f
chore: move init up
miguelpeixe b4e1ec2
feat: return empty array for empty groups in get_scheduled_actions
miguelpeixe 1ef00a7
refactor: optimize get_scheduled_actions query
miguelpeixe cda43b6
chore: fix docblock
miguelpeixe 43cdbc2
chore: fix docblock
miguelpeixe a95cf5a
feat: add is_available method and handle availability checks
miguelpeixe caa12e5
refactor: streamline SQL query preparation
miguelpeixe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| <?php | ||
| /** | ||
| * ActionScheduler utilities. | ||
| * | ||
| * @package Newspack | ||
| */ | ||
|
|
||
| namespace Newspack; | ||
|
|
||
| defined( 'ABSPATH' ) || exit; | ||
|
|
||
| /** | ||
| * General-purpose ActionScheduler helpers for Newspack. | ||
| */ | ||
| class Action_Scheduler { | ||
| /** | ||
| * Default ActionScheduler group for Newspack actions. | ||
| */ | ||
| const DEFAULT_GROUP = 'newspack'; | ||
|
|
||
| /** | ||
| * Prefix for Newspack ActionScheduler groups. | ||
| */ | ||
| const GROUP_PREFIX = 'newspack-'; | ||
|
|
||
| /** | ||
| * Whether ActionScheduler is available. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public static function is_available() { | ||
| return class_exists( 'ActionScheduler' ); | ||
| } | ||
|
|
||
| /** | ||
| * Get ActionScheduler group slugs matching a prefix. | ||
| * | ||
| * @param string $prefix The prefix to match (e.g. 'newspack-'). | ||
| * | ||
| * @return string[] Array of group slug strings. | ||
| */ | ||
| public static function get_groups_by_prefix( $prefix ) { | ||
| if ( ! self::is_available() ) { | ||
| return []; | ||
| } | ||
| global $wpdb; | ||
| $table = $wpdb->prefix . 'actionscheduler_groups'; | ||
| return $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | ||
| $wpdb->prepare( | ||
| "SELECT slug FROM {$table} WHERE slug LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared | ||
| $wpdb->esc_like( $prefix ) . '%' | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Query ActionScheduler actions by group slugs. | ||
| * | ||
| * @param array $args { | ||
| * Query arguments. | ||
| * | ||
| * @type string[] $groups Array of group slugs to query. | ||
| * @type string $status ActionScheduler status (pending, complete, failed, canceled). | ||
| * @type int $per_page Number of actions to return. Default 20. | ||
| * @type int $offset Offset for pagination. Default 0. | ||
| * @type string $orderby Column to order by. Default 'scheduled_date_gmt'. | ||
| * @type string $order ASC or DESC. Default 'DESC'. | ||
| * } | ||
| * | ||
| * @return array Array of action row objects. | ||
| */ | ||
| public static function get_scheduled_actions( $args = [] ) { | ||
| if ( ! self::is_available() ) { | ||
| return []; | ||
| } | ||
| global $wpdb; | ||
|
|
||
| $defaults = [ | ||
| 'groups' => [], | ||
| 'status' => '', | ||
| 'per_page' => 20, | ||
| 'offset' => 0, | ||
| 'orderby' => 'scheduled_date_gmt', | ||
| 'order' => 'DESC', | ||
| ]; | ||
| $args = wp_parse_args( $args, $defaults ); | ||
|
|
||
| $slugs = $args['groups']; | ||
| if ( empty( $slugs ) ) { | ||
| $slugs = array_merge( | ||
| [ self::DEFAULT_GROUP ], | ||
| self::get_groups_by_prefix( self::GROUP_PREFIX ) | ||
| ); | ||
| } | ||
| if ( empty( $slugs ) ) { | ||
| return []; | ||
| } | ||
|
|
||
| $allowed_orderby = [ 'scheduled_date_gmt', 'action_id', 'hook', 'status' ]; | ||
| $orderby = in_array( $args['orderby'], $allowed_orderby, true ) ? $args['orderby'] : 'scheduled_date_gmt'; | ||
| $order = 'ASC' === strtoupper( $args['order'] ) ? 'ASC' : 'DESC'; | ||
|
|
||
| $actions_table = $wpdb->prefix . 'actionscheduler_actions'; | ||
| $groups_table = $wpdb->prefix . 'actionscheduler_groups'; | ||
| $slug_placeholders = implode( ',', array_fill( 0, count( $slugs ), '%s' ) ); | ||
| $prepare_args = $slugs; | ||
|
|
||
| // Build optional status filter. | ||
| $status_clause = ''; | ||
| if ( ! empty( $args['status'] ) ) { | ||
| $status_clause = 'AND a.status = %s '; | ||
| $prepare_args[] = $args['status']; | ||
| } | ||
|
|
||
| $prepare_args[] = absint( $args['per_page'] ); | ||
| $prepare_args[] = absint( $args['offset'] ); | ||
|
|
||
| // Table names: $wpdb->prefix + hardcoded strings. $orderby/$order: allowlist/ternary validated. | ||
| // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared | ||
| $sql = "SELECT a.* FROM {$actions_table} a INNER JOIN {$groups_table} g ON a.group_id = g.group_id WHERE g.slug IN ({$slug_placeholders}) {$status_clause}ORDER BY a.{$orderby} {$order} LIMIT %d OFFSET %d"; | ||
|
|
||
| // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber | ||
| $query = $wpdb->prepare( $sql, ...$prepare_args ); | ||
|
|
||
| // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared | ||
| return $wpdb->get_results( $query ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.