Skip to content

Commit

Permalink
feat(ras-acc): render relevant newsletter lists in form
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed May 20, 2024
1 parent e579ad5 commit 19005db
Showing 1 changed file with 58 additions and 21 deletions.
79 changes: 58 additions & 21 deletions includes/reader-activation/class-reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ public static function enqueue_scripts() {
* @param bool $allow_reg_block_render Whether to allow the registration block to render.
*/
if ( apply_filters( 'newspack_reader_activation_should_render_auth', true ) ) {
$authenticated_email = '';
if ( \is_user_logged_in() && self::is_user_reader( \wp_get_current_user() ) ) {
$authenticated_email = \wp_get_current_user()->user_email;
}
$authenticated_email = self::get_logged_in_reader_email_address();
$script_dependencies = [];
$script_data = [
'auth_intention_cookie' => self::AUTH_INTENTION_COOKIE,
Expand Down Expand Up @@ -1337,26 +1334,41 @@ public static function render_auth_modal() {

/**
* Renders newsletters signup form.
*
* @param string $email_address Email address.
* @param array $newsletters_lists Array of newsletters lists.
*
* @return void
*/
private static function render_newsletters_signup_form() {
// TODO: Fetch lists to render.
private static function render_newsletters_signup_form( $email_address, $newsletters_lists ) {
?>
<div class="newspack-ui newspack-newsletters-signup">
<div class="newspack-ui__box newspack-ui__box--success newspack-ui__box--text-center" data-action="success">
<span class="newspack-ui__icon newspack-ui__icon--success">
<svg aria-hidden="true" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M16.7 7.1l-6.3 8.5-3.3-2.5-.9 1.2 4.5 3.4L17.9 8z" />
</svg>
</span>
<p>
<strong class="success-title"></strong>
</p>
<p class="newspack-ui__font--xs success-description"></p>
</div>
<form method="post" target="_top">
<p>
<input />
</p>
<input type="hidden" name="newsletter_signup_email" value="<?php echo esc_attr( $email_address ); ?>" />
<?php
foreach ( $newsletters_lists as $list ) {
$checkbox_id = sprintf( 'newspack-blocks-list-%s', $list['id'] );
?>
<label class="newspack-ui__input-card">
<input
type="checkbox"
name="lists[]"
value="<?php echo \esc_attr( $list['id'] ); ?>"
id="<?php echo \esc_attr( $checkbox_id ); ?>"
<?php
if ( isset( $list['checked'] ) && $list['checked'] ) {
echo 'checked';
}
?>
>
<strong><?php echo \esc_html( $list['title'] ); ?></strong>
<?php if ( ! empty( $list['description'] ) ) : ?>
<span class="newspack-ui__helper-text"><?php echo \esc_html( $list['description'] ); ?></span>
<?php endif; ?>
</label>
<?php
}
?>
<button type="submit" class="newspack-ui__button newspack-ui__button--wide newspack-ui__button--primary"><?php echo \esc_html( self::get_reader_activation_labels( 'newsletters_signup' ) ); ?></button>
</form>
</div>
Expand All @@ -1370,6 +1382,16 @@ public static function render_newsletters_signup_modal() {
if ( ! self::is_newsletters_signup_available() ) {
return;
}

$email_address = self::get_logged_in_reader_email_address();
if ( ! $email_address ) {
return;
}

$newsletters_lists = self::get_post_checkout_newsletter_lists( $email_address );
if ( empty( $newsletters_lists ) ) {
return;
}
?>
<div class="newspack-ui newspack-ui__modal-container newspack-newsletters-signup-modal">
<div class="newspack-ui__modal-container__overlay"></div>
Expand All @@ -1384,7 +1406,7 @@ public static function render_newsletters_signup_modal() {
</button>
</div>
<div class="newspack-ui__modal__content">
<?php self::render_newsletters_signup_form(); ?>
<?php self::render_newsletters_signup_form( $email_address, $newsletters_lists ); ?>
</div>
</div>
</div>
Expand Down Expand Up @@ -2169,5 +2191,20 @@ public static function rate_limit_lost_password( $errors, $user_data ) {
}
return $errors;
}

/**
* Gets the logged in reader's email address.
*
* @return string The reader's email address. Empty string if user is not logged in.
*/
private static function get_logged_in_reader_email_address() {
$email_address = '';

if ( \is_user_logged_in() && self::is_user_reader( \wp_get_current_user() ) ) {
$email_address = \wp_get_current_user()->user_email;
}

return $email_address;
}
}
Reader_Activation::init();

0 comments on commit 19005db

Please sign in to comment.