Skip to content

Commit

Permalink
feat: token replace function, logo or sitename token
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonrabb committed Apr 14, 2020
1 parent 7ae882a commit 0bc8875
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
43 changes: 43 additions & 0 deletions includes/class-newspack-newsletters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<!-- wp:heading {\"align\":\"center\",\"level\":1} -->\n<h1 class=\"has-text-align-center\">%s</h1>\n<!-- /wp:heading -->',
$sitename
);

$logo_block = $logo ? sprintf(
'<!-- wp:image {\"align\":\"center\",\"id\":%s,\"sizeSlug\":\"large\"} --><figure class=\"wp-block-image aligncenter size-large\"><img src=\"%s\" alt=\"%s\" /></figure><!-- /wp:image -->',
$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();
36 changes: 2 additions & 34 deletions newspack-newsletters-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand All @@ -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
);
4 changes: 2 additions & 2 deletions src/templates/template-1.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"__file": "wp_block",
"title": "Template 1",
"content": "<!-- wp:heading {\"align\":\"center\",\"level\":1} -->\n<h1 class=\"has-text-align-center\">__SITENAME__</h1>\n<!-- /wp:heading -->\n\n<!-- wp:image {\"align\":\"center\",\"id\":4533,\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image aligncenter size-large\"><img src=\"__LOGO__\" alt=\"\" /></figure>\n<!-- /wp:image -->\n\n<!-- wp:paragraph -->\n<p>Cras id dui. Nulla consequat massa quis enim. Phasellus a est. Fusce a quam. Phasellus gravida semper nisi.</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Proin faucibus arcu quis ante. Pellentesque posuere. Nunc sed turpis. Etiam sit amet orci eget eros faucibus tincidunt. Pellentesque dapibus hendrerit tortor.</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:separator {\"className\":\"is-style-wide\"} -->\n<hr class=\"wp-block-separator is-style-wide\"/>\n<!-- /wp:separator -->\n\n<!-- wp:paragraph -->\n<p>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.</p>\n<!-- /wp:paragraph -->"
"title": "Template 2",
"content": "__LOGO_OR_SITENAME__<!-- wp:paragraph -->\n<p>Start your newsletter with an intro, sometimes known as&nbsp;a “topper.” This is where the lead writer or editor for the newsletter introduces the main storylines&nbsp;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:</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:list -->\n<ul><li>Facts or figures about COVID-19 in your community</li><li>Any notable news in your area — openings/closings of key businesses, for instance</li><li>And anything else you want to make sure readers definitely see</li></ul>\n<!-- /wp:list -->\n\n<!-- wp:paragraph -->\n<p>You may also want to ask a key question here at the top if you’re soliciting story ideas or tips from readers.</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>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.</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>—Byline</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:separator {\"className\":\"is-style-wide\"} -->\n<hr class=\"wp-block-separator is-style-wide\"/>\n<!-- /wp:separator -->\n\n<!-- wp:heading -->\n<h2>The main story section</h2>\n<!-- /wp:heading -->\n\n<!-- wp:image -->\n<figure class=\"wp-block-image\"><img alt=\"\"/></figure>\n<!-- /wp:image -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3>You may want to have a lead story for the newsletter</h3>\n<!-- /wp:heading -->\n\n<!-- wp:paragraph -->\n<p>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.</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3>Latest headlines</h3>\n<!-- /wp:heading -->\n\n<!-- wp:columns -->\n<div class=\"wp-block-columns\"><!-- wp:column {\"width\":33.33} -->\n<div class=\"wp-block-column\" style=\"flex-basis:33.33%\"><!-- wp:image -->\n<figure class=\"wp-block-image\"><img alt=\"\"/></figure>\n<!-- /wp:image --></div>\n<!-- /wp:column -->\n\n<!-- wp:column {\"width\":66.66} -->\n<div class=\"wp-block-column\" style=\"flex-basis:66.66%\"><!-- wp:heading {\"level\":4} -->\n<h4>Headline for the story goes here</h4>\n<!-- /wp:heading -->\n\n<!-- wp:paragraph -->\n<p>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.</p>\n<!-- /wp:paragraph --></div>\n<!-- /wp:column --></div>\n<!-- /wp:columns -->\n\n<!-- wp:columns -->\n<div class=\"wp-block-columns\"><!-- wp:column {\"width\":33.33} -->\n<div class=\"wp-block-column\" style=\"flex-basis:33.33%\"><!-- wp:image -->\n<figure class=\"wp-block-image\"><img alt=\"\"/></figure>\n<!-- /wp:image --></div>\n<!-- /wp:column -->\n\n<!-- wp:column {\"width\":66.66} -->\n<div class=\"wp-block-column\" style=\"flex-basis:66.66%\"><!-- wp:heading {\"level\":4} -->\n<h4>Headline for another story goes here</h4>\n<!-- /wp:heading -->\n\n<!-- wp:paragraph -->\n<p>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.</p>\n<!-- /wp:paragraph --></div>\n<!-- /wp:column --></div>\n<!-- /wp:columns -->\n\n<!-- wp:columns -->\n<div class=\"wp-block-columns\"><!-- wp:column {\"width\":33.33} -->\n<div class=\"wp-block-column\" style=\"flex-basis:33.33%\"><!-- wp:image -->\n<figure class=\"wp-block-image\"><img alt=\"\"/></figure>\n<!-- /wp:image --></div>\n<!-- /wp:column -->\n\n<!-- wp:column {\"width\":66.66} -->\n<div class=\"wp-block-column\" style=\"flex-basis:66.66%\"><!-- wp:heading {\"level\":4} -->\n<h4>Headline for a third story goes here</h4>\n<!-- /wp:heading -->\n\n<!-- wp:paragraph -->\n<p>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.</p>\n<!-- /wp:paragraph --></div>\n<!-- /wp:column --></div>\n<!-- /wp:columns -->\n\n<!-- wp:separator {\"className\":\"is-style-wide\"} -->\n<hr class=\"wp-block-separator is-style-wide\"/>\n<!-- /wp:separator -->\n\n<!-- wp:heading -->\n<h2>Around the web</h2>\n<!-- /wp:heading -->\n\n<!-- wp:list -->\n<ul><li>You may want to curate some links from other news outlets to provide additional value [<a rel=\"noreferrer noopener\" href=\"http://link goes here\" target=\"_blank\">News outlet #1</a>]</li><li>Again, try to focus on curation here — this shouldn't be a pure link dump&nbsp;[<a rel=\"noreferrer noopener\" href=\"http://link goes here\" target=\"_blank\">News outlet #2</a>]</li><li>How many is right for this sort of section? [<a rel=\"noreferrer noopener\" href=\"http://link goes here\" target=\"_blank\">News outlet #3</a>]</li><li>Probably anywhere from 5 to 7 stories, depending on the day [<a rel=\"noreferrer noopener\" href=\"http://link goes here\" target=\"_blank\">News outlet #4</a>]</li><li>You could also move this section elsewhere in the newsletter, if you’d like — it doesn't have to come at the bottom [<a rel=\"noreferrer noopener\" href=\"http://link goes here\" target=\"_blank\">News outlet #5</a>]</li></ul>\n<!-- /wp:list -->\n\n<!-- wp:separator {\"className\":\"is-style-wide\"} -->\n<hr class=\"wp-block-separator is-style-wide\"/>\n<!-- /wp:separator -->\n\n<!-- wp:heading -->\n<h2>Support our newsroom</h2>\n<!-- /wp:heading -->\n\n<!-- wp:paragraph -->\n<p>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.</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:separator {\"className\":\"is-style-wide\"} -->\n<hr class=\"wp-block-separator is-style-wide\"/>\n<!-- /wp:separator -->\n\n<!-- wp:social-links {\"align\":\"center\"} -->\n<ul class=\"wp-block-social-links aligncenter\"><!-- wp:social-link {\"url\":\"#\",\"service\":\"facebook\"} /-->\n\n<!-- wp:social-link {\"url\":\"#\",\"service\":\"twitter\"} /-->\n\n<!-- wp:social-link {\"url\":\"#\",\"service\":\"instagram\"} /-->\n\n<!-- wp:social-link {\"url\":\"#\",\"service\":\"youtube\"} /--></ul>\n<!-- /wp:social-links -->"
}
5 changes: 0 additions & 5 deletions src/templates/template-2.json

This file was deleted.

0 comments on commit 0bc8875

Please sign in to comment.