ru-se.com

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

class-kirki-explode-background-field.php (6633B)


      1 <?php
      2 /**
      3  * Background fields are just lots of sub-fields combined.
      4  * We have to actually separate the field to its sub-parts
      5  * and register each one of them separately.
      6  *
      7  * @package     Kirki
      8  * @category    Core
      9  * @author      Aristeides Stathopoulos
     10  * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
     11  * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
     12  * @since       1.0
     13  */
     14 
     15 if ( ! class_exists( 'Kirki_Explode_Background_Field' ) ) {
     16 
     17 	/**
     18 	 * Explodes background fields and creates sub-fields for it.
     19 	 */
     20 	class Kirki_Explode_Background_Field {
     21 
     22 		/**
     23 		 * Build the background fields.
     24 		 * Takes a single field with type = background and explodes it to multiple controls.
     25 		 *
     26 		 * @param array $field The field arguments.
     27 		 * @return null|array
     28 		 */
     29 		public static function explode( $field ) {
     30 			$i18n    = Kirki_l10n::get_strings( $field['kirki_config'] );
     31 			$choices = self::background_choices();
     32 
     33 			// Early exit if this is not a background field.
     34 			if ( 'background' !== $field['type'] ) {
     35 				return;
     36 			}
     37 
     38 			// No need to proceed any further if no defaults have been set.
     39 			// We build the array of fields based on what default values have been defined.
     40 			if ( ! isset( $field['default'] ) || ! is_array( $field['default'] ) ) {
     41 				return;
     42 			}
     43 
     44 			$fields = array();
     45 			$i      = 0;
     46 			foreach ( $field['default'] as $key => $value ) {
     47 
     48 				// No need to process the opacity, it is factored in the color control.
     49 				if ( 'opacity' === $key ) {
     50 					continue;
     51 				}
     52 
     53 				$key               = esc_attr( $key );
     54 				$setting           = $key;
     55 				$tooltip           = $field['tooltip'];
     56 				$description       = isset( $i18n[ 'background-' . $key ] ) ? $i18n[ 'background-' . $key ] : '';
     57 				$output_property   = 'background-' . $key;
     58 				$label             = ( 0 === $i ) ? $field['label'] : '';
     59 				$type              = 'select';
     60 				$sanitize_callback = 'esc_attr';
     61 
     62 				switch ( $key ) {
     63 					case 'color':
     64 						// Use 'color-alpha' instead of 'color' if default is an rgba value or if 'opacity' is set.
     65 						$type = ( false !== strpos( $field['default']['color'], 'rgba' ) ) ? 'color-alpha' : 'color';
     66 						$type = ( isset( $field['default']['opacity'] ) ) ? 'color-alpha' : $type;
     67 						if ( isset( $field['default']['opacity'] ) && false === strpos( $value, 'rgb' ) ) {
     68 							$value = Kirki_Color::get_rgba( $value, $field['default']['opacity'] );
     69 						}
     70 						$sanitize_callback = array( 'Kirki_Sanitize_Values', 'color' );
     71 						break;
     72 					case 'image':
     73 						$type = 'image';
     74 						$sanitize_callback = 'esc_url_raw';
     75 						break;
     76 					case 'attach':
     77 						// Small hack so that background attachments properly work.
     78 						$output_property = 'background-attachment';
     79 						$description     = $i18n['background-attachment'];
     80 						break;
     81 					default:
     82 						$tooltip = '';
     83 						break;
     84 				}
     85 
     86 				// If we're using options & option_name is set, then we need to modify the setting.
     87 				if ( ( isset( $field['option_type'] ) && 'option' === $field['option_type'] && isset( $field['option_name'] ) ) && ! empty( $field['option_name'] ) ) {
     88 					$property_setting = str_replace( ']', '', str_replace( $field['option_name'] . '[', '', $field['settings'] ) );
     89 					$property_setting = esc_attr( $field['option_name'] ) . '[' . esc_attr( $property_setting ) . '_' . $setting . ']';
     90 				} else {
     91 					$property_setting = esc_attr( $field['settings'] ) . '_' . $setting;
     92 				}
     93 
     94 				// Build the field's output element.
     95 				$output_element = $field['output'];
     96 				if ( ! empty( $field['output'] ) ) {
     97 					if ( is_array( $field['output'] ) ) {
     98 						if ( isset( $field['output']['element'] ) ) {
     99 							$output_element = $field['output']['element'];
    100 						} elseif ( isset( $field['output'][0] ) && isset( $field['output'][0]['element'] ) ) {
    101 							$output_element = $field['output'][0]['element'];
    102 						}
    103 					}
    104 				}
    105 
    106 				/**
    107 				 * Build the field.
    108 				 * We're merging with the original field here, so any extra properties are inherited.
    109 				 */
    110 				$fields[ $property_setting ] = array_merge( $field, array(
    111 					'type'              => $type,
    112 					'label'             => $label,
    113 					'settings'          => $property_setting,
    114 					'tooltip'           => $tooltip,
    115 					'section'           => $field['section'],
    116 					'priority'          => $field['priority'],
    117 					'required'          => $field['required'],
    118 					'description'       => $description,
    119 					'default'           => $value,
    120 					'id'                => sanitize_key( str_replace( '[', '-', str_replace( ']', '', $property_setting ) ) ),
    121 					'choices'           => isset( $choices[ $key ] ) ? $choices[ $key ] : array(),
    122 					'sanitize_callback' => $sanitize_callback,
    123 					'output'            => ( ! empty( $field['output'] ) ) ? array(
    124 						array(
    125 							'element'  => $output_element,
    126 							'property' => $output_property,
    127 						),
    128 					) : array(),
    129 				) );
    130 				$i++;
    131 			}
    132 
    133 			return $fields;
    134 
    135 		}
    136 
    137 		/**
    138 		 * Parse all fields and add the new background fields
    139 		 *
    140 		 * @param 	array $fields An array of all the generated fields.
    141 		 * @return  array
    142 		 */
    143 		public static function process_fields( $fields ) {
    144 
    145 			foreach ( $fields as $field ) {
    146 				/**
    147 				 * Make sure background fields are exploded
    148 				 */
    149 				if ( isset( $field['type'] ) && 'background' === $field['type'] ) {
    150 					$explode = self::explode( $field );
    151 					$fields  = array_merge( $fields, $explode );
    152 				}
    153 			}
    154 
    155 			return $fields;
    156 
    157 		}
    158 
    159 
    160 		/**
    161 		 * The background choices.
    162 		 *
    163 		 * @return array<string,array>
    164 		 */
    165 		public static function background_choices() {
    166 
    167 			$i18n = Kirki_l10n::get_strings();
    168 
    169 			return array(
    170 				'repeat'        => array(
    171 					'no-repeat' => $i18n['no-repeat'],
    172 					'repeat'    => $i18n['repeat-all'],
    173 					'repeat-x'  => $i18n['repeat-x'],
    174 					'repeat-y'  => $i18n['repeat-y'],
    175 					'inherit'   => $i18n['inherit'],
    176 				),
    177 				'size'        => array(
    178 					'inherit' => $i18n['inherit'],
    179 					'cover'   => $i18n['cover'],
    180 					'contain' => $i18n['contain'],
    181 				),
    182 				'attach'      => array(
    183 					'inherit' => $i18n['inherit'],
    184 					'fixed'   => $i18n['fixed'],
    185 					'scroll'  => $i18n['scroll'],
    186 				),
    187 				'position'          => array(
    188 					'left-top'      => $i18n['left-top'],
    189 					'left-center'   => $i18n['left-center'],
    190 					'left-bottom'   => $i18n['left-bottom'],
    191 					'right-top'     => $i18n['right-top'],
    192 					'right-center'  => $i18n['right-center'],
    193 					'right-bottom'  => $i18n['right-bottom'],
    194 					'center-top'    => $i18n['center-top'],
    195 					'center-center' => $i18n['center-center'],
    196 					'center-bottom' => $i18n['center-bottom'],
    197 				),
    198 			);
    199 
    200 		}
    201 	}
    202 }