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