Skip to content

Commit

Permalink
feat (Svg): add new get_the_icon function
Browse files Browse the repository at this point in the history
  • Loading branch information
firestar300 committed May 12, 2023
1 parent 5b902f8 commit 1f5ad62
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
15 changes: 7 additions & 8 deletions inc/Helpers/Svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,28 @@
/**
* @usage BEA\Theme\Framework\Helpers\Svg\get_the_icon( 'like' );
*
* @param string $icon_class
* @param array $additionnal_classes
* @param string $icon_name
*
* @return string
*/
function get_the_icon( string $icon_class, $additionnal_classes = [] ): string {
function get_the_icon( string $icon_name ): string {
/**
* @var Svg $svg
*/
$svg = \BEA\Theme\Framework\Framework::get_container()->get_service( 'svg' );
return false !== $svg ? $svg->get_the_icon( $icon_class, $additionnal_classes ) : '';
return false !== $svg ? $svg->get_the_icon( $icon_name ) : '';
}

/**
* @usage BEA\Theme\Framework\Helpers\Svg\the_icon( 'like' );
*
* @param string $icon_class
* @param array $additionnal_classes
* @param string $icon_name
*
*/
function the_icon( string $icon_class, $additionnal_classes = [] ): void {
function the_icon( string $icon_name ): void {
/**
* @var Svg $svg
*/
$svg = \BEA\Theme\Framework\Framework::get_container()->get_service( 'svg' );
false !== $svg ? $svg->the_icon( $icon_class, $additionnal_classes ) : '';
false !== $svg ? $svg->the_icon( $icon_name ) : '';
}
55 changes: 37 additions & 18 deletions inc/Services/Svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,30 @@ public function get_service_name(): string {
}

/**
* @param string $icon_class
* @param array $additionnal_classes
* @param string $icon_name
*
* @return string
*/
public function get_the_icon( string $icon_class, array $additionnal_classes = [] ): string {
if ( empty( $icon_class ) ) {
public function get_the_icon( string $icon_name ): string {
if ( empty( $icon_name ) ) {
return '';
}

$sprite_name = 'icons';
$icon_path = sprintf( '/dist/%s', $this->get_icon_from_manifest( sprintf( 'assets/%s.svg', $icon_name ) ) );

if ( false !== strpos( $icon_class, '/' ) ) {
$sprite_name = strtok( $icon_class, '/' );
$icon_class = substr( $icon_class, strpos( $icon_class, '/' ) + 1 );
if ( ! file_exists( \get_theme_file_path( $icon_path ) ) ) {
return '';
}

$icon_slug = strpos( $icon_class, 'icon-' ) === 0 ? $icon_class : sprintf( 'icon-%s', $icon_class );
$classes = [ 'icon', $icon_slug ];
$classes = array_merge( $classes, $additionnal_classes );
$classes = array_map( 'sanitize_html_class', $classes );

return sprintf( '<svg class="%s" aria-hidden="true" focusable="false"><use href="%s#%s"></use></svg>', implode( ' ', $classes ), \get_theme_file_uri( sprintf( '/dist/icons/%s.svg', $sprite_name ) ), $icon_slug ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return file_get_contents( \get_theme_file_path( $icon_path ) );
}

/**
* @param string $icon_class
* @param array $additionnal_classes
* @param string $icon_name
*
*/
public function the_icon( string $icon_class, array $additionnal_classes = [] ): void {
echo $this->get_the_icon( $icon_class, $additionnal_classes ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
public function the_icon( string $icon_name ): void {
echo $this->get_the_icon( $icon_name ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
Expand Down Expand Up @@ -99,4 +92,30 @@ public function allow_svg_tag( $tags ) {

return $tags;
}

/**
* Get the compiled SVG path from JSON manifest
*
* @param $icon_path
*
* @return string
*
* @author Milan RICOUL
*/
public function get_icon_from_manifest( string $icon_path ): string {
$json = file_get_contents( \get_theme_file_path( '/dist/assets.json' ) ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$assets = json_decode( $json, true );

if ( empty( $assets ) || JSON_ERROR_NONE !== json_last_error() ) {
return '';
}

$file = $assets[ $icon_path ];

if ( empty( $file ) ) {
return '';
}

return $file;
}
}

0 comments on commit 1f5ad62

Please sign in to comment.