Skip to content

Instantly share code, notes, and snippets.

@korynorthrop
Created September 17, 2019 04:33
Show Gist options
  • Save korynorthrop/3fa94ccfae38f88169ebc1984680e55a to your computer and use it in GitHub Desktop.
Save korynorthrop/3fa94ccfae38f88169ebc1984680e55a to your computer and use it in GitHub Desktop.
/**
* Register all of our custom blocks
*
* @since 2.4.4
*/
public function register_blocks() {
// Newsletter signup block
register_block_type( 'hello-tools/newsletter-signup', array(
'style' => 'hello-tools-newsletter-signup',
'editor_script' => $this->plugin_slug . '-custom-blocks',
'attributes' => array (
'className' => array(
'type' => 'string',
'default' => 'hello-tools-newsletter-signup'
),
'headline' => array (
'type' => 'string',
'selector' => '.hello-tools-newsletter-signup__headline',
'default' => __( 'Subscribe to our newsletter', $this->plugin_slug )
),
'blurb' => array (
'type' => 'string',
'multiline' => 'p',
'selector' => '.hello-tools-newsletter-signup__blurb'
),
'bgColor' => array (
'type' => 'string',
'default' => 'white'
),
'bgClass' => array (
'type' => 'string',
'default' => 'hello-bg--white'
),
'btnClass' => array (
'type' => 'string',
'default' => 'hello-btn--blue'
),
'blockClasses' => array (
'type' => 'string',
'default' => 'hello-block--fullwidth alignfull'
)
),
'render_callback' => array( $this, 'hello_tools_newsletter_signup_callback' )
) );
}
public function hello_tools_newsletter_signup_callback( $attributes ) {
if ( !is_admin() ) {
$className = $attributes['className'] . ' ';
$blockClasses = $attributes['blockClasses'] . ' ';
$bgClass = $attributes['bgClass'] . ' ';
$has_blurb = ( array_key_exists( "blurb", $attributes ) && $attributes["blurb"] != "<p></p>" ) ? true : false;
$extra_heading_class = ( !$has_blurb ) ? ' pb-3' : '';
$theID = get_the_ID();
$nonce = wp_create_nonce( 'hello_newsletter_signup_' . $theID );
$privacyURL = esc_url( get_privacy_policy_url() ) . '#newsletter-signups';
$current_lang = ( defined( "ICL_LANGUAGE_NAME" ) ) ? ICL_LANGUAGE_NAME : "";
$current_lang_code = ( defined( "ICL_LANGUAGE_CODE" ) ) ? ICL_LANGUAGE_CODE : "";
$GDPRConsentTextVersion = "1.0";
$GDPRConsentText = sprintf( esc_html__( 'I have read the %1$s and agree to receive emails from %2$s.', $this->plugin_slug ), sprintf( '<a target="_blank" href="%s">%s</a>', $privacyURL, esc_html__( 'Privacy Info', $this->plugin_slug ) ), get_option( 'blogname' ) );
ob_start(); // Turn on output buffering
?>
<div class="<?php echo $className . $blockClasses . $bgClass; ?>hello-newsletter-form px-3 pt-5 pb-6 pb-lg-7 pt-lg-6">
<div class="hello-tools-newsletter-signup__wrap text-center mx-auto">
<h2 translate-name="headline" class="hello-tools-newsletter-signup__headline mb-4 <?php echo $extra_heading_class; ?>"><?php echo $attributes["headline"]; ?></h2>
<?php if ( $has_blurb ) : ?>
<div translate-name="blurb" class="hello-tools-newsletter-signup__blurb font-weight-bold mb-5"><?php echo $attributes["blurb"]; ?></div>
<?php endif; ?>
<form id="hello-classroom-callout__newsletter_form" action="" method="post" id="" name="" data-form-location="CTA block" data-nonce="<?php echo $nonce; ?>" data-post-id="<?php echo $theID; ?>" novalidate>
<div class="hello-newsletter-form__fields">
<div class="hello-tools-newsletter-signup__fieldwrap newsletter-field-group position-relative mx-auto">
<input type="email" name="EMAIL" placeholder="<?php _e( 'Enter email address...', $this->plugin_slug ); ?>" class="hello-tools-newsletter-signup__email newsletter-email-input px-4 mx-auto w-100 required email ltr-input text-left" />
<div id="newsletter-block-email-input-error-msg" class="newsletter-email-input-error-msg hello-form__error-msg alert-danger mt-2 mb-3 px-3 py-1 rounded" aria-live="polite"><?php esc_html_e( 'Please enter a valid email address.', $this->plugin_slug ); ?></div>
<input class="d-none" name="subscriber_name" type="text">
<input type="submit" value="<?php _e( 'Subscribe', $this->plugin_slug ); ?>" name="subscribe" class="hello-tools-newsletter-signup__submit btn hello-btn <?php echo $attributes["btnClass"]; ?>"/>
</div>
<div class="newsletter-field-group pt-3">
<fieldset class="gdprFieldset gdprRequired newsletter-field-group mb-4" name="gdpr_field">
<input type="checkbox" id="newsletter_block_gdpr" name="newsletter_block_gdpr" value="Y" class="hello-checkbox av-checkbox gdpr gdpr-checkbox hello-pointer mr-2"><label class="hello-checkbox-label hello-pointer checkbox subfield hello-pointer" for="newsletter_block_gdpr"><?php echo $GDPRConsentText; ?></label>
<div id="newsletter_block_gdpr-error-msg" class="gdpr-error-msg hello-form__error-msg alert-danger mt-2 mb-3 px-3 py-1 rounded" aria-live="polite"><?php esc_html_e( 'You must agree to these conditions before receiving our emails.', $this->plugin_slug ); ?></div>
</fieldset>
</div>
</div>
<div class="newsletter-submission-responses clear">
<div class="newsletter-form-error-response hello-form__error-msg response alert-danger my-3 p-3 rounded" id="newsletter-form-error-response" aria-live="polite"></div>
<div class="newsletter-form-success-response hello-form__success-msg response alert-success mb-3 p-3 rounded" id="newsletter-form-success-response" aria-live="polite"></div>
</div>
<input id="block-language-name-input" name="language-name-input" type="hidden" value="<?php echo $current_lang; ?>" name="LANGUAGE" class="language-name-input">
<input id="block-lang-code-input" name="lang-code-input" type="hidden" value="<?php echo $current_lang_code; ?>" name="lang_code" class="lang-code-input">
<input id="block-gdpr-text-input" type="hidden" value="<?php echo esc_html( $GDPRConsentTextVersion ); ?>" name="gdpr_text_input" class="gdpr-text-input">
</form>
</div>
</div>
<?php
$output = ob_get_contents(); // collect output
ob_end_clean(); // Turn off ouput buffer
return $output; // Print output
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment