PluginProbe ʕ •ᴥ•ʔ
External Links – nofollow, noopener & new window / 1.55
External Links – nofollow, noopener & new window v1.55
2.66 0.31 0.32 0.33 0.34 0.35 1.01 1.02 1.03 1.10 1.20 1.21 1.30 1.31 1.40 1.41 1.50 1.51 1.52 1.53 1.54 1.55 1.56 1.60 1.61 1.62 1.70 1.80 1.81 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 2.3 2.32 2.35 2.40 2.42 2.43 2.45 2.46 2.47 2.48 2.50 2.51 2.55 2.56 2.57 2.58 2.59 2.60 2.61 2.62 2.63 2.64 2.65 trunk 0.10 0.11 0.12 0.20 0.21 0.30
wp-external-links / includes / wp-plugin-dev-classes / class-wp-meta-box-page.php
wp-external-links / includes / wp-plugin-dev-classes Last commit date
class-wp-meta-box-page.php 11 years ago class-wp-option-forms.php 11 years ago
class-wp-meta-box-page.php
664 lines
1 <?php defined( 'ABSPATH' ) OR die( 'No direct access.' );
2 if ( ! class_exists( 'WP_Meta_Box_Page_01' ) ):
3 /**
4 * WP_Meta_Box_Page_01 Class
5 *
6 * Creating a dashboard-like admin page with meta boxes
7 *
8 * Requires WordPress 3.0+ and PHP 5.2+
9 *
10 * Custom Actions:
11 * - load action params: (1) this object
12 * - meta_box_load
13 * - meta_box_load-[page_slug]
14 *
15 * Custom Filters:
16 * - page_title filter params: (1) default content, (2) this object
17 * - meta_box_page_title
18 * - meta_box_page_title-[page_slug]
19 *
20 * - page_content filter params: (1) default content, (2) this object
21 * - meta_box_page_content
22 * - meta_box_page_content-[page_slug]
23 *
24 * - screen_settings filter params: (1) default content, (2) current screen object, (3) this object
25 * - meta_box_screen_settings
26 * - meta_box_screen_settings-[page_slug]
27 *
28 * - contextual_help filter params: (1) default content, (2) current screen id, (3) current screen object, (4) this object
29 * - meta_box_contextual_help
30 * - meta_box_contextual_help-[page_slug]
31 *
32 * @version 0.1
33 * @author Victor Villaverde Laan
34 * @link http://www.freelancephp.net/
35 * @license Dual licensed under the MIT and GPL licenses
36 */
37 class WP_Meta_Box_Page_01 {
38
39 /**
40 * Default settings
41 * @var array
42 */
43 private static $default_settings = array(
44 // Page title
45 'page_title' => NULL, // Default will be set equal to $menu_title
46
47 // Menu title
48 'menu_title' => NULL, // Default will be set equal to $page_title
49
50 // Page slug
51 'page_slug' => NULL, // Default will be set to: sanitize_title_with_dashes( $menu_title )
52
53 // Default number of columns
54 'default_columns' => 2,
55
56 // Column widths
57 'column_widths' => array(
58 1 => array( 99 ),
59 2 => array( 49, 49 ),
60 3 => array( 32.33, 32.33, 32.33 ),
61 4 => array( 24, 24, 24, 24 ),
62 ),
63
64 // Add page method
65 'add_page_method' => 'add_options_page', // OR: add_menu_page, add_object_page, add_submenu_page
66
67 // Extra params for the add_page_method
68
69 // Capability
70 'capability' => 'manage_options', // Optional for all methods
71
72 // Parent slug
73 'parent_slug' => NULL, // Nescessary when using "add_submenu_page"
74
75 // Url to the icon to be used for the menu ( around 20 x 20 pixels )
76 'icon_url' => NULL, // Only for "add_menu_page" or "add_object_page"
77
78 // The position in the menu order this menu should appear
79 'position' => NULL, // Only for "add_menu_page", default on bottom of the menu
80 );
81
82 /**
83 * Settings
84 * @var array
85 */
86 private $settings = array();
87
88 /**
89 * Meta boxes
90 * @var array
91 */
92 private $meta_boxes = array();
93
94 /**
95 * Pagehook ( will be set when page is created )
96 * @var string
97 */
98 protected $pagehook = NULL;
99
100
101 /**
102 * Initialize
103 * @param array $settings Optional
104 */
105 public function init( $settings = NULL, $load_callback = NULL ) {
106 // get settings of child class
107 $child_settings = $this->settings;
108
109 // set settings in 3 steps...
110 // (1) first set the default options
111 $this->set_setting( self::$default_settings );
112
113 // (2) set child settings
114 if ( ! empty( $child_settings ) )
115 $this->set_setting( $child_settings );
116
117 // (3) set param settings
118 if ( $settings !== NULL )
119 $this->set_setting( $settings );
120
121 // actions
122 add_action( 'admin_menu', array( $this, 'call_admin_menu' ) );
123
124 // add load action
125 if ( $load_callback !== NULL )
126 add_action( 'meta_box_load-'. $this->get_setting( 'page_slug' ), $load_callback );
127 }
128
129 /**
130 * Set default setting value
131 * @param mixed $key Also possible to give an array of key/value pairs
132 * @param mixed $value Optional
133 * @static
134 */
135 public static function set_default_setting( $key, $value = NULL ) {
136 if ( is_array( $key ) ) {
137 foreach ( $key AS $k => $v )
138 self::set_default_setting( $k, $v );
139 } else {
140 self::$default_settings[ $key ] = $value;
141 }
142 }
143
144 /**
145 * Set setting value
146 * @param mixed $key Also possible to give an array of key/value pairs
147 * @param mixed $value Optional
148 * @return $this For chaining
149 */
150 public function set_setting( $key, $value = NULL ) {
151 if ( is_array( $key ) ) {
152 foreach ( $key AS $k => $v )
153 $this->set_setting( $k, $v );
154 } else {
155 $this->settings[ $key ] = $value;
156 }
157
158 // auto-set related prop values
159 if ( $value !== NULL ) {
160 if ( $key == 'menu_title' AND $this->get_setting( 'page_title' ) === NULL )
161 $this->set_setting( 'page_title', $value );
162
163 if ( $key == 'page_title' AND $this->get_setting( 'menu_title' ) === NULL )
164 $this->set_setting( 'menu_title', $value );
165
166 if ( ( $key == 'menu_title' OR $key == 'page_title' ) AND $this->get_setting( 'page_slug' ) === NULL ) {
167 $new_val = $value;
168 $new_val = sanitize_title_with_dashes( $new_val );
169 $new_val = strtolower( $new_val );
170
171 // check for valid page_slug
172 if ( ! preg_match( '/^[a-z_-]+$/', $new_val ) ) {
173 $new_val = str_replace(
174 array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ),
175 array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ),
176 $new_val
177 );
178
179 $new_val = ereg_replace( '[^a-z_-]', '', $new_val );
180 }
181
182 $this->set_setting( 'page_slug', $new_val );
183 }
184 }
185
186 return $this;
187 }
188
189 /**
190 * Get setting value
191 * @param string $key Optional, when NULL will return array of all options
192 * @param mixed $default Optional, return default when key cannot be found OR is NULL
193 * @return mixed
194 */
195 public function get_setting( $key = NULL, $default = NULL ) {
196 if ( $key === NULL )
197 return $this->settings;
198
199 if ( key_exists( $key, $this->settings ) )
200 return ( $this->settings[ $key ] === NULL ) ? $default : $this->settings[ $key ];
201
202 return $default;
203 }
204
205 /**
206 * Helper for adding a meta box
207 * @param string $title Title of the meta box
208 * @param string $callback Callback for the meta box content
209 * @param mixed $context Optional, add meta box to this column (normal = 1, side = 2, column3 = 3, column4 = 4)
210 * @param string $priority Optional, the priority within the context where the boxes should show ( 'high', 'core', 'default' or 'low' )
211 * @return $this For chaining
212 */
213 public function add_meta_box( $title, $callback, $context = 'normal', $id = NULL, $priority = 'default' ) {
214 $this->meta_boxes[] = array(
215 'id' => $id,
216 'title' => $title,
217 'callback' => $callback,
218 'context' => $context,
219 'priority' => $priority,
220 );
221
222 return $this;
223 }
224
225 /**
226 * Add callback to "meta_box_load-[page]" action, only applied for this page/object
227 * @param mixed $callback Callback function
228 * @return $this
229 */
230 public function add_load_action( $callback ) {
231 add_filter( 'meta_box_load-' . $this->get_setting( 'page_slug' ), $callback );
232 return $this;
233 }
234
235 /**
236 * Add callback to "meta_box_load" action, applied to all instances of this class
237 * @param mixed $callback Callback function
238 * @static
239 */
240 public static function add_global_load_action( $callback ) {
241 add_filter( 'meta_box_load', $callback );
242 }
243
244 /**
245 * Add callback to "meta_box_screen_settings-[page]" filter, only applied for this page/object
246 * @param mixed $callback Callback function
247 * @return $this
248 */
249 public function add_screen_settings_filter( $callback ) {
250 add_filter( 'meta_box_screen_settings-' . $this->get_setting( 'page_slug' ), $callback );
251 return $this;
252 }
253
254 /**
255 * Add callback to "meta_box_contextual_help-[page]" filter, only applied for this page/object
256 * @param mixed $callback Callback function
257 * @return $this
258 */
259 public function add_contextual_help_filter( $callback ) {
260 add_filter( 'meta_box_contextual_help-' . $this->get_setting( 'page_slug' ), $callback );
261 return $this;
262 }
263
264 /**
265 * Add callback to "meta_box_page_title-[page]" filter, only applied for this page/object
266 * @param mixed $callback Callback function
267 * @return $this
268 */
269 public function add_title_filter( $callback ) {
270 add_filter( 'meta_box_page_title-' . $this->get_setting( 'page_slug' ), $callback );
271 return $this;
272 }
273
274 /**
275 * Add callback to "meta_box_page_content" filter, applied to all instances of this class
276 * @param mixed $callback Callback function
277 * @static
278 */
279 public static function add_global_title_filter( $callback ) {
280 add_filter( 'meta_box_page_title', $callback );
281 }
282
283 /**
284 * Add callback to "meta_box_page_content-[page]" filter, only applied for this page/object
285 * @param mixed $callback Callback function
286 * @return $this
287 */
288 public function add_content_filter( $callback ) {
289 add_filter( 'meta_box_page_title-' . $this->get_setting( 'page_slug' ), $callback );
290 return $this;
291 }
292
293 /**
294 * Add callback to "meta_box_page_content" filter, applied to all instances of this class
295 * @param mixed $callback Callback function
296 * @static
297 */
298 public static function add_global_content_filter( $callback ) {
299 add_filter( 'meta_box_page_title', $callback );
300 }
301
302 /**
303 * Admin menu callback
304 */
305 public function call_admin_menu() {
306 // add page
307 switch ( $this->get_setting( 'add_page_method' ) ) {
308 case 'add_menu_page':
309 $this->pagehook = add_menu_page(
310 $this->get_setting( 'page_title' ),
311 $this->get_setting( 'menu_title' ),
312 $this->get_setting( 'capability' ),
313 $this->get_setting( 'page_slug' ),
314 array( $this, 'call_page_content' ),
315 $this->get_setting( 'icon_url' ),
316 $this->get_setting( 'position' )
317 );
318 break;
319
320 case 'add_object_page':
321 $this->pagehook = add_object_page(
322 $this->get_setting( 'page_title' ),
323 $this->get_setting( 'menu_title' ),
324 $this->get_setting( 'capability' ),
325 $this->get_setting( 'page_slug' ),
326 array( $this, 'call_page_content' ),
327 $this->get_setting( 'icon_url' )
328 );
329 break;
330
331 case 'add_submenu_page':
332 $this->pagehook = add_submenu_page(
333 $this->get_setting( 'parent_slug' ),
334 $this->get_setting( 'page_title' ),
335 $this->get_setting( 'menu_title' ),
336 $this->get_setting( 'capability' ),
337 $this->get_setting( 'page_slug' ),
338 array( $this, 'call_page_content' )
339 );
340 break;
341
342 case 'add_options_page':
343 default:
344 $this->pagehook = add_options_page(
345 $this->get_setting( 'page_title' ),
346 $this->get_setting( 'menu_title' ),
347 $this->get_setting( 'capability' ),
348 $this->get_setting( 'page_slug' ),
349 array( $this, 'call_page_content' )
350 );
351 break;
352
353 }
354
355 // execute action
356 do_action( 'meta_box_load', $this );
357
358 // load page
359 add_action( 'load-' . $this->pagehook, array( $this, 'call_load_page' ) );
360 }
361
362 /**
363 * Admin head callback
364 */
365 public function call_admin_head() {
366 ?>
367 <style type="text/css">
368 .postbox-container { padding-right:1%; float:left ; /* for WP < 3.1 */ }
369 .postbox-container .meta-box-sortables { min-height:200px; }
370 .postbox-container .postbox { min-width:0; }
371 .postbox-container .postbox .inside { margin:10px 0 ; padding:0 10px; } /* for WP < 3.2 */
372 </style>
373 <?php
374 }
375
376 /**
377 * Load page callback
378 */
379 public function call_load_page() {
380 // execute action
381 do_action( 'meta_box_load-' . $this->get_setting( 'page_slug' ), $this );
382
383 // add script for meta boxes
384 wp_enqueue_script( 'postbox' );
385
386 // add to admin head
387 add_action( 'admin_head', array( $this, 'call_admin_head' ) );
388
389 // add screen settings filter
390 add_filter( 'screen_settings', array( $this, 'call_screen_settings') );
391
392 // add help text
393 add_filter( 'contextual_help', array( $this, 'call_contextual_help' ) );
394
395 // columns
396 if ( function_exists( 'add_screen_option' ) ) {
397 $count = count( $this->get_setting( 'column_widths' ) );
398
399 add_screen_option( 'layout_columns', array(
400 'max' => $count,
401 'default' => min( $count, $this->get_setting( 'default_columns' ) )
402 ));
403 }
404
405 // add meta boxes
406 $nr = 0;
407 foreach ( $this->meta_boxes AS $box ) {
408 $title = $box[ 'title' ];
409 $id = ( isset( $box[ 'id' ] ) ) ? $box[ 'id' ] : sanitize_title_with_dashes( $title .'-'. ++$nr, 'meta-box-' . $nr );
410 $callback = ( is_string( $box[ 'callback' ] ) && method_exists( $this, $box[ 'callback' ] ) ) ? array( $this, $box[ 'callback' ] ) : $box[ 'callback' ];
411 $context = $box[ 'context' ];
412 $priority = $box[ 'priority' ];
413
414 // set context
415 if ( $context == 2 OR strtolower( $context ) == 'side' ) {
416 $context = 'side';
417 } elseif ( $context == 3 OR strtolower( $context ) == 'column3' ) {
418 $context = 'column3';
419 } elseif ( $context == 4 OR strtolower( $context ) == 'column4' ) {
420 $context = 'column4';
421 } else { // default
422 $context = 'normal';
423 }
424
425 // add meta box
426 add_meta_box( $id, $title, $callback, $this->pagehook, $context, $priority ); // $callback_args, doesn't seem to work
427 }
428 }
429
430 /**
431 * Screen settings (callback)
432 * @param string $content
433 * @return string
434 */
435 public function call_screen_settings( $content ) {
436 if ( self::get_current_screen()->id == convert_to_screen( $this->pagehook )->id ) {
437 // apply filters for this meta box page
438 $content = apply_filters( 'meta_box_screen_settings-' . $this->get_setting( 'page_slug' ), $content, $this->get_current_screen(), $this );
439 }
440
441 return $content;
442 }
443
444 /**
445 * Contextual help (callback)
446 * @param string $content
447 * @return string
448 */
449 public function call_contextual_help( $content ) {
450 $current_screen = $this->get_current_screen();
451
452 if ( $current_screen->id == convert_to_screen( $this->pagehook )->id ) {
453 // apply filters for this meta box page
454 $content = apply_filters( 'meta_box_contextual_help-' . $this->get_setting( 'page_slug' ), $content, $current_screen->id, $current_screen, $this );
455 }
456
457 return $content;
458 }
459
460 /**
461 * Display admin page content (callback)
462 */
463 public function call_page_content() {
464 echo '<div class="wrap">';
465
466 // page title
467 $meta_boxes_page_title = apply_filters( 'meta_box_page_title', $this->get_page_title(), $this );
468 echo apply_filters( 'meta_box_page_title-' . $this->get_setting( 'page_slug' ), $meta_boxes_page_title, $this );
469
470 // page content
471 $meta_boxes_content = apply_filters( 'meta_box_page_content', $this->get_page_content(), $this );
472 echo apply_filters( 'meta_box_page_content-' . $this->get_setting( 'page_slug' ), $meta_boxes_content, $this );
473
474 echo '</div>';
475 }
476
477 /**
478 * Get page title
479 * Can be changed by using adding filters, see add_title_filter()
480 * @return string
481 */
482 private function get_page_title() {
483 return '<h2>' . get_admin_page_title() . '</h2>';
484 }
485
486 /**
487 * Get meta boxes content. Can be changed by adding filters, see add_content_filter()
488 * @return string
489 */
490 private function get_page_content() {
491 return self::get_ob_callback( array( $this, 'show_meta_boxes' ) );
492 }
493
494 /**
495 * Display meta boxes content
496 */
497 private function show_meta_boxes() {
498 $opt_column_widths = $this->get_setting( 'column_widths' );
499 $hide2 = $hide3 = $hide4 = '';
500 switch ( self::get_screen_layout_columns( $this->get_setting( 'default_columns' ) ) ) {
501 case 4:
502 $column_widths = $opt_column_widths[ 4 ];
503 break;
504 case 3:
505 $column_widths = $opt_column_widths[ 3 ];
506 $hide4 = 'display:none;';
507 break;
508 case 2:
509 $column_widths = $opt_column_widths[ 2 ];
510 $hide3 = $hide4 = 'display:none;';
511 break;
512 default:
513 $column_widths = $opt_column_widths[ 1 ];
514 $hide2 = $hide3 = $hide4 = 'display:none;';
515 }
516
517 $column_widths = array_pad( $column_widths, 4, 0 );
518 ?>
519 <div id='<?php echo $this->pagehook ?>-widgets' class='metabox-holder'>
520 <div class='postbox-container' style='width:<?php echo $column_widths[0] ?>%'>
521 <?php do_meta_boxes( $this->pagehook, 'normal', '' ); ?>
522 </div>
523
524 <div class='postbox-container' style='<?php echo $hide2 ?>width:<?php echo $column_widths[1] ?>%'>
525 <?php do_meta_boxes( $this->pagehook, 'side', '' ); ?>
526 </div>
527
528 <div class='postbox-container' style='<?php echo $hide3 ?>width:<?php echo $column_widths[2] ?>%'>
529 <?php do_meta_boxes( $this->pagehook, 'column3', '' ); ?>
530 </div>
531
532 <div class='postbox-container' style='<?php echo $hide4 ?>width:<?php echo $column_widths[3] ?>%'>
533 <?php do_meta_boxes( $this->pagehook, 'column4', '' ); ?>
534 </div>
535 </div>
536
537 <form style="display:none" method="get" action="">
538 <?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
539 <?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
540 </form>
541
542 <script type="text/javascript">
543 //<![CDATA[
544 jQuery( document ).ready( function( $ ){
545 var columnWidths = <?php echo json_encode( $opt_column_widths ) ?>,
546 $boxes = $( '.postbox-container' ),
547 setColumnWidths = function () {
548 var c = $( 'input[name="screen_columns"]:checked' ).val();
549
550 // first hide all boxes
551 $boxes.hide();
552
553 // set width and show boxes
554 for ( var x = 0; x < columnWidths[ c ].length; x++ ) {
555 $boxes.eq( x )
556 .css( 'width', columnWidths[ c ][ x ]+ '%' )
557 .show();
558 }
559 };
560
561 // radio screen columns
562 $( 'input[name="screen_columns"]' )
563 .click(function(){
564 if ( $( 'input[name="screen_columns"]:checked' ).val() == $( this ).val() ) {
565 setTimeout(function(){
566 setColumnWidths();
567 }, 1 );
568 }
569 })
570 .change(function( e ){
571 setColumnWidths();
572
573 // prevent
574 e.stopImmediatePropagation();
575 });
576
577 // trigger change event of selected column
578 $( 'input[name="screen_columns"]:checked' ).change();
579
580 <?php if ( self::wp_version( '3.2', '<' ) ): ?>
581 // for WP < 3.2
582
583 // close postboxes that should be closed
584 $( '.if-js-closed' ).removeClass( 'if-js-closed' ).addClass( 'closed' );
585
586 // Loading saved screen settings
587 postboxes.add_postbox_toggles( '<?php echo $this->pagehook ?>' );
588 <?php endif; ?>
589 });
590 //]]>
591 </script>
592 <?php
593 }
594
595 /**
596 * Static helpers
597 */
598
599 /**
600 * Get content displayed by given callback
601 * @param mixed $callback
602 * @return string
603 * @static
604 */
605 public static function get_ob_callback( $callback ) {
606 // start output buffer
607 ob_start();
608
609 // call callback
610 call_user_func( $callback );
611
612 // get the view content
613 $content = ob_get_contents();
614
615 // clean output buffer
616 ob_end_clean();
617
618 return $content;
619 }
620
621 /**
622 * Get current version of WP or compare versions
623 * @global string $wp_version
624 * @return mixed
625 * @static
626 */
627 public static function wp_version( $compare_version = NULL, $operator = NULL ) {
628 global $wp_version;
629 $cur_wp_version = preg_replace( '/-.*$/', '', $wp_version );
630
631 if ( $compare_version === NULL )
632 return $cur_wp_version;
633
634 // check comparison
635 return version_compare( $cur_wp_version, $compare_version, $operator );
636 }
637
638 /**
639 * Return global WP value of $screen_layout_columns
640 * @global integer $screen_layout_columns
641 * @return integer
642 * @static
643 */
644 public static function get_screen_layout_columns( $default = NULL ) {
645 global $screen_layout_columns;
646 return ( empty( $screen_layout_columns ) ) ? $default : $screen_layout_columns;
647 }
648
649 /**
650 * Return global WP value of $current_screen
651 * @global string $current_screen
652 * @return string
653 * @static
654 */
655 public static function get_current_screen( $default = NULL ) {
656 global $current_screen;
657 return $current_screen;
658 }
659
660 } // End WP_Meta_Box_Page_01 Class
661
662 endif;
663
664 /* ommit PHP closing tag, to prevent unwanted whitespace at the end of the parts generated by the included files */