feat: add a provider-agnostic subscribe form
Reading settings take an HTTPS endpoint and the email field name, so the form posts straight to Buttondown, Mailchimp, MailerLite or anything else without the theme picking one. Without an endpoint the block renders nothing rather than a form that goes nowhere. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
3 files changed
+146
No files matched your search
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"$schema": "https://schemas.wp.org/trunk/block.json",
|
||||
"apiVersion": 3,
|
||||
"name": "chrnx/subscribe-form",
|
||||
"version": "0.2.0",
|
||||
"title": "Subscribe Form",
|
||||
"category": "theme",
|
||||
"description": "The email subscription form, rendered only when a provider endpoint is configured.",
|
||||
"textdomain": "chrnx",
|
||||
"attributes": {
|
||||
"headingLevel": { "type": "integer", "default": 2, "enum": [2, 3, 4, 5, 6] }
|
||||
},
|
||||
"supports": { "html": false },
|
||||
"render": "file:./render.php"
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* Subscribe Form block render.
|
||||
*
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
$chrnx_endpoint = chrnx_get_subscribe_endpoint();
|
||||
// Nothing invented: without a configured provider there is no form to show.
|
||||
if ( '' === $chrnx_endpoint ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$chrnx_field = chrnx_get_subscribe_field_name();
|
||||
$chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 );
|
||||
$chrnx_input_id = wp_unique_id( 'chrnx-subscribe-email-' );
|
||||
?>
|
||||
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-subscribe-form' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
|
||||
<h<?php echo esc_attr( $chrnx_heading ); ?> class="chrnx-subscribe-form__title"><?php echo esc_html__( 'Get new work by email', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
|
||||
<p class="chrnx-subscribe-form__note"><?php echo esc_html__( 'New stories, releases, and episodes. No tracking, and you can leave whenever you want.', 'chrnx' ); ?></p>
|
||||
<form class="chrnx-subscribe-form__form" method="post" action="<?php echo esc_url( $chrnx_endpoint ); ?>" target="_blank" rel="noopener noreferrer">
|
||||
<label class="chrnx-subscribe-form__label" for="<?php echo esc_attr( $chrnx_input_id ); ?>"><?php echo esc_html__( 'Email address', 'chrnx' ); ?></label>
|
||||
<input
|
||||
class="chrnx-subscribe-form__input"
|
||||
id="<?php echo esc_attr( $chrnx_input_id ); ?>"
|
||||
type="email"
|
||||
name="<?php echo esc_attr( $chrnx_field ); ?>"
|
||||
autocomplete="email"
|
||||
inputmode="email"
|
||||
spellcheck="false"
|
||||
required
|
||||
>
|
||||
<button class="chrnx-subscribe-form__button" type="submit"><?php echo esc_html__( 'Subscribe', 'chrnx' ); ?></button>
|
||||
</form>
|
||||
</section>
|
||||
@@ -70,3 +70,99 @@ function chrnx_get_subscribe_url(): string {
|
||||
$url = get_permalink( $page );
|
||||
return is_string( $url ) ? $url : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize the subscription form endpoint and require HTTPS.
|
||||
*
|
||||
* @param mixed $value Submitted option value.
|
||||
* @return string Valid HTTPS URL or an empty string.
|
||||
*/
|
||||
function chrnx_sanitize_subscribe_endpoint( $value ) {
|
||||
return chrnx_sanitize_https_url( $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize the provider's email field name.
|
||||
*
|
||||
* @param mixed $value Submitted option value.
|
||||
* @return string Safe field name, defaulting to email.
|
||||
*/
|
||||
function chrnx_sanitize_subscribe_field( $value ) {
|
||||
if ( ! is_string( $value ) ) {
|
||||
return 'email';
|
||||
}
|
||||
$name = trim( $value );
|
||||
// Providers use names like email, EMAIL, or fields[email].
|
||||
return 1 === preg_match( '/^[A-Za-z0-9_\[\]-]{1,64}$/', $name ) ? $name : 'email';
|
||||
}
|
||||
|
||||
/** Render the subscription endpoint field. */
|
||||
function chrnx_render_subscribe_endpoint_field() {
|
||||
printf(
|
||||
'<input type="url" name="chrnx_subscribe_endpoint" id="chrnx_subscribe_endpoint" value="%s" class="regular-text" placeholder="https://">',
|
||||
esc_attr( (string) get_option( 'chrnx_subscribe_endpoint', '' ) )
|
||||
);
|
||||
echo '<p class="description">' . esc_html__( 'HTTPS form endpoint from your email provider. Leave empty to hide the form.', 'chrnx' ) . '</p>';
|
||||
}
|
||||
|
||||
/** Render the subscription email field-name input. */
|
||||
function chrnx_render_subscribe_field_field() {
|
||||
printf(
|
||||
'<input type="text" name="chrnx_subscribe_field" id="chrnx_subscribe_field" value="%s" class="regular-text">',
|
||||
esc_attr( chrnx_get_subscribe_field_name() )
|
||||
);
|
||||
echo '<p class="description">' . esc_html__( 'Name of the email input your provider expects, for example email or EMAIL.', 'chrnx' ) . '</p>';
|
||||
}
|
||||
|
||||
/** Register the subscription form settings and Reading fields. */
|
||||
function chrnx_register_subscribe_form_settings() {
|
||||
register_setting(
|
||||
'reading',
|
||||
'chrnx_subscribe_endpoint',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => '',
|
||||
'sanitize_callback' => 'chrnx_sanitize_subscribe_endpoint',
|
||||
)
|
||||
);
|
||||
register_setting(
|
||||
'reading',
|
||||
'chrnx_subscribe_field',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => 'email',
|
||||
'sanitize_callback' => 'chrnx_sanitize_subscribe_field',
|
||||
)
|
||||
);
|
||||
add_settings_field(
|
||||
'chrnx_subscribe_endpoint',
|
||||
esc_html__( 'Subscribe form endpoint', 'chrnx' ),
|
||||
'chrnx_render_subscribe_endpoint_field',
|
||||
'reading'
|
||||
);
|
||||
add_settings_field(
|
||||
'chrnx_subscribe_field',
|
||||
esc_html__( 'Subscribe email field name', 'chrnx' ),
|
||||
'chrnx_render_subscribe_field_field',
|
||||
'reading'
|
||||
);
|
||||
}
|
||||
add_action( 'admin_init', 'chrnx_register_subscribe_form_settings' );
|
||||
|
||||
/**
|
||||
* Return the configured subscription form endpoint.
|
||||
*
|
||||
* @return string Valid HTTPS endpoint or an empty string.
|
||||
*/
|
||||
function chrnx_get_subscribe_endpoint(): string {
|
||||
return chrnx_sanitize_https_url( get_option( 'chrnx_subscribe_endpoint', '' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configured email field name.
|
||||
*
|
||||
* @return string Safe field name.
|
||||
*/
|
||||
function chrnx_get_subscribe_field_name(): string {
|
||||
return chrnx_sanitize_subscribe_field( get_option( 'chrnx_subscribe_field', 'email' ) );
|
||||
}
|
||||
Reference in new issue
Block a user