1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* A utility class for Kirki. |
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 3.0.9 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Utility class. |
15
|
|
|
*/ |
16
|
|
|
class Kirki_Util { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constructor. |
20
|
|
|
* |
21
|
|
|
* @since 3.0.9 |
22
|
|
|
* @access public |
23
|
|
|
*/ |
24
|
|
|
public function __construct() { |
25
|
|
|
|
26
|
|
|
add_filter( 'http_request_args', array( $this, 'http_request' ), 10, 2 ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Determine if Kirki is installed as a plugin. |
31
|
|
|
* |
32
|
|
|
* @static |
33
|
|
|
* @access public |
34
|
|
|
* @since 3.0.0 |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public static function is_plugin() { |
38
|
|
|
|
39
|
|
|
$is_plugin = false; |
40
|
|
|
if ( ! function_exists( 'get_plugins' ) ) { |
41
|
|
|
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Get all plugins. |
45
|
|
|
$plugins = get_plugins(); |
46
|
|
|
$_plugin = ''; |
47
|
|
|
foreach ( $plugins as $plugin => $args ) { |
48
|
|
|
if ( ! $is_plugin && isset( $args['Name'] ) && ( 'Kirki' === $args['Name'] || 'Kirki Toolkit' === $args['Name'] ) ) { |
49
|
|
|
$is_plugin = true; |
50
|
|
|
$_plugin = $plugin; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// No need to proceed any further if Kirki wasn't found in the list of plugins. |
55
|
|
|
if ( ! $is_plugin ) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Make sure the is_plugins_loaded function is loaded. |
60
|
|
|
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
61
|
|
|
|
62
|
|
|
// Extra logic in case the plugin is installed but not activated. |
63
|
|
|
if ( $_plugin && ! is_plugin_active( $_plugin ) ) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
return $is_plugin; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Build the variables. |
71
|
|
|
* |
72
|
|
|
* @static |
73
|
|
|
* @access public |
74
|
|
|
* @since 3.0.9 |
75
|
|
|
* @return array Formatted as array( 'variable-name' => value ). |
76
|
|
|
*/ |
77
|
|
|
public static function get_variables() { |
78
|
|
|
|
79
|
|
|
$variables = array(); |
80
|
|
|
|
81
|
|
|
// Loop through all fields. |
82
|
|
|
foreach ( Kirki::$fields as $field ) { |
83
|
|
|
|
84
|
|
|
// Check if we have variables for this field. |
85
|
|
|
if ( isset( $field['variables'] ) && $field['variables'] && ! empty( $field['variables'] ) ) { |
86
|
|
|
|
87
|
|
|
// Loop through the array of variables. |
88
|
|
|
foreach ( $field['variables'] as $field_variable ) { |
89
|
|
|
|
90
|
|
|
// Is the variable ['name'] defined? If yes, then we can proceed. |
91
|
|
|
if ( isset( $field_variable['name'] ) ) { |
92
|
|
|
|
93
|
|
|
// Sanitize the variable name. |
94
|
|
|
$variable_name = esc_attr( $field_variable['name'] ); |
95
|
|
|
|
96
|
|
|
// Do we have a callback function defined? If not then set $variable_callback to false. |
97
|
|
|
$variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false; |
98
|
|
|
|
99
|
|
|
// If we have a variable_callback defined then get the value of the option |
100
|
|
|
// and run it through the callback function. |
101
|
|
|
// If no callback is defined (false) then just get the value. |
102
|
|
|
$variables[ $variable_name ] = Kirki_Values::get_value( $field['settings'] ); |
103
|
|
|
if ( $variable_callback ) { |
104
|
|
|
$variables[ $variable_name ] = call_user_func( $field_variable['callback'], Kirki_Values::get_value( $field['settings'] ) ); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Pass the variables through a filter ('kirki/variable') and return the array of variables. |
112
|
|
|
return apply_filters( 'kirki/variable', $variables ); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* HTTP Request injection. |
118
|
|
|
* |
119
|
|
|
* @access public |
120
|
|
|
* @since 3.0.0 |
121
|
|
|
* @param array $request The request params. |
122
|
|
|
* @param string $url The request URL. |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function http_request( $request = array(), $url = '' ) { |
126
|
|
|
// Early exit if installed as a plugin or not a request to wordpress.org, |
127
|
|
|
// or finally if we don't have everything we need. |
128
|
|
|
if ( |
129
|
|
|
self::is_plugin() || |
130
|
|
|
false === strpos( $url, 'wordpress.org' ) || ( |
131
|
|
|
! isset( $request['body'] ) || |
132
|
|
|
! isset( $request['body']['plugins'] ) || |
133
|
|
|
! isset( $request['body']['translations'] ) || |
134
|
|
|
! isset( $request['body']['locale'] ) || |
135
|
|
|
! isset( $request['body']['all'] ) |
136
|
|
|
) |
137
|
|
|
) { |
138
|
|
|
return $request; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$plugins = json_decode( $request['body']['plugins'], true ); |
142
|
|
|
if ( ! isset( $plugins['plugins'] ) ) { |
143
|
|
|
return $request; |
144
|
|
|
} |
145
|
|
|
$exists = false; |
146
|
|
|
foreach ( $plugins['plugins'] as $plugin ) { |
147
|
|
|
if ( isset( $plugin['Name'] ) && 'Kirki Toolkit' === $plugin['Name'] ) { |
148
|
|
|
$exists = true; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
// Inject data. |
152
|
|
|
if ( ! $exists && defined( 'KIRKI_PLUGIN_FILE' ) ) { |
153
|
|
|
$plugins['plugins']['kirki/kirki.php'] = get_plugin_data( KIRKI_PLUGIN_FILE ); |
154
|
|
|
} |
155
|
|
|
$request['body']['plugins'] = wp_json_encode( $plugins ); |
156
|
|
|
return $request; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Checks the WP version and returns true if the version is >= 4.9, |
161
|
|
|
* or false if the version is < 4.9. |
162
|
|
|
* |
163
|
|
|
* In WordPress 4.9 the colorpicker script changed so we are forced to use |
164
|
|
|
* separate scripts depending on the WordPress version used. |
165
|
|
|
* |
166
|
|
|
* @static |
167
|
|
|
* @access public |
168
|
|
|
* @since 3.0.12 |
169
|
|
|
* @return boolean |
170
|
|
|
*/ |
171
|
|
|
public static function is_colorpicker_script_new() { |
172
|
|
|
global $wp_version; |
173
|
|
|
if ( version_compare( $wp_version, '4.9-beta', '<' ) ) { |
|
|
|
|
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
return true; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|