Advertisement
Guest User

Untitled

a guest
Jun 1st, 2015
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.26 KB | None | 0 0
  1.     /**
  2.     Sends an email notification of a new post
  3.     */
  4.     function publish($post, $preview = '') {
  5.         if ( !$post ) { return $post; }
  6.  
  7.         if ( $this->s2_mu && !apply_filters('s2_allow_site_switching', $this->site_switching) ) {
  8.             global $switched;
  9.             if ( $switched ) { return; }
  10.         }
  11.  
  12.         if ( $preview == '' ) {
  13.             // we aren't sending a Preview to the current user so carry out checks
  14.             $s2mail = get_post_meta($post->ID, '_s2mail', true);
  15.             if ( (isset($_POST['s2_meta_field']) && $_POST['s2_meta_field'] == 'no') || strtolower(trim($s2mail)) == 'no' ) { return $post; }
  16.  
  17.             // are we doing daily digests? If so, don't send anything now
  18.             if ( $this->subscribe2_options['email_freq'] != 'never' ) { return $post; }
  19.  
  20.             // is the current post of a type that should generate a notification email?
  21.             // uses s2_post_types filter to allow for custom post types in WP 3.0
  22.             if ( $this->subscribe2_options['pages'] == 'yes' ) {
  23.                 $s2_post_types = array('page', 'post');
  24.             } else {
  25.                 $s2_post_types = array('post');
  26.             }
  27.             $s2_post_types = apply_filters('s2_post_types', $s2_post_types);
  28.             if ( !in_array($post->post_type, $s2_post_types) ) {
  29.                 return $post;
  30.             }
  31.  
  32.             // Are we sending notifications for password protected posts?
  33.             if ( $this->subscribe2_options['password'] == "no" && $post->post_password != '' ) {
  34.                     return $post;
  35.             }
  36.  
  37.             // Is the post assigned to a format for which we should not be sending posts
  38.             $post_format = get_post_format($post->ID);
  39.             $excluded_formats = explode(',', $this->subscribe2_options['exclude_formats']);
  40.             if ( $post_format !== false && in_array($post_format, $excluded_formats) ) {
  41.                 return $post;
  42.             }
  43.  
  44.             $s2_taxonomies = apply_filters('s2_taxonomies', array('category'));
  45.             $post_cats = wp_get_object_terms($post->ID, $s2_taxonomies, array('fields' => 'ids'));
  46.             $check = false;
  47.             // is the current post assigned to any categories
  48.             // which should not generate a notification email?
  49.             foreach ( explode(',', $this->subscribe2_options['exclude']) as $cat ) {
  50.                 if ( in_array($cat, $post_cats) ) {
  51.                     $check = true;
  52.                 }
  53.             }
  54.  
  55.             if ( $check ) {
  56.                 // hang on -- can registered users subscribe to
  57.                 // excluded categories?
  58.                 if ( '0' == $this->subscribe2_options['reg_override'] ) {
  59.                     // nope? okay, let's leave
  60.                     return $post;
  61.                 }
  62.             }
  63.  
  64.             // Are we sending notifications for Private posts?
  65.             // Action is added if we are, but double check option and post status
  66.             if ( $this->subscribe2_options['private'] == "yes" && $post->post_status == 'private' ) {
  67.                 // don't send notification to public users
  68.                 $check = true;
  69.             }
  70.  
  71.             // lets collect our subscribers
  72.             $public = array();
  73.             if ( !$check ) {
  74.                 // if this post is assigned to an excluded
  75.                 // category, or is a private post then
  76.                 // don't send public subscribers a notification
  77.                 $public = $this->get_public();
  78.             }
  79.             if ( $post->post_type == 'page' ) {
  80.                 $post_cats_string = implode(',', get_all_category_ids());
  81.             } else {
  82.                 $post_cats_string = implode(',', $post_cats);
  83.             }
  84.             $registered = $this->get_registered("cats=$post_cats_string");
  85.  
  86.             // do we have subscribers?
  87.             if ( empty($public) && empty($registered) ) {
  88.                 // if not, no sense doing anything else
  89.                 return $post;
  90.             }
  91.         } else {
  92.             // make sure we prime the taxonomy variable for preview posts
  93.             $s2_taxonomies = apply_filters('s2_taxonomies', array('category'));
  94.         }
  95.  
  96.         // we set these class variables so that we can avoid
  97.         // passing them in function calls a little later
  98.         $this->post_title = "<a href=\"" . get_permalink($post->ID) . "\">" . html_entity_decode($post->post_title, ENT_QUOTES) . "</a>";
  99.         $this->permalink = get_permalink($post->ID);
  100.         $this->post_date = get_the_time(get_option('date_format'), $post);
  101.         $this->post_time = get_the_time('', $post);
  102.  
  103.         $author = get_userdata($post->post_author);
  104.         $this->authorname = html_entity_decode(apply_filters('the_author', $author->display_name), ENT_QUOTES);
  105.  
  106.         // do we send as admin, or post author?
  107.         if ( 'author' == $this->subscribe2_options['sender'] ) {
  108.             // get author details
  109.             $user = &$author;
  110.             $this->myemail = $user->user_email;
  111.             $this->myname = html_entity_decode($user->display_name, ENT_QUOTES);
  112.         } elseif ( 'blogname' == $this->subscribe2_options['sender'] ) {
  113.             $this->myemail = get_option('admin_email');
  114.             $this->myname = html_entity_decode(get_option('blogname'), ENT_QUOTES);
  115.         } else {
  116.             // get admin details
  117.             $user = $this->get_userdata($this->subscribe2_options['sender']);
  118.             $this->myemail = $user->user_email;
  119.             $this->myname = html_entity_decode($user->display_name, ENT_QUOTES);
  120.         }
  121.  
  122.         $this->post_cat_names = implode(', ', wp_get_object_terms($post->ID, $s2_taxonomies, array('fields' => 'names')));
  123.         $this->post_tag_names = implode(', ', wp_get_post_tags($post->ID, array('fields' => 'names')));
  124.  
  125.         // Get email subject
  126.         $subject = html_entity_decode(stripslashes(wp_kses($this->substitute($this->subscribe2_options['notification_subject']), '')));
  127.         // Get the message template
  128.         $mailtext = apply_filters('s2_email_template', $this->subscribe2_options['mailtext']);
  129.         $mailtext = stripslashes($this->substitute($mailtext));
  130.  
  131.         $plaintext = $post->post_content;
  132.         $plaintext = strip_shortcodes($plaintext);
  133.  
  134.         $plaintext = preg_replace('|<s[^>]*>(.*)<\/s>|Ui','', $plaintext);
  135.         $plaintext = preg_replace('|<strike[^>]*>(.*)<\/strike>|Ui','', $plaintext);
  136.         $plaintext = preg_replace('|<del[^>]*>(.*)<\/del>|Ui','', $plaintext);
  137.         $excerpttext = $plaintext;
  138.  
  139.         if ( strstr($mailtext, "{REFERENCELINKS}") ) {
  140.             $mailtext = str_replace("{REFERENCELINKS}", '', $mailtext);
  141.             $plaintext_links = '';
  142.             $i = 0;
  143.             while ( preg_match('|<a([^>]*)>(.*)<\/a>|Ui', $plaintext, $matches) ) {
  144.                 if ( preg_match('|href="([^"]*)"|', $matches[1], $link_matches) ){
  145.                     $plaintext_links .= sprintf( "[%d] %s\r\n", ++$i, $link_matches[1] );
  146.                     $link_replacement = sprintf( "%s [%d]", $matches[2], $i );
  147.                 } else {
  148.                     $link_replacement = $matches[2];
  149.                 }
  150.                 $plaintext = preg_replace('|<a[^>]*>(.*)<\/a>|Ui', $link_replacement, $plaintext, 1);
  151.             }
  152.         }
  153.  
  154.         $plaintext = trim(strip_tags($plaintext));
  155.  
  156.         if ( strstr($mailtext, "{REFERENCELINKS}") && $plaintext_links != '' ) {
  157.             $plaintext .= "\r\n\r\n" . trim($plaintext_links);
  158.         }
  159.  
  160.         $gallid = '[gallery id="' . $post->ID . '"';
  161.         $content = str_replace('[gallery', $gallid, $post->post_content);
  162.  
  163.         // remove the autoembed filter to remove iframes from notification emails
  164.         if ( get_option('embed_autourls') ) {
  165.             global $wp_embed;
  166.             $priority = has_filter('the_content', array(&$wp_embed, 'autoembed'));
  167.             if ( $priority !== false ) {
  168.                 remove_filter('the_content', array(&$wp_embed, 'autoembed'), $priority);
  169.             }
  170.         }
  171.  
  172.         $content = apply_filters('the_content', $content);
  173.         $content = str_replace("]]>", "]]&gt", $content);
  174.  
  175.         $excerpt = trim($post->post_excerpt);
  176.         if ( '' == $excerpt ) {
  177.             // no excerpt, is there a <!--more--> ?
  178.             if ( false !== strpos($excerpttext, '<!--more-->') ) {
  179.                 list($excerpt, $more) = explode('<!--more-->', $excerpttext, 2);
  180.                 // strip tags and trailing whitespace
  181.                 $excerpt = trim(strip_tags($excerpt));
  182.             } else {
  183.                 // no <!--more-->, so grab the first 55 words
  184.                 $excerpt = trim(strip_tags($excerpttext));
  185.                 $words = explode(' ', $excerpt, $this->excerpt_length + 1);
  186.                 if (count($words) > $this->excerpt_length) {
  187.                     array_pop($words);
  188.                     array_push($words, '[...]');
  189.                     $excerpt = implode(' ', $words);
  190.                 }
  191.             }
  192.         }
  193.         $html_excerpt = trim($post->post_excerpt);
  194.         if ( '' == $html_excerpt ) {
  195.             // no excerpt, is there a <!--more--> ?
  196.             if ( false !== strpos($content, '<!--more-->') ) {
  197.                 list($html_excerpt, $more) = explode('<!--more-->', $content, 2);
  198.                 // balance HTML tags and then strip leading and trailing whitespace
  199.                 $html_excerpt = trim(balanceTags($html_excerpt, true));
  200.             } else {
  201.                 // no <!--more-->, so grab the first 55 words
  202.                 $words = explode(' ', $content, $this->excerpt_length + 1);
  203.                 if (count($words) > $this->excerpt_length) {
  204.                     array_pop($words);
  205.                     array_push($words, '[...]');
  206.                     $html_excerpt = implode(' ', $words);
  207.                     // balance HTML tags and then strip leading and trailing whitespace
  208.                     $html_excerpt = trim(balanceTags($html_excerpt, true));
  209.                 } else {
  210.                     $html_excerpt = $content;
  211.                 }
  212.             }
  213.         }
  214.  
  215.         // maybe add social media sharing buttons
  216.         $social = apply_filters('s2_social_links', array('facebook', 'twitter'));
  217.         if ( !empty($social) ) {
  218.             $social_buttons = $this->social_buttons($social);
  219.             $content .= $social_buttons;
  220.             $html_excerpt .= $social_buttons;
  221.         }
  222.  
  223.         // remove excess white space from with $excerpt and $plaintext
  224.         $excerpt = preg_replace('|[ ]+|', ' ', $excerpt);
  225.         $plaintext = preg_replace('|[ ]+|', ' ', $plaintext);
  226.  
  227.         // prepare mail body texts
  228.         $plain_excerpt_body = str_replace("{POST}", $excerpt, $mailtext);
  229.         $plain_body = str_replace("{POST}", $plaintext, $mailtext);
  230.         $html_body = str_replace("\r\n", "<br />\r\n", $mailtext);
  231.         $html_body = str_replace("{POST}", $content, $html_body);
  232.         $html_excerpt_body = str_replace("\r\n", "<br />\r\n", $mailtext);
  233.         $html_excerpt_body = str_replace("{POST}", $html_excerpt, $html_excerpt_body);
  234.  
  235.         if ( $preview != '' ) {
  236.             $this->myemail = $preview;
  237.             $this->myname = __('Plain Text Excerpt Preview', 'subscribe2');
  238.             $this->mail(array($preview), $subject, $plain_excerpt_body);
  239.             $this->myname = __('Plain Text Full Preview', 'subscribe2');
  240.             $this->mail(array($preview), $subject, $plain_body);
  241.             $this->myname = __('HTML Excerpt Preview', 'subscribe2');
  242.             $this->mail(array($preview), $subject, $html_excerpt_body, 'html');
  243.             $this->myname = __('HTML Full Preview', 'subscribe2');
  244.             $this->mail(array($preview), $subject, $html_body, 'html');
  245.         } else {
  246.             // Registered Subscribers first
  247.             // first we send plaintext summary emails
  248.             $recipients = $this->get_registered("cats=$post_cats_string&format=excerpt&author=$post->post_author");
  249.             $recipients = apply_filters('s2_send_plain_excerpt_subscribers', $recipients, $post->ID);
  250.             $this->mail($recipients, $subject, $plain_excerpt_body);
  251.  
  252.             // next we send plaintext full content emails
  253.             $recipients = $this->get_registered("cats=$post_cats_string&format=post&author=$post->post_author");
  254.             $recipients = apply_filters('s2_send_plain_fullcontent_subscribers', $recipients, $post->ID);
  255.             $this->mail($recipients, $subject, $plain_body);
  256.  
  257.             // next we send html excerpt content emails
  258.             $recipients = $this->get_registered("cats=$post_cats_string&format=html_excerpt&author=$post->post_author");
  259.             $recipients = apply_filters('s2_send_html_excerpt_subscribers', $recipients, $post->ID);
  260.             $this->mail($recipients, $subject, $html_excerpt_body, 'html');
  261.  
  262.             // next we send html full content emails
  263.             $recipients = $this->get_registered("cats=$post_cats_string&format=html&author=$post->post_author");
  264.             $recipients = apply_filters('s2_send_html_fullcontent_subscribers', $recipients, $post->ID);
  265.             $this->mail($recipients, $subject, $html_body, 'html');
  266.  
  267.             // and finally we send to Public Subscribers
  268.             $recipients = apply_filters('s2_send_public_subscribers', $public, $post->ID);
  269.             $this->mail($recipients, $subject, $plain_excerpt_body, 'text');
  270.         }
  271.     } // end publish()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement