PluginProbe
TablePress – Tables in WordPress made easy / 3.3
TablePress – Tables in WordPress made easy v3.3
3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.4.4 All 44 releases
tablepress / classes / class-view.php

class-view.php in TablePress – Tables in WordPress made easy 3.3, at classes/class-view.php

657 lines 24.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * TablePress Base View with members and methods for all views
4 *
5 * @package TablePress
6 * @subpackage Views
7 * @author Tobias Bäthge
8 * @since 1.0.0
9 */
10
11 // Prohibit direct script loading.
12 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13
14 /**
15 * TablePress Base View class
16 *
17 * @package TablePress
18 * @subpackage Views
19 * @author Tobias Bäthge
20 * @since 1.0.0
21 */
22 abstract class TablePress_View {
23
24 /**
25 * Data for the view.
26 *
27 * @since 1.0.0
28 * @var array<string, mixed>
29 */
30 protected array $data = array();
31
32 /**
33 * Number of screen columns for post boxes.
34 *
35 * @since 1.0.0
36 */
37 protected int $screen_columns = 0;
38
39 /**
40 * User action for this screen.
41 *
42 * @since 1.0.0
43 */
44 protected string $action = '';
45
46 /**
47 * List of text boxes (similar to post boxes, but just with text and without extra functionality).
48 *
49 * @since 1.0.0
50 * @var array<string, array<string, array<string, mixed>>>
51 */
52 protected array $textboxes = array();
53
54 /**
55 * List of messages that are to be displayed as boxes below the page title.
56 *
57 * @since 1.0.0
58 * @var string[]
59 */
60 protected array $header_messages = array();
61
62 /**
63 * Whether there are post boxes registered for this screen,
64 * is automatically set to true, when a meta box is added.
65 *
66 * @since 1.0.0
67 */
68 protected bool $has_meta_boxes = false;
69
70 /**
71 * List of WP feature pointers for this view.
72 *
73 * @since 1.0.0
74 * @var string[]
75 */
76 protected array $wp_pointers = array();
77
78 /**
79 * Initializes the View class, by setting the correct screen columns and adding help texts.
80 *
81 * @since 1.0.0
82 */
83 public function __construct() {
84 $screen = get_current_screen();
85 if ( 0 !== $this->screen_columns ) {
86 $screen->add_option( 'layout_columns', array( 'max' => $this->screen_columns ) ); // @phpstan-ignore method.nonObject
87 }
88 // Enable two column layout.
89 add_filter( "get_user_option_screen_layout_{$screen->id}", array( $this, 'set_current_screen_layout_columns' ) ); // @phpstan-ignore property.nonObject
90
91 /* translators: %1$s: URL to TablePress website, %2$s: URL to WordPress Plugin Directory */
92 $common_content = '<p>' . sprintf( __( 'More information about TablePress can be found on the <a href="%1$s">plugin website</a> or on its page in the <a href="%2$s">WordPress Plugin Directory</a>.', 'tablepress' ), 'https://tablepress.org/', 'https://wordpress.org/plugins/tablepress/' ) . '</p>';
93 /* translators: %s: URL to Documentation page */
94 $common_content .= '<p>' . sprintf( __( 'For technical information, please see the <a href="%s">Documentation</a>.', 'tablepress' ), 'https://tablepress.org/documentation/' ) . ' ';
95 /* translators: %s: URL to FAQ page */
96 $common_content .= sprintf( __( 'Common questions are answered in the <a href="%s">FAQ</a>.', 'tablepress' ), 'https://tablepress.org/faq/' ) . '</p>';
97
98 if ( tb_tp_fs()->is_free_plan() ) {
99 $common_content .= '<p>'
100 . sprintf( __( '<a href="%1$s">Support</a> is provided through the <a href="%2$s">WordPress Support Forums</a>.', 'tablepress' ), 'https://tablepress.org/support/', 'https://wordpress.org/support/plugin/tablepress' )
101 . ' '
102 . sprintf( __( 'Before asking for support, please carefully read the <a href="%s">Frequently Asked Questions</a>, where you will find answers to the most common questions, and search through the forums.', 'tablepress' ), 'https://tablepress.org/faq/' )
103 . '</p>';
104 $common_content .= '<p><strong>' . sprintf( __( 'More great features for you and your site’s visitors and priority email support are available with a Premium license plan of TablePress. <a href="%s">Go check them out!</a>', 'tablepress' ), 'https://tablepress.org/premium/?utm_source=plugin&utm_medium=textlink&utm_content=help-tab' ) . '</strong></p>';
105 }
106
107 $screen->add_help_tab( array( // @phpstan-ignore method.nonObject
108 'id' => 'tablepress-help', // This should be unique for the screen.
109 'title' => __( 'TablePress Help', 'tablepress' ),
110 'content' => '<p>' . $this->help_tab_content() . '</p>' . $common_content,
111 ) );
112 // "Sidebar" in the help tab.
113 $screen->set_help_sidebar( // @phpstan-ignore method.nonObject
114 '<p><strong>' . __( 'For more information:', 'tablepress' ) . '</strong></p>'
115 . '<p><a href="https://tablepress.org/">TablePress Website</a></p>'
116 . '<p><a href="https://tablepress.org/faq/">TablePress FAQ</a></p>'
117 . '<p><a href="https://tablepress.org/documentation/">TablePress Documentation</a></p>'
118 . '<p><a href="https://tablepress.org/support/">TablePress Support</a></p>'
119 );
120 }
121
122 /**
123 * Changes the value of the user option "screen_layout_{$screen->id}" through a filter.
124 *
125 * @since 1.0.0
126 *
127 * @param int|false $result Current value of the user option.
128 * @return int New value for the user option.
129 */
130 public function set_current_screen_layout_columns( /* int|false */ $result ): int {
131 if ( false === $result ) {
132 // The user option does not yet exist.
133 $result = $this->screen_columns;
134 } elseif ( $result > $this->screen_columns ) {
135 // The value of the user option is bigger than what is possible on this screen (e.g. because the number of columns was reduced in an update).
136 $result = $this->screen_columns;
137 }
138 return $result;
139 }
140
141 /**
142 * Sets up the view with data and do things that are necessary for all views.
143 *
144 * @since 1.0.0
145 *
146 * @param string $action Action for this view.
147 * @param array<string, mixed> $data Data for this view.
148 */
149 public function setup( /* string */ $action, array $data ) /* : void */ {
150 // Don't use type hints (except array $data) in method declaration, as the method is extended in some TablePress Extensions which are no longer updated.
151
152 $this->action = $action;
153 $this->data = $data;
154
155 // Set page title.
156 $GLOBALS['title'] = sprintf( __( '%1$s &lsaquo; %2$s', 'tablepress' ), $this->data['view_actions'][ $this->action ]['page_title'], 'TablePress' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
157
158 add_filter( 'admin_footer_text', array( $this, 'add_admin_footer_text' ) );
159
160 TablePress::enqueue_style( 'common', array( 'wp-components' ) );
161 // RTL styles for the admin interface.
162 if ( is_rtl() ) {
163 TablePress::enqueue_style( 'common-rtl', array( 'tablepress-common' ) );
164 }
165
166 // Initialize WP feature pointers for TablePress.
167 $this->_init_wp_pointers();
168
169 // Necessary fields for all views.
170 $this->add_text_box( 'default_nonce_fields', array( $this, 'default_nonce_fields' ), 'header', false );
171 $this->add_text_box( 'action_nonce_field', array( $this, 'action_nonce_field' ), 'header', false );
172 $this->add_text_box( 'action_field', array( $this, 'action_field' ), 'header', false );
173
174 if ( tb_tp_fs()->is_plan_or_trial__premium_only( 'pro' ) && ! tb_tp_fs()->is_paying_or_trial__premium_only() ) {
175 $message = '<span class="dashicons dashicons-warning" style="color:#cc1818"></span> ';
176 $message .= '<strong>' . sprintf( __( 'Your TablePress %s premium license has expired!', 'tablepress' ), tb_tp_fs()->is_plan_or_trial( 'max', true ) ? 'Max' : 'Pro' ) . '</strong> ';
177 $message .= sprintf(
178 __( 'Please <a href="%1$s"><strong>renew your license now</strong></a> to continue receiving direct developer support, new features, and security updates for TablePress.', 'tablepress' ),
179 esc_url( tb_tp_fs()->checkout_url( WP_FS__PERIOD_ANNUALLY, false ) ),
180 );
181
182 $this->add_header_message( $message, 'is-warning not-dismissible' );
183 }
184 }
185
186 /**
187 * Registers a header message for the view.
188 *
189 * @since 1.0.0
190 *
191 * @param string $text Text for the header message.
192 * @param string $css_class Optional. Additional CSS class for the header message.
193 * @param string $title Optional. Text for the header title.
194 */
195 protected function add_header_message( string $text, string $css_class = 'is-success', string $title = '' ): void {
196 if ( ! str_contains( $css_class, 'not-dismissible' ) ) {
197 $css_class .= ' is-dismissible';
198 }
199 if ( '' !== $title ) {
200 $title = "<h3>{$title}</h3>";
201 }
202 // Wrap the message text in HTML <p> tags if it does not already start with one (potentially with attributes), indicating custom message HTML.
203 if ( '' !== $text && ! str_starts_with( $text, '<p' ) ) {
204 $text = "<p>{$text}</p>";
205 }
206 $this->header_messages[] = "<div class=\"notice components-notice {$css_class}\"><div class=\"components-notice__content\">{$title}{$text}</div></div>\n";
207 }
208
209 /**
210 * Processes header action messages, i.e. check if a message should be added to the page.
211 *
212 * @since 1.0.0
213 *
214 * @param array<string, string> $action_messages Action messages for the screen.
215 */
216 protected function process_action_messages( array $action_messages ): void {
217 if ( $this->data['message'] && isset( $action_messages[ $this->data['message'] ] ) ) {
218 $class = ( str_starts_with( $this->data['message'], 'error' ) ) ? 'is-error' : 'is-success';
219
220 if ( '' !== $this->data['error_details'] ) {
221 $this->data['error_details'] = '</p><p>' . sprintf( __( 'Error code: %s', 'tablepress' ), '<code>' . esc_html( $this->data['error_details'] ) . '</code>' );
222 }
223
224 $this->add_header_message( "<strong>{$action_messages[ $this->data['message'] ]}</strong>{$this->data['error_details']}", $class );
225 }
226 }
227
228 /**
229 * Registers a text box for the view.
230 *
231 * @since 1.0.0
232 *
233 * @param string $id Unique HTML ID for the text box container (only visible with $wrap = true).
234 * @param callable $callback Callback that prints the contents of the text box.
235 * @param string $context Optional. Context/position of the text box (normal, side, additional, header, submit).
236 * @param bool $wrap Whether the content of the text box shall be wrapped in a <div> container.
237 */
238 protected function add_text_box( string $id, callable $callback, string $context = 'normal', bool $wrap = false ): void {
239 if ( ! isset( $this->textboxes[ $context ] ) ) {
240 $this->textboxes[ $context ] = array();
241 }
242
243 $long_id = "tablepress_{$this->action}-{$id}";
244 $this->textboxes[ $context ][ $id ] = array(
245 'id' => $long_id,
246 'callback' => $callback,
247 'context' => $context,
248 'wrap' => $wrap,
249 );
250 }
251
252 /**
253 * Registers a post meta box for the view, that is drag/droppable with WordPress functionality.
254 *
255 * @since 1.0.0
256 *
257 * @param string $id Unique ID for the meta box.
258 * @param string $title Title for the meta box.
259 * @param callable $callback Callback that prints the contents of the post meta box.
260 * @param 'normal'|'side'|'additional' $context Optional. Context/position of the post meta box (normal, side, additional).
261 * @param 'core'|'default'|'high'|'low' $priority Optional. Order of the post meta box for the $context position (high, default, low).
262 * @param mixed[]|null $callback_args Optional. Additional data for the callback function (e.g. useful when in different class).
263 */
264 protected function add_meta_box( string $id, string $title, callable $callback, string $context = 'normal', string $priority = 'default', ?array $callback_args = null ): void {
265 $this->has_meta_boxes = true;
266 add_meta_box( "tablepress_{$this->action}-{$id}", $title, $callback, null, $context, $priority, $callback_args );
267 }
268
269 /**
270 * Renders all text boxes for the given context.
271 *
272 * @since 1.0.0
273 *
274 * @param string $context Context (normal, side, additional, header, submit) for which registered text boxes shall be rendered.
275 */
276 protected function do_text_boxes( string $context ): void {
277 if ( empty( $this->textboxes[ $context ] ) ) {
278 return;
279 }
280
281 foreach ( $this->textboxes[ $context ] as $box ) {
282 if ( $box['wrap'] ) {
283 echo "<div id=\"{$box['id']}\" class=\"textbox\">\n";
284 }
285 call_user_func( $box['callback'], $this->data, $box );
286 if ( $box['wrap'] ) {
287 echo "</div>\n";
288 }
289 }
290 }
291
292 /**
293 * Renders all post meta boxes for the given context, if there are post meta boxes.
294 *
295 * @since 1.0.0
296 *
297 * @param string $context Context (normal, side, additional) for which registered post meta boxes shall be rendered.
298 */
299 protected function do_meta_boxes( string $context ): void {
300 if ( $this->has_meta_boxes ) {
301 do_meta_boxes( get_current_screen(), $context, $this->data ); // @phpstan-ignore argument.type
302 }
303 }
304
305 /**
306 * Prints hidden fields with nonces for post meta box AJAX handling, if there are post meta boxes on the screen.
307 *
308 * The check is possible as this function is executed after post meta boxes have to be registered.
309 *
310 * @since 1.0.0
311 *
312 * @param array<string, mixed> $data Data for this screen.
313 * @param array<string, mixed> $box Information about the text box.
314 */
315 protected function default_nonce_fields( array $data, array $box ): void {
316 if ( ! $this->has_meta_boxes ) {
317 return;
318 }
319 wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
320 echo "\n";
321 wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
322 echo "\n";
323 }
324
325 /**
326 * Prints hidden field with a nonce for the screen's action, to be transmitted in HTTP requests.
327 *
328 * @since 1.0.0
329 *
330 * @param array<string, mixed> $data Data for this screen.
331 * @param array<string, mixed> $box Information about the text box.
332 */
333 protected function action_nonce_field( array $data, array $box ): void {
334 wp_nonce_field( TablePress::nonce( $this->action ) );
335 echo "\n";
336 }
337
338 /**
339 * Prints hidden field with the screen action.
340 *
341 * @since 1.0.0
342 *
343 * @param array<string, mixed> $data Data for this screen.
344 * @param array<string, mixed> $box Information about the text box.
345 */
346 protected function action_field( array $data, array $box ): void {
347 echo "<input type=\"hidden\" name=\"action\" value=\"tablepress_{$this->action}\">\n";
348 }
349
350 /**
351 * Renders the current view.
352 *
353 * @since 1.0.0
354 */
355 public function render(): void {
356 ?>
357 <div id="tablepress-page" class="wrap">
358 <?php
359 $this->print_nav_tab_menu();
360 ?>
361 <div id="tablepress-body">
362 <hr class="wp-header-end">
363 <?php
364 // Print all header messages.
365 foreach ( $this->header_messages as $message ) {
366 echo $message;
367 }
368 // "Import" screen has file upload.
369 $enctype = ( 'import' === $this->action ) ? ' enctype="multipart/form-data"' : '';
370 ?>
371 <form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post"<?php echo $enctype; ?> id="tablepress-page-form">
372 <?php
373 $this->do_text_boxes( 'header' );
374 $hide_if_no_js = ( in_array( $this->action, array( 'export', 'import' ), true ) ) ? ' class="hide-if-no-js"' : '';
375 ?>
376 <div id="poststuff"<?php echo $hide_if_no_js; ?>>
377 <div id="post-body" class="metabox-holder columns-<?php echo ( isset( $GLOBALS['screen_layout_columns'] ) && ( 2 === $GLOBALS['screen_layout_columns'] ) ) ? '2' : '1'; ?>">
378 <div id="postbox-container-2" class="postbox-container">
379 <?php
380 $this->do_text_boxes( 'normal' );
381 $this->do_meta_boxes( 'normal' );
382
383 $this->do_text_boxes( 'additional' );
384 $this->do_meta_boxes( 'additional' );
385
386 // Print all submit buttons.
387 $this->do_text_boxes( 'submit' );
388 ?>
389 </div>
390 <div id="postbox-container-1" class="postbox-container">
391 <?php
392 // Print all boxes in the sidebar.
393 $this->do_text_boxes( 'side' );
394 $this->do_meta_boxes( 'side' );
395 ?>
396 </div>
397 </div>
398 <br class="clear">
399 </div>
400 </form>
401 </div>
402 </div>
403 <?php
404 }
405
406 /**
407 * Renders the navigation menu with links to the possible actions, highlighting the current one.
408 *
409 * @since 1.0.0
410 */
411 protected function print_nav_tab_menu(): void {
412 $name = __( 'TablePress', 'tablepress' );
413 $filename = 'admin/img/tablepress.svg';
414 ?>
415 <div id="tablepress-header" class="header">
416 <h1 class="name">
417 <img src="<?php echo plugins_url( $filename, TABLEPRESS__FILE__ ); ?>" alt="<?php esc_attr_e( 'TablePress plugin logo', 'tablepress' ); ?>">
418 <span class="screen-reader-text"><?php echo $name; ?></span>
419 </h1>
420 <?php if ( ! TABLEPRESS_IS_PLAYGROUND_PREVIEW && tb_tp_fs()->is_free_plan() ) : ?>
421 <div class="buttons">
422 <a href="<?php echo esc_url( tb_tp_fs()->pricing_url( WP_FS__PERIOD_ANNUALLY, false ) ); ?>" class="tablepress-button">
423 <span><?php _e( 'Upgrade to Premium', 'tablepress' ); ?></span>
424 <span class="dashicons dashicons-arrow-right-alt"></span>
425 </a>
426 </div>
427 <?php endif; ?>
428 </div>
429 <nav id="tablepress-nav">
430 <ul class="nav-menu">
431 <?php
432 foreach ( $this->data['view_actions'] as $action => $entry ) {
433 if ( '' === $entry['nav_tab_title'] ) {
434 continue;
435 }
436 if ( ! current_user_can( $entry['required_cap'] ) ) {
437 continue;
438 }
439
440 $url = esc_url( TablePress::url( array( 'action' => $action ) ) );
441 $active = ( $action === $this->action ) ? ' active' : '';
442 $separator = ( 'export' === $action ) ? ' separator' : ''; // Make the "Export" entry a separator, for some spacing.
443 echo "<li class=\"nav-item\"><a id=\"tablepress-nav-item-{$action}\" class=\"nav-link{$active}{$separator}\" href=\"{$url}\">{$entry['nav_tab_title']}</a></li>";
444 }
445 ?>
446 </ul>
447 </nav>
448 <?php
449 }
450
451 /**
452 * Prints a notification about JavaScript not being activated in the browser.
453 *
454 * @since 2.0.0
455 *
456 * @param array<string, mixed> $data Data for this screen.
457 * @param array<string, mixed> $box Information about the text box.
458 */
459 public function textbox_no_javascript( array $data, array $box ): void {
460 ?>
461 <div class="notice components-notice is-error hide-if-js">
462 <div class="components-notice__content">
463 <h3><em>
464 <?php _e( 'Attention: Unfortunately, there is a problem!', 'tablepress' ); ?>
465 </em></h3>
466 <p style="font-size:14px">
467 <strong><?php _e( 'This screen requires JavaScript. Please enable JavaScript in your browser settings.', 'tablepress' ); ?></strong><br>
468 <?php _e( 'For help, please follow <a href="https://www.enable-javascript.com/">the instructions on how to enable JavaScript in your browser</a>.', 'tablepress' ); ?>
469 </p>
470 <p>
471 <?php echo '<a href="' . esc_url( TablePress::url( array( 'action' => 'list' ) ) ) . '">' . __( 'Back to the List of Tables', 'tablepress' ) . '</a>'; ?>
472 </p>
473 </div>
474 </div>
475 <?php
476 }
477
478 /**
479 * Prints a submit button (only done when function is used as a callback for a text box).
480 *
481 * This method is soft-deprecated. It's no longer used in TablePress, but e.g. in the "TablePress Debug Extension".
482 *
483 * @since 1.0.0
484 *
485 * @param array<string, mixed> $data Data for this screen.
486 * @param array<string, mixed> $box Information about the text box.
487 */
488 protected function textbox_submit_button( array $data, array $box ): void {
489 ?>
490 <p class="submit"><input type="submit" class="components-button is-primary button-save-changes" value="<?php esc_attr_e( 'Save Changes', 'tablepress' ); ?>"></p>
491 <?php
492 }
493
494 /**
495 * Returns a safe JSON representation of a variable for printing inside of JavaScript code.
496 *
497 * @since 2.0.0
498 *
499 * @param mixed $data Variable to convert to JSON.
500 * @return string Safe JSON representation of a variable for printing inside of JavaScript code.
501 */
502 public function convert_to_json_parse_output( /* string|array|bool|int|float|null */ $data ): string {
503 $json = wp_json_encode( $data, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES );
504 if ( false === $json ) {
505 // JSON encoding failed, return an error object. Use a prefixed "_error" key to avoid conflicts with intentionally added "error" keys.
506 $json = '{ "_error": "The data could not be encoded to JSON!" }';
507 }
508 // Print the JSON data inside a `JSON.parse()` call in JS for speed gains, with necessary escaping of `\` and `'`.
509 $json = str_replace( array( '\\', "'" ), array( '\\\\', "\'" ), $json );
510 return "JSON.parse( '{$json}' )";
511 }
512
513 /**
514 * Prints JavaScript variables for the screen.
515 *
516 * @since 3.1.0
517 *
518 * @param string $variable Name of the JavaScript variable.
519 * @param array<string, mixed> $data Information about the text box.
520 */
521 protected function print_script_data_json( string $variable, array $data ): void {
522 echo "<script>\n";
523 echo "window.tp = window.tp || {};\n";
524 echo "tp.{$variable} = {\n";
525 foreach ( $data as $key => $value ) {
526 $value = $this->convert_to_json_parse_output( $value );
527 echo "\t{$key}: {$value},\n";
528 }
529 echo "};\n";
530 echo "</script>\n";
531 }
532
533 /**
534 * Returns the content for the help tab for this screen.
535 *
536 * Has to be implemented for every view that is visible in the WP Dashboard!
537 *
538 * @since 1.0.0
539 *
540 * @return string Help tab content for the view.
541 */
542 protected function help_tab_content(): string {
543 // Has to be implemented for every view that is visible in the WP Dashboard!
544 return '';
545 }
546
547 /**
548 * Adds a TablePress "Thank You" message to the admin footer content.
549 *
550 * @since 1.0.0
551 * @since 3.3.0 This method was moved from the now-removed `TablePress_Admin_Page` class.
552 *
553 * @param string $content Current admin footer content.
554 * @return string New admin footer content.
555 */
556 public function add_admin_footer_text( /* string */ $content ): string {
557 // Don't use a type hint in the method declaration as many WordPress plugins use the `admin_footer_text` filter without returning a string.
558
559 // Protect against other plugins not returning a string in their filter callbacks.
560 if ( ! is_string( $content ) ) { // @phpstan-ignore function.alreadyNarrowedType (The `is_string()` check is needed as the input is coming from a filter hook.)
561 $content = '';
562 }
563
564 /* translators: %s: URL to TablePress website */
565 $content .= ' &bull; ' . sprintf( __( 'Thank you for using <a href="%s">TablePress</a>.', 'tablepress' ), 'https://tablepress.org/' );
566 if ( tb_tp_fs()->is_free_plan() ) {
567 /* translators: %s: URL to TablePress premium features */
568 $content .= ' ' . sprintf( __( 'Take a look at the <a href="%s">Premium features</a>!', 'tablepress' ), 'https://tablepress.org/premium/?utm_source=plugin&utm_medium=textlink&utm_content=admin-footer' );
569 }
570 return $content;
571 }
572
573 /**
574 * Initializes the WP feature pointers for TablePress.
575 *
576 * @since 1.0.0
577 */
578 protected function _init_wp_pointers(): void {
579 // Check if there are WP pointers for this view.
580 if ( empty( $this->wp_pointers ) ) {
581 return;
582 }
583
584 // Get dismissed pointers.
585 $dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
586
587 $pointers_on_page = false;
588 foreach ( array_diff( $this->wp_pointers, $dismissed ) as $pointer ) {
589 // Bind pointer print function.
590 add_action( "admin_footer-{$GLOBALS['hook_suffix']}", array( $this, 'wp_pointer_' . $pointer ) ); // @phpstan-ignore argument.type
591 $pointers_on_page = true;
592 }
593
594 if ( $pointers_on_page ) {
595 wp_enqueue_style( 'wp-pointer' );
596 wp_enqueue_script( 'wp-pointer' );
597 }
598 }
599
600 /**
601 * Prints the JavaScript code for a WP feature pointer.
602 *
603 * @since 1.0.0
604 * @since 3.3.0 This method was moved from the now-removed `TablePress_Admin_Page` class.
605 *
606 * @param string $pointer_id The pointer ID.
607 * @param string $selector The HTML elements, on which the pointer should be attached.
608 * @param array<string, mixed> $args Arguments to be passed to the pointer JS (see wp-pointer.js).
609 */
610 public function print_wp_pointer_js( string $pointer_id, string $selector, array $args ): void {
611 if ( empty( $pointer_id ) || empty( $selector ) || empty( $args['content'] ) ) {
612 return;
613 }
614
615 $keyboard_shortcut = '';
616 if ( 'tp33_edit_quick_navigation' === $pointer_id ) {
617 $keyboard_shortcut = <<<JS
618 content: options.content.replace( /%metaKey%/g, window?.navigator?.platform?.includes( 'Mac' ) ? wp.i18n._x( '', 'keyboard shortcut modifier key on a Mac keyboard', 'tablepress' ) : wp.i18n._x( 'Ctrl+', 'keyboard shortcut modifier key on a non-Mac keyboard', 'tablepress' ) ),\n
619 JS;
620 }
621
622 /*
623 * Print JS code for the feature pointers, extended with event handling for opened/closed "Screen Options", so that pointers can
624 * be repositioned. 210 ms is slightly slower than jQuery's "fast" value, to allow all elements to reach their original position.
625 */
626 ?>
627 <script>
628 ( ( $ ) => {
629 let options = <?php echo wp_json_encode( $args, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?>;
630 if ( ! options ) {
631 return;
632 }
633
634 options = {
635 ...options,
636 <?php echo $keyboard_shortcut; ?>
637 close() {
638 $.post( ajaxurl, {
639 pointer: '<?php echo $pointer_id; ?>',
640 action: 'dismiss-wp-pointer'
641 } );
642 $( this ).pointer( { 'disabled': true } );
643 },
644 };
645
646 $( () => $( '<?php echo $selector; ?>' ).pointer( options ).pointer( 'open' ) );
647
648 $( document ).on( 'screen:options:open screen:options:close', () => {
649 setTimeout( () => $( '<?php echo $selector; ?>' ).pointer( 'reposition' ), 210 );
650 } );
651 } )( jQuery );
652 </script>
653 <?php
654 }
655
656 } // class TablePress_View
657