post_type = self::uglify( $name ); # Listen for the save post hook add_action( 'save_post', array( $this, 'save' ) ); } /** * Get default value for meta box * * @access public * @return string * @since 1.0.0 * * @package BizSmart WordPress Theme */ public static function get_value( $value, $field ){ if( empty( $value ) && is_array( $field ) && isset( $field[ 'default' ] ) ){ $value = $field[ 'default' ]; } return $value; } public static function label( $field ){ if( !isset( $field[ 'label' ] ) ) return; ?>

'; switch( $field[ 'type' ] ){ case 'radio': ?>
$option ): ?>
>
>
'; } /** * Returns the sanitized value * * @access public * @return string * @since 1.0.0 * * @package BizSmart WordPress Theme */ public static function sanitize( $field ){ $value = $field[ 'value' ]; switch( $field[ 'type' ] ){ case 'radio': case 'select': case 'checkbox': $value = sanitize_key( $value ); break; default: $value = wp_kses_post( $value ); break; } return $value; } /** * Stores all the meta boxes into the array and add it for registration. * * @access public * @return object * @since 1.0.0 * * @package BizSmart WordPress Theme */ public function add_meta_box( $title, $fields = array(), $context = 'side', $priority = 'default' ){ add_action( 'load-post.php', array( $this, 'init_metabox' ) ); add_action( 'load-post-new.php', array( $this, 'init_metabox' ) ); $boxes = array( 'id' => self::uglify( $title ), 'title' => self::beautify( $title ), 'fields' => $fields, 'context' => $context, 'priority' => $priority ); $this->meta_boxes[] = $boxes; } public function init_metabox(){ add_action( 'add_meta_boxes', array( $this, 'register_meta_box' ) ); } /** * Registers all the meta boxes from the array. * * @access public * @return object * @since 1.0.0 * * @package BizSmart WordPress Theme */ public function register_meta_box(){ if( is_array( $this->meta_boxes ) ){ foreach( $this->meta_boxes as $meta ){ add_meta_box( $meta[ 'id' ], $meta[ 'title' ], array( $this, 'render_meta_box' ), $this->post_type, $meta[ 'context' ], $meta[ 'priority' ], $meta[ 'fields' ] ); } } } /** * Displayes the meta box. * * @access public * @return void * @since 1.0.0 * * @package BizSmart WordPress Theme */ public function render_meta_box( $post, $box ){ if( !is_array( $box[ 'args' ] ) ) return; wp_nonce_field( self::with_prefix( 'meta_nonce' ), self::with_prefix( 'name_meta_nonce' ) ); ?>
$field ): $id = self::with_prefix( self::uglify( $id ) ); ?>
ID, $id, true ); $field[ 'value' ] = self::get_value( $v, $field ); self::render_field( $field ); ?>
post_type === $p[ 'post_type' ] ) { # do stuff foreach( $this->meta_boxes as $meta ){ foreach( $meta[ 'fields' ] as $id => $field){ $id = self::with_prefix( $id ); $value = self::sanitize( array( 'type' => $field[ 'type' ], 'value' => $p[ $id ] )); update_post_meta( $post_id, $id, $value ); } } } } } endif;