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
|
|
|
add_action( 'after_setup_theme', array( $this, 'acf_pro_compatibility' ) ); |
26
|
|
|
add_filter( 'http_request_args', array( $this, 'http_request' ), 10, 2 ); |
27
|
|
|
add_filter( 'option_active_plugins', array( $this, 'is_plugin_active' ) ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Changes select2 version in ACF. |
32
|
|
|
* Fixes a plugin conflict that was causing select fields to crash |
33
|
|
|
* because of a version mismatch between ACF's and Kirki's select2 scripts. |
34
|
|
|
* Props @hellor0bot |
35
|
|
|
* |
36
|
|
|
* @see https://github.com/aristath/kirki/issues/1302 |
37
|
|
|
* @access public |
38
|
|
|
* @since 3.0.0 |
39
|
|
|
*/ |
40
|
|
|
public function acf_pro_compatibility() { |
41
|
|
|
if ( is_customize_preview() ) { |
42
|
|
|
add_filter( 'acf/settings/enqueue_select2', '__return_false', 99 ); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Determine if Kirki is installed as a plugin. |
48
|
|
|
* |
49
|
|
|
* @static |
50
|
|
|
* @access public |
51
|
|
|
* @since 3.0.0 |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public static function is_plugin() { |
55
|
|
|
|
56
|
|
|
$is_plugin = false; |
57
|
|
|
if ( ! function_exists( 'get_plugins' ) ) { |
58
|
|
|
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Get all plugins. |
62
|
|
|
$plugins = get_plugins(); |
63
|
|
|
$_plugin = ''; |
64
|
|
|
foreach ( $plugins as $plugin => $args ) { |
65
|
|
|
if ( ! $is_plugin && isset( $args['Name'] ) && ( 'Kirki' === $args['Name'] || 'Kirki Toolkit' === $args['Name'] ) ) { |
66
|
|
|
$is_plugin = true; |
67
|
|
|
$_plugin = $plugin; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// No need to proceed any further if Kirki wasn't found in the list of plugins. |
72
|
|
|
if ( ! $is_plugin ) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Extra logic in case the plugin is installed but not activated. |
77
|
|
|
// Make sure the is_plugins_loaded function is loaded. |
78
|
|
|
if ( ! function_exists( 'is_plugin_active' ) ) { |
79
|
|
|
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ( $_plugin && ! is_plugin_active( $_plugin ) ) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
return $is_plugin; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Build the variables. |
90
|
|
|
* |
91
|
|
|
* @static |
92
|
|
|
* @access public |
93
|
|
|
* @since 3.0.9 |
94
|
|
|
* @return array Formatted as array( 'variable-name' => value ). |
95
|
|
|
*/ |
96
|
|
|
public static function get_variables() { |
97
|
|
|
|
98
|
|
|
$variables = array(); |
99
|
|
|
|
100
|
|
|
// Loop through all fields. |
101
|
|
|
foreach ( Kirki::$fields as $field ) { |
102
|
|
|
|
103
|
|
|
// Check if we have variables for this field. |
104
|
|
|
if ( isset( $field['variables'] ) && $field['variables'] && ! empty( $field['variables'] ) ) { |
105
|
|
|
|
106
|
|
|
// Loop through the array of variables. |
107
|
|
|
foreach ( $field['variables'] as $field_variable ) { |
108
|
|
|
|
109
|
|
|
// Is the variable ['name'] defined? If yes, then we can proceed. |
110
|
|
|
if ( isset( $field_variable['name'] ) ) { |
111
|
|
|
|
112
|
|
|
// Sanitize the variable name. |
113
|
|
|
$variable_name = esc_attr( $field_variable['name'] ); |
114
|
|
|
|
115
|
|
|
// Do we have a callback function defined? If not then set $variable_callback to false. |
116
|
|
|
$variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false; |
117
|
|
|
|
118
|
|
|
// If we have a variable_callback defined then get the value of the option |
119
|
|
|
// and run it through the callback function. |
120
|
|
|
// If no callback is defined (false) then just get the value. |
121
|
|
|
if ( $variable_callback ) { |
122
|
|
|
$variables[ $variable_name ] = call_user_func( $field_variable['callback'], Kirki::get_option( $field['settings'] ) ); |
123
|
|
|
} else { |
124
|
|
|
$variables[ $variable_name ] = Kirki::get_option( $field['settings'] ); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Pass the variables through a filter ('kirki/variable') and return the array of variables. |
132
|
|
|
return apply_filters( 'kirki/variable', $variables ); |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Plugin is active. |
138
|
|
|
* |
139
|
|
|
* @since 3.0.0 |
140
|
|
|
* @access public |
141
|
|
|
* @param array $plugins An array of active plugins. |
142
|
|
|
* @return array Active plugins. |
143
|
|
|
*/ |
144
|
|
|
public function is_plugin_active( $plugins ) { |
|
|
|
|
145
|
|
|
global $pagenow; |
146
|
|
|
$exclude = array( |
147
|
|
|
'plugins.php', |
148
|
|
|
'plugin-install.php', |
149
|
|
|
); |
150
|
|
|
$referer = ( isset( $_SERVER ) && isset( $_SERVER['HTTP_REFERER'] ) ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; |
|
|
|
|
151
|
|
|
$refered = false; |
152
|
|
|
foreach ( $exclude as $exception ) { |
153
|
|
|
if ( false !== strpos( $referer, $exception ) ) { |
154
|
|
|
$refered = true; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
if ( is_array( $plugins ) && ! in_array( $pagenow, $exclude, true ) && ! $refered ) { |
158
|
|
|
$exists = false; |
159
|
|
|
foreach ( $plugins as $plugin ) { |
160
|
|
|
if ( false !== strpos( $plugin, 'kirki.php' ) ) { |
161
|
|
|
$exists = true; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
if ( ! $exists ) { |
165
|
|
|
$plugins[] = 'kirki/kirki.php'; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
return $plugins; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* HTTP Request injection. |
173
|
|
|
* |
174
|
|
|
* @access public |
175
|
|
|
* @since 3.0.0 |
176
|
|
|
* @param array $r The request params. |
177
|
|
|
* @param string $url The request URL. |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
public function http_request( $r = array(), $url = '' ) { |
|
|
|
|
181
|
|
|
// Early exit if not a request to wordpress.org. |
182
|
|
|
if ( false === strpos( $url, 'wordpress.org' ) ) { |
183
|
|
|
return $r; |
184
|
|
|
} |
185
|
|
|
// Early exit if Kirki is installed as a plugin. |
186
|
|
|
if ( self::is_plugin() ) { |
187
|
|
|
return $r; |
188
|
|
|
} |
189
|
|
|
// Early exit if we don't have everything we need. |
190
|
|
|
if ( ! isset( $r['body'] ) || ! isset( $r['body']['plugins'] ) || ! isset( $r['body']['translations'] ) || ! isset( $r['body']['locale'] ) || ! isset( $r['body']['all'] ) ) { |
191
|
|
|
return $r; |
192
|
|
|
} |
193
|
|
|
// Inject data. |
194
|
|
|
$plugins = json_decode( $r['body']['plugins'], true ); |
195
|
|
|
if ( isset( $plugins['plugins'] ) ) { |
196
|
|
|
$exists = false; |
197
|
|
|
foreach ( $plugins['plugins'] as $plugin ) { |
198
|
|
|
if ( isset( $plugin['Name'] ) && 'Kirki Toolkit' === $plugin['Name'] ) { |
199
|
|
|
$exists = true; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
if ( ! $exists && defined( 'KIRKI_PLUGIN_FILE' ) ) { |
203
|
|
|
$plugins['plugins']['kirki/kirki.php'] = get_plugin_data( KIRKI_PLUGIN_FILE ); |
204
|
|
|
} |
205
|
|
|
$r['body']['plugins'] = json_encode( $plugins ); |
206
|
|
|
return $r; |
207
|
|
|
} |
208
|
|
|
return $r; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: