8889841ccmb-field-taxonomy-location.php000064400000021555150513303240012565 0ustar00setup_admin_scripts(); if ( version_compare( CMB2_VERSION, '2.2.2', '>=' ) ) { $field_type_object->type = new CMB2_Type_Select( $field_type_object ); } $nb_fields = apply_filters('wp_job_board_pro_cmb2_field_taxonomy_location_number', 4); $parent = 0; echo '
'; for ($i=1; $i <= $nb_fields; $i++) { $taxonomy_options = $this->get_taxonomy_options( $field_escaped_value, $field_type_object, $parent ); $parent = !empty($taxonomy_options['parent']) ? $taxonomy_options['parent'] : 'no'; $label = ''; if ( $i == 1 ) { $label = esc_html__('Country', 'wp-job-board-pro'); } elseif ( $i == 2 ) { $label = esc_html__('State', 'wp-job-board-pro'); } elseif ( $i == 3 ) { $label = esc_html__('City', 'wp-job-board-pro'); } elseif ( $i == 4 ) { $label = esc_html__('District', 'wp-job-board-pro'); } $field_name = apply_filters('wp_job_board_pro_cmb2_field_taxonomy_location_field_name_'.$i, $label); $placeholder = $field->args( 'attributes', 'placeholder' ) ? $field->args( 'attributes', 'placeholder' ) : $field_name; $placeholder = sprintf($placeholder, $field_name); $a = $field_type_object->parse_args( 'wpjb_taxonomy_location', array( 'style' => 'width: 99%', 'class' => 'wpjb_taxonomy_location wpjb_taxonomy_location'.$i, 'name' => $field_type_object->_name() . '[]', 'id' => $field_type_object->_id().$i, 'desc' => $field_type_object->_desc( true ), 'options' => $taxonomy_options['option'], 'data-placeholder' => $placeholder, 'data-next' => ($i + 1), 'data-taxonomy' => $field_type_object->field->args( 'taxonomy' ), 'data-allowclear' => true ) ); $attrs = $field_type_object->concat_attrs( $a, array( 'desc', 'options' ) ); echo sprintf( '
%s
', $i, $attrs, $a['options'] ); } echo '
'; if ( !empty($a['desc']) ) { echo $a['desc']; } } public function get_taxonomy_options( $field_escaped_value, $field_type_object, $parent ) { $options = (array) $this->get_terms($field_type_object->field->args( 'taxonomy' ), array('parent' => $parent)); $field_escaped_value = $this->options_terms($field_type_object->field); // if ( ! empty( $field_escaped_value ) ) { // if ( !is_array($field_escaped_value) ) { // $field_escaped_value = array($field_escaped_value); // } // $options = $this->sort_array_by_array( $options, $field_escaped_value ); // } $return = array(); $selected_items = ''; $other_items = ''; foreach ( $options as $option_value => $option_label ) { // Clone args & modify for just this item $option = array( 'value' => $option_value, 'label' => $option_label, ); // Split options into those which are selected and the rest if ( in_array( $option_value, (array) $field_escaped_value ) ) { $return['parent'] = $option_value; $option['checked'] = true; $selected_items .= $field_type_object->select_option( $option ); } else { $other_items .= $field_type_object->select_option( $option ); } } $return['option'] = $selected_items . $other_items; return $return; } public function options_terms($field) { if ( empty($field->data_args()['id']) ) { return array(); } $object_id = $field->data_args()['id']; $terms = get_the_terms( $object_id, $field->args( 'taxonomy' ) ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ( $terms as $index => $term ) { $terms[ $index ] = $term->term_id; } } return $terms; } public function sort_array_by_array( array $array, array $orderArray ) { $ordered = array(); foreach ( $orderArray as $key ) { if ( array_key_exists( $key, $array ) ) { $ordered[ $key ] = $array[ $key ]; unset( $array[ $key ] ); } } return $ordered + $array; } /** * Handle sanitization for repeatable fields */ public function sanitize( $check, $meta_value, $object_id, $field_args ) { if ( empty($meta_value) || !is_array( $meta_value ) ) { return $check; } if ( $field_args['repeatable'] ) { foreach ( $meta_value as $key => $val ) { $meta_value[$key] = array_map( 'absint', $val ); wp_set_object_terms( $object_id, array_map( 'absint', $val ), $field_args['taxonomy'], false ); } } else { $meta_value = array_map( 'absint', $meta_value ); wp_set_object_terms( $object_id, $meta_value, $field_args['taxonomy'], false ); } return $meta_value; } /** * Handle escaping for repeatable fields */ public function escaped_value( $check, $meta_value, $field_args ) { if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) { return $check; } foreach ( $meta_value as $key => $val ) { $meta_value[$key] = array_map( 'esc_attr', $val ); } return $meta_value; } /** * Add 'table-layout' class to multi-value select field */ public function table_row_class( $check ) { $check[] = 'wpjb_taxonomy_location'; return $check; } /** * Enqueue scripts and styles */ public function setup_admin_scripts() { $asset_path = apply_filters( 'wpjb_cmb2_field_select2_asset_path', plugins_url( '', __FILE__ ) ); wp_enqueue_script( 'wpjb-taxonomy-location-script', $asset_path . '/js/script.js', array( 'cmb2-scripts', 'wpjbp-select2', 'jquery-ui-sortable' ), self::VERSION ); wp_localize_script( 'wpjb-taxonomy-location-script', 'location_opts', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'ajaxurl_endpoint' => WP_Job_Board_Pro_Ajax::get_endpoint(), 'ajax_nonce' => wp_create_nonce( 'wpjb-ajax-nonce' ) )); wp_enqueue_style( 'wpjb-taxonomy-location-style', $asset_path . '/css/style.css', array( 'wpjbp-select2' ), self::VERSION ); } public function get_terms($taxonomy, $query_args = array()) { $return = array(); $defaults = array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', 'hierarchical' => 1, 'lang' => apply_filters( 'wp-job-board-pro-current-lang', null ) ); $args = wp_parse_args( $query_args, $defaults ); $terms_hash = 'wjbp_cats_' . md5( wp_json_encode( $args ) . WP_Job_Board_Pro_Cache_Helper::get_transient_version('wjbp_get_' . $taxonomy) ); $terms = get_transient( $terms_hash ); if ( empty( $terms ) ) { $terms = get_terms( $taxonomy, $args ); set_transient( $terms_hash, $terms, DAY_IN_SECONDS * 7 ); } if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ( $terms as $key => $term ) { $return[$term->term_id] = $term->name; } } return $return; } public function process_change_location() { check_ajax_referer( 'wpjb-ajax-nonce', 'security' ); $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : ''; $parent = !empty($_POST['parent']) ? $_POST['parent'] : ''; $hide_empty = isset($_POST['hide_empty']) ? $_POST['hide_empty'] : false; $options = array( array('id' => '', 'name' => '')); if ( $parent ) { $args = array( 'hide_empty' => $hide_empty, 'orderby' => 'name', 'order' => 'ASC', 'hierarchical' => 1, 'parent' => $parent ); $terms = get_terms( $taxonomy, $args ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ( $terms as $key => $term ) { $options[] = array('id' => $term->term_id, 'name' => $term->name); } } } echo json_encode($options); wp_die(); } } $wpjb_cmb2_field_select2 = new WP_Job_Board_Pro_CMB2_Field_Taxonomy_Location(); css/loading.gif000064400000010640150513303240007435 0ustar00GIF89a((<>"vaEqJuV yWwD'_KEM byt_K!Z$f}J_R%$WK x J xG'J If$^C $_bu{H9d4D>$DhgD !$jKBC*;Aa@c" `趁H&|@0e @ c RRNFMDD2Hp"M40+|. BL\R#ڜW*0djhܖ&pPf""%ET|@٦XbA7DKd"BK QnA>r5bD! ),((<>6QlMqeBbqB [E T(X}( q&L%C#D#wcF G(c!L~`c J qf  Ut#K#zf$$ 'w'E Zw#u[2B4=0`2 * { position: relative; } .field-taxonomy-location-wrapper .loading::before { background: rgba(255, 255, 255, 0.5) url("loading.gif") no-repeat center center/25px auto; position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: 9; width: 100%; height: 100%; content: ''; }js/script.js000064400000003735150513303240007026 0ustar00(function ($) { 'use strict'; $('.wpjb_taxonomy_location').each(function () { var allowclear = $(this).data('allowclear'); var width = $(this).data('width') ? $(this).data('width') : '100%'; $(this).select2({ allowClear: allowclear, width: width }); }); // Location Change $('body').on('change', 'select.wpjb_taxonomy_location', function(){ var val = $(this).val(); var next = $(this).data('next'); var main_select = 'select.wpjb_taxonomy_location' + next; if ( $(main_select).length > 0 ) { $(main_select).prop('disabled', true); $(main_select).val('').trigger('change'); if ( val ) { $(main_select).parent().addClass('loading'); $.ajax({ url: location_opts.ajaxurl, type:'POST', dataType: 'json', data:{ 'action': 'wpjb_process_change_location', 'parent': val, 'taxonomy': $(main_select).data('taxonomy'), 'security': location_opts.ajax_nonce, } }).done(function(data) { $(main_select).parent().removeClass('loading'); $(main_select).find('option').remove(); if ( data ) { $.each(data, function(i, item) { var option = new Option(item.name, item.id, true, true); $(main_select).append(option); }); } $(main_select).prop("disabled", false); $(main_select).val(null).select2("destroy").select2(); }); } else { $(main_select).find('option').remove(); $(main_select).prop("disabled", false); $(main_select).val(null).select2("destroy").select2(); } } }); })(jQuery);