コード例 #1
0
/**
 * Split topic handler
 *
 * Handles the front end split topic submission
 *
 * @since bbPress (r2756)
 *
 * @param string $action The requested action to compare this function to
 * @uses bbp_add_error() To add an error message
 * @uses bbp_get_reply() To get the reply
 * @uses bbp_get_topic() To get the topics
 * @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 topics
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
 * @uses do_action() Calls 'bbp_pre_split_topic' with the from reply id, source
 *                    and destination topic ids
 * @uses bbp_get_topic_subscribers() To get the source topic subscribers
 * @uses bbp_add_user_subscription() To add the user subscription
 * @uses bbp_get_topic_favoriters() To get the source topic favoriters
 * @uses bbp_add_user_favorite() To add the user favorite
 * @uses wp_get_post_terms() To get the source topic tags
 * @uses wp_set_post_terms() To set the topic tags
 * @uses bbp_get_reply_post_type() To get the reply post type
 * @uses wpdb::prepare() To prepare our sql query
 * @uses wpdb::get_results() To execute the sql query and get results
 * @uses wp_update_post() To update the replies
 * @uses bbp_update_reply_topic_id() To update the reply topic id
 * @uses bbp_get_topic_forum_id() To get the topic forum id
 * @uses bbp_update_reply_forum_id() To update the reply forum id
 * @uses do_action() Calls 'bbp_split_topic_reply' with the reply id and
 *                    destination topic id
 * @uses bbp_update_topic_last_reply_id() To update the topic last reply id
 * @uses bbp_update_topic_last_active_time() To update the topic last active meta
 * @uses do_action() Calls 'bbp_post_split_topic' with the destination and
 *                    source topic ids and source topic's forum id
 * @uses bbp_get_topic_permalink() To get the topic permalink
 * @uses wp_safe_redirect() To redirect to the topic link
 */
