@@ -67,6 +67,36 @@ function perflab_render_settings_page(): void {
67
67
<?php
68
68
}
69
69
70
+ /**
71
+ * Gets dismissed admin pointer IDs.
72
+ *
73
+ * @since n.e.x.t
74
+ *
75
+ * @return non-empty-string[] Dismissed admin pointer IDs.
76
+ */
77
+ function perflab_get_dismissed_admin_pointer_ids (): array {
78
+ return array_filter (
79
+ explode (
80
+ ', ' ,
81
+ (string ) get_user_meta ( get_current_user_id (), 'dismissed_wp_pointers ' , true )
82
+ )
83
+ );
84
+ }
85
+
86
+ /**
87
+ * Gets the admin pointers.
88
+ *
89
+ * @since n.e.x.t
90
+ *
91
+ * @return array<non-empty-string, string> Admin pointer messages with the admin pointer IDs as the keys.
92
+ */
93
+ function perflab_get_admin_pointers (): array {
94
+ return array (
95
+ 'perflab-admin-pointer ' => __ ( 'You can now test upcoming WordPress performance features. ' , 'performance-lab ' ),
96
+ 'perflab-feature-view-transitions ' => __ ( 'New <strong>View Transitions</strong> feature now available. ' , 'performance-lab ' ),
97
+ );
98
+ }
99
+
70
100
/**
71
101
* Initializes admin pointer.
72
102
*
@@ -84,18 +114,27 @@ function perflab_admin_pointer( ?string $hook_suffix = '' ): void {
84
114
return ;
85
115
}
86
116
$ current_user = get_current_user_id ();
87
- $ dismissed = array_filter ( explode ( ', ' , (string ) get_user_meta ( get_current_user_id (), 'dismissed_wp_pointers ' , true ) ) );
117
+ $ pointers = array_keys ( perflab_get_admin_pointers () );
118
+ $ dismissed = perflab_get_dismissed_admin_pointer_ids ();
88
119
89
- if ( in_array ( 'perflab-admin-pointer ' , $ dismissed , true ) ) {
120
+ // All pointers have been dismissed already.
121
+ if ( count ( array_diff ( $ pointers , $ dismissed ) ) === 0 ) {
90
122
return ;
91
123
}
92
124
125
+ // Do not show the admin pointer when not on the dashboard or plugins list table.
93
126
if ( ! in_array ( $ hook_suffix , array ( 'index.php ' , 'plugins.php ' ), true ) ) {
94
127
95
- // Do not show on the settings page and dismiss the pointer.
96
- if ( isset ( $ _GET ['page ' ] ) && PERFLAB_SCREEN === $ _GET ['page ' ] && ( ! in_array ( 'perflab-admin-pointer ' , $ dismissed , true ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
97
- $ dismissed [] = 'perflab-admin-pointer ' ;
98
- update_user_meta ( $ current_user , 'dismissed_wp_pointers ' , implode ( ', ' , $ dismissed ) );
128
+ // And if we're on the Performance screen, automatically dismiss the pointers.
129
+ if ( isset ( $ _GET ['page ' ] ) && PERFLAB_SCREEN === $ _GET ['page ' ] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
130
+ update_user_meta (
131
+ $ current_user ,
132
+ 'dismissed_wp_pointers ' ,
133
+ implode (
134
+ ', ' ,
135
+ array_unique ( array_merge ( $ dismissed , $ pointers ) )
136
+ )
137
+ );
99
138
}
100
139
101
140
return ;
@@ -113,53 +152,84 @@ function perflab_admin_pointer( ?string $hook_suffix = '' ): void {
113
152
*
114
153
* Handles the rendering of the admin pointer.
115
154
*
155
+ * @todo Eliminate this function in favor of putting it inside of perflab_admin_pointer() which attaches an inline script.
156
+ *
116
157
* @since 1.0.0
117
158
* @since 2.4.0 Optional arguments were added to make the function reusable for different pointers.
118
- *
119
- * @param string $pointer_id Optional. ID of the pointer. Default 'perflab-admin-pointer'.
120
- * @param array{heading?: string, content?: string} $args Optional. Pointer arguments. Supports 'heading' and 'content' entries.
121
- * Defaults are the heading and content for the 'perflab-admin-pointer'.
159
+ * @since n.e.x.t Unused arguments removed.
122
160
*/
123
- function perflab_render_pointer ( string $ pointer_id = 'perflab-admin-pointer ' , array $ args = array () ): void {
124
- if ( ! isset ( $ args ['heading ' ] ) ) {
125
- $ args ['heading ' ] = __ ( 'Performance Lab ' , 'performance-lab ' );
126
- }
127
- if ( ! isset ( $ args ['content ' ] ) ) {
128
- $ args ['content ' ] = sprintf (
129
- /* translators: %s: settings page link */
130
- esc_html__ ( 'You can now test upcoming WordPress performance features. Open %s to individually toggle the performance features. ' , 'performance-lab ' ),
131
- '<a href=" ' . esc_url ( add_query_arg ( 'page ' , PERFLAB_SCREEN , admin_url ( 'options-general.php ' ) ) ) . '"> ' . esc_html__ ( 'Settings > Performance ' , 'performance-lab ' ) . '</a> '
132
- );
161
+ function perflab_render_pointer (): void {
162
+ $ new_install_pointer_id = 'perflab-admin-pointer ' ;
163
+ $ perflab_admin_pointers = perflab_get_admin_pointers ();
164
+ $ dismissed_pointer_ids = perflab_get_dismissed_admin_pointer_ids ();
165
+
166
+ if ( ! in_array ( $ new_install_pointer_id , $ dismissed_pointer_ids , true ) ) {
167
+ $ needed_pointer_ids = array ( $ new_install_pointer_id );
168
+ } else {
169
+ $ needed_pointer_ids = array_diff ( array_keys ( $ perflab_admin_pointers ), $ dismissed_pointer_ids );
133
170
}
134
171
172
+ $ args = array (
173
+ 'heading ' => __ ( 'Performance Lab ' , 'performance-lab ' ),
174
+ );
175
+
176
+ $ args ['content ' ] = implode (
177
+ '' ,
178
+ array_map (
179
+ static function ( string $ needed_pointer ) use ( $ perflab_admin_pointers ): string {
180
+ return '<p> ' . $ perflab_admin_pointers [ $ needed_pointer ] . '</p> ' ;
181
+ },
182
+ $ needed_pointer_ids
183
+ )
184
+ );
185
+
186
+ $ args ['content ' ] .= '<p> ' . sprintf (
187
+ /* translators: %s: settings page link */
188
+ esc_html__ ( 'Open %s to individually toggle the performance features. ' , 'performance-lab ' ),
189
+ '<a href=" ' . esc_url ( add_query_arg ( 'page ' , PERFLAB_SCREEN , admin_url ( 'options-general.php ' ) ) ) . '"> ' . esc_html__ ( 'Settings > Performance ' , 'performance-lab ' ) . '</a> '
190
+ ) . '</p> ' ;
191
+
135
192
$ wp_kses_options = array (
136
- 'a ' => array (
193
+ 'a ' => array (
137
194
'href ' => array (),
138
195
),
196
+ 'p ' => array (),
197
+ 'strong ' => array (),
139
198
);
140
199
200
+ $ pointer_ids_to_dismiss = array_values ( array_diff ( array_keys ( $ perflab_admin_pointers ), $ dismissed_pointer_ids ) );
141
201
?>
142
- <script id=" <?php echo esc_attr ( $ pointer_id ); ?> " type="text/javascript" >
202
+ <script>
143
203
jQuery( function() {
204
+ const pointerIdsToDismiss = <?php echo wp_json_encode ( $ pointer_ids_to_dismiss , JSON_OBJECT_AS_ARRAY ); ?> ;
205
+ const nonce = <?php echo wp_json_encode ( wp_create_nonce ( 'dismiss_pointer ' ) ); ?> ;
206
+
207
+ function dismissNextPointer() {
208
+ const pointerId = pointerIdsToDismiss.shift();
209
+ if ( ! pointerId ) {
210
+ return;
211
+ }
212
+
213
+ jQuery.post(
214
+ window.ajaxurl,
215
+ {
216
+ pointer: pointerId,
217
+ action: 'dismiss-wp-pointer',
218
+ _wpnonce: nonce,
219
+ }
220
+ ).then( dismissNextPointer );
221
+ }
222
+
144
223
// Pointer Options.
145
224
const options = {
146
- content: <?php echo wp_json_encode ( '<h3> ' . esc_html ( $ args ['heading ' ] ) . '</h3><p> ' . wp_kses ( $ args ['content ' ], $ wp_kses_options ) . ' </p> ' ); ?> ,
225
+ content: <?php echo wp_json_encode ( '<h3> ' . esc_html ( $ args ['heading ' ] ) . '</h3> ' . wp_kses ( $ args ['content ' ], $ wp_kses_options ) ); ?> ,
147
226
position: {
148
227
edge: 'left',
149
228
align: 'right',
150
229
},
151
230
pointerClass: 'wp-pointer arrow-top',
152
231
pointerWidth: 420,
153
- close: function() {
154
- jQuery.post(
155
- window.ajaxurl,
156
- {
157
- pointer: <?php echo wp_json_encode ( $ pointer_id ); ?> ,
158
- action: 'dismiss-wp-pointer',
159
- _wpnonce: <?php echo wp_json_encode ( wp_create_nonce ( 'dismiss_pointer ' ) ); ?> ,
160
- }
161
- );
162
- }
232
+ close: dismissNextPointer
163
233
};
164
234
165
235
jQuery( '#menu-settings' ).pointer( options ).pointer( 'open' );
@@ -207,7 +277,11 @@ function perflab_plugin_action_links_add_settings( $links ) {
207
277
* @since 2.3.0
208
278
*/
209
279
function perflab_dismiss_wp_pointer_wrapper (): void {
210
- if ( isset ( $ _POST ['pointer ' ] ) && 'perflab-admin-pointer ' !== $ _POST ['pointer ' ] ) {
280
+ if (
281
+ isset ( $ _POST ['pointer ' ] )
282
+ &&
283
+ ! in_array ( $ _POST ['pointer ' ], array_keys ( perflab_get_admin_pointers () ), true )
284
+ ) {
211
285
// Another plugin's pointer, do nothing.
212
286
return ;
213
287
}
0 commit comments