PluginProbe
TablePress – Tables in WordPress made easy / trunk
TablePress – Tables in WordPress made easy vtrunk
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 trunk, at classes/class-view.php

646 lines 23.6 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
175 /**
176 * Registers a header message for the view.
177 *
178 * @since 1.0.0
179 *
180 * @param string $text Text for the header message.
181 * @param string $css_class Optional. Additional CSS class for the header message.
182 * @param string $title Optional. Text for the header title.
183 */
184 protected function add_header_message( string $text, string $css_class = 'is-success', string $title = '' ): void {
185 if ( ! str_contains( $css_class, 'not-dismissible' ) ) {
186 $css_class .= ' is-dismissible';
187 }
188 if ( '' !== $title ) {
189 $title = "<h3>{$title}</h3>";
190 }
191 // Wrap the message text in HTML <p> tags if it does not already start with one (potentially with attributes), indicating custom message HTML.
192 if ( '' !== $text && ! str_starts_with( $text, '<p' ) ) {
193 $text = "<p>{$text}</p>";
194 }
195 $this->header_messages[] = "<div class=\"notice components-notice {$css_class}\"><div class=\"components-notice__content\">{$title}{$text}</div></div>\n";
196 }
197
198 /**
199 * Processes header action messages, i.e. check if a message should be added to the page.
200 *
201 * @since 1.0.0
202 *
203 * @param array<string, string> $action_messages Action messages for the screen.
204 */
205 protected function process_action_messages( array $action_messages ): void {
206 if ( $this->data['message'] && isset( $action_messages[ $this->data['message'] ] ) ) {
207 $class = ( str_starts_with( $this->data['message'], 'error' ) ) ? 'is-error' : 'is-success';
208
209 if ( '' !== $this->data['error_details'] ) {
210 $this->data['error_details'] = '</p><p>' . sprintf( __( 'Error code: %s', 'tablepress' ), '<code>' . esc_html( $this->data['error_details'] ) . '</code>' );
211 }
212
213 $this->add_header_message( "<strong>{$action_messages[ $this->data['message'] ]}</strong>{$this->data['error_details']}", $class );
214 }
215 }
216
217 /**
218 * Registers a text box for the view.
219 *
220 * @since 1.0.0
221 *
222 * @param string $id Unique HTML ID for the text box container (only visible with $wrap = true).
223 * @param callable $callback Callback that prints the contents of the text box.
224 * @param string $context Optional. Context/position of the text box (normal, side, additional, header, submit).
225 * @param bool $wrap Whether the content of the text box shall be wrapped in a <div> container.
226 */
227 protected function add_text_box( string $id, callable $callback, string $context = 'normal', bool $wrap = false ): void {
228 if ( ! isset( $this->textboxes[ $context ] ) ) {
229 $this->textboxes[ $context ] = array();
230 }
231
232 $long_id = "tablepress_{$this->action}-{$id}";
233 $this->textboxes[ $context ][ $id ] = array(
234 'id' => $long_id,
235 'callback' => $callback,
236 'context' => $context,
237 'wrap' => $wrap,
238 );
239 }
240
241 /**
242 * Registers a post meta box for the view, that is drag/droppable with WordPress functionality.
243 *
244 * @since 1.0.0
245 *
246 * @param string $id Unique ID for the meta box.
247 * @param string $title Title for the meta box.
248 * @param callable $callback Callback that prints the contents of the post meta box.
249 * @param 'normal'|'side'|'additional' $context Optional. Context/position of the post meta box (normal, side, additional).
250 * @param 'core'|'default'|'high'|'low' $priority Optional. Order of the post meta box for the $context position (high, default, low).
251 * @param mixed[]|null $callback_args Optional. Additional data for the callback function (e.g. useful when in different class).
252 */
253 protected function add_meta_box( string $id, string $title, callable $callback, string $context = 'normal', string $priority = 'default', ?array $callback_args = null ): void {
254 $this->has_meta_boxes = true;
255 add_meta_box( "tablepress_{$this->action}-{$id}", $title, $callback, null, $context, $priority, $callback_args );
256 }
257
258 /**
259 * Renders all text boxes for the given context.
260 *
261 * @since 1.0.0
262 *
263 * @param string $context Context (normal, side, additional, header, submit) for which registered text boxes shall be rendered.
264 */
265 protected function do_text_boxes( string $context ): void {
266 if ( empty( $this->textboxes[ $context ] ) ) {
267 return;
268 }
269
270 foreach ( $this->textboxes[ $context ] as $box ) {
271 if ( $box['wrap'] ) {
272 echo "<div id=\"{$box['id']}\" class=\"textbox\">\n";
273 }
274 call_user_func( $box['callback'], $this->data, $box );
275 if ( $box['wrap'] ) {
276 echo "</div>\n";
277 }
278 }
279 }
280
281 /**
282 * Renders all post meta boxes for the given context, if there are post meta boxes.
283 *
284 * @since 1.0.0
285 *
286 * @param string $context Context (normal, side, additional) for which registered post meta boxes shall be rendered.
287 */
288 protected function do_meta_boxes( string $context ): void {
289 if ( $this->has_meta_boxes ) {
290 do_meta_boxes( get_current_screen(), $context, $this->data ); // @phpstan-ignore argument.type
291 }
292 }
293
294 /**
295 * Prints hidden fields with nonces for post meta box AJAX handling, if there are post meta boxes on the screen.
296 *
297 * The check is possible as this function is executed after post meta boxes have to be registered.
298 *
299 * @since 1.0.0
300 *
301 * @param array<string, mixed> $data Data for this screen.
302 * @param array<string, mixed> $box Information about the text box.
303 */
304 protected function default_nonce_fields( array $data, array $box ): void {
305 if ( ! $this->has_meta_boxes ) {
306 return;
307 }
308 wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
309 echo "\n";
310 wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
311 echo "\n";
312 }
313
314 /**
315 * Prints hidden field with a nonce for the screen's action, to be transmitted in HTTP requests.
316 *
317 * @since 1.0.0
318 *
319 * @param array<string, mixed> $data Data for this screen.
320 * @param array<string, mixed> $box Information about the text box.
321 */
322 protected function action_nonce_field( array $data, array $box ): void {
323 wp_nonce_field( TablePress::nonce( $this->action ) );
324 echo "\n";
325 }
326
327 /**
328 * Prints hidden field with the screen action.
329 *
330 * @since 1.0.0
331 *
332 * @param array<string, mixed> $data Data for this screen.
333 * @param array<string, mixed> $box Information about the text box.
334 */
335 protected function action_field( array $data, array $box ): void {
336 echo "<input type=\"hidden\" name=\"action\" value=\"tablepress_{$this->action}\">\n";
337 }
338
339 /**
340 * Renders the current view.
341 *
342 * @since 1.0.0
343 */
344 public function render(): void {
345 ?>
346 <div id="tablepress-page" class="wrap">
347 <?php
348 $this->print_nav_tab_menu();
349 ?>
350 <div id="tablepress-body">
351 <hr class="wp-header-end">
352 <?php
353 // Print all header messages.
354 foreach ( $this->header_messages as $message ) {
355 echo $message;
356 }
357 // "Import" screen has file upload.
358 $enctype = ( 'import' === $this->action ) ? ' enctype="multipart/form-data"' : '';
359 ?>
360 <form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post"<?php echo $enctype; ?> id="tablepress-page-form">
361 <?php
362 $this->do_text_boxes( 'header' );
363 $hide_if_no_js = ( in_array( $this->action, array( 'export', 'import' ), true ) ) ? ' class="hide-if-no-js"' : '';
364 ?>
365 <div id="poststuff"<?php echo $hide_if_no_js; ?>>
366 <div id="post-body" class="metabox-holder columns-<?php echo ( isset( $GLOBALS['screen_layout_columns'] ) && ( 2 === $GLOBALS['screen_layout_columns'] ) ) ? '2' : '1'; ?>">
367 <div id="postbox-container-2" class="postbox-container">
368 <?php
369 $this->do_text_boxes( 'normal' );
370 $this->do_meta_boxes( 'normal' );
371
372 $this->do_text_boxes( 'additional' );
373 $this->do_meta_boxes( 'additional' );
374
375 // Print all submit buttons.
376 $this->do_text_boxes( 'submit' );
377 ?>
378 </div>
379 <div id="postbox-container-1" class="postbox-container">
380 <?php
381 // Print all boxes in the sidebar.
382 $this->do_text_boxes( 'side' );
383 $this->do_meta_boxes( 'side' );
384 ?>
385 </div>
386 </div>
387 <br class="clear">
388 </div>
389 </form>
390 </div>
391 </div>
392 <?php
393 }
394
395 /**
396 * Renders the navigation menu with links to the possible actions, highlighting the current one.
397 *
398 * @since 1.0.0
399 */
400 protected function print_nav_tab_menu(): void {
401 $name = __( 'TablePress', 'tablepress' );
402 $filename = 'admin/img/tablepress.svg';
403 ?>
404 <div id="tablepress-header" class="header">
405 <h1 class="name">
406 <img src="<?php echo plugins_url( $filename, TABLEPRESS__FILE__ ); ?>" alt="<?php esc_attr_e( 'TablePress plugin logo', 'tablepress' ); ?>">
407 <span class="screen-reader-text"><?php echo $name; ?></span>
408 </h1>
409 <?php if ( ! TABLEPRESS_IS_PLAYGROUND_PREVIEW && tb_tp_fs()->is_free_plan() ) : ?>
410 <div class="buttons">
411 <a href="<?php echo esc_url( tb_tp_fs()->pricing_url( WP_FS__PERIOD_ANNUALLY, false ) ); ?>" class="tablepress-button">
412 <span><?php _e( 'Upgrade to Premium', 'tablepress' ); ?></span>
413 <span class="dashicons dashicons-arrow-right-alt"></span>
414 </a>
415 </div>
416 <?php endif; ?>
417 </div>
418 <nav id="tablepress-nav">
419 <ul class="nav-menu">
420 <?php
421 foreach ( $this->data['view_actions'] as $action => $entry ) {
422 if ( '' === $entry['nav_tab_title'] ) {
423 continue;
424 }
425 if ( ! current_user_can( $entry['required_cap'] ) ) {
426 continue;
427 }
428
429 $url = esc_url( TablePress::url( array( 'action' => $action ) ) );
430 $active = ( $action === $this->action ) ? ' active' : '';
431 $separator = ( 'export' === $action ) ? ' separator' : ''; // Make the "Export" entry a separator, for some spacing.
432 echo "<li class=\"nav-item\"><a id=\"tablepress-nav-item-{$action}\" class=\"nav-link{$active}{$separator}\" href=\"{$url}\">{$entry['nav_tab_title']}</a></li>";
433 }
434 ?>
435 </ul>
436 </nav>
437 <?php
438 }
439
440 /**
441 * Prints a notification about JavaScript not being activated in the browser.
442 *
443 * @since 2.0.0
444 *
445 * @param array<string, mixed> $data Data for this screen.
446 * @param array<string, mixed> $box Information about the text box.
447 */
448 public function textbox_no_javascript( array $data, array $box ): void {
449 ?>
450 <div class="notice components-notice is-error hide-if-js">
451 <div class="components-notice__content">
452 <h3><em>
453 <?php _e( 'Attention: Unfortunately, there is a problem!', 'tablepress' ); ?>
454 </em></h3>
455 <p style="font-size:14px">
456 <strong><?php _e( 'This screen requires JavaScript. Please enable JavaScript in your browser settings.', 'tablepress' ); ?></strong><br>
457 <?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' ); ?>
458 </p>
459 <p>
460 <?php echo '<a href="' . esc_url( TablePress::url( array( 'action' => 'list' ) ) ) . '">' . __( 'Back to the List of Tables', 'tablepress' ) . '</a>'; ?>
461 </p>
462 </div>
463 </div>
464 <?php
465 }
466
467 /**
468 * Prints a submit button (only done when function is used as a callback for a text box).
469 *
470 * This method is soft-deprecated. It's no longer used in TablePress, but e.g. in the "TablePress Debug Extension".
471 *
472 * @since 1.0.0
473 *
474 * @param array<string, mixed> $data Data for this screen.
475 * @param array<string, mixed> $box Information about the text box.
476 */
477 protected function textbox_submit_button( array $data, array $box ): void {
478 ?>
479 <p class="submit"><input type="submit" class="components-button is-primary button-save-changes" value="<?php esc_attr_e( 'Save Changes', 'tablepress' ); ?>"></p>
480 <?php
481 }
482
483 /**
484 * Returns a safe JSON representation of a variable for printing inside of JavaScript code.
485 *
486 * @since 2.0.0
487 *
488 * @param mixed $data Variable to convert to JSON.
489 * @return string Safe JSON representation of a variable for printing inside of JavaScript code.
490 */
491 public function convert_to_json_parse_output( /* string|array|bool|int|float|null */ $data ): string {
492 $json = wp_json_encode( $data, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES );
493 if ( false === $json ) {
494 // JSON encoding failed, return an error object. Use a prefixed "_error" key to avoid conflicts with intentionally added "error" keys.
495 $json = '{ "_error": "The data could not be encoded to JSON!" }';
496 }
497 // Print the JSON data inside a `JSON.parse()` call in JS for speed gains, with necessary escaping of `\` and `'`.
498 $json = str_replace( array( '\\', "'" ), array( '\\\\', "\'" ), $json );
499 return "JSON.parse( '{$json}' )";
500 }
501
502 /**
503 * Prints JavaScript variables for the screen.
504 *
505 * @since 3.1.0
506 *
507 * @param string $variable Name of the JavaScript variable.
508 * @param array<string, mixed> $data Information about the text box.
509 */
510 protected function print_script_data_json( string $variable, array $data ): void {
511 echo "<script>\n";
512 echo "window.tp = window.tp || {};\n";
513 echo "tp.{$variable} = {\n";
514 foreach ( $data as $key => $value ) {
515 $value = $this->convert_to_json_parse_output( $value );
516 echo "\t{$key}: {$value},\n";
517 }
518 echo "};\n";
519 echo "</script>\n";
520 }
521
522 /**
523 * Returns the content for the help tab for this screen.
524 *
525 * Has to be implemented for every view that is visible in the WP Dashboard!
526 *
527 * @since 1.0.0
528 *
529 * @return string Help tab content for the view.
530 */
531 protected function help_tab_content(): string {
532 // Has to be implemented for every view that is visible in the WP Dashboard!
533 return '';
534 }
535
536 /**
537 * Adds a TablePress "Thank You" message to the admin footer content.
538 *
539 * @since 1.0.0
540 * @since 3.3.0 This method was moved from the now-removed `TablePress_Admin_Page` class.
541 *
542 * @param string $content Current admin footer content.
543 * @return string New admin footer content.
544 */
545 public function add_admin_footer_text( /* string */ $content ): string {
546 // Don't use a type hint in the method declaration as many WordPress plugins use the `admin_footer_text` filter without returning a string.
547
548 // Protect against other plugins not returning a string in their filter callbacks.
549 if ( ! is_string( $content ) ) { // @phpstan-ignore function.alreadyNarrowedType (The `is_string()` check is needed as the input is coming from a filter hook.)
550 $content = '';
551 }
552
553 /* translators: %s: URL to TablePress website */
554 $content .= ' &bull; ' . sprintf( __( 'Thank you for using <a href="%s">TablePress</a>.', 'tablepress' ), 'https://tablepress.org/' );
555 if ( tb_tp_fs()->is_free_plan() ) {
556 /* translators: %s: URL to TablePress premium features */
557 $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' );
558 }
559 return $content;
560 }
561
562 /**
563 * Initializes the WP feature pointers for TablePress.
564 *
565 * @since 1.0.0
566 */
567 protected function _init_wp_pointers(): void {
568 // Check if there are WP pointers for this view.
569 if ( empty( $this->wp_pointers ) ) {
570 return;
571 }
572
573 // Get dismissed pointers.
574 $dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
575
576 $pointers_on_page = false;
577 foreach ( array_diff( $this->wp_pointers, $dismissed ) as $pointer ) {
578 // Bind pointer print function.
579 add_action( "admin_footer-{$GLOBALS['hook_suffix']}", array( $this, 'wp_pointer_' . $pointer ) ); // @phpstan-ignore argument.type
580 $pointers_on_page = true;
581 }
582
583 if ( $pointers_on_page ) {
584 wp_enqueue_style( 'wp-pointer' );
585 wp_enqueue_script( 'wp-pointer' );
586 }
587 }
588
589 /**
590 * Prints the JavaScript code for a WP feature pointer.
591 *
592 * @since 1.0.0
593 * @since 3.3.0 This method was moved from the now-removed `TablePress_Admin_Page` class.
594 *
595 * @param string $pointer_id The pointer ID.
596 * @param string $selector The HTML elements, on which the pointer should be attached.
597 * @param array<string, mixed> $args Arguments to be passed to the pointer JS (see wp-pointer.js).
598 */
599 public function print_wp_pointer_js( string $pointer_id, string $selector, array $args ): void {
600 if ( empty( $pointer_id ) || empty( $selector ) || empty( $args['content'] ) ) {
601 return;
602 }
603
604 $keyboard_shortcut = '';
605 if ( 'tp33_edit_quick_navigation' === $pointer_id ) {
606 $keyboard_shortcut = <<<JS
607 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
608 JS;
609 }
610
611 /*
612 * Print JS code for the feature pointers, extended with event handling for opened/closed "Screen Options", so that pointers can
613 * be repositioned. 210 ms is slightly slower than jQuery's "fast" value, to allow all elements to reach their original position.
614 */
615 ?>
616 <script>
617 ( ( $ ) => {
618 let options = <?php echo wp_json_encode( $args, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?>;
619 if ( ! options ) {
620 return;
621 }
622
623 options = {
624 ...options,
625 <?php echo $keyboard_shortcut; ?>
626 close() {
627 $.post( ajaxurl, {
628 pointer: '<?php echo $pointer_id; ?>',
629 action: 'dismiss-wp-pointer'
630 } );
631 $( this ).pointer( { 'disabled': true } );
632 },
633 };
634
635 $( () => $( '<?php echo $selector; ?>' ).pointer( options ).pointer( 'open' ) );
636
637 $( document ).on( 'screen:options:open screen:options:close', () => {
638 setTimeout( () => $( '<?php echo $selector; ?>' ).pointer( 'reposition' ), 210 );
639 } );
640 } )( jQuery );
641 </script>
642 <?php
643 }
644
645 } // class TablePress_View
646