/classes/class-s2-core.php

  1. <?php 
  2. class s2class { 
  3. // variables and constructor are declared at the end 
  4. /** 
  5. Load translations 
  6. */ 
  7. function load_translations() { 
  8. load_plugin_textdomain('subscribe2', false, S2DIR); 
  9. load_plugin_textdomain('subscribe2', false, S2DIR . "languages/"); 
  10. $mofile = WP_LANG_DIR . '/subscribe2-' . apply_filters('plugin_locale', get_locale(), 'subscribe2') . '.mo'; 
  11. load_textdomain('subscribe2', $mofile); 
  12. } // end load_translations() 
  13.  
  14. /** 
  15. Load all our strings 
  16. */ 
  17. function load_strings() { 
  18. // adjust the output of Subscribe2 here 
  19.  
  20. $this->please_log_in = "<p class=\"s2_message\">" . sprintf(__('To manage your subscription options please <a href="%1$s">login.</a>', 'subscribe2'), get_option('siteurl') . '/wp-login.php') . "</p>"; 
  21.  
  22. $this->profile = "<p class=\"s2_message\">" . sprintf(__('You may manage your subscription options from your <a href="%1$s">profile</a>', 'subscribe2'), get_option('siteurl') . "/wp-admin/admin.php?page=s2") . "</p>"; 
  23. if ( $this->s2_mu === true ) { 
  24. global $blog_id; 
  25. $user_ID = get_current_user_id(); 
  26. if ( !is_user_member_of_blog($user_ID, $blog_id) ) { 
  27. // if we are on multisite and the user is not a member of this blog change the link 
  28. $this->profile = "<p class=\"s2_message\">" . sprintf(__('<a href="%1$s">Subscribe</a> to email notifications when this blog posts new content.', 'subscribe2'), get_option('siteurl') . "/wp-admin/?s2mu_subscribe=" . $blog_id) . "</p>"; 
  29.  
  30. $this->confirmation_sent = "<p class=\"s2_message\">" . __('A confirmation message is on its way!', 'subscribe2') . "</p>"; 
  31.  
  32. $this->already_subscribed = "<p class=\"s2_error\">" . __('That email address is already subscribed.', 'subscribe2') . "</p>"; 
  33.  
  34. $this->not_subscribed = "<p class=\"s2_error\">" . __('That email address is not subscribed.', 'subscribe2') . "</p>"; 
  35.  
  36. $this->not_an_email = "<p class=\"s2_error\">" . __('Sorry, but that does not look like an email address to me.', 'subscribe2') . "</p>"; 
  37.  
  38. $this->barred_domain = "<p class=\"s2_error\">" . __('Sorry, email addresses at that domain are currently barred due to spam, please use an alternative email address.', 'subscribe2') . "</p>"; 
  39.  
  40. $this->error = "<p class=\"s2_error\">" . __('Sorry, there seems to be an error on the server. Please try again later.', 'subscribe2') . "</p>"; 
  41.  
  42. $this->no_page = __('You must to create a WordPress page for this plugin to work correctly.', 'subscribe2'); 
  43.  
  44. $this->disallowed_keywords = __('Your chosen email type (per-post or digest) does not support the following keywords:', 'subscribe2'); 
  45.  
  46. $this->mail_sent = "<p class=\"s2_message\">" . __('Message sent!', 'subscribe2') . "</p>"; 
  47.  
  48. $this->mail_failed = "<p class=\"s2_error\">" . __('Message failed!', 'subscribe2') . "</p>"; 
  49.  
  50. // confirmation messages 
  51. $this->no_such_email = "<p class=\"s2_error\">" . __('No such email address is registered.', 'subscribe2') . "</p>"; 
  52.  
  53. $this->added = "<p class=\"s2_message\">" . __('You have successfully subscribed!', 'subscribe2') . "</p>"; 
  54.  
  55. $this->deleted = "<p class=\"s2_message\">" . __('You have successfully unsubscribed.', 'subscribe2') . "</p>"; 
  56.  
  57. $this->subscribe = __('subscribe', 'subscribe2'); //ACTION replacement in subscribing confirmation email 
  58.  
  59. $this->unsubscribe = __('unsubscribe', 'subscribe2'); //ACTION replacement in unsubscribing in confirmation email 
  60.  
  61. // menu strings 
  62. $this->options_saved = __('Options saved!', 'subscribe2'); 
  63. $this->options_reset = __('Options reset!', 'subscribe2'); 
  64. } // end load_strings() 
  65.  
  66. /** ===== Install, upgrade, reset ===== */ 
  67. /** 
  68. Install our table 
  69. */ 
  70. function install() { 
  71. // load our translations and strings 
  72. $this->load_translations(); 
  73.  
  74. // include upgrade-functions for maybe_create_table; 
  75. if ( !function_exists('maybe_create_table') ) { 
  76. require_once(ABSPATH . 'wp-admin/install-helper.php'); 
  77. $date = date('Y-m-d'); 
  78. $sql = "CREATE TABLE $this->public ( 
  79. id int(11) NOT NULL auto_increment,  
  80. email varchar(64) NOT NULL default '',  
  81. active tinyint(1) default 0,  
  82. date DATE default '$date' NOT NULL,  
  83. time TIME DEFAULT '00:00:00' NOT NULL,  
  84. ip char(64) NOT NULL default 'admin',  
  85. conf_date DATE,  
  86. conf_time TIME,  
  87. conf_ip char(64),  
  88. PRIMARY KEY (id) )"; 
  89.  
  90. // create the table, as needed 
  91. maybe_create_table($this->public, $sql); 
  92.  
  93. // create table entries for registered users 
  94. $users = $this->get_all_registered('ID'); 
  95. if ( !empty($users) ) { 
  96. foreach ( $users as $user_ID ) { 
  97. $check_format = get_user_meta($user_ID, $this->get_usermeta_keyname('s2_format'), true); 
  98. if ( empty($check_format) ) { 
  99. // no prior settings so create them 
  100. $this->register($user_ID); 
  101.  
  102. // safety check if options exist and if not create them 
  103. if ( !is_array($this->subscribe2_options) ) { 
  104. $this->reset(); 
  105. } // end install() 
  106.  
  107. /** 
  108. Upgrade function for the database and settings 
  109. */ 
  110. function upgrade() { 
  111. // load our translations and strings 
  112. $this->load_translations(); 
  113.  
  114. require(S2PATH . "classes/class-s2-upgrade.php"); 
  115. global $s2_upgrade; 
  116. $s2_upgrade = new s2class_upgrade; 
  117.  
  118. // ensure that the options are in the database 
  119. require(S2PATH . "include/options.php"); 
  120. // catch older versions that didn't use serialised options 
  121. if ( !isset($this->subscribe2_options['version']) ) { 
  122. $this->subscribe2_options['version'] = '2.0'; 
  123.  
  124. // let's take the time to ensure that database entries exist for all registered users 
  125. $s2_upgrade->upgrade_core(); 
  126. if ( version_compare($this->subscribe2_options['version'], '2.3', '<') ) { 
  127. $s2_upgrade->upgrade23(); 
  128. $this->subscribe2_options['version'] = '2.3'; 
  129. update_option('subscribe2_options', $this->subscribe2_options); 
  130. if ( version_compare($this->subscribe2_options['version'], '5.1', '<') ) { 
  131. $s2_upgrade->upgrade51(); 
  132. $this->subscribe2_options['version'] = '5.1'; 
  133. update_option('subscribe2_options', $this->subscribe2_options); 
  134. if ( version_compare($this->subscribe2_options['version'], '5.6', '<') ) { 
  135. $s2_upgrade->upgrade56(); 
  136. $this->subscribe2_options['version'] = '5.6'; 
  137. update_option('subscribe2_options', $this->subscribe2_options); 
  138. if ( version_compare($this->subscribe2_options['version'], '5.9', '<') ) { 
  139. $s2_upgrade->upgrade59(); 
  140. $this->subscribe2_options['version'] = '5.9'; 
  141. update_option('subscribe2_options', $this->subscribe2_options); 
  142. if ( version_compare($this->subscribe2_options['version'], '6.4', '<') ) { 
  143. $s2_upgrade->upgrade64(); 
  144. $this->subscribe2_options['version'] = '6.4'; 
  145. update_option('subscribe2_options', $this->subscribe2_options); 
  146. if ( version_compare($this->subscribe2_options['version'], '7.0', '<') ) { 
  147. $s2_upgrade->upgrade70(); 
  148. $this->subscribe2_options['version'] = '7.0'; 
  149. update_option('subscribe2_options', $this->subscribe2_options); 
  150. if ( version_compare($this->subscribe2_options['version'], '8.5', '<') ) { 
  151. $s2_upgrade->upgrade85(); 
  152. $this->subscribe2_options['version'] = '8.5'; 
  153. update_option('subscribe2_options', $this->subscribe2_options); 
  154. if ( version_compare($this->subscribe2_options['version'], '8.6', '<') ) { 
  155. $s2_upgrade->upgrade86(); 
  156. $this->subscribe2_options['version'] = '8.6'; 
  157. update_option('subscribe2_options', $this->subscribe2_options); 
  158. if ( version_compare($this->subscribe2_options['version'], '8.8', '<') ) { 
  159. $s2_upgrade->upgrade88(); 
  160. $this->subscribe2_options['version'] = '8.8'; 
  161. update_option('subscribe2_options', $this->subscribe2_options); 
  162.  
  163. $this->subscribe2_options['version'] = S2VERSION; 
  164. update_option('subscribe2_options', $this->subscribe2_options); 
  165.  
  166. return; 
  167. } // end upgrade() 
  168.  
  169. /** 
  170. Reset our options 
  171. */ 
  172. function reset() { 
  173. // load our translations and strings 
  174. $this->load_translations(); 
  175.  
  176. delete_option('subscribe2_options'); 
  177. wp_clear_scheduled_hook('s2_digest_cron'); 
  178. unset($this->subscribe2_options); 
  179. require(S2PATH . "include/options.php"); 
  180. $this->subscribe2_options['version'] = S2VERSION; 
  181. update_option('subscribe2_options', $this->subscribe2_options); 
  182. } // end reset() 
  183.  
  184. /** ===== mail handling ===== */ 
  185. /** 
  186. Performs string substitutions for subscribe2 mail tags 
  187. */ 
  188. function substitute($string = '') { 
  189. if ( '' == $string ) { 
  190. return; 
  191. $string = str_replace("{BLOGNAME}", html_entity_decode(get_option('blogname'), ENT_QUOTES), $string); 
  192. $string = str_replace("{BLOGLINK}", get_option('home'), $string); 
  193. $string = str_replace("{TITLE}", stripslashes($this->post_title), $string); 
  194. $link = "<a href=\"" . $this->get_tracking_link($this->permalink) . "\">" . $this->get_tracking_link($this->permalink) . "</a>"; 
  195. $string = str_replace("{PERMALINK}", $link, $string); 
  196. if ( strstr($string, "{TINYLINK}") ) { 
  197. $tinylink = file_get_contents('http://tinyurl.com/api-create.php?url=' . urlencode($this->get_tracking_link($this->permalink))); 
  198. if ( $tinylink !== 'Error' && $tinylink != false ) { 
  199. $tlink = "<a href=\"" . $tinylink . "\">" . $tinylink . "</a>"; 
  200. $string = str_replace("{TINYLINK}", $tlink, $string); 
  201. } else { 
  202. $string = str_replace("{TINYLINK}", $link, $string); 
  203. $string = str_replace("{DATE}", $this->post_date, $string); 
  204. $string = str_replace("{TIME}", $this->post_time, $string); 
  205. $string = str_replace("{MYNAME}", stripslashes($this->myname), $string); 
  206. $string = str_replace("{EMAIL}", $this->myemail, $string); 
  207. $string = str_replace("{AUTHORNAME}", stripslashes($this->authorname), $string); 
  208. $string = str_replace("{CATS}", $this->post_cat_names, $string); 
  209. $string = str_replace("{TAGS}", $this->post_tag_names, $string); 
  210. $string = str_replace("{COUNT}", $this->post_count, $string); 
  211.  
  212. return $string; 
  213. } // end substitute() 
  214.  
  215. /** 
  216. Delivers email to recipients in HTML or plaintext 
  217. */ 
  218. function mail($recipients = array(), $subject = '', $message = '', $type = 'text', $attachments = array()) { 
  219. if ( empty($recipients) || '' == $message ) { return; } 
  220.  
  221. // Replace any escaped html symbols in subject then apply filter 
  222. $subject = strip_tags(html_entity_decode($subject, ENT_QUOTES)); 
  223. $subject = apply_filters('s2_email_subject', $subject); 
  224.  
  225. if ( 'html' == $type ) { 
  226. $headers = $this->headers('html', $attachments); 
  227. if ( 'yes' == $this->subscribe2_options['stylesheet'] ) { 
  228. $mailtext = apply_filters('s2_html_email', "<html><head><title>" . $subject . "</title><link rel=\"stylesheet\" href=\"" . get_stylesheet_uri() . "\" type=\"text/css\" media=\"screen\" /></head><body>" . $message . "</body></html>", $subject, $message); 
  229. } else { 
  230. $mailtext = apply_filters('s2_html_email', "<html><head><title>" . $subject . "</title></head><body>" . $message . "</body></html>", $subject, $message); 
  231. } else { 
  232. $headers = $this->headers('text', $attachments); 
  233. $message = preg_replace('|&[^a][^m][^p].{0, 3};|', '', $message); 
  234. $message = preg_replace('|&|', '&', $message); 
  235. $message = wordwrap(strip_tags($message), $this->word_wrap, "\n"); 
  236. $mailtext = apply_filters('s2_plain_email', $message); 
  237.  
  238. // Construct BCC headers for sending or send individual emails 
  239. $bcc = ''; 
  240. natcasesort($recipients); 
  241. if ( function_exists('wpmq_mail') || $this->subscribe2_options['bcclimit'] == 1 || count($recipients) == 1 ) { 
  242. // BCCLimit is 1 so send individual emails or we only have 1 recipient 
  243. foreach ( $recipients as $recipient ) { 
  244. $recipient = trim($recipient); 
  245. // sanity check -- make sure we have a valid email 
  246. if ( !is_email($recipient) || empty($recipient) ) { continue; } 
  247. // Use the mail queue provided we are not sending a preview 
  248. if ( function_exists('wpmq_mail') && !$this->preview_email ) { 
  249. @wp_mail($recipient, $subject, $mailtext, $headers, $attachments, 0); 
  250. } else { 
  251. @wp_mail($recipient, $subject, $mailtext, $headers, $attachments); 
  252. return true; 
  253. } elseif ( $this->subscribe2_options['bcclimit'] == 0 ) { 
  254. // we're not using BCCLimit 
  255. foreach ( $recipients as $recipient ) { 
  256. $recipient = trim($recipient); 
  257. // sanity check -- make sure we have a valid email 
  258. if ( !is_email($recipient) ) { continue; } 
  259. // and NOT the sender's email, since they'll get a copy anyway 
  260. if ( !empty($recipient) && $this->myemail != $recipient ) { 
  261. ('' == $bcc) ? $bcc = "Bcc: $recipient" : $bcc .= ", $recipient"; 
  262. // Bcc Headers now constructed by phpmailer class 
  263. $headers .= "$bcc\n"; 
  264. } else { 
  265. // we're using BCCLimit 
  266. $count = 1; 
  267. $batch = array(); 
  268. foreach ( $recipients as $recipient ) { 
  269. $recipient = trim($recipient); 
  270. // sanity check -- make sure we have a valid email 
  271. if ( !is_email($recipient) ) { continue; } 
  272. // and NOT the sender's email, since they'll get a copy anyway 
  273. if ( !empty($recipient) && $this->myemail != $recipient ) { 
  274. ('' == $bcc) ? $bcc = "Bcc: $recipient" : $bcc .= ", $recipient"; 
  275. // Bcc Headers now constructed by phpmailer class 
  276. if ( $this->subscribe2_options['bcclimit'] == $count ) { 
  277. $count = 0; 
  278. $batch[] = $bcc; 
  279. $bcc = ''; 
  280. $count++; 
  281. // add any partially completed batches to our batch array 
  282. if ( '' != $bcc ) { 
  283. $batch[] = $bcc; 
  284. // rewind the array, just to be safe 
  285. reset($recipients); 
  286.  
  287. // actually send mail 
  288. if ( isset($batch) && !empty($batch) ) { 
  289. foreach ( $batch as $bcc ) { 
  290. $newheaders = $headers . "$bcc\n"; 
  291. $status = @wp_mail($this->myemail, $subject, $mailtext, $newheaders, $attachments); 
  292. } else { 
  293. $status = @wp_mail($this->myemail, $subject, $mailtext, $headers, $attachments); 
  294. return $status; 
  295. } // end mail() 
  296.  
  297. /** 
  298. Construct standard set of email headers 
  299. */ 
  300. function headers($type = 'text', $attachments = array()) { 
  301. if ( empty($this->myname) || empty($this->myemail) ) { 
  302. if ( $this->subscribe2_options['sender'] == 'blogname' ) { 
  303. $this->myname = html_entity_decode(get_option('blogname'), ENT_QUOTES); 
  304. $this->myemail = get_option('admin_email'); 
  305. } else { 
  306. $admin = $this->get_userdata($this->subscribe2_options['sender']); 
  307. $this->myname = html_entity_decode($admin->display_name, ENT_QUOTES); 
  308. $this->myemail = $admin->user_email; 
  309. // fail safe to ensure sender details are not empty 
  310. if ( empty($this->myname) ) { 
  311. $this->myname = html_entity_decode(get_option('blogname'), ENT_QUOTES); 
  312. if ( empty($this->myemail) ) { 
  313. // Get the site domain and get rid of www. 
  314. $sitename = strtolower( $_SERVER['SERVER_NAME'] ); 
  315. if ( substr( $sitename, 0, 4 ) == 'www.' ) { 
  316. $sitename = substr( $sitename, 4 ); 
  317. $this->myemail = 'wordpress@' . $sitename; 
  318.  
  319. if ( function_exists('mb_encode_mimeheader') ) { 
  320. $header['From'] = mb_encode_mimeheader($this->myname, 'UTF-8', 'Q') . " <" . $this->myemail . ">"; 
  321. $header['Reply-To'] = mb_encode_mimeheader($this->myname, 'UTF-8', 'Q') . " <" . $this->myemail . ">"; 
  322. } else { 
  323. $header['From'] = $this->myname. " <" . $this->myemail . ">"; 
  324. $header['Reply-To'] = $this->myname . " <" . $this->myemail . ">"; 
  325. $header['Return-path'] = "<" . $this->myemail . ">"; 
  326. $header['Precedence'] = "list\nList-Id: " . html_entity_decode(get_option('blogname'), ENT_QUOTES) . ""; 
  327. if ( empty($attachments) && $type == 'html' ) { 
  328. // To send HTML mail, the Content-Type header must be set 
  329. $header['Content-Type'] = get_option('html_type') . "; charset=\"". get_option('blog_charset') . "\""; 
  330. } elseif ( empty($attachments) && $type == 'text' ) { 
  331. $header['Content-Type'] = "text/plain; charset=\"". get_option('blog_charset') . "\""; 
  332.  
  333. // apply header filter to allow on-the-fly amendments 
  334. $header = apply_filters('s2_email_headers', $header); 
  335. // collapse the headers using $key as the header name 
  336. foreach ( $header as $key => $value ) { 
  337. $headers[$key] = $key . ": " . $value; 
  338. $headers = implode("\n", $headers); 
  339. $headers .= "\n"; 
  340.  
  341. return $headers; 
  342. } // end headers() 
  343.  
  344. /** 
  345. Function to add UTM tracking details to links 
  346. */ 
  347. function get_tracking_link($link) { 
  348. if ( empty($link) ) { return; } 
  349. if ( !empty($this->subscribe2_options['tracking']) ) { 
  350. (strpos($link, '?') > 0) ? $delimiter .= '&' : $delimiter = '?'; 
  351. $tracking = $this->subscribe2_options['tracking']; 
  352. if ( strpos($tracking, "{ID}") ) { 
  353. $id = url_to_postid($link); 
  354. $tracking = str_replace("{ID}", $id, $tracking); 
  355. if ( strpos($tracking, "{TITLE}") ) { 
  356. $id = url_to_postid($link); 
  357. $title = urlencode(htmlentities(get_the_title($id), 1)); 
  358. $tracking = str_replace("{TITLE}", $title, $tracking); 
  359. return $link . $delimiter . $tracking; 
  360. } else { 
  361. return $link; 
  362. } // end get_tracking_link() 
  363.  
  364. /** 
  365. Sends an email notification of a new post 
  366. */ 
  367. function publish($post, $preview = '') { 
  368. if ( !$post ) { return $post; } 
  369.  
  370. if ( $this->s2_mu && !apply_filters('s2_allow_site_switching', $this->site_switching) ) { 
  371. global $switched; 
  372. if ( $switched ) { return; } 
  373.  
  374. if ( $preview == '' ) { 
  375. // we aren't sending a Preview to the current user so carry out checks 
  376. $s2mail = get_post_meta($post->ID, '_s2mail', true); 
  377. if ( (isset($_POST['s2_meta_field']) && $_POST['s2_meta_field'] == 'no') || strtolower(trim($s2mail)) == 'no' ) { return $post; } 
  378.  
  379. // are we doing daily digests? If so, don't send anything now 
  380. if ( $this->subscribe2_options['email_freq'] != 'never' ) { return $post; } 
  381.  
  382. // is the current post of a type that should generate a notification email? 
  383. // uses s2_post_types filter to allow for custom post types in WP 3.0 
  384. if ( $this->subscribe2_options['pages'] == 'yes' ) { 
  385. $s2_post_types = array('page', 'post'); 
  386. } else { 
  387. $s2_post_types = array('post'); 
  388. $s2_post_types = apply_filters('s2_post_types', $s2_post_types); 
  389. if ( !in_array($post->post_type, $s2_post_types) ) { 
  390. return $post; 
  391.  
  392. // Are we sending notifications for password protected posts? 
  393. if ( $this->subscribe2_options['password'] == "no" && $post->post_password != '' ) { 
  394. return $post; 
  395.  
  396. // Is the post assigned to a format for which we should not be sending posts 
  397. $post_format = get_post_format($post->ID); 
  398. $excluded_formats = explode(', ', $this->subscribe2_options['exclude_formats']); 
  399. if ( $post_format !== false && in_array($post_format, $excluded_formats) ) { 
  400. return $post; 
  401.  
  402. $s2_taxonomies = apply_filters('s2_taxonomies', array('category')); 
  403. $post_cats = wp_get_object_terms($post->ID, $s2_taxonomies, array('fields' => 'ids')); 
  404. $check = false; 
  405. // is the current post assigned to any categories 
  406. // which should not generate a notification email? 
  407. foreach ( explode(', ', $this->subscribe2_options['exclude']) as $cat ) { 
  408. if ( in_array($cat, $post_cats) ) { 
  409. $check = true; 
  410.  
  411. if ( $check ) { 
  412. // hang on -- can registered users subscribe to 
  413. // excluded categories? 
  414. if ( '0' == $this->subscribe2_options['reg_override'] ) { 
  415. // nope? okay, let's leave 
  416. return $post; 
  417.  
  418. // Are we sending notifications for Private posts? 
  419. // Action is added if we are, but double check option and post status 
  420. if ( $this->subscribe2_options['private'] == "yes" && $post->post_status == 'private' ) { 
  421. // don't send notification to public users 
  422. $check = true; 
  423.  
  424. // lets collect our subscribers 
  425. $public = array(); 
  426. if ( !$check ) { 
  427. // if this post is assigned to an excluded 
  428. // category, or is a private post then 
  429. // don't send public subscribers a notification 
  430. $public = $this->get_public(); 
  431. if ( $post->post_type == 'page' ) { 
  432. $post_cats_string = implode(', ', get_all_category_ids()); 
  433. } else { 
  434. $post_cats_string = implode(', ', $post_cats); 
  435. $registered = $this->get_registered("cats=$post_cats_string"); 
  436.  
  437. // do we have subscribers? 
  438. if ( empty($public) && empty($registered) ) { 
  439. // if not, no sense doing anything else 
  440. return $post; 
  441. } else { 
  442. // make sure we prime the taxonomy variable for preview posts 
  443. $s2_taxonomies = apply_filters('s2_taxonomies', array('category')); 
  444.  
  445. // we set these class variables so that we can avoid 
  446. // passing them in function calls a little later 
  447. $this->post_title = "<a href=\"" . get_permalink($post->ID) . "\">" . html_entity_decode($post->post_title, ENT_QUOTES) . "</a>"; 
  448. $this->permalink = get_permalink($post->ID); 
  449. $this->post_date = get_the_time(get_option('date_format'), $post); 
  450. $this->post_time = get_the_time('', $post); 
  451.  
  452. $author = get_userdata($post->post_author); 
  453. $this->authorname = html_entity_decode(apply_filters('the_author', $author->display_name), ENT_QUOTES); 
  454.  
  455. // do we send as admin, or post author? 
  456. if ( 'author' == $this->subscribe2_options['sender'] ) { 
  457. // get author details 
  458. $user = &$author; 
  459. $this->myemail = $user->user_email; 
  460. $this->myname = html_entity_decode($user->display_name, ENT_QUOTES); 
  461. } elseif ( 'blogname' == $this->subscribe2_options['sender'] ) { 
  462. $this->myemail = get_option('admin_email'); 
  463. $this->myname = html_entity_decode(get_option('blogname'), ENT_QUOTES); 
  464. } else { 
  465. // get admin details 
  466. $user = $this->get_userdata($this->subscribe2_options['sender']); 
  467. $this->myemail = $user->user_email; 
  468. $this->myname = html_entity_decode($user->display_name, ENT_QUOTES); 
  469.  
  470. $this->post_cat_names = implode(', ', wp_get_object_terms($post->ID, $s2_taxonomies, array('fields' => 'names'))); 
  471. $this->post_tag_names = implode(', ', wp_get_post_tags($post->ID, array('fields' => 'names'))); 
  472.  
  473. // Get email subject 
  474. $subject = html_entity_decode(stripslashes(wp_kses($this->substitute($this->subscribe2_options['notification_subject']), ''))); 
  475. // Get the message template 
  476. $mailtext = apply_filters('s2_email_template', $this->subscribe2_options['mailtext']); 
  477. $mailtext = stripslashes($this->substitute($mailtext)); 
  478.  
  479. $plaintext = $post->post_content; 
  480. if ( function_exists('strip_shortcodes') ) { 
  481. $plaintext = strip_shortcodes($plaintext); 
  482. $plaintext = preg_replace('|<s[^>]*>(.*)<\/s>|Ui', '', $plaintext); 
  483. $plaintext = preg_replace('|<strike[^>]*>(.*)<\/strike>|Ui', '', $plaintext); 
  484. $plaintext = preg_replace('|<del[^>]*>(.*)<\/del>|Ui', '', $plaintext); 
  485. $plaintext = trim(strip_tags($plaintext)); 
  486.  
  487. $gallid = ']>", "]]>", $content); 
  488.  
  489. $excerpt = $post->post_excerpt; 
  490. if ( '' == $excerpt ) { 
  491. // no excerpt, is there a <!--more--> ? 
  492. if ( false !== strpos($plaintext, '<!--more-->') ) { 
  493. list($excerpt, $more) = explode('<!--more-->', $plaintext, 2); 
  494. // strip leading and trailing whitespace 
  495. $excerpt = strip_tags($excerpt); 
  496. $excerpt = trim($excerpt); 
  497. } else { 
  498. // no <!--more-->, so grab the first 55 words 
  499. $excerpt = strip_tags($plaintext); 
  500. $words = explode(' ', $excerpt, $this->excerpt_length + 1); 
  501. if (count($words) > $this->excerpt_length) { 
  502. array_pop($words); 
  503. array_push($words, '[...]'); 
  504. $excerpt = implode(' ', $words); 
  505. $html_excerpt = $post->post_excerpt; 
  506. if ( '' == $html_excerpt ) { 
  507. // no excerpt, is there a <!--more--> ? 
  508. if ( false !== strpos($content, '<!--more-->') ) { 
  509. list($html_excerpt, $more) = explode('<!--more-->', $content, 2); 
  510. // balance HTML tags and then strip leading and trailing whitespace 
  511. $html_excerpt = trim(balanceTags($html_excerpt, true)); 
  512. } else { 
  513. // no <!--more-->, so grab the first 55 words 
  514. $words = explode(' ', $content, $this->excerpt_length + 1); 
  515. if (count($words) > $this->excerpt_length) { 
  516. array_pop($words); 
  517. array_push($words, '[...]'); 
  518. $html_excerpt = implode(' ', $words); 
  519. // balance HTML tags and then strip leading and trailing whitespace 
  520. $html_excerpt = trim(balanceTags($html_excerpt, true)); 
  521. } else { 
  522. $html_excerpt = $content; 
  523.  
  524. // remove excess white space from with $excerpt and $plaintext 
  525. $excerpt = preg_replace('|[ ]+|', ' ', $excerpt); 
  526. $plaintext = preg_replace('|[ ]+|', ' ', $plaintext); 
  527.  
  528. // prepare mail body texts 
  529. $plain_excerpt_body = str_replace("{POST}", $excerpt, $mailtext); 
  530. $plain_body = str_replace("{POST}", $plaintext, $mailtext); 
  531. $html_body = str_replace("\r\n", "<br />\r\n", $mailtext); 
  532. $html_body = str_replace("{POST}", $content, $html_body); 
  533. $html_excerpt_body = str_replace("\r\n", "<br />\r\n", $mailtext); 
  534. $html_excerpt_body = str_replace("{POST}", $html_excerpt, $html_excerpt_body); 
  535.  
  536. if ( $preview != '' ) { 
  537. $this->myemail = $preview; 
  538. $this->myname = __('Plain Text Excerpt Preview', 'subscribe2'); 
  539. $this->mail(array($preview), $subject, $plain_excerpt_body); 
  540. $this->myname = __('Plain Text Full Preview', 'subscribe2'); 
  541. $this->mail(array($preview), $subject, $plain_body); 
  542. $this->myname = __('HTML Excerpt Preview', 'subscribe2'); 
  543. $this->mail(array($preview), $subject, $html_excerpt_body, 'html'); 
  544. $this->myname = __('HTML Full Preview', 'subscribe2'); 
  545. $this->mail(array($preview), $subject, $html_body, 'html'); 
  546. } else { 
  547. // Registered Subscribers first 
  548. // first we send plaintext summary emails 
  549. $recipients = $this->get_registered("cats=$post_cats_string&format=excerpt&author=$post->post_author"); 
  550. $recipients = apply_filters('s2_send_plain_excerpt_suscribers', $recipients, $post->ID); 
  551. $this->mail($recipients, $subject, $plain_excerpt_body); 
  552.  
  553. // next we send plaintext full content emails 
  554. $recipients = $this->get_registered("cats=$post_cats_string&format=post&author=$post->post_author"); 
  555. $recipients = apply_filters('s2_send_plain_fullcontent_suscribers', $recipients, $post->ID); 
  556. $this->mail($recipients, $subject, $plain_body); 
  557.  
  558. // next we send html excerpt content emails 
  559. $recipients = $this->get_registered("cats=$post_cats_string&format=html_excerpt&author=$post->post_author"); 
  560. $recipients = apply_filters('s2_send_html_excerpt_suscribers', $recipients, $post->ID); 
  561. $this->mail($recipients, $subject, $html_excerpt_body, 'html'); 
  562.  
  563. // next we send html full content emails 
  564. $recipients = $this->get_registered("cats=$post_cats_string&format=html&author=$post->post_author"); 
  565. $recipients = apply_filters('s2_send_html_fullcontent_suscribers', $recipients, $post->ID); 
  566. $this->mail($recipients, $subject, $html_body, 'html'); 
  567.  
  568. // and finally we send to Public Subscribers 
  569. $recipients = apply_filters('s2_send_public_suscribers', $public, $post->ID); 
  570. $this->mail($recipients, $subject, $plain_excerpt_body, 'text'); 
  571. } // end publish() 
  572.  
  573. /** 
  574. Send confirmation email to a public subscriber 
  575. */ 
  576. function send_confirm($what = '', $is_remind = false) { 
  577. if ( $this->filtered == 1 ) { return true; } 
  578. if ( !$this->email || !$what ) { return false; } 
  579. $id = $this->get_id($this->email); 
  580. if ( !$id ) { 
  581. return false; 
  582.  
  583. // generate the URL "?s2=ACTION+HASH+ID" 
  584. // ACTION = 1 to subscribe, 0 to unsubscribe 
  585. // HASH = wp_hash of email address 
  586. // ID = user's ID in the subscribe2 table 
  587. // use home instead of siteurl incase index.php is not in core wordpress directory 
  588. $link = get_option('home') . "/?s2="; 
  589.  
  590. if ( 'add' == $what ) { 
  591. $link .= '1'; 
  592. } elseif ( 'del' == $what ) { 
  593. $link .= '0'; 
  594. $link .= wp_hash($this->email); 
  595. $link .= $id; 
  596.  
  597. // sort the headers now so we have all substitute information 
  598. $mailheaders = $this->headers(); 
  599.  
  600. if ( $is_remind == true ) { 
  601. $body = $this->substitute(stripslashes($this->subscribe2_options['remind_email'])); 
  602. $subject = $this->substitute(stripslashes($this->subscribe2_options['remind_subject'])); 
  603. } else { 
  604. $body = $this->substitute(stripslashes($this->subscribe2_options['confirm_email'])); 
  605. if ( 'add' == $what ) { 
  606. $body = str_replace("{ACTION}", $this->subscribe, $body); 
  607. $subject = str_replace("{ACTION}", $this->subscribe, $this->subscribe2_options['confirm_subject']); 
  608. } elseif ( 'del' == $what ) { 
  609. $body = str_replace("{ACTION}", $this->unsubscribe, $body); 
  610. $subject = str_replace("{ACTION}", $this->unsubscribe, $this->subscribe2_options['confirm_subject']); 
  611. $subject = html_entity_decode($this->substitute(stripslashes($subject)), ENT_QUOTES); 
  612.  
  613. $body = str_replace("{LINK}", $link, $body); 
  614.  
  615. if ( $is_remind == true && function_exists('wpmq_mail') ) { 
  616. // could be sending lots of reminders so queue them if wpmq is enabled 
  617. @wp_mail($this->email, $subject, $body, $mailheaders, '', 0); 
  618. } else { 
  619. return @wp_mail($this->email, $subject, $body, $mailheaders); 
  620. } // end send_confirm() 
  621.  
  622. /** ===== Public Subscriber functions ===== */ 
  623. /** 
  624. Return an array of all the public subscribers 
  625. */ 
  626. function get_public($confirmed = 1) { 
  627. global $wpdb; 
  628. if ( 1 == $confirmed ) { 
  629. if ( '' == $this->all_confirmed ) { 
  630. $this->all_confirmed = $wpdb->get_col("SELECT email FROM $this->public WHERE active='1'"); 
  631. return $this->all_confirmed; 
  632. } else { 
  633. if ( '' == $this->all_unconfirmed ) { 
  634. $this->all_unconfirmed = $wpdb->get_col("SELECT email FROM $this->public WHERE active='0'"); 
  635. return $this->all_unconfirmed; 
  636. } // end get_public() 
  637.  
  638. /** 
  639. Given a public subscriber ID, returns the email address 
  640. */ 
  641. function get_email($id = 0) { 
  642. global $wpdb; 
  643.  
  644. if ( !$id ) { 
  645. return false; 
  646. return $wpdb->get_var($wpdb->prepare("SELECT email FROM $this->public WHERE id=%d", $id)); 
  647. } // end get_email() 
  648.  
  649. /** 
  650. Given a public subscriber email, returns the subscriber ID 
  651. */ 
  652. function get_id($email = '') { 
  653. global $wpdb; 
  654.  
  655. if ( !$email ) { 
  656. return false; 
  657. return $wpdb->get_var($wpdb->prepare("SELECT id FROM $this->public WHERE email=%s", $email)); 
  658. } // end get_id() 
  659.  
  660. /** 
  661. Add an public subscriber to the subscriber table 
  662. If added by admin it is immediately confirmed, otherwise as unconfirmed 
  663. */ 
  664. function add($email = '', $confirm = false) { 
  665. if ( $this->filtered == 1 ) { return; } 
  666. global $wpdb; 
  667.  
  668. if ( !is_email($email) ) { return false; } 
  669.  
  670. if ( false !== $this->is_public($email) ) { 
  671. // is this an email for a registered user 
  672. $check = $wpdb->get_var($wpdb->prepare("SELECT user_email FROM $wpdb->users WHERE user_email=%s", $this->email)); 
  673. if ( $check ) { return; } 
  674. if ( $confirm ) { 
  675. $wpdb->query($wpdb->prepare("UPDATE $this->public SET active='1', ip=%s WHERE CAST(email as binary)=%s", $this->ip, $email)); 
  676. } else { 
  677. $wpdb->query($wpdb->prepare("UPDATE $this->public SET date=CURDATE(), time=CURTIME() WHERE CAST(email as binary)=%s", $email)); 
  678. } else { 
  679. if ( $confirm ) { 
  680. global $current_user; 
  681. $wpdb->query($wpdb->prepare("INSERT INTO $this->public (email, active, date, time, ip) VALUES (%s, %d, CURDATE(), CURTIME(), %s)", $email, 1, $current_user->user_login)); 
  682. } else { 
  683. $wpdb->query($wpdb->prepare("INSERT INTO $this->public (email, active, date, time, ip) VALUES (%s, %d, CURDATE(), CURTIME(), %s)", $email, 0, $this->ip)); 
  684. } // end add() 
  685.  
  686. /** 
  687. Remove a public subscriber user from the subscription table 
  688. */ 
  689. function delete($email = '') { 
  690. global $wpdb; 
  691.  
  692. if ( !is_email($email) ) { return false; } 
  693. $wpdb->query($wpdb->prepare("DELETE FROM $this->public WHERE CAST(email as binary)=%s", $email)); 
  694. } // end delete() 
  695.  
  696. /** 
  697. Toggle a public subscriber's status 
  698. */ 
  699. function toggle($email = '') { 
  700. global $wpdb; 
  701.  
  702. if ( '' == $email || !is_email($email) ) { return false; } 
  703.  
  704. // let's see if this is a public user 
  705. $status = $this->is_public($email); 
  706. if ( false === $status ) { return false; } 
  707.  
  708. if ( '0' == $status ) { 
  709. $wpdb->query($wpdb->prepare("UPDATE $this->public SET active='1', conf_date=CURDATE(), conf_time=CURTIME(), conf_ip=%s WHERE CAST(email as binary)=%s", $this->ip, $email)); 
  710. } else { 
  711. $wpdb->query($wpdb->prepare("UPDATE $this->public SET active='0', conf_date=CURDATE(), conf_time=CURTIME(), conf_ip=%s WHERE CAST(email as binary)=%s", $this->ip, $email)); 
  712. } // end toggle() 
  713.  
  714. /** 
  715. Send reminder email to unconfirmed public subscribers 
  716. */ 
  717. function remind($emails = '') { 
  718. if ( '' == $emails ) { return false; } 
  719.  
  720. $recipients = explode(", ", $emails); 
  721. if ( !is_array($recipients) ) { $recipients = (array)$recipients; } 
  722. foreach ( $recipients as $recipient ) { 
  723. $this->email = $recipient; 
  724. $this->send_confirm('add', true); 
  725. } //end remind() 
  726.  
  727. /** 
  728. Is the supplied email address a public subscriber? 
  729. */ 
  730. function is_public($email = '') { 
  731. global $wpdb; 
  732.  
  733. if ( '' == $email ) { return false; } 
  734.  
  735. // run the query and force case sensitivity 
  736. $check = $wpdb->get_var($wpdb->prepare("SELECT active FROM $this->public WHERE CAST(email as binary)=%s", $email)); 
  737. if ( '0' == $check || '1' == $check ) { 
  738. return $check; 
  739. } else { 
  740. return false; 
  741. } // end is_public() 
  742.  
  743. /** ===== Registered User and Subscriber functions ===== */ 
  744. /** 
  745. Is the supplied email address a registered user of the blog? 
  746. */ 
  747. function is_registered($email = '') { 
  748. global $wpdb; 
  749.  
  750. if ( '' == $email ) { return false; } 
  751.  
  752. $check = $wpdb->get_var($wpdb->prepare("SELECT user_email FROM $wpdb->users WHERE user_email=%s", $email)); 
  753. if ( $check ) { 
  754. return true; 
  755. } else { 
  756. return false; 
  757. } // end is_registered() 
  758.  
  759. /** 
  760. Return Registered User ID from email 
  761. */ 
  762. function get_user_id($email = '') { 
  763. global $wpdb; 
  764.  
  765. if ( '' == $email ) { return false; } 
  766.  
  767. $id = $wpdb->get_var($wpdb->prepare("SELECT id FROM $wpdb->users WHERE user_email=%s", $email)); 
  768.  
  769. return $id; 
  770. } // end get_user_id() 
  771.  
  772. /** 
  773. Return an array of all subscribers emails or IDs 
  774. */ 
  775. function get_all_registered($return = 'email') { 
  776. global $wpdb; 
  777.  
  778. if ( $this->s2_mu ) { 
  779. if ( $return === 'ID' ) { 
  780. if ( $this->all_registered_id === '' ) { 
  781. $this->all_registered_id = $wpdb->get_col("SELECT user_id FROM $wpdb->usermeta WHERE meta_key='" . $wpdb->prefix . "capabilities'"); 
  782. return $this->all_registered_id; 
  783. } else { 
  784. if ( $this->all_registered_email === '' ) { 
  785. $this->all_registered_email = $wpdb->get_col("SELECT a.user_email FROM $wpdb->users AS a INNER JOIN $wpdb->usermeta AS b ON a.ID = b.user_id WHERE b.meta_key='" . $wpdb->prefix . "capabilities'"); 
  786. return $this->all_registered_email; 
  787. } else { 
  788. if ( $return === 'ID' ) { 
  789. if ( $this->all_registered_id === '' ) { 
  790. $this->all_registered_id = $wpdb->get_col("SELECT ID FROM $wpdb->users"); 
  791. return $this->all_registered_id; 
  792. } else { 
  793. if ( $this->all_registered_email === '' ) { 
  794. $this->all_registered_email = $wpdb->get_col("SELECT user_email FROM $wpdb->users"); 
  795. return $this->all_registered_email; 
  796. } // end get_all_registered() 
  797.  
  798. /** 
  799. Return an array of registered subscribers 
  800. Collect all the registered users of the blog who are subscribed to the specified categories 
  801. */ 
  802. function get_registered($args = '') { 
  803. global $wpdb; 
  804.  
  805. parse_str($args, $r); 
  806. if ( !isset($r['format']) ) 
  807. $r['format'] = 'all'; 
  808. if ( !isset($r['cats']) ) 
  809. $r['cats'] = ''; 
  810. if ( !isset($r['author']) ) 
  811. $r['author'] = ''; 
  812.  
  813. // collect all subscribers for compulsory categories 
  814. $compulsory = explode(', ', $this->subscribe2_options['compulsory']); 
  815. foreach ( explode(', ', $r['cats']) as $cat ) { 
  816. if ( in_array($cat, $compulsory) ) { 
  817. $r['cats'] = ''; 
  818.  
  819. $JOIN = ''; $AND = ''; 
  820. // text or HTML subscribers 
  821. if ( 'all' != $r['format'] ) { 
  822. $JOIN .= "INNER JOIN $wpdb->usermeta AS b ON a.user_id = b.user_id "; 
  823. $AND .= $wpdb->prepare(" AND b.meta_key=%s AND b.meta_value=", $this->get_usermeta_keyname('s2_format')); 
  824. if ( 'html' == $r['format'] ) { 
  825. $AND .= "'html'"; 
  826. } elseif ( 'html_excerpt' == $r['format'] ) { 
  827. $AND .= "'html_excerpt'"; 
  828. } elseif ( 'post' == $r['format'] ) { 
  829. $AND .= "'post'"; 
  830. } elseif ( 'excerpt' == $r['format'] ) { 
  831. $AND .= "'excerpt'"; 
  832.  
  833. // specific category subscribers 
  834. if ( '' != $r['cats'] ) { 
  835. $JOIN .= "INNER JOIN $wpdb->usermeta AS c ON a.user_id = c.user_id "; 
  836. $and = ''; 
  837. foreach ( explode(', ', $r['cats']) as $cat ) { 
  838. ('' == $and) ? $and = $wpdb->prepare("c.meta_key=%s", $this->get_usermeta_keyname('s2_cat') . $cat) : $and .= $wpdb->prepare(" OR c.meta_key=%s", $this->get_usermeta_keyname('s2_cat') . $cat); 
  839. $AND .= " AND ($and)"; 
  840.  
  841. // specific authors 
  842. if ( '' != $r['author'] ) { 
  843. $JOIN .= "INNER JOIN $wpdb->usermeta AS d ON a.user_id = d.user_id "; 
  844. $AND .= $wpdb->prepare(" AND (d.meta_key=%s AND NOT FIND_IN_SET(%s, d.meta_value))", $this->get_usermeta_keyname('s2_authors'), $r['author']); 
  845.  
  846. if ( $this->s2_mu ) { 
  847. $sql = $wpdb->prepare("SELECT a.user_id FROM $wpdb->usermeta AS a INNER JOIN $wpdb->usermeta AS e ON a.user_id = e.user_id " . $JOIN . "WHERE a.meta_key='" . $wpdb->prefix . "capabilities' AND e.meta_key=%s AND e.meta_value <> ''" . $AND, $this->get_usermeta_keyname('s2_subscribed')); 
  848. } else { 
  849. $sql = $wpdb->prepare("SELECT a.user_id FROM $wpdb->usermeta AS a " . $JOIN . "WHERE a.meta_key=%s AND a.meta_value <> ''" . $AND, $this->get_usermeta_keyname('s2_subscribed')); 
  850. $result = $wpdb->get_col($sql); 
  851. if ( $result ) { 
  852. $ids = implode(', ', array_map(array($this, 'prepare_in_data'), $result)); 
  853. $registered = $wpdb->get_col("SELECT user_email FROM $wpdb->users WHERE ID IN ($ids)"); 
  854.  
  855. if ( empty($registered) ) { return array(); } 
  856.  
  857. // apply filter to registered users to add or remove additional addresses, pass args too for additional control 
  858. $registered = apply_filters('s2_registered_subscribers', $registered, $args); 
  859. return $registered; 
  860. } // end get_registered() 
  861.  
  862. /** 
  863. Function to ensure email is compliant with internet messaging standards 
  864. */ 
  865. function sanitize_email($email) { 
  866. $email = trim($email); 
  867. if ( !is_email($email) ) { return; } 
  868.  
  869. // ensure that domain is in lowercase as per internet email standards http://www.ietf.org/rfc/rfc5321.txt 
  870. list($name, $domain) = explode('@', $email, 2); 
  871. return $name . "@" . strtolower($domain); 
  872. } // end sanitize_email() 
  873.  
  874. /** 
  875. Create the appropriate usermeta values when a user registers 
  876. If the registering user had previously subscribed to notifications, this function will delete them from the public subscriber list first 
  877. */ 
  878. function register($user_ID = 0, $consent = false) { 
  879. global $wpdb; 
  880.  
  881. if ( 0 == $user_ID ) { return $user_ID; } 
  882. $user = get_userdata($user_ID); 
  883.  
  884. // Subscribe registered users to categories obeying excluded categories 
  885. if ( 0 == $this->subscribe2_options['reg_override'] || 'no' == $this->subscribe2_options['newreg_override'] ) { 
  886. $all_cats = $this->all_cats(true, 'ID'); 
  887. } else { 
  888. $all_cats = $this->all_cats(false, 'ID'); 
  889.  
  890. $cats = ''; 
  891. foreach ( $all_cats as $cat ) { 
  892. ('' == $cats) ? $cats = "$cat->term_id" : $cats .= ", $cat->term_id"; 
  893.  
  894. if ( '' == $cats ) { 
  895. // sanity check, might occur if all cats excluded and reg_override = 0 
  896. return $user_ID; 
  897.  
  898. // has this user previously signed up for email notification? 
  899. if ( false !== $this->is_public($this->sanitize_email($user->user_email)) ) { 
  900. // delete this user from the public table, and subscribe them to all the categories 
  901. $this->delete($user->user_email); 
  902. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), $cats); 
  903. foreach ( explode(', ', $cats) as $cat ) { 
  904. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat, $cat); 
  905. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_format'), 'excerpt'); 
  906. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_autosub'), $this->subscribe2_options['autosub_def']); 
  907. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_authors'), ''); 
  908. } else { 
  909. // create post format entries for all users 
  910. if ( in_array($this->subscribe2_options['autoformat'], array('html', 'html_excerpt', 'post', 'excerpt')) ) { 
  911. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_format'), $this->subscribe2_options['autoformat']); 
  912. } else { 
  913. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_format'), 'excerpt'); 
  914. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_autosub'), $this->subscribe2_options['autosub_def']); 
  915. // if the are no existing subscriptions, create them if we have consent 
  916. if ( true === $consent ) { 
  917. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), $cats); 
  918. foreach ( explode(', ', $cats) as $cat ) { 
  919. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat, $cat); 
  920. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_authors'), ''); 
  921. return $user_ID; 
  922. } // end register() 
  923.  
  924. /** 
  925. Get admin data from record 1 or first user with admin rights 
  926. */ 
  927. function get_userdata($admin_id) { 
  928. global $wpdb, $userdata; 
  929.  
  930. if ( is_numeric($admin_id) ) { 
  931. $admin = get_userdata($admin_id); 
  932. } elseif ( $admin_id == 'admin' ) { 
  933. //ensure compatibility with < 4.16 
  934. $admin = get_userdata('1'); 
  935. } else { 
  936. $admin = &$userdata; 
  937.  
  938. if ( empty($admin) || $admin->ID == 0 ) { 
  939. $role = array('role' => 'administrator'); 
  940. $wp_user_query = get_users( $role ); 
  941. $admin = $wp_user_query[0]; 
  942.  
  943. return $admin; 
  944. } //end get_userdata() 
  945.  
  946. /** 
  947. Subscribe/unsubscribe user from one-click submission 
  948. */ 
  949. function one_click_handler($user_ID, $action) { 
  950. if ( !isset($user_ID) || !isset($action) ) { return; } 
  951.  
  952. $all_cats = $this->all_cats(true); 
  953.  
  954. if ( 'subscribe' == $action ) { 
  955. // Subscribe 
  956. $new_cats = array(); 
  957. foreach ( $all_cats as $cat ) { 
  958. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat->term_id, $cat->term_id); 
  959. $new_cats[] = $cat->term_id; 
  960.  
  961. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), implode(', ', $new_cats)); 
  962.  
  963. if ( 'yes' == $this->subscribe2_options['show_autosub'] && 'no' != get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true) ) { 
  964. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_autosub'), 'yes'); 
  965. } elseif ( 'unsubscribe' == $action ) { 
  966. // Unsubscribe 
  967. foreach ( $all_cats as $cat ) { 
  968. delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat->term_id); 
  969.  
  970. delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed')); 
  971. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_autosub'), 'no'); 
  972. } //end one_click_handler() 
  973.  
  974. /** ===== helper functions: forms and stuff ===== */ 
  975. /** 
  976. Get an object of all categories, include default and custom type 
  977. */ 
  978. function all_cats($exclude = false, $orderby = 'slug') { 
  979. $all_cats = array(); 
  980. $s2_taxonomies = apply_filters('s2_taxonomies', array('category')); 
  981.  
  982. foreach( $s2_taxonomies as $taxonomy ) { 
  983. if ( taxonomy_exists($taxonomy) ) { 
  984. $all_cats = array_merge($all_cats, get_categories(array('hide_empty' => false, 'orderby' => $orderby, 'taxonomy' => $taxonomy))); 
  985.  
  986. if ( $exclude === true ) { 
  987. // remove excluded categories from the returned object 
  988. $excluded = explode(', ', $this->subscribe2_options['exclude']); 
  989.  
  990. // need to use $id like this as this is a mixed array / object 
  991. $id = 0; 
  992. foreach ( $all_cats as $cat) { 
  993. if ( in_array($cat->term_id, $excluded) ) { 
  994. unset($all_cats[$id]); 
  995. $id++; 
  996.  
  997. return $all_cats; 
  998. } // end all_cats() 
  999.  
  1000. /** 
  1001. Function to sanitise array of data for SQL 
  1002. */ 
  1003. function prepare_in_data($data) { 
  1004. global $wpdb; 
  1005. return $wpdb->prepare('%s', $data); 
  1006. } // end prepare_in_data() 
  1007.  
  1008. /** 
  1009. Export subscriber emails and other details to CSV 
  1010. */ 
  1011. function prepare_export( $subscribers ) { 
  1012. $subscribers = explode(", \r\n", $subscribers); 
  1013. natcasesort($subscribers); 
  1014.  
  1015. $exportcsv = "User Email, User Type, User Name"; 
  1016. $all_cats = $this->all_cats(false, 'ID'); 
  1017.  
  1018. foreach ($all_cats as $cat) { 
  1019. $exportcsv .= ", " . $cat->cat_name; 
  1020. $cat_ids[] = $cat->term_id; 
  1021. $exportcsv .= "\r\n"; 
  1022.  
  1023. if ( !function_exists('get_userdata') ) { 
  1024. require_once(ABSPATH . WPINC . '/pluggable.php'); 
  1025.  
  1026. foreach ( $subscribers as $subscriber ) { 
  1027. if ( $this->is_registered($subscriber) ) { 
  1028. $user_ID = $this->get_user_id( $subscriber ); 
  1029. $user_info = get_userdata( $user_ID ); 
  1030.  
  1031. $cats = explode(', ', get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true)); 
  1032. $subscribed_cats = ''; 
  1033. foreach ( $cat_ids as $cat ) { 
  1034. (in_array($cat, $cats)) ? $subscribed_cats .= ", Yes" : $subscribed_cats .= ", No"; 
  1035.  
  1036. $exportcsv .= $subscriber . ', '; 
  1037. $exportcsv .= __('Registered User', 'subscribe2'); 
  1038. $exportcsv .= ', ' . $user_info->display_name; 
  1039. $exportcsv .= $subscribed_cats . "\r\n"; 
  1040. } else { 
  1041. if ( $this->is_public($subscriber) === '1' ) { 
  1042. $exportcsv .= $subscriber . ', ' . __('Confirmed Public Subscriber', 'subscribe2') . "\r\n"; 
  1043. } elseif ( $this->is_public($subscriber) === '0' ) { 
  1044. $exportcsv .= $subscriber . ', ' . __('Unconfirmed Public Subscriber', 'subscribe2') . "\r\n"; 
  1045.  
  1046. return $exportcsv; 
  1047. } // end prepare_export() 
  1048.  
  1049. /** 
  1050. Filter for usermeta table key names to adjust them if needed for WPMU blogs 
  1051. */ 
  1052. function get_usermeta_keyname($metaname) { 
  1053. global $wpdb; 
  1054.  
  1055. // Is this WordPressMU or not? 
  1056. if ( $this->s2_mu === true ) { 
  1057. switch( $metaname ) { 
  1058. case 's2_subscribed': 
  1059. case 's2_cat': 
  1060. case 's2_format': 
  1061. case 's2_autosub': 
  1062. case 's2_authors': 
  1063. return $wpdb->prefix . $metaname; 
  1064. break; 
  1065. // Not MU or not a prefixed option name 
  1066. return $metaname; 
  1067. } // end get_usermeta_keyname() 
  1068.  
  1069. /** 
  1070. Adds information to the WordPress registration screen for new users 
  1071. */ 
  1072. function register_form() { 
  1073. if ( 'no' == $this->subscribe2_options['autosub'] ) { return; } 
  1074. if ( 'wpreg' == $this->subscribe2_options['autosub'] ) { 
  1075. echo "<p>\r\n<label>"; 
  1076. echo __('Check here to Subscribe to email notifications for new posts', 'subscribe2') . ":<br />\r\n"; 
  1077. echo "<input type=\"checkbox\" name=\"reg_subscribe\"" . checked($this->subscribe2_options['wpregdef'], 'yes', false) . " />"; 
  1078. echo "</label>\r\n"; 
  1079. echo "</p>\r\n"; 
  1080. } elseif ( 'yes' == $this->subscribe2_options['autosub'] ) { 
  1081. echo "<p>\r\n<center>\r\n"; 
  1082. echo __('By registering with this blog you are also agreeing to receive email notifications for new posts but you can unsubscribe at anytime', 'subscribe2') . ".<br />\r\n"; 
  1083. echo "</center></p>\r\n"; 
  1084. } // end register_form() 
  1085.  
  1086. /** 
  1087. Process function to add action if user selects to subscribe to posts during registration 
  1088. */ 
  1089. function register_post($user_ID = 0) { 
  1090. global $_POST; 
  1091. if ( 0 == $user_ID ) { return; } 
  1092. if ( 'yes' == $this->subscribe2_options['autosub'] || ( 'on' == $_POST['reg_subscribe'] && 'wpreg' == $this->subscribe2_options['autosub'] ) ) { 
  1093. $this->register($user_ID, true); 
  1094. } else { 
  1095. $this->register($user_ID, false); 
  1096. } // end register_post() 
  1097.  
  1098. /** ===== comment subscriber functions ===== */ 
  1099. /** 
  1100. Display check box on comment page 
  1101. */ 
  1102. function s2_comment_meta_form() { 
  1103. if ( is_user_logged_in() ) { 
  1104. echo $this->profile; 
  1105. } else { 
  1106. echo "<p style=\"width: auto;\"><label><input type=\"checkbox\" name=\"s2_comment_request\" value=\"1\" " . checked($this->subscribe2_options['comment_def'], 'yes', false) . "/>" . __('Check here to Subscribe to notifications for new posts', 'subscribe2') . "</label></p>"; 
  1107. } // end s2_comment_meta_form() 
  1108.  
  1109. /** 
  1110. Process comment meta data 
  1111. */ 
  1112. function s2_comment_meta($comment_ID, $approved = 0) { 
  1113. if ( $_POST['s2_comment_request'] == '1' ) { 
  1114. switch ($approved) { 
  1115. case '0': 
  1116. // Unapproved so hold in meta data pending moderation 
  1117. add_comment_meta($comment_ID, 's2_comment_request', $_POST['s2_comment_request']); 
  1118. break; 
  1119. case '1': 
  1120. // Approved so add 
  1121. $is_public = $this->is_public($comment->comment_author_email); 
  1122. if ( $is_public == 0 ) { 
  1123. $this->toggle($comment->comment_author_email); 
  1124. $is_registered = $this->is_registered($comment->comment_author_email); 
  1125. if ( !$is_public && !$is_registered ) { 
  1126. $this->add($comment->comment_author_email, true); 
  1127. break; 
  1128. default : 
  1129. break; 
  1130. } // end s2_comment_meta() 
  1131.  
  1132. /** 
  1133. Action subscribe requests made on comment forms when comments are approved 
  1134. */ 
  1135. function comment_status($comment_ID = 0) { 
  1136. global $wpdb; 
  1137.  
  1138. // get meta data 
  1139. $subscribe = get_comment_meta($comment_ID, 's2_comment_request', true); 
  1140. if ( $subscribe != '1' ) { return $comment_ID; } 
  1141.  
  1142. // Retrieve the information about the comment 
  1143. $sql = $wpdb->prepare("SELECT comment_author_email, comment_approved FROM $wpdb->comments WHERE comment_ID=%s LIMIT 1", $comment_ID); 
  1144. $comment = $wpdb->get_row($sql, OBJECT); 
  1145. if ( empty($comment) ) { return $comment_ID; } 
  1146.  
  1147. switch ($comment->comment_approved) { 
  1148. case '0': // Unapproved 
  1149. break; 
  1150. case '1': // Approved 
  1151. $is_public = $this->is_public($comment->comment_author_email); 
  1152. if ( $is_public == 0 ) { 
  1153. $this->toggle($comment->comment_author_email); 
  1154. $is_registered = $this->is_registered($comment->comment_author_email); 
  1155. if ( !$is_public && !$is_registered ) { 
  1156. $this->add($comment->comment_author_email, true); 
  1157. delete_comment_meta($comment_ID, 's2_comment_request'); 
  1158. break; 
  1159. default: // post is trash, spam or deleted 
  1160. delete_comment_meta($comment_ID, 's2_comment_request'); 
  1161. break; 
  1162.  
  1163. return $comment_ID; 
  1164. } // end comment_status() 
  1165.  
  1166. /** ===== widget functions ===== */ 
  1167. /** 
  1168. Register the form widget 
  1169. */ 
  1170. function subscribe2_widget() { 
  1171. require_once( S2PATH . 'include/widget.php'); 
  1172. register_widget('S2_Form_widget'); 
  1173. } // end subscribe2_widget() 
  1174.  
  1175. /** 
  1176. Register the counter widget 
  1177. */ 
  1178. function counter_widget() { 
  1179. require_once( S2PATH . 'include/counterwidget.php'); 
  1180. register_widget('S2_Counter_widget'); 
  1181. } // end counter_widget() 
  1182.  
  1183. /** ===== wp-cron functions ===== */ 
  1184. /** 
  1185. Add a weekly event to cron 
  1186. */ 
  1187. function add_weekly_sched($sched) { 
  1188. $sched['weekly'] = array('interval' => 604800, 'display' => __('Weekly', 'subscribe2')); 
  1189. return $sched; 
  1190. } // end add_weekly_sched() 
  1191.  
  1192. /** 
  1193. Send a digest of recent new posts 
  1194. */ 
  1195. function subscribe2_cron($preview = '', $resend = '') { 
  1196. if ( defined('DOING_S2_CRON') && DOING_S2_CRON ) { return; } 
  1197. define( 'DOING_S2_CRON', true ); 
  1198. global $wpdb, $post; 
  1199.  
  1200. if ( '' == $preview ) { 
  1201. // update last_s2cron execution time before completing or bailing 
  1202. $now = current_time('mysql'); 
  1203. $prev = $this->subscribe2_options['last_s2cron']; 
  1204. $last = $this->subscribe2_options['previous_s2cron']; 
  1205. $this->subscribe2_options['last_s2cron'] = $now; 
  1206. $this->subscribe2_options['previous_s2cron'] = $prev; 
  1207. if ( '' == $resend ) { 
  1208. // update sending times provided this is not a resend 
  1209. update_option('subscribe2_options', $this->subscribe2_options); 
  1210.  
  1211. // set up SQL query based on options 
  1212. if ( $this->subscribe2_options['private'] == 'yes' ) { 
  1213. $status = "'publish', 'private'"; 
  1214. } else { 
  1215. $status = "'publish'"; 
  1216.  
  1217. // send notifications for allowed post type (defaults for posts and pages) 
  1218. // uses s2_post_types filter to allow for custom post types in WP 3.0 
  1219. if ( $this->subscribe2_options['pages'] == 'yes' ) { 
  1220. $s2_post_types = array('page', 'post'); 
  1221. } else { 
  1222. $s2_post_types = array('post'); 
  1223. $s2_post_types = apply_filters('s2_post_types', $s2_post_types); 
  1224. foreach( $s2_post_types as $post_type ) { 
  1225. ('' == $type) ? $type = $wpdb->prepare("%s", $post_type) : $type .= $wpdb->prepare(", %s", $post_type); 
  1226.  
  1227. // collect posts 
  1228. if ( $resend == 'resend' ) { 
  1229. if ( $this->subscribe2_options['cron_order'] == 'desc' ) { 
  1230. $posts = $wpdb->get_results($wpdb->prepare("SELECT ID, post_title, post_excerpt, post_content, post_type, post_password, post_date, post_author FROM $wpdb->posts WHERE post_date >= %s AND post_date < %s AND post_status IN ($status) AND post_type IN ($type) ORDER BY post_date DESC", $last, $prev)); 
  1231. } else { 
  1232. $posts = $wpdb->get_results($wpdb->prepare("SELECT ID, post_title, post_excerpt, post_content, post_type, post_password, post_date, post_author FROM $wpdb->posts WHERE post_date >= %s AND post_date < %s AND post_status IN ($status) AND post_type IN ($type) ORDER BY post_date ASC", $last, $prev)); 
  1233. } else { 
  1234. if ( $this->subscribe2_options['cron_order'] == 'desc' ) { 
  1235. $posts = $wpdb->get_results($wpdb->prepare("SELECT ID, post_title, post_excerpt, post_content, post_type, post_password, post_date, post_author FROM $wpdb->posts WHERE post_date >= %s AND post_date < %s AND post_status IN ($status) AND post_type IN ($type) ORDER BY post_date DESC", $prev, $now)); 
  1236. } else { 
  1237. $posts = $wpdb->get_results($wpdb->prepare("SELECT ID, post_title, post_excerpt, post_content, post_type, post_password, post_date, post_author FROM $wpdb->posts WHERE post_date >= %s AND post_date < %s AND post_status IN ($status) AND post_type IN ($type) ORDER BY post_date ASC", $prev, $now)); 
  1238. } else { 
  1239. // we are sending a preview 
  1240. $posts = get_posts('numberposts=1'); 
  1241.  
  1242. // Collect sticky posts if desired 
  1243. if ( $this->subscribe2_options['stickies'] == 'yes' ) { 
  1244. $stickies = get_posts(array('post__in' => get_option('sticky_posts'))); 
  1245. if ( !empty($stickies) ) { 
  1246. $posts = array_merge((array)$stickies, (array)$posts); 
  1247.  
  1248. // do we have any posts? 
  1249. if ( empty($posts) && !has_filter('s2_digest_email') ) { return false; } 
  1250. $this->post_count = count($posts); 
  1251.  
  1252. // if we have posts, let's prepare the digest 
  1253. $datetime = get_option('date_format') . ' @ ' . get_option('time_format'); 
  1254. $all_post_cats = array(); 
  1255. $ids = array(); 
  1256. $mailtext = apply_filters('s2_email_template', $this->subscribe2_options['mailtext']); 
  1257. $table = ''; 
  1258. $tablelinks = ''; 
  1259. $message_post= ''; 
  1260. $message_posttime = ''; 
  1261. foreach ( $posts as $post ) { 
  1262. // keep an array of post ids and skip if we've already done it once 
  1263. if ( in_array($post->ID, $ids) ) { continue; } 
  1264. $ids[] = $post->ID; 
  1265. $s2_taxonomies = apply_filters('s2_taxonomies', array('category')); 
  1266. $post_cats = wp_get_object_terms($post->ID, $s2_taxonomies, array('fields' => 'ids')); 
  1267. $post_cats_string = implode(', ', $post_cats); 
  1268. $all_post_cats = array_unique(array_merge($all_post_cats, $post_cats)); 
  1269. $check = false; 
  1270. // Pages are put into category 1 so make sure we don't exclude 
  1271. // pages if category 1 is excluded 
  1272. if ( $post->post_type != 'page' ) { 
  1273. // is the current post assigned to any categories 
  1274. // which should not generate a notification email? 
  1275. foreach ( explode(', ', $this->subscribe2_options['exclude']) as $cat ) { 
  1276. if ( in_array($cat, $post_cats) ) { 
  1277. $check = true; 
  1278. // is the current post set by the user to 
  1279. // not generate a notification email? 
  1280. $s2mail = get_post_meta($post->ID, '_s2mail', true); 
  1281. if ( strtolower(trim($s2mail)) == 'no' ) { 
  1282. $check = true; 
  1283. // is the current post private 
  1284. // and should this not generate a notification email? 
  1285. if ( $this->subscribe2_options['password'] == 'no' && $post->post_password != '' ) { 
  1286. $check = true; 
  1287. // is the post assigned a format that should 
  1288. // not be included in the notification email? 
  1289. $post_format = get_post_format($post->ID); 
  1290. $excluded_formats = explode(', ', $this->subscribe2_options['exclude_formats']); 
  1291. if ( $post_format !== false && in_array($post_format, $excluded_formats) ) { 
  1292. $check = true; 
  1293. // if this post is excluded 
  1294. // don't include it in the digest 
  1295. if ( $check ) { 
  1296. continue; 
  1297. $post_title = html_entity_decode($post->post_title, ENT_QUOTES); 
  1298. ('' == $table) ? $table .= "* " . $post_title : $table .= "\r\n* " . $post_title; 
  1299. ('' == $tablelinks) ? $tablelinks .= "* " . $post_title : $tablelinks .= "\r\n* " . $post_title; 
  1300. $message_post .= $post_title; 
  1301. $message_posttime .= $post_title; 
  1302. if ( strstr($mailtext, "{AUTHORNAME}") ) { 
  1303. $author = get_userdata($post->post_author); 
  1304. if ( $author->display_name != '' ) { 
  1305. $message_post .= " (" . __('Author', 'subscribe2') . ": " . html_entity_decode(apply_filters('the_author', $author->display_name), ENT_QUOTES) . ")"; 
  1306. $message_posttime .= " (" . __('Author', 'subscribe2') . ": " . html_entity_decode(apply_filters('the_author', $author->display_name), ENT_QUOTES) . ")"; 
  1307. $message_post .= "\r\n"; 
  1308. $message_posttime .= "\r\n"; 
  1309.  
  1310. $message_posttime .= __('Posted on', 'subscribe2') . ": " . mysql2date($datetime, $post->post_date) . "\r\n"; 
  1311. if ( strstr($mailtext, "{TINYLINK}") ) { 
  1312. $tinylink = file_get_contents('http://tinyurl.com/api-create.php?url=' . urlencode($this->get_tracking_link(get_permalink($post->ID)))); 
  1313. } else { 
  1314. $tinylink = false; 
  1315. if ( strstr($mailtext, "{TINYLINK}") && $tinylink !== 'Error' && $tinylink !== false ) { 
  1316. $tablelinks .= "\r\n" . $tinylink . "\r\n"; 
  1317. $message_post .= $tinylink . "\r\n"; 
  1318. $message_posttime .= $tinylink . "\r\n"; 
  1319. } else { 
  1320. $tablelinks .= "\r\n" . $this->get_tracking_link(get_permalink($post->ID)) . "\r\n"; 
  1321. $message_post .= $this->get_tracking_link(get_permalink($post->ID)) . "\r\n"; 
  1322. $message_posttime .= $this->get_tracking_link(get_permalink($post->ID)) . "\r\n"; 
  1323.  
  1324. if ( strstr($mailtext, "{CATS}") ) { 
  1325. $post_cat_names = implode(', ', wp_get_object_terms($post->ID, $s2_taxonomies, array('fields' => 'names'))); 
  1326. $message_post .= __('Posted in', 'subscribe2') . ": " . $post_cat_names . "\r\n"; 
  1327. $message_posttime .= __('Posted in', 'subscribe2') . ": " . $post_cat_names . "\r\n"; 
  1328. if ( strstr($mailtext, "{TAGS}") ) { 
  1329. $post_tag_names = implode(', ', wp_get_post_tags($post->ID, array('fields' => 'names'))); 
  1330. if ( $post_tag_names != '' ) { 
  1331. $message_post .= __('Tagged as', 'subscribe2') . ": " . $post_tag_names . "\r\n"; 
  1332. $message_posttime .= __('Tagged as', 'subscribe2') . ": " . $post_tag_names . "\r\n"; 
  1333. $message_post .= "\r\n"; 
  1334. $message_posttime .= "\r\n"; 
  1335.  
  1336. ( !empty($post->post_excerpt) ) ? $excerpt = $post->post_excerpt : $excerpt = ''; 
  1337. if ( '' == $excerpt ) { 
  1338. // no excerpt, is there a <!--more--> ? 
  1339. if ( false !== strpos($post->post_content, '<!--more-->') ) { 
  1340. list($excerpt, $more) = explode('<!--more-->', $post->post_content, 2); 
  1341. $excerpt = strip_tags($excerpt); 
  1342. if ( function_exists('strip_shortcodes') ) { 
  1343. $excerpt = strip_shortcodes($excerpt); 
  1344. } else { 
  1345. $excerpt = strip_tags($post->post_content); 
  1346. if ( function_exists('strip_shortcodes') ) { 
  1347. $excerpt = strip_shortcodes($excerpt); 
  1348. $words = explode(' ', $excerpt, $this->excerpt_length + 1); 
  1349. if ( count($words) > $this->excerpt_length ) { 
  1350. array_pop($words); 
  1351. array_push($words, '[...]'); 
  1352. $excerpt = implode(' ', $words); 
  1353. // strip leading and trailing whitespace 
  1354. $excerpt = trim($excerpt); 
  1355. $message_post .= $excerpt . "\r\n\r\n"; 
  1356. $message_posttime .= $excerpt . "\r\n\r\n"; 
  1357.  
  1358. // we add a blank line after each post excerpt now trim white space that occurs for the last post 
  1359. $message_post = trim($message_post); 
  1360. $message_posttime = trim($message_posttime); 
  1361. // remove excess white space from within $message_post and $message_posttime 
  1362. $message_post = preg_replace('|[ ]+|', ' ', $message_post); 
  1363. $message_posttime = preg_replace('|[ ]+|', ' ', $message_posttime); 
  1364. $message_post = preg_replace("|[\r\n]{3, }|", "\r\n\r\n", $message_post); 
  1365. $message_posttime = preg_replace("|[\r\n]{3, }|", "\r\n\r\n", $message_posttime); 
  1366.  
  1367. // apply filter to allow external content to be inserted or content manipulated 
  1368. $message_post = apply_filters('s2_digest_email', $message_post, $now, $prev, $last, $this->subscribe2_options['cron_order']); 
  1369. $message_posttime = apply_filters('s2_digest_email', $message_posttime, $now, $prev, $last, $this->subscribe2_options['cron_order']); 
  1370.  
  1371. //sanity check - don't send a mail if the content is empty 
  1372. if ( !$message_post && !$message_posttime && !$table && !$tablelinks ) { 
  1373. return; 
  1374.  
  1375. // get sender details 
  1376. if ( $this->subscribe2_options['sender'] == 'blogname' ) { 
  1377. $this->myname = html_entity_decode(get_option('blogname'), ENT_QUOTES); 
  1378. $this->myemail = get_bloginfo('admin_email'); 
  1379. } else { 
  1380. $user = $this->get_userdata($this->subscribe2_options['sender']); 
  1381. $this->myemail = $user->user_email; 
  1382. $this->myname = html_entity_decode($user->display_name, ENT_QUOTES); 
  1383.  
  1384. $scheds = (array)wp_get_schedules(); 
  1385. $email_freq = $this->subscribe2_options['email_freq']; 
  1386. $display = $scheds[$email_freq]['display']; 
  1387. ( '' == get_option('blogname') ) ? $subject = "" : $subject = "[" . stripslashes(html_entity_decode(get_option('blogname'), ENT_QUOTES)) . "] "; 
  1388. $subject .= $display . " " . __('Digest Email', 'subscribe2'); 
  1389. $mailtext = str_replace("{TABLELINKS}", $tablelinks, $mailtext); 
  1390. $mailtext = str_replace("{TABLE}", $table, $mailtext); 
  1391. $mailtext = str_replace("{POSTTIME}", $message_posttime, $mailtext); 
  1392. $mailtext = str_replace("{POST}", $message_post, $mailtext); 
  1393. $mailtext = stripslashes($this->substitute($mailtext)); 
  1394.  
  1395. // prepare recipients 
  1396. if ( $preview != '' ) { 
  1397. $this->myemail = $preview; 
  1398. $this->myname = __('Digest Preview', 'subscribe2'); 
  1399. $this->mail(array($preview), $subject, $mailtext); 
  1400. } else { 
  1401. $public = $this->get_public(); 
  1402. $all_post_cats_string = implode(', ', $all_post_cats); 
  1403. $registered = $this->get_registered("cats=$all_post_cats_string"); 
  1404. $recipients = array_merge((array)$public, (array)$registered); 
  1405. $this->mail($recipients, $subject, $mailtext); 
  1406. } // end subscribe2_cron() 
  1407.  
  1408. function s2cleaner_task() { 
  1409. $unconfirmed = $this->get_public('0'); 
  1410. if ( empty($unconfirmed) ) { return; } 
  1411. global $wpdb; 
  1412. $sql = "SELECT email FROM $this->public WHERE active='0' AND date < DATE_SUB(CURDATE(), INTERVAL " . $this->clean_interval . " DAY)"; 
  1413. $old_unconfirmed = $wpdb->get_col( $sql ); 
  1414. if ( empty($old_unconfirmed) ) { 
  1415. return; 
  1416. } else { 
  1417. foreach ($old_unconfirmed as $email) { 
  1418. $this->delete($email); 
  1419. return; 
  1420. } // end s2cleaner_task() 
  1421.  
  1422. /** ===== Our constructor ===== */ 
  1423. /** 
  1424. Subscribe2 constructor 
  1425. */ 
  1426. function s2init() { 
  1427. global $wpdb, $wp_version, $wpmu_version; 
  1428. // load the options 
  1429. $this->subscribe2_options = get_option('subscribe2_options'); 
  1430. // if SCRIPT_DEBUG is true, use dev scripts 
  1431. $this->script_debug = ( defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ) ? '' : '.min'; 
  1432.  
  1433. // get the WordPress release number for in code version comparisons 
  1434. $tmp = explode('-', $wp_version, 2); 
  1435. $this->wp_release = $tmp[0]; 
  1436.  
  1437. // Is this WordPressMU or not? 
  1438. if ( isset($wpmu_version) || strpos($wp_version, 'wordpress-mu') ) { 
  1439. $this->s2_mu = true; 
  1440. if ( function_exists('is_multisite') && is_multisite() ) { 
  1441. $this->s2_mu = true; 
  1442.  
  1443. // add action to handle WPMU subscriptions and unsubscriptions 
  1444. if ( $this->s2_mu === true ) { 
  1445. require_once(S2PATH . "classes/class-s2-multisite.php"); 
  1446. global $s2class_multisite; 
  1447. $s2class_multisite = new s2_multisite; 
  1448. if ( isset($_GET['s2mu_subscribe']) || isset($_GET['s2mu_unsubscribe']) ) { 
  1449. add_action('init', array(&$s2class_multisite, 'wpmu_subscribe')); 
  1450.  
  1451. // load our translations 
  1452. add_action('plugins_loaded', array(&$this, 'load_translations')); 
  1453.  
  1454. // do we need to install anything? 
  1455. $this->public = $wpdb->prefix . "subscribe2"; 
  1456. if ( $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $this->public)) != $this->public ) { $this->install(); } 
  1457. //do we need to upgrade anything? 
  1458. if ( $this->subscribe2_options === false || is_array($this->subscribe2_options) && $this->subscribe2_options['version'] !== S2VERSION ) { 
  1459. add_action('shutdown', array(&$this, 'upgrade')); 
  1460.  
  1461. // add core actions 
  1462. add_filter('cron_schedules', array(&$this, 'add_weekly_sched')); 
  1463. // add actions for automatic subscription based on option settings 
  1464. add_action('register_form', array(&$this, 'register_form')); 
  1465. add_action('user_register', array(&$this, 'register_post')); 
  1466. if ( $this->s2_mu ) { 
  1467. add_action('add_user_to_blog', array(&$s2class_multisite, 'wpmu_add_user'), 10); 
  1468. add_action('remove_user_from_blog', array(&$s2class_multisite, 'wpmu_remove_user'), 10); 
  1469. // add actions for processing posts based on per-post or cron email settings 
  1470. if ( $this->subscribe2_options['email_freq'] != 'never' ) { 
  1471. add_action('s2_digest_cron', array(&$this, 'subscribe2_cron')); 
  1472. } else { 
  1473. add_action('new_to_publish', array(&$this, 'publish')); 
  1474. add_action('draft_to_publish', array(&$this, 'publish')); 
  1475. add_action('auto-draft_to_publish', array(&$this, 'publish')); 
  1476. add_action('pending_to_publish', array(&$this, 'publish')); 
  1477. add_action('private_to_publish', array(&$this, 'publish')); 
  1478. add_action('future_to_publish', array(&$this, 'publish')); 
  1479. if ( $this->subscribe2_options['private'] == 'yes' ) { 
  1480. add_action('new_to_private', array(&$this, 'publish')); 
  1481. add_action('draft_to_private', array(&$this, 'publish')); 
  1482. add_action('auto-draft_to_private', array(&$this, 'publish')); 
  1483. add_action('pending_to_private', array(&$this, 'publish')); 
  1484. // add actions for comment subscribers 
  1485. if ( 'no' != $this->subscribe2_options['comment_subs'] ) { 
  1486. if ( 'before' == $this->subscribe2_options['comment_subs'] ) { 
  1487. add_action('comment_form_after_fields', array(&$this, 's2_comment_meta_form')); 
  1488. } else { 
  1489. add_action('comment_form', array(&$this, 's2_comment_meta_form')); 
  1490. add_action('comment_post', array(&$this, 's2_comment_meta'), 1, 2); 
  1491. add_action('wp_set_comment_status', array(&$this, 'comment_status')); 
  1492. // add action to display widget if option is enabled 
  1493. if ( '1' == $this->subscribe2_options['widget'] ) { 
  1494. add_action('widgets_init', array(&$this, 'subscribe2_widget')); 
  1495. // add action to display counter widget if option is enabled 
  1496. if ( '1' == $this->subscribe2_options['counterwidget'] ) { 
  1497. add_action('widgets_init', array(&$this, 'counter_widget')); 
  1498.  
  1499. // add action to 'clean' unconfirmed Public Subscribers 
  1500. if ( $this->clean_interval > 0 ) { 
  1501. add_action('wp_scheduled_delete', array(&$this, 's2cleaner_task')); 
  1502.  
  1503. // Add actions specific to admin or frontend 
  1504. if ( is_admin() ) { 
  1505. // load strings 
  1506. add_action('init', array(&$this, 'load_strings')); 
  1507.  
  1508. //add menu, authoring and category admin actions 
  1509. add_action('admin_menu', array(&$this, 'admin_menu')); 
  1510. add_action('admin_menu', array(&$this, 's2_meta_init')); 
  1511. add_action('save_post', array(&$this, 's2_meta_handler')); 
  1512. add_action('create_category', array(&$this, 'new_category')); 
  1513. add_action('delete_category', array(&$this, 'delete_category')); 
  1514.  
  1515. // Add filters for Ozh Admin Menu 
  1516. if ( function_exists('wp_ozh_adminmenu') ) { 
  1517. add_filter('ozh_adminmenu_icon_s2_posts', array(&$this, 'ozh_s2_icon')); 
  1518. add_filter('ozh_adminmenu_icon_s2_users', array(&$this, 'ozh_s2_icon')); 
  1519. add_filter('ozh_adminmenu_icon_s2_tools', array(&$this, 'ozh_s2_icon')); 
  1520. add_filter('ozh_adminmenu_icon_s2_settings', array(&$this, 'ozh_s2_icon')); 
  1521.  
  1522. // add write button 
  1523. if ( '1' == $this->subscribe2_options['show_button'] ) { 
  1524. add_action('admin_init', array(&$this, 'button_init')); 
  1525.  
  1526. // add counterwidget css and js 
  1527. if ( '1' == $this->subscribe2_options['counterwidget'] ) { 
  1528. add_action('admin_init', array(&$this, 'widget_s2counter_css_and_js')); 
  1529.  
  1530. // add one-click handlers 
  1531. if ( 'yes' == $this->subscribe2_options['one_click_profile'] ) { 
  1532. add_action( 'show_user_profile', array(&$this, 'one_click_profile_form') ); 
  1533. add_action( 'edit_user_profile', array(&$this, 'one_click_profile_form') ); 
  1534. add_action( 'personal_options_update', array(&$this, 'one_click_profile_form_save') ); 
  1535. add_action( 'edit_user_profile_update', array(&$this, 'one_click_profile_form_save') ); 
  1536.  
  1537. // capture CSV export 
  1538. if ( isset($_POST['s2_admin']) && isset($_POST['csv']) ) { 
  1539. $date = date('Y-m-d'); 
  1540. header("Content-Description: File Transfer"); 
  1541. header("Content-type: application/octet-stream"); 
  1542. header("Content-Disposition: attachment; filename=subscribe2_users_$date.csv"); 
  1543. header("Pragma: no-cache"); 
  1544. header("Expires: 0"); 
  1545. echo $this->prepare_export($_POST['exportcsv']); 
  1546. exit(0); 
  1547. } else { 
  1548. // load strings later on frontend for polylang plugin compatibility 
  1549. add_action('wp', array(&$this, 'load_strings')); 
  1550.  
  1551. if ( isset($_GET['s2']) ) { 
  1552. // someone is confirming a request 
  1553. if ( defined('DOING_S2_CONFIRM') && DOING_S2_CONFIRM ) { return; } 
  1554. define( 'DOING_S2_CONFIRM', true ); 
  1555. add_filter('request', array(&$this, 'query_filter')); 
  1556. add_filter('the_title', array(&$this, 'title_filter')); 
  1557. add_filter('the_content', array(&$this, 'confirm')); 
  1558.  
  1559. // add the frontend filters 
  1560. add_shortcode('subscribe2', array(&$this, 'shortcode')); 
  1561. add_filter('the_content', array(&$this, 'filter'), 10); 
  1562.  
  1563. // add actions for other plugins 
  1564. if ( '1' == $this->subscribe2_options['show_meta'] ) { 
  1565. add_action('wp_meta', array(&$this, 'add_minimeta'), 0); 
  1566.  
  1567. // add actions for ajax form if enabled 
  1568. if ( '1' == $this->subscribe2_options['ajax'] ) { 
  1569. add_action('wp_enqueue_scripts', array(&$this, 'add_ajax')); 
  1570. add_action('wp_footer', array(&$this, 'add_s2_ajax')); 
  1571. } // end s2init() 
  1572.  
  1573. /** 
  1574. PHP5 Constructor 
  1575. Allows dynamic variable setting 
  1576. */ 
  1577. function __construct() { 
  1578. $this->word_wrap = apply_filters('s2_word_wrap', 80); 
  1579. $this->excerpt_length = apply_filters('s2_excerpt_length', 55); 
  1580. $this->site_switching = apply_filters('s2_allow_site_switching', false); 
  1581. $this->clean_interval = apply_filters('s2_clean_interval', 28); 
  1582. } // end __construct() 
  1583.  
  1584. /** ===== our variables ===== */ 
  1585. // cache variables 
  1586. var $subscribe2_options = array(); 
  1587. var $all_confirmed = ''; 
  1588. var $all_unconfirmed = ''; 
  1589. var $all_registered_id = ''; 
  1590. var $all_registered_email = ''; 
  1591. var $all_authors = ''; 
  1592. var $excluded_cats = ''; 
  1593. var $post_title = ''; 
  1594. var $permalink = ''; 
  1595. var $post_date = ''; 
  1596. var $post_time = ''; 
  1597. var $myname = ''; 
  1598. var $myemail = ''; 
  1599. var $authorname = ''; 
  1600. var $post_cat_names = ''; 
  1601. var $post_tag_names = ''; 
  1602. var $post_count = ''; 
  1603. var $signup_dates = array(); 
  1604. var $filtered = 0; 
  1605. var $preview_email = false; 
  1606.  
  1607. // state variables used to affect processing 
  1608. var $s2_mu = false; 
  1609. var $action = ''; 
  1610. var $email = ''; 
  1611. var $message = ''; 
  1612. var $word_wrap; 
  1613. var $excerpt_length; 
  1614. var $site_switching; 
  1615. var $clean_interval; 
  1616.  
  1617. // some messages 
  1618. var $please_log_in = ''; 
  1619. var $profile = ''; 
  1620. var $confirmation_sent = ''; 
  1621. var $already_subscribed = ''; 
  1622. var $not_subscribed =''; 
  1623. var $not_an_email = ''; 
  1624. var $barred_domain = ''; 
  1625. var $error = ''; 
  1626. var $mail_sent = ''; 
  1627. var $mail_failed = ''; 
  1628. var $form = ''; 
  1629. var $no_such_email = ''; 
  1630. var $added = ''; 
  1631. var $deleted = ''; 
  1632. var $subscribe = ''; 
  1633. var $unsubscribe = ''; 
  1634. var $confirm_subject = ''; 
  1635. var $options_saved = ''; 
  1636. var $options_reset = ''; 
  1637. } // end class subscribe2 
  1638. ?> 
.