s2class

The Subscribe2 s2class class.

Defined (1)

The class is defined in the following location(s).

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