/** * Remove a topic from user's subscriptions * * @since bbPress (r2668) * * @param int $user_id Optional. User id * @param int $topic_id Optional. Topic id * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions * @uses update_user_option() To update the user's subscriptions * @uses delete_user_option() To delete the user's subscriptions meta * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and * topic id * @return bool True if the topic was removed from user's subscriptions, * otherwise false */ function bbp_remove_user_subscription($user_id, $topic_id) { if (empty($user_id) || empty($topic_id)) { return false; } $subscriptions = (array) bbp_get_user_subscribed_topic_ids($user_id); if (empty($subscriptions)) { return false; } $pos = array_search($topic_id, $subscriptions); if (is_numeric($pos)) { array_splice($subscriptions, $pos, 1); $subscriptions = array_filter($subscriptions); if (!empty($subscriptions)) { $subscriptions = implode(',', $subscriptions); update_user_option($user_id, '_bbp_subscriptions', $subscriptions); } else { delete_user_option($user_id, '_bbp_subscriptions'); } wp_cache_delete('bbp_get_topic_subscribers_' . $topic_id, 'bbpress'); } do_action('bbp_remove_user_subscription', $user_id, $topic_id); return true; }
/** * Create the forum topic subscriptions CSV when requested. * * @since 1.0.0 */ public function run_forum_topic_subscriptions_csv() { // Output headers so that the file is downloaded rather than displayed. header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=cc-forum-topic-subscriptions.csv'); // Create a file pointer connected to the output stream. $output = fopen('php://output', 'w'); // Write a header row. $row = array('user_id', 'user_email', 'subscribed_to_topic_id', 'subscribed_to_topic_title', 'subscribed_to_topic_by_user_id', 'subscribed_to_topic_by_user_email', 'topic_date'); fputcsv($output, $row); // Use a WP_User_Query meta_query to find users who have subscribed to forums. $args = array('meta_key' => 'wp__bbp_subscriptions', 'meta_compare' => 'EXISTS'); $user_query = new WP_User_Query($args); // User Loop if (!empty($user_query->results)) { foreach ($user_query->results as $user) { $subscriptions = bbp_get_user_subscribed_topic_ids($user->ID); if (empty($subscriptions)) { continue; } $topics = new WP_QUERY(array('post_type' => 'topic', 'post__in' => $subscriptions, 'cache_results' => false, 'update_post_meta_cache' => false, 'update_post_term_cache' => false)); if (!empty($topics->posts)) { foreach ($topics->posts as $item) { $op = get_userdata($item->post_author); $row = array($user->ID, $user->user_email, $item->ID, $item->post_title, $item->post_author, $op->user_email, $item->post_date); fputcsv($output, $row); } } } } fclose($output); exit; }
/** * Check if a topic is in user's subscription list or not * * @since 2.5.0 bbPress (r5156) * * @param int $user_id Optional. User id * @param int $topic_id Optional. Topic id * @param array $subscribed_ids Optional. Array of topic ID's to check * @uses bbp_get_user_id() To get the user id * @uses bbp_get_topic() To get the topic * @uses bbp_get_topic_id() To get the topic id * @uses bbp_is_object_of_user() To check if the user is subscribed * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id, * topic id and subsriptions * @return bool True if the topic is in user's subscriptions, otherwise false */ function bbp_is_user_subscribed_to_topic($user_id = 0, $topic_id = 0, $subscribed_ids = array()) { // Assume user is not subscribed $retval = false; // Validate user $user_id = bbp_get_user_id($user_id, true, true); if (!empty($user_id)) { // Get subscription ID's if none passed if (empty($subscribed_ids)) { $subscribed_ids = bbp_get_user_subscribed_topic_ids($user_id); } // User has topic subscriptions if (!empty($subscribed_ids)) { // Checking a specific topic id if (!empty($topic_id)) { $topic = bbp_get_topic($topic_id); $topic_id = !empty($topic) ? $topic->ID : 0; // Using the global topic id } elseif (bbp_get_topic_id()) { $topic_id = bbp_get_topic_id(); // Use the current post id } elseif (!bbp_get_topic_id()) { $topic_id = get_the_ID(); } // Is topic_id in the user's subscriptions if (!empty($topic_id)) { $retval = bbp_is_object_of_user($topic_id, $user_id, '_bbp_subscription'); } } } return (bool) apply_filters('bbp_is_user_subscribed_to_topic', (bool) $retval, $user_id, $topic_id, $subscribed_ids); }
/** * Displays the output, the login form * * @since bbPress (r2827) * * @param mixed $args Arguments * @param array $instance Instance * @uses apply_filters() Calls 'bbp_login_widget_title' with the title * @uses get_template_part() To get the login/logged in form */ public function widget($args = array(), $instance = array()) { // Get widget settings $settings = $this->parse_settings($instance); // Typical WordPress filter $settings['title'] = apply_filters('widget_title', $settings['title'], $instance, $this->id_base); // bbPress filters $settings['title'] = apply_filters('bbp_login_widget_title', $settings['title'], $instance, $this->id_base); $settings['register'] = apply_filters('bbp_login_widget_register', $settings['register'], $instance, $this->id_base); $settings['lostpass'] = apply_filters('bbp_login_widget_lostpass', $settings['lostpass'], $instance, $this->id_base); echo $args['before_widget']; if (!empty($settings['title'])) { echo $args['before_title'] . $settings['title'] . $args['after_title']; } if (!is_user_logged_in()) { ?> <form method="post" action="<?php bbp_wp_login_action(array('context' => 'login_post')); ?> " class="bbp-login-form"> <fieldset> <legend><?php _e('Log In', 'bbpress'); ?> </legend> <div class="bbp-username form-group"> <label for="user_login" class="sr-only"><?php _e('Username', 'bbpress'); ?> : </label> <div class="input-group"> <span class="input-group-addon"> <span class="glyphicon ipt-icomoon-user"></span> </span> <input placeholder="<?php _e('Username', 'bbpress'); ?> " class="form-control" type="text" name="log" value="<?php bbp_sanitize_val('user_login', 'text'); ?> " size="20" id="user_login" tabindex="<?php bbp_tab_index(); ?> " /> </div> </div> <div class="bbp-password form-group"> <label for="user_pass" class="sr-only"><?php _e('Password', 'bbpress'); ?> : </label> <div class="input-group"> <span class="input-group-addon"> <span class="glyphicon ipt-icomoon-console"></span> </span> <input placeholder="<?php _e('Password', 'bbpress'); ?> " class="form-control" type="password" name="pwd" value="<?php bbp_sanitize_val('user_pass', 'password'); ?> " size="20" id="user_pass" tabindex="<?php bbp_tab_index(); ?> " /> </div> </div> <?php do_action('login_form'); ?> <div class="bbp-remember-me checkbox"> <input type="checkbox" name="rememberme" value="forever" <?php checked(bbp_get_sanitize_val('rememberme', 'checkbox'), true, true); ?> id="rememberme" tabindex="<?php bbp_tab_index(); ?> " /> <label for="rememberme"><?php _e('Remember Me', 'bbpress'); ?> </label> </div> <div class="bbp-submit-wrapper btn-group"> <?php if (!empty($settings['lostpass'])) { ?> <a href="<?php echo esc_url($settings['lostpass']); ?> " title="<?php esc_attr_e('Lost Password', 'bbpress'); ?> " class="bbp-lostpass-link btn btn-default"><span class="glyphicon ipt-icomoon-info"></span></a> <?php } ?> <?php if (!empty($settings['register'])) { ?> <a href="<?php echo esc_url($settings['register']); ?> " title="<?php esc_attr_e('Register', 'bbpress'); ?> " class="bbp-register-link btn btn-default"><span class="glyphicon ipt-icomoon-signup"></span> <?php _e('Register', 'bbpress'); ?> </a> <?php } ?> <button class="btn btn-primary" type="submit" name="user-submit" id="user-submit" tabindex="<?php bbp_tab_index(); ?> " class="button submit user-submit"><span class="glyphicon ipt-icomoon-switch"></span> <?php _e('Log In', 'bbpress'); ?> </button> </div> <?php bbp_user_login_fields(); ?> </fieldset> </form> <?php } else { ?> <div class="bbp-logged-in"> <a href="<?php bbp_user_profile_url(bbp_get_current_user_id()); ?> " class="submit user-submit thumbnail pull-left"><?php echo get_avatar(bbp_get_current_user_id(), '64'); ?> </a> <h4><?php bbp_user_profile_link(bbp_get_current_user_id()); ?> </h4> <div class="btn-group"> <a class="btn btn-default btn-sm" href="<?php bbp_user_profile_edit_url(bbp_get_current_user_id()); ?> " title="<?php printf(esc_attr__("Edit Your Profile", 'ipt_kb')); ?> "><span class="glyphicon glyphicon-edit"></span> <?php _e('Edit', 'bbpress'); ?> </a> <?php bbp_logout_link(); ?> </div> <div class="clearfix"></div> <div class="list-group"> <a href="<?php bbp_user_profile_url(bbp_get_current_user_id()); ?> " class="list-group-item bbp-user-forum-role <?php if (bbp_is_user_home() && bbp_is_single_user_profile()) { echo 'active'; } ?> "> <span class="glyphicon ipt-icomoon-user4"></span> <?php printf(__('%s Forum Role', 'ipt_kb'), '<span class="badge">' . bbp_get_user_display_role(bbp_get_current_user_id()) . '</span>'); ?> </a> <a href="<?php bbp_user_topics_created_url(bbp_get_current_user_id()); ?> " class="list-group-item bbp-user-topic-count <?php if (bbp_is_user_home() && bbp_is_single_user_topics()) { echo 'active'; } ?> "> <span class="glyphicon ipt-icomoon-bubbles4"></span> <?php printf(__('%s Topics Started', 'ipt_kb'), '<span class="badge">' . bbp_get_user_topic_count_raw(bbp_get_current_user_id()) . '</span>'); ?> </a> <a href="<?php bbp_user_replies_created_url(bbp_get_current_user_id()); ?> " class="list-group-item bbp-user-reply-count <?php if (bbp_is_user_home() && bbp_is_single_user_replies()) { echo 'active'; } ?> "> <span class="glyphicon ipt-icomoon-reply"></span> <?php printf(__('%s Replies Created', 'ipt_kb'), '<span class="badge">' . bbp_get_user_reply_count_raw(bbp_get_current_user_id()) . '</span>'); ?> </a> <?php if (bbp_is_favorites_active()) { ?> <a href="<?php bbp_favorites_permalink(bbp_get_current_user_id()); ?> " class="list-group-item bbp-user-favorite-count <?php if (bbp_is_user_home() && bbp_is_favorites()) { echo 'active'; } ?> " title="<?php printf(esc_attr__("Your Favorites", 'ipt_kb')); ?> "> <span class="glyphicon ipt-icomoon-heart"></span> <?php printf(__('%s Favorites', 'ipt_kb'), '<span class="badge">' . count(bbp_get_user_favorites_topic_ids(bbp_get_current_user_id())) . '</span>'); ?> </a> <?php } ?> <?php if (bbp_is_subscriptions_active()) { ?> <a href="<?php bbp_subscriptions_permalink(bbp_get_current_user_id()); ?> " class="list-group-item bbp-user-subscribe-count <?php if (bbp_is_user_home() && bbp_is_subscriptions()) { echo 'active'; } ?> " title="<?php printf(esc_attr__("Your Subscriptions", 'ipt_kb')); ?> "> <span class="glyphicon ipt-icomoon-bookmarks"></span> <?php printf(__('%s Subscriptions', 'ipt_kb'), '<span class="badge">' . count(bbp_get_user_subscribed_topic_ids(bbp_get_current_user_id())) . '</span>'); ?> </a> <?php } ?> </div> </div> <?php } echo $args['after_widget']; }
/** * @covers ::bbp_get_user_subscribed_topic_ids */ public function test_bbp_get_user_subscribed_topic_ids() { $u = $this->factory->user->create(); $t = $this->factory->topic->create_many(3); // Add topic subscriptions. bbp_add_user_topic_subscription($u, $t[0]); bbp_add_user_topic_subscription($u, $t[1]); bbp_add_user_topic_subscription($u, $t[2]); $subscriptions = bbp_get_user_subscribed_topic_ids($u); $this->assertEquals(array($t[0], $t[1], $t[2]), $subscriptions); // Remove topic subscription. bbp_remove_user_topic_subscription($u, $t[1]); $subscriptions = bbp_get_user_subscribed_topic_ids($u); $this->assertEquals(array($t[0], $t[2]), $subscriptions); }