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

Alpha release Jan 10 #3666

Merged
merged 9 commits into from
Jan 10, 2025
4 changes: 2 additions & 2 deletions includes/cli/class-ras.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public static function cli_setup_ras() {
*
* ## OPTIONS
*
* [--user=<user ID|email>]
* [--user-id=<id|email>]
* : The user ID or email address associated with the reader account to verify.
*
* @param array $args Positional args.
* @param array $assoc_args Associative args.
*/
public static function cli_verify_reader( $args, $assoc_args ) {
$user_id_or_email = ! empty( $args ) ? reset( $args ) : false;
$user_id_or_email = ! empty( $assoc_args ) ? reset( $assoc_args ) : false;
if ( ! $user_id_or_email ) {
WP_CLI::error( __( 'Please provide a user ID or email address.', 'newspack-plugin' ) );
exit;
Expand Down
64 changes: 54 additions & 10 deletions includes/data-events/class-data-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,22 @@ final class Data_Events {
*/
private static $global_handlers = [];

/**
* Dispatches queued for execution on shutdown.
*
* @var array[]
*/
private static $queued_dispatches = [];

/**
* Initialize hooks.
*/
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( 'shutdown', [ __CLASS__, 'execute_queued_dispatches' ] );
}


/**
* Maybe handle an event.
*/
Expand All @@ -57,16 +64,24 @@ public static function maybe_handle() {
\wp_die();
}

$action_name = isset( $_POST['action_name'] ) ? \sanitize_text_field( \wp_unslash( $_POST['action_name'] ) ) : null;
if ( empty( $action_name ) || ! isset( self::$actions[ $action_name ] ) ) {
$dispatches = isset( $_POST['dispatches'] ) ? $_POST['dispatches'] : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

if ( empty( $dispatches ) || ! is_array( $dispatches ) ) {
\wp_die();
}

$timestamp = isset( $_POST['timestamp'] ) ? \sanitize_text_field( \wp_unslash( $_POST['timestamp'] ) ) : null;
$data = isset( $_POST['data'] ) ? $_POST['data'] : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$client_id = isset( $_POST['client_id'] ) ? \sanitize_text_field( \wp_unslash( $_POST['client_id'] ) ) : null;
foreach ( $dispatches as $dispatch ) {
$action_name = isset( $dispatch['action_name'] ) ? \sanitize_text_field( $dispatch['action_name'] ) : null;
if ( empty( $action_name ) || ! isset( self::$actions[ $action_name ] ) ) {
continue;
}

$timestamp = isset( $dispatch['timestamp'] ) ? \sanitize_text_field( $dispatch['timestamp'] ) : null;
$data = isset( $dispatch['data'] ) ? $dispatch['data'] : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$client_id = isset( $dispatch['client_id'] ) ? \sanitize_text_field( $dispatch['client_id'] ) : null;

self::handle( $action_name, $timestamp, $data, $client_id );
self::handle( $action_name, $timestamp, $data, $client_id );
}

\wp_die();
}
Expand Down Expand Up @@ -312,8 +327,26 @@ public static function dispatch( $action_name, $data, $use_client_id = true ) {
return $body;
}

self::$queued_dispatches[] = $body;

// If we're in shutdown, execute the dispatches immediately.
if ( did_action( 'shutdown' ) ) {
self::execute_queued_dispatches();
}
}

/**
* Execute queued dispatches.
*/
public static function execute_queued_dispatches() {
if ( empty( self::$queued_dispatches ) ) {
return;
}

$actions = array_column( self::$queued_dispatches, 'action_name' );

Logger::log(
sprintf( 'Dispatching action "%s".', $action_name ),
sprintf( 'Dispatching actions: "%s".', implode( ', ', $actions ) ),
self::LOGGER_HEADER
);

Expand All @@ -325,16 +358,27 @@ public static function dispatch( $action_name, $data, $use_client_id = true ) {
\admin_url( 'admin-ajax.php' )
);

return \wp_remote_post(
$request = \wp_remote_post(
$url,
[
'timeout' => 0.01,
'blocking' => false,
'body' => $body,
'body' => [ 'dispatches' => self::$queued_dispatches ],
'cookies' => $_COOKIE, // phpcs:ignore
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
]
);

/**
* Fires after dispatching queued actions.
*
* @param WP_Error|WP_HTTP_Response $request The request object.
* @param array $queued_dispatches The queued dispatches.
*/
\do_action( 'newspack_data_events_dispatched', $request, self::$queued_dispatches );

// Clear the queue in case of a retry.
self::$queued_dispatches = [];
}
}
Data_Events::init();
23 changes: 12 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"@testing-library/react": "^12.1.4",
"@types/qs": "^6.9.17",
"@types/react": "^17.0.75",
"@wordpress/browserslist-config": "^6.14.0",
"@wordpress/browserslist-config": "^6.15.0",
"eslint": "^8.57.0",
"lint-staged": "^15.2.11",
"lint-staged": "^15.3.0",
"newspack-scripts": "^5.5.2",
"postcss-scss": "^4.0.9"
},
Expand Down
28 changes: 25 additions & 3 deletions tests/unit-tests/data-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,32 @@ public function test_dispatch() {

// Assert the hook was called once.
$this->assertEquals( 1, $call_count );
}

/**
* Test that executing queued dispatches triggers the dispatched action hook.
*/
public function test_execute_queued_dispatches() {
$action_name = 'test_action';
$data = [ 'test' => 'data' ];

$hook_request = null;
$hook_queued_dispatches = null;

$hook = function( $request, $queued_dispatches ) use ( &$hook_request, &$hook_queued_dispatches ) {
$hook_request = $request;
$hook_queued_dispatches = $queued_dispatches;
};
add_action( 'newspack_data_events_dispatched', $hook, 10, 2 );

Data_Events::register_action( $action_name );
Data_Events::dispatch( $action_name, $data );
Data_Events::execute_queued_dispatches();

// Assert it returns a WP_Http response.
$this->assertIsArray( $result );
$this->assertArrayHasKey( 'http_response', $result );
$this->assertIsArray( $hook_request );
$this->assertIsArray( $hook_queued_dispatches );
$this->assertEquals( $action_name, $hook_queued_dispatches[0]['action_name'] );
$this->assertEquals( $data, $hook_queued_dispatches[0]['data'] );
}

/**
Expand Down