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