Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion assets/blocks/reader-registration/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
"lists": {
"type": "array",
"default": [],
"items": { "type": "string" }
"items": {
"type": "string"
}
},
"haveAccountLabel": {
"type": "string",
Expand All @@ -48,6 +50,10 @@
"signInLabel": {
"type": "string",
"default": "Sign in"
},
"signedInLabel": {
"type": "string",
"default": "An account was already registered with this email. Please check your inbox for an authentication link."
}
},
"supports": {
Expand Down
34 changes: 25 additions & 9 deletions assets/blocks/reader-registration/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import './editor.scss';

const editedStateOptions = [
{ label: __( 'Initial', 'newspack' ), value: 'initial' },
{ label: __( 'Success', 'newspack' ), value: 'success' },
{ label: __( 'Registration Success', 'newspack' ), value: 'registration' },
{ label: __( 'Login Success', 'newspack' ), value: 'login' },
];
export default function ReaderRegistrationEdit( {
setAttributes,
Expand All @@ -48,13 +49,13 @@ export default function ReaderRegistrationEdit( {
newsletterLabel,
haveAccountLabel,
signInLabel,
signedInLabel,
lists,
className,
},
} ) {
const blockProps = useBlockProps();
const [ editedState, setEditedState ] = useState( editedStateOptions[ 0 ].value );
const isInitial = editedState === 'initial';

const innerBlocksProps = useInnerBlocksProps(
{},
Expand Down Expand Up @@ -176,12 +177,15 @@ export default function ReaderRegistrationEdit( {
<div className="newspack-registration__state-bar">
<span>{ __( 'Edited State', 'newspack' ) }</span>
<div>
<Button data-is-active={ isInitial } onClick={ () => setEditedState( 'initial' ) }>
{ __( 'Initial', 'newspack' ) }
</Button>
<Button data-is-active={ ! isInitial } onClick={ () => setEditedState( 'success' ) }>
{ __( 'Success', 'newspack' ) }
</Button>
{ editedStateOptions.map( option => (
<Button
key={ option.value }
data-is-active={ editedState === option.value }
onClick={ () => setEditedState( option.value ) }
>
{ option.label }
</Button>
) ) }
</div>
</div>
{ editedState === 'initial' && (
Expand Down Expand Up @@ -292,12 +296,24 @@ export default function ReaderRegistrationEdit( {
</form>
</div>
) }
{ editedState === 'success' && (
{ editedState === 'registration' && (
<>
<div className="newspack-registration__icon" />
<div { ...innerBlocksProps } />
</>
) }
{ editedState === 'login' && (
<>
<div className="newspack-registration__icon" />
<RichText
align="center"
onChange={ value => setAttributes( { signedInLabel: value } ) }
placeholder={ __( 'Logged in message…', 'newspack' ) }
value={ signedInLabel }
tagName="p"
/>
</>
) }
</div>
</>
);
Expand Down
7 changes: 7 additions & 0 deletions assets/blocks/reader-registration/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function render_block( $attrs, $content ) {
'newsletterLabel' => __( 'Subscribe to our newsletter', 'newspack' ),
'haveAccountLabel' => __( 'Already have an account?', 'newspack' ),
'signInLabel' => __( 'Sign in', 'newspack' ),
'signedInLabel' => __( 'An account was already registered with this email. Please check your inbox for an authentication link.', 'newspack' ),
];
$attrs = \wp_parse_args( $attrs, $default_attrs );
foreach ( $default_attrs as $key => $value ) {
Expand Down Expand Up @@ -190,6 +191,10 @@ function render_block( $attrs, $content ) {
<div class="newspack-registration__icon"></div>
<?php echo \wp_kses_post( $success_markup ); ?>
</div>
<div class="newspack-login__success newspack-registration--hidden">
<div class="newspack-registration__icon"></div>
<p class="has-text-align-center"><?php echo \wp_kses_post( $attrs['signedInLabel'] ); ?></p>
</div>
<?php endif; ?>
</div>
<?php
Expand Down Expand Up @@ -245,6 +250,7 @@ function send_form_response( $data, $message = '' ) {
[
'newspack_reader' => $is_error ? '0' : '1',
'message' => $message,
'existing_user' => isset( $data['existing_user'] ) && boolval( $data['existing_user'] ) ? 1 : 0,
],
\remove_query_arg( $args_to_remove )
)
Expand Down Expand Up @@ -297,6 +303,7 @@ function process_form() {
[
'email' => $email,
'authenticated' => $user_logged_in,
'existing_user' => ! $user_logged_in,
]
);
}
Expand Down
6 changes: 5 additions & 1 deletion assets/blocks/reader-registration/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function domReady( callback ) {

const messageElement = container.querySelector( '.newspack-registration__response' );
const submitElement = form.querySelector( 'input[type="submit"]' );
const successElement = container.querySelector( '.newspack-registration__success' );
let successElement = container.querySelector( '.newspack-registration__success' );

readerActivation.on( 'reader', ( { detail: { authenticated } } ) => {
if ( authenticated ) {
Expand All @@ -58,6 +58,10 @@ function domReady( callback ) {
messageNode = document.createElement( 'div' );
messageNode.textContent = message;
}
if ( data?.existing_user ) {
successElement = document.querySelector( '.newspack-login__success' );
}

const isSuccess = status === 200;
container.classList.add( `newspack-registration--${ isSuccess ? 'success' : 'error' }` );
if ( isSuccess ) {
Expand Down
1 change: 1 addition & 0 deletions includes/class-reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ public static function register_reader( $email, $display_name = '', $authenticat
$user_id = false;

if ( $existing_user ) {
Logger::log( "User with $email already exists. Sending magic link." );
Magic_Link::send_email( $existing_user );
} else {
/**
Expand Down