PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / customizer / packages / utils / src / Util.php
kirki / customizer / packages / utils / src Last commit date
Setting 5 months ago Helper.php 5 months ago MaterialColors.php 5 months ago Util.php 3 weeks ago
Util.php
225 lines
1 <?php
2 /**
3 * A utility class for Kirki.
4 *
5 * @package Kirki
6 * @category Core
7 * @author Themeum
8 * @copyright Copyright (c) 2023, Themeum
9 * @license https://opensource.org/licenses/MIT
10 * @since 3.0.9
11 */
12
13 namespace Kirki\Util;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Utility class.
21 */
22 class Util {
23
24 /**
25 * Fields containing variables.
26 *
27 * @static
28 * @access private
29 * @since 4.0
30 * @var array
31 */
32 private $variables_fields = [];
33
34 /**
35 * Constructor.
36 *
37 * @since 3.0.9
38 * @access public
39 */
40 public function __construct() {
41 add_filter( 'http_request_args', [ $this, 'http_request' ], 10, 2 );
42 add_action( 'kirki_field_init', [ $this, 'field_init_variables' ], 10, 2 );
43 }
44
45 /**
46 * Determine if Kirki is installed as a plugin.
47 *
48 * @static
49 * @access public
50 * @since 3.0.0
51 * @return bool
52 */
53 public static function is_plugin() {
54 $is_plugin = false;
55 if ( ! function_exists( 'get_plugins' ) ) {
56 require_once ABSPATH . 'wp-admin/includes/plugin.php'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
57 }
58
59 // Get all plugins.
60 $plugins = get_plugins();
61 $_plugin = '';
62 foreach ( $plugins as $plugin => $args ) {
63 if ( ! $is_plugin && isset( $args['Name'] ) && ( 'Kirki' === $args['Name'] || 'Kirki Toolkit' === $args['Name'] ) ) {
64 $is_plugin = true;
65 $_plugin = $plugin;
66 }
67 }
68
69 // No need to proceed any further if Kirki wasn't found in the list of plugins.
70 if ( ! $is_plugin ) {
71 return false;
72 }
73
74 // Make sure the is_plugins_loaded function is loaded.
75 include_once ABSPATH . 'wp-admin/includes/plugin.php'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
76
77 // Extra logic in case the plugin is installed but not activated.
78 if ( $_plugin && is_plugin_inactive( $_plugin ) ) {
79 return false;
80 }
81 return $is_plugin;
82 }
83
84 /**
85 * Add fields with variables to self::$variables_fields.
86 *
87 * @access public
88 * @since 4.0
89 * @param array $args The field args.
90 * @param Object $object The field object.
91 * @return void
92 */
93 public function field_init_variables( $args, $object ) {
94 if ( isset( $args['variables'] ) ) {
95 self::$variables_fields[] = $args;
96 }
97 }
98
99 /**
100 * Build the variables.
101 *
102 * @static
103 * @access public
104 * @since 3.0.9
105 * @return array Formatted as array( 'variable-name' => value ).
106 */
107 public static function get_variables() {
108
109 $variables = [];
110 $fields = self::$variables_fields;
111
112 /**
113 * Compatibility with Kirki v3.x API.
114 * If the Kirki class exists, check for fields inside it
115 * and add them to our fields array.
116 */
117 if ( class_exists( '\Kirki\Compatibility\Kirki' ) ) {
118 $fields = array_merge( \Kirki\Compatibility\Kirki::$fields, $fields );
119 }
120
121 // Loop through all fields.
122 foreach ( $fields as $field ) {
123
124 // Skip if this field doesn't have variables.
125 if ( ! isset( $field['variables'] ) || ! $field['variables'] || empty( $field['variables'] ) ) {
126 continue;
127 }
128
129 $option_type = ( isset( $field['option_type'] ) ) ? $field['option_type'] : 'theme_mod';
130 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
131 $value = apply_filters( 'kirki_get_value', get_theme_mod( $field['settings'], $default ), $field['settings'], $default, $option_type );
132
133 // Loop through the array of variables.
134 foreach ( $field['variables'] as $field_variable ) {
135
136 // Is the variable ['name'] defined? If yes, then we can proceed.
137 if ( isset( $field_variable['name'] ) ) {
138
139 // Do we have a callback function defined? If not then set $variable_callback to false.
140 $variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false;
141
142 /**
143 * If we have a variable_callback defined then get the value of the option
144 * and run it through the callback function.
145 * If no callback is defined (false) then just get the value.
146 */
147 $variables[ $field_variable['name'] ] = $value;
148 if ( $variable_callback ) {
149 $variables[ $field_variable['name'] ] = call_user_func( $field_variable['callback'], $value );
150 }
151 }
152 }
153 }
154
155 // Pass the variables through a filter ('kirki_variable') and return the array of variables.
156 return apply_filters( 'kirki_variable', $variables );
157 }
158
159 /**
160 * HTTP Request injection.
161 *
162 * @access public
163 * @since 3.0.0
164 * @param array $request The request params.
165 * @param string $url The request URL.
166 * @return array
167 */
168 public function http_request( $request = [], $url = '' ) {
169
170 // Early exit if installed as a plugin or not a request to wordpress.org,
171 // or finally if we don't have everything we need.
172 if (
173 self::is_plugin() ||
174 false === strpos( $url, 'wordpress.org' ) || (
175 ! isset( $request['body'] ) ||
176 ! isset( $request['body']['plugins'] ) ||
177 ! isset( $request['body']['translations'] ) ||
178 ! isset( $request['body']['locale'] ) ||
179 ! isset( $request['body']['all'] )
180 )
181 ) {
182 return $request;
183 }
184
185 $plugins = json_decode( $request['body']['plugins'], true );
186 if ( ! isset( $plugins['plugins'] ) ) {
187 return $request;
188 }
189 $exists = false;
190 foreach ( $plugins['plugins'] as $plugin ) {
191 if ( isset( $plugin['Name'] ) && 'Kirki Toolkit' === $plugin['Name'] ) {
192 $exists = true;
193 }
194 }
195 // Inject data.
196 if ( ! $exists && defined( 'KIRKI_PLUGIN_FILE' ) ) {
197 $plugins['plugins']['kirki/kirki.php'] = get_plugin_data( KIRKI_PLUGIN_FILE );
198 }
199 $request['body']['plugins'] = wp_json_encode( $plugins );
200 return $request;
201 }
202
203 /**
204 * Returns the $wp_version.
205 *
206 * @static
207 * @access public
208 * @since 3.0.12
209 * @param string $context Use 'minor' or 'major'.
210 * @return int|string Returns integer when getting the 'major' version.
211 * Returns string when getting the 'minor' version.
212 */
213 public static function get_wp_version( $context = 'minor' ) {
214 global $wp_version;
215
216 // We only need the major version.
217 if ( 'major' === $context ) {
218 $version_parts = explode( '.', $wp_version );
219 return $version_parts[0];
220 }
221
222 return $wp_version;
223 }
224 }
225