Completed
Push — develop ( 37e01c...d1218e )
by Aristeides
02:48
created

Kirki_Helper::array_replace_recursive()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 2
dl 0
loc 19
rs 8.8571
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
	public static function init_filesystem() {
81
		global $wp_filesystem;
82
		if ( empty( $wp_filesystem ) ) {
83
			require_once( ABSPATH . '/wp-admin/includes/file.php' );
84
			WP_Filesystem();
85
		}
86
	}
87
88
	/**
89
	 * Returns the attachment object
90
	 *
91
	 * @static
92
	 * @access public
93
	 * @see https://pippinsplugins.com/retrieve-attachment-id-from-image-url/
94
	 * @param string $url URL to the image.
95
	 * @return int|string Numeric ID of the attachement.
96
	 */
97
	public static function get_image_id( $url ) {
98
		global $wpdb;
99
		if ( empty( $url ) ) {
100
			return 0;
101
		}
102
103
		$attachment = wp_cache_get( 'kirki_image_id_' . md5( $url ), null );
104
		if ( false === $attachment ) {
105
			$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid = '%s';", $url ) );
106
			wp_cache_add( 'kirki_image_id_' . md5( $url ), $attachment, null );
107
		}
108
109
		if ( ! empty( $attachment ) ) {
110
			return $attachment[0];
111
		}
112
		return 0;
113
	}
114
115
	/**
116
	 * Returns an array of the attachment's properties.
117
	 *
118
	 * @param string $url URL to the image.
119
	 * @return array
120
	 */
121
	public static function get_image_from_url( $url ) {
122
123
		$image_id = self::get_image_id( $url );
124
		$image    = wp_get_attachment_image_src( $image_id, 'full' );
125
126
		return array(
127
			'url'       => $image[0],
128
			'width'     => $image[1],
129
			'height'    => $image[2],
130
			'thumbnail' => $image[3],
131
		);
132
133
	}
134
135
	/**
136
	 * Get an array of posts.
137
	 *
138
	 * @static
139
	 * @access public
140
	 * @param array $args Define arguments for the get_posts function.
141
	 * @return array
142
	 */
143
	public static function get_posts( $args ) {
144
145
		if ( is_string( $args ) ) {
146
			$args = add_query_arg( array(
147
				'suppress_filters' => false,
148
			) );
149
		} elseif ( is_array( $args ) && ! isset( $args['suppress_filters'] ) ) {
150
			$args['suppress_filters'] = false;
151
		}
152
153
		// Get the posts.
154
		$posts = get_posts( $args );
155
156
		// Properly format the array.
157
		$items = array();
158
		foreach ( $posts as $post ) {
159
			$items[ $post->ID ] = $post->post_title;
160
		}
161
		wp_reset_postdata();
162
163
		return $items;
164
165
	}
166
167
	/**
168
	 * Get an array of publicly-querable taxonomies.
169
	 *
170
	 * @static
171
	 * @access public
172
	 * @return array
173
	 */
174
	public static function get_taxonomies() {
175
176
		$items = array();
177
178
		// Get the taxonomies.
179
		$taxonomies = get_taxonomies( array(
180
			'public' => true,
181
		) );
182
183
		// Build the array.
184
		foreach ( $taxonomies as $taxonomy ) {
185
			$id           = $taxonomy;
186
			$taxonomy     = get_taxonomy( $taxonomy );
187
			$items[ $id ] = $taxonomy->labels->name;
188
		}
189
190
		return $items;
191
192
	}
193
194
	/**
195
	 * Get an array of publicly-querable post-types.
196
	 *
197
	 * @static
198
	 * @access public
199
	 * @return array
200
	 */
201
	public static function get_post_types() {
202
203
		$items = array();
204
205
		// Get the post types.
206
		$post_types = get_post_types( array(
207
			'public' => true,
208
		), 'objects' );
209
210
		// Build the array.
211
		foreach ( $post_types as $post_type ) {
212
			$items[ $post_type->name ] = $post_type->labels->name;
213
		}
214
215
		return $items;
216
217
	}
