Completed
Push — develop ( 758947...76df30 )
by Aristeides
02:42
created

Kirki_Values::get_from_theme_mod()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
	 * Get the value of a field.
23
	 *
24
	 * @static
25
	 * @access public
26
	 * @param string $config_id The configuration ID. @see Kirki_Config.
27
	 * @param string $field_id  The field ID.
28
	 * @return string|array|bool|int|float
29
	 */
30
	public static function get_value( $config_id = '', $field_id = '' ) {
31
32
		// Make sure value is defined.
33
		$value = '';
34
35
		// This allows us to skip the $config_id argument.
36
		// If we skip adding a $config_id, use the 'global' configuration.
37
		if ( ( '' === $field_id ) && '' !== $config_id ) {
38
			$field_id  = $config_id;
39
			$config_id = 'global';
40
		}
41
42
		// If $config_id is empty, set it to 'global'.
43
		$config_id = ( '' === $config_id ) ? 'global' : $config_id;
44
45
		// Fallback to 'global' if $config_id is not found.
46
		if ( ! isset( Kirki::$config[ $config_id ] ) ) {
47
			$config_id = 'global';
48
		}
49
50
		switch ( Kirki::$config[ $config_id ]['option_type'] ) {
51
			case 'option':
52
			case 'site_option':
53
				$site_option = (bool) ( 'site_option' === Kirki::$config[ $config_id ] );
54
				return apply_filters( 'kirki/values/get_value', $this->get_from_option( $config_id, $field_id, $site_option ), $field_id );
55
			case 'user_meta':
56
				$user_id = get_current_user_id();
57
				// @codingStandardsIgnoreLine
58
				return ( $user_id ) ? get_user_meta( $user_id, $field_id, true ) : null;
59
			default:
60
				return apply_filters( 'kirki/values/get_value', $this->get_from_theme_mod( $field_id ), $field_id );
61
		}
62
	}
63
64
	/**
65
	 * Gets value from theme_mod.
66
	 *
67
	 * @static
68
	 * @access private
69
	 * @since 3.0.10
70
	 * @param string $field_id The field ID.
71
	 * @return string|array|bool|int|float
72
	 */
73
	private static function get_from_theme_mod( $field_id ) {
74
		// We're using theme_mods so just get the value using get_theme_mod.
75
		$default_value = null;
76
		if ( isset( Kirki::$fields[ $field_id ] ) && isset( Kirki::$fields[ $field_id ]['default'] ) ) {
77
			$default_value = Kirki::$fields[ $field_id ]['default'];
78
		}
79
		return get_theme_mod( $field_id, $default_value );
80
	}
81
82
	/**
83
	 * Gets value from option.
84
	 *
85
	 * @static
86
	 * @access private
87
	 * @since 3.0.10
88
	 * @param string $config_id   The configuration ID. @see Kirki_Config.
89
	 * @param string $field_id    The field ID.
90
	 * @param bool   $site_option If we're using site-options or normal options.
91
	 * @return string|array|bool|int|float
92
	 */
93
	private static function get_from_option( $config_id, $field_id, $site_option = false ) {
94
		$get_option = ( $site_option ) ? 'get_site_option' : 'get_option';
95
		if ( '' !== Kirki::$config[ $config_id ]['option_name'] ) {
96
97
			if ( false !== strpos( $field_id, Kirki::$config[ $config_id ]['option_name'] . '[' ) ) {
98
				$field_id = str_replace( array( Kirki::$config[ $config_id ]['option_name'], '[', ']' ), '', $field_id );
99
			}
100
101
			// Options are serialized as a single option in the db.
102
			// We'll have to get the option and then get the item from the array.
103
			$options = $get_option( Kirki::$config[ $config_id ]['option_name'] );
104
105
			if ( ! isset( Kirki::$fields[ $field_id ] ) && isset( Kirki::$fields[ Kirki::$config[ $config_id ]['option_name'] . '[' . $field_id . ']' ] ) ) {
106
				$field_id = Kirki::$config[ $config_id ]['option_name'] . '[' . $field_id . ']';
107
			}
108
			$setting_modified = str_replace( ']', '', str_replace( Kirki::$config[ $config_id ]['option_name'] . '[', '', $field_id ) );
109
110
			$default_value = ( isset( Kirki::$fields[ $field_id ] ) && isset( Kirki::$fields[ $field_id ]['default'] ) ) ? Kirki::$fields[ $field_id ]['default'] : '';
111
			$value = ( isset( $options[ $setting_modified ] ) ) ? $options[ $setting_modified ] : $default_value;
112
			$value = maybe_unserialize( $value );
113
			return apply_filters( 'kirki/values/get_value', $value, $field_id );
114
		}
115
116
		// Each option separately saved in the db.
117
		return $get_option( $field_id, Kirki::$fields[ $field_id ]['default'] );
118
	}
119
120
	/**
121
	 * Gets the value or fallsback to default.
122
	 *
123
	 * @static
124
	 * @access public
125
	 * @param array $field The field aruments.
126
	 * @return string|array
127
	 */
128
	public static function get_sanitized_field_value( $field ) {
129
		$value = $field['default'];
130
		if ( isset( $field['option_type'] ) && 'theme_mod' === $field['option_type'] ) {
131
			$value = get_theme_mod( $field['settings'], $field['default'] );
132
		} elseif ( isset( $field['option_type'] ) && 'option' === $field['option_type'] ) {
133
			if ( isset( $field['option_name'] ) && '' !== $field['option_name'] ) {
134
				$all_values = get_option( $field['option_name'], array() );
135
				$sub_setting_id = str_replace( array( ']', $field['option_name'] . '[' ), '', $field['settings'] );
136
				if ( isset( $all_values[ $sub_setting_id ] ) ) {
137
					$value = $all_values[ $sub_setting_id ];
138
				}
139
			} else {
140
				$value = get_option( $field['settings'], $field['default'] );
141
			}
142
		}
143
144
		return $value;
145
146
	}
147
}
148