bbpress_topic_post_type = bbp_get_topic_post_type(); $this->bbpress_reply_post_type = bbp_get_reply_post_type(); } function bg_notify_new_reply($topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0) { wp_schedule_single_event(time() + 10, 'bbpress_notify_bg_reply', array($topic_id, $forum_id, $anonymous_data, $topic_author)); } function bg_notify_new_topic($topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0) { wp_schedule_single_event(time() + 10, 'bbpress_notify_bg_topic', array($topic_id, $forum_id, $anonymous_data, $topic_author)); } function deactivate_old() { $old_plugin = 'bbpress-notify/bbpress-notify.php'; if (is_plugin_active($old_plugin)) { deactivate_plugins($old_plugin); } } /** * Deprecated, the project did not get backing. */ function maybe_show_pro_message() { if ( isset($_GET['dismiss-bbpnp']) ) { update_option('bbpress-notify-pro-dismissed', true); } elseif ( ! get_option('bbpress-notify-pro-dismissed') ) { $url = add_query_arg( array( 'dismiss-bbpnp' => 1), $_SERVER['REQUEST_URI'] ); ?>

bbPress Notify Pro project at Kickstarter? ' . 'Help us reach the goal and get a nifty reward =] ! | Dismiss.',$url), $this->domain ); ?>

bbpress_topic_post_type}_notification")) { update_option("bbpress_notify_default_{$this->bbpress_topic_post_type}_notification", 0); } if (!get_option("bbpress_notify_default_{$this->bbpress_reply_post_type}_notification")) { update_option("bbpress_notify_default_{$this->bbpress_reply_post_type}_notification", 0); } } /** * @since 1.0 */ function notify_new_topic($topic_id = 0, $forum_id = 0) { global $wpdb; $status = get_post_status($topic_id); if ( 'spam' === $status || 'publish' !== $status ) return -1; if (0 === $forum_id) $forum_id = bbp_get_topic_forum_id($topic_id); if (true === apply_filters('bbpnns_skip_topic_notification', false, $forum_id, $topic_id)) return -3; $opt_recipients = apply_filters('bbpress_notify_recipients_hidden_forum', get_option('bbpress_notify_newtopic_recipients'), $forum_id); $recipients = array(); foreach ((array)$opt_recipients as $opt_recipient) { if (! $opt_recipient) continue; $users = get_users(array('role' => $opt_recipient)); foreach ((array)$users as $user) { $user = get_object_vars($user); $recipients[$user['ID']] = $user['ID']; // make sure unique recepients } } /** * Allow topic recipients munging * @since 1.6.5 */ $recipients = apply_filters('bbpress_topic_notify_recipients', $recipients, $topic_id, $forum_id); if ( empty($recipients) ) return -2; list($email_subject, $email_body) = $this->_build_email('topic', $topic_id); return $this->send_notification($recipients, $email_subject, $email_body); } /** * @since 1.5 * @desc Forces admin-only recipients if forum is hidden * @param array $recipients * @param number $topic_id * @return array */ public function munge_newtopic_recipients($recipients=array(), $forum_id = 0) { if (true === (bool) bbp_is_forum_hidden($forum_id) && true === (bool) get_option('bbpress_notify_hidden_forum_topic_override', true)) { $recipients = 'administrator'; } return $recipients; } /** * @since 1.0 */ function notify_new_reply($reply_id = 0, $topic_id = 0, $forum_id = 0) { global $wpdb; $status = get_post_status($reply_id); if ( 'spam' === $status || 'publish' !== $status ) return -1; if (true === apply_filters('bbpnns_skip_reply_notification', false, $forum_id, $topic_id, $reply_id)) return -3; $opt_recipients = apply_filters('bbpress_notify_recipients_hidden_forum', get_option('bbpress_notify_newreply_recipients'), $forum_id); $recipients = array(); foreach ((array)$opt_recipients as $opt_recipient) { if (! $opt_recipient) continue; $users = get_users(array('role' => $opt_recipient)); foreach ((array)$users as $user) { $user = get_object_vars($user); $recipients[$user['ID']] = $user['ID']; // make sure unique recepients } } /** * Allow reply recipients munging * @since 1.6.5 */ $recipients = apply_filters('bbpress_reply_notify_recipients', $recipients, $reply_id, $topic_id, $forum_id); if ( empty($recipients) ) return -2; list($email_subject, $email_body) = $this->_build_email('reply', $reply_id); return $this->send_notification($recipients, $email_subject, $email_body); } /** * @since 1.4 */ private function _build_email($type, $post_id) { $email_subject = get_option("bbpress_notify_new{$type}_email_subject"); $email_body = get_option("bbpress_notify_new{$type}_email_body"); $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); $excerpt_size = apply_filters('bpnns_excerpt_size', 100); // Replace shortcodes if ('topic' === $type) { $content = bbp_get_topic_content($post_id); $title = html_entity_decode(strip_tags(bbp_get_topic_title($post_id)), ENT_NOQUOTES, 'UTF-8'); $excerpt = html_entity_decode(strip_tags(bbp_get_topic_excerpt($post_id, $excerpt_size)), ENT_NOQUOTES, 'UTF-8'); $author = bbp_get_topic_author($post_id); $url = apply_filters( 'bbpnns_topic_url', bbp_get_topic_permalink($post_id), $post_id, $title ); $forum = html_entity_decode(strip_tags(get_the_title(bbp_get_topic_forum_id($post_id))), ENT_NOQUOTES, 'UTF-8'); } elseif ('reply' === $type) { $content = bbp_get_reply_content($post_id); $title = html_entity_decode(strip_tags(bbp_get_reply_title($post_id)), ENT_NOQUOTES, 'UTF-8'); $excerpt = html_entity_decode(strip_tags(bbp_get_reply_excerpt($post_id, $excerpt_size)), ENT_NOQUOTES, 'UTF-8'); $author = bbp_get_reply_author($post_id); $url = apply_filters( 'bbpnns_reply_url', bbp_get_reply_permalink($post_id), $post_id, $title ); $forum = html_entity_decode(strip_tags(get_the_title(bbp_get_reply_forum_id($post_id))), ENT_NOQUOTES, 'UTF-8'); } else { wp_die('Invalid type!'); } $content = preg_replace('//is', "\n", $content); $content = preg_replace('/(?:<\/p>\s*

)/ism', "\n\n", $content); $content = html_entity_decode(strip_tags($content), ENT_NOQUOTES, 'UTF-8'); $topic_reply = apply_filters( 'bbpnns_topic_reply', bbp_get_reply_url($post_id), $post_id, $title ); $email_subject = str_replace('[blogname]', $blogname, $email_subject); $email_subject = str_replace("[$type-title]", $title, $email_subject); $email_subject = str_replace("[$type-content]", $content, $email_subject); $email_subject = str_replace("[$type-excerpt]", $excerpt, $email_subject); $email_subject = str_replace("[$type-author]", $author, $email_subject); $email_subject = str_replace("[$type-url]", $url, $email_subject); $email_subject = str_replace("[$type-replyurl]", $topic_reply, $email_subject); $email_subject = str_replace("[$type-forum]", $forum, $email_subject); $email_body = str_replace('[blogname]', $blogname, $email_body); $email_body = str_replace("[$type-title]", $title, $email_body); $email_body = str_replace("[$type-content]", $content, $email_body); $email_body = str_replace("[$type-excerpt]", $excerpt, $email_body); $email_body = str_replace("[$type-author]", $author, $email_body); $email_body = str_replace("[$type-url]", $url, $email_body); $email_body = str_replace("[$type-replyurl]", $topic_reply, $email_body); $email_body = str_replace("[$type-forum]", $forum, $email_body); /** * Allow subject and body modifications * @since 1.6.6 */ $email_subject = apply_filters('bbpnns_filter_email_subject_in_build', $email_subject); $email_body = apply_filters('bbpnns_filter_email_body_in_build', $email_body); return array($email_subject, $email_body); } /** * @since 1.0 */ function send_notification($recipients, $subject, $body) { $headers = sprintf("From: %s <%s>\r\n", get_option('blogname'), get_bloginfo('admin_email')); $headers = apply_filters('bbpnns_extra_headers', $headers, $recipients, $subject, $body); // Allow Management of recipients list $recipients = apply_filters('bbpnns_filter_recipients_before_send', $recipients); foreach ( (array) $recipients as $recipient_id) { $user_info = get_userdata($recipient_id); /** * Allow per user subject and body modifications * @since 1.6.4 */ $filtered_subject = apply_filters('bbpnns_filter_email_subject_for_user', $subject, $user_info); $filtered_body = apply_filters('bbpnns_filter_email_body_for_user', $body, $user_info); $email = ($recipient_id == -1) ? get_bloginfo('admin_email') : (string) $user_info->user_email ; if (false === apply_filters('bbpnns_dry_run', false)) { if ( ! wp_mail($email, $filtered_subject, $filtered_body, $headers) ) { error_log('[bbPress Notify No Spam] wp_mail failed: ' . print_r(error_get_last(),1)); return false; } } } if (true === apply_filters('bbpnns_dry_run', false)) return array($recipients, $body); return true; } /** * @since 1.0 */ /* Add the settings to the bbPress page in the Dashboard */ function admin_settings() { // Add section to bbPress options add_settings_section($this->settings_section, __('E-mail Notifications', 'bbpress_notify'), array(&$this,'_settings_intro_text'), 'bbpress'); // Add background option add_settings_field('bbpress_notify_newtopic_background', __('Background Topic Notifications', 'bbpress_notify'), array(&$this,'_topic_background_inputfield'), 'bbpress', 'bbpress_notify_options'); // Add default notification option add_settings_field("bbpress_notify_default_{$this->bbpress_topic_post_type}_notification", __('Admin UI Topic Notifications', 'bbpress_notify'), array(&$this,'_admin_ui_topic_inputfield'), 'bbpress', 'bbpress_notify_options'); // Add form fields for all settings add_settings_field('bbpress_notify_newtopic_recipients', __('Notifications about new topics are sent to', 'bbpress_notify'), array(&$this,'_topic_recipients_inputfield'), 'bbpress', 'bbpress_notify_options'); add_settings_field('bbpress_notify_hidden_forum_topic_override', __('Force Admin-only emails if Forum is hidden (topics)', 'bbpress_notify'), array(&$this,'_hidden_forum_topic_override'), 'bbpress', 'bbpress_notify_options'); add_settings_field('bbpress_notify_newtopic_email_subject', __('E-mail subject', 'bbpress_notify'), array(&$this,'_email_newtopic_subject_inputfield'), 'bbpress', 'bbpress_notify_options'); add_settings_field('bbpress_notify_newtopic_email_body', __('E-mail body (template tags: [blogname], [topic-title], [topic-content], [topic-excerpt], [topic-author], [topic-url], [topic-forum], [topic-replyurl])', 'bbpress_notify'), array(&$this,'_email_newtopic_body_inputfield'), 'bbpress', 'bbpress_notify_options'); add_settings_field('bbpress_notify_newreply_background', __('Background Reply Notifications', 'bbpress_notify'), array(&$this,'_reply_background_inputfield'), 'bbpress', 'bbpress_notify_options'); // Add default notification option add_settings_field("bbpress_notify_default_{$this->bbpress_reply_post_type}_notification", __('Admin UI Reply Notifications', 'bbpress_notify'), array(&$this,'_admin_ui_reply_inputfield'), 'bbpress', 'bbpress_notify_options'); add_settings_field('bbpress_notify_newreply_recipients', __('Notifications about replies are sent to', 'bbpress_notify'), array(&$this,'_reply_recipients_inputfield'), 'bbpress', 'bbpress_notify_options'); add_settings_field('bbpress_notify_hidden_forum_reply_override', __('Force Admin-only emails if Forum is hidden (replies)', 'bbpress_notify'), array(&$this,'_hidden_forum_reply_override'), 'bbpress', 'bbpress_notify_options'); add_settings_field('bbpress_notify_newreply_email_subject', __('E-mail subject', 'bbpress_notify'), array(&$this,'_email_newreply_subject_inputfield'), 'bbpress', 'bbpress_notify_options'); add_settings_field('bbpress_notify_newreply_email_body', __('E-mail body (template tags: [blogname], [reply-title], [reply-content], [reply-excerpt], [reply-author], [reply-url], [reply-forum], [reply-replyurl])', 'bbpress_notify'), array(&$this,'_email_newreply_body_inputfield'), 'bbpress', 'bbpress_notify_options'); // Register the settings as part of the bbPress settings register_setting('bbpress', 'bbpress_notify_newtopic_recipients'); register_setting('bbpress', 'bbpress_notify_hidden_forum_topic_override'); register_setting('bbpress', 'bbpress_notify_hidden_forum_reply_override'); register_setting('bbpress', 'bbpress_notify_newtopic_email_subject'); register_setting('bbpress', 'bbpress_notify_newtopic_email_body'); register_setting('bbpress', 'bbpress_notify_newtopic_background'); register_setting('bbpress', 'bbpress_notify_newreply_recipients'); register_setting('bbpress', 'bbpress_notify_newreply_email_subject'); register_setting('bbpress', 'bbpress_notify_newreply_email_body'); register_setting('bbpress', 'bbpress_notify_newreply_background'); register_setting('bbpress', "bbpress_notify_default_{$this->bbpress_topic_post_type}_notification"); register_setting('bbpress', "bbpress_notify_default_{$this->bbpress_reply_post_type}_notification"); } /** * @since 1.0 */ function _settings_intro_text($args) { printf('%s', $args['id'], __('Configure e-mail notifications when new topics and/or replies are posted.', 'bbpress_notify')); } /** * @since 1.3 */ function _topic_background_inputfield() { $saved_option = get_option('bbpress_notify_newtopic_background'); $html_checked = ( $saved_option ) ? 'checked="checked"' : ''; $description = __('Send emails in the background the next time the site is visited', 'bbpress_notify'); printf(' %s
', $html_checked, $description); } /** * @since 1.3 */ function _reply_background_inputfield() { $saved_option = get_option('bbpress_notify_newreply_background'); $html_checked = ( $saved_option ) ? 'checked="checked"' : ''; $description = __('Send emails in the background the next time the site is visited', 'bbpress_notify'); printf(' %s
', $html_checked, $description); } /** * @since 1.0 */ /* Show a %s
', $html_checked, $value, $description); } } /** * @since 1.5 */ function _hidden_forum_topic_override() { $saved_option = get_option('bbpress_notify_hidden_forum_topic_override'); $checked = true === (bool) $saved_option ? 'checked="checked"' : ''; printf('
', $checked); } /** * @since 1.0 */ /* Show a %s
', $html_checked, $value, $description); } } /** * @since 1.5 */ function _hidden_forum_reply_override() { $saved_option = get_option('bbpress_notify_hidden_forum_reply_override'); $checked = true === (bool) $saved_option ? 'checked="checked"' : ''; printf('
', $checked); } /** * @since 1.0 */ /* Show a field for new topic e-mail subject */ function _email_newtopic_subject_inputfield() { printf('', get_option('bbpress_notify_newtopic_email_subject')); } /** * @since 1.0 */ /* Show a ', get_option('bbpress_notify_newtopic_email_body')); printf('

%s: [blogname], [topic-title], [topic-content], [topic-excerpt], [topic-url], [topic-replyurl], [topic-author]

', __('Shortcodes', 'bbpress_notify')); } /** * @since 1.0 */ /* Show a field for new reply e-mail subject */ function _email_newreply_subject_inputfield() { printf('', get_option('bbpress_notify_newreply_email_subject')); } /** * @since 1.0 */ /* Show a ', get_option('bbpress_notify_newreply_email_body')); printf('

%s: [blogname], [reply-title], [reply-content], [reply-excerpt], [reply-url], [reply-replyurl], [reply-author]

', __('Shortcodes', 'bbpress_notify')); } /** * @since 1.4 */ function plugin_action_links( $links, $file ) { if ( $file === plugin_basename( dirname(__FILE__).'/bbpress-notify-nospam.php' ) ) $links[] = ''.__( 'Settings' ).''; return $links; } /** * @since 1.4 */ function add_notification_meta_box() { add_meta_box( 'send_notification', __( 'Notifications', 'bbpress_notify' ), array(&$this,'notification_meta_box_content'), bbp_get_topic_post_type(), 'side','high' ); add_meta_box( 'send_notification', __( 'Notifications', 'bbpress_notify' ), array(&$this,'notification_meta_box_content'), bbp_get_reply_post_type(), 'side','high' ); } /** * @since 1.4 */ function notification_meta_box_content( $post ) { $type = ($post->post_type === $this->bbpress_topic_post_type) ? 'topic' : 'reply'; $default = get_option("bbpress_notify_default_{$type}_notification"); $checked = checked($default, true, false); wp_create_nonce("bbpress_send_{$type}_notification_nonce"); wp_nonce_field("bbpress_send_{$type}_notification_nonce", "bbpress_send_{$type}_notification_nonce"); printf(' %s',$checked, __('Send notification.', 'bbpress_notify')); } /** * @since 1.4 */ function _admin_ui_topic_inputfield() { $default = get_option("bbpress_notify_default_{$this->bbpress_topic_post_type}_notification"); $checked = checked($default, true, false); printf(' ', "bbpress_notify_default_{$this->bbpress_topic_post_type}_notification", $checked); _e('Send notifications when creating Topics in the Admin UI (Can be overridden in the New/Update Topic screen).'); } /** * @since 1.4 */ function _admin_ui_reply_inputfield() { $default = get_option("bbpress_notify_default_{$this->bbpress_reply_post_type}_notification"); $checked = checked($default, true, false); printf(' ', "bbpress_notify_default_{$this->bbpress_reply_post_type}_notification", $checked); _e('Send notifications when creating Replies in the Admin UI (Can be overridden in the New/Update Reply screen).'); } /** * Sends notifications when user saves/publishes a post. Note that the send notification checkbox must be ticked. * @param int $post_id * @param object $post * @return array */ function notify_on_save($post_id, $post) { if (empty($_POST)) return; if ($this->bbpress_topic_post_type !== $post->post_type && $this->bbpress_reply_post_type !== $post->post_type) return; if (! current_user_can('manage_options') && ! current_user_can('edit_post', $post_id)) return; if (wp_is_post_revision( $post_id )) return; if (! isset($_POST['bbpress_notify_send_notification']) || ! $_POST['bbpress_notify_send_notification']) return; $type = ($post->post_type === $this->bbpress_topic_post_type) ? 'topic' : 'reply'; if (! isset($_POST["bbpress_send_{$type}_notification_nonce"]) || ! wp_verify_nonce( $_POST["bbpress_send_{$type}_notification_nonce"], "bbpress_send_{$type}_notification_nonce" ) ) { return; } // Still here, so we can notify if ($post->post_type === $this->bbpress_topic_post_type) { return $this->notify_new_topic($post_id); } else { return $this->notify_new_reply($post_id); } } /** * @since 1.5.4 */ public static function missing_bbpress_notice() { ?>

bbPress Notify (No-Spam) could not find an active bbPress plugin. It will not load until bbPress is installed and active.'); ?>