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
4 changes: 4 additions & 0 deletions packages/js/src/general/components/opt-in-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ export const OptInContainer = () => {
hideOptInNotification( "task_list" );
}, [ hideOptInNotification ] );

if ( ! isOpen ) {
return null;
}

return <TaskListOptInNotification isOpen={ isOpen } onClose={ onClose } />;
};
14 changes: 13 additions & 1 deletion src/general/user-interface/opt-in-route.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,20 @@ public function register_routes() {
public function set_opt_in_seen( $request ) {
$key = $request->get_param( 'key' );
$current_user_id = $this->user_helper->get_current_user_id();
$meta_key = '_yoast_wpseo_' . $key . '_opt_in_notification_seen';

// If already seen, return success immediately (update_user_meta returns false when the value is unchanged).
if ( $this->user_helper->get_meta( $current_user_id, $meta_key, true ) === '1' ) {
return new WP_REST_Response(
(object) [
'success' => true,
'status' => 200,
],
200,
);
}

$result = $this->user_helper->update_meta( $current_user_id, '_yoast_wpseo_' . $key . '_opt_in_notification_seen', true );
$result = $this->user_helper->update_meta( $current_user_id, $meta_key, true );
$success = $result !== false;
$status = ( $success ) ? 200 : 400;

Expand Down
24 changes: 24 additions & 0 deletions tests/WP/General/User_Interface/Set_Opt_In_Seen_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ public function test_set_opt_in_seen_with_empty_key() {
$this->assertSame( $response_data['code'], 'rest_invalid_param' );
}

/**
* Tests that calling the endpoint twice returns 200 both times (idempotent).
*
* @return void
*/
public function test_set_opt_in_seen_is_idempotent() {
$user = $this->factory->user->create_and_get( [ 'role' => 'administrator' ] );
$user->add_cap( 'wpseo_manage_options' );
\wp_set_current_user( $user->ID );

$request = new WP_REST_Request( 'POST', '/yoast/v1/seen-opt-in-notification' );
$request->set_param( 'key', 'task_list' );

// First call: sets the meta.
$response = \rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->status );
$this->assertTrue( $response->get_data()->success );

// Second call: meta already set, should still return 200.
$response = \rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->status );
$this->assertTrue( $response->get_data()->success );
}

/**
* Tests setting opt-in notification as seen without key parameter.
*
Expand Down