218
219
	/**
220
	 * Get an array of terms from a taxonomy
221
	 *
222
	 * @static
223
	 * @access public
224
	 * @param string|array $taxonomies See https://developer.wordpress.org/reference/functions/get_terms/ for details.
225
	 * @return array
226
	 */
227
	public static function get_terms( $taxonomies ) {
228
229
		$items = array();
230
231
		// Get the post types.
232
		$terms = get_terms( $taxonomies );
233
234
		// Build the array.
235
		foreach ( $terms as $term ) {
236
			$items[ $term->term_id ] = $term->name;
237
		}
238
239
		return $items;
240
241
	}
242
243
	/**
244
	 * Gets an array of material-design colors.
245
	 *
246
	 * @static
247
	 * @access public
248
	 * @param string $context Allows us to get subsets of the palette.
249
	 * @return array
250
	 */
251
	public static function get_material_design_colors( $context = 'primary' ) {
252
253
		$colors = array(
254
			'primary'     => array( '#FFFFFF', '#000000', '#F44336', '#E91E63', '#9C27B0', '#673AB7', '#3F51B5', '#2196F3', '#03A9F4', '#00BCD4', '#009688', '#4CAF50', '#8BC34A', '#CDDC39', '#FFEB3B', '#FFC107', '#FF9800', '#FF5722', '#795548', '#9E9E9E', '#607D8B' ),
255
			'red'         => array( '#FFEBEE', '#FFCDD2', '#EF9A9A', '#E57373', '#EF5350', '#F44336', '#E53935', '#D32F2F', '#C62828', '#B71C1C', '#FF8A80', '#FF5252', '#FF1744', '#D50000' ),
256
			'pink'        => array( '#FCE4EC', '#F8BBD0', '#F48FB1', '#F06292', '#EC407A', '#E91E63', '#D81B60', '#C2185B', '#AD1457', '#880E4F', '#FF80AB', '#FF4081', '#F50057', '#C51162' ),
257
			'purple'      => array( '#F3E5F5', '#E1BEE7', '#CE93D8', '#BA68C8', '#AB47BC', '#9C27B0', '#8E24AA', '#7B1FA2', '#6A1B9A', '#4A148C', '#EA80FC', '#E040FB', '#D500F9', '#AA00FF' ),
258
			'deep-purple' => array( '#EDE7F6', '#D1C4E9', '#B39DDB', '#9575CD', '#7E57C2', '#673AB7', '#5E35B1', '#512DA8', '#4527A0', '#311B92', '#B388FF', '#7C4DFF', '#651FFF', '#6200EA' ),
259
			'indigo'      => array( '#E8EAF6', '#C5CAE9', '#9FA8DA', '#7986CB', '#5C6BC0', '#3F51B5', '#3949AB', '#303F9F', '#283593', '#1A237E', '#8C9EFF', '#536DFE', '#3D5AFE', '#304FFE' ),
260
			'blue'        => array( '#E3F2FD', '#BBDEFB', '#90CAF9', '#64B5F6', '#42A5F5', '#2196F3', '#1E88E5', '#1976D2', '#1565C0', '#0D47A1', '#82B1FF', '#448AFF', '#2979FF', '#2962FF' ),
261
			'light_blue'  => array( '#E1F5FE', '#B3E5FC', '#81D4fA', '#4fC3F7', '#29B6FC', '#03A9F4', '#039BE5', '#0288D1', '#0277BD', '#01579B', '#80D8FF', '#40C4FF', '#00B0FF', '#0091EA' ),
262
			'cyan'        => array( '#E0F7FA', '#B2EBF2', '#80DEEA', '#4DD0E1', '#26C6DA', '#00BCD4', '#00ACC1', '#0097A7', '#00838F', '#006064', '#84FFFF', '#18FFFF', '#00E5FF', '#00B8D4' ),
263
			'teal'        => array( '#E0F2F1', '#B2DFDB', '#80CBC4', '#4DB6AC', '#26A69A', '#009688', '#00897B', '#00796B', '#00695C', '#004D40', '#A7FFEB', '#64FFDA', '#1DE9B6', '#00BFA5' ),
264
			'green'       => array( '#E8F5E9', '#C8E6C9', '#A5D6A7', '#81C784', '#66BB6A', '#4CAF50', '#43A047', '#388E3C', '#2E7D32', '#1B5E20', '#B9F6CA', '#69F0AE', '#00E676', '#00C853' ),
265
			'light-green' => array( '#F1F8E9', '#DCEDC8', '#C5E1A5', '#AED581', '#9CCC65', '#8BC34A', '#7CB342', '#689F38', '#558B2F', '#33691E', '#CCFF90', '#B2FF59', '#76FF03', '#64DD17' ),
266
			'lime'        => array( '#F9FBE7', '#F0F4C3', '#E6EE9C', '#DCE775', '#D4E157', '#CDDC39', '#C0CA33', '#A4B42B', '#9E9D24', '#827717', '#F4FF81', '#EEFF41', '#C6FF00', '#AEEA00' ),
267
			'yellow'      => array( '#FFFDE7', '#FFF9C4', '#FFF590', '#FFF176', '#FFEE58', '#FFEB3B', '#FDD835', '#FBC02D', '#F9A825', '#F57F17', '#FFFF82', '#FFFF00', '#FFEA00', '#FFD600' ),
268
			'amber'       => array( '#FFF8E1', '#FFECB3', '#FFE082', '#FFD54F', '#FFCA28', '#FFC107', '#FFB300', '#FFA000', '#FF8F00', '#FF6F00', '#FFE57F', '#FFD740', '#FFC400', '#FFAB00' ),
269
			'orange'      => array( '#FFF3E0', '#FFE0B2', '#FFCC80', '#FFB74D', '#FFA726', '#FF9800', '#FB8C00', '#F57C00', '#EF6C00', '#E65100', '#FFD180', '#FFAB40', '#FF9100', '#FF6D00' ),
270
			'deep-orange' => array( '#FBE9A7', '#FFCCBC', '#FFAB91', '#FF8A65', '#FF7043', '#FF5722', '#F4511E', '#E64A19', '#D84315', '#BF360C', '#FF9E80', '#FF6E40', '#FF3D00', '#DD2600' ),
271
			'brown'       => array( '#EFEBE9', '#D7CCC8', '#BCAAA4', '#A1887F', '#8D6E63', '#795548', '#6D4C41', '#5D4037', '#4E342E', '#3E2723' ),
272
			'grey'        => array( '#FAFAFA', '#F5F5F5', '#EEEEEE', '#E0E0E0', '#BDBDBD', '#9E9E9E', '#757575', '#616161', '#424242', '#212121', '#000000', '#ffffff' ),
273
			'blue-grey'   => array( '#ECEFF1', '#CFD8DC', '#B0BBC5', '#90A4AE', '#78909C', '#607D8B', '#546E7A', '#455A64', '#37474F', '#263238' ),
274
		);
275
276
		switch ( $context ) {
277
278
			case '50':
279
			case '100':
280
			case '200':
281
			case '300':
282
			case '400':
283
			case '500':
284
			case '600':
285
			case '700':
286
			case '800':
287
			case '900':
288
			case 'A100':
289
			case 'A200':
290
			case 'A400':
291
			case 'A700':
292
				if ( 'A100' === $context ) {
293
					$key = 10;
294
					unset( $colors['grey'] );
295
				} elseif ( 'A200' === $context ) {
296
					$key = 11;
297
					unset( $colors['grey'] );
298
				} elseif ( 'A400' === $context ) {
299
					$key = 12;
300
					unset( $colors['grey'] );
301
				} elseif ( 'A700' === $context ) {
302
					$key = 13;
303
					unset( $colors['grey'] );
304
				} else {
305
					$key = $context / 100;
306
				}
307
				unset( $colors['primary'] );
308
				$position_colors = array();
309
				foreach ( $colors as $color_family ) {
310
					if ( isset( $color_family[ $key ] ) ) {
311
						$position_colors[] = $color_family[ $key ];
312
					}
313
				}
314
				return $position_colors;
315
			case 'all':
316
				unset( $colors['primary'] );
317
				$all_colors = array();
318
				foreach ( $colors as $color_family ) {
319
					foreach ( $color_family as $color ) {
320
						$all_colors[] = $color;
321
					}
322
				}
323
				return $all_colors;
324
			case 'primary':
325
				return $colors['primary'];
326
			default:
327
				if ( isset( $colors[ $context ] ) ) {
328
					return $colors[ $context ];
329
				}
330
				return $colors['primary'];
331
		} // End switch().
332
	}
