diff --git a/assets/js/details.asset.php b/assets/js/details.asset.php index d517dc1..8838b70 100644 --- a/assets/js/details.asset.php +++ b/assets/js/details.asset.php @@ -1 +1 @@ - array(), 'version' => 'bed58a396803aff05da4'); + array(), 'version' => '2480e11519e6a10f6042'); diff --git a/assets/js/details.js b/assets/js/details.js index 8590a32..c732990 100644 --- a/assets/js/details.js +++ b/assets/js/details.js @@ -1 +1 @@ -(()=>{const t=()=>{const t=getComputedStyle(document.body).getPropertyValue("--wp--custom--transition--duration");return t.includes("ms")?parseFloat(t):t.includes("s")?1e3*parseFloat(t):300},e=e=>{const o=e;let n=null;const s=o.querySelector("summary");if(!s)return;const r=()=>{const t=getComputedStyle(o),e=parseFloat(t.borderTopWidth)||0,n=parseFloat(t.borderBottomWidth)||0;return s.offsetHeight+e+n};o.style.maxHeight=`${r()}px`;const l=e=>{const l=Array.from(o.children).filter((t=>t!==s)).reduce(((t,e)=>{const o=e,n=getComputedStyle(o),s=parseFloat(n.marginTop)||0,r=parseFloat(n.marginBottom)||0;return t+o.offsetHeight+s+r}),0),i=r(),a=`${o.offsetHeight}px`,c=e?`${i+l}px`:`${i}px`;e||o.classList.add("closing"),o.style.overflow="hidden",n?.cancel(),n=o.animate({maxHeight:[a,c]},{duration:t(),easing:"ease-out"}),n.onfinish=()=>{n=null,o.style.overflow="",o.style.maxHeight=e?"none":`${i}px`,e||(o.classList.remove("closing"),o.removeAttribute("open"))}};((t,e)=>{window.addEventListener("scroll",(()=>{const o=(()=>{const e=getComputedStyle(t);return parseFloat(e.top)||0})(),n=t.getBoundingClientRect();"sticky"===window.getComputedStyle(t).position&&n.top<=o&&t.open&&e(!1)}))})(o,l),s.addEventListener("click",(t=>{t.preventDefault();const e=!o.open;e&&o.setAttribute("open",""),l(e)}))};document.querySelectorAll("details").forEach((t=>{t instanceof HTMLElement&&e(t)}))})(); \ No newline at end of file +(()=>{const t=()=>{const t=getComputedStyle(document.body).getPropertyValue("--wp--custom--transition--duration");return t.includes("ms")?parseFloat(t):t.includes("s")?1e3*parseFloat(t):300},e=e=>{const o=e;let n=null;const s=o.querySelector("summary");if(!s)return;const r=()=>{const t=getComputedStyle(o),e=parseFloat(t.borderTopWidth)||0,n=parseFloat(t.borderBottomWidth)||0;return s.offsetHeight+e+n};o.style.maxHeight=`${r()}px`;const l=e=>{const l=Array.from(o.children).filter((t=>t!==s)).reduce(((t,e)=>{const o=e,n=getComputedStyle(o),s=parseFloat(n.marginTop)||0,r=parseFloat(n.marginBottom)||0;return t+o.offsetHeight+s+r}),0),i=r(),a=`${o.offsetHeight}px`,c=e?`${i+l}px`:`${i}px`;e||o.classList.add("closing"),o.style.overflow="hidden",n?.cancel(),n=o.animate({maxHeight:[a,c]},{duration:t(),easing:"ease-out"}),n.onfinish=()=>{n=null,o.style.overflow="",o.style.maxHeight=e?"none":`${i}px`,e||(o.classList.remove("closing"),o.removeAttribute("open"))}};((t,e)=>{window.addEventListener("scroll",(()=>{const o=(()=>{const e=getComputedStyle(t);return parseFloat(e.top)||0})(),n=t.getBoundingClientRect();"sticky"===window.getComputedStyle(t).position&&n.top<=o&&t.open&&e(!1)}))})(o,l),s.addEventListener("click",(t=>{t.preventDefault();const e=!o.open;e&&o.setAttribute("open",""),l(e)}))};document.querySelectorAll("details:not([open])").forEach((t=>{t instanceof HTMLElement&&e(t)}))})(); \ No newline at end of file diff --git a/functions.php b/functions.php index 7fc96c4..05f3aab 100644 --- a/functions.php +++ b/functions.php @@ -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; - - } - } diff --git a/includes/extensions/svg.php b/includes/extensions/svg.php index 2faf774..96cd16e 100644 --- a/includes/extensions/svg.php +++ b/includes/extensions/svg.php @@ -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 ); diff --git a/includes/extensions/template-tags.php b/includes/extensions/template-tags.php index d77acdf..7aa4eb5 100644 --- a/includes/extensions/template-tags.php +++ b/includes/extensions/template-tags.php @@ -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; @@ -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; } @@ -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; @@ -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 ]; @@ -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 ) ) { @@ -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 ); @@ -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' ); @@ -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; diff --git a/includes/utility/icon.php b/includes/utility/icon.php index 8b56a0b..aa65155 100644 --- a/includes/utility/icon.php +++ b/includes/utility/icon.php @@ -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; @@ -26,7 +28,7 @@ * * @since 0.9.10 * - * @return array + * @return array [ 'set' => 'path' ] */ function get_icon_sets(): array { $theme = [ @@ -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 ) ); + } } } @@ -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 ); @@ -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 ]; } diff --git a/src/public/details.tsx b/src/public/details.tsx index 231eae7..f2965ef 100644 --- a/src/public/details.tsx +++ b/src/public/details.tsx @@ -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 ); }