예제 #1
0
/**
 * Handles the front end edit topic submission
 *
 * @param string $action The requested action to compare this function to
 * @uses bbp_add_error() To add an error message
 * @uses bbp_get_topic() To get the topic
 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
 * @uses bbp_is_topic_anonymous() To check if topic is by an anonymous user
 * @uses current_user_can() To check if the current user can edit the topic
 * @uses bbp_filter_anonymous_post_data() To filter anonymous data
 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
 * @uses esc_attr() For sanitization
 * @uses bbp_is_forum_category() To check if the forum is a category
 * @uses bbp_is_forum_closed() To check if the forum is closed
 * @uses bbp_is_forum_private() To check if the forum is private
 * @uses remove_filter() To remove kses filters if needed
 * @uses apply_filters() Calls 'bbp_edit_topic_pre_title' with the title and
 *                        topic id
 * @uses apply_filters() Calls 'bbp_edit_topic_pre_content' with the content
 *                        and topic id
 * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
 * @uses wp_save_post_revision() To save a topic revision
 * @uses bbp_update_topic_revision_log() To update the topic revision log
 * @uses bbp_stick_topic() To stick or super stick the topic
 * @uses bbp_unstick_topic() To unstick the topic
 * @uses wp_update_post() To update the topic
 * @uses do_action() Calls 'bbp_edit_topic' with the topic id, forum id,
 *                    anonymous data and reply author
 * @uses bbp_move_topic_handler() To handle movement of a topic from one forum
 *                                 to another
 * @uses bbp_get_topic_permalink() To get the topic permalink
 * @uses wp_safe_redirect() To redirect to the topic link
 * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
 *                                              messages
 */
