Completed
Push — develop ( f37854...ab6492 )
by Aristeides
02:13
created

Kirki_Helper::init_filesystem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
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) 2016, 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
		$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $url ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
100
		if ( ! is_array( $attachment ) || ! isset( $attachment[0] ) ) {
101
			return 0;
102
		}
103
		return $attachment[0];
104
	}
105
106
	/**
107
	 * Returns an array of the attachment's properties.
108
	 *
109
	 * @param string $url URL to the image.
110
	 * @return array
111
	 */
112
	public static function get_image_from_url( $url ) {
113
114
		$image_id = self::get_image_id( $url );
115
		$image    = wp_get_attachment_image_src( $image_id, 'full' );
116
117
		return array(
118
			'url'       => $image[0],
119
			'width'     => $image[1],
120
			'height'    => $image[2],
121
			'thumbnail' => $image[3],
122
		);
123
124
	}
125
126
	/**
127
	 * Get an array of posts.
128
	 *
129
	 * @static
130
	 * @access public
131
	 * @param array $args Define arguments for the get_posts function.
132
	 * @return array
133
	 */
134
	public static function get_posts( $args ) {
135
136
		// Get the posts.
137
		if ( ! isset( $args['suppress_filters'] ) ) {
138
			$args['suppress_filters'] = false;
139
		}
140
		$posts = get_posts( $args );
141
142
		// Properly format the array.
143
		$items = array();
144
		foreach ( $posts as $post ) {
145
			$items[ $post->ID ] = $post->post_title;
146
		}
147
148
		return $items;
149
150
	}
151
152
	/**
153
	 * Get an array of publicly-querable taxonomies.
154
	 *
155
	 * @static
156
	 * @access public
157
	 * @return array
158
	 */
159
	public static function get_taxonomies() {
160
161
		$items = array();
162
163
		// Get the taxonomies.
164
		$taxonomies = get_taxonomies( array( 'public' => true ) );
165
166
		// Build the array.
167
		foreach ( $taxonomies as $taxonomy ) {
168
			$id           = $taxonomy;
169
			$taxonomy     = get_taxonomy( $taxonomy );
170
			$items[ $id ] = $taxonomy->labels->name;
171
		}
172
173
		return $items;
174
175
	}
176
177
	/**
178
	 * Get an array of publicly-querable post-types.
179
	 *
180
	 * @static
181
	 * @access public
182
	 * @return array
183
	 */
184
	public static function get_post_types() {
185
186
		$items = array();
187
188
		// Get the post types.
189
		$post_types = get_post_types( array( 'public' => true ), 'objects' );
190
191
		// Build the array.
192
		foreach ( $post_types as $post_type ) {
193
			$items[ $post_type->name ] = $post_type->labels->name;
194
		}
195
196
		return $items;
197
198
	}
199
200
	/**
201
	 * Get an array of terms from a taxonomy
202
	 *
203
	 * @static
204
	 * @access public
205
	 * @param string|array $taxonomies See https://developer.wordpress.org/reference/functions/get_terms/ for details.
206
	 * @return array
207
	 */
208
	public static function get_terms( $taxonomies ) {
209
210
		$items = array();
211
212
		// Get the post types.
213
		$terms = get_terms( $taxonomies );
214
215
		// Build the array.
216
		foreach ( $terms as $term ) {
217
			$items[ $term->term_id ] = $term->name;
218
		}
219
220
		return $items;
221
222
	}
223
224
	/**
225
	 * Gets an array of material-design colors.
226
	 *
227
	 * @static
228
	 * @access public
229
	 * @param false|string $context Allows us to get subsets of the palette.
230
	 * @return array
231
	 */
