Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Sends an email notification of a new post
- */
- function publish($post, $preview = '') {
- if ( !$post ) { return $post; }
- if ( $this->s2_mu && !apply_filters('s2_allow_site_switching', $this->site_switching) ) {
- global $switched;
- if ( $switched ) { return; }
- }
- if ( $preview == '' ) {
- // we aren't sending a Preview to the current user so carry out checks
- $s2mail = get_post_meta($post->ID, '_s2mail', true);
- if ( (isset($_POST['s2_meta_field']) && $_POST['s2_meta_field'] == 'no') || strtolower(trim($s2mail)) == 'no' ) { return $post; }
- // are we doing daily digests? If so, don't send anything now
- if ( $this->subscribe2_options['email_freq'] != 'never' ) { return $post; }
- // is the current post of a type that should generate a notification email?
- // uses s2_post_types filter to allow for custom post types in WP 3.0
- if ( $this->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);
- if ( !in_array($post->post_type, $s2_post_types) ) {
- return $post;
- }
- // Are we sending notifications for password protected posts?
- if ( $this->subscribe2_options['password'] == "no" && $post->post_password != '' ) {
- return $post;
- }
- // Is the post assigned to a format for which we should not be sending posts
- $post_format = get_post_format($post->ID);
- $excluded_formats = explode(',', $this->subscribe2_options['exclude_formats']);
- if ( $post_format !== false && in_array($post_format, $excluded_formats) ) {
- return $post;
- }
- $s2_taxonomies = apply_filters('s2_taxonomies', array('category'));
- $post_cats = wp_get_object_terms($post->ID, $s2_taxonomies, array('fields' => 'ids'));
- $check = false;
- // is the current post assigned to any categories
- // which should not generate a notification email?
- foreach ( explode(',', $this->subscribe2_options['exclude']) as $cat ) {
- if ( in_array($cat, $post_cats) ) {
- $check = true;
- }
- }
- if ( $check ) {
- // hang on -- can registered users subscribe to
- // excluded categories?
- if ( '0' == $this->subscribe2_options['reg_override'] ) {
- // nope? okay, let's leave
- return $post;
- }
- }
- // Are we sending notifications for Private posts?
- // Action is added if we are, but double check option and post status
- if ( $this->subscribe2_options['private'] == "yes" && $post->post_status == 'private' ) {
- // don't send notification to public users
- $check = true;
- }
- // lets collect our subscribers
- $public = array();
- if ( !$check ) {
- // if this post is assigned to an excluded
- // category, or is a private post then
- // don't send public subscribers a notification
- $public = $this->get_public();
- }
- if ( $post->post_type == 'page' ) {
- $post_cats_string = implode(',', get_all_category_ids());
- } else {
- $post_cats_string = implode(',', $post_cats);
- }
- $registered = $this->get_registered("cats=$post_cats_string");
- // do we have subscribers?
- if ( empty($public) && empty($registered) ) {
- // if not, no sense doing anything else
- return $post;
- }
- } else {
- // make sure we prime the taxonomy variable for preview posts
- $s2_taxonomies = apply_filters('s2_taxonomies', array('category'));
- }
- // we set these class variables so that we can avoid
- // passing them in function calls a little later
- $this->post_title = "<a href=\"" . get_permalink($post->ID) . "\">" . html_entity_decode($post->post_title, ENT_QUOTES) . "</a>";
- $this->permalink = get_permalink($post->ID);
- $this->post_date = get_the_time(get_option('date_format'), $post);
- $this->post_time = get_the_time('', $post);
- $author = get_userdata($post->post_author);
- $this->authorname = html_entity_decode(apply_filters('the_author', $author->display_name), ENT_QUOTES);
- // do we send as admin, or post author?
- if ( 'author' == $this->subscribe2_options['sender'] ) {
- // get author details
- $user = &$author;
- $this->myemail = $user->user_email;
- $this->myname = html_entity_decode($user->display_name, ENT_QUOTES);
- } elseif ( 'blogname' == $this->subscribe2_options['sender'] ) {
- $this->myemail = get_option('admin_email');
- $this->myname = html_entity_decode(get_option('blogname'), ENT_QUOTES);
- } else {
- // get admin details
- $user = $this->get_userdata($this->subscribe2_options['sender']);
- $this->myemail = $user->user_email;
- $this->myname = html_entity_decode($user->display_name, ENT_QUOTES);
- }
- $this->post_cat_names = implode(', ', wp_get_object_terms($post->ID, $s2_taxonomies, array('fields' => 'names')));
- $this->post_tag_names = implode(', ', wp_get_post_tags($post->ID, array('fields' => 'names')));
- // Get email subject
- $subject = html_entity_decode(stripslashes(wp_kses($this->substitute($this->subscribe2_options['notification_subject']), '')));
- // Get the message template
- $mailtext = apply_filters('s2_email_template', $this->subscribe2_options['mailtext']);
- $mailtext = stripslashes($this->substitute($mailtext));
- $plaintext = $post->post_content;
- $plaintext = strip_shortcodes($plaintext);
- $plaintext = preg_replace('|<s[^>]*>(.*)<\/s>|Ui','', $plaintext);
- $plaintext = preg_replace('|<strike[^>]*>(.*)<\/strike>|Ui','', $plaintext);
- $plaintext = preg_replace('|<del[^>]*>(.*)<\/del>|Ui','', $plaintext);
- $excerpttext = $plaintext;
- if ( strstr($mailtext, "{REFERENCELINKS}") ) {
- $mailtext = str_replace("{REFERENCELINKS}", '', $mailtext);
- $plaintext_links = '';
- $i = 0;
- while ( preg_match('|<a([^>]*)>(.*)<\/a>|Ui', $plaintext, $matches) ) {
- if ( preg_match('|href="([^"]*)"|', $matches[1], $link_matches) ){
- $plaintext_links .= sprintf( "[%d] %s\r\n", ++$i, $link_matches[1] );
- $link_replacement = sprintf( "%s [%d]", $matches[2], $i );
- } else {
- $link_replacement = $matches[2];
- }
- $plaintext = preg_replace('|<a[^>]*>(.*)<\/a>|Ui', $link_replacement, $plaintext, 1);
- }
- }
- $plaintext = trim(strip_tags($plaintext));
- if ( strstr($mailtext, "{REFERENCELINKS}") && $plaintext_links != '' ) {
- $plaintext .= "\r\n\r\n" . trim($plaintext_links);
- }
- $gallid = '[gallery id="' . $post->ID . '"';
- $content = str_replace('[gallery', $gallid, $post->post_content);
- // remove the autoembed filter to remove iframes from notification emails
- if ( get_option('embed_autourls') ) {
- global $wp_embed;
- $priority = has_filter('the_content', array(&$wp_embed, 'autoembed'));
- if ( $priority !== false ) {
- remove_filter('the_content', array(&$wp_embed, 'autoembed'), $priority);
- }
- }
- $content = apply_filters('the_content', $content);
- $content = str_replace("]]>", "]]>", $content);
- $excerpt = trim($post->post_excerpt);
- if ( '' == $excerpt ) {
- // no excerpt, is there a <!--more--> ?
- if ( false !== strpos($excerpttext, '<!--more-->') ) {
- list($excerpt, $more) = explode('<!--more-->', $excerpttext, 2);
- // strip tags and trailing whitespace
- $excerpt = trim(strip_tags($excerpt));
- } else {
- // no <!--more-->, so grab the first 55 words
- $excerpt = trim(strip_tags($excerpttext));
- $words = explode(' ', $excerpt, $this->excerpt_length + 1);
- if (count($words) > $this->excerpt_length) {
- array_pop($words);
- array_push($words, '[...]');
- $excerpt = implode(' ', $words);
- }
- }
- }
- $html_excerpt = trim($post->post_excerpt);
- if ( '' == $html_excerpt ) {
- // no excerpt, is there a <!--more--> ?
- if ( false !== strpos($content, '<!--more-->') ) {
- list($html_excerpt, $more) = explode('<!--more-->', $content, 2);
- // balance HTML tags and then strip leading and trailing whitespace
- $html_excerpt = trim(balanceTags($html_excerpt, true));
- } else {
- // no <!--more-->, so grab the first 55 words
- $words = explode(' ', $content, $this->excerpt_length + 1);
- if (count($words) > $this->excerpt_length) {
- array_pop($words);
- array_push($words, '[...]');
- $html_excerpt = implode(' ', $words);
- // balance HTML tags and then strip leading and trailing whitespace
- $html_excerpt = trim(balanceTags($html_excerpt, true));
- } else {
- $html_excerpt = $content;
- }
- }
- }
- // maybe add social media sharing buttons
- $social = apply_filters('s2_social_links', array('facebook', 'twitter'));
- if ( !empty($social) ) {
- $social_buttons = $this->social_buttons($social);
- $content .= $social_buttons;
- $html_excerpt .= $social_buttons;
- }
- // remove excess white space from with $excerpt and $plaintext
- $excerpt = preg_replace('|[ ]+|', ' ', $excerpt);
- $plaintext = preg_replace('|[ ]+|', ' ', $plaintext);
- // prepare mail body texts
- $plain_excerpt_body = str_replace("{POST}", $excerpt, $mailtext);
- $plain_body = str_replace("{POST}", $plaintext, $mailtext);
- $html_body = str_replace("\r\n", "<br />\r\n", $mailtext);
- $html_body = str_replace("{POST}", $content, $html_body);
- $html_excerpt_body = str_replace("\r\n", "<br />\r\n", $mailtext);
- $html_excerpt_body = str_replace("{POST}", $html_excerpt, $html_excerpt_body);
- if ( $preview != '' ) {
- $this->myemail = $preview;
- $this->myname = __('Plain Text Excerpt Preview', 'subscribe2');
- $this->mail(array($preview), $subject, $plain_excerpt_body);
- $this->myname = __('Plain Text Full Preview', 'subscribe2');
- $this->mail(array($preview), $subject, $plain_body);
- $this->myname = __('HTML Excerpt Preview', 'subscribe2');
- $this->mail(array($preview), $subject, $html_excerpt_body, 'html');
- $this->myname = __('HTML Full Preview', 'subscribe2');
- $this->mail(array($preview), $subject, $html_body, 'html');
- } else {
- // Registered Subscribers first
- // first we send plaintext summary emails
- $recipients = $this->get_registered("cats=$post_cats_string&format=excerpt&author=$post->post_author");
- $recipients = apply_filters('s2_send_plain_excerpt_subscribers', $recipients, $post->ID);
- $this->mail($recipients, $subject, $plain_excerpt_body);
- // next we send plaintext full content emails
- $recipients = $this->get_registered("cats=$post_cats_string&format=post&author=$post->post_author");
- $recipients = apply_filters('s2_send_plain_fullcontent_subscribers', $recipients, $post->ID);
- $this->mail($recipients, $subject, $plain_body);
- // next we send html excerpt content emails
- $recipients = $this->get_registered("cats=$post_cats_string&format=html_excerpt&author=$post->post_author");
- $recipients = apply_filters('s2_send_html_excerpt_subscribers', $recipients, $post->ID);
- $this->mail($recipients, $subject, $html_excerpt_body, 'html');
- // next we send html full content emails
- $recipients = $this->get_registered("cats=$post_cats_string&format=html&author=$post->post_author");
- $recipients = apply_filters('s2_send_html_fullcontent_subscribers', $recipients, $post->ID);
- $this->mail($recipients, $subject, $html_body, 'html');
- // and finally we send to Public Subscribers
- $recipients = apply_filters('s2_send_public_subscribers', $public, $post->ID);
- $this->mail($recipients, $subject, $plain_excerpt_body, 'text');
- }
- } // end publish()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement