diff --git a/includes/class-newspack-newsletters.php b/includes/class-newspack-newsletters.php index 6462331ae..20f4deee6 100644 --- a/includes/class-newspack-newsletters.php +++ b/includes/class-newspack-newsletters.php @@ -560,5 +560,48 @@ public static function send_campaign( $id, $post ) { ]; $result = $mc->post( "campaigns/$mc_campaign_id/actions/send", $payload ); } + + /** + * Token replacement for newsletter templates. + * + * @param string $content Template content. + * @param array $extra Associative array of additional tokens to replace. + * @return string Content. + */ + public static function template_token_replacement( $content, $extra = [] ) { + $sitename = get_bloginfo( 'name' ); + $custom_logo_id = get_theme_mod( 'custom_logo' ); + $logo = $custom_logo_id ? wp_get_attachment_image_src( $custom_logo_id, 'full' )[0] : null; + + $sitename_block = sprintf( + '\n

%s

\n', + $sitename + ); + + $logo_block = $logo ? sprintf( + '
\"%s\"
', + $custom_logo_id, + $sitename, + $logo + ) : null; + + $search = array_merge( + [ + '__SITENAME__', + '__LOGO__', + '__LOGO_OR_SITENAME__', + ], + array_keys( $extra ) + ); + $replace = array_merge( + [ + $sitename, + $logo, + $logo ? $logo_block : $sitename_block, + ], + array_values( $extra ) + ); + return str_replace( $search, $replace, $content ); + } } Newspack_Newsletters::instance(); diff --git a/newspack-newsletters-templates.php b/newspack-newsletters-templates.php index 45ba18b78..819179015 100644 --- a/newspack-newsletters-templates.php +++ b/newspack-newsletters-templates.php @@ -23,25 +23,8 @@ function( $templates ) { add_filter( 'newspack_newsletters_templates', function( $templates ) { - $decode = json_decode( file_get_contents( NEWSPACK_NEWSLETTERS_PLUGIN_FILE . 'src/templates/template-1.json'), true ); //phpcs:ignore - $content = $decode['content']; - - $custom_logo_id = get_theme_mod( 'custom_logo' ); - $logo = wp_get_attachment_image_src( $custom_logo_id, 'full' )[0]; - - $sitename = get_bloginfo( 'name' ); - $content = str_replace( - [ - '__SITENAME__', - '__LOGO__', - ], - [ - $sitename, - $logo, - ], - $content - ); - + $decode = json_decode( file_get_contents( NEWSPACK_NEWSLETTERS_PLUGIN_FILE . 'src/templates/template-1.json'), true ); //phpcs:ignore + $content = Newspack_Newsletters::template_token_replacement( $decode['content'] ); $templates[] = [ 'content' => $content, 'title' => __( 'Template 1', 'newspack-newsletters' ), @@ -51,18 +34,3 @@ function( $templates ) { 10, 2 ); - -add_filter( - 'newspack_newsletters_templates', - function( $templates ) { - $decode = json_decode( file_get_contents( NEWSPACK_NEWSLETTERS_PLUGIN_FILE . 'src/templates/template-2.json'), true ); //phpcs:ignore - $content = $decode['content']; - $templates[] = [ - 'content' => $content, - 'title' => __( 'Template 2', 'newspack-newsletters' ), - ]; - return $templates; - }, - 10, - 2 -); diff --git a/src/templates/template-1.json b/src/templates/template-1.json index 82bf0ad15..f14f80e80 100644 --- a/src/templates/template-1.json +++ b/src/templates/template-1.json @@ -1,5 +1,5 @@ { "__file": "wp_block", - "title": "Template 1", - "content": "\n

__SITENAME__

\n\n\n\n
\"\"
\n\n\n\n

Cras id dui. Nulla consequat massa quis enim. Phasellus a est. Fusce a quam. Phasellus gravida semper nisi.

\n\n\n\n

Proin faucibus arcu quis ante. Pellentesque posuere. Nunc sed turpis. Etiam sit amet orci eget eros faucibus tincidunt. Pellentesque dapibus hendrerit tortor.

\n\n\n\n
\n\n\n\n

Aenean tellus metus, bibendum sed, posuere ac, mattis non, nunc. Phasellus consectetuer vestibulum elit. Phasellus leo dolor, tempus non, auctor et, hendrerit quis, nisi. Praesent nonummy mi in odio. Donec mollis hendrerit risus.

\n" + "title": "Template 2", + "content": "__LOGO_OR_SITENAME__\n

Start your newsletter with an intro, sometimes known as a “topper.” This is where the lead writer or editor for the newsletter introduces the main storylines for the day. You might want to consider adding some bullet points to highlight the biggest stories of the day so that a reader can easily scan through this section. Those bullets could include:

\n\n\n\n\n\n\n\n

You may also want to ask a key question here at the top if you’re soliciting story ideas or tips from readers.

\n\n\n\n

You should also consider having this section bylined — it can help establish a relationship between a reader and your staff, and readers may be more likely to reply to a person than a generic voice.

\n\n\n\n

—Byline

\n\n\n\n
\n\n\n\n

The main story section

\n\n\n\n
\"\"/
\n\n\n\n

You may want to have a lead story for the newsletter

\n\n\n\n

This is where you can add additional context or a dek to tease this story. Try to add at least or sentence or two to the dek.

\n\n\n\n

Latest headlines

\n\n\n\n
\n
\n
\"\"/
\n
\n\n\n\n
\n

Headline for the story goes here

\n\n\n\n

This is where you'll put the dek for secondary stories. Make sure you really describe what’s in the story and give readers a reason to click.

\n
\n
\n\n\n\n
\n
\n
\"\"/
\n
\n\n\n\n
\n

Headline for another story goes here

\n\n\n\n

This is where you'll put the dek for the second story. Focus on curation here — don’t send a reader too many links. The goal is to get readers to your site. Once they’re there, you can focus on driving deeper engagement.

\n
\n
\n\n\n\n
\n
\n
\"\"/
\n
\n\n\n\n
\n

Headline for a third story goes here

\n\n\n\n

How many links is too many? That’s worth testing out for yourself to see what works best for your audience, but start with about 5-7 links for this section and see how readers react.

\n
\n
\n\n\n\n
\n\n\n\n

Around the web

\n\n\n\n\n\n\n\n
\n\n\n\n

Support our newsroom

\n\n\n\n

Finally, make sure you have a call-to-action to support your work. This can be a few sentences like this, reminding readers of the value of your newsroom, or it can be an image. Either way, make sure you give a clear CTA and a unique link to track support back to this email.

\n\n\n\n
\n\n\n\n\n" } diff --git a/src/templates/template-2.json b/src/templates/template-2.json deleted file mode 100644 index 768cd88f2..000000000 --- a/src/templates/template-2.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "__file": "wp_block", - "title": "Template 2", - "content": "\n
\"\"/
\n\n\n\n

Start your newsletter with an intro, sometimes known as a “topper.” This is where the lead writer or editor for the newsletter introduces the main storylines for the day. You might want to consider adding some bullet points to highlight the biggest stories of the day so that a reader can easily scan through this section. Those bullets could include:

\n\n\n\n\n\n\n\n

You may also want to ask a key question here at the top if you’re soliciting story ideas or tips from readers.

\n\n\n\n

You should also consider having this section bylined — it can help establish a relationship between a reader and your staff, and readers may be more likely to reply to a person than a generic voice.

\n\n\n\n

—Byline

\n\n\n\n
\n\n\n\n

The main story section

\n\n\n\n
\"\"/
\n\n\n\n

You may want to have a lead story for the newsletter

\n\n\n\n

This is where you can add additional context or a dek to tease this story. Try to add at least or sentence or two to the dek.

\n\n\n\n

Latest headlines

\n\n\n\n
\n
\n
\"\"/
\n
\n\n\n\n
\n

Headline for the story goes here

\n\n\n\n

This is where you'll put the dek for secondary stories. Make sure you really describe what’s in the story and give readers a reason to click.

\n
\n
\n\n\n\n
\n
\n
\"\"/
\n
\n\n\n\n
\n

Headline for another story goes here

\n\n\n\n

This is where you'll put the dek for the second story. Focus on curation here — don’t send a reader too many links. The goal is to get readers to your site. Once they’re there, you can focus on driving deeper engagement.

\n
\n
\n\n\n\n
\n
\n
\"\"/
\n
\n\n\n\n
\n

Headline for a third story goes here

\n\n\n\n

How many links is too many? That’s worth testing out for yourself to see what works best for your audience, but start with about 5-7 links for this section and see how readers react.

\n
\n
\n\n\n\n
\n\n\n\n

Around the web

\n\n\n\n\n\n\n\n
\n\n\n\n

Support our newsroom

\n\n\n\n

Finally, make sure you have a call-to-action to support your work. This can be a few sentences like this, reminding readers of the value of your newsroom, or it can be an image. Either way, make sure you give a clear CTA and a unique link to track support back to this email.

\n\n\n\n
\n\n\n\n\n" -}