232
	public static function get_material_design_colors( $context = false ) {
233
234
		$colors = array(
235
			'primary' => array(
236
				'#FFFFFF',
237
				'#000000',
238
				'#F44336',
239
				'#E91E63',
240
				'#9C27B0',
241
				'#673AB7',
242
				'#3F51B5',
243
				'#2196F3',
244
				'#03A9F4',
245
				'#00BCD4',
246
				'#009688',
247
				'#4CAF50',
248
				'#8BC34A',
249
				'#CDDC39',
250
				'#FFEB3B',
251
				'#FFC107',
252
				'#FF9800',
253
				'#FF5722',
254
				'#795548',
255
				'#9E9E9E',
256
				'#607D8B',
257
			),
258
			'red' => array(
259
				'#FFEBEE',
260
				'#FFCDD2',
261
				'#EF9A9A',
262
				'#E57373',
263
				'#EF5350',
264
				'#F44336',
265
				'#E53935',
266
				'#D32F2F',
267
				'#C62828',
268
				'#B71C1C',
269
				'#FF8A80',
270
				'#FF5252',
271
				'#FF1744',
272
				'#D50000',
273
			),
274
			'pink' => array(
275
				'#FCE4EC',
276
				'#F8BBD0',
277
				'#F48FB1',
278
				'#F06292',
279
				'#EC407A',
280
				'#E91E63',
281
				'#D81B60',
282
				'#C2185B',
283
				'#AD1457',
284
				'#880E4F',
285
				'#FF80AB',
286
				'#FF4081',
287
				'#F50057',
288
				'#C51162',
289
			),
290
			'purple' => array(
291
				'#F3E5F5',
292
				'#E1BEE7',
293
				'#CE93D8',
294
				'#BA68C8',
295
				'#AB47BC',
296
				'#9C27B0',
297
				'#8E24AA',
298
				'#7B1FA2',
299
				'#6A1B9A',
300
				'#4A148C',
301
				'#EA80FC',
302
				'#E040FB',
303
				'#D500F9',
304
				'#AA00FF',
305
			),
306
			'deep-purple' => array(
307
				'#EDE7F6',
308
				'#D1C4E9',
309
				'#B39DDB',
310
				'#9575CD',
311
				'#7E57C2',
312
				'#673AB7',
313
				'#5E35B1',
314
				'#512DA8',
315
				'#4527A0',
316
				'#311B92',
317
				'#B388FF',
318
				'#7C4DFF',
319
				'#651FFF',
320
				'#6200EA',
321
			),
322
			'indigo' => array(
323
				'#E8EAF6',
324
				'#C5CAE9',
325
				'#9FA8DA',
326
				'#7986CB',
327
				'#5C6BC0',
328
				'#3F51B5',
329
				'#3949AB',
330
				'#303F9F',
331
				'#283593',
332
				'#1A237E',
333
				'#8C9EFF',
334
				'#536DFE',
335
				'#3D5AFE',
336
				'#304FFE',
337
			),
338
			'blue' => array(
339
				'#E3F2FD',
340
				'#BBDEFB',
341
				'#90CAF9',
342
				'#64B5F6',
343
				'#42A5F5',
344
				'#2196F3',
345
				'#1E88E5',
346
				'#1976D2',
347
				'#1565C0',
348
				'#0D47A1',
349
				'#82B1FF',
350
				'#448AFF',
351
				'#2979FF',
352
				'#2962FF',
353
			),
354
			'light_blue' => array(
355
				'#E1F5FE',
356
				'#B3E5FC',
357
				'#81D4fA',
358
				'#4fC3F7',
359
				'#29B6FC',
360
				'#03A9F4',
361
				'#039BE5',
362
				'#0288D1',
363
				'#0277BD',
364
				'#01579B',
365
				'#80D8FF',
366
				'#40C4FF',
367
				'#00B0FF',
368
				'#0091EA',
369
			),
370
			'cyan' => array(
371
				'#E0F7FA',
372
				'#B2EBF2',
373
				'#80DEEA',
374
				'#4DD0E1',
375
				'#26C6DA',
376
				'#00BCD4',
377
				'#00ACC1',
378
				'#0097A7',
379
				'#00838F',
380
				'#006064',
381
				'#84FFFF',
382
				'#18FFFF',
383
				'#00E5FF',
384
				'#00B8D4',
385
			),
386
			'teal' => array(
387
				'#E0F2F1',
388
				'#B2DFDB',
389
				'#80CBC4',
390
				'#4DB6AC',
391
				'#26A69A',
392
				'#009688',
393
				'#00897B',
394
				'#00796B',
395
				'#00695C',
396
				'#004D40',
397
				'#A7FFEB',
398
				'#64FFDA',
399
				'#1DE9B6',
400
				'#00BFA5',
401
			),
402
			'green' => array(
403
				'#E8F5E9',
404
				'#C8E6C9',
405
				'#A5D6A7',
406
				'#81C784',
407
				'#66BB6A',
408
				'#4CAF50',
409
				'#43A047',
410
				'#388E3C',
411
				'#2E7D32',
412
				'#1B5E20',
413
				'#B9F6CA',
414
				'#69F0AE',
415
				'#00E676',
416
				'#00C853',
417
			),
418
			'light-green' => array(
419
				'#F1F8E9',
420
				'#DCEDC8',
421
				'#C5E1A5',
422
				'#AED581',
423
				'#9CCC65',
424
				'#8BC34A',
425
				'#7CB342',
426
				'#689F38',
427
				'#558B2F',
428
				'#33691E',
429
				'#CCFF90',
430
				'#B2FF59',
431
				'#76FF03',
432
				'#64DD17',
433
			),
434
			'lime' => array(
435
				'#F9FBE7',
436
				'#F0F4C3',
437
				'#E6EE9C',
438
				'#DCE775',
439
				'#D4E157',
440
				'#CDDC39',
441
				'#C0CA33',
442
				'#A4B42B',
443
				'#9E9D24',
444
				'#827717',
445
				'#F4FF81',
446
				'#EEFF41',
447
				'#C6FF00',
448
				'#AEEA00',
449
			),
450
			'yellow' => array(
451
				'#FFFDE7',
452
				'#FFF9C4',
453
				'#FFF590',
454
				'#FFF176',
455
				'#FFEE58',
456
				'#FFEB3B',
457
				'#FDD835',
458
				'#FBC02D',
459
				'#F9A825',
460
				'#F57F17',
461
				'#FFFF82',
462
				'#FFFF00',
463
				'#FFEA00',
464
				'#FFD600',
465
			),
466
			'amber' => array(
467
				'#FFF8E1',
468
				'#FFECB3',
469
				'#FFE082',
470
				'#FFD54F',
471
				'#FFCA28',
472
				'#FFC107',
473
				'#FFB300',
474
				'#FFA000',
475
				'#FF8F00',
476
				'#FF6F00',
477
				'#FFE57F',
478
				'#FFD740',
479
				'#FFC400',
480
				'#FFAB00',
481
			),
482
			'orange' => array(
483
				'#FFF3E0',
484
				'#FFE0B2',
485
				'#FFCC80',
486
				'#FFB74D',
487
				'#FFA726',
488
				'#FF9800',
489
				'#FB8C00',
490
				'#F57C00',
491
				'#EF6C00',
492
				'#E65100',
493
				'#FFD180',
494
				'#FFAB40',
495
				'#FF9100',
496
				'#FF6D00',
497
			),
498
			'deep-orange' => array(
499
				'#FBE9A7',
500
				'#FFCCBC',
501
				'#FFAB91',
502
				'#FF8A65',
503
				'#FF7043',
504
				'#FF5722',
505
				'#F4511E',
506
				'#E64A19',
507
				'#D84315',
508
				'#BF360C',
509
				'#FF9E80',
510
				'#FF6E40',
511
				'#FF3D00',
512
				'#DD2600',
513
			),
514
			'brown' => array(
515
				'#EFEBE9',
516
				'#D7CCC8',
517
				'#BCAAA4',
518
				'#A1887F',
519
				'#8D6E63',
520
				'#795548',
521
				'#6D4C41',
522
				'#5D4037',
523
				'#4E342E',
524
				'#3E2723',
525
			),
526
			'grey' => array(
527
				'#FAFAFA',
528
				'#F5F5F5',
529
				'#EEEEEE',
530
				'#E0E0E0',
531
				'#BDBDBD',
532
				'#9E9E9E',
533
				'#757575',
534
				'#616161',
535
				'#424242',
536
				'#212121',
537
				'#000000',
538
				'#ffffff',
539
			),
540
			'blue-grey' => array(
541
				'#ECEFF1',
542
				'#CFD8DC',
543
				'#B0BBC5',
544
				'#90A4AE',
545
				'#78909C',
546
				'#607D8B',
547
				'#546E7A',
548
				'#455A64',
549
				'#37474F',
550
				'#263238',
551
			),
552
		);
553
554
		switch ( $context ) {
555
556
			case '50':
557
			case '100':
558
			case '200':
559
			case '300':
560
			case '400':
561
			case '500':
562
			case '600':
563
			case '700':
564
			case '800':
565
			case '900':
566
			case 'A100':
567
			case 'A200':
568
			case 'A400':
569
			case 'A700':
570
				if ( 'A100' === $context ) {
571
					$key = 10;
572
					unset( $colors['grey'] );
573
				} elseif ( 'A200' === $context ) {
574
					$key = 11;
575
					unset( $colors['grey'] );
576
				} elseif ( 'A400' === $context ) {
577
					$key = 12;
578
					unset( $colors['grey'] );
579
				} elseif ( 'A700' === $context ) {
580
					$key = 13;
581
					unset( $colors['grey'] );
582
				} else {
583
					$key = $context / 100;
584
				}
585
				unset( $colors['primary'] );
586
				$position_colors = array();
587
				foreach ( $colors as $color_family ) {
588
					if ( isset( $color_family[ $key ] ) ) {
589
						$position_colors[] = $color_family[ $key ];
590
					}
591
				}
592
				return $position_colors;
593
			case 'all':
594
				unset( $colors['primary'] );
595
				$all_colors = array();
596
				foreach ( $colors as $color_family ) {
597
					foreach ( $color_family as $color ) {
598
						$all_colors[] = $color;
599
					}
600
				}
601
				return $all_colors;
602
			case 'primary':
603
				return $colors['primary'];
604
			default:
605
				if ( isset( $colors[ $context ] ) ) {
606
					return $colors[ $context ];
607
				}
608
				return $colors['primary'];
609
		}
610
	}
