/** * Return the link to subscribe/unsubscribe from a topic * * @since bbPress (r2668) * * @param mixed $args This function supports these arguments: * - subscribe: Subscribe text * - unsubscribe: Unsubscribe text * - user_id: User id * - topic_id: Topic id * - before: Before the link * - after: After the link * @param int $user_id Optional. User id * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">. * @uses bbp_get_user_id() To get the user id * @uses current_user_can() To check if the current user can edit user * @uses bbp_get_topic_id() To get the topic id * @uses bbp_is_user_subscribed() To check if the user is subscribed * @uses bbp_is_subscriptions() To check if it's the subscriptions page * @uses bbp_get_subscriptions_permalink() To get subscriptions link * @uses bbp_get_topic_permalink() To get topic link * @uses apply_filters() Calls 'bbp_get_user_subscribe_link' with the * link, args, user id & topic id * @return string Permanent link to topic */ function stachestack_bbp_get_user_subscribe_link($args = '', $user_id = 0, $wrap = true) { if (!bbp_is_subscriptions_active()) { return; } // Parse arguments against default values $r = bbp_parse_args($args, array('subscribe' => __('Subscribe', 'bbpress'), 'unsubscribe' => __('Unsubscribe', 'bbpress'), 'user_id' => 0, 'topic_id' => 0, 'before' => ' | ', 'after' => ''), 'get_user_subscribe_link'); // Validate user and topic ID's $user_id = bbp_get_user_id($r['user_id'], true, true); $topic_id = bbp_get_topic_id($r['topic_id']); if (empty($user_id) || empty($topic_id)) { return false; } // No link if you can't edit yourself if (!current_user_can('edit_user', (int) $user_id)) { return false; } // Decide which link to show $is_subscribed = bbp_is_user_subscribed($user_id, $topic_id); if (!empty($is_subscribed)) { $text = $r['unsubscribe']; $query_args = array('action' => 'bbp_unsubscribe', 'topic_id' => $topic_id); } else { $text = $r['subscribe']; $query_args = array('action' => 'bbp_subscribe', 'topic_id' => $topic_id); } // Create the link based where the user is and if the user is // subscribed already if (bbp_is_subscriptions()) { $permalink = bbp_get_subscriptions_permalink($user_id); } elseif (bbp_is_single_topic() || bbp_is_single_reply()) { $permalink = bbp_get_topic_permalink($topic_id); } else { $permalink = get_permalink(); } $url = esc_url(wp_nonce_url(add_query_arg($query_args, $permalink), 'toggle-subscription_' . $topic_id)); $sub = $is_subscribed ? ' class="is-subscribed"' : ''; $html = sprintf('%s<span id="subscribe-%d" %s><a href="%s" class="btn btn-warning btn-xs subscription-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after']); // Initial output is wrapped in a span, ajax output is hooked to this if (!empty($wrap)) { $html = '<span id="subscription-toggle">' . $html . '</span>'; } // Return the link return apply_filters('bbp_get_user_subscribe_link', $html, $r, $user_id, $topic_id); }
/** * Handles the front end subscribing and unsubscribing topics * * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_get_user_id() To get the user id * @uses bbp_verify_nonce_request() To verify the nonce and check the request * @uses current_user_can() To check if the current user can edit the user * @uses bbPress:errors:add() To log the error messages * @uses bbp_is_user_subscribed() To check if the topic is in user's * subscriptions * @uses bbp_remove_user_subscription() To remove the user subscription * @uses bbp_add_user_subscription() To add the user subscription * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id, * topic id and action * @uses bbp_is_subscription() To check if it's the subscription page * @uses bbp_get_subscription_link() To get the subscription page link * @uses bbp_get_topic_permalink() To get the topic permalink * @uses wp_safe_redirect() To redirect to the url */ function bbp_subscriptions_handler() { if (!bbp_is_subscriptions_active()) { return false; } // Bail if not a GET action if ('GET' !== strtoupper($_SERVER['REQUEST_METHOD'])) { return; } // Bail if required GET actions aren't passed if (empty($_GET['topic_id']) || empty($_GET['action'])) { return; } // Setup possible get actions $possible_actions = array('bbp_subscribe', 'bbp_unsubscribe'); // Bail if actions aren't meant for this function if (!in_array($_GET['action'], $possible_actions)) { return; } // Get required data $action = $_GET['action']; $user_id = bbp_get_user_id(0, true, true); $topic_id = intval($_GET['topic_id']); // Check for empty topic if (empty($topic_id)) { bbp_add_error('bbp_subscription_topic_id', __('<strong>ERROR</strong>: No topic was found! Which topic are you subscribing/unsubscribing to?', 'bbpress')); // Check nonce } elseif (!bbp_verify_nonce_request('toggle-subscription_' . $topic_id)) { bbp_add_error('bbp_subscription_topic_id', __('<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress')); // Check current user's ability to edit the user } elseif (!current_user_can('edit_user', $user_id)) { bbp_add_error('bbp_subscription_permissions', __('<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress')); } // Bail if we have errors if (bbp_has_errors()) { return; } /** No errors *************************************************************/ $is_subscription = bbp_is_user_subscribed($user_id, $topic_id); $success = false; if (true == $is_subscription && 'bbp_unsubscribe' == $action) { $success = bbp_remove_user_subscription($user_id, $topic_id); } elseif (false == $is_subscription && 'bbp_subscribe' == $action) { $success = bbp_add_user_subscription($user_id, $topic_id); } // Do additional subscriptions actions do_action('bbp_subscriptions_handler', $success, $user_id, $topic_id, $action); // Success! if (true == $success) { // Redirect back from whence we came if (bbp_is_subscriptions()) { $redirect = bbp_get_subscriptions_permalink($user_id); } elseif (bbp_is_single_user()) { $redirect = bbp_get_user_profile_url(); } elseif (is_singular(bbp_get_topic_post_type())) { $redirect = bbp_get_topic_permalink($topic_id); } elseif (is_single() || is_page()) { $redirect = get_permalink(); } wp_safe_redirect($redirect); // For good measure exit; // Fail! Handle errors } elseif (true == $is_subscription && 'bbp_unsubscribe' == $action) { bbp_add_error('bbp_unsubscribe', __('<strong>ERROR</strong>: There was a problem unsubscribing from that topic!', 'bbpress')); } elseif (false == $is_subscription && 'bbp_subscribe' == $action) { bbp_add_error('bbp_subscribe', __('<strong>ERROR</strong>: There was a problem subscribing to that topic!', 'bbpress')); } }
/** * Handle all the extra meta stuff from posting a new topic * * @param int $topic_id Optional. Topic id * @param int $forum_id Optional. Forum id * @param bool|array $anonymous_data Optional logged-out user data. * @param int $author_id Author id * @param bool $is_edit Optional. Is the post being edited? Defaults to false. * @uses bbp_get_topic_id() To get the topic id * @uses bbp_get_forum_id() To get the forum id * @uses bbp_get_current_user_id() To get the current user id * @yses bbp_get_topic_forum_id() To get the topic forum id * @uses update_post_meta() To update the topic metas * @uses set_transient() To update the flood check transient for the ip * @uses bbp_update_user_last_posted() To update the users last posted time * @uses bbp_is_subscriptions_active() To check if the subscriptions feature is * activated or not * @uses bbp_is_user_subscribed() To check if the user is subscribed * @uses bbp_remove_user_subscription() To remove the user's subscription * @uses bbp_add_user_subscription() To add the user's subscription * @uses bbp_update_topic_forum_id() To update the topic's forum id * @uses bbp_update_topic_topic_id() To update the topic's topic id * @uses bbp_update_topic_last_reply_id() To update the last reply id topic meta * @uses bbp_update_topic_last_active_id() To update the topic last active id * @uses bbp_update_topic_last_active_time() To update the last active topic meta * @uses bbp_update_topic_reply_count() To update the topic reply count * @uses bbp_update_topic_reply_count_hidden() To udpate the topic hidden reply count * @uses bbp_update_topic_voice_count() To update the topic voice count * @uses bbp_update_topic_walker() To udpate the topic's ancestors */ function bbp_update_topic($topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false) { // Validate the ID's passed from 'bbp_new_topic' action $topic_id = bbp_get_topic_id($topic_id); $forum_id = bbp_get_forum_id($forum_id); // Bail if there is no topic if (empty($topic_id)) { return; } // Check author_id if (empty($author_id)) { $author_id = bbp_get_current_user_id(); } // Check forum_id if (empty($forum_id)) { $forum_id = bbp_get_topic_forum_id($topic_id); } // If anonymous post, store name, email, website and ip in post_meta. // It expects anonymous_data to be sanitized. // Check bbp_filter_anonymous_post_data() for sanitization. if (!empty($anonymous_data) && is_array($anonymous_data)) { // Parse arguments against default values $r = bbp_parse_args($anonymous_data, array('bbp_anonymous_name' => '', 'bbp_anonymous_email' => '', 'bbp_anonymous_website' => ''), 'update_topic'); // Update all anonymous metas foreach ($r as $anon_key => $anon_value) { update_post_meta($topic_id, '_' . $anon_key, (string) $anon_value, false); } // Set transient for throttle check (only on new, not edit) if (empty($is_edit)) { set_transient('_bbp_' . bbp_current_author_ip() . '_last_posted', time()); } } else { if (empty($is_edit) && !current_user_can('throttle')) { bbp_update_user_last_posted($author_id); } } // Handle Subscription Checkbox if (bbp_is_subscriptions_active() && !empty($author_id)) { $subscribed = bbp_is_user_subscribed($author_id, $topic_id); $subscheck = !empty($_POST['bbp_topic_subscription']) && 'bbp_subscribe' === $_POST['bbp_topic_subscription'] ? true : false; // Subscribed and unsubscribing if (true === $subscribed && false === $subscheck) { bbp_remove_user_subscription($author_id, $topic_id); // Subscribing } elseif (false === $subscribed && true === $subscheck) { bbp_add_user_subscription($author_id, $topic_id); } } // Forum topic meta bbp_update_topic_forum_id($topic_id, $forum_id); bbp_update_topic_topic_id($topic_id, $topic_id); // Update associated topic values if this is a new topic if (empty($is_edit)) { // Update poster IP if not editing update_post_meta($topic_id, '_bbp_author_ip', bbp_current_author_ip(), false); // Last active time $last_active = current_time('mysql'); // Reply topic meta bbp_update_topic_last_reply_id($topic_id, 0); bbp_update_topic_last_active_id($topic_id, $topic_id); bbp_update_topic_last_active_time($topic_id, $last_active); bbp_update_topic_reply_count($topic_id, 0); bbp_update_topic_reply_count_hidden($topic_id, 0); bbp_update_topic_voice_count($topic_id); // Walk up ancestors and do the dirty work bbp_update_topic_walker($topic_id, $last_active, $forum_id, 0, false); } }
/** * AJAX handler to Subscribe/Unsubscribe a user from a topic * * @since bbPress (r3732) * * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_is_user_logged_in() To check if user is logged in * @uses bbp_get_current_user_id() To get the current user id * @uses current_user_can() To check if the current user can edit the user * @uses bbp_get_topic() To get the topic * @uses wp_verify_nonce() To verify the nonce * @uses bbp_is_user_subscribed() To check if the topic is in user's subscriptions * @uses bbp_remove_user_subscriptions() To remove the topic from user's subscriptions * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions * @uses bbp_ajax_response() To return JSON */ public function ajax_subscription() { // Bail if subscriptions are not active if (!bbp_is_subscriptions_active()) { bbp_ajax_response(false, __('Subscriptions are no longer active.', 'bbpress'), 300); } // Bail if user is not logged in if (!is_user_logged_in()) { bbp_ajax_response(false, __('Please login to subscribe to this topic.', 'bbpress'), 301); } // Get user and topic data $user_id = bbp_get_current_user_id(); $id = intval($_POST['id']); // Bail if user cannot add favorites for this user if (!current_user_can('edit_user', $user_id)) { bbp_ajax_response(false, __('You do not have permission to do this.', 'bbpress'), 302); } // Get the topic $topic = bbp_get_topic($id); // Bail if topic cannot be found if (empty($topic)) { bbp_ajax_response(false, __('The topic could not be found.', 'bbpress'), 303); } // Bail if user did not take this action if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'toggle-subscription_' . $topic->ID)) { bbp_ajax_response(false, __('Are you sure you meant to do that?', 'bbpress'), 304); } // Take action $status = bbp_is_user_subscribed($user_id, $topic->ID) ? bbp_remove_user_subscription($user_id, $topic->ID) : bbp_add_user_subscription($user_id, $topic->ID); // Bail if action failed if (empty($status)) { bbp_ajax_response(false, __('The request was unsuccessful. Please try again.', 'bbpress'), 305); } // Put subscription attributes in convenient array $attrs = array('topic_id' => $topic->ID, 'user_id' => $user_id); // Action succeeded bbp_ajax_response(true, bbp_get_user_subscribe_link($attrs, $user_id, false), 200); }
/** * Subscribe/Unsubscribe a user from a topic * * @since bbPress (r2668) * * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_get_current_user_id() To get the current user id * @uses current_user_can() To check if the current user can edit the user * @uses bbp_get_topic() To get the topic * @uses check_ajax_referer() To verify the nonce & check the referer * @uses bbp_is_user_subscribed() To check if the topic is in user's * subscriptions * @uses bbp_remove_user_subscriptions() To remove the topic from user's * subscriptions * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions */ function bbp_skeleton_dim_subscription() { if (!bbp_is_subscriptions_active()) { return; } $user_id = bbp_get_current_user_id(); $id = intval($_POST['id']); if (!current_user_can('edit_user', $user_id)) { die('-1'); } if (!($topic = bbp_get_topic($id))) { die('0'); } check_ajax_referer("toggle-subscription_{$topic->ID}"); if (bbp_is_user_subscribed($user_id, $topic->ID)) { if (bbp_remove_user_subscription($user_id, $topic->ID)) { die('1'); } } else { if (bbp_add_user_subscription($user_id, $topic->ID)) { die('1'); } } die('0'); }
/** * Subscribe/Unsubscribe a user from a topic * * @since bbPress (r3732) * * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_get_current_user_id() To get the current user id * @uses current_user_can() To check if the current user can edit the user * @uses bbp_get_topic() To get the topic * @uses check_ajax_referer() To verify the nonce & check the referer * @uses bbp_is_user_subscribed() To check if the topic is in user's * subscriptions * @uses bbp_remove_user_subscriptions() To remove the topic from user's * subscriptions * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions */ public function ajax_subscription() { if (!bbp_is_subscriptions_active()) { return; } $user_id = bbp_get_current_user_id(); $id = intval($_POST['id']); if (!current_user_can('edit_user', $user_id)) { die('-1'); } $topic = bbp_get_topic($id); if (empty($topic)) { die('0'); } check_ajax_referer('toggle-subscription_' . $topic->ID); if (bbp_is_user_subscribed($user_id, $topic->ID)) { if (bbp_remove_user_subscription($user_id, $topic->ID)) { die('1'); } } else { if (bbp_add_user_subscription($user_id, $topic->ID)) { die('1'); } } die('0'); }
/** * Return the link to subscribe/unsubscribe from a topic * * @since bbPress (r2668) * * @param mixed $args This function supports these arguments: * - subscribe: Subscribe text * - unsubscribe: Unsubscribe text * - user_id: User id * - topic_id: Topic id * - before: Before the link * - after: After the link * @param int $user_id Optional. User id * @uses bbp_get_user_id() To get the user id * @uses current_user_can() To check if the current user can edit user * @uses bbp_get_topic_id() To get the topic id * @uses bbp_is_user_subscribed() To check if the user is subscribed * @uses bbp_is_subscriptions() To check if it's the subscriptions page * @uses bbp_get_subscriptions_permalink() To get subscriptions link * @uses bbp_get_topic_permalink() To get topic link * @uses apply_filters() Calls 'bbp_get_user_subscribe_link' with the * link, args, user id & topic id * @return string Permanent link to topic */ function bbp_get_user_subscribe_link($args = '', $user_id = 0) { if (!bbp_is_subscriptions_active()) { return; } $defaults = array('subscribe' => __('Subscribe', 'bbpress'), 'unsubscribe' => __('Unsubscribe', 'bbpress'), 'user_id' => 0, 'topic_id' => 0, 'before' => ' | ', 'after' => ''); $args = bbp_parse_args($args, $defaults, 'get_user_subscribe_link'); extract($args); // Validate user and topic ID's $user_id = bbp_get_user_id($user_id, true, true); $topic_id = bbp_get_topic_id($topic_id); if (empty($user_id) || empty($topic_id)) { return false; } // No link if you can't edit yourself if (!current_user_can('edit_user', (int) $user_id)) { return false; } // Decine which link to show $is_subscribed = bbp_is_user_subscribed($user_id, $topic_id); if (!empty($is_subscribed)) { $text = $unsubscribe; $query_args = array('action' => 'bbp_unsubscribe', 'topic_id' => $topic_id); } else { $text = $subscribe; $query_args = array('action' => 'bbp_subscribe', 'topic_id' => $topic_id); } // Create the link based where the user is and if the user is // subscribed already if (bbp_is_subscriptions()) { $permalink = bbp_get_subscriptions_permalink($user_id); } elseif (is_singular(bbp_get_topic_post_type())) { $permalink = bbp_get_topic_permalink($topic_id); } elseif (is_singular(bbp_get_reply_post_type())) { $permalink = bbp_get_topic_permalink($topic_id); } elseif (bbp_is_query_name('bbp_single_topic')) { $permalink = get_permalink(); } $url = esc_url(wp_nonce_url(add_query_arg($query_args, $permalink), 'toggle-subscription_' . $topic_id)); $is_subscribed = $is_subscribed ? 'is-subscribed' : ''; $html = '<span id="subscription-toggle">' . $before . '<span id="subscribe-' . $topic_id . '" class="' . $is_subscribed . '"><a href="' . $url . '" class="dim:subscription-toggle:subscribe-' . $topic_id . ':is-subscribed">' . $text . '</a></span>' . $after . '</span>'; // Return the link return apply_filters('bbp_get_user_subscribe_link', $html, $args, $user_id, $topic_id); }
/** * Return checked value of topic subscription * * @since bbPress (r2976) * * @uses bbp_is_topic_edit() To check if it's the topic edit page * @uses bbp_is_user_subscribed() To check if the user is subscribed to * the topic * @uses apply_filters() Calls 'bbp_get_form_topic_subscribed' with the * option * @return string Checked value of topic subscription */ function bbp_get_form_topic_subscribed() { // Get _POST data if ('post' == strtolower($_SERVER['REQUEST_METHOD']) && isset($_POST['bbp_topic_subscription'])) { $topic_subscribed = $_POST['bbp_topic_subscription']; // Get edit data } elseif (bbp_is_topic_edit() || bbp_is_reply_edit()) { // Get current posts author $post_author = bbp_get_global_post_field('post_author', 'raw'); // Post author is not the current user if (bbp_get_current_user_id() != $post_author) { $topic_subscribed = bbp_is_user_subscribed($post_author); // Post author is the current user } else { $topic_subscribed = bbp_is_user_subscribed(bbp_get_current_user_id()); } // Get current status } elseif (bbp_is_single_topic()) { $topic_subscribed = bbp_is_user_subscribed(bbp_get_current_user_id()); // No data } else { $topic_subscribed = 0; } // Get checked output $checked = checked($topic_subscribed, true, false); return apply_filters('bbp_get_form_topic_subscribed', $checked, $topic_subscribed); }
/** * @covers ::bbp_is_user_subscribed */ public function test_bbp_is_user_subscribed() { $u = $this->factory->user->create(); $f = $this->factory->forum->create_many(2); $t = $this->factory->topic->create_many(2); // Add forum subscription. bbp_add_user_forum_subscription($u, $f[0]); $this->assertTrue(bbp_is_user_subscribed($u, $f[0])); $this->assertFalse(bbp_is_user_subscribed($u, $f[1])); // Add topic subscription. bbp_add_user_topic_subscription($u, $t[0]); $this->assertTrue(bbp_is_user_subscribed($u, $t[0])); $this->assertFalse(bbp_is_user_subscribed($u, $t[1])); }
/** * Perform the export * * @access public * @since 1.0 * @uses bbp_Export::csv_rows_out() * @return void */ public function import() { if (!$this->can_import()) { wp_die(__('You do not have permission to import data.', 'bbp-export-import'), __('Error', 'bbp-export-import')); } $csv_array = $this->csv_to_array($_FILES['bbp_csv_file']['tmp_name'], ';'); foreach ($csv_array as $key => $line) { $post_args = $line; $meta_args = array(); // Setup author date if ($post_args['anonymous'] == '1') { $meta_args['anonymous_email'] = $line['post_author']; } else { $user = get_user_by('email', $post_args['post_author']); if (!$user) { // The user doesn't exist, so create them $user = wp_insert_user(array('user_email' => $post_args['post_author'], 'user_login' => $post_args['user_login'])); } $post_args['post_author'] = $user->ID; } // Decode content $post_args['post_content'] = html_entity_decode($post_args['post_content']); $topic_type = bbp_get_topic_post_type(); $reply_type = bbp_get_reply_post_type(); // Remove the post args we don't want sent to wp_insert_post unset($post_args['anonymous']); unset($post_args['user_login']); switch ($line['post_type']) { case $topic_type: // Set the forum parent for topics $post_args['post_parent'] = $this->forum_id; $meta_args['voice_count'] = $line['voices']; $meta_args['reply_count'] = $post_args['reply_count']; $topic_id = bbp_insert_topic($post_args, $meta_args); // Subscribe the original poster to the topic bbp_add_user_subscription($post_args['post_author'], $topic_id); // Add the topic to the user's favorites if (bbp_is_user_favorite($post_args['post_author'], $topic_id)) { bbp_add_user_favorite($post_args['post_author'], $topic_id); } // Set topic as resolved if GetShopped Support Forum is active if ($post_args['resolved'] == '1') { add_post_meta($topic_id, '_bbps_topic_status', '2'); } break; case $reply_type: // Set the forum parent for replies. The topic ID is created above when the replie's topic is first created $post_args['post_parent'] = $topic_id; $reply_id = bbp_insert_reply($post_args, $meta_args); // Subscribe reply author, if not already if (!bbp_is_user_subscribed($post_args['post_author'], $topic_id)) { bbp_add_user_subscription($post_args['post_author'], $topic_id); } // Mark as favorite if (bbp_is_user_favorite($post_args['post_author'], $topic_id)) { bbp_add_user_favorite($post_args['post_author'], $topic_id); } // Check if the next row is a topic, meaning we have reached the last reply and need to update the last active time if ($csv_array[$key + 1]['post_type'] == bbp_get_topic_post_type()) { bbp_update_forum_last_active_time($this->forum_id, $post_args['post_date']); } break; } } // Recount forum topic / reply counts bbp_admin_repair_forum_topic_count(); bbp_admin_repair_forum_reply_count(); wp_redirect(admin_url('post.php?post=' . $this->forum_id . '&action=edit')); exit; }