diff --git a/functions.php b/functions.php index 3732aed..adb5e08 100644 --- a/functions.php +++ b/functions.php @@ -53,7 +53,7 @@ function ipsum_enqueue_styles() { 'ipsum-style', get_parent_theme_file_uri( 'style.css' ), array(), - wp_get_theme()->get( 'Version' ) + wp_get_theme( get_template() )->get( 'Version' ) ); // Enqueue theme stylesheet. @@ -62,26 +62,84 @@ function ipsum_enqueue_styles() { endif; add_action( 'wp_enqueue_scripts', 'ipsum_enqueue_styles' ); +if ( ! function_exists( 'ipsum_get_asset_url' ) ) : + /** + * Gets the URL of a theme asset, with the theme version added for cache busting. + * + * The asset and the version both come from the parent theme, so a child + * theme's version doesn't stand in for Ipsum's. + * + * @since Ipsum 1.0 + * + * @param string $path Path to the asset, relative to the theme directory, such as + * 'assets/images/ipsum-polygons.webp'. + * @return string The asset URL, with a `ver` query argument when the theme + * version can be read. + * + * @phpstan-param non-falsy-string $path + */ + function ipsum_get_asset_url( string $path ): string { + $url = get_parent_theme_file_uri( $path ); + $version = wp_get_theme( get_template() )->get( 'Version' ); + if ( false !== $version ) { + $url = add_query_arg( 'ver', $version, $url ); + } + return $url; + } +endif; + +if ( ! function_exists( 'ipsum_serialize_block_attribute' ) ) : + /** + * Serializes a value for a block attribute in a pattern's block delimiter. + * + * Block attributes are JSON in an HTML comment, so a value computed in PHP, + * such as a translated string, has to be encoded the way the block editor + * serializes attributes, not escaped for HTML. HTML entities would be kept + * as literal text in the attribute value, and a backslash or a line break in + * a translation would make the JSON invalid, losing all the block's attributes. + * + * The value is serialized under a key, which is then removed, so that it gets + * the same escaping as serialize_block_attributes() gives a whole block. + * + * @since Ipsum 1.0 + * + * @param mixed $value Attribute value. A value for an attribute that holds HTML, + * such as a label, must be escaped for HTML first. + * @return string The serialized value, including the quotes around a string. + */ + function ipsum_serialize_block_attribute( $value ): string { + $prefix = '{"value":'; + return substr( serialize_block_attributes( array( 'value' => $value ) ), strlen( $prefix ), -1 ); + } +endif; + if ( ! function_exists( 'ipsum_comments_cta_binding' ) ) : /** * Returns the comments call to action for the current post. * * @since Ipsum 1.0 * - * @param array $source_args Block binding source arguments. - * @param WP_Block $block_instance The block instance. - * @return string The call to action text. + * @param array $source_args Block binding source arguments. Unused. + * @param WP_Block $block_instance The block instance. + * @return string|null The call to action text, or null to keep the block's + * original content when there is no post. */ function ipsum_comments_cta_binding( $source_args, $block_instance ) { - $post_id = $block_instance->context['postId'] ?? get_the_ID(); + unset( $source_args ); // Unused, but must be accepted to get the block instance. + + $post_id = $block_instance->context['postId'] ?? null; + $post = get_post( is_numeric( $post_id ) ? (int) $post_id : null ); + if ( ! $post ) { + return null; + } - if ( get_comments_number( $post_id ) > 0 ) { + if ( get_comments_number( $post ) > 0 ) { $text = __( 'Join the conversation', 'ipsum' ); } else { $text = __( 'Be the first to comment', 'ipsum' ); } - return '' . esc_html( $text ) . ''; + return '' . esc_html( $text ) . ''; } endif; @@ -113,8 +171,9 @@ function ipsum_register_block_bindings() { * * @since Ipsum 1.0 * - * @param array $default_template_types The default template types. - * @return array The template types, with the sidebar variants added. + * @param array> $default_template_types The default template types, keyed by + * slug, each with a title and description. + * @return array> The template types, with the sidebar variants added. */ function ipsum_sidebar_template_types( $default_template_types ) { $default_template_types['index-sidebar'] = array( @@ -162,12 +221,14 @@ function ipsum_register_pattern_categories() { * * @since Ipsum 1.0 * - * @param array $metadata Metadata for the block type being registered. - * @return array The block metadata, with ARIA label support on the Search block. + * @param array $metadata Metadata for the block type being registered. + * @return array The block metadata, with ARIA label support on the Search block. */ function ipsum_search_aria_label_support( $metadata ) { if ( isset( $metadata['name'] ) && 'core/search' === $metadata['name'] ) { - $metadata['supports']['ariaLabel'] = true; + $supports = isset( $metadata['supports'] ) && is_array( $metadata['supports'] ) ? $metadata['supports'] : array(); + $supports['ariaLabel'] = true; + $metadata['supports'] = $supports; } return $metadata; } diff --git a/patterns/about-row.php b/patterns/about-row.php index 542589d..79e2d4b 100644 --- a/patterns/about-row.php +++ b/patterns/about-row.php @@ -11,7 +11,7 @@ */ ?> - +