icons.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Helper functions for dealing with the icon fonts used by the theme
  4. *
  5. * @package vamtam/consulting
  6. */
  7. function vamtam_icon_type( $icon ) {
  8. echo esc_html( vamtam_get_icon_type( $icon ) );
  9. }
  10. function vamtam_get_icon_type( $icon ) {
  11. if ( strpos( $icon, 'vamtam-theme-' ) === 0 )
  12. return 'theme';
  13. return '';
  14. }
  15. function vamtam_icon( $key ) {
  16. echo vamtam_get_icon( $key ); // xss ok
  17. }
  18. function esc_attr_vamtam_icon( $key ) {
  19. echo esc_attr( vamtam_get_icon( $key ) );
  20. }
  21. function vamtam_get_icon( $key ) {
  22. if ( ( $num = vamtam_get_icon_num( $key ) ) !== false ) {
  23. return "&#$num;";
  24. }
  25. return $key;
  26. }
  27. function vamtam_get_icon_num( $key ) {
  28. $icons = vamtam_get_icon_list();
  29. $theme_icons = vamtam_get_theme_icon_list();
  30. if ( isset( $icons[ $key ] ) )
  31. return $icons[ $key ];
  32. $theme_key = preg_replace( '/^vamtam-theme-/', '', $key, 1 );
  33. if ( isset( $theme_icons[ $theme_key ] ) ) {
  34. return $theme_icons[ $theme_key ];
  35. }
  36. return false;
  37. }
  38. /**
  39. * Returns the list of Icomoon icons
  40. * @return array list of icons
  41. */
  42. function vamtam_get_icon_list() {
  43. if ( ! isset( $GLOBALS['VAMTAM_ICONS_CACHE'] ) ) {
  44. $GLOBALS['VAMTAM_ICONS_CACHE'] = include VAMTAM_ASSETS_DIR . 'fonts/icons/list.php';
  45. }
  46. return $GLOBALS['VAMTAM_ICONS_CACHE'];
  47. }
  48. /**
  49. * Returns the list of theme icons
  50. * @return array list of icons
  51. */
  52. function vamtam_get_theme_icon_list() {
  53. if ( ! isset( $GLOBALS['VAMTAM_THEME_ICONS_CACHE'] ) ) {
  54. $GLOBALS['VAMTAM_THEME_ICONS_CACHE'] = include VAMTAM_ASSETS_DIR . 'fonts/theme-icons/list.php';
  55. }
  56. return $GLOBALS['VAMTAM_THEME_ICONS_CACHE'];
  57. }
  58. function vamtam_get_icons_extended() {
  59. $result = array();
  60. $icons = vamtam_get_icon_list();
  61. $theme_icons = vamtam_get_theme_icon_list();
  62. ksort( $icons );
  63. ksort( $theme_icons );
  64. foreach ( $icons as $key => $num ) {
  65. $result[ $key ] = $key;
  66. }
  67. foreach ( $theme_icons as $key => $num ) {
  68. $result[ 'vamtam-theme-' . $key ] = 'vamtam-theme-' . $key;
  69. }
  70. return $result;
  71. }
  72. function vamtam_get_icon_html( $atts ) {
  73. $raw_atts = $atts;
  74. $atts = shortcode_atts( array(
  75. 'name' => '',
  76. 'style' => '',
  77. 'color' => '',
  78. 'size' => '',
  79. 'lheight' => 1,
  80. 'link_hover' => true,
  81. ), $atts );
  82. $icon_char = vamtam_get_icon( $atts['name'] );
  83. $collection = '';
  84. if ( strpos( $atts['name'], 'vamtam-theme-' ) === 0 ) {
  85. $collection = 'theme';
  86. }
  87. $color = vamtam_sanitize_accent( $atts['color'], 'css' );
  88. $style = '';
  89. if ( ! empty( $color ) ) {
  90. $style = "color:$color;";
  91. }
  92. $style .= ( 1 !== (int) $atts['lheight'] && (int) $atts['lheight'] !== (int) $atts['size'] ) ? "line-height:{$atts['lheight']};" : '';
  93. if ( ! empty( $atts['size'] ) ) {
  94. if ( substr( $atts['size'], -2 ) !== 'em' ) {
  95. $atts['size'] .= 'px';
  96. }
  97. $style .= "font-size:{$atts['size']} !important;";
  98. }
  99. $class = array( $collection, $atts['style'] );
  100. if ( $atts['link_hover'] ) {
  101. $class[] = 'use-hover';
  102. }
  103. $class = implode( ' ', $class );
  104. return "<span class='icon shortcode $class' style='{$style}'>$icon_char</span>";
  105. }