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