Skip to content

Instantly share code, notes, and snippets.

@drblue
Last active February 21, 2021 23:05
Show Gist options
  • Save drblue/48be0c192dea8f2cfa5e to your computer and use it in GitHub Desktop.
Save drblue/48be0c192dea8f2cfa5e to your computer and use it in GitHub Desktop.
Wordpress subscribe2 plugin digest template modification
<?php
/*
*
* fix s2 plugin to have template support
*
*/
// enable repeatable templating
function llab_s2_filter($mailtext, $post_ids = null) {
if (empty($post_ids)) return $mailtext;
$post_query = new WP_Query(array(
"post__in" => $post_ids,
"posts_per_page" => -1
));
$repeatable_block = file_get_contents(get_stylesheet_directory() . "/templates/email/repeatable-text-only.html");
$repeatable_block_with_thumbnail = file_get_contents(get_stylesheet_directory() . "/templates/email/repeatable-with-thumbnail.html");
$posts = array();
while ($post_query->have_posts()) {
$post_query->the_post();
$post_block = $repeatable_block;
if (has_post_thumbnail()) {
$post_block = $repeatable_block_with_thumbnail;
$post_thumbnail = get_the_post_thumbnail(get_the_ID(), array(200, 150), array("class" => "columnImage", "style" => "max-width:100%;"));
$post_block = str_replace("{POST_THUMBNAIL}", $post_thumbnail, $post_block);
}
$post_block = str_replace("{POST_PERMALINK}", get_the_permalink(), $post_block);
$post_block = str_replace("{POST_TITLE}", get_the_title(), $post_block);
$post_block = str_replace("{POST_DATE}", get_the_date(), $post_block);
$post_block = str_replace("{POST_AUTHOR}", get_the_author(), $post_block);
$post_block = str_replace("{POST_EXCERPT}", get_the_excerpt(), $post_block);
$posts[] = $post_block;
}
wp_reset_postdata();
return implode("", $posts);
}
add_filter("s2_custom_keywords", "llab_s2_filter", 10, 2);
// enable body templating
function llab_s2_html_email($html, $subject, $content) {
$skeleton = file_get_contents(get_stylesheet_directory() . "/templates/email/skeleton.html");
$html = str_replace("{CONTENT}", $content, $skeleton);
return $html;
}
add_filter("s2_html_email", "llab_s2_html_email", 10, 3);
// Uncomment below to NOT update posts with digest sent!
function llab_s2_update_post_status($ids) {
return array();
}
//add_filter("s2_update_post_status", "llab_s2_update_post_status");
// Uncomment below to NOT update when the last digest was sent!
function llab_last_s2cron_digest_post_ids($digest_ids, $current_s2cron_id) {
return $current_s2cron_id;
}
//add_filter("last_s2cron_digest_post_ids", "llab_last_s2cron_digest_post_ids", 10, 2);
// we likey html email, plz
function llab_s2_digest_mailtype($mailtype) {
return "html";
}
add_filter("s2_digest_mailtype", "llab_s2_digest_mailtype");
// get a REAL preview of what the digest would look like
function llab_s2_cron_preview_posts($posts, $instance) {
global $wpdb;
// set up SQL query based on options
if ( $instance->subscribe2_options['private'] == 'yes' ) {
$status = "'publish', 'private'";
} else {
$status = "'publish'";
}
// send notifications for allowed post type (defaults for posts and pages)
// uses s2_post_types filter to allow for custom post types in WP 3.0
if ( $instance->subscribe2_options['pages'] == 'yes' ) {
$s2_post_types = array('page', 'post');
} else {
$s2_post_types = array('post');
}
$s2_post_types = apply_filters('s2_post_types', $s2_post_types);
foreach ( $s2_post_types as $post_type ) {
if ( !isset($type) ) {
$type = $wpdb->prepare("%s", $post_type);
} else {
$type .= $wpdb->prepare(", %s", $post_type);
}
}
// collect posts
$sql = "SELECT ID, post_title, post_excerpt, post_content, post_type, post_password, post_date, post_author FROM $wpdb->posts AS a INNER JOIN $wpdb->postmeta AS b ON b.post_id = a.ID";
$sql .= " AND b.meta_key = '_s2_digest_post_status' AND b.meta_value = 'pending' WHERE post_status IN ($status) AND post_type IN ($type) ORDER BY post_date " . (($instance->subscribe2_options['cron_order'] === 'desc') ? 'DESC' : 'ASC');
$posts = $wpdb->get_results($sql);
return $posts;
}
add_filter("s2_cron_preview_posts", "llab_s2_cron_preview_posts", 10, 2);
// get ALL users, not just public or registered
function llab_s2_digest_recipients($recipients, $instance) {
return array_merge($recipients, $instance->get_all_registered("email"));
}
add_filter("s2_digest_recipients", "llab_s2_digest_recipients", 10, 2);
// set custom subject
function llab_s2_digest_subject($subject) {
return "Ett automagiskt veckobrev, bara för dig!";
}
add_filter("s2_digest_subject", "llab_s2_digest_subject", 10, 2);
@humbertorames
Copy link

is this plugin still functioning?

@drblue
Copy link
Author

drblue commented Feb 21, 2021

I honestly doubt it, this code is from September 2014.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment