Icon.php
41 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\GlobalElements; |
| 4 | use Kubio\Core\Element; |
| 5 | use Kubio\Core\LodashBasic; |
| 6 | class Icon extends Element { |
| 7 | public function __construct( $tag_name, $props = array(), $children = array(), $block = null ) { |
| 8 | $default_icon = 'font-awesome/star'; |
| 9 | $icon = LodashBasic::get( $props, 'name', $default_icon ); |
| 10 | if ( ! $icon ) { |
| 11 | $icon = $default_icon; |
| 12 | } |
| 13 | $svg = ''; |
| 14 | if ( $icon && is_string( $icon ) ) { |
| 15 | $icon_folder_name = explode( '/', $icon ); |
| 16 | $library = $icon_folder_name[0]; |
| 17 | $icon_name = str_replace( ' ', '-', trim( $icon_folder_name[1] ) ); |
| 18 | |
| 19 | $svg_file = ( KUBIO_ROOT_DIR . 'static/icons/' . sanitize_file_name( $library ) . '/' . $icon_name . '.svg' ); |
| 20 | if ( file_exists( $svg_file ) ) { |
| 21 | $svg = file_get_contents( $svg_file ); |
| 22 | } |
| 23 | } |
| 24 | parent::__construct( |
| 25 | Element::SPAN, |
| 26 | LodashBasic::merge( |
| 27 | $props, |
| 28 | array( |
| 29 | 'className' => array( 'h-svg-icon' ), |
| 30 | ) |
| 31 | ), |
| 32 | array( $svg ), |
| 33 | $block |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | public function __toString() { |
| 38 | return parent::__toString(); |
| 39 | } |
| 40 | } |
| 41 |