Completed
Push — develop ( 2a22fb...0feec4 )
by Aristeides
03:44
created

core/class-kirki-util.php (1 issue)

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'] );
0 ignored issues
show
The type Kirki_Values was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
	 * Returns the $wp_version.
161
	 *
162
	 * @static
163
	 * @access public
164
	 * @since 3.0.12
165
	 * @param string  $context      Use 'minor' or 'major'.
166
	 * @param boolean $only_numeric Set to true if you want to skip the alpha/beta etc parts.
167
	 * @return int|float|string     Returns integer when getting the 'major' version.
168
	 *                              Returns float when getting the 'minor' version with $only_numeric set to true.
169
	 *                              Returns string when getting the 'minor' version with $only_numeric set to false.
170
	 */
171
	public static function get_wp_version( $context = 'minor', $only_numeric = true ) {
172
		global $wp_version;
173
174
		// We only need the major version.
175
		if ( 'major' === $context ) {
176
			$version_parts = explode( '.', $wp_version );
177
			return ( $only_numeric ) ? absint( $version_parts[0] ) : $version_parts[0];
178
		}
179
180
		// If we got this far, we want the full monty.
181
		if ( $only_numeric ) {
182
			// Get the numeric part of the version without any beta, alpha etc parts.
183
			if ( false !== strpos( $wp_version, '-' ) ) {
184
				// We're on a dev version.
185
				$version_parts = explode( '-', $wp_version );
186
				return floatval( $version_parts[0] );
187
			}
188
			return floatval( $wp_version );
189
		}
190
		return $wp_version;
191
	}
192
}
193