add_section( 'general', array( 'title' => esc_html__( 'General', 'botiga' ), ) ); $this->add_field( '_botiga_hide_page_title', array( 'post_type' => array( 'page' ), 'section' => 'general', 'type' => 'switcher', 'title' => esc_html__( 'Hide Page Title', 'botiga' ), ) ); $this->add_field( '_botiga_disable_header_transparent', array( 'section' => 'general', 'type' => 'switcher', 'title' => esc_html__( 'Disable Transparent Header', 'botiga' ), ) ); $this->add_field( '_botiga_page_builder_mode', array( 'post_type' => array( 'post', 'page' ), 'section' => 'general', 'type' => 'switcher', 'title' => esc_html__( 'Page Builder Mode', 'botiga' ), 'subtitle' => esc_html__( 'This mode activates a simplified canvas for building custom pages with either the WP editor or a page builder plugin.', 'botiga' ), ) ); // End: General Options // // // Begin: Sidebar Options $this->add_section( 'sidebar', array( 'post_type' => array( 'post', 'page' ), 'title' => esc_html__( 'Sidebar', 'botiga' ), ) ); $this->add_field( '_botiga_sidebar_layout', array( 'section' => 'sidebar', 'type' => 'choices', 'title' => esc_html__( 'Sidebar Layout', 'botiga' ), 'options' => array( 'customizer' => array( 'label' => esc_html__( 'Default', 'botiga' ), 'image' => '%s/assets/img/meta-sidebar-default.svg', ), 'sidebar-left' => array( 'label' => esc_html__( 'Left', 'botiga' ), 'image' => '%s/assets/img/meta-sidebar-left.svg', ), 'sidebar-right' => array( 'label' => esc_html__( 'Right', 'botiga' ), 'image' => '%s/assets/img/meta-sidebar-right.svg', ), 'no-sidebar' => array( 'label' => esc_html__( 'No Sidebar', 'botiga' ), 'image' => '%s/assets/img/meta-sidebar-none.svg', ), ), ) ); // End: Sidebar Options // do_action( 'botiga_metabox_options', self::$options ); self::$options = apply_filters( 'botiga_metabox_options_filter', self::$options ); // // Set priority order // self::$options = wp_list_sort( self::$options, array( 'priority' => 'ASC' ), 'ASC', true ); foreach ( self::$options as $key => $value ) { self::$options[ $key ]['fields'] = wp_list_sort( $value['fields'], array( 'priority' => 'ASC' ), 'ASC', true ); } return self::$options; } public function add_section( $id, $args ) { if ( ! empty( $args['post_type'] ) && ! in_array( get_post_type(), $args['post_type'] ) ) { return; } $args = wp_parse_args( $args, array( 'title' => '', 'fields' => array(), 'priority' => ( count( self::$options ) + 1 ) * 10, ) ); self::$options[ $id ] = $args; } public function add_field( $id, $args ) { if ( ( ! empty( $args['post_type'] ) && ! in_array( get_post_type(), $args['post_type'] ) ) || empty( self::$options[ $args['section'] ] ) ) { return; } $args = wp_parse_args( $args, array( 'priority' => ( count( self::$options[ $args['section'] ]['fields'] ) + 1 ) * 10, ) ); self::$options[ $args['section'] ]['fields'][ $id ] = $args; } public function add_metabox( $post_type ) { if ( $post_type === 'attachment' ) { return; } $types = get_post_types( array( 'public' => true, ) ); if ( ! in_array( $post_type, $types ) ) { return; } switch ( $post_type ) { case 'post': $metabox_title = esc_html__( 'Botiga Post Options', 'botiga' ); break; case 'page': $metabox_title = esc_html__( 'Botiga Page Options', 'botiga' ); break; case 'product': $metabox_title = esc_html__( 'Botiga Product Options', 'botiga' ); break; } $metabox_title = apply_filters( 'botiga_metabox_title', $metabox_title, $post_type ); add_meta_box( 'botiga_metabox', $metabox_title, array( $this, 'render_metabox_content' ), $types, 'normal', 'low' ); } public function render_metabox_content( $post ) { $options = $this->metabox_options(); $post_type = get_post_type( $post ); wp_nonce_field( 'botiga_metabox', 'botiga_metabox_nonce' ); echo '
'; } public function save_metabox( $post_id ) { if ( ! isset( $_POST['botiga_metabox_nonce'] ) ) { return $post_id; } $nonce = sanitize_key( wp_unslash( $_POST['botiga_metabox_nonce'] ) ); if ( ! wp_verify_nonce( $nonce, 'botiga_metabox' ) ) { return $post_id; } if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return $post_id; } if ( ! current_user_can( 'edit_page', $post_id ) ) { return $post_id; } $options = $this->metabox_options(); if ( empty( $options ) ) { return $post_id; } foreach ( $options as $option ) { if ( ! empty( $option['fields'] ) ) { foreach ( $option['fields'] as $field_id => $field ) { if ( in_array( $field['type'], array( 'content' ) ) ) { continue; } $value = ( isset( $_POST[ $field_id ] ) ) ? wp_unslash( $_POST[ $field_id ] ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $value = $this->sanitize( $field, $value ); update_post_meta( $post_id, $field_id, $value ); } } } } public function sanitize( $field, $value ) { switch ( $field['type'] ) { case 'text': return sanitize_text_field( $value ); break; case 'textarea': return sanitize_textarea_field( $value ); break; case 'checkbox': case 'switcher': return ( $value === '1' ) ? 1 : 0; break; case 'number': return absint( $value ); break; case 'select': case 'choices': return ( in_array( $value, array_keys( $field['options'] ) ) ) ? sanitize_key( $value ) : ''; break; } return $value; } public function get_field( $field_id, $field, $value ) { switch ( $field['type'] ) { case 'text': echo ''; break; case 'number': echo ''; break; case 'textarea': echo ''; break; case 'checkbox': case 'switcher': $field = wp_parse_args( $field, array( 'label' => '', ) ); echo ''; break; case 'select': echo ''; break; case 'choices': echo ''; break; case 'content': echo ''; break; } } } new Botiga_Metabox();