Skip to content

Commit

Permalink
Merge pull request #2782 from Automattic/master
Browse files Browse the repository at this point in the history
Alpha release Nov 30
  • Loading branch information
miguelpeixe authored Nov 30, 2023
2 parents 5311828 + fcad059 commit d0c3b00
Show file tree
Hide file tree
Showing 28 changed files with 695 additions and 296 deletions.
18 changes: 16 additions & 2 deletions assets/blocks/reader-registration/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ function register_block() {
* Enqueue front-end scripts.
*/
function enqueue_scripts() {
// No need to enqueue scripts if Reader Activation is disabled and not a preview request.
if ( ! Reader_Activation::allow_reg_block_render() ) {
$should_enqueue_scripts = Reader_Activation::allow_reg_block_render();
/**
* Filters whether to enqueue the reader registration block scripts.
*
* @param bool $should_enqueue_scripts Whether to enqueue the reader registration block scripts.
*/
if ( ! apply_filters( 'newspack_enqueue_reader_activation_block', $should_enqueue_scripts ) ) {
return;
}

Expand Down Expand Up @@ -430,10 +435,19 @@ function process_form() {

$popup_id = isset( $_REQUEST['newspack_popup_id'] ) ? (int) $_REQUEST['newspack_popup_id'] : false;
$metadata['newspack_popup_id'] = $popup_id;

if ( $popup_id ) {
$metadata['registration_method'] = 'registration-block-popup';
}

/**
* Filters the metadata to be saved for a reader registered through the Reader Registration Block.
*
* @param array $metadata Metadata.
* @param string $email Email address of the reader.
*/
$metadata = apply_filters( 'newspack_register_reader_form_metadata', $metadata, $email );

$user_id = Reader_Activation::register_reader( $email, '', true, $metadata );

/**
Expand Down
106 changes: 106 additions & 0 deletions assets/memberships-gate/gate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Internal dependencies
*/
import './gate.scss';

/**
* Specify a function to execute when the DOM is fully loaded.
*
* @see https://github.com/WordPress/gutenberg/blob/trunk/packages/dom-ready/
*
* @param {Function} callback A function to execute after the DOM is ready.
* @return {void}
*/
function domReady( callback ) {
if ( typeof document === 'undefined' ) {
return;
}
if (
document.readyState === 'complete' || // DOMContentLoaded + Images/Styles/etc loaded, so we call directly.
document.readyState === 'interactive' // DOMContentLoaded fires at this point, so we call directly.
) {
return void callback();
}
// DOMContentLoaded has not fired yet, delay callback until then.
document.addEventListener( 'DOMContentLoaded', callback );
}

/**
* Adds 'memberships_content_gate' hidden input to every form inside the gate.
*
* @param {HTMLElement} gate The gate element.
*/
function addFormInputs( gate ) {
const forms = gate.querySelectorAll( 'form' );
forms.forEach( form => {
const input = document.createElement( 'input' );
input.type = 'hidden';
input.name = 'memberships_content_gate';
input.value = '1';
form.appendChild( input );
} );
}

/**
* Push gate 'seen' event to Google Analytics.
*/
function pushSeenEvent() {
const eventName = 'np_gate_interaction';
const payload = {
action: 'seen',
};
if ( 'function' === typeof window.gtag && payload ) {
window.gtag( 'event', eventName, payload );
}
}

/**
* Initializes the overlay gate.
*
* @param {HTMLElement} gate The gate element.
*/
function initOverlay( gate ) {
let entry = document.querySelector( '.entry-content' );
if ( ! entry ) {
entry = document.querySelector( '#content' );
}
gate.style.removeProperty( 'display' );
let seen = false;
const handleScroll = () => {
const delta = ( entry?.getBoundingClientRect().top || 0 ) - window.innerHeight / 2;
let visible = false;
if ( delta < 0 ) {
visible = true;
if ( ! seen ) {
pushSeenEvent();
}
seen = true;
}
gate.setAttribute( 'data-visible', visible );
};
document.addEventListener( 'scroll', handleScroll );
handleScroll();
}

domReady( function () {
const gate = document.querySelector( '.newspack-memberships__gate' );
if ( ! gate ) {
return;
}
addFormInputs( gate );

if ( gate.classList.contains( 'newspack-memberships__overlay-gate' ) ) {
initOverlay( gate );
} else {
// Seen event for inline gate.
const detectSeen = () => {
const delta = ( gate?.getBoundingClientRect().top || 0 ) - window.innerHeight / 2;
if ( delta < 0 ) {
pushSeenEvent();
document.removeEventListener( 'scroll', detectSeen );
}
};
document.addEventListener( 'scroll', detectSeen );
detectSeen();
}
} );
File renamed without changes.
21 changes: 5 additions & 16 deletions assets/memberships-gate/metering.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,12 @@ function lockContent() {
prompts.forEach( prompt => {
prompt.parentNode.removeChild( prompt );
} );
const visibleParagraphs = settings.visible_paragraphs;
const articleElements = document.querySelectorAll( '.entry-content > *' );
const moreIndex = content.innerHTML.indexOf( '<!--more-->' );
// Replace content.
content.innerHTML = settings.excerpt;
// Remove comments.
document.getElementById( 'comments' ).remove();
// Append inline gate, if any.
const inlineGate = document.querySelector( '.newspack-memberships__inline-gate' );
if ( moreIndex > -1 && settings.use_more_tag ) {
content.innerHTML = content.innerHTML.substring( 0, moreIndex );
} else {
let paragraphIndex = 0;
articleElements.forEach( element => {
if ( element.tagName === 'P' ) {
paragraphIndex++;
}
if ( paragraphIndex > visibleParagraphs ) {
content.removeChild( element );
}
} );
}
if ( inlineGate ) {
content.appendChild( inlineGate );
}
Expand Down
50 changes: 0 additions & 50 deletions assets/memberships-gate/overlay.js

This file was deleted.

8 changes: 6 additions & 2 deletions assets/reader-activation/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ window.newspackRAS.push( function ( readerActivation ) {
if ( ! body.has( 'npe' ) || ! body.get( 'npe' ) ) {
return form.endFlow( newspack_reader_auth_labels.invalid_email, 400 );
}
readerActivation.setReaderEmail( body.get( 'npe' ) );
if ( 'otp' === action ) {
readerActivation
.authenticateOTP( body.get( 'otp_code' ) )
Expand Down Expand Up @@ -425,9 +424,14 @@ window.newspackRAS.push( function ( readerActivation ) {
if ( currentHash ) {
redirect = '';
}
if ( status === 200 ) {
readerActivation.setReaderEmail( body.get( 'npe' ) );
}
const otpHash = readerActivation.getOTPHash();
if ( otpHash && [ 'register', 'link' ].includes( action ) ) {
setFormAction( 'otp' );
if ( status === 200 ) {
setFormAction( 'otp' );
}
/** If action is link, suppress message and status so the OTP handles it. */
if ( status === 200 && action === 'link' ) {
status = null;
Expand Down
3 changes: 3 additions & 0 deletions assets/wizards/popups/views/segments/segments-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ const SegmentActionCard = ( {
description={ segmentDescription( segment ) }
toggleChecked={ ! segment.configuration.is_disabled }
toggleOnChange={ () => toggleSegmentStatus( segment ) }
badge={
segment.is_criteria_duplicated ? __( 'Duplicate', 'newspack-plugin' ) : undefined
}
actionText={
<>
<Button
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions includes/class-donations.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static function init() {
add_filter( 'amp_skip_post', [ __CLASS__, 'should_skip_amp' ], 10, 2 );
add_filter( 'newspack_blocks_donate_billing_fields_keys', [ __CLASS__, 'get_billing_fields' ] );
add_action( 'woocommerce_checkout_create_order_line_item', [ __CLASS__, 'checkout_create_order_line_item' ], 10, 4 );
add_action( 'woocommerce_coupons_enabled', [ __CLASS__, 'disable_coupons' ] );
}
}

Expand Down Expand Up @@ -1036,5 +1037,23 @@ public static function update_billing_fields( $billing_fields ) {
update_option( self::DONATION_BILLING_FIELDS_OPTION, $billing_fields );
return $billing_fields;
}

/**
* Disable coupons for donation checkouts.
*
* @param bool $enabled Whether coupons are enabled.
*
* @return bool
*/
public static function disable_coupons( $enabled ) {
$cart = WC()->cart;
if ( ! $cart ) {
return $enabled;
}
if ( ! self::is_donation_cart( $cart ) ) {
return $enabled;
}
return false;
}
}
Donations::init();
12 changes: 10 additions & 2 deletions includes/class-magic-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class Magic_Link {
const AUTH_ACTION_RESULT = 'np_auth_link_result';
const COOKIE = 'np_auth_link';

const RATE_INTERVAL = 60; // Interval in seconds to rate limit token generation.

const OTP_LENGTH = 6;
const OTP_MAX_ATTEMPTS = 5;
const OTP_AUTH_ACTION = 'np_otp_auth';
Expand Down Expand Up @@ -346,11 +348,15 @@ public static function generate_token( $user ) {
if ( ! empty( $tokens ) ) {
/** Limit maximum tokens to 5. */
$tokens = array_slice( $tokens, -4, 4 );
/** Clear expired tokens. */
foreach ( $tokens as $index => $token_data ) {
/** Clear expired tokens. */
if ( $token_data['time'] < $expire ) {
unset( $tokens[ $index ] );
}
/** Rate limit token generation. */
if ( $token_data['time'] + self::RATE_INTERVAL > $now ) {
return new \WP_Error( 'rate_limit_exceeded', __( 'Please wait a minute before requesting another authorization code.', 'newspack' ) );
}
}
$tokens = array_values( $tokens );
}
Expand Down Expand Up @@ -538,7 +544,9 @@ public static function validate_otp( $user_id, $hash, $code ) {
$errors->add( 'invalid_otp', __( 'OTP is not enabled.', 'newspack' ) );
} else {
$tokens = \get_user_meta( $user->ID, self::TOKENS_META, true );
if ( empty( $tokens ) || empty( $hash ) || empty( $code ) ) {
if ( empty( $tokens ) || empty( $hash ) ) {
$errors->add( 'invalid_hash', __( 'Invalid hash.', 'newspack' ) );
} elseif ( empty( $code ) ) {
$errors->add( 'invalid_otp', __( 'Invalid OTP.', 'newspack' ) );
}
}
Expand Down
1 change: 1 addition & 0 deletions includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private function includes() {
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-api.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/listeners.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-popups.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/class-memberships.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/connectors/ga4/class-ga4.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/connectors/class-mailchimp.php';
include_once NEWSPACK_ABSPATH . 'includes/data-events/connectors/class-activecampaign.php';
Expand Down
10 changes: 8 additions & 2 deletions includes/class-recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public static function add_recaptcha_to_checkout() {
<script>
grecaptcha.ready( function() {
var field;
setInterval( function() {
function refreshToken() {
grecaptcha.execute(
'<?php echo \esc_attr( $site_key ); ?>',
{ action: 'checkout' }
Expand All @@ -332,7 +332,13 @@ public static function add_recaptcha_to_checkout() {
field.value = token;
}
} );
}, 30000 );
}
setInterval( refreshToken, 30000 );
( function( $ ) {
if ( ! $ ) { return; }
$( document ).on( 'updated_checkout', refreshToken );
$( document.body ).on( 'checkout_error', refreshToken );
} )( jQuery );
grecaptcha.execute(
'<?php echo \esc_attr( $site_key ); ?>',
{ action: 'checkout' }
Expand Down
Loading

0 comments on commit d0c3b00

Please sign in to comment.