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

539 lines 19.7 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 * Instance of the Admin Page Helper Class, with necessary functions.
48 *
49 * @since 1.0.0
50 */
51 protected \TablePress_Admin_Page $admin_page;
52
53 /**
54 * List of text boxes (similar to post boxes, but just with text and without extra functionality).
55 *
56 * @since 1.0.0
57 * @var array<string, array<string, array<string, mixed>>>
58 */
59 protected array $textboxes = array();
60
61 /**
62 * List of messages that are to be displayed as boxes below the page title.
63 *
64 * @since 1.0.0
65 * @var string[]
66 */
67 protected array $header_messages = array();
68
69 /**
70 * Whether there are post boxes registered for this screen,
71 * is automatically set to true, when a meta box is added.
72 *
73 * @since 1.0.0
74 */
75 protected bool $has_meta_boxes = false;
76
77 /**
78 * List of WP feature pointers for this view.
79 *
80 * @since 1.0.0
81 * @var string[]
82 */
83 protected array $wp_pointers = array();
84
85 /**
86 * Initialize the View class, by setting the correct screen columns and adding help texts.
87 *
88 * @since 1.0.0
89 */
90 public function __construct() {
91 $screen = get_current_screen();
92 if ( 0 !== $this->screen_columns ) {
93 $screen->add_option( 'layout_columns', array( 'max' => $this->screen_columns ) ); // @phpstan-ignore method.nonObject
94 }
95 // Enable two column layout.
96 add_filter( "get_user_option_screen_layout_{$screen->id}", array( $this, 'set_current_screen_layout_columns' ) ); // @phpstan-ignore property.nonObject
97
98 $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>';
99 $common_content .= '<p>' . sprintf( __( 'For technical information, please see the <a href="%s">Documentation</a>.', 'tablepress' ), 'https://tablepress.org/documentation/' ) . ' ' . sprintf( __( 'Common questions are answered in the <a href="%s">FAQ</a>.', 'tablepress' ), 'https://tablepress.org/faq/' ) . '</p>';
100
101 if ( tb_tp_fs()->is_free_plan() ) {
102 $common_content .= '<p>'
103 . 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/tags/tablepress' )
104 . ' '
105 . 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/' )
106 . '</p>';
107 $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>';
108 }
109
110 $screen->add_help_tab( array( // @phpstan-ignore method.nonObject
111 'id' => 'tablepress-help', // This should be unique for the screen.
112 'title' => __( 'TablePress Help', 'tablepress' ),
113 'content' => '<p>' . $this->help_tab_content() . '</p>' . $common_content,
114 ) );
115 // "Sidebar" in the help tab.
116 $screen->set_help_sidebar( // @phpstan-ignore method.nonObject
117 '<p><strong>' . __( 'For more information:', 'tablepress' ) . '</strong></p>'
118 . '<p><a href="https://tablepress.org/">TablePress Website</a></p>'
119 . '<p><a href="https://tablepress.org/faq/">TablePress FAQ</a></p>'
120 . '<p><a href="https://tablepress.org/documentation/">TablePress Documentation</a></p>'
121 . '<p><a href="https://tablepress.org/support/">TablePress Support</a></p>'
122 );
123 }
124
125 /**
126 * Change the value of the user option "screen_layout_{$screen->id}" through a filter.
127 *
128 * @since 1.0.0
129 *
130 * @param int|false $result Current value of the user option.
131 * @return int New value for the user option.
132 */
133 public function set_current_screen_layout_columns( /* int|false */ $result ): int {
134 if ( false === $result ) {
135 // The user option does not yet exist.
136 $result = $this->screen_columns;
137 } elseif ( $result > $this->screen_columns ) {
138 // 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).
139 $result = $this->screen_columns;
140 }
141 return $result;
142 }
143
144 /**
145 * Set up the view with data and do things that are necessary for all views.
146 *
147 * @since 1.0.0
148 *
149 * @param string $action Action for this view.
150 * @param array<string, mixed> $data Data for this view.
151 */
152 public function setup( /* string */ $action, array $data ) /* : void */ {
153 // 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.
154
155 $this->action = $action;
156 $this->data = $data;
157
158 // Set page title.
159 $GLOBALS['title'] = sprintf( __( '%1$s &lsaquo; %2$s', 'tablepress' ), $this->data['view_actions'][ $this->action ]['page_title'], 'TablePress' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
160
161 // Admin page helpers, like script/style loading, could be moved to view.
162 $this->admin_page = TablePress::load_class( 'TablePress_Admin_Page', 'class-admin-page-helper.php', 'classes' );
163 $this->admin_page->enqueue_style( 'common', array( 'wp-components' ) );
164 // RTL styles for the admin interface.
165 if ( is_rtl() ) {
166 $this->admin_page->enqueue_style( 'common-rtl', array( 'tablepress-common' ) );
167 }
168 $this->admin_page->enqueue_script( 'common', array( 'jquery-core', 'postbox' ) );
169
170 $this->admin_page->add_admin_footer_text();
171
172 // Initialize WP feature pointers for TablePress.
173 $this->_init_wp_pointers();
174
175 // Necessary fields for all views.
176 $this->add_text_box( 'default_nonce_fields', array( $this, 'default_nonce_fields' ), 'header', false );
177 $this->add_text_box( 'action_nonce_field', array( $this, 'action_nonce_field' ), 'header', false );
178 $this->add_text_box( 'action_field', array( $this, 'action_field' ), 'header', false );
179 }
180
181 /**
182 * Register a header message for the view.
183 *
184 * @since 1.0.0
185 *
186 * @param string $text Text for the header message.
187 * @param string $css_class Optional. Additional CSS class for the header message.
188 * @param string $title Optional. Text for the header title.
189 */
190 protected function add_header_message( string $text, string $css_class = 'notice-success', string $title = '' ): void {
191 if ( ! str_contains( $css_class, 'not-dismissible' ) ) {
192 $css_class .= ' is-dismissible';
193 }
194 if ( '' !== $title ) {
195 $title = "<h3>{$title}</h3>";
196 }
197 // Wrap the message text in HTML <p> tags if it does not already start with one (potentially with attributes), indicating custom message HTML.
198 if ( '' !== $text && ! str_starts_with( $text, '<p' ) ) {
199 $text = "<p>{$text}</p>";
200 }
201 $this->header_messages[] = "<div class=\"notice {$css_class}\">{$title}{$text}</div>\n";
202 }
203
204 /**
205 * Process header action messages, i.e. check if a message should be added to the page.
206 *
207 * @since 1.0.0
208 *
209 * @param array<string, string> $action_messages Action messages for the screen.
210 */
211 protected function process_action_messages( array $action_messages ): void {
212 if ( $this->data['message'] && isset( $action_messages[ $this->data['message'] ] ) ) {
213 $class = ( str_starts_with( $this->data['message'], 'error' ) ) ? 'notice-error' : 'notice-success';
214
215 if ( '' !== $this->data['error_details'] ) {
216 $this->data['error_details'] = '</p><p>' . sprintf( __( 'Error code: %s', 'tablepress' ), '<code>' . esc_html( $this->data['error_details'] ) . '</code>' );
217 }
218
219 $this->add_header_message( "<strong>{$action_messages[ $this->data['message'] ]}</strong>{$this->data['error_details']}", $class );
220 }
221 }
222
223 /**
224 * Register a text box for the view.
225 *
226 * @since 1.0.0
227 *
228 * @param string $id Unique HTML ID for the text box container (only visible with $wrap = true).
229 * @param callable $callback Callback that prints the contents of the text box.
230 * @param string $context Optional. Context/position of the text box (normal, side, additional, header, submit).
231 * @param bool $wrap Whether the content of the text box shall be wrapped in a <div> container.
232 */
233 protected function add_text_box( string $id, callable $callback, string $context = 'normal', bool $wrap = false ): void {
234 if ( ! isset( $this->textboxes[ $context ] ) ) {
235 $this->textboxes[ $context ] = array();
236 }
237
238 $long_id = "tablepress_{$this->action}-{$id}";
239 $this->textboxes[ $context ][ $id ] = array(
240 'id' => $long_id,
241 'callback' => $callback,
242 'context' => $context,
243 'wrap' => $wrap,
244 );
245 }
246
247 /**
248 * Register a post meta box for the view, that is drag/droppable with WordPress functionality.
249 *
250 * @since 1.0.0
251 *
252 * @param string $id Unique ID for the meta box.
253 * @param string $title Title for the meta box.
254 * @param callable $callback Callback that prints the contents of the post meta box.
255 * @param 'normal'|'side'|'additional' $context Optional. Context/position of the post meta box (normal, side, additional).
256 * @param 'core'|'default'|'high'|'low' $priority Optional. Order of the post meta box for the $context position (high, default, low).
257 * @param mixed[]|null $callback_args Optional. Additional data for the callback function (e.g. useful when in different class).
258 */
259 protected function add_meta_box( string $id, string $title, callable $callback, string $context = 'normal', string $priority = 'default', ?array $callback_args = null ): void {
260 $this->has_meta_boxes = true;
261 add_meta_box( "tablepress_{$this->action}-{$id}", $title, $callback, null, $context, $priority, $callback_args );
262 }
263
264 /**
265 * Render all text boxes for the given context.
266 *
267 * @since 1.0.0
268 *
269 * @param string $context Context (normal, side, additional, header, submit) for which registered text boxes shall be rendered.
270 */
271 protected function do_text_boxes( string $context ): void {
272 if ( empty( $this->textboxes[ $context ] ) ) {
273 return;
274 }
275
276 foreach ( $this->textboxes[ $context ] as $box ) {
277 if ( $box['wrap'] ) {
278 echo "<div id=\"{$box['id']}\" class=\"textbox\">\n";
279 }
280 call_user_func( $box['callback'], $this->data, $box );
281 if ( $box['wrap'] ) {
282 echo "</div>\n";
283 }
284 }
285 }
286
287 /**
288 * Render all post meta boxes for the given context, if there are post meta boxes.
289 *
290 * @since 1.0.0
291 *
292 * @param string $context Context (normal, side, additional) for which registered post meta boxes shall be rendered.
293 */
294 protected function do_meta_boxes( string $context ): void {
295 if ( $this->has_meta_boxes ) {
296 do_meta_boxes( get_current_screen(), $context, $this->data ); // @phpstan-ignore argument.type
297 }
298 }
299
300 /**
301 * Print hidden fields with nonces for post meta box AJAX handling, if there are post meta boxes on the screen.
302 *
303 * The check is possible as this function is executed after post meta boxes have to be registered.
304 *
305 * @since 1.0.0
306 *
307 * @param array<string, mixed> $data Data for this screen.
308 * @param array<string, mixed> $box Information about the text box.
309 */
310 protected function default_nonce_fields( array $data, array $box ): void {
311 if ( ! $this->has_meta_boxes ) {
312 return;
313 }
314 wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
315 echo "\n";
316 wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
317 echo "\n";
318 }
319
320 /**
321 * Print hidden field with a nonce for the screen's action, to be transmitted in HTTP requests.
322 *
323 * @since 1.0.0
324 *
325 * @param array<string, mixed> $data Data for this screen.
326 * @param array<string, mixed> $box Information about the text box.
327 */
328 protected function action_nonce_field( array $data, array $box ): void {
329 wp_nonce_field( TablePress::nonce( $this->action ) );
330 echo "\n";
331 }
332
333 /**
334 * Print hidden field with the screen action.
335 *
336 * @since 1.0.0
337 *
338 * @param array<string, mixed> $data Data for this screen.
339 * @param array<string, mixed> $box Information about the text box.
340 */
341 protected function action_field( array $data, array $box ): void {
342 echo "<input type=\"hidden\" name=\"action\" value=\"tablepress_{$this->action}\">\n";
343 }
344
345 /**
346 * Render the current view.
347 *
348 * @since 1.0.0
349 */
350 public function render(): void {
351 ?>
352 <div id="tablepress-page" class="wrap">
353 <?php
354 $this->print_nav_tab_menu();
355 ?>
356 <div id="tablepress-body">
357 <hr class="wp-header-end">
358 <?php
359 // Print all header messages.
360 foreach ( $this->header_messages as $message ) {
361 echo $message;
362 }
363 // "Import" screen has file upload.
364 $enctype = ( 'import' === $this->action ) ? ' enctype="multipart/form-data"' : '';
365 ?>
366 <form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post"<?php echo $enctype; ?> id="tablepress-page-form">
367 <?php
368 $this->do_text_boxes( 'header' );
369 $hide_if_no_js = ( in_array( $this->action, array( 'export', 'import' ), true ) ) ? ' class="hide-if-no-js"' : '';
370 ?>
371 <div id="poststuff"<?php echo $hide_if_no_js; ?>>
372 <div id="post-body" class="metabox-holder columns-<?php echo ( isset( $GLOBALS['screen_layout_columns'] ) && ( 2 === $GLOBALS['screen_layout_columns'] ) ) ? '2' : '1'; ?>">
373 <div id="postbox-container-2" class="postbox-container">
374 <?php
375 $this->do_text_boxes( 'normal' );
376 $this->do_meta_boxes( 'normal' );
377
378 $this->do_text_boxes( 'additional' );
379 $this->do_meta_boxes( 'additional' );
380
381 // Print all submit buttons.
382 $this->do_text_boxes( 'submit' );
383 ?>
384 </div>
385 <div id="postbox-container-1" class="postbox-container">
386 <?php
387 // Print all boxes in the sidebar.
388 $this->do_text_boxes( 'side' );
389 $this->do_meta_boxes( 'side' );
390 ?>
391 </div>
392 </div>
393 <br class="clear">
394 </div>
395 </form>
396 </div>
397 </div>
398 <?php
399 }
400
401 /**
402 * Render the navigation menu with links to the possible actions, highlighting the current one.
403 *
404 * @since 1.0.0
405 */
406 protected function print_nav_tab_menu(): void {
407 $name = __( 'TablePress', 'tablepress' );
408 $filename = 'admin/img/tablepress.svg';
409 ?>
410 <div id="tablepress-header" class="header">
411 <h1 class="name">
412 <img src="<?php echo plugins_url( $filename, TABLEPRESS__FILE__ ); ?>" alt="<?php esc_attr_e( 'TablePress plugin logo', 'tablepress' ); ?>">
413 <span class="screen-reader-text"><?php echo $name; ?></span>
414 </h1>
415 <?php if ( ! TABLEPRESS_IS_PLAYGROUND_PREVIEW && tb_tp_fs()->is_free_plan() ) : ?>
416 <div class="buttons">
417 <?php
418 $timestamp_today = strtotime( 'today' );
419 if ( strtotime( '2024-11-24' ) < $timestamp_today && $timestamp_today < strtotime( '2024-12-03' ) ) :
420 ?>
421 <a href="https://tablepress.org/premium/?utm_campaign=black-week-2024&utm_source=plugin&utm_medium=button&utm_content=upgrade-button" class="tablepress-button" style="background:linear-gradient(135deg,rgba(252,185,0,1) 0%,rgba(255,105,0,1) 100%);border-color:#ffffff;font-weight:bold;font-size:20px;border-radius:6px;">
422 <span>Premium: 20% off during Black Week 2024!</span>
423 <span class="dashicons dashicons-arrow-right-alt" />
424 </a>
425 <?php else : ?>
426 <a href="<?php echo esc_url( tb_tp_fs()->pricing_url( WP_FS__PERIOD_ANNUALLY, false ) ); ?>" class="tablepress-button">
427 <span><?php _e( 'Upgrade to Premium', 'tablepress' ); ?></span>
428 <span class="dashicons dashicons-arrow-right-alt"></span>
429 </a>
430 <?php endif; ?>
431 </div>
432 <?php endif; ?>
433 </div>
434 <nav id="tablepress-nav">
435 <ul class="nav-menu">
436 <?php
437 foreach ( $this->data['view_actions'] as $action => $entry ) {
438 if ( '' === $entry['nav_tab_title'] ) {
439 continue;
440 }
441 if ( ! current_user_can( $entry['required_cap'] ) ) {
442 continue;
443 }
444
445 $url = esc_url( TablePress::url( array( 'action' => $action ) ) );
446 $active = ( $action === $this->action ) ? ' active' : '';
447 $separator = ( 'export' === $action ) ? ' separator' : ''; // Make the "Export" entry a separator, for some spacing.
448 echo "<li class=\"nav-item\"><a id=\"tablepress-nav-item-{$action}\" class=\"nav-link{$active}{$separator}\" href=\"{$url}\">{$entry['nav_tab_title']}</a></li>";
449 }
450 ?>
451 </ul>
452 </nav>
453 <?php
454 }
455
456 /**
457 * Prints a notification about JavaScript not being activated in the browser.
458 *
459 * @since 2.0.0
460 *
461 * @param array<string, mixed> $data Data for this screen.
462 * @param array<string, mixed> $box Information about the text box.
463 */
464 public function textbox_no_javascript( array $data, array $box ): void {
465 ?>
466 <div class="notice notice-error notice-alt notice-large hide-if-js">
467 <h3><em>
468 <?php _e( 'Attention: Unfortunately, there is a problem!', 'tablepress' ); ?>
469 </em></h3>
470 <p style="font-size:14px">
471 <strong><?php _e( 'This screen requires JavaScript. Please enable JavaScript in your browser settings.', 'tablepress' ); ?></strong><br>
472 <?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' ); ?>
473 </p>
474 <p>
475 <?php echo '<a href="' . esc_url( TablePress::url( array( 'action' => 'list' ) ) ) . '">' . __( 'Back to the List of Tables', 'tablepress' ) . '</a>'; ?>
476 </p>
477 </div>
478 <?php
479 }
480
481 /**
482 * Prints a submit button (only done when function is used as a callback for a text box).
483 *
484 * This method is soft-deprecated. It's no longer used in TablePress, but e.g. in the "TablePress Debug Extension".
485 *
486 * @since 1.0.0
487 *
488 * @param array<string, mixed> $data Data for this screen.
489 * @param array<string, mixed> $box Information about the text box.
490 */
491 protected function textbox_submit_button( array $data, array $box ): void {
492 ?>
493 <p class="submit"><input type="submit" class="components-button is-primary button-save-changes" value="<?php esc_attr_e( 'Save Changes', 'tablepress' ); ?>"></p>
494 <?php
495 }
496
497 /**
498 * Return the content for the help tab for this screen.
499 *
500 * Has to be implemented for every view that is visible in the WP Dashboard!
501 *
502 * @since 1.0.0
503 *
504 * @return string Help tab content for the view.
505 */
506 protected function help_tab_content(): string {
507 // Has to be implemented for every view that is visible in the WP Dashboard!
508 return '';
509 }
510
511 /**
512 * Initialize the WP feature pointers for TablePress.
513 *
514 * @since 1.0.0
515 */
516 protected function _init_wp_pointers(): void {
517 // Check if there are WP pointers for this view.
518 if ( empty( $this->wp_pointers ) ) {
519 return;
520 }
521
522 // Get dismissed pointers.
523 $dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
524
525 $pointers_on_page = false;
526 foreach ( array_diff( $this->wp_pointers, $dismissed ) as $pointer ) {
527 // Bind pointer print function.
528 add_action( "admin_footer-{$GLOBALS['hook_suffix']}", array( $this, 'wp_pointer_' . $pointer ) ); // @phpstan-ignore argument.type
529 $pointers_on_page = true;
530 }
531
532 if ( $pointers_on_page ) {
533 wp_enqueue_style( 'wp-pointer' );
534 wp_enqueue_script( 'wp-pointer' );
535 }
536 }
537
538 } // class TablePress_View
539