Completed
Pull Request — master (#1653)
by Aristeides
03:41 queued 01:33
created

Kirki_Helper::get_image_from_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Helper methods
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       1.0
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
/**
19
 * A simple object containing static methods.
20
 */
21
class Kirki_Helper {
22
23
	/**
24
	 * Recursive replace in arrays.
25
	 *
26
	 * @static
27
	 * @access public
28
	 * @param array $array The first array.
29
	 * @param array $array1 The second array.
30
	 * @return mixed
31
	 */
32
	public static function array_replace_recursive( $array, $array1 ) {
33
		if ( function_exists( 'array_replace_recursive' ) ) {
34
			return array_replace_recursive( $array, $array1 );
35
		}
36
37
		// Handle the arguments, merge one by one.
38
		$args  = func_get_args();
39
		$array = $args[0];
40
		if ( ! is_array( $array ) ) {
41
			return $array;
42
		}
43
		$count = count( $args );
44
		for ( $i = 1; $i < $count; $i++ ) {
45
			if ( is_array( $args[ $i ] ) ) {
46
				$array = self::recurse( $array, $args[ $i ] );
47
			}
48
		}
49
		return $array;
50
	}
51
52
	/**
53
	 * Helper method to be used from the array_replace_recursive method.
54
	 *
55
	 * @static
56
	 * @access public
57
	 * @param array $array The first array.
58
	 * @param array $array1 The second array.
59
	 * @return array
60
	 */
61
	public static function recurse( $array, $array1 ) {
62
		foreach ( $array1 as $key => $value ) {
63
			// Create new key in $array, if it is empty or not an array.
64
			if ( ! isset( $array[ $key ] ) || ( isset( $array[ $key ] ) && ! is_array( $array[ $key ] ) ) ) {
65
				$array[ $key ] = array();
66
			}
67
68
			// Overwrite the value in the base array.
69
			if ( is_array( $value ) ) {
70
				$value = self::recurse( $array[ $key ], $value );
71
			}
72
			$array[ $key ] = $value;
73
		}
74
		return $array;
75
	}
76
77
	/**
78
	 * Initialize the WP_Filesystem
79
	 *
80
	 * @static
81
	 * @access public
82
	 * @return object WP_Filesystem
83
	 */
84
	public static function init_filesystem() {
85
		global $wp_filesystem;
86
		if ( empty( $wp_filesystem ) ) {
87
			require_once( ABSPATH . '/wp-admin/includes/file.php' );
88
			WP_Filesystem();
89
		}
90
		return $wp_filesystem;
91
	}
92
93
	/**
94
	 * Returns the attachment object
95
	 *
96
	 * @static
97
	 * @access public
98
	 * @see https://pippinsplugins.com/retrieve-attachment-id-from-image-url/
99
	 * @param string $url URL to the image.
100
	 * @return int|string Numeric ID of the attachement.
101
	 */
102
	public static function get_image_id( $url ) {
103
		global $wpdb;
104
		if ( empty( $url ) ) {
105
			return 0;
106
		}
107
108
		$attachment = wp_cache_get( 'kirki_image_id_' . md5( $url ), null );
109
		if ( false === $attachment ) {
110
			$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid = %s;", $url ) );
111
			wp_cache_add( 'kirki_image_id_' . md5( $url ), $attachment, null );
112
		}
113
114
		if ( ! empty( $attachment ) ) {
115
			return $attachment[0];
116
		}
117
		return 0;
118
	}
119
120
	/**
121
	 * Returns an array of the attachment's properties.
122
	 *
123
	 * @param string $url URL to the image.
124
	 * @return array
125
	 */
126
	public static function get_image_from_url( $url ) {
127
128
		$image_id = self::get_image_id( $url );
129
		$image    = wp_get_attachment_image_src( $image_id, 'full' );
130
131
		return array(
132
			'url'       => $image[0],
133
			'width'     => $image[1],
134
			'height'    => $image[2],
135
			'thumbnail' => $image[3],
136
		);
137
138
	}
139
140
	/**
141
	 * Get an array of posts.
142
	 *
143
	 * @static
144
	 * @access public
145
	 * @param array $args Define arguments for the get_posts function.
146
	 * @return array
147
	 */
148
	public static function get_posts( $args ) {
149
150
		if ( is_string( $args ) ) {
151
			$args = add_query_arg(
152
				array(
153
					'suppress_filters' => false,
154
				)
155
			);
156
		} elseif ( is_array( $args ) && ! isset( $args['suppress_filters'] ) ) {
157
			$args['suppress_filters'] = false;
158
		}
159
160
		// Get the posts.
161
		$posts = get_posts( $args );
162
163
		// Properly format the array.
164
		$items = array();
165
		foreach ( $posts as $post ) {
166
			$items[ $post->ID ] = $post->post_title;
167
		}
168
		wp_reset_postdata();
169
170
		return $items;
171
172
	}
173
174
	/**
175
	 * Get an array of publicly-querable taxonomies.
176
	 *
177
	 * @static
178
	 * @access public
179
	 * @return array
180
	 */
181
	public static function get_taxonomies() {
182
183
		$items = array();
184
185
		// Get the taxonomies.
186
		$taxonomies = get_taxonomies(
187
			array(
188
				'public' => true,
189
			)
190
		);
191
192
		// Build the array.
193
		foreach ( $taxonomies as $taxonomy ) {
194
			$id           = $taxonomy;
195
			$taxonomy     = get_taxonomy( $taxonomy );
196
			$items[ $id ] = $taxonomy->labels->name;
197
		}
198
199
		return $items;
200
201
	}
202
203
	/**
204
	 * Get an array of publicly-querable post-types.
205
	 *
206
	 * @static
207
	 * @access public
208
	 * @return array
209
	 */
210
	public static function get_post_types() {
211
212
		$items = array();
213
214
		// Get the post types.
215
		$post_types = get_post_types(
216
			array(
217
				'public' => true,
218
			), 'objects'
219
		);
220
221
		// Build the array.
222
		foreach ( $post_types as $post_type ) {
223
			$items[ $post_type->name ] = $post_type->labels->name;
224
		}
225
226
		return $items;
227
228
	}
229
230
	/**
231
	 * Get an array of terms from a taxonomy
232
	 *
233
	 * @static
234
	 * @access public
235
	 * @param string|array $taxonomies See https://developer.wordpress.org/reference/functions/get_terms/ for details.
236
	 * @return array
237
	 */
238
	public static function get_terms( $taxonomies ) {
239
240
		$items = array();
241
242
		// Get the post types.
243
		$terms = get_terms( $taxonomies );
244
245
		// Build the array.
246
		foreach ( $terms as $term ) {
247
			$items[ $term->term_id ] = $term->name;
248
		}
249
250
		return $items;
251
252
	}
253
254
	/**
255
	 * Gets an array of material-design colors.
256
	 *
257
	 * @static
258
	 * @access public
259
	 * @param string $context Allows us to get subsets of the palette.
260
	 * @return array
261
	 */
262
	public static function get_material_design_colors( $context = 'primary' ) {
263
264
		$colors = array(
265
			'primary'     => array( '#FFFFFF', '#000000', '#F44336', '#E91E63', '#9C27B0', '#673AB7', '#3F51B5', '#2196F3', '#03A9F4', '#00BCD4', '#009688', '#4CAF50', '#8BC34A', '#CDDC39', '#FFEB3B', '#FFC107', '#FF9800', '#FF5722', '#795548', '#9E9E9E', '#607D8B' ),
266
			'red'         => array( '#FFEBEE', '#FFCDD2', '#EF9A9A', '#E57373', '#EF5350', '#F44336', '#E53935', '#D32F2F', '#C62828', '#B71C1C', '#FF8A80', '#FF5252', '#FF1744', '#D50000' ),
267
			'pink'        => array( '#FCE4EC', '#F8BBD0', '#F48FB1', '#F06292', '#EC407A', '#E91E63', '#D81B60', '#C2185B', '#AD1457', '#880E4F', '#FF80AB', '#FF4081', '#F50057', '#C51162' ),
268
			'purple'      => array( '#F3E5F5', '#E1BEE7', '#CE93D8', '#BA68C8', '#AB47BC', '#9C27B0', '#8E24AA', '#7B1FA2', '#6A1B9A', '#4A148C', '#EA80FC', '#E040FB', '#D500F9', '#AA00FF' ),
269
			'deep-purple' => array( '#EDE7F6', '#D1C4E9', '#B39DDB', '#9575CD', '#7E57C2', '#673AB7', '#5E35B1', '#512DA8', '#4527A0', '#311B92', '#B388FF', '#7C4DFF', '#651FFF', '#6200EA' ),
270
			'indigo'      => array( '#E8EAF6', '#C5CAE9', '#9FA8DA', '#7986CB', '#5C6BC0', '#3F51B5', '#3949AB', '#303F9F', '#283593', '#1A237E', '#8C9EFF', '#536DFE', '#3D5AFE', '#304FFE' ),
271
			'blue'        => array( '#E3F2FD', '#BBDEFB', '#90CAF9', '#64B5F6', '#42A5F5', '#2196F3', '#1E88E5', '#1976D2', '#1565C0', '#0D47A1', '#82B1FF', '#448AFF', '#2979FF', '#2962FF' ),
272
			'light_blue'  => array( '#E1F5FE', '#B3E5FC', '#81D4fA', '#4fC3F7', '#29B6FC', '#03A9F4', '#039BE5', '#0288D1', '#0277BD', '#01579B', '#80D8FF', '#40C4FF', '#00B0FF', '#0091EA' ),
273
			'cyan'        => array( '#E0F7FA', '#B2EBF2', '#80DEEA', '#4DD0E1', '#26C6DA', '#00BCD4', '#00ACC1', '#0097A7', '#00838F', '#006064', '#84FFFF', '#18FFFF', '#00E5FF', '#00B8D4' ),
274
			'teal'        => array( '#E0F2F1', '#B2DFDB', '#80CBC4', '#4DB6AC', '#26A69A', '#009688', '#00897B', '#00796B', '#00695C', '#004D40', '#A7FFEB', '#64FFDA', '#1DE9B6', '#00BFA5' ),
275
			'green'       => array( '#E8F5E9', '#C8E6C9', '#A5D6A7', '#81C784', '#66BB6A', '#4CAF50', '#43A047', '#388E3C', '#2E7D32', '#1B5E20', '#B9F6CA', '#69F0AE', '#00E676', '#00C853' ),
276
			'light-green' => array( '#F1F8E9', '#DCEDC8', '#C5E1A5', '#AED581', '#9CCC65', '#8BC34A', '#7CB342', '#689F38', '#558B2F', '#33691E', '#CCFF90', '#B2FF59', '#76FF03', '#64DD17' ),
277
			'lime'        => array( '#F9FBE7', '#F0F4C3', '#E6EE9C', '#DCE775', '#D4E157', '#CDDC39', '#C0CA33', '#A4B42B', '#9E9D24', '#827717', '#F4FF81', '#EEFF41', '#C6FF00', '#AEEA00' ),
278
			'yellow'      => array( '#FFFDE7', '#FFF9C4', '#FFF590', '#FFF176', '#FFEE58', '#FFEB3B', '#FDD835', '#FBC02D', '#F9A825', '#F57F17', '#FFFF82', '#FFFF00', '#FFEA00', '#FFD600' ),
279
			'amber'       => array( '#FFF8E1', '#FFECB3', '#FFE082', '#FFD54F', '#FFCA28', '#FFC107', '#FFB300', '#FFA000', '#FF8F00', '#FF6F00', '#FFE57F', '#FFD740', '#FFC400', '#FFAB00' ),
280
			'orange'      => array( '#FFF3E0', '#FFE0B2', '#FFCC80', '#FFB74D', '#FFA726', '#FF9800', '#FB8C00', '#F57C00', '#EF6C00', '#E65100', '#FFD180', '#FFAB40', '#FF9100', '#FF6D00' ),
281
			'deep-orange' => array( '#FBE9A7', '#FFCCBC', '#FFAB91', '#FF8A65', '#FF7043', '#FF5722', '#F4511E', '#E64A19', '#D84315', '#BF360C', '#FF9E80', '#FF6E40', '#FF3D00', '#DD2600' ),
282
			'brown'       => array( '#EFEBE9', '#D7CCC8', '#BCAAA4', '#A1887F', '#8D6E63', '#795548', '#6D4C41', '#5D4037', '#4E342E', '#3E2723' ),
283
			'grey'        => array( '#FAFAFA', '#F5F5F5', '#EEEEEE', '#E0E0E0', '#BDBDBD', '#9E9E9E', '#757575', '#616161', '#424242', '#212121', '#000000', '#ffffff' ),
284
			'blue-grey'   => array( '#ECEFF1', '#CFD8DC', '#B0BBC5', '#90A4AE', '#78909C', '#607D8B', '#546E7A', '#455A64', '#37474F', '#263238' ),
285
		);
286
287
		switch ( $context ) {
288
289
			case '50':
290
			case '100':
291
			case '200':
292
			case '300':
293
			case '400':
294
			case '500':
295
			case '600':
296
			case '700':
297
			case '800':
298
			case '900':
299
			case 'A100':
300
			case 'A200':
301
			case 'A400':
302
			case 'A700':
303
				$key = absint( $context ) / 100;
304
				if ( 'A100' === $context ) {
305
					$key = 10;
306
					unset( $colors['grey'] );
307
				} elseif ( 'A200' === $context ) {
308
					$key = 11;
309
					unset( $colors['grey'] );
310
				} elseif ( 'A400' === $context ) {
311
					$key = 12;
312
					unset( $colors['grey'] );
313
				} elseif ( 'A700' === $context ) {
314
					$key = 13;
315
					unset( $colors['grey'] );
316
				}
317
				unset( $colors['primary'] );
318
				$position_colors = array();
319
				foreach ( $colors as $color_family ) {
320
					if ( isset( $color_family[ $key ] ) ) {
321
						$position_colors[] = $color_family[ $key ];
322
					}
323
				}
324
				return $position_colors;
325
			case 'all':
326
				unset( $colors['primary'] );
327
				$all_colors = array();
328
				foreach ( $colors as $color_family ) {
329
					foreach ( $color_family as $color ) {
330
						$all_colors[] = $color;
331
					}
332
				}
333
				return $all_colors;
334
			case 'primary':
335
				return $colors['primary'];
336
			default:
337
				if ( isset( $colors[ $context ] ) ) {
338
					return $colors[ $context ];
339
				}
340
				return $colors['primary'];
341
		} // End switch().
342
	}
343
344
	/**
345
	 * Get an array of all available dashicons.
346
	 *
347
	 * @static
348
	 * @access public
349
	 * @return array
350
	 */
351
	public static function get_dashicons() {
352
353
		return array(
354
			'admin-menu'     => array( 'menu', 'admin-site', 'dashboard', 'admin-post', 'admin-media', 'admin-links', 'admin-page', 'admin-comments', 'admin-appearance', 'admin-plugins', 'admin-users', 'admin-tools', 'admin-settings', 'admin-network', 'admin-home', 'admin-generic', 'admin-collapse', 'filter', 'admin-customizer', 'admin-multisite' ),
355
			'welcome-screen' => array( 'welcome-write-blog', 'welcome-add-page', 'welcome-view-site', 'welcome-widgets-menus', 'welcome-comments', 'welcome-learn-more' ),
356
			'post-formats'   => array( 'format-aside', 'format-image', 'format-gallery', 'format-video', 'format-status', 'format-quote', 'format-chat', 'format-audio', 'camera', 'images-alt', 'images-alt2', 'video-alt', 'video-alt2', 'video-alt3' ),
357
			'media'          => array( 'media-archive', 'media-audio', 'media-code', 'media-default', 'media-document', 'media-interactive', 'media-spreadsheet', 'media-text', 'media-video', 'playlist-audio', 'playlist-video', 'controls-play', 'controls-pause', 'controls-forward', 'controls-skipforward', 'controls-back', 'controls-skipback', 'controls-repeat', 'controls-volumeon', 'controls-volumeoff' ),
358
			'image-editing'  => array( 'image-crop', 'image-rotate', 'image-rotate-left', 'image-rotate-right', 'image-flip-vertical', 'image-flip-horizontal', 'image-filter', 'undo', 'redo' ),
359
			'tinymce'        => array( 'editor-bold', 'editor-italic', 'editor-ul', 'editor-ol', 'editor-quote', 'editor-alignleft', 'editor-aligncenter', 'editor-alignright', 'editor-insertmore', 'editor-spellcheck', 'editor-expand', 'editor-contract', 'editor-kitchensink', 'editor-underline', 'editor-justify', 'editor-textcolor', 'editor-paste-word', 'editor-paste-text', 'editor-removeformatting', 'editor-video', 'editor-customchar', 'editor-outdent', 'editor-indent', 'editor-help', 'editor-strikethrough', 'editor-unlink', 'editor-rtl', 'editor-break', 'editor-code', 'editor-paragraph', 'editor-table' ),
360
			'posts'          => array( 'align-left', 'align-right', 'align-center', 'align-none', 'lock', 'unlock', 'calendar', 'calendar-alt', 'visibility', 'hidden', 'post-status', 'edit', 'trash', 'sticky' ),
361
			'sorting'        => array( 'external', 'arrow-up', 'arrow-down', 'arrow-right', 'arrow-left', 'arrow-up-alt', 'arrow-down-alt', 'arrow-right-alt', 'arrow-left-alt', 'arrow-up-alt2', 'arrow-down-alt2', 'arrow-right-alt2', 'arrow-left-alt2', 'sort', 'leftright', 'randomize', 'list-view', 'exerpt-view', 'grid-view' ),
362
			'social'         => array( 'share', 'share-alt', 'share-alt2', 'twitter', 'rss', 'email', 'email-alt', 'facebook', 'facebook-alt', 'googleplus', 'networking' ),
363
			'wordpress_org'  => array( 'hammer', 'art', 'migrate', 'performance', 'universal-access', 'universal-access-alt', 'tickets', 'nametag', 'clipboard', 'heart', 'megaphone', 'schedule' ),
364
			'products'       => array( 'wordpress', 'wordpress-alt', 'pressthis', 'update', 'screenoptions', 'info', 'cart', 'feedback', 'cloud', 'translation' ),
365
			'taxonomies'     => array( 'tag', 'category' ),
366
			'widgets'        => array( 'archive', 'tagcloud', 'text' ),
367
			'notifications'  => array( 'yes', 'no', 'no-alt', 'plus', 'plus-alt', 'minus', 'dismiss', 'marker', 'star-filled', 'star-half', 'star-empty', 'flag', 'warning' ),
368
			'misc'           => array( 'location', 'location-alt', 'vault', 'shield', 'shield-alt', 'sos', 'search', 'slides', 'analytics', 'chart-pie', 'chart-bar', 'chart-line', 'chart-area', 'groups', 'businessman', 'id', 'id-alt', 'products', 'awards', 'forms', 'testimonial', 'portfolio', 'book', 'book-alt', 'download', 'upload', 'backup', 'clock', 'lightbulb', 'microphone', 'desktop', 'tablet', 'smartphone', 'phone', 'index-card', 'carrot', 'building', 'store', 'album', 'palmtree', 'tickets-alt', 'money', 'smiley', 'thumbs-up', 'thumbs-down', 'layout' ),
369
		);
370
371
	}
372
373
	/**
374
	 * Compares the 2 values given the condition
375
	 *
376
	 * @param mixed  $value1   The 1st value in the comparison.
377
	 * @param mixed  $value2   The 2nd value in the comparison.
378
	 * @param string $operator The operator we'll use for the comparison.
379
	 * @return boolean whether The comparison has succeded (true) or failed (false).
380
	 */
381
	public static function compare_values( $value1, $value2, $operator ) {
382
		if ( '===' === $operator ) {
383
			$show = ( $value1 === $value2 ) ? true : false;
384
		} elseif ( '!==' === $operator ) {
385
			$show = ( $value1 !== $value2 ) ? true : false;
386
		} elseif ( '!=' === $operator || 'not equal' === $operator ) {
387
			$show = ( $value1 != $value2 ) ? true : false;
388
		} elseif ( '>=' === $operator || 'greater or equal' === $operator || 'equal or greater' === $operator ) {
389
			$show = ( $value2 >= $value1 ) ? true : false;
390
		} elseif ( '<=' === $operator || 'smaller or equal' === $operator || 'equal or smaller' === $operator ) {
391
			$show = ( $value2 <= $value1 ) ? true : false;
392
		} elseif ( '>' === $operator || 'greater' === $operator ) {
393
			$show = ( $value2 > $value1 ) ? true : false;
394
		} elseif ( '<' === $operator || 'smaller' === $operator ) {
395
			$show = ( $value2 < $value1 ) ? true : false;
396
		} elseif ( 'contains' === $operator || 'in' === $operator ) {
397
			if ( is_array( $value1 ) && ! is_array( $value2 ) ) {
398
				$show = ( ! in_array( $value2, $value1, true ) ) ? false : $show;
0 ignored issues
show
Bug introduced by
The variable $show seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
399
			} elseif ( is_array( $value2 ) && ! is_array( $value1 ) ) {
400
				$show = ( ! in_array( $value1, $value2, true ) ) ? false : $show;
0 ignored issues
show
Bug introduced by
The variable $show seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
401
			} elseif ( false === strrpos( $value1, $value2 ) && false === strpos( $value2, $value1 ) ) {
402
				$show = false;
403
			}
404
		} else {
405
			$show = ( $value1 == $value2 ) ? true : false;
406
		}
407
		return ( isset( $show ) ) ? $show : true;
408
	}
409
}
410