Skip to content

Commit

Permalink
Explore different ways of providing Bit renderers.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Jun 15, 2024
1 parent 667d614 commit 0993cc1
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/wp-includes/bits.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @since {WP_VERSION}
*/

declare( strict_types=1 );

/**
* Hello Dolly Bit Rendering function.
*
Expand Down Expand Up @@ -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(
'<span data-vocalist="</%name>"></%name></span>',
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;
}
48 changes: 48 additions & 0 deletions src/wp-includes/html-api/class-wp-html-template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare( strict_types=1 );

class WP_HTML_Template {
/**
* HTML Template indicating where to mix a static HTML template
* and placeholders for dynamic values.
*
* @var string
*/
public $template;

/**
* Stores data necessary to render a template, if any required.
*
* @var array|null
*/
public $data;

/**
* Constructor function.
*
* Example:
*
* // No placeholders are required, only a template string.
* new WP_HTML_Template( '<p>Hello, World!</p>' );
*
* // Placeholders for simple substitution.
* new WP_HTML_Template( '<p>Hello, </%name>!</p>', array( 'name' => $name ) );
*
* // Spread-operator for sets of attributes.
* new WP_HTML_Template(
* '<button ...interactivity_args>Click me!</button>',
* 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;
}
}
1 change: 1 addition & 0 deletions src/wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 0993cc1

Please sign in to comment.