diff --git a/projects/packages/connection/changelog/update-optimize-connection-admin-notice-hook b/projects/packages/connection/changelog/update-optimize-connection-admin-notice-hook new file mode 100644 index 0000000000000..256ab91ca3838 --- /dev/null +++ b/projects/packages/connection/changelog/update-optimize-connection-admin-notice-hook @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Adjust conditions to optimize admin notices callback. diff --git a/projects/packages/connection/src/class-connection-notice.php b/projects/packages/connection/src/class-connection-notice.php index eaaebc1a35860..714eca4eb9a28 100644 --- a/projects/packages/connection/src/class-connection-notice.php +++ b/projects/packages/connection/src/class-connection-notice.php @@ -40,7 +40,7 @@ public function __construct() { * @return void */ public function initialize_notices( $screen ) { - if ( ! in_array( + if ( in_array( $screen->id, array( 'jetpack_page_akismet-key-config', @@ -48,16 +48,8 @@ public function initialize_notices( $screen ) { ), 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 @@ -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();