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

462 lines 15.2 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 * @package TablePress
17 * @subpackage Views
18 * @author Tobias Bäthge
19 * @since 1.0.0
20 */
21 abstract class TablePress_View {
22
23 /**
24 * Data for the view.
25 *
26 * @since 1.0.0
27 * @var array
28 */
29 protected $data = array();
30
31 /**
32 * Number of screen columns for post boxes.
33 *
34 * @since 1.0.0
35 * @var int
36 */
37 protected $screen_columns = 0;
38
39 /**
40 * User action for this screen.
41 *
42 * @since 1.0.0
43 * @var string
44 */
45 protected $action = '';
46
47 /**
48 * Instance of the Admin Page Helper Class, with necessary functions.
49 *
50 * @since 1.0.0
51 * @var TablePress_Admin_Page
52 */
53 protected $admin_page;
54
55 /**
56 * List of text boxes (similar to post boxes, but just with text and without extra functionality).
57 *
58 * @since 1.0.0
59 * @var array
60 */
61 protected $textboxes = array();
62
63 /**
64 * List of messages that are to be displayed as boxes below the page title.
65 *
66 * @since 1.0.0
67 * @var array
68 */
69 protected $header_messages = array();
70
71 /**
72 * Whether there are post boxes registered for this screen,
73 * is automatically set to true, when a meta box is added.
74 *
75 * @since 1.0.0
76 * @var bool
77 */
78 protected $has_meta_boxes = false;
79
80 /**
81 * List of WP feature pointers for this view.
82 *
83 * @since 1.0.0
84 * @var array
85 */
86 protected $wp_pointers = array();
87
88 /**
89 * Initialize the View class, by setting the correct screen columns and adding help texts.
90 *
91 * @since 1.0.0
92 */
93 public function __construct() {
94 $screen = get_current_screen();
95 if ( 0 !== $this->screen_columns ) {
96 $screen->add_option( 'layout_columns', array( 'max' => $this->screen_columns ) );
97 }
98 // Enable two column layout.
99 add_filter( "get_user_option_screen_layout_{$screen->id}", array( $this, 'set_current_screen_layout_columns' ) );
100
101 $screen->add_help_tab( array(
102 'id' => 'tablepress-help', // This should be unique for the screen.
103 'title' => __( 'TablePress Help', 'tablepress' ),
104 'content' => '<p>' . $this->help_tab_content() . '</p>'
105 . '<p>' . sprintf( __( 'More information about TablePress can be found on the <a href="%1$s">plugin&#8217;s 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/' ) . ' '
106 . sprintf( __( 'For technical information, please see the <a href="%s">documentation</a>.', 'tablepress' ), 'https://tablepress.org/documentation/' ) . ' '
107 . 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' ) . ' '
108 . 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/' ) . '<br />'
109 . sprintf( __( 'If you like the plugin, <a href="%1$s"><strong>a donation</strong></a> is recommended.', 'tablepress' ), 'https://tablepress.org/donate/' ) . '</p>',
110 ) );
111 // "sidebar" in the help tab.
112 $screen->set_help_sidebar( '<p><strong>' . __( 'For more information:', 'tablepress' ) . '</strong></p><p><a href="https://tablepress.org/" target="_blank">TablePress Website</a></p><p><a href="https://tablepress.org/faq/" target="_blank">TablePress FAQ</a></p><p><a href="https://tablepress.org/documentation/" target="_blank">TablePress Documentation</a></p><p><a href="https://tablepress.org/support/" target="_blank">TablePress Support</a></p>' );
113 }
114
115 /**
116 * Change the value of the user option "screen_layout_{$screen->id}" through a filter.
117 *
118 * @since 1.0.0
119 *
120 * @param int|bool Current value of the user option.
121 * @return int New value for the user option.
122 */
123 public function set_current_screen_layout_columns( $result ) {
124 if ( false === $result ) {
125 // The user option does not yet exist.
126 $result = $this->screen_columns;
127 } elseif ( $result > $this->screen_columns ) {
128 // 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).
129 $result = $this->screen_columns;
130 }
131 return $result;
132 }
133
134 /**
135 * Set up the view with data and do things that are necessary for all views.
136 *
137 * @since 1.0.0
138 *
139 * @param string $action Action for this view.
140 * @param array $data Data for this view.
141 */
142 public function setup( $action, array $data ) {
143 $this->action = $action;
144 $this->data = $data;
145
146 // Set page title.
147 $GLOBALS['title'] = sprintf( __( '%1$s &lsaquo; %2$s', 'tablepress' ), $this->data['view_actions'][ $this->action ]['page_title'], 'TablePress' );
148
149 // Admin page helpers, like script/style loading, could be moved to view.
150 $this->admin_page = TablePress::load_class( 'TablePress_Admin_Page', 'class-admin-page-helper.php', 'classes' );
151 $this->admin_page->enqueue_style( 'common' );
152 // RTL styles for the admin interface.
153 if ( is_rtl() ) {
154 $this->admin_page->enqueue_style( 'common-rtl', array( 'tablepress-common' ) );
155 }
156 $this->admin_page->enqueue_script( 'common', array( 'jquery-core', 'postbox' ), array(
157 'common' => array(
158 'ays_delete_single_table' => _n( 'Do you really want to delete this table?', 'Do you really want to delete these tables?', 1, 'tablepress' ),
159 'ays_delete_multiple_tables' => _n( 'Do you really want to delete this table?', 'Do you really want to delete these tables?', 2, 'tablepress' ),
160 )
161 ) );
162
163 $this->admin_page->add_admin_footer_text();
164
165 // Initialize WP feature pointers for TablePress.
166 $this->_init_wp_pointers();
167
168 // Necessary fields for all views.
169 $this->add_text_box( 'default_nonce_fields', array( $this, 'default_nonce_fields' ), 'header', false );
170 $this->add_text_box( 'action_nonce_field', array( $this, 'action_nonce_field' ), 'header', false );
171 $this->add_text_box( 'action_field', array( $this, 'action_field' ), 'header', false );
172 }
173
174 /**
175 * Register a header message for the view.
176 *
177 * @since 1.0.0
178 *
179 * @param string $text Text for the header message.
180 * @param string $class Optional. Additional CSS class for the header message.
181 */
182 protected function add_header_message( $text, $class = 'notice-success' ) {
183 if ( ! stripos( $class, 'not-dismissible' ) ) {
184 $class .= ' is-dismissible';
185 }
186 $this->header_messages[] = "<div class=\"notice {$class}\"><p>{$text}</p></div>\n";
187 }
188
189 /**
190 * Process header action messages, i.e. check if a message should be added to the page.
191 *
192 * @since 1.0.0
193 *
194 * @param array $action_messages Action messages for the screen.
195 */
196 protected function process_action_messages( array $action_messages ) {
197 if ( $this->data['message'] && isset( $action_messages[ $this->data['message'] ] ) ) {
198 $class = ( 'error' === substr( $this->data['message'], 0, 5 ) ) ? 'notice-error' : 'notice-success';
199 $this->add_header_message( "<strong>{$action_messages[ $this->data['message'] ]}</strong>", $class );
200 }
201 }
202
203 /**
204 * Register a text box for the view.
205 *
206 * @since 1.0.0
207 *
208 * @param string $id Unique HTML ID for the text box container (only visible with $wrap = true).
209 * @param callback $callback Callback that prints the contents of the text box.
210 * @param string $context Optional. Context/position of the text box (normal, side, additional, header, submit).
211 * @param bool $ wrap Whether the content of the text box shall be wrapped in a <div> container.
212 */
213 protected function add_text_box( $id, $callback, $context = 'normal', $wrap = false ) {
214 if ( ! isset( $this->textboxes[ $context ] ) ) {
215 $this->textboxes[ $context ] = array();
216 }
217
218 $long_id = "tablepress_{$this->action}-{$id}";
219 $this->textboxes[ $context ][ $id ] = array(
220 'id' => $long_id,
221 'callback' => $callback,
222 'context' => $context,
223 'wrap' => $wrap,
224 );
225 }
226
227 /**
228 * Register a post meta box for the view, that is drag/droppable with WordPress functionality.
229 *
230 * @since 1.0.0
231 *
232 * @param string $id Unique ID for the meta box.
233 * @param string $title Title for the meta box.
234 * @param callback $callback Callback that prints the contents of the post meta box.
235 * @param string $context Optional. Context/position of the post meta box (normal, side, additional).
236 * @param string $priority Optional. Order of the post meta box for the $context position (high, default, low).
237 * @param bool $callback_args Optional. Additional data for the callback function (e.g. useful when in different class).
238 */
239 protected function add_meta_box( $id, $title, $callback, $context = 'normal', $priority = 'default', $callback_args = null ) {
240 $this->has_meta_boxes = true;
241 add_meta_box( "tablepress_{$this->action}-{$id}", $title, $callback, null, $context, $priority, $callback_args );
242 }
243
244 /**
245 * Render all text boxes for the given context.
246 *
247 * @since 1.0.0
248 *
249 * @param string $context Context (normal, side, additional, header, submit) for which registered text boxes shall be rendered.
250 */
251 protected function do_text_boxes( $context ) {
252 if ( empty( $this->textboxes[ $context ] ) ) {
253 return;
254 }
255
256 foreach ( $this->textboxes[ $context ] as $box ) {
257 if ( $box['wrap'] ) {
258 echo "<div id=\"{$box['id']}\" class=\"textbox\">\n";
259 }
260 call_user_func( $box['callback'], $this->data, $box );
261 if ( $box['wrap'] ) {
262 echo "</div>\n";
263 }
264 }
265 }
266
267 /**
268 * Render all post meta boxes for the given context, if there are post meta boxes.
269 *
270 * @since 1.0.0
271 *
272 * @param string $context Context (normal, side, additional) for which registered post meta boxes shall be rendered.
273 */
274 protected function do_meta_boxes( $context ) {
275 if ( ! $this->has_meta_boxes ) {
276 return;
277 }
278 do_meta_boxes( null, $context, $this->data );
279 }
280
281 /**
282 * Print hidden fields with nonces for post meta box AJAX handling, if there are post meta boxes on the screen.
283 *
284 * The check is possible as this function is executed after post meta boxes have to be registered.
285 *
286 * @since 1.0.0
287 *
288 * @param array $data Data for this screen.
289 * @param array $box Information about the text box.
290 */
291 protected function default_nonce_fields( array $data, array $box ) {
292 if ( ! $this->has_meta_boxes ) {
293 return;
294 }
295 wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
296 echo "\n";
297 wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
298 echo "\n";
299 }
300
301 /**
302 * Print hidden field with a nonce for the screen's action, to be transmitted in HTTP requests.
303 *
304 * @since 1.0.0
305 *
306 * @param array $data Data for this screen.
307 * @param array $box Information about the text box.
308 */
309 protected function action_nonce_field( array $data, array $box ) {
310 wp_nonce_field( TablePress::nonce( $this->action ) );
311 echo "\n";
312 }
313
314 /**
315 * Print hidden field with the screen action.
316 *
317 * @since 1.0.0
318 *
319 * @param array $data Data for this screen.
320 * @param array $box Information about the text box.
321 */
322 protected function action_field( array $data, array $box ) {
323 echo "<input type=\"hidden\" name=\"action\" value=\"tablepress_{$this->action}\" />\n";
324 }
325
326 /**
327 * Render the current view.
328 *
329 * @since 1.0.0
330 */
331 public function render() {
332 ?>
333 <div id="tablepress-page" class="wrap">
334 <?php
335 $this->print_nav_tab_menu();
336 // Print all header messages.
337 foreach ( $this->header_messages as $message ) {
338 echo $message;
339 }
340 // "Import" screen has file upload.
341 $enctype = ( 'import' === $this->action ) ? ' enctype="multipart/form-data"' : '';
342 ?>
343 <form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post"<?php echo $enctype; ?>>
344 <?php
345 $this->do_text_boxes( 'header' );
346 ?>
347 <div id="poststuff">
348 <div id="post-body" class="metabox-holder columns-<?php echo ( isset( $GLOBALS['screen_layout_columns'] ) && ( 2 === $GLOBALS['screen_layout_columns'] ) ) ? '2' : '1'; ?>">
349 <div id="postbox-container-2" class="postbox-container">
350 <?php
351 $this->do_text_boxes( 'normal' );
352 $this->do_meta_boxes( 'normal' );
353
354 $this->do_text_boxes( 'additional' );
355 $this->do_meta_boxes( 'additional' );
356
357 // Print all submit buttons.
358 $this->do_text_boxes( 'submit' );
359 ?>
360 </div>
361 <div id="postbox-container-1" class="postbox-container">
362 <?php
363 // Print all boxes in the sidebar.
364 $this->do_text_boxes( 'side' );
365 $this->do_meta_boxes( 'side' );
366 ?>
367 </div>
368 </div>
369 <br class="clear" />
370 </div>
371 </form>
372 </div>
373 <?php
374 }
375
376 /**
377 * Render the navigation menu with links to the possible actions, highlighting the current one.
378 *
379 * @since 1.0.0
380 */
381 protected function print_nav_tab_menu() {
382 ?>
383 <div id="tablepress-nav" class="nav-tab-wrapper">
384 <h1 class="wp-heading-inline"><?php _e( 'TablePress', 'tablepress' ); ?></h1>
385 <?php
386 foreach ( $this->data['view_actions'] as $action => $entry ) {
387 if ( '' === $entry['nav_tab_title'] ) {
388 continue;
389 }
390 if ( ! current_user_can( $entry['required_cap'] ) ) {
391 continue;
392 }
393
394 $url = esc_url( TablePress::url( array( 'action' => $action ) ) );
395 $active = ( $action === $this->action ) ? ' nav-tab-active' : '';
396 $separator = ( 'options' === $action ) ? ' nav-tab-separator' : ''; // Make the "Plugin Options" entry a separator, for some spacing.
397 echo "<a class=\"nav-tab{$active}{$separator}\" href=\"{$url}\">{$entry['nav_tab_title']}</a>";
398 }
399 ?>
400 </div><hr class="wp-header-end" />
401 <?php
402 }
403
404 /**
405 * Print a submit button (only done when function is used as a callback for a text box).
406 *
407 * @since 1.0.0
408 *
409 * @param array $data Data for this screen.
410 * @param array $box Information about the text box.
411 */
412 protected function textbox_submit_button( array $data, array $box ) {
413 $caption = isset( $data['submit_button_caption'] ) ? $data['submit_button_caption'] : __( 'Save Changes', 'tablepress' );
414 ?>
415 <p class="submit"><input type="submit" value="<?php echo esc_attr( $caption ); ?>" class="button button-primary button-large" name="submit" /></p>
416 <?php
417 }
418
419 /**
420 * Return the content for the help tab for this screen.
421 *
422 * Has to be implemented for every view that is visible in the WP Dashboard!
423 *
424 * @since 1.0.0
425 */
426 protected function help_tab_content() {
427 // Has to be implemented for every view that is visible in the WP Dashboard!
428 return '';
429 }
430
431 /**
432 * Initialize the WP feature pointers for TablePress.
433 *
434 * @since 1.0.0
435 */
436 protected function _init_wp_pointers() {
437 // Check if there are WP pointers for this view.
438 if ( empty( $this->wp_pointers ) ) {
439 return;
440 }
441
442 // Get dismissed pointers.
443 $dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
444
445 $got_pointers = false;
446 foreach ( array_diff( $this->wp_pointers, $dismissed ) as $pointer ) {
447 // Bind pointer print function.
448 add_action( "admin_footer-{$GLOBALS['hook_suffix']}", array( $this, 'wp_pointer_' . $pointer ) );
449 $got_pointers = true;
450 }
451
452 if ( ! $got_pointers ) {
453 return;
454 }
455
456 // Add pointers script and style to queue.
457 wp_enqueue_style( 'wp-pointer' );
458 wp_enqueue_script( 'wp-pointer' );
459 }
460
461 } // class TablePress_View
462