Skip to content

Commit 695ea71

Browse files
committed
Refactor wp-pointer logic to use inline script
1 parent 3187e8c commit 695ea71

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

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

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ function perflab_admin_pointer( ?string $hook_suffix = '' ): void {
123123
if ( is_network_admin() || is_user_admin() ) {
124124
return;
125125
}
126-
$current_user = get_current_user_id();
127-
$pointers = array_keys( perflab_get_admin_pointers() );
128-
$dismissed = perflab_get_dismissed_admin_pointer_ids();
126+
127+
$admin_pointers = perflab_get_admin_pointers();
128+
$admin_pointer_ids = array_keys( $admin_pointers );
129+
$dismissed_pointer_ids = perflab_get_dismissed_admin_pointer_ids();
129130

130131
// All pointers have been dismissed already.
131-
if ( count( array_diff( $pointers, $dismissed ) ) === 0 ) {
132+
if ( count( array_diff( $admin_pointer_ids, $dismissed_pointer_ids ) ) === 0 ) {
132133
return;
133134
}
134135

@@ -138,11 +139,11 @@ function perflab_admin_pointer( ?string $hook_suffix = '' ): void {
138139
// And if we're on the Performance screen, automatically dismiss the pointers.
139140
if ( isset( $_GET['page'] ) && PERFLAB_SCREEN === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
140141
update_user_meta(
141-
$current_user,
142+
get_current_user_id(),
142143
'dismissed_wp_pointers',
143144
implode(
144145
',',
145-
array_unique( array_merge( $dismissed, $pointers ) )
146+
array_unique( array_merge( $dismissed_pointer_ids, $admin_pointer_ids ) )
146147
)
147148
);
148149
}
@@ -153,30 +154,12 @@ function perflab_admin_pointer( ?string $hook_suffix = '' ): void {
153154
// Enqueue pointer CSS and JS.
154155
wp_enqueue_style( 'wp-pointer' );
155156
wp_enqueue_script( 'wp-pointer' );
156-
add_action( 'admin_print_footer_scripts', 'perflab_render_pointer', 10, 0 );
157-
}
158-
add_action( 'admin_enqueue_scripts', 'perflab_admin_pointer' );
159157

160-
/**
161-
* Renders the Admin Pointer.
162-
*
163-
* Handles the rendering of the admin pointer.
164-
*
165-
* @todo Eliminate this function in favor of putting it inside of perflab_admin_pointer() which attaches an inline script.
166-
*
167-
* @since 1.0.0
168-
* @since 2.4.0 Optional arguments were added to make the function reusable for different pointers.
169-
* @since n.e.x.t Unused arguments removed.
170-
*/
171-
function perflab_render_pointer(): void {
172158
$new_install_pointer_id = 'perflab-admin-pointer';
173-
$perflab_admin_pointers = perflab_get_admin_pointers();
174-
$dismissed_pointer_ids = perflab_get_dismissed_admin_pointer_ids();
175-
176159
if ( ! in_array( $new_install_pointer_id, $dismissed_pointer_ids, true ) ) {
177160
$needed_pointer_ids = array( $new_install_pointer_id );
178161
} else {
179-
$needed_pointer_ids = array_diff( array_keys( $perflab_admin_pointers ), $dismissed_pointer_ids );
162+
$needed_pointer_ids = array_diff( array_keys( $admin_pointers ), $dismissed_pointer_ids );
180163
}
181164

182165
$args = array(
@@ -186,8 +169,8 @@ function perflab_render_pointer(): void {
186169
$args['content'] = implode(
187170
'',
188171
array_map(
189-
static function ( string $needed_pointer ) use ( $perflab_admin_pointers ): string {
190-
return '<p>' . $perflab_admin_pointers[ $needed_pointer ] . '</p>';
172+
static function ( string $needed_pointer ) use ( $admin_pointers ): string {
173+
return '<p>' . $admin_pointers[ $needed_pointer ] . '</p>';
191174
},
192175
$needed_pointer_ids
193176
)
@@ -207,7 +190,9 @@ static function ( string $needed_pointer ) use ( $perflab_admin_pointers ): stri
207190
'strong' => array(),
208191
);
209192

210-
$pointer_ids_to_dismiss = array_values( array_diff( array_keys( $perflab_admin_pointers ), $dismissed_pointer_ids ) );
193+
$pointer_ids_to_dismiss = array_values( array_diff( array_keys( $admin_pointers ), $dismissed_pointer_ids ) );
194+
195+
ob_start();
211196
?>
212197
<script>
213198
jQuery( function() {
@@ -246,7 +231,12 @@ function dismissNextPointer() {
246231
} );
247232
</script>
248233
<?php
234+
$processor = new WP_HTML_Tag_Processor( (string) ob_get_clean() );
235+
if ( $processor->next_tag( array( 'tag_name' => 'SCRIPT' ) ) ) {
236+
wp_add_inline_script( 'wp-pointer', $processor->get_modifiable_text() );
237+
}
249238
}
239+
add_action( 'admin_enqueue_scripts', 'perflab_admin_pointer' );
250240

251241
/**
252242
* Adds a link to the features page to the plugin's entry in the plugins list table.

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,17 @@ public function test_perflab_admin_pointer( ?Closure $set_up, ?string $hook_suff
208208
}
209209
$this->assertFalse( is_network_admin() || is_user_admin() );
210210
perflab_admin_pointer( $hook_suffix );
211-
$this->assertSame( $expected ? 10 : false, has_action( 'admin_print_footer_scripts', 'perflab_render_pointer' ) );
211+
212+
$after_script = '';
213+
$script_dependency = wp_scripts()->query( 'wp-pointer' );
214+
if ( $script_dependency instanceof _WP_Dependency ) {
215+
$after_script = implode( "\n", array_filter( $script_dependency->extra['after'] ?? array() ) );
216+
}
217+
if ( $expected ) {
218+
$this->assertStringContainsString( 'pointerIdsToDismiss', $after_script );
219+
} else {
220+
$this->assertStringNotContainsString( 'pointerIdsToDismiss', $after_script );
221+
}
212222
$this->assertSame( $expected, wp_script_is( 'wp-pointer', 'enqueued' ) );
213223
$this->assertSame( $expected, wp_style_is( 'wp-pointer', 'enqueued' ) );
214224
$this->assertSame( $dismissed_wp_pointers, get_user_meta( $user_id, 'dismissed_wp_pointers', true ) );

0 commit comments

Comments
 (0)