Passed
Push — develop ( 3892e2...b1663d )
by Aristeides
03:47
created

core/class-kirki-values.php (1 issue)

1
<?php
2
/**
3
 * Hekoers to get the values of a field.
4
 * WARNING: PLEASE DO NOT USE THESE.
5
 * we only have these for backwards-compatibility purposes.
6
 * please use get_option() & get_theme_mod() instead.
7
 *
8
 * @package     Kirki
9
 * @category    Core
10
 * @author      Aristeides Stathopoulos
11
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
12
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
13
 * @since       1.0
14
 */
15
16
/**
17
 * Wrapper class for static methods.
18
 */
19
class Kirki_Values {
20
21
	/**
22
	 * Constructor.
23
	 *
24
	 * @access public
25
	 * @since 3.0.10
26
	 */
27
	public function __construct() {
28
29
		add_filter( 'kirki_values_get_value', array( $this, 'typography_field_tweaks' ), 10, 2 );
30
	}
31
32
	/**
33
	 * Tweaks for typography fields.
34
	 *
35
	 * @access public
36
	 * @since 3.0.10
37
	 * @param string|array $value    The value.
38
	 * @param string       $field_id The field-ID.
39
	 * @return array
40
	 */
41
	public function typography_field_tweaks( $value, $field_id ) {
42
43
		if ( isset( Kirki::$fields[ $field_id ] ) && isset( Kirki::$fields[ $field_id ]['type'] ) ) {
44
			if ( 'kirki-typography' === Kirki::$fields[ $field_id ]['type'] ) {
45
46
				// Sanitize the value.
47
				// This also adds font-weight if it doesn't already exist.
48
				$value = Kirki_Field_Typography::sanitize( $value );
0 ignored issues
show
It seems like $value can also be of type string; however, parameter $value of Kirki_Field_Typography::sanitize() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
				$value = Kirki_Field_Typography::sanitize( /** @scrutinizer ignore-type */ $value );
Loading history...
49
50
				// Combine font-family and font-backup.
51
				if ( isset( $value['font-family'] ) && isset( $value['font-backup'] ) ) {
52
					$value['font-family'] .= ', ' . $value['font-backup'];
53
					unset( $value['font-backup'] );
54
				}
55
			}
56
		}
57
		return $value;
58
	}
59
60
61
	/**
62
	 * Get the value of a field.
63
	 *
64
	 * @static
65
	 * @access public
66
	 * @param string $config_id The configuration ID. @see Kirki_Config.
67
	 * @param string $field_id  The field ID.
68
	 * @return string|array
69
	 */
70
	public static function get_value( $config_id = '', $field_id = '' ) {
71
72
		// Make sure value is defined.
73
		$value = '';
74
75
		// This allows us to skip the $config_id argument.
76
		// If we skip adding a $config_id, use the 'global' configuration.
77
		if ( ( '' === $field_id ) && '' !== $config_id ) {
78
			$field_id  = $config_id;
79
			$config_id = 'global';
80
		}
81
82
		// If $config_id is empty, set it to 'global'.
83
		$config_id = ( '' === $config_id ) ? 'global' : $config_id;
84
85
		// Fallback to 'global' if $config_id is not found.
86
		if ( ! isset( Kirki::$config[ $config_id ] ) ) {
87
			$config_id = 'global';
88
		}
89
90
		if ( 'theme_mod' === Kirki::$config[ $config_id ]['option_type'] ) {
91
92
			// We're using theme_mods so just get the value using get_theme_mod.
93
			$default_value = null;
94
			if ( isset( Kirki::$fields[ $field_id ] ) && isset( Kirki::$fields[ $field_id ]['default'] ) ) {
95
				$default_value = Kirki::$fields[ $field_id ]['default'];
96
			}
97
			$value = get_theme_mod( $field_id, $default_value );
98
			return apply_filters( 'kirki_values_get_value', $value, $field_id );
99
		}
100
101
		if ( 'option' === Kirki::$config[ $config_id ]['option_type'] ) {
102
103
			// We're using options.
104
			if ( '' !== Kirki::$config[ $config_id ]['option_name'] ) {
105
106
				// Options are serialized as a single option in the db.
107
				// We'll have to get the option and then get the item from the array.
108
				$options = get_option( Kirki::$config[ $config_id ]['option_name'] );
109
110
				if ( ! isset( Kirki::$fields[ $field_id ] ) && isset( Kirki::$fields[ Kirki::$config[ $config_id ]['option_name'] . '[' . $field_id . ']' ] ) ) {
111
					$field_id = Kirki::$config[ $config_id ]['option_name'] . '[' . $field_id . ']';
112
				}
113
				$setting_modified = str_replace( ']', '', str_replace( Kirki::$config[ $config_id ]['option_name'] . '[', '', $field_id ) );
114
115
				$default_value = ( isset( Kirki::$fields[ $field_id ] ) && isset( Kirki::$fields[ $field_id ]['default'] ) ) ? Kirki::$fields[ $field_id ]['default'] : '';
116
				$value         = ( isset( $options[ $setting_modified ] ) ) ? $options[ $setting_modified ] : $default_value;
117
				$value         = maybe_unserialize( $value );
118
				return apply_filters( 'kirki_values_get_value', $value, $field_id );
119
			}
120
121
			// Each option separately saved in the db.
122
			$value = get_option( $field_id, Kirki::$fields[ $field_id ]['default'] );
123
			return apply_filters( 'kirki_values_get_value', $value, $field_id );
124
125
		} // End if().
126
127
		return apply_filters( 'kirki_values_get_value', $value, $field_id );
128
129
	}
130
131
	/**
132
	 * Gets the value or fallsback to default.
133
	 *
134
	 * @static
135
	 * @access public
136
	 * @param array $field The field aruments.
137
	 * @return string|array
138
	 */
139
	public static function get_sanitized_field_value( $field ) {
140
		$value = $field['default'];
141
		if ( isset( $field['option_type'] ) && 'theme_mod' === $field['option_type'] ) {
142
			$value = get_theme_mod( $field['settings'], $field['default'] );
143
		} elseif ( isset( $field['option_type'] ) && 'option' === $field['option_type'] ) {
144
			if ( isset( $field['option_name'] ) && '' !== $field['option_name'] ) {
145
				$all_values     = get_option( $field['option_name'], array() );
146
				$sub_setting_id = str_replace( array( ']', $field['option_name'] . '[' ), '', $field['settings'] );
147
				if ( isset( $all_values[ $sub_setting_id ] ) ) {
148
					$value = $all_values[ $sub_setting_id ];
149
				}
150
			} else {
151
				$value = get_option( $field['settings'], $field['default'] );
152
			}
153
		}
154
155
		return $value;
156
157
	}
158
}
159