Completed
Push — develop ( d8be36...adf00f )
by Aristeides
02:34
created

background_choices()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
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
/**
16
 * Explodes background fields and creates sub-fields for it.
17
 */
18
class Kirki_Explode_Background_Field {
19
20
	/**
21
	 * Build the background fields.
22
	 * Takes a single field with type = background and explodes it to multiple controls.
23
	 *
24
	 * @param array $field The field arguments.
25
	 * @return null|array
26
	 */
27
	public static function explode( $field ) {
28
		$l10n    = self::l10n( $field['kirki_config'] );
29
		$choices = self::background_choices();
30
31
		// Early exit if this is not a background field.
32
		if ( 'background' !== $field['type'] ) {
33
			return;
34
		}
35
36
		// No need to proceed any further if no defaults have been set.
37
		// We build the array of fields based on what default values have been defined.
38
		if ( ! isset( $field['default'] ) || ! is_array( $field['default'] ) ) {
39
			return;
40
		}
41
42
		$fields = array();
43
		$i      = 0;
44
		foreach ( $field['default'] as $key => $value ) {
45
46
			// No need to process the opacity, it is factored in the color control.
47
			if ( 'opacity' === $key ) {
48
				continue;
49
			}
50
51
			$key               = esc_attr( $key );
52
			$setting           = $key;
53
			$tooltip           = $field['tooltip'];
54
			$description       = isset( $l10n[ 'background-' . $key ] ) ? $l10n[ 'background-' . $key ] : '';
55
			$output_property   = 'background-' . $key;
56
			$label             = ( 0 === $i ) ? $field['label'] : '';
57
			$type              = 'select';
58
			$sanitize_callback = 'esc_attr';
59
60
			switch ( $key ) {
61
				case 'color':
62
					// Use 'color-alpha' instead of 'color' if default is an rgba value or if 'opacity' is set.
63
					$type = ( false !== strpos( $field['default']['color'], 'rgba' ) ) ? 'color-alpha' : 'color';
64
					$type = ( isset( $field['default']['opacity'] ) ) ? 'color-alpha' : $type;
65
					if ( isset( $field['default']['opacity'] ) && false === strpos( $value, 'rgb' ) ) {
66
						$value = Kirki_Color::get_rgba( $value, $field['default']['opacity'] );
67
					}
68
					$sanitize_callback = array( 'Kirki_Sanitize_Values', 'color' );
69
					break;
70
				case 'image':
71
					$type = 'image';
72
					$sanitize_callback = 'esc_url_raw';
73
					break;
74
				case 'attach':
75
					// Small hack so that background attachments properly work.
76
					$output_property = 'background-attachment';
77
					$description     = $l10n['background-attachment'];
78
					break;
79
				default:
80
					$tooltip = '';
81
					break;
82
			}
83
84
			// If we're using options & option_name is set, then we need to modify the setting.
85
			if ( ( isset( $field['option_type'] ) && 'option' === $field['option_type'] && isset( $field['option_name'] ) ) && ! empty( $field['option_name'] ) ) {
86
				$property_setting = str_replace( ']', '', str_replace( $field['option_name'] . '[', '', $field['settings'] ) );
87
				$property_setting = esc_attr( $field['option_name'] ) . '[' . esc_attr( $property_setting ) . '_' . $setting . ']';
88
			} else {
89
				$property_setting = esc_attr( $field['settings'] ) . '_' . $setting;
90
			}
91
92
			// Build the field's output element.
93
			$output_element = $field['output'];
94
			if ( ! empty( $field['output'] ) ) {
95
				if ( is_array( $field['output'] ) ) {
96
					if ( isset( $field['output']['element'] ) ) {
97
						$output_element = $field['output']['element'];
98
					} elseif ( isset( $field['output'][0] ) && isset( $field['output'][0]['element'] ) ) {
99
						$output_element = $field['output'][0]['element'];
100
					}
101
				}
102
			}
103
104
			/**
105
			 * Build the field.
106
			 * We're merging with the original field here, so any extra properties are inherited.
107
			 */
108
			$fields[ $property_setting ] = array_merge( $field, array(
109
				'type'              => $type,
110
				'label'             => $label,
111
				'settings'          => $property_setting,
112
				'tooltip'           => $tooltip,
113
				'section'           => $field['section'],
114
				'priority'          => $field['priority'],
115
				'required'          => $field['required'],
116
				'description'       => $description,
117
				'default'           => $value,
118
				'id'                => sanitize_key( str_replace( '[', '-', str_replace( ']', '', $property_setting ) ) ),
119
				'choices'           => isset( $choices[ $key ] ) ? $choices[ $key ] : array(),
120
				'sanitize_callback' => $sanitize_callback,
121
				'output'            => ( ! empty( $field['output'] ) ) ? array(
122
					array(
123
						'element'  => $output_element,
124
						'property' => $output_property,
125
					),
126
				) : array(),
127
			) );
128
			$i++;
129
		}
130
131
		return $fields;
132
133
	}
134
135
	/**
136
	 * Parse all fields and add the new background fields
137
	 *
138
	 * @param 	array $fields An array of all the generated fields.
139
	 * @return  array
140
	 */
141
	public static function process_fields( $fields ) {
142
143
		foreach ( $fields as $field ) {
144
			/**
145
			 * Make sure background fields are exploded
146
			 */
147
			if ( isset( $field['type'] ) && 'background' === $field['type'] ) {
148
				$explode = self::explode( $field );
149
				$fields  = array_merge( $fields, $explode );
150
			}
151
		}
152
153
		return $fields;
154
155
	}
156
157
158
	/**
159
	 * The background choices.
160
	 *
161
	 * @return array<string,array>
162
	 */
163
	public static function background_choices() {
164
165
		$l10n = self::l10n( $field['kirki_config'] );
0 ignored issues
show
Bug introduced by
The variable $field does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
166
167
		return array(
168
			'repeat'        => array(
169
				'no-repeat' => $l10n['no-repeat'],
170
				'repeat'    => $l10n['repeat-all'],
171
				'repeat-x'  => $l10n['repeat-x'],
172
				'repeat-y'  => $l10n['repeat-y'],
173
				'inherit'   => $l10n['inherit'],
174
			),
175
			'size'        => array(
176
				'inherit' => $l10n['inherit'],
177
				'cover'   => $l10n['cover'],
178
				'contain' => $l10n['contain'],
179
			),
180
			'attach'      => array(
181
				'inherit' => $l10n['inherit'],
182
				'fixed'   => $l10n['fixed'],
183
				'scroll'  => $l10n['scroll'],
184
			),
185
			'position'          => array(
186
				'left-top'      => $l10n['left-top'],
187
				'left-center'   => $l10n['left-center'],
188
				'left-bottom'   => $l10n['left-bottom'],
189
				'right-top'     => $l10n['right-top'],
190
				'right-center'  => $l10n['right-center'],
191
				'right-bottom'  => $l10n['right-bottom'],
192
				'center-top'    => $l10n['center-top'],
193
				'center-center' => $l10n['center-center'],
194
				'center-bottom' => $l10n['center-bottom'],
195
			),
196
		);
197
	}
198
199
	/**
200
	 * Returns an array of translation strings.
201
	 *
202
	 * @static
203
	 * @access protected
204
	 * @since 2.4.0
205
	 * @param string|false $id The string-ID.
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
206
	 * @return array
207
	 */
208
	protected static function l10n( $config_id ) {
209
		$translation_strings = array(
210
			'background-color'      => esc_attr__( 'Background Color', 'kirki' ),
211
			'background-image'      => esc_attr__( 'Background Image', 'kirki' ),
212
			'no-repeat'             => esc_attr__( 'No Repeat', 'kirki' ),
213
			'repeat-all'            => esc_attr__( 'Repeat All', 'kirki' ),
214
			'repeat-x'              => esc_attr__( 'Repeat Horizontally', 'kirki' ),
215
			'repeat-y'              => esc_attr__( 'Repeat Vertically', 'kirki' ),
216
			'inherit'               => esc_attr__( 'Inherit', 'kirki' ),
217
			'background-repeat'     => esc_attr__( 'Background Repeat', 'kirki' ),
218
			'cover'                 => esc_attr__( 'Cover', 'kirki' ),
219
			'contain'               => esc_attr__( 'Contain', 'kirki' ),
220
			'background-size'       => esc_attr__( 'Background Size', 'kirki' ),
221
			'fixed'                 => esc_attr__( 'Fixed', 'kirki' ),
222
			'scroll'                => esc_attr__( 'Scroll', 'kirki' ),
223
			'background-attachment' => esc_attr__( 'Background Attachment', 'kirki' ),
224
			'left-top'              => esc_attr__( 'Left Top', 'kirki' ),
225
			'left-center'           => esc_attr__( 'Left Center', 'kirki' ),
226
			'left-bottom'           => esc_attr__( 'Left Bottom', 'kirki' ),
227
			'right-top'             => esc_attr__( 'Right Top', 'kirki' ),
228
			'right-center'          => esc_attr__( 'Right Center', 'kirki' ),
229
			'right-bottom'          => esc_attr__( 'Right Bottom', 'kirki' ),
230
			'center-top'            => esc_attr__( 'Center Top', 'kirki' ),
231
			'center-center'         => esc_attr__( 'Center Center', 'kirki' ),
232
			'center-bottom'         => esc_attr__( 'Center Bottom', 'kirki' ),
233
			'background-position'   => esc_attr__( 'Background Position', 'kirki' ),
234
			'background-opacity'    => esc_attr__( 'Background Opacity', 'kirki' ),
235
		);
236
		return apply_filters( 'kirki/' . $config_id . '/l10n', $translation_strings );
237
	}
238
}
239