Issues (377)

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    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
		add_filter( 'kirki_values_get_value', array( $this, 'typography_field_tweaks' ), 10, 2 );
29
	}
30
31
	/**
32
	 * Tweaks for typography fields.
33
	 *
34
	 * @access public
35
	 * @since 3.0.10
36
	 * @param string|array $value    The value.
37
	 * @param string       $field_id The field-ID.
38
	 * @return array
39
	 */
40
	public function typography_field_tweaks( $value, $field_id ) {
41
		if ( isset( Kirki::$fields[ $field_id ] ) && isset( Kirki::$fields[ $field_id ]['type'] ) ) {
42
			if ( 'kirki-typography' === Kirki::$fields[ $field_id ]['type'] ) {
43
44
				// Sanitize the value.
45
				// This also adds font-weight if it doesn't already exist.
46
				$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

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