boxes = array( array( 'settings', 'Settings box', 'normal' )
... );
You must also define two methods in your class for each box:
function settings_box() - this is where the box content is echoed
function settings_handler() - this is where the box settings are saved
...
*/
abstract class scbBoxesPage extends scbAdminPage {
/*
A box definition looks like this:
array( $slug, $title, $column );
Available columns: normal, side, column3, column4
*/
protected $boxes = array();
function __construct( $file, $options = null ) {
parent::__construct( $file, $options );
// too late
scbUtil::add_uninstall_hook( $this->file, array( $this, 'uninstall' ) );
}
function page_init() {
if ( !isset( $this->args['columns'] ) )
$this->args['columns'] = 2;
parent::page_init();
add_action( 'load-' . $this->pagehook, array( $this, 'boxes_init' ) );
add_filter( 'screen_layout_columns', array( $this, 'columns' ) );
}
function default_css() {
?>
default_css();
global $screen_layout_columns;
if ( isset( $screen_layout_columns ) ) {
$hide2 = $hide3 = $hide4 = '';
switch ( $screen_layout_columns ) {
case 4:
$width = 'width:24.5%;';
break;
case 3:
$width = 'width:32.67%;';
$hide4 = 'display:none;';
break;
case 2:
$width = 'width:49%;';
$hide3 = $hide4 = 'display:none;';
break;
default:
$width = 'width:98%;';
$hide2 = $hide3 = $hide4 = 'display:none;';
}
}
?>
\n";
do_meta_boxes( $this->pagehook, 'normal', '' );
echo "\t
\n";
do_meta_boxes( $this->pagehook, 'side', '' );
echo "\t
\n";
do_meta_boxes( $this->pagehook, 'column3', '' );
echo "\t
\n";
do_meta_boxes( $this->pagehook, 'column4', '' );
?>
_boxes_js_init();
}
function form_handler() {
if ( empty( $_POST ) )
return;
check_admin_referer( $this->nonce );
// Box handler
foreach ( $this->boxes as $box ) {
$args = isset( $box[4] ) ? $box[4] : array();
$handler = $box[0] . '_handler';
if ( method_exists( $this, $handler ) )
call_user_func_array( array( $this, $handler ), $args );
}
}
function columns( $columns ) {
$columns[$this->pagehook] = $this->args['columns'];
return $columns;
}
function uninstall() {
global $wpdb;
$hook = str_replace( '-', '', $this->pagehook );
foreach ( array( 'metaboxhidden', 'closedpostboxes', 'wp_metaboxorder', 'screen_layout' ) as $option )
$keys[] = "'{$option}_{$hook}'";
$keys = '( ' . implode( ', ', $keys ) . ' )';
$wpdb->query( "
DELETE FROM {$wpdb->usermeta}
WHERE meta_key IN {$keys}
" );
}
function boxes_init() {
wp_enqueue_script( 'common' );
wp_enqueue_script( 'wp-lists' );
wp_enqueue_script( 'postbox' );
$registered = array();
foreach( $this->boxes as $box_args ) {
@list( $name, $title, $context, $priority, $args ) = $box_args;
if ( empty( $title ) )
$title = ucfirst( $name );
if ( empty( $context ) )
$context = 'normal';
if ( empty( $priority ) )
$priority = 'default';
if ( empty( $args ) )
$args = array();
if ( isset( $registered[$name] ) ) {
if ( empty( $args ) )
trigger_error( "Duplicate box name: $name", E_USER_NOTICE );
$name = $this->_increment( $name );
} else {
$registered[$name] = true;
}
add_meta_box( $name, $title, array( $this, '_intermediate_callback' ), $this->pagehook, $context, $priority, $args );
}
}
// Make it so that $args is actually what's passed to the callback
function _intermediate_callback( $_, $box ) {
list( $name ) = explode( '-', $box['id'] );
call_user_func_array( array( $this, $name . '_box' ), $box['args'] );
}
private function _increment( $name ) {
$parts = explode( '-', $name );
if ( isset( $parts[1] ) )
$parts[1]++;
else
$parts[1] = 2;
return implode( '-', $parts );
}
// Adds necesary code for JS to work
function _boxes_js_init() {
echo $this->js_wrap( <<pagehook' );
} );
EOT
);
?>