Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 21 additions & 49 deletions includes/data-events/class-data-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,8 @@ final class Data_Events {
public static function init() {
\add_action( 'wp_ajax_' . self::ACTION, [ __CLASS__, 'maybe_handle' ] );
\add_action( 'wp_ajax_nopriv_' . self::ACTION, [ __CLASS__, 'maybe_handle' ] );
\add_action( 'newspack_data_events_as_dispatch', [ __CLASS__, 'handle' ], 10, 4 );
}

/**
* Whether to use Action Scheduler if available.
*
* @return bool
*/
public static function use_action_scheduler() {
$use_action_scheduler = false;
if ( function_exists( 'as_enqueue_async_action' ) ) {
$use_action_scheduler = true;
}
/**
* Filters whether to use the Action Scheduler if available.
*
* @param bool $use_action_scheduler
*/
return \apply_filters( 'newspack_data_events_use_action_scheduler', $use_action_scheduler );
}

/**
* Maybe handle an event.
Expand Down Expand Up @@ -330,39 +312,29 @@ public static function dispatch( $action_name, $data, $use_client_id = true ) {
return $body;
}

if ( self::use_action_scheduler() ) {
Logger::log(
sprintf( 'Dispatching action "%s" via Action Scheduler.', $action_name ),
self::LOGGER_HEADER
);

\as_enqueue_async_action( 'newspack_data_events_as_dispatch', array_values( $body ), 'newspack-data-events' );

} else {
Logger::log(
sprintf( 'Dispatching action "%s" via HTTP request.', $action_name ),
self::LOGGER_HEADER
);
Logger::log(
sprintf( 'Dispatching action "%s".', $action_name ),
self::LOGGER_HEADER
);

$url = \add_query_arg(
[
'action' => self::ACTION,
'nonce' => \wp_create_nonce( self::ACTION ),
],
\admin_url( 'admin-ajax.php' )
);
$url = \add_query_arg(
[
'action' => self::ACTION,
'nonce' => \wp_create_nonce( self::ACTION ),
],
\admin_url( 'admin-ajax.php' )
);

return \wp_remote_post(
$url,
[
'timeout' => 0.01,
'blocking' => false,
'body' => $body,
'cookies' => $_COOKIE, // phpcs:ignore
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
]
);
}
return \wp_remote_post(
$url,
[
'timeout' => 0.01,
'blocking' => false,
'body' => $body,
'cookies' => $_COOKIE, // phpcs:ignore
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
]
);
}
}
Data_Events::init();
26 changes: 22 additions & 4 deletions includes/data-events/class-webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,30 @@ public static function clear_finished() {
}
}

/**
* Whether to use Action Scheduler if available.
*
* @return bool
*/
private static function use_action_scheduler() {
$use_action_scheduler = false;
if ( function_exists( 'as_enqueue_async_action' ) ) {
$use_action_scheduler = true;
}
/**
* Filters whether to use the Action Scheduler if available.
*
* @param bool $use_action_scheduler
*/
return \apply_filters( 'newspack_data_events_use_action_scheduler', $use_action_scheduler );
}

/**
* Send webhook requests that should've been sent but are still pending at
* 'future' status.
*/
public static function send_late_requests() {
if ( Data_Events::use_action_scheduler() ) {
if ( self::use_action_scheduler() ) {
return;
}
$requests = \get_posts(
Expand Down Expand Up @@ -602,7 +620,7 @@ public static function transition_post_status( $new_status, $old_status, $post )
self::REQUEST_POST_TYPE === $post->post_type &&
'publish' === $new_status &&
'publish' !== $old_status &&
! Data_Events::use_action_scheduler()
! self::use_action_scheduler()
) {
self::process_request( $post->ID );
}
Expand Down Expand Up @@ -635,7 +653,7 @@ private static function schedule_request( $request_id, $delay = 1 ) {
$date = date( 'Y-m-d H:i:s', $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$date_gmt = gmdate( 'Y-m-d H:i:s', strtotime( $date ) );
\update_post_meta( $request_id, 'scheduled', $time );
if ( Data_Events::use_action_scheduler() ) {
if ( self::use_action_scheduler() ) {
Logger::log( "Scheduling request {$request_id} for {$date_gmt} via Action Scheduler.", self::LOGGER_HEADER );
\as_schedule_single_action( $time, 'newspack_webhooks_as_process_request', [ $request_id ], 'newspack-data-events' );
} else {
Expand All @@ -661,7 +679,7 @@ public static function finish_request( $request_id ) {
Logger::log( "Finishing request {$request_id}.", self::LOGGER_HEADER );
\update_post_meta( $request_id, 'status', 'finished' );
// If using ActionScheduler, manually set to publish.
if ( Data_Events::use_action_scheduler() ) {
if ( self::use_action_scheduler() ) {
\wp_publish_post( $request_id );
}
}
Expand Down
4 changes: 4 additions & 0 deletions includes/data-events/connectors/ga4/class-ga4.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ public static function filter_donation_new_event_body( $body, $event_name ) {
$order_id = $body['data']['interaction_data']['donation_order_id'] ?? false;
}

if ( ! function_exists( 'wc_get_order' ) ) {
return $body;
}

$order = wc_get_order( $order_id );
if ( $order ) {
$ga_client_id = $order->get_meta( '_newspack_ga_client_id' );
Expand Down