Last active
October 31, 2019 17:19
-
-
Save sungraizfaryad/18b03d6919ed23757fa04247085323ea to your computer and use it in GitHub Desktop.
You can create additional metabox and display them on frontend.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Class Inspiry_Additional_Meta_Fields_Builder { | |
| public static $_instance; | |
| public $meta_box_data; | |
| function __construct(){ | |
| $this->meta_box_data = $this->inspiry_get_meta_box_value_array(); | |
| // Register settings page. In this case, it's a theme options page | |
| add_filter( 'mb_settings_pages', [$this, 'inspiry_additional_options_page'] ); | |
| // Register meta boxes and fields for settings page | |
| add_filter( 'rwmb_meta_boxes', [$this, 'inspiry_additional_meta_boxes'] ); | |
| // Priority 20 makes sure all meta boxes are already registered | |
| add_filter( 'rwmb_meta_boxes', [$this, 'inspiry_build_property_meta_boxes'], 20 ); | |
| add_filter( 'inspiry_real_estate_meta_search', [$this, 'inspiry_additional_fields_search_filter'] ); | |
| add_action('inspiry_additional_search_fields', [$this, 'inspiry_render_search_fields']); | |
| add_action('inspiry_additional_edit_property_fields', [$this, 'inspiry_render_submit_fields']); | |
| add_action('inspiry_additional_submit_property_fields', [$this, 'inspiry_render_submit_fields']); | |
| add_action('inspiry_after_property_submit', [$this, 'inspiry_submit_fields_database']); | |
| add_action('inspiry_after_property_update', [$this, 'inspiry_submit_fields_database']); | |
| add_action('inspiry_additional_property_meta_fields', [$this, 'inspiry_property_single_meta_fields']); | |
| add_action( 'admin_enqueue_scripts', [$this, 'inspiry_enqueue_my_scripts'] ); | |
| } | |
| public static function instance() { | |
| if ( is_null( self::$_instance ) ) { | |
| self::$_instance = new self(); | |
| } | |
| return self::$_instance; | |
| } | |
| function inspiry_additional_options_page( $settings_pages ) { | |
| $settings_pages[] = array( | |
| 'id' => 'inspiry_additional_meta_boxes_page', | |
| 'option_name' => 'inspiry_additional_meta_boxes', | |
| 'menu_title' => esc_html__( 'Inspiry Additional Meta Box', 'framework' ), | |
| 'parent' => 'real_homes', | |
| ); | |
| return $settings_pages; | |
| } | |
| // Helper functions to generate a unique key for meta box to avoid any duplication. User can make mistake. | |
| function inspiry_generate_meta_box_key( $key ){ | |
| return 'inspiry_' . preg_replace('/\s+/', '', $key); | |
| } | |
| // Helper functions to get meta box value array | |
| function inspiry_get_meta_box_value_array(){ | |
| $additional_meta_boxes = get_option('inspiry_additional_meta_boxes'); | |
| if( !empty( $additional_meta_boxes ) && isset($additional_meta_boxes['inspiry_additional_meta_list'])) { | |
| $add_meta_boxes = $additional_meta_boxes['inspiry_additional_meta_list']; | |
| return $add_meta_boxes; | |
| } | |
| return false; | |
| } | |
| // Helper functions to build array from user added options to generate select field | |
| function inspiry_build_meta_box_options($options){ | |
| $options = explode(',', $options); | |
| $options = array_filter(array_map('trim', $options)); | |
| $options = array_combine($options, $options); | |
| return $options; | |
| } | |
| function inspiry_additional_meta_boxes( $meta_boxes ) { | |
| //Get Tabs from meta box | |
| //$tabs = $meta_boxes[0]['tabs']; | |
| //Build Tabs array for use | |
| //$tabs= array_map(function($value){ return $value['label']; }, $tabs); | |
| //Attach additional tabs tab so user can add all meta fields for separate tab | |
| //$tabs['inspiry_additional_tabs']= esc_html__( 'Additional Meta', 'framework' ); | |
| $meta_boxes[] = array( | |
| 'id' => 'inspiry_additional_meta_boxes_settings', | |
| 'title' => esc_html__( 'Add New Meta Boxes', 'framework' ), | |
| 'settings_pages' => 'inspiry_additional_meta_boxes_page', | |
| 'fields' => array( | |
| array( | |
| 'id' => 'inspiry_additional_meta_list', | |
| 'type' => 'group', | |
| 'clone' => true, | |
| 'sort_clone' => true, | |
| 'fields' => array( | |
| array( | |
| 'name' => esc_html__('Meta Filed Name', 'framework'), | |
| 'desc' => esc_html__('Keep it short and unique. Do not use any special Characters. Example: Test Title One', 'framework'), | |
| 'id' => 'meta_name', | |
| 'type' => 'text', | |
| ), | |
| array( | |
| 'name' => esc_html__('Meta Filed Type', 'framework'), | |
| 'id' => 'meta_type', | |
| 'type' => 'select', | |
| 'class' => 'logical_select', | |
| 'options' => array( | |
| 'text' => 'Text', | |
| 'datetime' => 'Date & Time', | |
| 'select' => 'Select', | |
| ), | |
| ), | |
| array( | |
| 'name' => esc_html__('Meta Filed Select Options', 'framework'), | |
| 'desc' => esc_html__('Please add comma separated options. Example: one, two, three', 'framework'), | |
| 'id' => 'meta_select_options', | |
| 'class' => 'logical_show_hide', | |
| 'type' => 'textarea', | |
| ), | |
| array( | |
| 'name' => esc_html__('Meta Icon', 'framework'), | |
| 'desc' => sprintf( esc_html__( 'You can use the %s. You just need to add the class like %s ', 'framework' ), '<a target="_blank" href="https://fontawesome.com/v4.7.0/icons/">' . esc_html__( 'Font Awesome Icons', 'framework' ) . '</a>' , '<strong>fa fa-star</strong>' ), | |
| 'id' => 'meta_icon', | |
| 'type' => 'text', | |
| ), | |
| // array( | |
| // 'name' => esc_html__('Meta Box Tab', 'framework'), | |
| // 'desc' => sprintf( esc_html__( 'You can choose the %s in which you want to display thi field', 'framework' ), '<strong>' . esc_html__( 'Tab', 'framework' ) . '</strong>' ), | |
| // 'id' => 'meta_tab', | |
| // 'type' => 'select', | |
| // 'options' => $tabs, | |
| // ), | |
| array( | |
| 'name' => esc_html__('Where you want to display?', 'framework'), | |
| 'id' => 'meta_field_display', | |
| 'type' => 'checkbox_list', | |
| 'options' => array( | |
| 'search' => esc_html__('Advance Search Form', 'framework'), | |
| 'submit' => esc_html__('Property Submit', 'framework'), | |
| 'single' => esc_html__('Property Single', 'framework'), | |
| 'list_card' => esc_html__('Property List Card', 'framework'), | |
| 'grid_card' => esc_html__('Property Grid Card', 'framework'), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| return $meta_boxes; | |
| } | |
| function inspiry_build_property_meta_boxes( $meta_boxes ) { | |
| foreach ( $meta_boxes as $k => $meta_box ) { | |
| // Edit "Property Info" meta box | |
| if ( isset( $meta_box['id'] ) && 'property-meta-box' == $meta_box['id'] ) { | |
| $meta_boxes[$k]['tabs']['inspiry_additional_tabs']= esc_html__( 'Additional Meta', 'framework' ); | |
| $meta_box_array = $this->meta_box_data; | |
| if( $meta_box_array ){ | |
| foreach ($meta_box_array as $value){ | |
| $build = array( | |
| 'name' => $value['meta_name'], | |
| 'id' => $this->inspiry_generate_meta_box_key($value['meta_name']), | |
| 'type' => $value['meta_type'], | |
| 'tab' => 'inspiry_additional_tabs', // there is n issue in metabox columns which needs fix first before providing the tab option | |
| 'columns' => 6, | |
| ); | |
| if('select' === $value['meta_type']){ | |
| $result = $this->inspiry_build_meta_box_options( $value['meta_select_options']); | |
| $choose = [' ' => esc_html__('Choose Value', 'framework')]; | |
| $result = $choose + $result; | |
| $build['options'] = $result; | |
| } | |
| $meta_boxes[$k]['fields'][] = $build; | |
| } | |
| } | |
| } | |
| } | |
| return $meta_boxes; | |
| } | |
| function inspiry_additional_fields_search_filter( $meta_query ) { | |
| $meta_box_array = $this->meta_box_data; | |
| if( $meta_box_array){ | |
| foreach ($meta_box_array as $meta_box){ | |
| if(in_array('search', $meta_box['meta_field_display'])) { | |
| $key = $this->inspiry_generate_meta_box_key($meta_box['meta_name']); | |
| if ( ( ! empty( $_GET[ $key ] ) ) && ( $_GET[ $key ] != inspiry_any_value() ) ) { | |
| $meta_query[] = array( | |
| 'key' => $key, | |
| 'value' => $_GET[ $key ], | |
| ); | |
| } | |
| } | |
| } | |
| }; | |
| return $meta_query; | |
| } | |
| function inspiry_render_text_select_fields($basic, $classes, $searched, $options= []){ | |
| if('text' === $basic[0] || 'datetime' === $basic[0]){ | |
| ?> | |
| <div class="<?php echo esc_attr($classes[0]); ?>"> | |
| <label for="<?php echo esc_attr($basic[2]); ?>"> <?php echo esc_html($basic[1]); ?> </label> | |
| <input type="<?php echo $basic[0]; ?>" name="<?php echo esc_attr($basic[2]); ?>" id="<?php echo esc_attr($basic[2]); ?>" value="<?php echo esc_attr( $searched ); ?>" placeholder="<?php echo esc_attr( rh_any_text() ); ?>"/> | |
| </div> | |
| <?php | |
| }else{ | |
| ?> | |
| <div class="<?php echo esc_attr($classes[0]); ?>"> | |
| <label for="<?php echo esc_attr($basic[2]); ?>"> <?php echo esc_html($basic[1]); ?> </label> | |
| <span class="<?php echo esc_attr($classes[1]); ?>"> | |
| <select name="<?php echo esc_attr($basic[2]); ?>" id="<?php echo esc_attr($basic[2]); ?>" class="<?php echo esc_attr($classes[2]); ?>"> | |
| <?php | |
| if ( ( null == $searched ) ) { | |
| echo '<option value="' . inspiry_any_value() . '" selected="selected">' . esc_html( rh_any_text()) . '</option>'; | |
| }else{ | |
| echo '<option value="' . inspiry_any_value() . '">' . esc_html( rh_any_text()) . '</option>'; | |
| } | |
| foreach ($options as $keyword => $option){ | |
| if ( ( null !== $searched ) && ( $searched === $keyword ) ) { | |
| echo '<option selected="selected" value="' . esc_attr( $keyword ) . '">' . esc_html( $option ) . '</option>'; | |
| }else{ | |
| echo '<option value="' . esc_attr( $keyword ) . '">' . esc_html( $option ) . '</option>'; | |
| } | |
| } | |
| ?> | |
| </select> | |
| </span> | |
| </div> | |
| <?php | |
| } | |
| } | |
| function inspiry_render_search_fields() { | |
| $meta_box_array = $this->meta_box_data; | |
| if ( $meta_box_array ) { | |
| foreach ( $meta_box_array as $meta_box ) { | |
| //build basic info array | |
| $basic = [ | |
| $meta_box['meta_type'], | |
| $meta_box['meta_name'], | |
| $this->inspiry_generate_meta_box_key( $meta_box['meta_name'] ) | |
| ]; | |
| $searched = isset( $_GET[$basic[2]] ) ? $_GET[$basic[2]] : ''; | |
| if ( in_array( 'search', $meta_box['meta_field_display'] ) ) { | |
| if ( 'select' === $meta_box['meta_type'] ) { | |
| $classes = [ | |
| 'rh_prop_search__option rh_prop_search__select', | |
| 'rh_prop_search__selectwrap', | |
| 'rh_select2' | |
| ]; | |
| $options = $this->inspiry_build_meta_box_options( $meta_box['meta_select_options'] ); | |
| $this->inspiry_render_text_select_fields( $basic, $classes, $searched, $options ); | |
| } else { | |
| $classes = [ | |
| 'rh_prop_search__option' | |
| ]; | |
| $this->inspiry_render_text_select_fields( $basic, $classes, $searched ); | |
| } | |
| } | |
| } | |
| }; | |
| } | |
| function inspiry_render_submit_fields(){ | |
| $meta_box_array = $this->meta_box_data; | |
| if($meta_box_array){ | |
| echo '<div class="rh_form__row">'; | |
| foreach ($meta_box_array as $meta_box){ | |
| //build basic info array | |
| $basic = [ | |
| $meta_box['meta_type'], | |
| $meta_box['meta_name'], | |
| $this->inspiry_generate_meta_box_key( $meta_box['meta_name'] ) | |
| ]; | |
| if ( inspiry_is_edit_property() ) { | |
| global $post_meta_data; | |
| if ( isset( $post_meta_data[$basic[2]] ) ) { | |
| $searched = esc_attr( $post_meta_data[$basic[2]][0] ); | |
| } | |
| } | |
| if(in_array('submit', $meta_box['meta_field_display'])) { | |
| if( 'select' === $meta_box['meta_type'] ){ | |
| $classes = [ | |
| 'rh_form__item rh_form--3-column rh_form--columnAlign', | |
| 'selectwrap', | |
| 'rh_select2' | |
| ]; | |
| $options = $this->inspiry_build_meta_box_options( $meta_box['meta_select_options'] ); | |
| $this->inspiry_render_text_select_fields( $basic, $classes, $searched, $options ); | |
| } else { | |
| $classes = [ | |
| 'rh_form__item rh_form--3-column rh_form--columnAlign' | |
| ]; | |
| $this->inspiry_render_text_select_fields( $basic, $classes, $searched ); | |
| } | |
| } | |
| } | |
| echo '</div>'; | |
| }; | |
| } | |
| function inspiry_submit_fields_database($id){ | |
| $meta_box_array = $this->meta_box_data; | |
| if($meta_box_array){ | |
| foreach ($meta_box_array as $meta_box){ | |
| if(in_array('submit', $meta_box['meta_field_display'])) { | |
| $key = $this->inspiry_generate_meta_box_key($meta_box['meta_name']); | |
| if ( isset( $_POST[$key] ) && ! empty( $_POST[$key] ) ) { | |
| if('text' === $meta_box['meta_type']){ | |
| update_post_meta( $id, $key, sanitize_text_field($_POST[$key]) ); | |
| }else{ | |
| update_post_meta( $id, $key, $_POST[$key] ); | |
| } | |
| } else { | |
| delete_post_meta( $id, $key ); | |
| } | |
| } | |
| } | |
| }; | |
| } | |
| function inspiry_property_single_meta_fields(){ | |
| $meta_box_array = $this->meta_box_data; | |
| if($meta_box_array){ | |
| foreach ($meta_box_array as $meta_box){ | |
| if(in_array('single', $meta_box['meta_field_display'])) { | |
| $key = $this->inspiry_generate_meta_box_key($meta_box['meta_name']); | |
| $value = get_post_meta( get_the_ID(), $key, true ); | |
| $class = is_singular('property') ? 'rh_property__meta' : 'rh_prop_card__meta'; | |
| $color = is_singular('property') ? '#1ea69a' : '#b3b3b3'; | |
| if ( ! empty( $value ) ) : ?> | |
| <div class="<?php echo esc_attr($class); ?>"> | |
| <span class="rh_meta_titles"> | |
| <?php echo $meta_box['meta_name']; ?> | |
| </span> | |
| <div> | |
| <?php if (!empty ($meta_box['meta_icon'] ) ) : ?> | |
| <i style="font-size: 30px; color: <?php echo $color; ?>; vertical-align: bottom; line-height: 23px;" class=" <?php echo $meta_box['meta_icon']; ?>" aria-hidden="true"></i> | |
| <?php endif; ?> | |
| <span class="figure"> | |
| <?php echo get_post_meta( get_the_ID(), $key, true ); ?> | |
| </span> | |
| </div> | |
| </div> | |
| <!-- /.rh_property__meta --> | |
| <?php endif; | |
| } | |
| } | |
| }; | |
| } | |
| function inspiry_enqueue_my_scripts($hook) { | |
| if ( 'real-homes_page_inspiry_additional_meta_boxes_page' != $hook ) { | |
| return; | |
| } | |
| ?> | |
| <script> | |
| window.addEventListener('DOMContentLoaded', (event) => { | |
| (function ($) { | |
| $(document).find('.logical_select').each(function (index, value) { | |
| var selectedVal = $(this).find('select').children("option:selected").val(); | |
| if('select' === selectedVal){ | |
| $(this).next('.logical_show_hide').show(); | |
| }else{ | |
| $(this).next('.logical_show_hide').hide(); | |
| } | |
| }); | |
| $(document).on('change', '.logical_select', function (e) { | |
| var selectedVal = $(this).find('select').children("option:selected").val(); | |
| if('select' === selectedVal){ | |
| $(this).next('.logical_show_hide').show(); | |
| }else{ | |
| $(this).next('.logical_show_hide').hide(); | |
| } | |
| }); | |
| })(jQuery); | |
| }); | |
| </script> | |
| <?php | |
| } | |
| } | |
| /** | |
| * Initialize ERE admin menu class. | |
| */ | |
| function inspiry_fields_builder_initialize() { | |
| return Inspiry_Additional_Meta_Fields_Builder::instance(); | |
| } | |
| inspiry_fields_builder_initialize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment