|
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
|
|
|
$variables[ $variable_name ] = Kirki::get_option( $field['settings'] ); |
|
122
|
|
|
if ( $variable_callback ) { |
|
123
|
|
|
$variables[ $variable_name ] = call_user_func( $field_variable['callback'], Kirki::get_option( $field['settings'] ) ); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Pass the variables through a filter ('kirki/variable') and return the array of variables. |
|
131
|
|
|
return apply_filters( 'kirki/variable', $variables ); |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Plugin is active. |
|
137
|
|
|
* |
|
138
|
|
|
* @since 3.0.0 |
|
139
|
|
|
* @access public |
|
140
|
|
|
* @param array $plugins An array of active plugins. |
|
141
|
|
|
* @return array Active plugins. |
|
142
|
|
|
*/ |
|
143
|
|
|
public function is_plugin_active( $plugins ) { |
|
|
|
|
|
|
144
|
|
|
global $pagenow; |
|
145
|
|
|
$exclude = array( |
|
146
|
|
|
'plugins.php', |
|
147
|
|
|
'plugin-install.php', |
|
148
|
|
|
); |
|
149
|
|
|
$referer = ( isset( $_SERVER ) && isset( $_SERVER['HTTP_REFERER'] ) ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; |
|
|
|
|
|
|
150
|
|
|
$refered = false; |
|
151
|
|
|
foreach ( $exclude as $exception ) { |
|
152
|
|
|
if ( false !== strpos( $referer, $exception ) ) { |
|
153
|
|
|
$refered = true; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
if ( is_array( $plugins ) && ! in_array( $pagenow, $exclude, true ) && ! $refered ) { |
|
157
|
|
|
$exists = false; |
|
158
|
|
|
foreach ( $plugins as $plugin ) { |
|
159
|
|
|
if ( false !== strpos( $plugin, 'kirki.php' ) ) { |
|
160
|
|
|
$exists = true; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
if ( ! $exists ) { |
|
164
|
|
|
$plugins[] = 'kirki/kirki.php'; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
return $plugins; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* HTTP Request injection. |
|
172
|
|
|
* |
|
173
|
|
|
* @access public |
|
174
|
|
|
* @since 3.0.0 |
|
175
|
|
|
* @param array $r The request params. |
|
176
|
|
|
* @param string $url The request URL. |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
|
|
public function http_request( $r = array(), $url = '' ) { |
|
|
|
|
|
|
180
|
|
|
// Early exit if installed as a plugin or not a request to wordpress.org, |
|
181
|
|
|
// or finally if we don't have everything we need. |
|
182
|
|
|
if ( self::is_plugin() || false === strpos( $url, 'wordpress.org' ) || ( ! isset( $r['body'] ) || ! isset( $r['body']['plugins'] ) || ! isset( $r['body']['translations'] ) || ! isset( $r['body']['locale'] ) || ! isset( $r['body']['all'] ) ) ) { |
|
183
|
|
|
return $r; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
// Inject data. |
|
187
|
|
|
$plugins = json_decode( $r['body']['plugins'], true ); |
|
188
|
|
|
if ( isset( $plugins['plugins'] ) ) { |
|
189
|
|
|
$exists = false; |
|
190
|
|
|
foreach ( $plugins['plugins'] as $plugin ) { |
|
191
|
|
|
if ( isset( $plugin['Name'] ) && 'Kirki Toolkit' === $plugin['Name'] ) { |
|
192
|
|
|
$exists = true; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
if ( ! $exists && defined( 'KIRKI_PLUGIN_FILE' ) ) { |
|
196
|
|
|
$plugins['plugins']['kirki/kirki.php'] = get_plugin_data( KIRKI_PLUGIN_FILE ); |
|
197
|
|
|
} |
|
198
|
|
|
$r['body']['plugins'] = json_encode( $plugins ); |
|
199
|
|
|
return $r; |
|
200
|
|
|
} |
|
201
|
|
|
return $r; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
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: