s2_admin

The Subscribe2 s2 admin class.

Defined (1)

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

/classes/class-s2-admin.php  
  1. class s2_admin extends s2class { 
  2. /** ===== WordPress menu registration and scripts ===== */ 
  3. /** 
  4. Hook the menu 
  5. */ 
  6. function admin_menu() { 
  7. add_menu_page(__('Subscribe2', 'subscribe2'), __('Subscribe2', 'subscribe2'), apply_filters('s2_capability', "read", 'user'), 's2', NULL, S2URL . 'include/email_edit.png'); 
  8.  
  9. $s2user = add_submenu_page('s2', __('Your Subscriptions', 'subscribe2'), __('Your Subscriptions', 'subscribe2'), apply_filters('s2_capability', "read", 'user'), 's2', array(&$this, 'user_menu'), S2URL . 'include/email_edit.png'); 
  10. add_action("admin_print_scripts-$s2user", array(&$this, 'checkbox_form_js')); 
  11. add_action("admin_print_styles-$s2user", array(&$this, 'user_admin_css')); 
  12. add_action('load-' . $s2user, array(&$this, 'user_help')); 
  13.  
  14. $s2subscribers = add_submenu_page('s2', __('Subscribers', 'subscribe2'), __('Subscribers', 'subscribe2'), apply_filters('s2_capability', "manage_options", 'manage'), 's2_tools', array(&$this, 'subscribers_menu')); 
  15. add_action("admin_print_scripts-$s2subscribers", array(&$this, 'checkbox_form_js')); 
  16. add_action('load-' . $s2subscribers, array(&$this, 'subscribers_help')); 
  17.  
  18. $s2settings = add_submenu_page('s2', __('Settings', 'subscribe2'), __('Settings', 'subscribe2'), apply_filters('s2_capability', "manage_options", 'settings'), 's2_settings', array(&$this, 'settings_menu')); 
  19. add_action("admin_print_scripts-$s2settings", array(&$this, 'checkbox_form_js')); 
  20. add_action("admin_print_scripts-$s2settings", array(&$this, 'option_form_js')); 
  21. add_filter('plugin_row_meta', array(&$this, 'plugin_links'), 10, 2); 
  22. add_action('load-' . $s2settings, array(&$this, 'settings_help')); 
  23.  
  24. $s2mail = add_submenu_page('s2', __('Send Email', 'subscribe2'), __('Send Email', 'subscribe2'), apply_filters('s2_capability', "publish_posts", 'send'), 's2_posts', array(&$this, 'write_menu')); 
  25. add_action('load-' . $s2mail, array(&$this, 'mail_help')); 
  26.  
  27. $s2nonce = wp_hash('subscribe2'); 
  28. } // end admin_menu() 
  29.  
  30. /** 
  31. Contextual Help 
  32. */ 
  33. function user_help() { 
  34. $screen = get_current_screen(); 
  35. if ( $this->subscribe2_options['email_freq'] != 'never' ) { 
  36. $screen->add_help_tab(array( 
  37. 'id' => 's2-user-help1',  
  38. 'title' => __('Overview', 'subscribe2'),  
  39. 'content' => '<p>' . __('From this page you can opt in or out of receiving a periodical digest style email of blog posts.', 'subscribe2') . '</p>' 
  40. )); 
  41. } else { 
  42. $screen->add_help_tab(array( 
  43. 'id' => 's2-user-help1',  
  44. 'title' => __('Overview', 'subscribe2'),  
  45. 'content' => '<p>' . __('From this page you can control your subscription preferences. Choose the email format you wish to receive, which categories you would like to receive notification for and depending on the site settings which authors you would like to read.', 'subscribe2') . '</p>' 
  46. )); 
  47. } // end user_help() 
  48.  
  49. function subscribers_help() { 
  50. $screen = get_current_screen(); 
  51. $screen->add_help_tab(array( 
  52. 'id' => 's2-subscribers-help1',  
  53. 'title' => __('Overview', 'subscribe2'),  
  54. 'content' => '<p>' . __('From this page you can manage your subscribers.', 'subscribe2') . '</p>' 
  55. )); 
  56. $screen->add_help_tab(array( 
  57. 'id' => 's2-subscribers-help2',  
  58. 'title' => __('Public Subscribers', 'subscribe2'),  
  59. 'content' => '<p>' . __('Public Subscribers are subscribers who have used the plugin form and only provided their email address.', 'subscribe2') . '</p><p>'. __('On this page public subscribers can be viewed, searched, deleted and also toggled between Confirmed and Unconfirmed status.', 'subscribe2') . '</p>' 
  60. )); 
  61. $screen->add_help_tab(array( 
  62. 'id' => 's2-subscribers-help3',  
  63. 'title' => __('Registered Subscribers', 'subscribe2'),  
  64. 'content' => '<p>' . __('Registered Subscribers are subscribers who have registered in WordPress and have a username and password.', 'subscribe2') . 
  65. '</p><p>'. __('Registered Subscribers have greater personal control over their subscription. They can change the format of the email and also select which categories and authors they want to receive notifications about.', 'subscribe2') . 
  66. '</p><p>'. __('On this page registered subscribers can be viewed and searched. User accounts can be deleted from here with any posts created by those users being assigned to the currently logged in user. Bulk changes can be applied to all user settings changing their subscription email format and categories.', 'subscribe2') . '</p>' 
  67. )); 
  68. } // end subscribers_help() 
  69.  
  70. function settings_help() { 
  71. $screen = get_current_screen(); 
  72. $screen->add_help_tab(array( 
  73. 'id' => 's2-settings-help1',  
  74. 'title' => __('Overview', 'subscribe2'),  
  75. 'content' => '<p>' . __('From this page you can adjust the Settings for Subscribe2.', 'subscribe2') . '</p>' 
  76. )); 
  77. $screen->add_help_tab(array( 
  78. 'id' => 's2-settings-help2',  
  79. 'title' => __('Email Settings', 'subscribe2'),  
  80. 'content' => '<p>' . __('This section allows you to specify settings that apply to the emails generated by the site.', 'subscribe2') . 
  81. '</p><p>'. __('Emails can be sent to individual subscribers by setting the number of recipients per email to 1. A setting greater than one will group recipients together and make use of the BCC emails header. A setting of 0 sends a single email with all subscribers in one large BCC group. A setting of 1 looks less like spam email to filters but takes longer to process.', 'subscribe2'). 
  82. '</p><p>'. __('This section is also where the sender of the email on this page is chosen. You can choose Post Author or your Blogname but it is recommended to create a user account with an email address that really exists and shares the same domain name as your site (the bit after the @ should be the same as your sites web address) and then use this account.', 'subscribe2') . 
  83. '</p><p>'. __('This page also configures the frequency of emails. This can be at the time new posts are made (per post) or periodically with an excerpt of each post made (digest). Additionally the post types (pages, private, password protected) can also be configured here.', 'subscribe2') . '</p>' 
  84. )); 
  85. $screen->add_help_tab(array( 
  86. 'id' => 's2-settings-help3',  
  87. 'title' => __('Templates', 'subscribe2'),  
  88. 'content' => '<p>' . __('This section allows you to customise the content of your notification emails.', 'subscribe2') . 
  89. '</p><p>'. __('There are special {KEYWORDS} that are used by Subscribe2 to place content into the final email. The template also accepts regular text and HTML as desired in the final emails.', 'subscribe2') . 
  90. '</p><p>'. __('The {KEYWORDS} are listed on the right of the templates, note that some are for per post emails only and some are for digest emails only. Make sure the correct keywords are used based upon the Email Settings.', 'subscribe2') . '</p>' 
  91. )); 
  92. $screen->add_help_tab(array( 
  93. 'id' => 's2-settings-help4',  
  94. 'title' => __('Registered Users', 'subscribe2'),  
  95. 'content' => '<p>' . __('This section allows settings that apply to Registered Subscribers to be configured.', 'subscribe2') . 
  96. '</p><p>'. __('Categories can be made compulsory so emails are always sent for posts in these categories. They can also be excludes so that emails are not generated. Excluded categories take precedence over Compulsory categories.', 'subscribe2') . 
  97. '</p><p>'. __('A set of default settings for new users can also be specified using the Auto Subscribe section. Settings specified here will be applied to any newly created user accounts while Subscribe2 is activated.', 'subscribe2') . '</p>' 
  98. )); 
  99. $screen->add_help_tab(array( 
  100. 'id' => 's2-settings-help5',  
  101. 'title' => __('Appearance', 'subscribe2'),  
  102. 'content' => '<p>' . __('This section allows you to enable several aspect of the plugin such as Widgets and editor buttons.', 'subscribe2') . 
  103. '</p><p>'. __('AJAX mode can be enabled that is intended to work with the shortcode link parameter so that a dialog opens in the centre of the browser rather then using the regular form.', 'subscribe2') . 
  104. '</p><p>'. __('The email over ride check box can be set to be automatically checked for every new post and page from here to, this may be useful if you will only want to send very occasional notifications for specific posts. You can then uncheck this box just before you publish your content.', 'subscribe2') . '</p>' 
  105. )); 
  106. $screen->add_help_tab(array( 
  107. 'id' => 's2-settings-help6',  
  108. 'title' => __('Miscellaneous', 'subscribe2'),  
  109. 'content' => '<p>' . __('This section contains a place to bar specified domains from becoming Public Subscribers and links to help and support pages.', 'subscribe2') . 
  110. '</p><p>'. __('In the paid Subscribe2 HTML version there is also a place here to enter a license code so that updates can be accessed automatically.', 'subscribe2') . 
  111. '</p>' 
  112. )); 
  113. } // end settings_help() 
  114.  
  115. function mail_help() { 
  116. $screen = get_current_screen(); 
  117. $screen->add_help_tab(array( 
  118. 'id' => 's2-send-mail-help1',  
  119. 'title' => __('Overview', 'subscribe2'),  
  120. 'content' => '<p>' . __('From this page you can send emails to the recipients in the group selected in the drop down.', 'subscribe2') . 
  121. '</p><p>' . __('<strong>Preview</strong> will send a preview of the email to the currently logged in user. <strong>Send</strong> will send the email to the recipient list.', 'subscribe2') . '</p>' 
  122. )); 
  123. } // end send_email_help() 
  124.  
  125. /** 
  126. Hook for Admin Drop Down Icons 
  127. */ 
  128. function ozh_s2_icon() { 
  129. return S2URL . 'include/email_edit.png'; 
  130. } // end ozh_s2_icon() 
  131.  
  132. /** 
  133. Insert Javascript and CSS into admin_header 
  134. */ 
  135. function checkbox_form_js() { 
  136. wp_register_script('s2_checkbox', S2URL . 'include/s2_checkbox' . $this->script_debug . '.js', array('jquery'), '1.3'); 
  137. wp_enqueue_script('s2_checkbox'); 
  138. } //end checkbox_form_js() 
  139.  
  140. function user_admin_css() { 
  141. wp_register_style('s2_user_admin', S2URL . 'include/s2_user_admin.css', array(), '1.0'); 
  142. wp_enqueue_style('s2_user_admin'); 
  143. } // end user_admin_css() 
  144.  
  145. function option_form_js() { 
  146. wp_register_script('s2_edit', S2URL . 'include/s2_edit' . $this->script_debug . '.js', array('jquery'), '1.1'); 
  147. wp_enqueue_script('s2_edit'); 
  148. wp_enqueue_script('jquery-ui-datepicker'); 
  149. wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/smoothness/jquery-ui.css'); 
  150. wp_register_script('s2_date_time', S2URL . 'include/s2_date_time' . $this->script_debug . '.js', array('jquery-ui-datepicker'), '1.0'); 
  151. wp_enqueue_script('s2_date_time'); 
  152. } // end option_form_js() 
  153.  
  154. /** 
  155. Adds a links directly to the settings page from the plugin page 
  156. */ 
  157. function plugin_links($links, $file) { 
  158. if ( $file == S2DIR.'subscribe2.php' ) { 
  159. $links[] = "<a href='admin.php?page=s2_settings'>" . __('Settings', 'subscribe2') . "</a>"; 
  160. $links[] = "<a href='http://plugins.trac.wordpress.org/browser/subscribe2/i18n/'>" . __('Translation Files', 'subscribe2') . "</a>"; 
  161. $links[] = "<a href='https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=2387904'><b>" . __('Donate', 'subscribe2') . "</b></a>"; 
  162. return $links; 
  163. } // end plugin_links() 
  164.  
  165. /** ===== Menus ===== */ 
  166. /** 
  167. Our subscriber management page 
  168. */ 
  169. function subscribers_menu() { 
  170. require_once(S2PATH . 'admin/subscribers.php'); 
  171. } // end subscribers_menu() 
  172.  
  173. /** 
  174. Our settings page 
  175. */ 
  176. function settings_menu() { 
  177. require_once(S2PATH . 'admin/settings.php'); 
  178. } // end settings_menu() 
  179.  
  180. /** 
  181. Our profile menu 
  182. */ 
  183. function user_menu() { 
  184. require_once(S2PATH . 'admin/your_subscriptions.php'); 
  185. } // end user_menu() 
  186.  
  187. /** 
  188. Display the Write sub-menu 
  189. */ 
  190. function write_menu() { 
  191. require_once(S2PATH . 'admin/send_email.php'); 
  192. } // end write_menu() 
  193.  
  194. /** ===== Write Toolbar Button Functions ===== */ 
  195. /** 
  196. Register our button in the QuickTags bar 
  197. */ 
  198. function button_init() { 
  199. global $pagenow; 
  200. if ( !in_array($pagenow, array('post-new.php', 'post.php', 'page-new.php', 'page.php')) ) { return; } 
  201. if ( !current_user_can('edit_posts') && !current_user_can('edit_pages') ) { return; } 
  202. if ( 'true' == get_user_option('rich_editing') ) { 
  203. // Hook into the rich text editor 
  204. add_filter('mce_external_plugins', array(&$this, 'mce_plugin')); 
  205. add_filter('mce_buttons', array(&$this, 'mce_button')); 
  206. } else { 
  207. wp_enqueue_script('subscribe2_button', S2URL . 'include/s2_button' . $this->script_debug . '.js', array('quicktags'), '2.0' ); 
  208. } // end button_init() 
  209.  
  210. /** 
  211. Add buttons for Rich Text Editor 
  212. */ 
  213. function mce_plugin($arr) { 
  214. if ( version_compare($this->wp_release, '3.9', '<') ) { 
  215. $path = S2URL . 'tinymce/editor_plugin3' . $this->script_debug . '.js'; 
  216. } else { 
  217. $path = S2URL . 'tinymce/editor_plugin4' . $this->script_debug . '.js'; 
  218. $arr['subscribe2'] = $path; 
  219. return $arr; 
  220. } // end mce_plugin() 
  221.  
  222. function mce_button($arr) { 
  223. $arr[] = 'subscribe2'; 
  224. return $arr; 
  225. } // end mce_button() 
  226.  
  227. /** ===== widget functions ===== */ 
  228. /** 
  229. Function to add css and js files to admin header 
  230. */ 
  231. function widget_s2counter_css_and_js() { 
  232. // ensure we only add colorpicker js to widgets page 
  233. if ( stripos($_SERVER['REQUEST_URI'], 'widgets.php' ) !== false ) { 
  234. wp_enqueue_style('farbtastic'); 
  235. wp_enqueue_script('farbtastic'); 
  236. wp_register_script('s2_colorpicker', S2URL . 'include/s2_colorpicker' . $this->script_debug . '.js', array('farbtastic'), '1.2'); 
  237. wp_enqueue_script('s2_colorpicker'); 
  238. } // end widget_s2_counter_css_and_js() 
  239.  
  240. /** ===== meta box functions to allow per-post override ===== */ 
  241. /** 
  242. Create meta box on write pages 
  243. */ 
  244. function s2_meta_init() { 
  245. add_meta_box('subscribe2', __('Subscribe2 Notification Override', 'subscribe2' ), array(&$this, 's2_meta_box'), 'post', 'advanced'); 
  246. add_meta_box('subscribe2', __('Subscribe2 Notification Override', 'subscribe2' ), array(&$this, 's2_meta_box'), 'page', 'advanced'); 
  247. } // end s2_meta_init() 
  248.  
  249. /** 
  250. Meta box code 
  251. */ 
  252. function s2_meta_box() { 
  253. global $post_ID; 
  254. $s2mail = get_post_meta($post_ID, '_s2mail', true); 
  255. echo "<input type=\"hidden\" name=\"s2meta_nonce\" id=\"s2meta_nonce\" value=\"" . wp_create_nonce(wp_hash(plugin_basename(__FILE__))) . "\" />"; 
  256. echo __("Check here to disable sending of an email notification for this post/page", 'subscribe2'); 
  257. echo "  <input type=\"checkbox\" name=\"s2_meta_field\" value=\"no\""; 
  258. if ( $s2mail == 'no' || ($this->subscribe2_options['s2meta_default'] == "1" && $s2mail == "") ) { 
  259. echo " checked=\"checked\""; 
  260. echo " />"; 
  261. } // end s2_meta_box() 
  262.  
  263. /** 
  264. Meta box form handler 
  265. */ 
  266. function s2_meta_handler($post_id) { 
  267. if ( !isset($_POST['s2meta_nonce']) || !wp_verify_nonce($_POST['s2meta_nonce'], wp_hash(plugin_basename(__FILE__))) ) { return $post_id; } 
  268.  
  269. if ( 'page' == $_POST['post_type'] ) { 
  270. if ( !current_user_can('edit_page', $post_id) ) { return $post_id; } 
  271. } else { 
  272. if ( !current_user_can('edit_post', $post_id) ) { return $post_id; } 
  273.  
  274. if ( isset($_POST['s2_meta_field']) && $_POST['s2_meta_field'] == 'no' ) { 
  275. update_post_meta($post_id, '_s2mail', $_POST['s2_meta_field']); 
  276. } else { 
  277. update_post_meta($post_id, '_s2mail', 'yes'); 
  278. } // end s2_meta_box_handler() 
  279.  
  280. /** ===== WordPress menu helper functions ===== */ 
  281. /** 
  282. Collects the signup date for all public subscribers 
  283. */ 
  284. function signup_date($email = '') { 
  285. if ( '' == $email ) { return false; } 
  286.  
  287. global $wpdb; 
  288. if ( !empty($this->signup_dates) ) { 
  289. return $this->signup_dates[$email]; 
  290. } else { 
  291. $results = $wpdb->get_results("SELECT email, date FROM $this->public", ARRAY_N); 
  292. foreach ( $results as $result ) { 
  293. $this->signup_dates[$result[0]] = $result[1]; 
  294. return $this->signup_dates[$email]; 
  295. } // end signup_date() 
  296.  
  297. /** 
  298. Collects the ip address for all public subscribers 
  299. */ 
  300. function signup_ip($email = '') { 
  301. if ( '' == $email ) {return false; } 
  302.  
  303. global $wpdb; 
  304. if ( !empty($this->signup_ips) ) { 
  305. return $this->signup_ips[$email]; 
  306. } else { 
  307. $results = $wpdb->get_results("SELECT email, ip FROM $this->public", ARRAY_N); 
  308. foreach ( $results as $result ) { 
  309. $this->signup_ips[$result[0]] = $result[1]; 
  310. return $this->signup_ips[$email]; 
  311. } // end signup_ip() 
  312.  
  313. /** 
  314. Export subscriber emails and other details to CSV 
  315. */ 
  316. function prepare_export( $subscribers ) { 
  317. if ( empty($subscribers) ) { return; } 
  318. $subscribers = explode(", \r\n", $subscribers); 
  319. natcasesort($subscribers); 
  320.  
  321. $exportcsv = _x('User Email, User Type, User Name, Confirm Date, IP', 'Comma Separated Column Header names for CSV Export' , 'subscribe2'); 
  322. $all_cats = $this->all_cats(false, 'ID'); 
  323.  
  324. foreach ($all_cats as $cat) { 
  325. $exportcsv .= ", " . html_entity_decode($cat->cat_name, ENT_QUOTES); 
  326. $cat_ids[] = $cat->term_id; 
  327. $exportcsv .= "\r\n"; 
  328.  
  329. if ( !function_exists('get_userdata') ) { 
  330. require_once(ABSPATH . WPINC . '/pluggable.php'); 
  331.  
  332. foreach ( $subscribers as $subscriber ) { 
  333. if ( $this->is_registered($subscriber) ) { 
  334. $user_ID = $this->get_user_id( $subscriber ); 
  335. $user_info = get_userdata( $user_ID ); 
  336.  
  337. $cats = explode(', ', get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true)); 
  338. $subscribed_cats = ''; 
  339. foreach ( $cat_ids as $cat ) { 
  340. (in_array($cat, $cats)) ? $subscribed_cats .= ", Yes" : $subscribed_cats .= ", No"; 
  341.  
  342. $exportcsv .= $subscriber . ', '; 
  343. $exportcsv .= __('Registered User', 'subscribe2'); 
  344. $exportcsv .= ', ' . $user_info->display_name; 
  345. $exportcsv .= ', , ' . $subscribed_cats . "\r\n"; 
  346. } else { 
  347. if ( $this->is_public($subscriber) === '1' ) { 
  348. $exportcsv .= $subscriber . ', ' . __('Confirmed Public Subscriber', 'subscribe2') . ', , ' . $this->signup_date($subscriber) . ', ' . $this->signup_ip($subscriber) . "\r\n"; 
  349. } elseif ( $this->is_public($subscriber) === '0' ) { 
  350. $exportcsv .= $subscriber . ', ' . __('Unconfirmed Public Subscriber', 'subscribe2') . ', , ' . $this->signup_date($subscriber) . ', ' . $this->signup_ip($subscriber) . "\r\n"; 
  351.  
  352. return $exportcsv; 
  353. } // end prepare_export() 
  354.  
  355. /** 
  356. Display a table of categories with checkboxes 
  357. Optionally pre-select those categories specified 
  358. */ 
  359. function display_category_form($selected = array(), $override = 1, $compulsory = array(), $name = 'category') { 
  360. global $wpdb; 
  361.  
  362. if ( $override == 0 ) { 
  363. $all_cats = $this->all_cats(true); 
  364. } else { 
  365. $all_cats = $this->all_cats(false); 
  366.  
  367. $half = (count($all_cats) / 2); 
  368. $i = 0; 
  369. $j = 0; 
  370. echo "<table style=\"width: 100%; border-collapse: separate; border-spacing: 2px; *border-collapse: expression('separate', cellSpacing = '2px');\" class=\"editform\">\r\n"; 
  371. echo "<tr><td style=\"text-align: left;\" colspan=\"2\">\r\n"; 
  372. echo "<label><input type=\"checkbox\" name=\"checkall\" value=\"checkall_" . $name . "\" /> " . __('Select / Unselect All', 'subscribe2') . "</label>\r\n"; 
  373. echo "</td></tr>\r\n"; 
  374. echo "<tr style=\"vertical-align: top;\"><td style=\"width: 50%; text-align: left;\">\r\n"; 
  375. foreach ( $all_cats as $cat ) { 
  376. if ( $i >= $half && 0 == $j ) { 
  377. echo "</td><td style=\"width: 50%; text-align: left;\">\r\n"; 
  378. $j++; 
  379. $catName = ''; 
  380. $parents = array_reverse( get_ancestors($cat->term_id, $cat->taxonomy) ); 
  381. if ( $parents ) { 
  382. foreach ( $parents as $parent ) { 
  383. $parent = get_term($parent, $cat->taxonomy); 
  384. $catName .= $parent->name . ' » '; 
  385. $catName .= $cat->name; 
  386.  
  387. if ( 0 == $j ) { 
  388. echo "<label><input class=\"checkall_" . $name . "\" type=\"checkbox\" name=\"" . $name . "[]\" value=\"" . $cat->term_id . "\""; 
  389. if ( in_array($cat->term_id, $selected) || in_array($cat->term_id, $compulsory) ) { 
  390. echo " checked=\"checked\""; 
  391. if ( in_array($cat->term_id, $compulsory) && $name === 'category' ) { 
  392. echo " DISABLED"; 
  393. echo " /> <abbr title=\"" . $cat->slug . "\">" . $catName . "</abbr></label><br />\r\n"; 
  394. } else { 
  395. echo "<label><input class=\"checkall_" . $name . "\" type=\"checkbox\" name=\"" . $name . "[]\" value=\"" . $cat->term_id . "\""; 
  396. if ( in_array($cat->term_id, $selected) || in_array($cat->term_id, $compulsory) ) { 
  397. echo " checked=\"checked\""; 
  398. if ( in_array($cat->term_id, $compulsory) && $name === 'category' ) { 
  399. echo " DISABLED"; 
  400. echo " /> <abbr title=\"" . $cat->slug . "\">" . $catName . "</abbr></label><br />\r\n"; 
  401. $i++; 
  402. if ( !empty($compulsory) ) { 
  403. foreach ($compulsory as $cat) { 
  404. echo "<input type=\"hidden\" name=\"" . $name . "[]\" value=\"" . $cat . "\">\r\n"; 
  405. echo "</td></tr>\r\n"; 
  406. echo "</table>\r\n"; 
  407. } // end display_category_form() 
  408.  
  409. /** 
  410. Display a table of post formats supported by the currently active theme 
  411. */ 
  412. function display_format_form($formats, $selected = array()) { 
  413. $half = (count($formats[0]) / 2); 
  414. $i = 0; 
  415. $j = 0; 
  416. echo "<table style=\"width: 100%; border-collapse: separate; border-spacing: 2px; *border-collapse: expression('separate', cellSpacing = '2px');\" class=\"editform\">\r\n"; 
  417. echo "<tr><td style=\"text-align: left;\" colspan=\"2\">\r\n"; 
  418. echo "<label><input type=\"checkbox\" name=\"checkall\" value=\"checkall_format\" /> " . __('Select / Unselect All', 'subscribe2') . "</label>\r\n"; 
  419. echo "</td></tr>\r\n"; 
  420. echo "<tr style=\"vertical-align: top;\"><td style=\"width: 50%; text-align: left\">\r\n"; 
  421. foreach ( $formats[0] as $format ) { 
  422. if ( $i >= $half && 0 == $j ) { 
  423. echo "</td><td style=\"width: 50%; text-align: left\">\r\n"; 
  424. $j++; 
  425.  
  426. if ( 0 == $j ) { 
  427. echo "<label><input class=\"checkall_format\" type=\"checkbox\" name=\"format[]\" value=\"" . $format . "\""; 
  428. if ( in_array($format, $selected) ) { 
  429. echo " checked=\"checked\""; 
  430. echo " /> " . ucwords($format) . "</label><br />\r\n"; 
  431. } else { 
  432. echo "<label><input class=\"checkall_format\" type=\"checkbox\" name=\"format[]\" value=\"" . $format . "\""; 
  433. if ( in_array($format, $selected) ) { 
  434. echo " checked=\"checked\""; 
  435. echo " /> " . ucwords($format) . "</label><br />\r\n"; 
  436. $i++; 
  437. echo "</td></tr>\r\n"; 
  438. echo "</table>\r\n"; 
  439. } // end display_format_form() 
  440.  
  441. /** 
  442. Display a table of authors with checkboxes 
  443. Optionally pre-select those authors specified 
  444. */ 
  445. function display_author_form($selected = array()) { 
  446. $all_authors = $this->get_authors(); 
  447.  
  448. $half = (count($all_authors) / 2); 
  449. $i = 0; 
  450. $j = 0; 
  451. echo "<table style=\"width: 100%; border-collapse: separate; border-spacing: 2px; *border-collapse: expression('separate', cellSpacing = '2px');\" class=\"editform\">\r\n"; 
  452. echo "<tr><td style=\"text-align: left;\" colspan=\"2\">\r\n"; 
  453. echo "<label><input type=\"checkbox\" name=\"checkall\" value=\"checkall_author\" /> " . __('Select / Unselect All', 'subscribe2') . "</label>\r\n"; 
  454. echo "</td></tr>\r\n"; 
  455. echo "<tr style=\"vertical-align: top;\"><td style=\"width: 50%; test-align: left;\">\r\n"; 
  456. foreach ( $all_authors as $author ) { 
  457. if ( $i >= $half && 0 == $j ) { 
  458. echo "</td><td style=\"width: 50%; text-align: left;\">\r\n"; 
  459. $j++; 
  460. if ( 0 == $j ) { 
  461. echo "<label><input class=\"checkall_author\" type=\"checkbox\" name=\"author[]\" value=\"" . $author->ID . "\""; 
  462. if ( in_array($author->ID, $selected) ) { 
  463. echo " checked=\"checked\""; 
  464. echo " /> " . $author->display_name . "</label><br />\r\n"; 
  465. } else { 
  466. echo "<label><input class=\"checkall_author\" type=\"checkbox\" name=\"author[]\" value=\"" . $author->ID . "\""; 
  467. if ( in_array($author->ID, $selected) ) { 
  468. echo " checked=\"checked\""; 
  469. echo " /> " . $author->display_name . "</label><br />\r\n"; 
  470. $i++; 
  471. echo "</td></tr>\r\n"; 
  472. echo "</table>\r\n"; 
  473. } // end display_author_form() 
  474.  
  475. /** 
  476. Collect an array of all author level users and above 
  477. */ 
  478. function get_authors() { 
  479. if ( '' == $this->all_authors ) { 
  480. $role = array('fields' => array('ID', 'display_name'), 'role' => 'administrator'); 
  481. $administrators = get_users( $role ); 
  482. $role = array('fields' => array('ID', 'display_name'), 'role' => 'editor'); 
  483. $editors = get_users( $role ); 
  484. $role = array('fields' => array('ID', 'display_name'), 'role' => 'author'); 
  485. $authors = get_users( $role ); 
  486.  
  487. $this->all_authors = array_merge($administrators, $editors, $authors); 
  488. return apply_filters('s2_authors', $this->all_authors); 
  489. } // end get_authors() 
  490.  
  491. /** 
  492. Display a drop-down form to select subscribers 
  493. $selected is the option to select 
  494. $submit is the text to use on the Submit button 
  495. */ 
  496. function display_subscriber_dropdown($selected = 'registered', $submit = '', $exclude = array()) { 
  497. global $wpdb; 
  498.  
  499. $who = array('all' => __('All Users and Subscribers', 'subscribe2'),  
  500. 'public' => __('Public Subscribers', 'subscribe2'),  
  501. 'confirmed' => '   ' . __('Confirmed', 'subscribe2'),  
  502. 'unconfirmed' => '   ' . __('Unconfirmed', 'subscribe2'),  
  503. 'all_users' => __('All Registered Users', 'subscribe2'),  
  504. 'registered' => __('Registered Subscribers', 'subscribe2')); 
  505.  
  506. $all_cats = $this->all_cats(false); 
  507.  
  508. // count the number of subscribers 
  509. $count['confirmed'] = $wpdb->get_var("SELECT COUNT(id) FROM $this->public WHERE active='1'"); 
  510. $count['unconfirmed'] = $wpdb->get_var("SELECT COUNT(id) FROM $this->public WHERE active='0'"); 
  511. if ( in_array('unconfirmed', $exclude) ) { 
  512. $count['public'] = $count['confirmed']; 
  513. } elseif ( in_array('confirmed', $exclude) ) { 
  514. $count['public'] = $count['unconfirmed']; 
  515. } else { 
  516. $count['public'] = ($count['confirmed'] + $count['unconfirmed']); 
  517. if ( $this->s2_mu ) { 
  518. $count['all_users'] = $wpdb->get_var("SELECT COUNT(meta_key) FROM $wpdb->usermeta WHERE meta_key='" . $wpdb->prefix . "capabilities'"); 
  519. } else { 
  520. $count['all_users'] = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); 
  521. if ( $this->s2_mu ) { 
  522. $count['registered'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(b.meta_key) FROM $wpdb->usermeta AS a INNER JOIN $wpdb->usermeta AS b ON a.user_id = b.user_id WHERE a.meta_key='" . $wpdb->prefix . "capabilities' AND b.meta_key=%s AND b.meta_value <> ''", $this->get_usermeta_keyname('s2_subscribed'))); 
  523. } else { 
  524. $count['registered'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(meta_key) FROM $wpdb->usermeta WHERE meta_key=%s AND meta_value <> ''", $this->get_usermeta_keyname('s2_subscribed'))); 
  525. $count['all'] = ($count['confirmed'] + $count['unconfirmed'] + $count['all_users']); 
  526. // get subscribers to individual categories but only if we are using per-post notifications 
  527. if ( $this->subscribe2_options['email_freq'] == 'never' ) { 
  528. $compulsory = explode(', ', $this->subscribe2_options['compulsory']); 
  529. if ( $this->s2_mu ) { 
  530. foreach ( $all_cats as $cat ) { 
  531. if ( in_array($cat->term_id, $compulsory) ) { 
  532. $count[$cat->name] = $count['all_users']; 
  533. } else { 
  534. $count[$cat->name] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(a.meta_key) FROM $wpdb->usermeta AS a INNER JOIN $wpdb->usermeta AS b ON a.user_id = b.user_id WHERE a.meta_key='" . $wpdb->prefix . "capabilities' AND b.meta_key=%s", $this->get_usermeta_keyname('s2_cat') . $cat->term_id)); 
  535. } else { 
  536. foreach ( $all_cats as $cat ) { 
  537. if ( in_array($cat->term_id, $compulsory) ) { 
  538. $count[$cat->name] = $count['all_users']; 
  539. } else { 
  540. $count[$cat->name] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(meta_value) FROM $wpdb->usermeta WHERE meta_key=%s", $this->get_usermeta_keyname('s2_cat') . $cat->term_id)); 
  541.  
  542. // do have actually have some subscribers? 
  543. if ( 0 == $count['confirmed'] && 0 == $count['unconfirmed'] && 0 == $count['all_users'] ) { 
  544. // no? bail out 
  545. return; 
  546.  
  547. echo "<select name=\"what\">\r\n"; 
  548. foreach ( $who as $whom => $display ) { 
  549. if ( in_array($whom, $exclude) ) { continue; } 
  550. if ( 0 == $count[$whom] ) { continue; } 
  551.  
  552. echo "<option value=\"" . $whom . "\""; 
  553. if ( $whom == $selected ) { echo " selected=\"selected\" "; } 
  554. echo ">$display (" . ($count[$whom]) . ")</option>\r\n"; 
  555.  
  556. if ( $count['registered'] > 0 && $this->subscribe2_options['email_freq'] == 'never' ) { 
  557. foreach ( $all_cats as $cat ) { 
  558. if ( in_array($cat->term_id, $exclude) ) { continue; } 
  559. echo "<option value=\"" . $cat->term_id . "\""; 
  560. if ( $cat->term_id == $selected ) { echo " selected=\"selected\" "; } 
  561. echo ">   " . $cat->name . " (" . $count[$cat->name] . ") </option>\r\n"; 
  562. echo "</select>"; 
  563. if ( false !== $submit ) { 
  564. echo " <input type=\"submit\" class=\"button-secondary\" value=\"$submit\" />\r\n"; 
  565. } // end display_subscriber_dropdown() 
  566.  
  567. /** 
  568. Display a drop down list of administrator level users and 
  569. optionally include a choice for Post Author 
  570. */ 
  571. function admin_dropdown($inc_author = false) { 
  572. global $wpdb; 
  573.  
  574. $args = array('fields' => array('ID', 'display_name'), 'role' => 'administrator'); 
  575. $wp_user_query = get_users( $args ); 
  576. if ( !empty($wp_user_query) ) { 
  577. foreach ($wp_user_query as $user) { 
  578. $admins[] = $user; 
  579. } else { 
  580. $admins = array(); 
  581.  
  582. if ( $inc_author ) { 
  583. $author[] = (object)array('ID' => 'author', 'display_name' => __('Post Author', 'subscribe2')); 
  584. $author[] = (object)array('ID' => 'blogname', 'display_name' => html_entity_decode(get_option('blogname'), ENT_QUOTES)); 
  585. $admins = array_merge($author, $admins); 
  586.  
  587. echo "<select name=\"sender\">\r\n"; 
  588. foreach ( $admins as $admin ) { 
  589. echo "<option value=\"" . $admin->ID . "\""; 
  590. if ( $admin->ID == $this->subscribe2_options['sender'] ) { 
  591. echo " selected=\"selected\""; 
  592. echo ">" . $admin->display_name . "</option>\r\n"; 
  593. echo "</select>\r\n"; 
  594. } // end admin_dropdown() 
  595.  
  596. /** 
  597. Display a dropdown of choices for digest email frequency 
  598. and give user details of timings when event is scheduled 
  599. */ 
  600. function display_digest_choices() { 
  601. global $wpdb; 
  602. $cron_file = ABSPATH . 'wp-cron.php'; 
  603. if ( !is_readable($cron_file) ) { 
  604. echo "<strong><em style=\"color: red\">" . __('The WordPress cron functions may be disabled on this server. Digest notifications may not work.', 'subscribe2') . "</em></strong><br />\r\n"; 
  605. $scheduled_time = wp_next_scheduled('s2_digest_cron'); 
  606. $offset = get_option('gmt_offset') * 60 * 60; 
  607. $schedule = (array)wp_get_schedules(); 
  608. $schedule = array_merge(array('never' => array('interval' => 0, 'display' => __('For each Post', 'subscribe2'))), $schedule); 
  609. $sort = array(); 
  610. foreach ( (array)$schedule as $key => $value ) { 
  611. $sort[$key] = $value['interval']; 
  612. asort($sort); 
  613. $schedule_sorted = array(); 
  614. foreach ( $sort as $key => $value ) { 
  615. $schedule_sorted[$key] = $schedule[$key]; 
  616. foreach ( $schedule_sorted as $key => $value ) { 
  617. echo "<label><input type=\"radio\" name=\"email_freq\" value=\"" . $key . "\"" . checked($this->subscribe2_options['email_freq'], $key, false) . " />"; 
  618. echo " " . $value['display'] . "</label><br />\r\n"; 
  619. if ( $scheduled_time ) { 
  620. $date_format = get_option('date_format'); 
  621. $time_format = get_option('time_format'); 
  622. echo "<p>" . __('Current UTC time is', 'subscribe2') . ": \r\n"; 
  623. echo "<strong>" . date_i18n($date_format . " @ " . $time_format, false, 'gmt') . "</strong></p>\r\n"; 
  624. echo "<p>" . __('Current blog time is', 'subscribe2') . ": \r\n"; 
  625. echo "<strong>" . date_i18n($date_format . " @ " . $time_format) . "</strong></p>\r\n"; 
  626. echo "<p>" . __('Next email notification will be sent when your blog time is after', 'subscribe2') . ": \r\n"; 
  627. echo "<input type=\"hidden\" id=\"jscrondate\" value=\"" . date_i18n($date_format, $scheduled_time + $offset) . "\" />"; 
  628. echo "<input type=\"hidden\" id=\"jscrontime\" value=\"" . date_i18n($time_format, $scheduled_time + $offset) . "\" />"; 
  629. echo "<span id=\"s2cron_1\"><span id=\"s2crondate\" style=\"background-color: #FFFBCC\">" . date_i18n($date_format, $scheduled_time + $offset) . "</span>"; 
  630. echo " @ <span id=\"s2crontime\" style=\"background-color: #FFFBCC\">" . date_i18n($time_format, $scheduled_time + $offset) . "</span> "; 
  631. echo "<a href=\"#\" onclick=\"s2_show('cron'); return false;\">" . __('Edit', 'subscribe2') . "</a></span>\n"; 
  632. echo "<span id=\"s2cron_2\">\r\n"; 
  633. echo "<input id=\"s2datepicker\" name=\"crondate\" value=\"" . date_i18n($date_format, $scheduled_time + $offset) . "\">\r\n"; 
  634. $hours = array('12:00 am', '1:00 am', '2:00 am', '3:00 am', '4:00 am', '5:00 am', '6:00 am', '7:00 am', '8:00 am', '9:00 am', '10:00 am', '11:00 am', '12:00 pm', '1:00 pm', '2:00 pm', '3:00 pm', '4:00 pm', '5:00 pm', '6:00 pm', '7:00 pm', '8:00 pm', '9:00 pm', '10:00 pm', '11:00 pm'); 
  635. $current_hour = intval(date_i18n('G', $scheduled_time + $offset)); 
  636. echo "<select name=\"crontime\">\r\n"; 
  637. foreach ( $hours as $key => $value ) { 
  638. echo "<option value=\"" . $key . "\""; 
  639. if ( !empty($scheduled_time) && $key === $current_hour ) { 
  640. echo " selected=\"selected\""; 
  641. echo ">" . $value . "</option>\r\n"; 
  642. echo "</select>\r\n"; 
  643. echo "<a href=\"#\" onclick=\"s2_cron_update('cron'); return false;\">". __('Update', 'subscribe2') . "</a>\n"; 
  644. echo "<a href=\"#\" onclick=\"s2_cron_revert('cron'); return false;\">". __('Revert', 'subscribe2') . "</a></span>\n"; 
  645. if ( !empty($this->subscribe2_options['last_s2cron']) ) { 
  646. echo "<p>" . __('Attempt to resend the last Digest Notification email', 'subscribe2') . ": "; 
  647. echo "<input type=\"submit\" class=\"button-secondary\" name=\"resend\" value=\"" . __('Resend Digest', 'subscribe2') . "\" /></p>\r\n"; 
  648. } else { 
  649. echo "<br />"; 
  650. } // end display_digest_choices() 
  651.  
  652. /** 
  653. Create and display a dropdown list of pages 
  654. */ 
  655. function pages_dropdown($s2page) { 
  656. $pages = get_pages(); 
  657. if ( empty($pages) ) { return; } 
  658.  
  659. foreach ( $pages as $page ) { 
  660. $option .= "<option value=\"" . $page->ID . "\""; 
  661. if ( $page->ID == $s2page ) { 
  662. $option .= " selected=\"selected\""; 
  663. $option .= ">"; 
  664. $parents = array_reverse( get_ancestors($page->ID, 'page') ); 
  665. if ( $parents ) { 
  666. foreach ( $parents as $parent ) { 
  667. $option .= get_the_title($parent) . ' » '; 
  668. $option .= $page->post_title . "</option>\r\n"; 
  669.  
  670. echo $option; 
  671. } // end pages_dropdown() 
  672.  
  673. /** 
  674. Subscribe all registered users to category selected on Admin Manage Page 
  675. */ 
  676. function subscribe_registered_users($emails = '', $cats = array()) { 
  677. if ( '' == $emails || '' == $cats ) { return false; } 
  678. global $wpdb; 
  679.  
  680. $useremails = explode(", \r\n", $emails); 
  681. $useremails = implode(", ", array_map(array($this, 'prepare_in_data'), $useremails)); 
  682.  
  683. $sql = "SELECT ID FROM $wpdb->users WHERE user_email IN ($useremails)"; 
  684. $user_IDs = $wpdb->get_col($sql); 
  685.  
  686. foreach ( $user_IDs as $user_ID ) { 
  687. $old_cats = get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true); 
  688. if ( !empty($old_cats) ) { 
  689. $old_cats = explode(', ', $old_cats); 
  690. $newcats = array_unique(array_merge($cats, $old_cats)); 
  691. } else { 
  692. $newcats = $cats; 
  693. if ( !empty($newcats) && $newcats !== $old_cats) { 
  694. // add subscription to these cat IDs 
  695. foreach ( $newcats as $id ) { 
  696. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $id, $id); 
  697. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), implode(', ', $newcats)); 
  698. unset($newcats); 
  699. } // end subscribe_registered_users() 
  700.  
  701. /** 
  702. Unsubscribe all registered users to category selected on Admin Manage Page 
  703. */ 
  704. function unsubscribe_registered_users($emails = '', $cats = array()) { 
  705. if ( '' == $emails || '' == $cats ) { return false; } 
  706. global $wpdb; 
  707.  
  708. $useremails = explode(", \r\n", $emails); 
  709. $useremails = implode(", ", array_map(array($this, 'prepare_in_data'), $useremails)); 
  710.  
  711. $sql = "SELECT ID FROM $wpdb->users WHERE user_email IN ($useremails)"; 
  712. $user_IDs = $wpdb->get_col($sql); 
  713.  
  714. foreach ( $user_IDs as $user_ID ) { 
  715. $old_cats = explode(', ', get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true)); 
  716. $remain = array_diff($old_cats, $cats); 
  717. if ( !empty($remain) && $remain !== $old_cats) { 
  718. // remove subscription to these cat IDs and update s2_subscribed 
  719. foreach ( $cats as $id ) { 
  720. delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $id); 
  721. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), implode(', ', $remain)); 
  722. } else { 
  723. // remove subscription to these cat IDs and update s2_subscribed to '' 
  724. foreach ( $cats as $id ) { 
  725. delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $id); 
  726. delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed')); 
  727. unset($remain); 
  728. } // end unsubscribe_registered_users() 
  729.  
  730. /** 
  731. Handles bulk changes to email format for Registered Subscribers 
  732. */ 
  733. function format_change($emails, $format) { 
  734. if ( empty($format) ) { return; } 
  735.  
  736. global $wpdb; 
  737. $useremails = explode(", \r\n", $emails); 
  738. $useremails = implode(", ", array_map(array($this, 'prepare_in_data'), $useremails)); 
  739. $ids = $wpdb->get_col("SELECT ID FROM $wpdb->users WHERE user_email IN ($useremails)"); 
  740. $ids = implode(', ', array_map(array($this, 'prepare_in_data'), $ids)); 
  741. $sql = "UPDATE $wpdb->usermeta SET meta_value='{$format}' WHERE meta_key='" . $this->get_usermeta_keyname('s2_format') . "' AND user_id IN ($ids)"; 
  742. $wpdb->query($sql); 
  743. } // end format_change() 
  744.  
  745. /** 
  746. Handles bulk update to digest preferences 
  747. */ 
  748. function digest_change($emails, $digest) { 
  749. if ( empty($digest) ) { return; } 
  750.  
  751. global $wpdb; 
  752. $useremails = explode(", \r\n", $emails); 
  753. $useremails = implode(", ", array_map(array($this, 'prepare_in_data'), $useremails)); 
  754.  
  755. $sql = "SELECT ID FROM $wpdb->users WHERE user_email IN ($useremails)"; 
  756. $user_IDs = $wpdb->get_col($sql); 
  757.  
  758. if ( $digest == 'digest' ) { 
  759. $exclude = explode(', ', $this->subscribe2_options['exclude']); 
  760. if ( !empty($exclude) ) { 
  761. $all_cats = $this->all_cats(true, 'ID'); 
  762. } else { 
  763. $all_cats = $this->all_cats(false, 'ID'); 
  764.  
  765. $cats_string = ''; 
  766. foreach ( $all_cats as $cat ) { 
  767. ('' == $cats_string) ? $cats_string = "$cat->term_id" : $cats_string .= ", $cat->term_id"; 
  768.  
  769. foreach ( $user_IDs as $user_ID ) { 
  770. foreach ( $all_cats as $cat ) { 
  771. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat->term_id, $cat->term_id); 
  772. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), $cats_string); 
  773. } elseif ( $digest == '-1' ) { 
  774. foreach ( $user_IDs as $user_ID ) { 
  775. $cats = explode(', ', get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true)); 
  776. foreach ( $cats as $id ) { 
  777. delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $id); 
  778. delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed')); 
  779. } // end digest_change() 
  780.  
  781. /** ===== functions to handle addition and removal of WordPress categories ===== */ 
  782. /** 
  783. Autosubscribe registered users to newly created categories 
  784. if registered user has selected this option 
  785. */ 
  786. function new_category($new_category='') { 
  787. if ( 'no' == $this->subscribe2_options['show_autosub'] ) { return; } 
  788.  
  789. global $wpdb; 
  790. if ( $this->subscribe2_options['email_freq'] != 'never' ) { 
  791. // if we are doing digests add new categories to users who are currently opted in 
  792. $sql = $wpdb->prepare("SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE meta_key=%s AND meta_value<>''", $this->get_usermeta_keyname('s2_subscribed')); 
  793. $user_IDs = $wpdb->get_col($sql); 
  794. foreach ( $user_IDs as $user_ID ) { 
  795. $old_cats = get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true); 
  796. $old_cats = explode(', ', $old_cats); 
  797. $newcats = array_merge($old_cats, (array)$new_category); 
  798. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $new_category, $new_category); 
  799. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), implode(', ', $newcats)); 
  800. return; 
  801.  
  802. if ( 'yes' == $this->subscribe2_options['show_autosub'] ) { 
  803. if ( $this->s2_mu ) { 
  804. $sql = $wpdb->prepare("SELECT DISTINCT a.user_id FROM $wpdb->usermeta AS a INNER JOIN $wpdb->usermeta AS b WHERE a.user_id = b.user_id AND a.meta_key=%s AND a.meta_value='yes' AND b.meta_key=%s", $this->get_usermeta_keyname('s2_autosub'), $this->get_usermeta_keyname('s2_subscribed')); 
  805. } else { 
  806. $sql = $wpdb->prepare("SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE $wpdb->usermeta.meta_key=%s AND $wpdb->usermeta.meta_value='yes'", $this->get_usermeta_keyname('s2_autosub')); 
  807. $user_IDs = $wpdb->get_col($sql); 
  808. if ( '' == $user_IDs ) { return; } 
  809.  
  810. foreach ( $user_IDs as $user_ID ) { 
  811. $old_cats = get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true); 
  812. if ( empty($old_cats) ) { 
  813. $newcats = (array)$new_category; 
  814. } else { 
  815. $old_cats = explode(', ', $old_cats); 
  816. $newcats = array_merge($old_cats, (array)$new_category); 
  817. // add subscription to these cat IDs 
  818. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $new_category, $new_category); 
  819. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), implode(', ', $newcats)); 
  820. } elseif ( 'exclude' == $this->subscribe2_options['show_autosub'] ) { 
  821. $excluded_cats = explode(', ', $this->subscribe2_options['exclude']); 
  822. $excluded_cats[] = $new_category; 
  823. $this->subscribe2_options['exclude'] = implode(', ', $excluded_cats); 
  824. update_option('subscribe2_options', $this->subscribe2_options); 
  825. } // end new_category() 
  826.  
  827. /** 
  828. Automatically delete subscriptions to a category when it is deleted 
  829. */ 
  830. function delete_category($deleted_category='') { 
  831. global $wpdb; 
  832.  
  833. if ( $this->s2_mu ) { 
  834. $sql = $wpdb->prepare("SELECT DISTINCT a.user_id FROM $wpdb->usermeta AS a INNER JOIN $wpdb->usermeta AS b WHERE a.user_id = b.user_id AND a.meta_key=%s AND b.meta_key=%s", $this->get_usermeta_keyname('s2_cat') . $deleted_category, $this->get_usermeta_keyname('s2_subscribed')); 
  835. } else { 
  836. $sql = $wpdb->prepare("SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE meta_key=%s", $this->get_usermeta_keyname('s2_cat') . $deleted_category); 
  837. $user_IDs = $wpdb->get_col($sql); 
  838. if ( '' == $user_IDs ) { return; } 
  839.  
  840. foreach ( $user_IDs as $user_ID ) { 
  841. $old_cats = explode(', ', get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true)); 
  842. if ( !is_array($old_cats) ) { 
  843. $old_cats = array($old_cats); 
  844. // add subscription to these cat IDs 
  845. delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $deleted_category); 
  846. $remain = array_diff($old_cats, (array)$deleted_category); 
  847. update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), implode(', ', $remain)); 
  848. } // end delete_category() 
  849.  
  850. /** ===== functions to show & handle one-click subscription ===== */ 
  851. /** 
  852. Show form for one-click subscription on user profile page 
  853. */ 
  854. function one_click_profile_form($user) { 
  855. echo "<h3>" . __('Email subscription', 'subscribe2') . "</h3>\r\n"; 
  856. echo "<table class=\"form-table\">\r\n"; 
  857. echo "<tr><th scope=\"row\">" . __('Subscribe / Unsubscribe', 'subscribe2') . "</th>\r\n"; 
  858. echo "<td><label><input type=\"checkbox\" name=\"sub2-one-click-subscribe\" value=\"1\" " . checked( ! get_user_meta($user->ID, $this->get_usermeta_keyname('s2_subscribed'), true), false, false ) . " /> " . __('Receive notifications', 'subscribe2') . "</label><br />\r\n"; 
  859. echo "<span class=\"description\">" . __('Check if you want to receive email notification when new posts are published', 'subscribe2') . "</span>\r\n"; 
  860. echo "</td></tr></table>\r\n"; 
  861. } // end one_click_profile_form() 
  862.  
  863. /** 
  864. Handle submission from profile one-click subscription 
  865. */ 
  866. function one_click_profile_form_save($user_ID) { 
  867. if ( !current_user_can( 'edit_user', $user_ID ) ) { 
  868. return false; 
  869.  
  870. if ( isset( $_POST['sub2-one-click-subscribe'] ) && 1 == $_POST['sub2-one-click-subscribe'] ) { 
  871. // Subscribe 
  872. $this->one_click_handler($user_ID, 'subscribe'); 
  873. } else { 
  874. // Unsubscribe 
  875. $this->one_click_handler($user_ID, 'unsubscribe'); 
  876. } // end one_click_profile_form_save()