611
612
	/**
613
	 * Get an array of all available dashicons.
614
	 *
615
	 * @static
616
	 * @access public
617
	 * @return array
618
	 */
619
	public static function get_dashicons() {
620
621
		$admin_menu = array(
622
			'menu',
623
			'admin-site',
624
			'dashboard',
625
			'admin-post',
626
			'admin-media',
627
			'admin-links',
628
			'admin-page',
629
			'admin-comments',
630
			'admin-appearance',
631
			'admin-plugins',
632
			'admin-users',
633
			'admin-tools',
634
			'admin-settings',
635
			'admin-network',
636
			'admin-home',
637
			'admin-generic',
638
			'admin-collapse',
639
			'filter',
640
			'admin-customizer',
641
			'admin-multisite',
642
		);
643
644
		$welcome_screen = array(
645
			'welcome-write-blog',
646
			'welcome-add-page',
647
			'welcome-view-site',
648
			'welcome-widgets-menus',
649
			'welcome-comments',
650
			'welcome-learn-more',
651
		);
652
653
		$post_formats = array(
654
			'format-aside',
655
			'format-image',
656
			'format-gallery',
657
			'format-video',
658
			'format-status',
659
			'format-quote',
660
			'format-chat',
661
			'format-audio',
662
			'camera',
663
			'images-alt',
664
			'images-alt2',
665
			'video-alt',
666
			'video-alt2',
667
			'video-alt3',
668
		);
669
670
		$media = array(
671
			'media-archive',
672
			'media-audio',
673
			'media-code',
674
			'media-default',
675
			'media-document',
676
			'media-interactive',
677
			'media-spreadsheet',
678
			'media-text',
679
			'media-video',
680
			'playlist-audio',
681
			'playlist-video',
682
			'controls-play',
683
			'controls-pause',
684
			'controls-forward',
685
			'controls-skipforward',
686
			'controls-back',
687
			'controls-skipback',
688
			'controls-repeat',
689
			'controls-volumeon',
690
			'controls-volumeoff',
691
		);
692
693
		$image_editing = array(
694
			'image-crop',
695
			'image-rotate',
696
			'image-rotate-left',
697
			'image-rotate-right',
698
			'image-flip-vertical',
699
			'image-flip-horizontal',
700
			'image-filter',
701
			'undo',
702
			'redo',
703
		);
704
705
		$tinymce = array(
706
			'editor-bold',
707
			'editor-italic',
708
			'editor-ul',
709
			'editor-ol',
710
			'editor-quote',
711
			'editor-alignleft',
712
			'editor-aligncenter',
713
			'editor-alignright',
714
			'editor-insertmore',
715
			'editor-spellcheck',
716
			'editor-expand',
717
			'editor-contract',
718
			'editor-kitchensink',
719
			'editor-underline',
720
			'editor-justify',
721
			'editor-textcolor',
722
			'editor-paste-word',
723
			'editor-paste-text',
724
			'editor-removeformatting',
725
			'editor-video',
726
			'editor-customchar',
727
			'editor-outdent',
728
			'editor-indent',
729
			'editor-help',
730
			'editor-strikethrough',
731
			'editor-unlink',
732
			'editor-rtl',
733
			'editor-break',
734
			'editor-code',
735
			'editor-paragraph',
736
			'editor-table',
737
		);
738
739
		$posts = array(
740
			'align-left',
741
			'align-right',
742
			'align-center',
743
			'align-none',
744
			'lock',
745
			'unlock',
746
			'calendar',
747
			'calendar-alt',
748
			'visibility',
749
			'hidden',
750
			'post-status',
751
			'edit',
752
			'trash',
753
			'sticky',
754
		);
755
756
		$sorting = array(
757
			'external',
758
			'arrow-up',
759
			'arrow-down',
760
			'arrow-right',
761
			'arrow-left',
762
			'arrow-up-alt',
763
			'arrow-down-alt',
764
			'arrow-right-alt',
765
			'arrow-left-alt',
766
			'arrow-up-alt2',
767
			'arrow-down-alt2',
768
			'arrow-right-alt2',
769
			'arrow-left-alt2',
770
			'sort',
771
			'leftright',
772
			'randomize',
773
			'list-view',
774
			'exerpt-view',
775
			'grid-view',
776
		);
777
778
		$social = array(
779
			'share',
780
			'share-alt',
781
			'share-alt2',
782
			'twitter',
783
			'rss',
784
			'email',
785
			'email-alt',
786
			'facebook',
787
			'facebook-alt',
788
			'googleplus',
789
			'networking',
790
		);
791
792
		$wordpress_org = array(
793
			'hammer',
794
			'art',
795
			'migrate',
796
			'performance',
797
			'universal-access',
798
			'universal-access-alt',
799
			'tickets',
800
			'nametag',
801
			'clipboard',
802
			'heart',
803
			'megaphone',
804
			'schedule',
805
		);
806
807
		$products = array(
808
			'wordpress',
809
			'wordpress-alt',
810
			'pressthis',
811
			'update',
812
			'screenoptions',
813
			'info',
814
			'cart',
815
			'feedback',
816
			'cloud',
817
			'translation',
818
		);
819
820
		$taxonomies = array(
821
			'tag',
822
			'category',
823
		);
824
825
		$widgets = array(
826
			'archive',
827
			'tagcloud',
828
			'text',
829
		);
830
831
		$notifications = array(
832
			'yes',
833
			'no',
834
			'no-alt',
835
			'plus',
836
			'plus-alt',
837
			'minus',
838
			'dismiss',
839
			'marker',
840
			'star-filled',
841
			'star-half',
842
			'star-empty',
843
			'flag',
844
			'warning',
845
		);
846
847
		$misc = array(
848
			'location',
849
			'location-alt',
850
			'vault',
851
			'shield',
852
			'shield-alt',
853
			'sos',
854
			'search',
855
			'slides',
856
			'analytics',
857
			'chart-pie',
858
			'chart-bar',
859
			'chart-line',
860
			'chart-area',
861
			'groups',
862
			'businessman',
863
			'id',
864
			'id-alt',
865
			'products',
866
			'awards',
867
			'forms',
868
			'testimonial',
869
			'portfolio',
870
			'book',
871
			'book-alt',
872
			'download',
873
			'upload',
874
			'backup',
875
			'clock',
876
			'lightbulb',
877
			'microphone',
878
			'desktop',
879
			'tablet',
880
			'smartphone',
881
			'phone',
882
			'index-card',
883
			'carrot',
884
			'building',
885
			'store',
886
			'album',
887
			'palmtree',
888
			'tickets-alt',
889
			'money',
890
			'smiley',
891
			'thumbs-up',
892
			'thumbs-down',
893
			'layout',
894
		);
895
896
		return array(
897
			'admin-menu'     => $admin_menu,
898
			'welcome-screen' => $welcome_screen,
899
			'post-formats'   => $post_formats,
900
			'media'          => $media,
901
			'image-editing'  => $image_editing,
902
			'tinymce'        => $tinymce,
903
			'posts'          => $posts,
904
			'sorting'        => $sorting,
905
			'social'         => $social,
906
			'wordpress_org'  => $wordpress_org,
907
			'products'       => $products,
908
			'taxonomies'     => $taxonomies,
909
			'widgets'        => $widgets,
910
			'notifications'  => $notifications,
911
			'misc'           => $misc,
912
		);
913
914
	}
915
}
916