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 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_filter( 'http_request_args', array( $this, 'http_request' ), 10, 2 ); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Determine if Kirki is installed as a plugin. |
||
30 | * |
||
31 | * @static |
||
32 | * @access public |
||
33 | * @since 3.0.0 |
||
34 | * @return bool |
||
35 | */ |
||
36 | public static function is_plugin() { |
||
37 | $is_plugin = false; |
||
38 | if ( ! function_exists( 'get_plugins' ) ) { |
||
39 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
40 | } |
||
41 | |||
42 | // Get all plugins. |
||
43 | $plugins = get_plugins(); |
||
44 | $_plugin = ''; |
||
45 | foreach ( $plugins as $plugin => $args ) { |
||
46 | if ( ! $is_plugin && isset( $args['Name'] ) && ( 'Kirki' === $args['Name'] || 'Kirki Toolkit' === $args['Name'] ) ) { |
||
47 | $is_plugin = true; |
||
48 | $_plugin = $plugin; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | // No need to proceed any further if Kirki wasn't found in the list of plugins. |
||
53 | if ( ! $is_plugin ) { |
||
54 | return false; |
||
55 | } |
||
56 | |||
57 | // Make sure the is_plugins_loaded function is loaded. |
||
58 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
59 | |||
60 | // Extra logic in case the plugin is installed but not activated. |
||
61 | if ( $_plugin && is_plugin_inactive( $_plugin ) ) { |
||
62 | return false; |
||
63 | } |
||
64 | return $is_plugin; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Build the variables. |
||
69 | * |
||
70 | * @static |
||
71 | * @access public |
||
72 | * @since 3.0.9 |
||
73 | * @return array Formatted as array( 'variable-name' => value ). |
||
74 | */ |
||
75 | public static function get_variables() { |
||
76 | |||
77 | $variables = array(); |
||
78 | |||
79 | // Loop through all fields. |
||
80 | foreach ( Kirki::$fields as $field ) { |
||
81 | |||
82 | // Check if we have variables for this field. |
||
83 | if ( isset( $field['variables'] ) && $field['variables'] && ! empty( $field['variables'] ) ) { |
||
84 | |||
85 | // Loop through the array of variables. |
||
86 | foreach ( $field['variables'] as $field_variable ) { |
||
87 | |||
88 | // Is the variable ['name'] defined? If yes, then we can proceed. |
||
89 | if ( isset( $field_variable['name'] ) ) { |
||
90 | |||
91 | // Do we have a callback function defined? If not then set $variable_callback to false. |
||
92 | $variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false; |
||
93 | |||
94 | // If we have a variable_callback defined then get the value of the option |
||
95 | // and run it through the callback function. |
||
96 | // If no callback is defined (false) then just get the value. |
||
97 | $variables[ $field_variable['name'] ] = Kirki_Values::get_value( $field['settings'] ); |
||
98 | if ( $variable_callback ) { |
||
99 | $variables[ $field_variable['name'] ] = call_user_func( $field_variable['callback'], Kirki_Values::get_value( $field['settings'] ) ); |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | |||
106 | // Pass the variables through a filter ('kirki_variable') and return the array of variables. |
||
107 | return apply_filters( 'kirki_variable', $variables ); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * HTTP Request injection. |
||
112 | * |
||
113 | * @access public |
||
114 | * @since 3.0.0 |
||
115 | * @param array $request The request params. |
||
116 | * @param string $url The request URL. |
||
117 | * @return array |
||
118 | */ |
||
119 | public function http_request( $request = array(), $url = '' ) { |
||
120 | |||
121 | // Early exit if installed as a plugin or not a request to wordpress.org, |
||
122 | // or finally if we don't have everything we need. |
||
123 | if ( |
||
124 | self::is_plugin() || |
||
125 | false === strpos( $url, 'wordpress.org' ) || ( |
||
126 | ! isset( $request['body'] ) || |
||
127 | ! isset( $request['body']['plugins'] ) || |
||
128 | ! isset( $request['body']['translations'] ) || |
||
129 | ! isset( $request['body']['locale'] ) || |
||
130 | ! isset( $request['body']['all'] ) |
||
131 | ) |
||
132 | ) { |
||
133 | return $request; |
||
134 | } |
||
135 | |||
136 | $plugins = json_decode( $request['body']['plugins'], true ); |
||
137 | if ( ! isset( $plugins['plugins'] ) ) { |
||
138 | return $request; |
||
139 | } |
||
140 | $exists = false; |
||
141 | foreach ( $plugins['plugins'] as $plugin ) { |
||
142 | if ( isset( $plugin['Name'] ) && 'Kirki Toolkit' === $plugin['Name'] ) { |
||
143 | $exists = true; |
||
144 | } |
||
145 | } |
||
146 | // Inject data. |
||
147 | if ( ! $exists && defined( 'KIRKI_PLUGIN_FILE' ) ) { |
||
148 | $plugins['plugins']['kirki/kirki.php'] = get_plugin_data( KIRKI_PLUGIN_FILE ); |
||
149 | } |
||
150 | $request['body']['plugins'] = wp_json_encode( $plugins ); |
||
151 | return $request; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Returns the $wp_version. |
||
156 | * |
||
157 | * @static |
||
158 | * @access public |
||
159 | * @since 3.0.12 |
||
160 | * @param string $context Use 'minor' or 'major'. |
||
161 | * @return int|string Returns integer when getting the 'major' version. |
||
162 | * Returns string when getting the 'minor' version. |
||
163 | */ |
||
164 | public static function get_wp_version( $context = 'minor' ) { |
||
165 | global $wp_version; |
||
166 | |||
167 | // We only need the major version. |
||
168 | if ( 'major' === $context ) { |
||
169 | $version_parts = explode( '.', $wp_version ); |
||
170 | return $version_parts[0]; |
||
171 | } |
||
172 | |||
173 | return $wp_version; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Returns the $wp_version, only numeric value. |
||
178 | * |
||
179 | * @static |
||
180 | * @access public |
||
181 | * @since 3.0.12 |
||
182 | * @param string $context Use 'minor' or 'major'. |
||
183 | * @param bool $only_numeric Whether we wwant to return numeric value or include beta/alpha etc. |
||
184 | * @return int|float Returns integer when getting the 'major' version. |
||
185 | * Returns float when getting the 'minor' version. |
||
186 | */ |
||
187 | public static function get_wp_version_numeric( $context = 'minor', $only_numeric = true ) { |
||
0 ignored issues
–
show
|
|||
188 | global $wp_version; |
||
189 | |||
190 | // We only need the major version. |
||
191 | if ( 'major' === $context ) { |
||
192 | $version_parts = explode( '.', $wp_version ); |
||
193 | return absint( $version_parts[0] ); |
||
194 | } |
||
195 | |||
196 | // If we got this far, we want the full monty. |
||
197 | // Get the numeric part of the version without any beta, alpha etc parts. |
||
198 | if ( false !== strpos( $wp_version, '-' ) ) { |
||
199 | // We're on a dev version. |
||
200 | $version_parts = explode( '-', $wp_version ); |
||
201 | return floatval( $version_parts[0] ); |
||
202 | } |
||
203 | return floatval( $wp_version ); |
||
204 | } |
||
205 | } |
||
206 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.