Skip to content

Commit 724e2df

Browse files
committed
Refactor perflab_admin_pointer() to consolidate user meta update and improve coverage
1 parent 6253cc1 commit 724e2df

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

plugins/performance-lab/includes/admin/load.php

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -120,78 +120,84 @@ function perflab_get_admin_pointers(): array {
120120
* ensure that `$hook_suffix` is a string when it calls `do_action( 'admin_enqueue_scripts', $hook_suffix )`.
121121
*/
122122
function perflab_admin_pointer( ?string $hook_suffix = '' ): void {
123-
// Do not show admin pointer in multisite Network admin or User admin UI.
124-
if ( is_network_admin() || is_user_admin() ) {
123+
$is_performance_screen = (
124+
'options-general.php' === $hook_suffix &&
125+
( isset( $_GET['page'] ) && PERFLAB_SCREEN === $_GET['page'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
126+
);
127+
128+
// Do not show admin pointer in multisite Network admin, User admin UI, dashboard, or plugins list table. However,
129+
// do proceed on the Performance screen so that all pointers can be auto-dismissed.
130+
if (
131+
is_network_admin() ||
132+
is_user_admin() ||
133+
(
134+
! in_array( $hook_suffix, array( 'index.php', 'plugins.php' ), true ) &&
135+
! $is_performance_screen
136+
)
137+
) {
125138
return;
126139
}
127140

128141
$admin_pointers = perflab_get_admin_pointers();
129142
$admin_pointer_ids = array_keys( $admin_pointers );
130143
$dismissed_pointer_ids = perflab_get_dismissed_admin_pointer_ids();
131144

132-
// All pointers have been dismissed already.
133-
if ( count( array_diff( $admin_pointer_ids, $dismissed_pointer_ids ) ) === 0 ) {
134-
return;
135-
}
136-
137-
// Do not show the admin pointer when not on the dashboard or plugins list table.
138-
if ( ! in_array( $hook_suffix, array( 'index.php', 'plugins.php' ), true ) ) {
139-
140-
// And if we're on the Performance screen, automatically dismiss the pointers.
141-
if ( isset( $_GET['page'] ) && PERFLAB_SCREEN === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
142-
update_user_meta(
143-
get_current_user_id(),
144-
'dismissed_wp_pointers',
145-
implode(
146-
',',
147-
array_unique( array_merge( $dismissed_pointer_ids, $admin_pointer_ids ) )
148-
)
149-
);
150-
}
151-
152-
return;
145+
// And if we're on the Performance screen, automatically dismiss all the pointers.
146+
$auto_dismissed_pointer_ids = array();
147+
if ( $is_performance_screen ) {
148+
$auto_dismissed_pointer_ids = array_merge( $auto_dismissed_pointer_ids, $admin_pointer_ids );
153149
}
154150

155-
/**
156-
* Installed plugin slugs.
157-
*
158-
* @var non-empty-string[] $installed_plugin_slugs
159-
*/
160-
$installed_plugin_slugs = array_map(
161-
static function ( $name ) {
162-
return strtok( $name, '/' );
163-
},
164-
array_keys( get_plugins() )
165-
);
166-
167151
// List of pointer IDs that are tied to feature plugin slugs.
152+
// TODO: Add this to perflab_get_admin_pointers().
168153
$plugin_dependent_pointers = array(
169154
'perflab-feature-view-transitions' => 'view-transitions',
170155
'perflab-feature-nocache-bfcache' => 'nocache-bfcache',
171156
);
172157

173-
// Make sure that the dismissed pointers include any plugins that are already installed.
174-
$dismissed_pointers_updated = false;
175-
foreach ( $plugin_dependent_pointers as $pointer_id => $slug ) {
176-
if ( in_array( $slug, $installed_plugin_slugs, true ) && ! in_array( $pointer_id, $dismissed_pointer_ids, true ) ) {
177-
$dismissed_pointer_ids[] = $pointer_id;
178-
$dismissed_pointers_updated = true;
158+
// Preemptively dismiss plugin-specific pointers for plugins which are already installed.
159+
$plugin_dependent_pointers_undismissed = array_diff( array_keys( $plugin_dependent_pointers ), $dismissed_pointer_ids );
160+
if ( count( $plugin_dependent_pointers_undismissed ) > 0 ) {
161+
/**
162+
* Installed plugin slugs.
163+
*
164+
* @var non-empty-string[] $installed_plugin_slugs
165+
*/
166+
$installed_plugin_slugs = array_map(
167+
static function ( $name ) {
168+
return strtok( $name, '/' );
169+
},
170+
array_keys( get_plugins() )
171+
);
172+
173+
foreach ( $plugin_dependent_pointers_undismissed as $pointer_id ) {
174+
if (
175+
in_array( $plugin_dependent_pointers[ $pointer_id ], $installed_plugin_slugs, true ) &&
176+
! in_array( $pointer_id, $dismissed_pointer_ids, true )
177+
) {
178+
$auto_dismissed_pointer_ids[] = $pointer_id;
179+
}
179180
}
180181
}
181-
if ( $dismissed_pointers_updated ) {
182+
183+
// Persist the automatically-dismissed pointers.
184+
if ( count( $auto_dismissed_pointer_ids ) > 0 ) {
185+
$dismissed_pointer_ids = array_unique( array_merge( $dismissed_pointer_ids, $auto_dismissed_pointer_ids ) );
182186
update_user_meta(
183187
get_current_user_id(),
184188
'dismissed_wp_pointers',
185189
implode( ',', $dismissed_pointer_ids )
186190
);
187191
}
188192

193+
// Determine which admin pointers we need.
189194
$new_install_pointer_id = 'perflab-admin-pointer';
190195
if ( ! in_array( $new_install_pointer_id, $dismissed_pointer_ids, true ) ) {
191196
$needed_pointer_ids = array( $new_install_pointer_id );
192197
} else {
193-
$needed_pointer_ids = array_diff( $admin_pointer_ids, $dismissed_pointer_ids );
198+
$needed_pointer_ids = $admin_pointer_ids;
194199
}
200+
$needed_pointer_ids = array_diff( $needed_pointer_ids, $dismissed_pointer_ids );
195201

196202
// No admin pointers are needed, so abort.
197203
if ( count( $needed_pointer_ids ) === 0 ) {

plugins/performance-lab/tests/includes/admin/test-load.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,24 @@ public function data_provider_test_perflab_admin_pointer(): array {
152152
),
153153
'dashboard_new_dismissed' => array(
154154
'set_up' => static function (): void {
155+
// Note: If the No-cache BFCache plugin (not part of the monorepo) is installed, then this test will likely fail and it should be skipped.
155156
update_user_meta( wp_get_current_user()->ID, 'dismissed_wp_pointers', 'perflab-admin-pointer' );
156157
},
157158
'hook_suffix' => 'index.php',
158159
'expected' => true,
159160
'assert' => null,
160161
'dismissed_wp_pointers' => 'perflab-admin-pointer,perflab-feature-view-transitions',
161162
),
163+
'dashboard_one_dismissed' => array(
164+
'set_up' => static function (): void {
165+
// Note: The No-cache BFCache plugin is not part of the monorepo, so it is not automatically installed in the dev environment.
166+
update_user_meta( wp_get_current_user()->ID, 'dismissed_wp_pointers', 'perflab-admin-pointer,perflab-feature-nocache-bfcache' );
167+
},
168+
'hook_suffix' => 'index.php',
169+
'expected' => false,
170+
'assert' => null,
171+
'dismissed_wp_pointers' => 'perflab-admin-pointer,perflab-feature-nocache-bfcache,perflab-feature-view-transitions',
172+
),
162173
'dashboard_all_dismissed' => array(
163174
'set_up' => static function (): void {
164175
update_user_meta( wp_get_current_user()->ID, 'dismissed_wp_pointers', implode( ',', array_keys( perflab_get_admin_pointers() ) ) );

0 commit comments

Comments
 (0)