| 1 |
<?php |
| 2 |
/** |
| 3 |
* Wrapper for Feather Icons library. |
| 4 |
* |
| 5 |
* Handles all icons operations. |
| 6 |
* |
| 7 |
* @package Feather |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Feather; |
| 13 |
|
| 14 |
/** |
| 15 |
* Wraps the feather icons functionality. |
| 16 |
* |
| 17 |
* Handles all icons operations. |
| 18 |
* |
| 19 |
* @package Feather |
| 20 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class Icons { |
| 24 |
|
| 25 |
/** |
| 26 |
* Already loaded raw icons. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @var array $icons Already loaded raw icons. |
| 30 |
*/ |
| 31 |
private static $icons = []; |
| 32 |
|
| 33 |
/** |
| 34 |
* Initializes the class and set its properties. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get a raw (SVG) icon. |
| 43 |
* |
| 44 |
* @param string $name Optional. The name of the icon. |
| 45 |
* @return string The raw value of the SVG icon. |
| 46 |
* @since 1.0.0 |
| 47 |
*/ |
| 48 |
public static function get_raw( $name = 'x' ) { |
| 49 |
$name = strtolower( $name ); |
| 50 |
$filename = __DIR__ . '/icons/' . $name . '.svg'; |
| 51 |
if ( array_key_exists( $name, self::$icons ) ) { |
| 52 |
return self::$icons[ $name ]; |
| 53 |
} |
| 54 |
if ( ! file_exists( $filename ) ) { |
| 55 |
return ( 'x' === $name ? '' : self::get_raw() ); |
| 56 |
} |
| 57 |
self::$icons[ $name ] = file_get_contents( $filename ); |
| 58 |
return ( self::get_raw( $name ) ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns a base64 svg resource for the icon. |
| 63 |
* |
| 64 |
* @param string $color Optional. Color of the icon. |
| 65 |
* @return string The svg resource as a base64. |
| 66 |
* @since 1.0.0 |
| 67 |
*/ |
| 68 |
public static function get_base64( $name = 'x', $fill = 'none', $stroke = 'currentColor', $stroke_width = '2', $line_join = 'round', $line_cap = 'round' ) { |
| 69 |
$source = self::get_raw( $name ); |
| 70 |
$source = str_replace( 'fill="none"', 'fill="' . $fill . '"', $source ); |
| 71 |
$source = str_replace( 'stroke="currentColor"', 'stroke="' . $stroke . '"', $source ); |
| 72 |
$source = str_replace( 'stroke-width="2"', 'stroke-width="' . $stroke_width . '"', $source ); |
| 73 |
$source = str_replace( 'stroke-linejoin="round"', 'stroke-linejoin="' . $line_join . '"', $source ); |
| 74 |
$source = str_replace( 'stroke-linecap="round"', 'stroke-linecap="' . $line_cap . '"', $source ); |
| 75 |
return 'data:image/svg+xml;base64,' . base64_encode( $source ); |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|