function bbp_edit_topic_handler($action = '')
{
    // Bail if action is not bbp-edit-topic
    if ('bbp-edit-topic' !== $action) {
        return;
    }
    // Define local variable(s)
    $revisions_removed = false;
    $topic = $topic_id = $topic_author = $forum_id = $anonymous_data = 0;
    $topic_title = $topic_content = $topic_edit_reason = '';
    /** Topic *****************************************************************/
    // Topic id was not passed
    if (empty($_POST['bbp_topic_id'])) {
        bbp_add_error('bbp_edit_topic_id', __('<strong>ERROR</strong>: Topic ID not found.', 'bbpress'));
        return;
        // Topic id was passed
    } elseif (is_numeric($_POST['bbp_topic_id'])) {
        $topic_id = (int) $_POST['bbp_topic_id'];
        $topic = bbp_get_topic($topic_id);
    }
    // Topic does not exist
    if (empty($topic)) {
        bbp_add_error('bbp_edit_topic_not_found', __('<strong>ERROR</strong>: The topic you want to edit was not found.', 'bbpress'));
        return;
        // Topic exists
    } else {
        // Check users ability to create new topic
        if (!bbp_is_topic_anonymous($topic_id)) {
            // User cannot edit this topic
            if (!current_user_can('edit_topic', $topic_id)) {
                bbp_add_error('bbp_edit_topic_permissions', __('<strong>ERROR</strong>: You do not have permission to edit that topic.', 'bbpress'));
            }
            // Set topic author
            $topic_author = bbp_get_topic_author_id($topic_id);
            // It is an anonymous post
        } else {
            // Filter anonymous data
            $anonymous_data = bbp_filter_anonymous_post_data(array(), true);
        }
    }
    // Nonce check
    if (!bbp_verify_nonce_request('bbp-edit-topic_' . $topic_id)) {
        bbp_add_error('bbp_edit_topic_nonce', __('<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress'));
        return;
    }
    // Remove kses filters from title and content for capable users and if the nonce is verified
    if (current_user_can('unfiltered_html') && !empty($_POST['_bbp_unfiltered_html_topic']) && wp_create_nonce('bbp-unfiltered-html-topic_' . $topic_id) === $_POST['_bbp_unfiltered_html_topic']) {
        remove_filter('bbp_edit_topic_pre_title', 'wp_filter_kses');
        remove_filter('bbp_edit_topic_pre_content', 'bbp_encode_bad', 10);
        remove_filter('bbp_edit_topic_pre_content', 'bbp_filter_kses', 30);
    }
    /** Topic Forum ***********************************************************/
    // Forum id was not passed
    if (empty($_POST['bbp_forum_id'])) {
        bbp_add_error('bbp_topic_forum_id', __('<strong>ERROR</strong>: Forum ID is missing.', 'bbpress'));
        // Forum id was passed
    } elseif (is_numeric($_POST['bbp_forum_id'])) {
        $forum_id = (int) $_POST['bbp_forum_id'];
    }
    // Current forum this topic is in
    $current_forum_id = bbp_get_topic_forum_id($topic_id);
    // Forum exists
    if (!empty($forum_id) && $forum_id !== $current_forum_id) {
        // Forum is a category
        if (bbp_is_forum_category($forum_id)) {
            bbp_add_error('bbp_edit_topic_forum_category', __('<strong>ERROR</strong>: This forum is a category. No topics can be created in it.', 'bbpress'));
            // Forum is not a category
        } else {
            // Forum is closed and user cannot access
            if (bbp_is_forum_closed($forum_id) && !current_user_can('edit_forum', $forum_id)) {
                bbp_add_error('bbp_edit_topic_forum_closed', __('<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress'));
            }
            // Forum is private and user cannot access
            if (bbp_is_forum_private($forum_id)) {
                if (!current_user_can('read_private_forums')) {
                    bbp_add_error('bbp_edit_topic_forum_private', __('<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress'));
                }
                // Forum is hidden and user cannot access
            } elseif (bbp_is_forum_hidden($forum_id)) {
                if (!current_user_can('read_hidden_forums')) {
                    bbp_add_error('bbp_edit_topic_forum_hidden', __('<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress'));
                }
            }
        }
    }
    /** Topic Title ***********************************************************/
    if (!empty($_POST['bbp_topic_title'])) {
        $topic_title = esc_attr(strip_tags($_POST['bbp_topic_title']));
    }
    // Filter and sanitize
    $topic_title = apply_filters('bbp_edit_topic_pre_title', $topic_title, $topic_id);
    // No topic title
    if (empty($topic_title)) {
        bbp_add_error('bbp_edit_topic_title', __('<strong>ERROR</strong>: Your topic needs a title.', 'bbpress'));
    }
    /** Topic Content *********************************************************/
    if (!empty($_POST['bbp_topic_content'])) {
        $topic_content = $_POST['bbp_topic_content'];
    }
    // Filter and sanitize
    $topic_content = apply_filters('bbp_edit_topic_pre_content', $topic_content, $topic_id);
    // No topic content
    if (empty($topic_content)) {
        bbp_add_error('bbp_edit_topic_content', __('<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress'));
    }
    /** Topic Blacklist *******************************************************/
    if (!bbp_check_for_blacklist($anonymous_data, $topic_author, $topic_title, $topic_content)) {
        bbp_add_error('bbp_topic_blacklist', __('<strong>ERROR</strong>: Your topic cannot be edited at this time.', 'bbpress'));
    }
    /** Topic Status **********************************************************/
    // Maybe put into moderation
    if (!bbp_check_for_moderation($anonymous_data, $topic_author, $topic_title, $topic_content)) {
        // Set post status to pending if public or closed
        if (in_array($topic->post_status, array(bbp_get_public_status_id(), bbp_get_closed_status_id()))) {
            $topic_status = bbp_get_pending_status_id();
        }
        // Check a whitelist of possible topic status ID's
    } elseif (!empty($_POST['bbp_topic_status']) && in_array($_POST['bbp_topic_status'], array_keys(bbp_get_topic_statuses()))) {
        $topic_status = $_POST['bbp_topic_status'];
        // Use existing post_status
    } else {
        $topic_status = $topic->post_status;
    }
    /** Topic Tags ************************************************************/
    // Either replace terms
    if (bbp_allow_topic_tags() && current_user_can('assign_topic_tags') && !empty($_POST['bbp_topic_tags'])) {
        // Escape tag input
        $terms = esc_attr(strip_tags($_POST['bbp_topic_tags']));
        // Explode by comma
        if (strstr($terms, ',')) {
            $terms = explode(',', $terms);
        }
        // Add topic tag ID as main key
        $terms = array(bbp_get_topic_tag_tax_id() => $terms);
        // ...or remove them.
    } elseif (isset($_POST['bbp_topic_tags'])) {
        $terms = array(bbp_get_topic_tag_tax_id() => array());
        // Existing terms
    } else {
        $terms = array(bbp_get_topic_tag_tax_id() => explode(',', bbp_get_topic_tag_names($topic_id, ',')));
    }
    /** Additional Actions (Before Save) **************************************/
    do_action('bbp_edit_topic_pre_extras', $topic_id);
    // Bail if errors
    if (bbp_has_errors()) {
        return;
    }
    /** No Errors *************************************************************/
    // Add the content of the form to $topic_data as an array
    // Just in time manipulation of topic data before being edited
    $topic_data = apply_filters('bbp_edit_topic_pre_insert', array('ID' => $topic_id, 'post_title' => $topic_title, 'post_content' => $topic_content, 'post_status' => $topic_status, 'post_parent' => $forum_id, 'post_author' => $topic_author, 'post_type' => bbp_get_topic_post_type(), 'tax_input' => $terms));
    // Toggle revisions to avoid duplicates
    if (post_type_supports(bbp_get_topic_post_type(), 'revisions')) {
        $revisions_removed = true;
        remove_post_type_support(bbp_get_topic_post_type(), 'revisions');
    }
    // Insert topic
    $topic_id = wp_update_post($topic_data);
    // Toggle revisions back on
    if (true === $revisions_removed) {
        $revisions_removed = false;
        add_post_type_support(bbp_get_topic_post_type(), 'revisions');
    }
    /** No Errors *************************************************************/
    if (!empty($topic_id) && !is_wp_error($topic_id)) {
        // Update counts, etc...
        do_action('bbp_edit_topic', $topic_id, $forum_id, $anonymous_data, $topic_author, true);
        /** Revisions *********************************************************/
        // Revision Reason
        if (!empty($_POST['bbp_topic_edit_reason'])) {
            $topic_edit_reason = esc_attr(strip_tags($_POST['bbp_topic_edit_reason']));
        }
        // Update revision log
        if (!empty($_POST['bbp_log_topic_edit']) && "1" === $_POST['bbp_log_topic_edit']) {
            $revision_id = wp_save_post_revision($topic_id);
            if (!empty($revision_id)) {
                bbp_update_topic_revision_log(array('topic_id' => $topic_id, 'revision_id' => $revision_id, 'author_id' => bbp_get_current_user_id(), 'reason' => $topic_edit_reason));
            }
        }
        /** Move Topic ********************************************************/
        // If the new forum id is not equal to the old forum id, run the
        // bbp_move_topic action and pass the topic's forum id as the
        // first arg and topic id as the second to update counts.
        if ($forum_id !== $topic->post_parent) {
            bbp_move_topic_handler($topic_id, $topic->post_parent, $forum_id);
        }
        /** Stickies **********************************************************/
        if (!empty($_POST['bbp_stick_topic']) && in_array($_POST['bbp_stick_topic'], array_keys(bbp_get_topic_types()))) {
            // What's the caps?
            if (current_user_can('moderate')) {
                // What's the haps?
                switch ($_POST['bbp_stick_topic']) {
                    // Sticky in forum
                    case 'stick':
                        bbp_stick_topic($topic_id);
                        break;
                        // Sticky in all forums
                    // Sticky in all forums
                    case 'super':
                        bbp_stick_topic($topic_id, true);
                        break;
                        // Normal
                    // Normal
                    case 'unstick':
                    default:
                        bbp_unstick_topic($topic_id);
                        break;
                }
            }
        }
        /** Additional Actions (After Save) ***********************************/
        do_action('bbp_edit_topic_post_extras', $topic_id);
        /** Redirect **********************************************************/
        // Redirect to
        $redirect_to = bbp_get_redirect_to();
        // View all?
        $view_all = bbp_get_view_all();
        // Get the topic URL
        $topic_url = bbp_get_topic_permalink($topic_id, $redirect_to);
        // Add view all?
        if (!empty($view_all)) {
            $topic_url = bbp_add_view_all($topic_url);
        }
        // Allow to be filtered
        $topic_url = apply_filters('bbp_edit_topic_redirect_to', $topic_url, $view_all, $redirect_to);
        /** Successful Edit ***************************************************/
        // Redirect back to new topic
        wp_safe_redirect($topic_url);
        // For good measure
        exit;
        /** Errors ****************************************************************/
    } else {
        $append_error = is_wp_error($topic_id) && $topic_id->get_error_message() ? $topic_id->get_error_message() . ' ' : '';
        bbp_add_error('bbp_topic_error', __('<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error . 'Please try again.', 'bbpress'));
    }
}
예제 #2
0
/**
 * Returns topic type select box (normal/sticky/super sticky)
 *
 * @since bbPress (r5059)
 *
 * @param $args This function supports these arguments:
 *  - select_id: Select id. Defaults to bbp_stick_topic
 *  - tab: Tabindex
 *  - topic_id: Topic id
 *  - selected: Override the selected option
 * @uses bbp_get_topic_id() To get the topic id
 * @uses bbp_is_single_topic() To check if we're viewing a single topic
 * @uses bbp_is_topic_edit() To check if it is the topic edit page
 * @uses bbp_is_topic_super_sticky() To check if the topic is a super sticky
 * @uses bbp_is_topic_sticky() To check if the topic is a sticky
 */
function stachestack_bbp_get_form_topic_type_dropdown($args = '')
{
    // Parse arguments against default values
    $r = bbp_parse_args($args, array('select_id' => 'bbp_stick_topic', 'tab' => bbp_get_tab_index(), 'topic_id' => 0, 'selected' => false), 'topic_type_select');
    // No specific selected value passed
    if (empty($r['selected'])) {
        // Post value is passed
        if (bbp_is_post_request() && isset($_POST[$r['select_id']])) {
            $r['selected'] = $_POST[$r['select_id']];
            // No Post value passed
        } else {
            // Edit topic
            if (bbp_is_single_topic() || bbp_is_topic_edit()) {
                // Get current topic id
                $topic_id = bbp_get_topic_id($r['topic_id']);
                // Topic is super sticky
                if (bbp_is_topic_super_sticky($topic_id)) {
                    $r['selected'] = 'super';
                    // Topic is sticky or normal
                } else {
                    $r['selected'] = bbp_is_topic_sticky($topic_id, false) ? 'stick' : 'unstick';
                }
            }
        }
    }
    // Used variables
    $tab = !empty($r['tab']) ? ' tabindex="' . (int) $r['tab'] . '"' : '';
    // Start an output buffer, we'll finish it after the select loop
    ob_start();
    ?>

		<select class="form-control" name="<?php 
    echo esc_attr($r['select_id']);
    ?>
" id="<?php 
    echo esc_attr($r['select_id']);
    ?>
_select"<?php 
    echo $tab;
    ?>
>

			<?php 
    foreach (bbp_get_topic_types() as $key => $label) {
        ?>

				<option value="<?php 
        echo esc_attr($key);
        ?>
"<?php 
        selected($key, $r['selected']);
        ?>
><?php 
        echo esc_html($label);
        ?>
</option>

			<?php 
    }
    ?>

		</select>

		<?php 
    // Return the results
    return apply_filters('bbp_get_form_topic_type_dropdown', ob_get_clean(), $r);
}
예제 #3
0
/**
 * 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
 * @uses 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);
    }
    // Get the topic types
    $topic_types = bbp_get_topic_types($topic_id);
    // Sticky check after 'bbp_new_topic' action so forum ID meta is set
    if (!empty($_POST['bbp_stick_topic']) && in_array($_POST['bbp_stick_topic'], array_keys($topic_types))) {
        // What's the caps?
        if (current_user_can('moderate', $topic_id)) {
            // What's the haps?
            switch ($_POST['bbp_stick_topic']) {
                // Sticky in this forum
                case 'stick':
                    bbp_stick_topic($topic_id);
                    break;
                    // Super sticky in all forums
                // Super sticky in all forums
                case 'super':
                    bbp_stick_topic($topic_id, true);
                    break;
                    // We can avoid this as it is a new topic
                // We can avoid this as it is a new topic
                case 'unstick':
                default:
                    break;
            }
        }
    }
    // 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 = get_post_field('post_date', $topic_id);
        // 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);
    }
}