Skip to content

Commit

Permalink
fix icon issue
Browse files Browse the repository at this point in the history
  • Loading branch information
seothemes committed Dec 21, 2023
1 parent 59ca7ac commit 48f3496
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 103 deletions.
2 changes: 1 addition & 1 deletion assets/js/details.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array(), 'version' => 'bed58a396803aff05da4');
<?php return array('dependencies' => array(), 'version' => '2480e11519e6a10f6042');
2 changes: 1 addition & 1 deletion assets/js/details.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 0 additions & 65 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,69 +101,4 @@ function setup(): void {
require_once $path;
}
}

$icons = \glob( __DIR__ . '/assets/svg/devicon/*.svg' );

foreach ( $icons as $icon ) {
$contents = \file_get_contents( $icon );
$dom = dom( $contents );
$svg = get_dom_element( 'svg', $dom );

if ( ! $svg ) {
continue;
}

$fill = $svg->getAttribute( 'fill' );

if ( $fill ) {
$svg->removeAttribute( 'fill' );
$svg->setAttribute( 'fill', 'currentColor' );
}

$paths = $svg->getElementsByTagName( 'path' );

foreach ( $paths as $path ) {
$fill = $path->getAttribute( 'fill' );

if ( $fill ) {
$path->removeAttribute( 'fill' );
$path->setAttribute( 'fill', 'currentColor' );
}
}

$gs = $svg->getElementsByTagName( 'g' );

foreach ( $gs as $g ) {
$fill = $g->getAttribute( 'fill' );

if ( $fill ) {
$g->removeAttribute( 'fill' );
$g->setAttribute( 'fill', 'currentColor' );
}
}

$circles = $svg->getElementsByTagName( 'circle' );

foreach ( $circles as $circle ) {
$fill = $circle->getAttribute( 'fill' );

if ( $fill ) {
$circle->removeAttribute( 'fill' );
$circle->setAttribute( 'fill', 'currentColor' );
}
}

\file_put_contents(
$icon,
\str_replace(
'viewbox',
'viewBox',
$dom->saveHTML()
)
);

break;

}

}
6 changes: 3 additions & 3 deletions includes/extensions/svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,17 @@ function render_inline_svg( string $html, array $block ): string {
$alt = $attrs['alt'] ?? $img->getAttribute( 'alt' );

if ( $width ) {
$svg->setAttribute( 'width', str_replace( 'px', '', $width ) );
$svg->setAttribute( 'width', str_replace( 'px', '', (string) $width ) );
}

if ( $height ) {
$svg->setAttribute( 'height', str_replace( 'px', '', $height ) );
$svg->setAttribute( 'height', str_replace( 'px', '', (string) $height ) );
}

if ( $alt ) {
$svg->setAttribute( 'aria-label', $alt );
}

$svg->setAttribute( 'class', $img->getAttribute( 'class' ) );

( $link ?? $first )->removeChild( $img );
Expand Down
39 changes: 24 additions & 15 deletions includes/extensions/template-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use function get_post_type;
use function get_post_type_object;
use function get_the_ID;
use function get_the_title;
use function gmdate;
use function home_url;
use function in_array;
Expand Down Expand Up @@ -53,7 +54,9 @@ function render_template_tags( string $html, array $block, WP_Block $object ): s

$category = $registered_blocks[ $block_name ]->category ?? '';

if ( 'text' !== $category && ! in_array( $block_name, [ 'core/button', 'core/navigation-link' ], true ) ) {
$other_blocks = [ 'core/button', 'core/navigation-link', 'core/image' ];

if ( 'text' !== $category && ! in_array( $block_name, $other_blocks, true ) ) {
return $html;
}

Expand All @@ -69,27 +72,29 @@ function render_template_tags( string $html, array $block, WP_Block $object ): s

preg_match_all( '#\{(.*?)}#', $html, $matches );

if ( empty( $matches[1] ) ) {
$without_brackets = $matches[1] ?? [];

if ( empty( $without_brackets ) ) {
return $html;
}

$id = $object->context['postId'] ?? get_the_ID();
$post_id = $object->context['postId'] ?? get_the_ID();
$replacements = [];

foreach ( $matches[1] as $tag ) {
foreach ( $without_brackets as $tag ) {
$replacement = '';

if ( shortcode_exists( $tag ) ) {
continue;
}

if ( ! is_null( $id ) ) {
$post_field = get_post_field( $tag, $id );
if ( ! is_null( $post_id ) ) {
$post_field = get_post_field( $tag, $post_id );

if ( $post_field ) {
$replacement = $post_field;
} else {
$post_meta = get_post_meta( $id, $tag, true );
$post_meta = get_post_meta( $post_id, $tag, true );

if ( $post_meta ) {
$replacement = $post_meta;
Expand All @@ -98,7 +103,7 @@ function render_template_tags( string $html, array $block, WP_Block $object ): s
}

if ( ! $replacement ) {
$tags = get_template_tags( $id );
$tags = get_template_tags( $post_id ?: null );

if ( isset( $tags[ $tag ] ) ) {
$replacement = is_callable( $tags[ $tag ] ) ? call_user_func( $tags[ $tag ] ) : $tags[ $tag ];
Expand All @@ -118,11 +123,11 @@ function render_template_tags( string $html, array $block, WP_Block $object ): s
*
* @since 0.9.34
*
* @param int $post_id Extra tags.
* @param ?int $post_id Extra tags.
*
* @return array
*/
function get_template_tags( int $post_id ): array {
function get_template_tags( ?int $post_id ): array {
static $tags = null;

if ( is_null( $tags ) ) {
Expand Down Expand Up @@ -159,11 +164,15 @@ function get_template_tags( int $post_id ): array {
* @since 1.3.0
*
* @param array $tags Template tags.
* @param int $post_id Post ID.
* @param ?int $post_id Post ID.
*
* @return array
*/
function add_post_template_tags( array $tags, int $post_id ): array {
function add_post_template_tags( array $tags, ?int $post_id ): array {
if ( ! $post_id ) {
return $tags;
}

$post_type = get_post_type( $post_id );
$post_type_object = get_post_type_object( $post_type );

Expand All @@ -181,11 +190,11 @@ function add_post_template_tags( array $tags, int $post_id ): array {
* @since 1.3.0
*
* @param array $tags Template tags.
* @param int $post_id Post ID.
* @param ?int $post_id Post ID.
*
* @return array
*/
function add_archive_template_tags( array $tags, int $post_id ): array {
function add_archive_template_tags( array $tags, ?int $post_id ): array {
if ( is_archive() || is_home() ) {
$tags['archive_title'] = static function (): string {
add_filter( 'get_the_archive_title_prefix', '__return_empty_string' );
Expand Down Expand Up @@ -213,7 +222,7 @@ function add_archive_template_tags( array $tags, int $post_id ): array {
*/
function get_the_archive_title_home( string $title ): string {
if ( is_home() ) {
$title = \get_the_title( get_option( 'page_for_posts', true ) );
$title = get_the_title( get_option( 'page_for_posts', true ) );
}

return $title;
Expand Down
63 changes: 46 additions & 17 deletions includes/utility/icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use function file_get_contents;
use function get_stylesheet_directory;
use function glob;
use function implode;
use function is_array;
use function is_null;
use function is_string;
use function preg_replace;
use function str_replace;
Expand All @@ -26,7 +28,7 @@
*
* @since 0.9.10
*
* @return array
* @return array [ 'set' => 'path' ]
*/
function get_icon_sets(): array {
$theme = [
Expand Down Expand Up @@ -147,24 +149,27 @@ function get_icon_data( WP_REST_Request $request ) {
* @return array
*/
function get_icons( string $set = '' ): array {
$icons = [];
$icon_sets = get_icon_sets();
static $icons = null;

foreach ( $icon_sets as $icon_set => $dir ) {
if ( ! is_string( $dir ) ) {
continue;
}
if ( is_null( $icons ) ) {
$icon_sets = get_icon_sets();

$icons[ $icon_set ] = [];
foreach ( $icon_sets as $icon_set => $dir ) {
if ( ! is_string( $dir ) ) {
continue;
}

$files = glob( $dir . '/*.svg' );
$icons[ $icon_set ] = [];

if ( ! is_array( $files ) ) {
continue;
}
$files = glob( $dir . '/*.svg' );

foreach ( $files as $file ) {
$icons[ $icon_set ][ basename( $file, '.svg' ) ] = trim( file_get_contents( $file ) );
if ( ! is_array( $files ) ) {
continue;
}

foreach ( $files as $file ) {
$icons[ $icon_set ][ basename( $file, '.svg' ) ] = trim( file_get_contents( $file ) );
}
}
}

Expand All @@ -183,8 +188,30 @@ function get_icons( string $set = '' ): array {
* @return string
*/
function get_icon( string $set, string $name, $size = null ): string {
$set = strtolower( $set );
$icon = get_icons()[ $set ][ $name ] ?? '';
$set = strtolower( $set );

static $cache = [];

$cache_key = implode( '-', [ $set, $name, $size ] );

if ( isset( $cache[ $cache_key ] ) ) {
return $cache[ $cache_key ];
}

$icon_sets = get_icon_sets();

if ( ! isset( $icon_sets[ $set ] ) ) {
return '';
}

$dir = $icon_sets[ $set ];
$file = $dir . '/' . $name . '.svg';

if ( ! file_exists( $file ) ) {
return '';
}

$icon = file_get_contents( $file );
$dom = dom( $icon );
$svg = get_dom_element( 'svg', $dom );

Expand Down Expand Up @@ -228,5 +255,7 @@ function get_icon( string $set, string $name, $size = null ): string {
$svg->setAttribute( 'fill', 'currentColor' );
}

return trim( $dom->saveHTML() );
$cache[ $cache_key ] = trim( $dom->saveHTML() );

return $cache[ $cache_key ];
}
2 changes: 1 addition & 1 deletion src/public/details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const initAccordion = ( el: HTMLElement ): void => {
} );
};

document.querySelectorAll( 'details' ).forEach( ( el ) => {
document.querySelectorAll( 'details:not([open])' ).forEach( ( el ) => {
if ( el instanceof HTMLElement ) {
initAccordion( el );
}
Expand Down

0 comments on commit 48f3496

Please sign in to comment.