Completed
Pull Request — master (#1454)
by Aristeides
04:04
created

Kirki_Field_Property_Output   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B get_property() 0 26 6
F get_output() 0 26 14
1
<?php
2
/**
3
 * Properly format the "output" argument.
4
 *
5
 * @package     Kirki
6
 * @category    Core
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
9
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
10
 * @since       1.0
11
 */
12
13
/**
14
 * Properly format the "output" argument.
15
 */
16
class Kirki_Field_Property_Output extends Kirki_Field_Property {
17
18
	/**
19
	 * Gets the property.
20
	 *
21
	 * @access public
22
	 */
23
	public function get_property() {
24
25
		if ( empty( $this->args['output'] ) ) {
26
			return array();
27
		}
28
		if ( ! empty( $this->args['output'] ) && ! is_array( $this->args['output'] ) ) {
29
			/* translators: %s represents the field ID where the error occurs. */
30
			_doing_it_wrong( __METHOD__, sprintf( esc_attr__( '"output" invalid format in field %s. The "output" argument should be defined as an array of arrays.', 'kirki' ), esc_attr( $this->settings ) ), '3.0.10' );
0 ignored issues
show
Bug introduced by
The property settings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
			$this->args['output'] = array(
32
				array(
33
					'element' => $this->args['output'],
34
				),
35
			);
36
		}
37
		// Convert to array of arrays if needed.
38
		if ( isset( $this->args['output']['element'] ) ) {
39
			/* translators: %s represents the field ID where the error occurs. */
40
			_doing_it_wrong( __METHOD__, sprintf( esc_attr__( '"output" invalid format in field %s. The "output" argument should be defined as an array of arrays.', 'kirki' ), esc_attr( $this->settings ) ), '3.0.10' );
41
			$this->args['output'] = array( $this->args['output'] );
42
		}
43
		$outputs = array();
44
		foreach ( $this->args['output'] as $output ) {
45
			$outputs[] = $this->get_output( $output );
46
		}
47
		return $outputs;
48
	}
49
50
	/**
51
	 * Properly format each output sub-array.
52
	 *
53
	 * @access private
54
	 * @since 3.0.10
55
	 * @param array $output The output argument.
56
	 * @return array
57
	 */
58
	private function get_output( $output ) {
59
		if ( ! isset( $output['element'] ) ) {
60
			return array();
61
		}
62
		if ( ! isset( $output['sanitize_callback'] ) && isset( $output['callback'] ) ) {
63
			$output['sanitize_callback'] = $output['callback'];
64
		}
65
		// Convert element arrays to strings.
66
		if ( is_array( $output['element'] ) ) {
67
			$output['element'] = array_unique( $output['element'] );
68
			sort( $output['element'] );
69
			$output['element'] = implode( ',', $output['element'] );
70
		}
71
		return array(
72
			'element'           => $output['element'],
73
			'property'          => ( isset( $output['property'] ) ) ? $output['property'] : '',
74
			'media_query'       => ( isset( $output['media_query'] ) ) ? $output['media_query'] : 'global',
75
			'sanitize_callback' => ( isset( $output['sanitize_callback'] ) ) ? $output['sanitize_callback'] : '',
76
			'units'             => ( isset( $output['units'] ) ) ? $output['units'] : '',
77
			'prefix'            => ( isset( $output['prefix'] ) ) ? $output['prefix'] : '',
78
			'suffix'            => ( isset( $output['suffix'] ) ) ? $output['suffix'] : '',
79
			'exclude'           => ( isset( $output['exclude'] ) ) ? $output['exclude'] : false,
80
			'value_pattern'     => ( isset( $output['value_pattern'] ) ) ? $output['value_pattern'] : '$',
81
			'pattern_replace'   => ( isset( $output['pattern_replace'] ) ) ? $output['pattern_replace'] : array(),
82
		);
83
	}
84
}
85