Completed
Push — develop ( 6bc4f7...cab9c0 )
by Aristeides
04:02
created

Kirki_Setting_User_Meta::set_root_value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * WordPress Customize Setting classes
4
 *
5
 * @package Kirki
6
 * @subpackage Modules
7
 * @since 3.0.0
8
 */
9
10
/**
11
 * Handles saving and sanitizing of user-meta.
12
 *
13
 * @since 3.0.0
14
 * @see WP_Customize_Setting
15
 */
16
class Kirki_Setting_User_Meta extends WP_Customize_Setting {
17
18
	/**
19
	 * Type of customize settings.
20
	 *
21
	 * @access public
22
	 * @since 3.0.0
23
	 * @var string
24
	 */
25
	public $type = 'user_meta';
26
27
	/**
28
	 * Get the root value for a setting, especially for multidimensional ones.
29
	 *
30
	 * @access protected
31
	 * @since 3.0.0
32
	 * @param mixed $default Value to return if root does not exist.
33
	 * @return mixed
34
	 */
35
	protected function get_root_value( $default = null ) {
36
		$id_base = $this->id_data['base'];
37
38
		// Get all user-meta.
39
		// We'll use this to check if the value is set or not,
40
		// in order to figure out if we need to return the default value.
41
		// @codingStandardsIgnoreLine
42
		$user_meta = get_user_meta( get_current_user_id() );
43
44
		// Get the single meta.
45
		// @codingStandardsIgnoreLine
46
		$single_meta = get_user_meta( get_current_user_id(), $id_base, true );
47
48
		if ( isset( $user_meta[ $id_base ] ) ) {
49
			return $single_meta;
50
		}
51
		return $default;
52
	}
53
54
	/**
55
	 * Set the root value for a setting, especially for multidimensional ones.
56
	 *
57
	 * @access protected
58
	 * @since 3.0.0
59
	 * @param mixed $value Value to set as root of multidimensional setting.
60
	 * @return bool Whether the multidimensional root was updated successfully.
61
	 */
62
	protected function set_root_value( $value ) {
63
		$id_base = $this->id_data['base'];
64
65
		// First delete the current user-meta.
66
		// We're doing this to avoid duplicate entries.
67
		// @codingStandardsIgnoreLine
68
		delete_user_meta( get_current_user_id(), $id_base );
69
70
		// Update the user-meta.
71
		// @codingStandardsIgnoreLine
72
		return update_user_meta( get_current_user_id(), $id_base, $value );
73
	}
74
75
	/**
76
	 * Save the value of the setting, using the related API.
77
	 *
78
	 * @access protected
79
	 * @since 3.0.0
80
	 * @param mixed $value The value to update.
81
	 * @return bool The result of saving the value.
82
	 */
83
	protected function update( $value ) {
84
		$id_base = $this->id_data['base'];
85
		return $this->set_root_value( $value );
86
	}
87
88
	/**
89
	 * Fetch the value of the setting.
90
	 *
91
	 * @access protected
92
	 * @since 3.0.0
93
	 * @return mixed The value.
94
	 */
95
	public function value() {
96
		return $this->get_root_value( $this->default );
97
	}
98
}
99