$term, 'post_type' => $source, 'post_status' => 'publish', 'posts_per_page' => 25, 'order' => 'DESC', ) ); if ( ! empty( $query->posts ) ) { foreach( $query->posts as $post ) { $options[] = array( 'id' => $post->ID, 'text' => $post->post_title, ); } } break; } wp_send_json_success( $options ); } else { wp_send_json_error(); } } public function enqueue_metabox_scripts() { wp_enqueue_code_editor( array( 'type' => 'text/html', 'codemirror' => array( 'indentUnit' => 2, 'tabSize' => 2, ), ) ); wp_enqueue_script( 'botiga-select2', get_template_directory_uri() . '/assets/vendor/select2/select2.full.min.js', array( 'jquery' ), '4.0.13', true ); wp_enqueue_style( 'botiga-select2', get_template_directory_uri() . '/assets/vendor/select2/select2.min.css', array(), '4.0.13', 'all' ); wp_enqueue_style( 'botiga-metabox-styles', get_template_directory_uri() . '/assets/css/metabox.min.css', array(), BOTIGA_VERSION ); wp_enqueue_script( 'botiga-metabox-scripts', get_template_directory_uri() . '/assets/js/metabox.min.js', array( 'jquery', 'jquery-ui-sortable' ), BOTIGA_VERSION, true ); wp_localize_script( 'botiga-metabox-scripts', 'botiga_metabox', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'ajaxnonce' => wp_create_nonce( 'botiga_metabox' ), ) ); } public function metabox_options() { // // Begin: General Options $this->add_section( 'general', array( 'title' => esc_html__( 'General', 'botiga' ), /** * Hook 'botiga_metabox_exclude_post_types_from_general_section' * * @since 1.0.0 */ 'exclude' => apply_filters( 'botiga_metabox_exclude_post_types_from_general_section', array() ), ) ); $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( /** * Hook 'botiga_metabox_exclude_post_types_from_sidebar_section' * * @since 1.0.0 */ 'exclude' => apply_filters( 'botiga_metabox_exclude_post_types_from_sidebar_section', array() ), '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 // /** * Hook 'botiga_metabox_options' * * @since 1.0.0 */ do_action( 'botiga_metabox_options', self::$options ); /** * Hook 'botiga_metabox_options_filter' * * @since 1.0.0 */ 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; } if ( ! empty( $args['exclude'] ) && in_array( get_post_type(), $args['exclude'] ) ) { 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 ) { global $post; // Do not render the metabox in the page for posts (blog page). $page_for_posts = get_option( 'page_for_posts' ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual if( $page_for_posts && $post->ID == $page_for_posts ) { return; } if ( $post_type === 'attachment' ) { return; } $types = get_post_types( array( 'public' => true, ) ); if ( ! in_array( $post_type, $types ) ) { return; } $metabox_title = 'Botiga Options'; 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; case 'size_chart': $metabox_title = esc_html__( 'Botiga Size Chart Options', 'botiga' ); break; case 'linked_variation': $metabox_title = esc_html__( 'Botiga Linked Variation Options', 'botiga' ); break; } if ( class_exists( 'Botiga_Modules' ) && Botiga_Modules::is_module_active( 'templates' ) ) { unset( $types[ 'athemes_hf' ] ); } /** * Hook 'botiga_metabox_title' * * @since 1.0.0 */ $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 '
'; $has_tabs = ( ! empty( array_filter( array_column( $options, 'title' ) ) ) ) ? true : false; if ( ! empty( $has_tabs ) ) { echo '
'; $num = 0; foreach ( $options as $option ) { if ( ! empty( $option['title'] ) ) { $active = ( $num === 0 ) ? ' active' : ''; echo ''. esc_html( $option['title'] ) .''; ++$num; } } echo '
'; } echo '
'; $num = 0; foreach ( $options as $option ) { $active = ( $num === 0 ) ? ' active' : ''; echo '
'; if ( ! empty( $option['title'] ) ) { echo '

'. esc_html( $option['title'] ) .'

'; } if ( ! empty( $option['fields'] ) ) { foreach ( $option['fields'] as $field_id => $field ) { $separator = ( ! empty( $field['separator'] ) ) ? $field['separator'] : 'after'; $classes = array(); $classes[] = 'botiga-metabox-field'; $classes[] = 'botiga-metabox-field-separator-'. $separator; $classes[] = 'botiga-metabox-field-'. $field['type']; if ( ! empty( $field['class'] ) ) { $classes[] = $field['class']; } if ( ! empty( $field['inline'] ) ) { $classes[] = 'botiga-metabox-field-inline'; } if ( ! empty( $field['depend'] ) ) { $depend_meta = get_post_meta( $post->ID, $field['depend'], true ); if ( empty( $depend_meta ) ) { $classes[] = 'botiga-metabox-field-hidden'; } echo '
'; } else { echo '
'; } if ( isset( $field['title'] ) || isset( $field['subtitle'] ) ) { echo '
'; if ( ! empty( $field['title'] ) ) { echo '

'. wp_kses_post( $field['title'] ) .'

'; } if ( ! empty( $field['subtitle'] ) ) { echo ''. wp_kses_post( $field['subtitle'] ) .''; } echo '
'; } echo '
'; $meta = get_post_meta( $post->ID, $field_id ); $default = ( isset( $field['default'] ) ) ? $field['default'] : null; $value = ( isset( $meta[0] ) ) ? $meta[0] : $default; $this->get_field( $field_id, $field, $value ); if ( ! empty( $field['desc'] ) ) { echo '
'. wp_kses_post( $field['desc'] ) .'
'; } echo '
'; echo '
'; } } echo '
'; ++$num; } echo '
'; 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': case 'sidebar-select': case 'size-chart-select': 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': // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict return ( in_array( $value, array_keys( $field['options'] ) ) ) ? sanitize_key( $value ) : ''; break; case 'select-ajax': return ( is_array( $value ) && ! empty( $value ) ) ? array_filter( array_map( 'sanitize_text_field', $value ) ) : array(); break; case 'wc-attributes': return ( is_array( $value ) && ! empty( $value ) ) ? array_filter( array_map( 'sanitize_text_field', $value ) ) : array(); break; case 'repeater': case 'uploads': return ( is_array( $value ) && ! empty( $value ) ) ? array_filter( map_deep( $value, 'sanitize_text_field' ) ) : array(); break; case 'size-chart': return ( is_array( $value ) && ! empty( $value ) ) ? array_filter( map_deep( $value, 'sanitize_text_field' ) ) : array(); break; case 'wp-editor': return wp_kses_post( $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 'select-ajax': $field = wp_parse_args( $field, array( 'source' => 'post', ) ); $ids = ( is_array( $value ) && ! empty( $value ) ) ? $value : (array) $value; echo ''; break; case 'wc-attributes': $attributes = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_id' ); $values = ( is_array( $value ) && ! empty( $value ) ) ? $value : array(); if ( ! empty( $attributes ) ) { echo '
'; echo ''; echo '
'; } break; case 'choices': echo '
'; foreach ( $field['options'] as $key => $option ) { echo ''; } echo '
'; break; case 'content': echo wp_kses_post( $field['content'] ); break; case 'repeater': $field = wp_parse_args( $field, array( 'button' => '', ) ); echo '
'; $values = ( is_array( $value ) && ! empty( $value ) ) ? $value : array(); echo ''; echo ''; echo '
'; break; case 'media': $placeholder = class_exists( 'Woocommerce' ) ? wc_placeholder_img_src( 'thumbnail' ) : get_template_directory_uri() . '/assets/placeholder.svg'; $hidden_class = ( empty( $value ) ) ? ' hidden' : ''; if ( ! empty( $value ) ) { $attachment = wp_get_attachment_image_src( $value, 'thumbnail' ); $thumbnail = ( is_array( $attachment ) && ! empty( $attachment[0] ) ) ? $attachment[0] : $placeholder; } else { $thumbnail = $placeholder; } echo '
'; echo '
'; echo ''; echo '
'; echo ''; echo '
'; break; case 'uploads': $field = wp_parse_args( $field, array( 'button' => '', 'library' => 'image', ) ); echo '
'; $values = ( is_array( $value ) && ! empty( $value ) ) ? $value : array(); $name = $field['library'] == 'video' ? $field_id . '[0][src]' : $field_id . '[]'; $thumb_name = $field_id . '[0][thumb]'; echo ''; echo ''; echo '
'; break; case 'size-chart': $field = wp_parse_args( $field, array( 'button' => '', ) ); echo '
'; echo ''; echo ''; echo '
'; break; case 'size-chart-select': $options = array(); $posts = get_posts( array( 'post_type' => 'size_chart', 'posts_per_page' => -1, // phpcs:ignore WPThemeReview.CoreFunctionality.PostsPerPage.posts_per_page_posts_per_page 'post_status' => 'publish', ) ); if ( ! is_wp_error( $posts ) && ! empty( $posts ) ) { foreach ( $posts as $_post ) { $options[ $_post->ID ] = $_post->post_title; } } echo ''; break; case 'sidebar-select': $options = array(); global $wp_registered_sidebars; if ( ! empty( $wp_registered_sidebars ) ) { foreach ( $wp_registered_sidebars as $sidebar ) { $options[ $sidebar['id'] ] = $sidebar['name']; } } echo ''; break; case 'wp-editor': $field = wp_parse_args( $field, array( 'height' => 150, ) ); wp_editor( $value, $field_id, array( 'editor_height' => $field['height'], ) ); break; case 'code-editor': echo ''; break; case 'hook-select': $value = ! is_array( $value ) ? array( 'hook-name' => '', 'hook-priority' => '' ) : $value; $priority_value = ! empty( $value[ 'hook-priority' ] ) ? $value[ 'hook-priority' ] : 10; echo '
'; echo ''; echo ''; echo '
'; break; } } } new Botiga_Metabox();