From 0993cc192b4d9c0e91f8c1f5283d21af6fc35e66 Mon Sep 17 00:00:00 2001
From: Dennis Snell
Date: Sat, 15 Jun 2024 17:36:40 +0200
Subject: [PATCH] Explore different ways of providing Bit renderers.
---
src/wp-includes/bits.php | 75 +++++++++++++++++++
.../html-api/class-wp-html-template.php | 48 ++++++++++++
src/wp-settings.php | 1 +
3 files changed, 124 insertions(+)
create mode 100644 src/wp-includes/html-api/class-wp-html-template.php
diff --git a/src/wp-includes/bits.php b/src/wp-includes/bits.php
index 7e982c1addd97..a4edd2dea0dc1 100644
--- a/src/wp-includes/bits.php
+++ b/src/wp-includes/bits.php
@@ -8,6 +8,8 @@
* @since {WP_VERSION}
*/
+declare( strict_types=1 );
+
/**
* Hello Dolly Bit Rendering function.
*
@@ -48,3 +50,76 @@ function core_bit_hello_dolly( string $name, string $output_type, ?array $attrib
}
}
+class WP_Hello_Dolly_Bit extends BitProvider {
+ /**
+ * @inheritDoc
+ */
+ public function handle_plaintext( string $bit_name, ?array $attributes ): string {
+ return self::get_random_vocalist();
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function handle_richtext( string $bit_name, ?array $attributes ): WP_HTML_Template {
+ $name = self::get_random_vocalist();
+
+ return new WP_HTML_Template(
+ '%name>',
+ array( 'name' => $name )
+ );
+ }
+
+ /**
+ * Returns the name of a random Jazz vocalist.
+ *
+ * @return string
+ */
+ private function get_random_vocalist(): string {
+ static $vocalists = array(
+ 'Julie Dahle AagÄrd',
+ 'Mindi Abair',
+ 'Lorez Alexandria',
+ 'Karrin Allyson',
+ 'Michelle Amato',
+ 'Ernestine Anderson',
+ 'Ivie Anderson',
+ );
+
+ return $vocalists[ wp_rand( 0, count( $vocalists ) - 1 ) ];
+ }
+}
+
+abstract class BitProvider {
+ /**
+ * Performs initialize of Bit Provider during WordPress bootup.
+ */
+ public function register(): void {
+ // This is optional.
+ };
+
+ /**
+ * Called to source content in plaintext contexts. For example, when a
+ * Bit is found within an HTML attribute, or inside a `TITLE` element.
+ *
+ * @see WP_HTML_Template
+ *
+ * @param string $bit_name Full name with namespace of matched Bit, e.g. "core/post-author".
+ * @param array|null $attributes Configured attributes found on Bit, if found, otherwise `null`.
+ *
+ * @return string Plaintext value for provided content.
+ */
+ abstract public function handle_plaintext( string $bit_name, ?array $attributes ): string;
+
+ /**
+ * Called to source content in Rich Text (HTML Markup) contexts. For example,
+ * when a Bit is found within the inner content of an HTML tag.
+ *
+ * @see WP_HTML_Template
+ *
+ * @param string $bit_name Full name with namespace of matched Bit, e.g. "core/post-author".
+ * @param array|null $attributes Configured attributes found on Bit, if found, otherwise `null`.
+ * @return WP_HTML_Template HTML template for provided content: a string or array.
+ */
+ abstract public function handle_richtext( string $bit_name, ?array $attributes ): WP_HTML_Template;
+}
diff --git a/src/wp-includes/html-api/class-wp-html-template.php b/src/wp-includes/html-api/class-wp-html-template.php
new file mode 100644
index 0000000000000..f181478e41e63
--- /dev/null
+++ b/src/wp-includes/html-api/class-wp-html-template.php
@@ -0,0 +1,48 @@
+Hello, World!
' );
+ *
+ * // Placeholders for simple substitution.
+ * new WP_HTML_Template( 'Hello, %name>!
', array( 'name' => $name ) );
+ *
+ * // Spread-operator for sets of attributes.
+ * new WP_HTML_Template(
+ * '',
+ * array(
+ * 'data-wp-text="context.buttonLabel"',
+ * 'data-wp-click="actions.clickButton",
+ * )
+ * );
+ *
+ * @param string $template Static HTML template, possibly including placeholders.
+ * @param array|null $data Optional. Data provided for placeholders, if any.
+ */
+ public function __construct( string $template, array $data = null ) {
+ $this->template = $template;
+ $this->data = $data;
+ }
+}
diff --git a/src/wp-settings.php b/src/wp-settings.php
index 2e343f5d6ac06..2071922cd2da0 100644
--- a/src/wp-settings.php
+++ b/src/wp-settings.php
@@ -262,6 +262,7 @@
require ABSPATH . WPINC . '/html-api/class-wp-html-stack-event.php';
require ABSPATH . WPINC . '/html-api/class-wp-html-processor-state.php';
require ABSPATH . WPINC . '/html-api/class-wp-html-processor.php';
+require ABSPATH . WPINC . '/html-api/class-wp-html-template.php';
require ABSPATH . WPINC . '/html-api/class-wp-bits.php';
require ABSPATH . WPINC . '/class-wp-http.php';
require ABSPATH . WPINC . '/class-wp-http-streams.php';