Skip to content

Commit

Permalink
Connection: optimize admin_notices owner delete warning (#39650)
Browse files Browse the repository at this point in the history
Optimize the `admin_notices` hook conditions to get rid of an excessive callback.
  • Loading branch information
sergeymitr authored Oct 7, 2024
1 parent dd2077a commit 0d4d8a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Adjust conditions to optimize admin notices callback.
28 changes: 12 additions & 16 deletions projects/packages/connection/src/class-connection-notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,16 @@ public function __construct() {
* @return void
*/
public function initialize_notices( $screen ) {
if ( ! in_array(
if ( in_array(
$screen->id,
array(
'jetpack_page_akismet-key-config',
'admin_page_jetpack_modules',
),
true
) ) {
add_action( 'admin_notices', array( $this, 'delete_user_update_connection_owner_notice' ) );
return;
}
}

/**
* This is an entire admin notice dedicated to messaging and handling of the case where a user is trying to delete
* the connection owner.
*/
public function delete_user_update_connection_owner_notice() {
global $current_screen;

/*
* phpcs:disable WordPress.Security.NonceVerification.Recommended
Expand All @@ -66,14 +58,18 @@ public function delete_user_update_connection_owner_notice() {
* page. Nonce will be already checked by WordPress, so we do not need to check ourselves.
*/

if ( ! isset( $current_screen->base ) || 'users' !== $current_screen->base ) {
return;
}

if ( ! isset( $_REQUEST['action'] ) || 'delete' !== $_REQUEST['action'] ) {
return;
if ( isset( $screen->base ) && 'users' === $screen->base
&& isset( $_REQUEST['action'] ) && 'delete' === $_REQUEST['action']
) {
add_action( 'admin_notices', array( $this, 'delete_user_update_connection_owner_notice' ) );
}
}

/**
* This is an entire admin notice dedicated to messaging and handling of the case where a user is trying to delete
* the connection owner.
*/
public function delete_user_update_connection_owner_notice() {
// Get connection owner or bail.
$connection_manager = new Manager();
$connection_owner_id = $connection_manager->get_connection_owner_id();
Expand Down

0 comments on commit 0d4d8a8

Please sign in to comment.