s2_frontend

The Subscribe2 s2 frontend class.

Defined (1)

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

/classes/class-s2-frontend.php  
  1. class s2_frontend extends s2class { 
  2. /** 
  3. Load all our strings 
  4. */ 
  5. function load_strings() { 
  6. $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>"; 
  7.  
  8. $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>"; 
  9. if ( $this->s2_mu === true ) { 
  10. global $blog_id; 
  11. $user_ID = get_current_user_id(); 
  12. if ( !is_user_member_of_blog($user_ID, $blog_id) ) { 
  13. // if we are on multisite and the user is not a member of this blog change the link 
  14. $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>"; 
  15.  
  16. $this->confirmation_sent = "<p class=\"s2_message\">" . __('A confirmation message is on its way!', 'subscribe2') . "</p>"; 
  17.  
  18. $this->already_subscribed = "<p class=\"s2_error\">" . __('That email address is already subscribed.', 'subscribe2') . "</p>"; 
  19.  
  20. $this->not_subscribed = "<p class=\"s2_error\">" . __('That email address is not subscribed.', 'subscribe2') . "</p>"; 
  21.  
  22. $this->not_an_email = "<p class=\"s2_error\">" . __('Sorry, but that does not look like an email address to me.', 'subscribe2') . "</p>"; 
  23.  
  24. $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>"; 
  25.  
  26. $this->error = "<p class=\"s2_error\">" . __('Sorry, there seems to be an error on the server. Please try again later.', 'subscribe2') . "</p>"; 
  27.  
  28. // confirmation messages 
  29. $this->no_such_email = "<p class=\"s2_error\">" . __('No such email address is registered.', 'subscribe2') . "</p>"; 
  30.  
  31. $this->added = "<p class=\"s2_message\">" . __('You have successfully subscribed!', 'subscribe2') . "</p>"; 
  32.  
  33. $this->deleted = "<p class=\"s2_message\">" . __('You have successfully unsubscribed.', 'subscribe2') . "</p>"; 
  34.  
  35. /**/$this->subscribe = __('subscribe', 'subscribe2'); //ACTION replacement in subscribing confirmation email 
  36.  
  37. /**/$this->unsubscribe = __('unsubscribe', 'subscribe2'); //ACTION replacement in unsubscribing in confirmation email 
  38. } // end load_strings() 
  39.  
  40. /** ===== template and filter functions ===== */ 
  41. /** 
  42. Display our form; also handles (un)subscribe requests 
  43. */ 
  44. function shortcode($atts) { 
  45. extract(shortcode_atts(array( 
  46. 'hide' => '',  
  47. 'id' => '',  
  48. 'nojs' => 'false',  
  49. 'noantispam' => 'false',  
  50. 'link' => '',  
  51. 'size' => 20,  
  52. 'wrap' => 'true' 
  53. ), $atts)); 
  54.  
  55. // if link is true return a link to the page with the ajax class 
  56. if ( $link !== '' && !is_user_logged_in() ) { 
  57. $hide_id = ($hide === '') ? "": " id=\"" . strtolower($hide) . "\""; 
  58. $this->s2form = "<a href=\"" . get_permalink($this->subscribe2_options['s2page']) . "\" class=\"s2popup\"" . $hide_id . ">" . $link . "</a>\r\n"; 
  59. return $this->s2form; 
  60.  
  61. // Apply filters to button text 
  62. $unsubscribe_button_value = apply_filters('s2_unsubscribe_button', __('Unsubscribe', 'subscribe2')); 
  63. $subscribe_button_value = apply_filters('s2_subscribe_button', __('Subscribe', 'subscribe2')); 
  64.  
  65. // if a button is hidden, show only other 
  66. if ( strtolower($hide) == 'subscribe' ) { 
  67. $this->input_form_action = "<input type=\"submit\" name=\"unsubscribe\" value=\"" . esc_attr($unsubscribe_button_value) . "\" />"; 
  68. } elseif ( strtolower($hide) == 'unsubscribe' ) { 
  69. $this->input_form_action = "<input type=\"submit\" name=\"subscribe\" value=\"" . esc_attr($subscribe_button_value) . "\" />"; 
  70. } else { 
  71. // both form input actions 
  72. $this->input_form_action = "<input type=\"submit\" name=\"subscribe\" value=\"" . esc_attr($subscribe_button_value) . "\" /> <input type=\"submit\" name=\"unsubscribe\" value=\"" . esc_attr($unsubscribe_button_value) . "\" />"; 
  73.  
  74. // if ID is provided, get permalink 
  75. $action = ''; 
  76. if ( is_numeric($id) ) { 
  77. $action = " action=\"" . get_permalink( $id ) . "\""; 
  78. } elseif ( $id === 'home' ) { 
  79. $action = " action=\"" . get_site_url() . "\""; 
  80. } elseif ( $id === 'self' ) { 
  81. $action = ''; 
  82. } elseif ( $this->subscribe2_options['s2page'] > 0 ) { 
  83. $action = " action=\"" . get_permalink( $this->subscribe2_options['s2page'] ) . "\""; 
  84.  
  85. // allow remote setting of email in form 
  86. if ( isset($_REQUEST['email']) && is_email($_REQUEST['email']) ) { 
  87. $value = $this->sanitize_email($_REQUEST['email']); 
  88. } elseif ( strtolower($nojs) == 'true' ) { 
  89. $value = ''; 
  90. } else { 
  91. $value = __('Enter email address...', 'subscribe2'); 
  92.  
  93. // if wrap is true add paragraph html tags 
  94. $wrap_text = ''; 
  95. if ( strtolower($wrap) == 'true' ) { 
  96. $wrap_text = '</p><p>'; 
  97.  
  98. // deploy some anti-spam measures 
  99. $antispam_text = ''; 
  100. if ( strtolower($noantispam) != 'true' ) { 
  101. $antispam_text = "<span style=\"display:none !important\">"; 
  102. $antispam_text .= "<label for=\"name\">Leave Blank:</label><input type=\"text\" id=\"name\" name=\"name\" />"; 
  103. $antispam_text .= "<label for=\"uri\">Do Not Change:</label><input type=\"text\" id=\"uri\" name=\"uri\" value=\"http://\" />"; 
  104. $antispam_text .= "</span>"; 
  105.  
  106. // build default form 
  107. if ( strtolower($nojs) == 'true' ) { 
  108. $this->form = "<form method=\"post\"" . $action . "><input type=\"hidden\" name=\"ip\" value=\"" . $_SERVER['REMOTE_ADDR'] . "\" />" . $antispam_text . "<p><label for=\"s2email\">" . __('Your email:', 'subscribe2') . "</label><br /><input type=\"text\" name=\"email\" id=\"s2email\" value=\"" . $value . "\" size=\"" . $size . "\" />" . $wrap_text . $this->input_form_action . "</p></form>"; 
  109. } else { 
  110. $this->form = "<form method=\"post\"" . $action . "><input type=\"hidden\" name=\"ip\" value=\"" . $_SERVER['REMOTE_ADDR'] . "\" />" . $antispam_text . "<p><label for=\"s2email\">" . __('Your email:', 'subscribe2') . "</label><br /><input type=\"text\" name=\"email\" id=\"s2email\" value=\"" . $value . "\" size=\"" . $size . "\" onfocus=\"if (this.value == '" . $value . "') {this.value = '';}\" onblur=\"if (this.value == '') {this.value = '" . $value . "';}\" />" . $wrap_text . $this->input_form_action . "</p></form>\r\n"; 
  111. $this->s2form = apply_filters('s2_form', $this->form); 
  112.  
  113. global $user_ID; 
  114. get_currentuserinfo(); 
  115. if ( $user_ID ) { 
  116. $this->s2form = $this->profile; 
  117. if ( isset($_POST['subscribe']) || isset($_POST['unsubscribe']) ) { 
  118. // anti spam sign up measure 
  119. if ( $_POST['name'] != '' || $_POST['uri'] != 'http://' ) { 
  120. // looks like some invisible-to-user fields were changed; falsely report success 
  121. return $this->confirmation_sent; 
  122. global $wpdb, $user_email; 
  123. $this->email = $this->sanitize_email($_POST['email']); 
  124. if ( !is_email($this->email) ) { 
  125. $this->s2form = $this->form . $this->not_an_email; 
  126. } elseif ( $this->is_barred($this->email) ) { 
  127. $this->s2form = $this->form . $this->barred_domain; 
  128. } else { 
  129. $this->ip = $_POST['ip']; 
  130. if ( is_int($this->lockout) && $this->lockout > 0 ) { 
  131. $date = date('H:i:s.u', $this->lockout); 
  132. $ips = $wpdb->get_col($wpdb->prepare("SELECT ip FROM $this->public WHERE date = CURDATE() AND time > SUBTIME(CURTIME(), %s)", $date)); 
  133. if ( in_array($this->ip, $ips) ) { 
  134. return __('Slow down, you move too fast.', 'subscribe2'); 
  135. // does the supplied email belong to a registered user? 
  136. $check = $wpdb->get_var($wpdb->prepare("SELECT user_email FROM $wpdb->users WHERE user_email = %s", $this->email)); 
  137. if ( '' != $check ) { 
  138. // this is a registered email 
  139. $this->s2form = $this->please_log_in; 
  140. } else { 
  141. // this is not a registered email 
  142. // what should we do? 
  143. if ( isset($_POST['subscribe']) ) { 
  144. // someone is trying to subscribe 
  145. // lets see if they've tried to subscribe previously 
  146. if ( '1' !== $this->is_public($this->email) ) { 
  147. // the user is unknown or inactive 
  148. $this->add($this->email); 
  149. $status = $this->send_confirm('add'); 
  150. // set a variable to denote that we've already run, and shouldn't run again 
  151. $this->filtered = 1; 
  152. if ( $status ) { 
  153. $this->s2form = $this->confirmation_sent; 
  154. } else { 
  155. $this->s2form = $this->error; 
  156. } else { 
  157. // they're already subscribed 
  158. $this->s2form = $this->already_subscribed; 
  159. $this->action = 'subscribe'; 
  160. } elseif ( isset($_POST['unsubscribe']) ) { 
  161. // is this email a subscriber? 
  162. if ( false == $this->is_public($this->email) ) { 
  163. $this->s2form = $this->form . $this->not_subscribed; 
  164. } else { 
  165. $status = $this->send_confirm('del'); 
  166. // set a variable to denote that we've already run, and shouldn't run again 
  167. $this->filtered = 1; 
  168. if ( $status ) { 
  169. $this->s2form = $this->confirmation_sent; 
  170. } else { 
  171. $this->s2form = $this->error; 
  172. $this->action = 'unsubscribe'; 
  173. return $this->s2form; 
  174. } // end shortcode() 
  175.  
  176. /** 
  177. Display form when deprecated <!--subscribe2--> is used 
  178. */ 
  179. function filter($content = '') { 
  180. if ( '' == $content || !strstr($content, '<!--subscribe2-->') ) { return $content; } 
  181.  
  182. return preg_replace('|(<p>)?(\n)*<!--subscribe2-->(\n)*(</p>)?|', do_shortcode( '[subscribe2]' ), $content); 
  183. } // end filter() 
  184.  
  185. /** 
  186. Overrides the default query when handling a (un)subscription confirmation 
  187. This is basically a trick: if the s2 variable is in the query string, just grab the first 
  188. static page and override it's contents later with title_filter() 
  189. */ 
  190. function query_filter() { 
  191. // don't interfere if we've already done our thing 
  192. if ( 1 == $this->filtered ) { return; } 
  193.  
  194. global $wpdb; 
  195.  
  196. // brute force Simple Facebook Connect to bypass compatiblity issues 
  197. $priority = has_filter('wp_head', 'sfc_base_meta'); 
  198. if ( $priority !== false ) { 
  199. remove_action('wp_head', 'sfc_base_meta', $priority); 
  200.  
  201. if ( 0 != $this->subscribe2_options['s2page'] ) { 
  202. return array('page_id' => $this->subscribe2_options['s2page']); 
  203. } else { 
  204. $id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status='publish' LIMIT 1"); 
  205. if ( $id ) { 
  206. return array('page_id' => $id); 
  207. } else { 
  208. return array('showposts' => 1); 
  209. } // end query_filter() 
  210.  
  211. /** 
  212. Overrides the page title 
  213. */ 
  214. function title_filter($title) { 
  215. // don't interfere if we've already done our thing 
  216. if ( in_the_loop() ) { 
  217. $code = $_GET['s2']; 
  218. $action = intval(substr($code, 0, 1)); 
  219. if ( $action == '1' ) { 
  220. return __('Subscription Confirmation', 'subscribe2'); 
  221. } else { 
  222. return __('Unsubscription Confirmation', 'subscribe2'); 
  223. } else { 
  224. return $title; 
  225. } // end title_filter() 
  226.  
  227. /** 
  228. Confirm request from the link emailed to the user and email the admin 
  229. */ 
  230. function confirm($content = '') { 
  231. global $wpdb; 
  232.  
  233. if ( 1 == $this->filtered ) { return $content; } 
  234.  
  235. $code = $_GET['s2']; 
  236. $action = intval(substr($code, 0, 1)); 
  237. $hash = substr($code, 1, 32); 
  238. $id = intval(substr($code, 33)); 
  239. if ( $id ) { 
  240. $this->email = $this->sanitize_email($this->get_email($id)); 
  241. if ( !$this->email || $hash !== wp_hash($this->email) ) { 
  242. return $this->no_such_email; 
  243. } else { 
  244. return $this->no_such_email; 
  245.  
  246. // get current status of email so messages are only sent once per emailed link 
  247. $current = $this->is_public($this->email); 
  248.  
  249. if ( '1' == $action ) { 
  250. // make this subscription active 
  251. $this->message = apply_filters('s2_subscribe_confirmed', $this->added); 
  252. if ( '1' != $current ) { 
  253. $this->ip = $_SERVER['REMOTE_ADDR']; 
  254. $this->toggle($this->email); 
  255. if ( $this->subscribe2_options['admin_email'] == 'subs' || $this->subscribe2_options['admin_email'] == 'both' ) { 
  256. ( '' == get_option('blogname') ) ? $subject = "" : $subject = "[" . stripslashes(html_entity_decode(get_option('blogname'), ENT_QUOTES)) . "] "; 
  257. $subject .= __('New Subscription', 'subscribe2'); 
  258. $subject = html_entity_decode($subject, ENT_QUOTES); 
  259. $message = $this->email . " " . __('subscribed to email notifications!', 'subscribe2'); 
  260. $role = array('fields' => array('user_email'), 'role' => 'administrator'); 
  261. $wp_user_query = get_users( $role ); 
  262. foreach ($wp_user_query as $user) { 
  263. $recipients[] = $user->user_email; 
  264. $recipients = apply_filters('s2_admin_email', $recipients, 'subscribe'); 
  265. $headers = $this->headers(); 
  266. // send individual emails so we don't reveal admin emails to each other 
  267. foreach ( $recipients as $recipient ) { 
  268. @wp_mail($recipient, $subject, $message, $headers); 
  269. $this->filtered = 1; 
  270. } elseif ( '0' == $action ) { 
  271. // remove this subscriber 
  272. $this->message = apply_filters('s2_unsubscribe_confirmed', $this->deleted); 
  273. if ( '0' != $current ) { 
  274. $this->delete($this->email); 
  275. if ( $this->subscribe2_options['admin_email'] == 'unsubs' || $this->subscribe2_options['admin_email'] == 'both' ) { 
  276. ( '' == get_option('blogname') ) ? $subject = "" : $subject = "[" . stripslashes(html_entity_decode(get_option('blogname'), ENT_QUOTES)) . "] "; 
  277. $subject .= __('New Unsubscription', 'subscribe2'); 
  278. $subject = html_entity_decode($subject, ENT_QUOTES); 
  279. $message = $this->email . " " . __('unsubscribed from email notifications!', 'subscribe2'); 
  280. $role = array('fields' => array('user_email'), 'role' => 'administrator'); 
  281. $wp_user_query = get_users( $role ); 
  282. foreach ($wp_user_query as $user) { 
  283. $recipients[] = $user->user_email; 
  284. $recipients = apply_filters('s2_admin_email', $recipients, 'unsubscribe'); 
  285. $headers = $this->headers(); 
  286. // send individual emails so we don't reveal admin emails to each other 
  287. foreach ( $recipients as $recipient ) { 
  288. @wp_mail($recipient, $subject, $message, $headers); 
  289. $this->filtered = 1; 
  290.  
  291. if ( '' != $this->message ) { 
  292. return $this->message; 
  293. } // end confirm() 
  294.  
  295. /** 
  296. Add hook for Minimeta Widget plugin 
  297. */ 
  298. function add_minimeta() { 
  299. if ( $this->subscribe2_options['s2page'] != 0 ) { 
  300. echo "<li><a href=\"" . get_permalink($this->subscribe2_options['s2page']) . "\">" . __('[Un]Subscribe to Posts', 'subscribe2') . "</a></li>\r\n"; 
  301. } // end add_minimeta() 
  302.  
  303. /** 
  304. Add jQuery code and CSS to front pages for ajax form 
  305. */ 
  306. function add_ajax() { 
  307. // enqueue the jQuery script we need and let WordPress handle the dependencies 
  308. wp_enqueue_script('jquery-ui-dialog'); 
  309. $css = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-darkness/jquery-ui.css'; 
  310. if ( is_ssl() ) { 
  311. $css = str_replace('http:', 'https:', $css); 
  312. wp_register_style('jquery-ui-style', apply_filters('s2_jqueryui_css', $css)); 
  313. wp_enqueue_style('jquery-ui-style'); 
  314. } // end add_ajax() 
  315.  
  316. /** 
  317. Write Subscribe2 form js code dynamically so we can pull WordPress functions 
  318. */ 
  319. function add_s2_ajax() { 
  320. echo "<script type=\"text/javascript\">\r\n"; 
  321. echo "//<![CDATA[\r\n"; 
  322. echo "var s2jQuery = jQuery.noConflict();\r\n"; 
  323. echo "s2jQuery(document).ready(function() {\r\n"; 
  324. echo " var dialog = s2jQuery('<div></div>');\r\n"; 
  325. echo " if (s2jQuery('a.s2popup').attr('id') === 'unsubscribe') {\r\n"; 
  326. echo " dialog.html('" . do_shortcode('[subscribe2 nojs="true" hide="unsubscribe"]') . "');\r\n"; 
  327. echo " } else if (s2jQuery('a.s2popup').attr('id') === 'subscribe') {\r\n"; 
  328. echo " dialog.html('" . do_shortcode('[subscribe2 nojs="true" hide="subscribe"]') . "');\r\n"; 
  329. echo " } else {\r\n"; 
  330. echo " dialog.html('" . do_shortcode('[subscribe2 nojs="true"]') . "');\r\n"; 
  331. echo " }\r\n"; 
  332. if ( $this->s2form != $this->form && !is_user_logged_in() ) { 
  333. echo " dialog.dialog({modal: true, zIndex: 10000, title: '" . __('Subscribe to this blog', 'subscribe2') . "'});\r\n"; 
  334. } else { 
  335. echo " dialog.dialog({autoOpen: false, modal: true, zIndex: 10000, title: '" . __('Subscribe to this blog', 'subscribe2') . "'});\r\n"; 
  336. echo " s2jQuery('a.s2popup').click(function() {\r\n"; 
  337. echo " dialog.dialog('open');\r\n"; 
  338. echo " return false;\r\n"; 
  339. echo " });\r\n"; 
  340. echo "});\r\n"; 
  341. echo "//]]>\r\n"; 
  342. echo "</script>\r\n"; 
  343. } // end add_s2_ajax() 
  344.  
  345. /** 
  346. Check email is not from a barred domain 
  347. */ 
  348. function is_barred($email = '') { 
  349. if ( '' == $email ) { return false; } 
  350.  
  351. $bar_check = false; 
  352. list($user, $domain) = explode('@', $email, 2); 
  353. foreach ( preg_split("|[\s, ]+|", $this->subscribe2_options['barred']) as $barred_domain ) { 
  354. if ( strtolower($domain) === strtolower(trim($barred_domain)) ) { 
  355. $bar_check = true; 
  356. return $bar_check; 
  357. } // end is_barred()