# customify/1.6.0/features/customizer/controls/class-Pix_Customize_Preset_Control.php

Customify, version 1.6.0. 395 lines.

- Page: https://pluginprobe.com/plugins/customify/1.6.0/code/features/customizer/controls/class-Pix_Customize_Preset_Control.php
- Raw: https://pluginprobe.com/plugins/customify/1.6.0/raw/features/customizer/controls/class-Pix_Customize_Preset_Control.php
- Modified: 2018-04-05T11:47:40+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/customify/1.6.0/code/features/customizer/controls/class-Pix_Customize_Preset_Control.php#L10-L20`.

```php
<?php

/**
 * Class Pix_Customize_Preset_Control
 * A simple Select2 Control
 */
class Pix_Customize_Preset_Control extends Pix_Customize_Control {
	public $type = 'preset';
	public $choices_type = 'select';
	public $description = null;

	/**
	 * Render the control's content.
	 *
	 * @since 3.4.0
	 */
	public function render_content() {

		switch ( $this->choices_type ) {

			case 'select' : { ?>
			<label>
				<?php if ( ! empty( $this->label ) ) { ?>
					<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
				<?php }
				if ( ! empty( $this->description ) ) { ?>
					<span class="description customize-control-description"><?php echo $this->description; ?></span>
				<?php } ?>

				<select <?php $this->link(); ?> class="customify_preset select">
					<?php
					foreach ( $this->choices as $choice_value => $choice_config ){
						if ( ! isset( $choice_config['options']) || ! isset( $choice_config['label'] ) ) {
							continue;
						}
						$label = $choice_config['label'];
						$options = $this->convertChoiceOptionsIdsToSettingIds( $choice_config['options'] );
						$data = ' data-options=\'' . json_encode($options) . '\'';
						echo '<option value="' . esc_attr( $choice_value ) . '" ' . selected( $this->value(), $choice_value, false ) . $data . ' >' . $label . '</option>';
					} ?>
				</select>
			</label>
			<?php break;
			}

			case 'radio' : { ?>
				<label>
					<?php if ( ! empty( $this->label ) ) { ?>
						<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
					<?php }
					if ( ! empty( $this->description ) ) { ?>
						<span class="description customize-control-description"><?php echo $this->description; ?></span>
					<?php } ?>

					<div class="customify_preset radio customize-control customize-control-radio">
						<?php
						foreach ( $this->choices as $choice_value => $choice_config ){
							if ( ! isset( $choice_config['options']) || ! isset( $choice_config['label'] ) ) {
								continue;
							}
							$color = '';
							if ( isset( $choice_config['color'] ) ) {
								$color .= ' style="background-color: ' . $choice_config['color'] . '"';
							}

							$label = $choice_config['label'];
							$options = $this->convertChoiceOptionsIdsToSettingIds( $choice_config['options'] );
							$data = ' data-options=\'' . json_encode( $options ) . '\''; ?>

							<span class="customize-inside-control-row">
								<input <?php $this->link(); echo 'name="' . $this->setting->id . '" id="' . esc_attr( $choice_value ) . '" type="radio" value="' . esc_attr( $choice_value ) . '" ' . selected( $this->value(), $choice_value, false ) . $data . $color .' />'; ?>
								<label for="<?php echo esc_attr( $choice_value ); ?>">
									<?php echo $label; ?>
								</label>
							</span>
						<?php } ?>
					</div>
				</label>
			<?php break;
			}

			case 'buttons' : { ?>
				<label>
					<?php if ( ! empty( $this->label ) ) { ?>
						<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
					<?php }
					if ( ! empty( $this->description ) ) { ?>
						<span class="description customize-control-description"><?php echo $this->description; ?></span>
					<?php } ?>

					<div class="customify_preset radio_buttons">
						<?php
						foreach ( $this->choices as $choice_value => $choice_config ){
							if ( ! isset( $choice_config['options']) || ! isset( $choice_config['label'] ) ) {
								continue;
							}
							$color = '';
							if ( isset( $choice_config['color'] ) ) {
								$color .= ' style="border-left-color: ' . $choice_config['color'] . '; color: ' . $choice_config['color'] . ';"';
							}

							$label = $choice_config['label'];
							$options = $this->convertChoiceOptionsIdsToSettingIds( $choice_config['options'] );
							$data = ' data-options=\'' . json_encode($options) . '\'';?>

							<fieldset class="customify_radio_button">
								<input <?php $this->link(); echo 'name="' .  $this->setting->id . '" type="radio" value="' . esc_attr( $choice_value ) . '" ' . selected( $this->value(), $choice_value, false ) . $data .' />'; ?>
								<label class="button" for="<?php echo $this->setting->id; ?>" <?php echo $color; ?>>
									<?php echo $label; ?>
								</label>
							</fieldset>
						<?php } ?>
					</div>
				</label>
				<?php break;
			}

			case 'color_palette' : { ?>
				<label>
					<?php if ( ! empty( $this->label ) ) { ?>
						<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
					<?php }

					if ( ! empty( $this->description ) ) { ?>
						<span class="description customize-control-description"><?php echo $this->description; ?></span>
					<?php } ?>

					<div class="customify_preset color_palette customize-control customize-control-radio">
						<?php
						foreach ( $this->choices as $choice_value => $choice_config ){
							if ( empty( $choice_config['options'] ) ) {
								continue;
							}

							// Make sure that the defaults are in place
							$choice_config = wp_parse_args( $choice_config, array(
								'label' => '',
								'preview' => array(),
							) );

							// Make sure that the preview defaults are in place
							$choice_config['preview'] = wp_parse_args( $choice_config['preview'], array(
								'sample_letter' => 'A',
								'background_image_url' => plugins_url( 'images/color_palette_image.jpg', PixCustomifyPlugin()->file ),
							) );

							// Determine a (primary) color with fallback for missing options
							$sm_color = '#777777';
							if ( isset( $choice_config['options']['sm_color_primary'] ) ) {
								$sm_color = $choice_config['options']['sm_color_primary'];
							} elseif ( isset( $choice_config['options']['sm_color_secondary'] ) ) {
								$sm_color = $choice_config['options']['sm_color_secondary'];
							} elseif ( isset( $choice_config['options']['sm_color_tertiary'] ) ) {
								$sm_color = $choice_config['options']['sm_color_tertiary'];
							} elseif ( isset( $choice_config['options']['sm_color_quaternary'] ) ) {
								$sm_color = $choice_config['options']['sm_color_quaternary'];
							} elseif ( isset( $choice_config['options']['sm_color_quinary'] ) ) {
								$sm_color = $choice_config['options']['sm_color_quinary'];
							}

							// Determine a (primary) dark color with fallback for missing options
							$sm_dark = '#000000';
							if ( isset( $choice_config['options']['sm_dark_primary'] ) ) {
								$sm_dark = $choice_config['options']['sm_dark_primary'];
							} elseif ( isset( $choice_config['options']['sm_dark_secondary'] ) ) {
								$sm_dark = $choice_config['options']['sm_dark_secondary'];
							} elseif ( isset( $choice_config['options']['sm_dark_tertiary'] ) ) {
								$sm_dark = $choice_config['options']['sm_dark_tertiary'];
							} elseif ( isset( $choice_config['options']['sm_dark_quaternary'] ) ) {
								$sm_dark = $choice_config['options']['sm_dark_quaternary'];
							} elseif ( isset( $choice_config['options']['sm_dark_quinary'] ) ) {
								$sm_dark = $choice_config['options']['sm_dark_quinary'];
							}

							// Determine a (primary) light color with fallback for missing options
							$sm_light = '#FFFFFF';
							if ( isset( $choice_config['options']['sm_light_primary'] ) ) {
								$sm_light = $choice_config['options']['sm_light_primary'];
							} elseif ( isset( $choice_config['options']['sm_light_secondary'] ) ) {
								$sm_light = $choice_config['options']['sm_light_secondary'];
							} elseif ( isset( $choice_config['options']['sm_light_tertiary'] ) ) {
								$sm_light = $choice_config['options']['sm_light_tertiary'];
							} elseif ( isset( $choice_config['options']['sm_light_quaternary'] ) ) {
								$sm_light = $choice_config['options']['sm_light_quaternary'];
							} elseif ( isset( $choice_config['options']['sm_light_quinary'] ) ) {
								$sm_light = $choice_config['options']['sm_light_quinary'];
							}

							$label = $choice_config['label'];
							$options = $this->convertChoiceOptionsIdsToSettingIds( $choice_config['options'] );
							$data = ' data-options=\'' . json_encode( $options ) . '\''; ?>

							<span class="customize-inside-control-row <?php echo ( (string) $this->value() === (string) $choice_value ? 'current-color-palette' : '' );?>" style="background-image: url( <?php echo esc_url( $choice_config['preview']['background_image_url'] ); ?> );">
								<input <?php $this->link(); echo 'name="' . $this->setting->id . '" id="' . esc_attr( $choice_value ) . '" type="radio" value="' . esc_attr( $choice_value ) . '" ' . selected( $this->value(), $choice_value, false ) . $data .' />'; ?>
								<label for="<?php echo esc_attr( $choice_value ); ?>">
									<span class="label__inner" style="color: <?php echo esc_attr( $this->lightOrDark( $sm_light ) ); ?>; background: <?php echo esc_attr( $sm_light ); ?>;"><span class="preview__letter" style="background: <?php echo $sm_color; ?>"><?php echo $choice_config['preview']['sample_letter']; ?></span><?php echo esc_html( $label ); ?></span>
								</label>
								<div class="palette">
									<?php foreach ( $choice_config['options'] as $color_name => $color_value ) {
										echo '<div class="palette__item ' . esc_attr( $color_name ) . '" style="background: ' . esc_attr( $color_value ) . '"></div>' . PHP_EOL;
									} ?>
								</div>
							</span>
						<?php } ?>
					</div>
				</label>

				<?php break;
			}

			case 'awesome' : { ?>
				<label>
					<?php if ( ! empty( $this->label ) ) { ?>
						<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
					<?php } ?>

					<div class="customify_preset awesome_presets">
						<?php

						$google_links = array();

						foreach ( $this->choices as $choice_value => $choice_config ){
							if ( ! isset( $choice_config['options']) || ! isset( $choice_config['label'] ) ) {
								continue;
							}

							$preset_style = ' style="background-color: #444;';
							$preset_name_style = ' style=" color: #000; background-color: #ccc; border-color: #aaa;';
							$preset_text_color = ' style=" color: #ebebeb';

							$first_font = $second_font = '';
							if ( isset( $choice_config['preview'] ) ) {

								if ( isset( $choice_config['preview']['background-card'] ) ) {
									$preset_style = ' style="';
									$preset_style .= 'background-color: ' .  $choice_config['preview']['background-card'] . ';';
								}

								if ( isset( $choice_config['preview']['background-label'] ) ) {

									$this_preset_color = $choice_config['preview']['background-label'];

									if ( $this->isLight( $this_preset_color ) ) {
										$this_preset_color = '#000000';
									} else {
										$this_preset_color = '#ffffff';
									}
									$preset_name_style = ' style="';
									$preset_name_style .= 'color: ' .$this_preset_color . ';background-color: ' .  $choice_config['preview']['background-label'] . '; border-color: ' .  $choice_config['preview']['background-label'];
								}

								if ( isset( $choice_config['preview']['color-text'] ) ) {
									$preset_text_color = ' style="';
									$preset_text_color .= 'color: ' .  $choice_config['preview']['color-text'] . ';"';
								}

								if ( isset( $choice_config['preview']['font-main'] ) ) {
									$first_font = ' style="font-family: ' . $choice_config['preview']['font-main'] . '"' ;
									$google_links[] = str_replace( ' ', '+', $choice_config['preview']['font-main'] );
								}

								if ( isset( $choice_config['preview']['font-alt'] ) ) {
									$second_font = ' style="font-family: ' . $choice_config['preview']['font-alt'] . '"' ;
									$google_links[] = str_replace( ' ', '+', $choice_config['preview']['font-alt'] );
								}
							}

							$preset_style .= '"';
							$preset_text_color .= '"';
							$preset_name_style .= '"';

							$label = $choice_config['label'];
							$options = $this->convertChoiceOptionsIdsToSettingIds( $choice_config['options'] );
							$data = ' data-options=\'' . json_encode($options) . '\'';?>
							<div class="awesome_preset" <?php echo $preset_text_color; ?>>
								<input <?php $this->link(); echo 'name="' .  $this->setting->id . '" type="radio" value="' . esc_attr( $choice_value ) . '" ' . selected( $this->value(), $choice_value, false ) . $data .' >'  . '</input>'; ?>
								<div class="preset-wrap">
                                    <div class="preset-color" <?php echo $preset_style; ?>>
                                        <span class="first-font" <?php echo $first_font; ?>><?php echo substr( get_bloginfo('name'), 0, 2); ?></span>
                                        <span class="secondary-font" <?php echo $second_font; ?>>AaBbCc</span>
                                    </div>
                                    <div class="preset-name" <?php echo $preset_name_style; ?>>
                                        <?php echo $label; ?>
                                    </div>
                                </div>
							</div>
						<?php }

						// ok now we have our preview fonts, let's ask them from google
						// note that we request only these chars "AaBbCc" so it should be a small request
						echo '<link href="//fonts.googleapis.com/css?family=' . implode('|', $google_links ) . '&text=AaBbCc' . substr( get_bloginfo('name'), 0, 2) . '" rel=\'stylesheet\' type=\'text/css\'>';?>
					</div>

					<?php
					if ( ! empty( $this->description ) ) { ?>
						<span class="description customize-control-description"><?php echo $this->description; ?></span>
					<?php } ?>
				</label>
				<?php break;
			}

			default:
				break;
		}
	}

	/**
	 * Returns whether or not given color is considered "light"
	 * @param string|Boolean $color
	 * @return boolean
	 */
	public function isLight( $color = FALSE ){
		// Get our color
		$color = ($color) ? $color : $this->_hex;
		// Calculate straight from rbg
		$r = hexdec($color[0].$color[1]);
		$g = hexdec($color[2].$color[3]);
		$b = hexdec($color[4].$color[5]);
		return (( $r*299 + $g*587 + $b*114 )/1000 > 130);
	}

	/**
	 * Detect if we should use a light or dark color on a background color.
	 *
	 * Taken from WooCommerce: woocommerce/includes/wc-formatting-functions.php
	 * @link http://woocommerce.wp-a2z.org/oik_api/wc_light_or_dark/
	 *
	 * @param mixed  $color Color.
	 * @param string $dark  Darkest reference.
	 *                      Defaults to '#000000'.
	 * @param string $light Lightest reference.
	 *                      Defaults to '#FFFFFF'.
	 * @return string
	 */
	function lightOrDark( $color, $dark = '#000000', $light = '#FFFFFF' ) {

		$hex = str_replace( '#', '', $color );

		$c_r = hexdec( substr( $hex, 0, 2 ) );
		$c_g = hexdec( substr( $hex, 2, 2 ) );
		$c_b = hexdec( substr( $hex, 4, 2 ) );

		$brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;

		return $brightness > 180 ? $dark : $light;
	}

	/**
	 * We will receive the choice options IDs as defined in the Customify config (the options array keys) and we will convert them to actual setting IDs.
	 *
	 * @param array $options
	 *
	 * @return array
	 */
	protected function convertChoiceOptionsIdsToSettingIds( $options ) {
		$settings = array();

		if ( empty( $options ) ) {
			return $settings;
		}

		$localPlugin = PixCustomifyPlugin();
		$customizer_config = $localPlugin->get_customizer_config();

		// first check the very needed options name
		if ( empty( $customizer_config['opt-name'] ) ) {
			return $settings;
		}
		$options_name = $customizer_config['opt-name'];

		// Coerce a single string into an array and treat it as a field ID.
		if ( is_string( $options ) ) {
			$options = array( $options );
		}

		foreach ( $options as $option_id => $option_value ) {
			$option_config = $localPlugin->get_option_customizer_config( $option_id );
			if ( empty( $option_config ) ) {
				continue;
			}

			// If we have been explicitly given a setting ID we will use that
			if ( ! empty( $option_config['setting_id'] ) ) {
				$setting_id = $option_config['setting_id'];
			} else {
				$setting_id = $options_name . '[' . $option_id . ']';
			}

			$settings[ $setting_id ] = $option_value;
		}

		return $settings;
	}
}

```