333
334
	/**
335
	 * Get an array of all available dashicons.
336
	 *
337
	 * @static
338
	 * @access public
339
	 * @return array
340
	 */
341
	public static function get_dashicons() {
342
343
		$admin_menu = array(
344
			'menu',
345
			'admin-site',
346
			'dashboard',
347
			'admin-post',
348
			'admin-media',
349
			'admin-links',
350
			'admin-page',
351
			'admin-comments',
352
			'admin-appearance',
353
			'admin-plugins',
354
			'admin-users',
355
			'admin-tools',
356
			'admin-settings',
357
			'admin-network',
358
			'admin-home',
359
			'admin-generic',
360
			'admin-collapse',
361
			'filter',
362
			'admin-customizer',
363
			'admin-multisite',
364
		);
365
366
		$welcome_screen = array(
367
			'welcome-write-blog',
368
			'welcome-add-page',
369
			'welcome-view-site',
370
			'welcome-widgets-menus',
371
			'welcome-comments',
372
			'welcome-learn-more',
373
		);
374
375
		$post_formats = array(
376
			'format-aside',
377
			'format-image',
378
			'format-gallery',
379
			'format-video',
380
			'format-status',
381
			'format-quote',
382
			'format-chat',
383
			'format-audio',
384
			'camera',
385
			'images-alt',
386
			'images-alt2',
387
			'video-alt',
388
			'video-alt2',
389
			'video-alt3',
390
		);
391
392
		$media = array(
393
			'media-archive',
394
			'media-audio',
395
			'media-code',
396
			'media-default',
397
			'media-document',
398
			'media-interactive',
399
			'media-spreadsheet',
400
			'media-text',
401
			'media-video',
402
			'playlist-audio',
403
			'playlist-video',
404
			'controls-play',
405
			'controls-pause',
406
			'controls-forward',
407
			'controls-skipforward',
408
			'controls-back',
409
			'controls-skipback',
410
			'controls-repeat',
411
			'controls-volumeon',
412
			'controls-volumeoff',
413
		);
414
415
		$image_editing = array(
416
			'image-crop',
417
			'image-rotate',
418
			'image-rotate-left',
419
			'image-rotate-right',
420
			'image-flip-vertical',
421
			'image-flip-horizontal',
422
			'image-filter',
423
			'undo',
424
			'redo',
425
		);
426
427
		$tinymce = array(
428
			'editor-bold',
429
			'editor-italic',
430
			'editor-ul',
431
			'editor-ol',
432
			'editor-quote',
433
			'editor-alignleft',
434
			'editor-aligncenter',
435
			'editor-alignright',
436
			'editor-insertmore',
437
			'editor-spellcheck',
438
			'editor-expand',
439
			'editor-contract',
440
			'editor-kitchensink',
441
			'editor-underline',
442
			'editor-justify',
443
			'editor-textcolor',
444
			'editor-paste-word',
445
			'editor-paste-text',
446
			'editor-removeformatting',
447
			'editor-video',
448
			'editor-customchar',
449
			'editor-outdent',
450
			'editor-indent',
451
			'editor-help',
452
			'editor-strikethrough',
453
			'editor-unlink',
454
			'editor-rtl',
455
			'editor-break',
456
			'editor-code',
457
			'editor-paragraph',
458
			'editor-table',
459
		);
460
461
		$posts = array(
462
			'align-left',
463
			'align-right',
464
			'align-center',
465
			'align-none',
466
			'lock',
467
			'unlock',
468
			'calendar',
469
			'calendar-alt',
470
			'visibility',
471
			'hidden',
472
			'post-status',
473
			'edit',
474
			'trash',
475
			'sticky',
476
		);
477
478
		$sorting = array(
479
			'external',
480
			'arrow-up',
481
			'arrow-down',
482
			'arrow-right',
483
			'arrow-left',
484
			'arrow-up-alt',
485
			'arrow-down-alt',
486
			'arrow-right-alt',
487
			'arrow-left-alt',
488
			'arrow-up-alt2',
489
			'arrow-down-alt2',
490
			'arrow-right-alt2',
491
			'arrow-left-alt2',
492
			'sort',
493
			'leftright',
494
			'randomize',
495
			'list-view',
496
			'exerpt-view',
497
			'grid-view',
498
		);
499
500
		$social = array(
501
			'share',
502
			'share-alt',
503
			'share-alt2',
504
			'twitter',
505
			'rss',
506
			'email',
507
			'email-alt',
508
			'facebook',
509
			'facebook-alt',
510
			'googleplus',
511
			'networking',
512
		);
513
514
		$wordpress_org = array(
515
			'hammer',
516
			'art',
517
			'migrate',
518
			'performance',
519
			'universal-access',
520
			'universal-access-alt',
521
			'tickets',
522
			'nametag',
523
			'clipboard',
524
			'heart',
525
			'megaphone',
526
			'schedule',
527
		);
528
529
		$products = array(
530
			'wordpress',
531
			'wordpress-alt',
532
			'pressthis',
533
			'update',
534
			'screenoptions',
535
			'info',
536
			'cart',
537
			'feedback',
538
			'cloud',
539
			'translation',
540
		);
541
542
		$taxonomies = array(
543
			'tag',
544
			'category',
545
		);
546
547
		$widgets = array(
548
			'archive',
549
			'tagcloud',
550
			'text',
551
		);
552
553
		$notifications = array(
554
			'yes',
555
			'no',
556
			'no-alt',
557
			'plus',
558
			'plus-alt',
559
			'minus',
560
			'dismiss',
561
			'marker',
562
			'star-filled',
563
			'star-half',
564
			'star-empty',
565
			'flag',
566
			'warning',
567
		);
568
569
		$misc = array(
570
			'location',
571
			'location-alt',
572
			'vault',
573
			'shield',
574
			'shield-alt',
575
			'sos',
576
			'search',
577
			'slides',
578
			'analytics',
579
			'chart-pie',
580
			'chart-bar',
581
			'chart-line',
582
			'chart-area',
583
			'groups',
584
			'businessman',
585
			'id',
586
			'id-alt',
587
			'products',
588
			'awards',
589
			'forms',
590
			'testimonial',
591
			'portfolio',
592
			'book',
593
			'book-alt',
594
			'download',
595
			'upload',
596
			'backup',
597
			'clock',
598
			'lightbulb',
599
			'microphone',
600
			'desktop',
601
			'tablet',
602
			'smartphone',
603
			'phone',
604
			'index-card',
605
			'carrot',
606
			'building',
607
			'store',
608
			'album',
609
			'palmtree',
610
			'tickets-alt',
611
			'money',
612
			'smiley',
613
			'thumbs-up',
614
			'thumbs-down',
615
			'layout',
616
		);
617
618
		return array(
619
			'admin-menu'     => $admin_menu,
620
			'welcome-screen' => $welcome_screen,
621
			'post-formats'   => $post_formats,
622
			'media'          => $media,
623
			'image-editing'  => $image_editing,
624
			'tinymce'        => $tinymce,
625
			'posts'          => $posts,
626
			'sorting'        => $sorting,
627
			'social'         => $social,
628
			'wordpress_org'  => $wordpress_org,
629
			'products'       => $products,
630
			'taxonomies'     => $taxonomies,
631
			'widgets'        => $widgets,
632
			'notifications'  => $notifications,
633
			'misc'           => $misc,
634
		);
635
636
	}
637
}
638