| 1 |
<?php |
| 2 |
namespace MbSocial; |
| 3 |
defined('ABSPATH') or die('No direct access permitted'); |
| 4 |
|
| 5 |
use \maxbuttons\maxUtils as maxUtils; |
| 6 |
|
| 7 |
// temp class ( hopefully ) for doing FA conversion on the server |
| 8 |
class FAUtils |
| 9 |
{ |
| 10 |
protected static $instance = null; |
| 11 |
|
| 12 |
protected $faicons_array = null; |
| 13 |
protected $fashims_array = null; |
| 14 |
protected $faicons_searcharray = null; |
| 15 |
|
| 16 |
public static function getInstance() |
| 17 |
{ |
| 18 |
if (is_null(self::$instance)) |
| 19 |
{ |
| 20 |
self::$instance = new FAUtils(); |
| 21 |
|
| 22 |
} |
| 23 |
|
| 24 |
return self::$instance; |
| 25 |
} |
| 26 |
|
| 27 |
protected function getFAIcons() |
| 28 |
{ |
| 29 |
if (! is_null($this->faicons_array) ) |
| 30 |
{ |
| 31 |
return $this->faicons_array; |
| 32 |
} |
| 33 |
|
| 34 |
$dir = MB()->get_plugin_path() . "assets/libraries/font-awesome-5"; |
| 35 |
|
| 36 |
$icon_list = array(); |
| 37 |
|
| 38 |
$icon_json = file_get_contents($dir . '/icons.json'); |
| 39 |
$file_icon_array = json_decode($icon_json, true); |
| 40 |
|
| 41 |
$icon_array = array(); |
| 42 |
$search_array = array(); |
| 43 |
|
| 44 |
foreach($file_icon_array as $name => $data) |
| 45 |
{ |
| 46 |
$styles = $data['styles']; // [brands, solid, regular] |
| 47 |
$nice_name = $data['label']; |
| 48 |
|
| 49 |
$style_tag = ''; |
| 50 |
foreach($styles as $style) |
| 51 |
{ |
| 52 |
switch ($style) |
| 53 |
{ |
| 54 |
case 'brands': |
| 55 |
$style_tag = 'fab'; |
| 56 |
$category = 'brands'; |
| 57 |
break; |
| 58 |
case 'solid': |
| 59 |
$style_tag = 'fas'; |
| 60 |
$category = 'solid'; |
| 61 |
break; |
| 62 |
case 'regular': |
| 63 |
$style_tag = 'far'; |
| 64 |
$category = 'regular'; |
| 65 |
break; |
| 66 |
} |
| 67 |
$full_icon = $style_tag . ' fa-' . $name; |
| 68 |
$icon_array[$name][$style]['icon'] = $full_icon; |
| 69 |
$icon_array[$name][$style]['name'] = $name; |
| 70 |
$icon_array[$name][$style]['category'] = $category; |
| 71 |
$icon_array[$name][$style]['nice_name'] = $nice_name; |
| 72 |
$search_array[$full_icon]['path'] = $data['svg'][$style]['path']; |
| 73 |
$search_array[$full_icon]['viewbox'] = $data['svg'][$style]['viewBox']; |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
$this->faicons_array = $icon_array; |
| 79 |
$this->faicons_searcharray = $search_array; |
| 80 |
return $icon_array; |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
public function getFASVG($args) |
| 85 |
{ |
| 86 |
$defaults = array('icon'=> null, |
| 87 |
'title' => null, |
| 88 |
'size' => 20, |
| 89 |
); |
| 90 |
$args = wp_parse_args($args, $defaults); |
| 91 |
|
| 92 |
if (is_null($args['icon']) || strlen($args['icon']) <= 0) |
| 93 |
{ |
| 94 |
return ''; |
| 95 |
} |
| 96 |
|
| 97 |
if (is_null ($this->faicons_searcharray)) |
| 98 |
{ |
| 99 |
$this->getFAIcons(); |
| 100 |
} |
| 101 |
|
| 102 |
$icons = $this->faicons_searcharray; |
| 103 |
|
| 104 |
$icon = $args['icon']; |
| 105 |
$size = $args['size']; |
| 106 |
|
| 107 |
if (! isset($icons[$icon])) |
| 108 |
{ |
| 109 |
echo 'Icon not in icon set: ' . $icon; |
| 110 |
} |
| 111 |
|
| 112 |
$faicon_svg = $icons[$icon]['path']; |
| 113 |
$faicon_viewbox = implode(' ', $icons[$icon]['viewbox']); |
| 114 |
//$faicon_viewbox = '0 0 ' . $args['size'] . ' ' . $args['size']; |
| 115 |
|
| 116 |
$svg = '<svg class="svg-mbp-fa" width="' . $size . '" height="' . $size . '" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="' . $faicon_viewbox . '"><path fill="currentColor" d="' . $faicon_svg . '"></path></svg>'; |
| 117 |
//print_R($args); |
| 118 |
return $svg; |
| 119 |
} |
| 120 |
|
| 121 |
public function checkShims($icon) |
| 122 |
{ |
| 123 |
if (is_null($this->fashims_array ) ) |
| 124 |
{ |
| 125 |
$conversion_path = MB()->get_plugin_path() . '/assets/libraries/font-awesome-5/shims.json'; |
| 126 |
$this->fashims_array = json_decode(file_get_contents($conversion_path), ARRAY_A); |
| 127 |
} |
| 128 |
|
| 129 |
$old_value = $icon; |
| 130 |
|
| 131 |
$old_value = str_replace('fa-','', $old_value); |
| 132 |
$old_value = str_replace('fa', '', $old_value); |
| 133 |
$old_value = trim($old_value); |
| 134 |
|
| 135 |
return $this->searchNewFA($old_value, $this->fashims_array); |
| 136 |
} |
| 137 |
|
| 138 |
public function searchNewFA($old, $conversion_array) |
| 139 |
{ |
| 140 |
$new = false; |
| 141 |
foreach($conversion_array as $key => $items) |
| 142 |
{ |
| 143 |
if ($items[0] == $old) |
| 144 |
{ |
| 145 |
$new = (strlen($items[1]) > 0) ? $items[1] : 'fas'; // if not set, fas is the set |
| 146 |
$new .= ' fa-'; |
| 147 |
$new .= (strlen($items[2]) > 0) ? $items[2] : $old ; |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
if (! $new) |
| 153 |
{ |
| 154 |
$new = 'fas fa-'. $old; |
| 155 |
} |
| 156 |
|
| 157 |
return $new; |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|