function bbp_split_topic_handler($action = '')
{
    // Bail if action is not 'bbp-split-topic'
    if ('bbp-split-topic' !== $action) {
        return;
    }
    global $wpdb;
    // Prevent debug notices
    $from_reply_id = $destination_topic_id = 0;
    $destination_topic_title = '';
    $destination_topic = $from_reply = $source_topic = '';
    $split_option = false;
    /** Split Reply ***********************************************************/
    if (empty($_POST['bbp_reply_id'])) {
        bbp_add_error('bbp_split_topic_reply_id', __('<strong>ERROR</strong>: Reply ID to split the topic from not found!', 'bbpress'));
    } else {
        $from_reply_id = (int) $_POST['bbp_reply_id'];
    }
    $from_reply = bbp_get_reply($from_reply_id);
    // Reply exists
    if (empty($from_reply)) {
        bbp_add_error('bbp_split_topic_r_not_found', __('<strong>ERROR</strong>: The reply you want to split from was not found.', 'bbpress'));
    }
    /** Topic to Split ********************************************************/
    // Get the topic being split
    $source_topic = bbp_get_topic($from_reply->post_parent);
    // No topic
    if (empty($source_topic)) {
        bbp_add_error('bbp_split_topic_source_not_found', __('<strong>ERROR</strong>: The topic you want to split was not found.', 'bbpress'));
    }
    // Nonce check failed
    if (!bbp_verify_nonce_request('bbp-split-topic_' . $source_topic->ID)) {
        bbp_add_error('bbp_split_topic_nonce', __('<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress'));
        return;
    }
    // Use cannot edit topic
    if (!current_user_can('edit_topic', $source_topic->ID)) {
        bbp_add_error('bbp_split_topic_source_permission', __('<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress'));
    }
    // How to Split
    if (!empty($_POST['bbp_topic_split_option'])) {
        $split_option = (string) trim($_POST['bbp_topic_split_option']);
    }
    // Invalid split option
    if (empty($split_option) || !in_array($split_option, array('existing', 'reply'))) {
        bbp_add_error('bbp_split_topic_option', __('<strong>ERROR</strong>: You need to choose a valid split option.', 'bbpress'));
        // Valid Split Option
    } else {
        // What kind of split
        switch ($split_option) {
            // Into an existing topic
            case 'existing':
                // Get destination topic id
                if (empty($_POST['bbp_destination_topic'])) {
                    bbp_add_error('bbp_split_topic_destination_id', __('<strong>ERROR</strong>: Destination topic ID not found!', 'bbpress'));
                } else {
                    $destination_topic_id = (int) $_POST['bbp_destination_topic'];
                }
                // Get the destination topic
                $destination_topic = bbp_get_topic($destination_topic_id);
                // No destination topic
                if (empty($destination_topic)) {
                    bbp_add_error('bbp_split_topic_destination_not_found', __('<strong>ERROR</strong>: The topic you want to split to was not found!', 'bbpress'));
                }
                // User cannot edit the destination topic
                if (!current_user_can('edit_topic', $destination_topic->ID)) {
                    bbp_add_error('bbp_split_topic_destination_permission', __('<strong>ERROR</strong>: You do not have the permissions to edit the destination topic!', 'bbpress'));
                }
                break;
                // Split at reply into a new topic
            // Split at reply into a new topic
            case 'reply':
            default:
                // User needs to be able to publish topics
                if (current_user_can('publish_topics')) {
                    // Use the new title that was passed
                    if (!empty($_POST['bbp_topic_split_destination_title'])) {
                        $destination_topic_title = esc_attr(strip_tags($_POST['bbp_topic_split_destination_title']));
                        // Use the source topic title
                    } else {
                        $destination_topic_title = $source_topic->post_title;
                    }
                    // Update the topic
                    $destination_topic_id = wp_update_post(array('ID' => $from_reply->ID, 'post_title' => $destination_topic_title, 'post_name' => false, 'post_type' => bbp_get_topic_post_type(), 'post_parent' => $source_topic->post_parent, 'menu_order' => 0, 'guid' => ''));
                    $destination_topic = bbp_get_topic($destination_topic_id);
                    // Make sure the new topic knows its a topic
                    bbp_update_topic_topic_id($from_reply->ID);
                    // Shouldn't happen
                    if (false === $destination_topic_id || is_wp_error($destination_topic_id) || empty($destination_topic)) {
                        bbp_add_error('bbp_split_topic_destination_reply', __('<strong>ERROR</strong>: There was a problem converting the reply into the topic. Please try again.', 'bbpress'));
                    }
                    // User cannot publish posts
                } else {
                    bbp_add_error('bbp_split_topic_destination_permission', __('<strong>ERROR</strong>: You do not have the permissions to create new topics. The reply could not be converted into a topic.', 'bbpress'));
                }
                break;
        }
    }
    // Bail if there are errors
    if (bbp_has_errors()) {
        return;
    }
    /** No Errors - Do the Spit ***********************************************/
    // Update counts, etc...
    do_action('bbp_pre_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID);
    /** Date Check ************************************************************/
    // Check if the destination topic is older than the from reply
    if (strtotime($from_reply->post_date) < strtotime($destination_topic->post_date)) {
        // Set destination topic post_date to 1 second before from reply
        $destination_post_date = date('Y-m-d H:i:s', strtotime($from_reply->post_date) - 1);
        // Update destination topic
        wp_update_post(array('ID' => $destination_topic_id, 'post_date' => $destination_post_date, 'post_date_gmt' => get_gmt_from_date($destination_post_date)));
    }
    /** Subscriptions *********************************************************/
    // Copy the subscribers
    if (!empty($_POST['bbp_topic_subscribers']) && "1" === $_POST['bbp_topic_subscribers'] && bbp_is_subscriptions_active()) {
        // Get the subscribers
        $subscribers = bbp_get_topic_subscribers($source_topic->ID);
        if (!empty($subscribers)) {
            // Add subscribers to new topic
            foreach ((array) $subscribers as $subscriber) {
                bbp_add_user_subscription($subscriber, $destination_topic->ID);
            }
        }
    }
    /** Favorites *************************************************************/
    // Copy the favoriters if told to
    if (!empty($_POST['bbp_topic_favoriters']) && "1" === $_POST['bbp_topic_favoriters']) {
        // Get the favoriters
        $favoriters = bbp_get_topic_favoriters($source_topic->ID);
        if (!empty($favoriters)) {
            // Add the favoriters to new topic
            foreach ((array) $favoriters as $favoriter) {
                bbp_add_user_favorite($favoriter, $destination_topic->ID);
            }
        }
    }
    /** Tags ******************************************************************/
    // Copy the tags if told to
    if (!empty($_POST['bbp_topic_tags']) && "1" === $_POST['bbp_topic_tags']) {
        // Get the source topic tags
        $source_topic_tags = wp_get_post_terms($source_topic->ID, bbp_get_topic_tag_tax_id(), array('fields' => 'names'));
        if (!empty($source_topic_tags)) {
            wp_set_post_terms($destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true);
        }
    }
    /** Split Replies *********************************************************/
    // get_posts() is not used because it doesn't allow us to use '>='
    // comparision without a filter.
    $replies = (array) $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_date >= %s AND {$wpdb->posts}.post_parent = %d AND {$wpdb->posts}.post_type = %s ORDER BY {$wpdb->posts}.post_date ASC", $from_reply->post_date, $source_topic->ID, bbp_get_reply_post_type()));
    // Make sure there are replies to loop through
    if (!empty($replies) && !is_wp_error($replies)) {
        // Calculate starting point for reply positions
        switch ($split_option) {
            // Get topic reply count for existing topic
            case 'existing':
                $reply_position = bbp_get_topic_reply_count($destination_topic->ID);
                break;
                // Account for new lead topic
            // Account for new lead topic
            case 'reply':
                $reply_position = 1;
                break;
        }
        // Save reply ids
        $reply_ids = array();
        // Change the post_parent of each reply to the destination topic id
        foreach ($replies as $reply) {
            // Bump the reply position each iteration through the loop
            $reply_position++;
            // Update the reply
            wp_update_post(array('ID' => $reply->ID, 'post_title' => sprintf(__('Reply To: %s', 'bbpress'), $destination_topic->post_title), 'post_name' => false, 'post_parent' => $destination_topic->ID, 'menu_order' => $reply_position, 'guid' => ''));
            // Gather reply ids
            $reply_ids[] = $reply->ID;
            // Adjust reply meta values
            bbp_update_reply_topic_id($reply->ID, $destination_topic->ID);
            bbp_update_reply_forum_id($reply->ID, bbp_get_topic_forum_id($destination_topic->ID));
            // Adjust reply to values
            $reply_to = bbp_get_reply_to($reply->ID);
            // Not a reply to a reply that moved over
            if (!in_array($reply_to, $reply_ids)) {
                bbp_update_reply_to($reply->ID, 0);
            }
            // New topic from reply can't be a reply to
            if ($from_reply->ID === $destination_topic->ID && $from_reply->ID === $reply_to) {
                bbp_update_reply_to($reply->ID, 0);
            }
            // Do additional actions per split reply
            do_action('bbp_split_topic_reply', $reply->ID, $destination_topic->ID);
        }
        // Remove reply to from new topic
        if ($from_reply->ID === $destination_topic->ID) {
            delete_post_meta($from_reply->ID, '_bbp_reply_to');
        }
        // Set the last reply ID and freshness
        $last_reply_id = $reply->ID;
        $freshness = $reply->post_date;
        // Set the last reply ID and freshness to the from_reply
    } else {
        $last_reply_id = $from_reply->ID;
        $freshness = $from_reply->post_date;
    }
    // It is a new topic and we need to set some default metas to make
    // the topic display in bbp_has_topics() list
    if ('reply' === $split_option) {
        bbp_update_topic_last_reply_id($destination_topic->ID, $last_reply_id);
        bbp_update_topic_last_active_id($destination_topic->ID, $last_reply_id);
        bbp_update_topic_last_active_time($destination_topic->ID, $freshness);
    }
    // Update source topic ID last active
    bbp_update_topic_last_reply_id($source_topic->ID);
    bbp_update_topic_last_active_id($source_topic->ID);
    bbp_update_topic_last_active_time($source_topic->ID);
    /** Successful Split ******************************************************/
    // Update counts, etc...
    do_action('bbp_post_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID);
    // Redirect back to the topic
    wp_safe_redirect(bbp_get_topic_permalink($destination_topic->ID));
    // For good measure
    exit;
}
コード例 #2
0
/**
 * Handle all the extra meta stuff from posting a new reply or editing a reply
 *
 * @param int $reply_id Optional. Reply id
 * @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.
 * @param int $reply_to Optional. Reply to id
 * @uses bbp_get_reply_id() To get the reply id
 * @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
 * @uses bbp_get_reply_topic_id() To get the reply topic id
 * @uses bbp_get_topic_forum_id() To get the topic forum id
 * @uses update_post_meta() To update the reply 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_reply_forum_id() To update the reply forum id
 * @uses bbp_update_reply_topic_id() To update the reply topic id
 * @uses bbp_update_reply_to() To update the reply to id
 * @uses bbp_update_reply_walker() To update the reply's ancestors' counts
 */
function bbp_update_reply($reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false, $reply_to = 0)
{
    // Validate the ID's passed from 'bbp_new_reply' action
    $reply_id = bbp_get_reply_id($reply_id);
    $topic_id = bbp_get_topic_id($topic_id);
    $forum_id = bbp_get_forum_id($forum_id);
    $reply_to = bbp_validate_reply_to($reply_to);
    // Bail if there is no reply
    if (empty($reply_id)) {
        return;
    }
    // Check author_id
    if (empty($author_id)) {
        $author_id = bbp_get_current_user_id();
    }
    // Check topic_id
    if (empty($topic_id)) {
        $topic_id = bbp_get_reply_topic_id($reply_id);
    }
    // Check forum_id
    if (!empty($topic_id) && 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_reply');
        // Update all anonymous metas
        foreach ($r as $anon_key => $anon_value) {
            update_post_meta($reply_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) && !empty($topic_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);
        }
    }
    // Reply meta relating to reply position in tree
    bbp_update_reply_forum_id($reply_id, $forum_id);
    bbp_update_reply_topic_id($reply_id, $topic_id);
    bbp_update_reply_to($reply_id, $reply_to);
    // Update associated topic values if this is a new reply
    if (empty($is_edit)) {
        // Update poster IP if not editing
        update_post_meta($reply_id, '_bbp_author_ip', bbp_current_author_ip(), false);
        // Last active time
        $last_active_time = current_time('mysql');
        // Walk up ancestors and do the dirty work
        bbp_update_reply_walker($reply_id, $last_active_time, $forum_id, $topic_id, false);
    }
}
コード例 #3
0
ファイル: functions.php プロジェクト: hscale/webento
/**
 * 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'));
    }
}
コード例 #4
0
ファイル: functions.php プロジェクト: besimhu/legacy
 /**
  * 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');
 }
コード例 #5
0
function bbps_assign_topic()
{
    $user_id = $_POST['bbps_assign_list'];
    $topic_id = $_POST['bbps_topic_id'];
    if ($user_id > 0) {
        $userinfo = get_userdata($user_id);
        $user_email = $userinfo->user_email;
        $post_link = get_permalink($topic_id);
        //add the user as a subscriber to the topic and send them an email to let them know they have been assigned to a topic
        bbp_add_user_subscription($user_id, $topic_id);
        /*update the post meta with the assigned users id*/
        $assigned = update_post_meta($topic_id, 'bbps_topic_assigned', $user_id);
        $message = <<<EMAILMSG
\t\tYou have been assigned to the following topic, by another forum moderator or the site administrator. Please take a look at it when you get a chance.
\t\t{$post_link}
EMAILMSG;
        if ($assigned == true) {
            wp_mail($user_email, 'A forum topic has been assigned to you', $message);
        }
    }
}
コード例 #6
0
 /**
  * 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);
 }
コード例 #7
0
ファイル: bbPress.php プロジェクト: rmccue/Falcon
 public function handle_insert($value, Falcon_Reply $reply)
 {
     if (!empty($value)) {
         return $value;
     }
     $post = get_post($reply->post);
     if (!$this->is_allowed_type($post->post_type)) {
         return $value;
     }
     $user = $reply->get_user();
     if ($reply->is_valid()) {
         Falcon::notify_invalid($user, bbp_get_topic_title($reply->post));
         return false;
     }
     $new_reply = array('post_parent' => $reply->post, 'post_author' => $user->ID, 'post_content' => $reply->parse_body(), 'post_title' => $reply->subject);
     $meta = array('author_ip' => '127.0.0.1', 'forum_id' => bbp_get_topic_forum_id($reply->post), 'topic_id' => $reply->post);
     $reply_id = bbp_insert_reply($new_reply, $meta);
     do_action('bbp_new_reply', $reply_id, $meta['topic_id'], $meta['forum_id'], false, $new_reply['post_author']);
     // bbPress removes the user's subscription because bbp_update_reply() is hooked to 'bbp_new_reply' and it checks for $_POST['bbp_topic_subscription']
     bbp_add_user_subscription($new_reply['post_author'], $meta['topic_id']);
     return $reply_id;
 }
コード例 #8
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');
 }
コード例 #9
0
ファイル: subscriptions.php プロジェクト: joeyblake/bbpress
 /**
  * @covers ::bbp_remove_user_subscription
  */
 public function test_bbp_remove_user_subscription()
 {
     $u = $this->factory->user->create();
     $f = $this->factory->forum->create();
     $t = $this->factory->topic->create(array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f)));
     // Add forum subscription.
     bbp_add_user_subscription($u, $f);
     $this->assertTrue(bbp_is_user_subscribed_to_forum($u, $f));
     // Remove forum subscription.
     bbp_remove_user_subscription($u, $f);
     $this->assertFalse(bbp_is_user_subscribed_to_forum($u, $f));
     // Add topic subscription.
     bbp_add_user_subscription($u, $t);
     $this->assertTrue(bbp_is_user_subscribed_to_topic($u, $t));
     // Remove topic subscription.
     bbp_remove_user_subscription($u, $t);
     $this->assertFalse(bbp_is_user_subscribed_to_topic($u, $t));
 }
コード例 #10
0
 /**
  * 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;
 }