/classes/class-s2-core.php

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