Skip to content

Commit

Permalink
feat: merge in updates from epic/ras-acc
Browse files Browse the repository at this point in the history
  • Loading branch information
laurelfulford committed Apr 21, 2024
2 parents 9e7bdd4 + 507fd5a commit 130b79a
Show file tree
Hide file tree
Showing 14 changed files with 1,412 additions and 306 deletions.
5 changes: 5 additions & 0 deletions assets/other-scripts/emails/style.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.editor-post-title {
display: none;
}

.editor-styles-wrapper .block-editor-block-list__layout .wp-block-button {
margin-left: 0 !important;
margin-right: 0 !important;
}
32 changes: 30 additions & 2 deletions assets/wizards/engagement/views/reader-activation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { ExternalLink } from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { ExternalLink } from '@wordpress/components';
import { useEffect, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -20,6 +20,7 @@ import {
TextControl,
Waiting,
withWizardScreen,
utils,
} from '../../../../components/src';
import Prerequisite from '../../components/prerequisite';
import ActiveCampaign from '../../components/active-campaign';
Expand All @@ -39,6 +40,7 @@ export default withWizardScreen( ( { wizardApiFetch } ) => {
const [ prerequisites, setPrerequisites ] = useState( null );
const [ missingPlugins, setMissingPlugins ] = useState( [] );
const [ showAdvanced, setShowAdvanced ] = useState( false );

const updateConfig = ( key, val ) => {
setConfig( { ...config, [ key ]: val } );
};
Expand Down Expand Up @@ -73,6 +75,18 @@ export default withWizardScreen( ( { wizardApiFetch } ) => {
.catch( setError )
.finally( () => setInFlight( false ) );
};
const resetEmail = postId => {
setError( false );
setInFlight( true );
wizardApiFetch( {
path: `/newspack/v1/wizard/newspack-engagement-wizard/reader-activation/emails/${ postId }`,
method: 'DELETE',
quiet: true,
} )
.then( emails => setConfig( { ...config, emails } ) )
.catch( setError )
.finally( () => setInFlight( false ) );
};
useEffect( () => {
window.scrollTo( 0, 0 );
fetchConfig();
Expand Down Expand Up @@ -284,6 +298,20 @@ export default withWizardScreen( ( { wizardApiFetch } ) => {
href={ email.edit_link }
description={ email.description }
actionText={ __( 'Edit', 'newspack-plugin' ) }
onSecondaryActionClick={ () => {
if (
utils.confirmAction(
__(
'Are you sure you want to reset the contents of this email?',
'newspack-plugin'
)
)
) {
resetEmail( email.post_id );
}
} }
secondaryActionText={ __( 'Reset', 'newspack-plugin' ) }
secondaryDestructive={ true }
isSmall
/>
) ) }
Expand Down
37 changes: 32 additions & 5 deletions assets/wizards/readerRevenue/views/emails/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import values from 'lodash/values';
/**
* Internal dependencies
*/
import { PluginInstaller, ActionCard, Notice } from '../../../../components/src';
import { PluginInstaller, ActionCard, Notice, utils } from '../../../../components/src';

const EMAILS = values( newspack_reader_revenue.emails );
const postType = newspack_reader_revenue.email_cpt;
Expand Down Expand Up @@ -47,6 +47,18 @@ const Emails = () => {
.catch( setError )
.finally( () => setInFlight( false ) );
};
const resetEmail = postId => {
setError( false );
setInFlight( true );
apiFetch( {
path: `/newspack/v1/wizard/newspack-reader-revenue-wizard/donations/emails/${ postId }`,
method: 'DELETE',
quiet: true,
} )
.then( result => setEmails( values( result ) ) )
.catch( setError )
.finally( () => setInFlight( false ) );
};

if ( false === pluginsReady ) {
return (
Expand Down Expand Up @@ -75,6 +87,10 @@ const Emails = () => {
<>
{ emails.map( email => {
const isActive = email.status === 'publish';
const notification =
email.type === 'receipt'
? __( 'This email is not active. The default receipt will be used.', 'newspack-plugin' )
: __( 'This email is not active.', 'newspack-plugin' );
return (
<ActionCard
key={ email.post_id }
Expand All @@ -84,15 +100,26 @@ const Emails = () => {
href={ email.edit_link }
description={ email.description }
actionText={ __( 'Edit', 'newspack' ) }
secondaryActionText={ __( 'Reset', 'newspack' ) }
onSecondaryActionClick={ () => {
if (
utils.confirmAction(
__(
'Are you sure you want to reset the contents of this email?',
'newspack-plugin'
)
)
) {
resetEmail( email.post_id );
}
} }
secondaryDestructive={ true }
toggleChecked={ isActive }
toggleOnChange={ value => updateStatus( email.post_id, value ? 'publish' : 'draft' ) }
{ ...( isActive
? {}
: {
notification: __(
'This email is not active. The default receipt will be used.',
'newspack'
),
notification,
notificationLevel: 'info',
} ) }
>
Expand Down
11 changes: 11 additions & 0 deletions includes/class-magic-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ public static function send_email( $user, $redirect_to = '', $use_otp = true ) {
if ( \is_wp_error( $token_data ) ) {
return $token_data;
}

$key = \get_password_reset_key( $user );
if ( is_wp_error( $key ) ) {
return $key;
}

$url = \add_query_arg(
[
'action' => self::AUTH_ACTION,
Expand All @@ -427,6 +433,11 @@ public static function send_email( $user, $redirect_to = '', $use_otp = true ) {
'template' => '*MAGIC_LINK_URL*',
'value' => $url,
],
[
'template' => '*SET_PASSWORD_LINK*',
'value' => Emails::get_password_reset_url( $user, $key ),
],

];
if ( $use_otp && ! empty( $token_data['otp'] ) ) {
$email_type = 'OTP_AUTH';
Expand Down
150 changes: 147 additions & 3 deletions includes/emails/class-emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public static function init() {
add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'enqueue_block_editor_assets' ] );
add_filter( 'newspack_newsletters_email_editor_cpts', [ __CLASS__, 'register_email_cpt_with_email_editor' ] );
add_filter( 'newspack_newsletters_allowed_editor_actions', [ __CLASS__, 'register_scripts_enqueue_with_email_editor' ] );
add_action( 'update_option_theme_mods_' . get_template(), [ __CLASS__, 'maybe_update_email_templates' ], 10, 2 );
add_action( 'admin_head', [ __CLASS__, 'inject_dynamic_email_template_styles' ] );
}

/**
Expand Down Expand Up @@ -170,20 +172,50 @@ public static function get_email_payload( $config_name, $placeholders = [] ) {
$email_config = self::get_email_config_by_type( $config_name );
$html = $email_config['html_payload'];
$reply_to_email = $email_config['reply_to_email'];
$placeholders = array_merge(

if ( class_exists( 'WC' ) ) {
$base_address = WC()->countries->get_base_address();
$base_city = WC()->countries->get_base_city();
$base_postcode = WC()->countries->get_base_postcode();

$site_address = sprintf(
// translators: formatted store address where 1 is street address, 2 is city, and 3 is postcode.
__( '%1$s, %2$s %3$s', 'newspack' ),
$base_address,
$base_city,
$base_postcode
);
} else {
$site_address = sprintf(
// translators: formatted store address where 1 is street address, 2 is city, and 3 is postcode.
__( '%1$s, %2$s %3$s', 'newspack' ),
get_option( 'woocommerce_store_address', '' ),
get_option( 'woocommerce_store_city', '' ),
get_option( 'woocommerce_store_postcode', '' )
);
}
$placeholders = array_merge(
[
[
'template' => '*CONTACT_EMAIL*',
'value' => sprintf( '<a href="mailto:%s">%s</a>', $reply_to_email, $reply_to_email ),
],
[
'template' => '*SITE_URL*',
'value' => get_site_url(),
'template' => '*SITE_ADDRESS*',
'value' => $site_address,
],
[
'template' => '*SITE_LOGO*',
'value' => esc_url( wp_get_attachment_url( get_theme_mod( 'custom_logo' ) ) ),
],
[
'template' => '*SITE_TITLE*',
'value' => get_bloginfo( 'name' ),
],
[
'template' => '*SITE_URL*',
'value' => get_bloginfo( 'wpurl' ),
],
],
$placeholders
);
Expand All @@ -209,6 +241,35 @@ public static function send_email( $config_name, $to, $placeholders = [] ) {
return false;
}

// Migrate to RAS-ACC email templates if migration option is not set AND there have been no manual updates to the templates.
if ( get_option( 'newspack_email_templates_migrated', '' ) !== 'v1' ) {
$migrated = true;
$templates = get_posts(
[
'post_type' => self::POST_TYPE,
'posts_per_page' => -1,
'post_status' => 'publish',
]
);

foreach ( $templates as $template ) {
$publish_date = get_the_date( 'Y-m-d H:i:s', $template->ID );
$last_modified_date = get_the_modified_date( 'Y-m-d H:i:s', $template->ID );

// Template has not been modified, so trash the post so we can trigger a template update.
if ( $publish_date === $last_modified_date ) {
if ( ! wp_trash_post( $template->ID ) ) {
// Flag the migration as failed so we can trigger another attempt later.
$migrated = false;
}
}
}

if ( $migrated ) {
update_option( 'newspack_email_templates_migrated', 'v1' );
}
}

$switched_locale = \switch_to_locale( \get_user_locale( \wp_get_current_user() ) );

if ( 'string' === gettype( $config_name ) ) {
Expand Down Expand Up @@ -335,6 +396,7 @@ private static function serialize_email( $type = null, $post_id = 0 ) {
$edit_link = str_replace( site_url(), '', $post_link );
}
$serialized_email = [
'type' => $type,
'label' => $email_config['label'],
'description' => $email_config['description'],
'post_id' => $post_id,
Expand Down Expand Up @@ -557,5 +619,87 @@ public static function get_password_reset_url( $user, $key ) {

return wp_lostpassword_url();
}

/**
* Trigger an update to all email template posts when theme color is updated in customizer.
* This is to force an update of dynamic properties such as theme colors.
*
* @param string|array $previous_value previous option value.
* @param string|array $updated_value updated option value.
*
* @return void
*/
public static function maybe_update_email_templates( $previous_value, $updated_value ) {
// Check for theme mod color settings in case a non-newspack theme is installed.
if ( ! isset( $previous_value['primary_color_hex'], $previous_value['secondary_color_hex'], $updated_value['primary_color_hex'], $updated_value['secondary_color_hex'] ) ) {
return;
}

if ( $previous_value['primary_color_hex'] !== $updated_value['primary_color_hex'] || $previous_value['secondary_color_hex'] !== $updated_value['secondary_color_hex'] ) {
$templates = get_posts(
[
'post_type' => self::POST_TYPE,
'posts_per_page' => -1,
'post_status' => 'publish',
]
);

foreach ( $templates as $template ) {
wp_update_post( [ 'ID' => $template->ID ] );
}

if ( class_exists( 'Newspack_Newsletters' ) ) {
// Update newsletters color palette option so emails reflect the new colors.
$request = new \WP_REST_Request( 'POST', '/newspack-newsletters/v1/color-palette' );
$request->set_body(
wp_json_encode(
[
'primary' => $updated_value['primary_color_hex'],
'secondary' => $updated_value['secondary_color_hex'],
'primary-text' => newspack_get_color_contrast( $updated_value['primary_color_hex'] ),
'secondary-text' => newspack_get_color_contrast( $updated_value['secondary_color_hex'] ),
]
)
);

$response = rest_do_request( $request );

if ( $response->is_error() ) {
Logger::error( 'Error updating newsletters color palette: ' . $response->as_error()->get_error_message() );
}
}
}
}

/**
* Inject dynamic email template styles for dynamic text colors in the editor.
*
* @return void
*/
public static function inject_dynamic_email_template_styles() {
if ( get_post_type() !== self::POST_TYPE ) {
return;
}

[
'primary_text_color' => $primary_text_color,
'secondary_text_color' => $secondary_text_color,
] = newspack_get_theme_colors();

?>
<style type="text/css">
.<?php echo esc_html( self::POST_TYPE ); ?>-has-primary-text-color,
.<?php echo esc_html( self::POST_TYPE ); ?>-has-primary-text-color a {
color: <?php echo esc_attr( $primary_text_color ); ?> !important;
}

.<?php echo esc_html( self::POST_TYPE ); ?>-has-secondary-text-color,
.<?php echo esc_html( self::POST_TYPE ); ?>-has-secondary-text-color a {
color: <?php echo esc_attr( $secondary_text_color ); ?> !important;
}
</style>
<?php
}
}

Emails::init();
Loading

0 comments on commit 130b79a

Please sign in to comment.