$val ) { add_action( 'add_meta_boxes_'. $val, array( $this, 'post_meta' ), 11 ); } } // Save meta Box add_action( 'save_post', array( $this, 'save_meta_data' ) ); // Load scripts for the meta-box add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) ); } /** * Loads the required media files for the media manager and scripts for media widgets. */ public function load_scripts( $hook ) { // Only needed on these admin screens if ( $hook != 'edit.php' && $hook != 'post.php' && $hook != 'post-new.php' ) { return; } // Get global post global $post; // Return if post is not object if ( ! is_object( $post ) ) { return; } // Enqueue Style wp_enqueue_style( 'bizness-meta-box', BIZNESS_THEME_URI .'inc/meta-boxes/assets/css/style.css', false, BIZNESS_THEME_VERSION, 'all' ); // Enqueue media js wp_enqueue_media(); // Enqueue Script wp_enqueue_script( 'bizness-meta-box', BIZNESS_THEME_URI . 'inc/meta-boxes/assets/js/script.js', array( 'jquery' ), BIZNESS_THEME_VERSION, true ); } /** * Add Meta-Box * * @param $post */ public function post_meta( $post ) { // Add meta-box $obj = get_post_type_object( $post->post_type ); add_meta_box( 'post_meta_fields', $obj->labels->singular_name . ' '. esc_html__( 'Settings', 'bizness' ), array( $this, 'display_meta_box' ), $post->post_type, 'normal', 'high' ); } /** * Display Meta-Box Fields * * @param $post */ public function display_meta_box( $post ) { // Add nonce for security and authentication. wp_nonce_field( basename( __FILE__ ), 'bizness_meta_nonce' ); // Get current post data $post_id = $post->ID; $post_type = get_post_type(); // Get tabs $tabs = $this->meta_array( $post ); // Empty notice $empty_notice = '
'. esc_html__( 'No meta settings available for this post type or user.', 'bizness' ) .'
'; // Make sure tabs aren't empty if ( empty( $tabs ) ) { echo $empty_notice; return; } // Store tabs that should display on this specific page in an array for use later $active_tabs = array(); foreach ( $tabs as $tab ) { $tab_post_type = isset( $tab['post_type'] ) ? $tab['post_type'] : ''; if ( ! $tab_post_type ) { $display_tab = true; } elseif ( in_array( $post_type, $tab_post_type ) ) { $display_tab = true; } else { $display_tab = false; } if ( $display_tab ) { $active_tabs[] = $tab; } } // No active tabs if ( empty( $active_tabs ) ) { echo $empty_notice; return; } ?> meta_array(); $settings = array(); foreach( $tabs as $tab ) { foreach ( $tab['settings'] as $setting ) { $settings[] = $setting; } } // Loop through settings and validate foreach ( $settings as $setting ) { // Vars $value = ''; $id = $setting['id']; $type = isset ( $setting['type'] ) ? $setting['type'] : 'text'; // Make sure field exists and if so validate the data if ( isset( $_POST[$id] ) ) { // Validate text if ( 'text' == $type ) { $value = sanitize_text_field( $_POST[$id] ); } // Links elseif ( 'link' == $type ) { $value = esc_url_raw( $_POST[$id] ); } // Validate select elseif ( 'select' == $type ) { if ( '' !== $_POST[$id] ) { $value = sanitize_text_field( $_POST[$id] ); } } // Validate radio elseif ( 'radio' == $type ) { if ( 'default' == $_POST[$id] ) { $value = ''; } else { $value = sanitize_text_field( $_POST[$id] ); } } // Image elseif ( 'image' == $type ) { $value = absint( $_POST[$id] ); } // All else else { $value = sanitize_text_field( $_POST[$id] ); } // Update meta if value exists if ( $value ) { update_post_meta( $post_id, $id, $value ); } // Otherwise cleanup stuff else { delete_post_meta( $post_id, $id ); } } } } /** * Settings Array * * @param null $post * @return array */ private function meta_array( $post = null ) { // Prefix $prefix = 'bizness_'; // Define array $array = array(); // Sidebar Tab $array['sidebar'] = array( 'title' => esc_html__( 'Sidebar', 'bizness' ), 'settings' => array( 'sidebar_layout' =>array( 'title' => esc_html__( 'Sidebar Layout', 'bizness' ), 'description' => esc_html__( 'Default value is inherit from customizer saved value.', 'bizness' ), 'id' => $prefix . 'sidebar_layout', 'type' => 'radio', 'options' => array( 'default' => esc_html__( 'From Customizer', 'bizness' ), 'left' => esc_html__( 'Left Sidebar', 'bizness' ), 'right' => esc_html__( 'Right Sidebar', 'bizness' ), 'none' => esc_html__( 'Full Width', 'bizness' ), ), 'default' => 'default' ), ), ); // Apply filter & return settings array return apply_filters( 'bizness_meta_box_settings', $array, $post ); } } // Class needed only in the admin if ( is_admin() ) { Bizness_Meta_Boxes::get_instance(); }