post_types = array( 'post', 'page' ); // Loop through post types and add meta-box to corresponding post types if ( $this->post_types ) { foreach( $this->post_types as $key => $val ) { add_action( 'add_meta_boxes_'. $val, array( $this, 'post_meta' ), 11 ); } } // Load scripts for the metabox add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) ); // Save meta Box add_action( 'save_post', array( $this, 'save_meta_data' ) ); } /** * Add Meta-Box */ 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', 'atento' ), array( $this, 'display_meta_box' ), $post->post_type, 'normal', 'high' ); } /** * Enqueue scripts and styles */ 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; } // Return if wrong post type if ( ! in_array( $post->post_type, $this->post_types ) ) { return; } $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; // Enqueue style wp_enqueue_style( 'atento-metabox-style', ATENTO_THEME_URI .'/assets/back-end/css/metabox-style' . $min . '.css', false, ATENTO_THEME_VERSION, 'all' ); // Image Uploader wp_enqueue_media(); // Enqueue Script wp_enqueue_script( 'atento-metabox-script', ATENTO_THEME_URI . '/assets/back-end/js/metabox-script' . $min . '.js', array( 'jquery' ), ATENTO_THEME_VERSION, true ); } /** * Display Meta-Box Fields */ public function display_meta_box( $post ) { // Add nonce for security and authentication. wp_nonce_field( basename( __FILE__ ), 'atento_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.', 'atento' ) .'
'; // Make sure tabs aren't empty if ( empty( $tabs ) ) { echo wp_kses_post( $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 wp_kses_post( $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] ) ) { if ( 'radio' == $type ) { if ( 'default-sidebar' !== $_POST[$id] ) { $value = sanitize_text_field( wp_unslash( $_POST[$id] ) ); } } // All else else { $value = sanitize_text_field( wp_unslash( $_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 */ private function meta_array( $post = null ) { // Prefix $prefix = 'atento_'; // Define array $array = array(); // Default variable $default = esc_html__( 'Default', 'atento' ); // Sidebar Layout Tab $array['sidebar'] = array( 'title' => esc_html__( 'Sidebar Layout', 'atento' ), 'settings' => array( 'sidebar_layout' =>array( 'description' => esc_html__( 'Default value is inherit from single post or page settings.', 'atento' ), 'id' => $prefix . 'sidebar_layout', 'type' => 'radio', 'options' => atento_content_layouts( array( 'default-sidebar' => $default ) ), 'default' => 'default-sidebar', ), ), ); // Apply filter & return settings array return apply_filters( 'atento_metabox_array', $array, $post ); } } // Class needed only in the admin if ( is_admin() ) { new Atento_Post_Global_Meta_Box; } }