예제 #1
0
파일: forum.php 프로젝트: joeyblake/bbpress
 /**
  * @group canonical
  * @covers ::bbp_insert_forum
  */
 public function test_bbp_insert_forum()
 {
     $f = $this->factory->forum->create(array('post_title' => 'Forum 1', 'post_content' => 'Content of Forum 1'));
     $now = time();
     $post_date = date('Y-m-d H:i:s', $now - 60 * 60 * 100);
     $t = $this->factory->topic->create(array('post_parent' => $f, 'post_date' => $post_date, 'topic_meta' => array('forum_id' => $f)));
     $r = $this->factory->reply->create(array('post_parent' => $t, 'post_date' => $post_date, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t)));
     // Get the forum.
     $forum = bbp_get_forum($f);
     // Forum post.
     $this->assertSame('Forum 1', bbp_get_forum_title($f));
     $this->assertSame('Content of Forum 1', bbp_get_forum_content($f));
     $this->assertSame('open', bbp_get_forum_status($f));
     $this->assertSame('forum', bbp_get_forum_type($f));
     $this->assertTrue(bbp_is_forum_public($f));
     $this->assertSame(0, bbp_get_forum_parent_id($f));
     $this->assertEquals('http://' . WP_TESTS_DOMAIN . '/?forum=' . $forum->post_name, $forum->guid);
     // Forum meta.
     $this->assertSame(0, bbp_get_forum_subforum_count($f, true));
     $this->assertSame(1, bbp_get_forum_topic_count($f, false, true));
     $this->assertSame(1, bbp_get_forum_topic_count($f, true, true));
     $this->assertSame(0, bbp_get_forum_topic_count_hidden($f, true));
     $this->assertSame(1, bbp_get_forum_reply_count($f, false, true));
     $this->assertSame(1, bbp_get_forum_reply_count($f, true, true));
     $this->assertSame(2, bbp_get_forum_post_count($f, false, true));
     $this->assertSame(2, bbp_get_forum_post_count($f, true, true));
     $this->assertSame($t, bbp_get_forum_last_topic_id($f));
     $this->assertSame($r, bbp_get_forum_last_reply_id($f));
     $this->assertSame($r, bbp_get_forum_last_active_id($f));
     $this->assertSame('4 days, 4 hours ago', bbp_get_forum_last_active_time($f));
 }
예제 #2
0
 public function __construct($r = array())
 {
     $args = array('plural' => 'topics', 'singular' => 'topic', 'ajax' => false);
     // $this->query =& bbpress()->topic_query;
     $this->forum = bbp_get_forum($r['post_parent']);
     $this->forum_op = bbpresskr()->forum_options($r['post_parent']);
     // override query vars
     $this->_topic_args = array_merge($r, array('posts_per_page' => $this->forum_op['posts_per_page']));
     $this->post_type = $r['post_type'];
     $this->post_type_object = get_post_type_object($this->post_type);
     add_filter("bbpkr_{$this->post_type}_columns", array($this, 'get_columns'), 0);
     if (!$args['plural']) {
         $args['plural'] = 'topics';
     }
     $args['plural'] = sanitize_key($args['plural']);
     $args['singular'] = sanitize_key($args['singular']);
     $this->_args = $args;
     add_action('bbp_has_topics', array(&$this, 'prepare_items'), 10, 2);
     // list topics of sub-forum of forum category
     add_filter('bbp_after_has_topics_parse_args', array(&$this, 'add_topic_parent_forums'), 99);
     /*if ( $args['ajax'] ) {
     			// wp_enqueue_script( 'list-table' );
     			add_action( 'admin_footer', array( $this, '_js_vars' ) );
     		}*/
 }
예제 #3
0
function bbp_get_topic_write_url($forum_id = 0)
{
    global $wp_rewrite;
    $bbp = bbpress();
    $forum = bbp_get_forum(bbp_get_forum_id($forum_id));
    if (empty($forum)) {
        return;
    }
    // Remove view=all link from edit
    $forum_link = bbp_get_forum_permalink($forum_id);
    // Pretty permalinks
    if ($wp_rewrite->using_permalinks()) {
        $url = trailingslashit($forum_link) . $bbp->write_id;
        $url = trailingslashit($url);
        // Unpretty permalinks
    } else {
        $url = add_query_arg(array(bbp_get_forum_post_type() => $forum->post_name, $bbp->write_id => '1'), $forum_link);
    }
    return apply_filters('bbp_get_topic_write_url', $url, $forum_id);
}
/**
 * Handles the front end reply submission
 *
 * @since bbPress (r2574)
 *
 * @param string $action The requested action to compare this function to
 * @uses bbp_add_error() To add an error message
 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
 * @uses bbp_is_anonymous() To check if an anonymous post is being made
 * @uses current_user_can() To check if the current user can publish replies
 * @uses bbp_get_current_user_id() To get the current user id
 * @uses bbp_filter_anonymous_post_data() To filter anonymous data
 * @uses bbp_set_current_anonymous_user_data() To set the anonymous user
 *                                                cookies
 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
 * @uses remove_filter() To remove kses filters if needed
 * @uses esc_attr() For sanitization
 * @uses bbp_check_for_flood() To check for flooding
 * @uses bbp_check_for_duplicate() To check for duplicates
 * @uses apply_filters() Calls 'bbp_new_reply_pre_title' with the title
 * @uses apply_filters() Calls 'bbp_new_reply_pre_content' with the content
 * @uses bbp_get_reply_post_type() To get the reply post type
 * @uses wp_set_post_terms() To set the topic tags
 * @uses wp_insert_post() To insert the reply
 * @uses do_action() Calls 'bbp_new_reply' with the reply id, topic id, forum
 *                    id, anonymous data, reply author, edit (false), and
 *                    the reply to id
 * @uses bbp_get_reply_url() To get the paginated url to the reply
 * @uses wp_safe_redirect() To redirect to the reply url
 * @uses bbPress::errors::get_error_message() To get the {@link WP_Error} error
 *                                              message
 */
function bbp_new_reply_handler($action = '')
{
    // Bail if action is not bbp-new-reply
    if ('bbp-new-reply' !== $action) {
        return;
    }
    // Nonce check
    if (!bbp_verify_nonce_request('bbp-new-reply')) {
        bbp_add_error('bbp_new_reply_nonce', __('<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress'));
        return;
    }
    // Define local variable(s)
    $topic_id = $forum_id = $reply_author = $anonymous_data = $reply_to = 0;
    $reply_title = $reply_content = $terms = '';
    /** Reply Author **********************************************************/
    // User is anonymous
    if (bbp_is_anonymous()) {
        // Filter anonymous data
        $anonymous_data = bbp_filter_anonymous_post_data();
        // Anonymous data checks out, so set cookies, etc...
        if (!empty($anonymous_data) && is_array($anonymous_data)) {
            bbp_set_current_anonymous_user_data($anonymous_data);
        }
        // User is logged in
    } else {
        // User cannot create replies
        if (!current_user_can('publish_replies')) {
            bbp_add_error('bbp_reply_permissions', __('<strong>ERROR</strong>: You do not have permission to reply.', 'bbpress'));
        }
        // Reply author is current user
        $reply_author = bbp_get_current_user_id();
    }
    /** Topic ID **************************************************************/
    // Topic id was not passed
    if (empty($_POST['bbp_topic_id'])) {
        bbp_add_error('bbp_reply_topic_id', __('<strong>ERROR</strong>: Topic ID is missing.', 'bbpress'));
        // Topic id is not a number
    } elseif (!is_numeric($_POST['bbp_topic_id'])) {
        bbp_add_error('bbp_reply_topic_id', __('<strong>ERROR</strong>: Topic ID must be a number.', 'bbpress'));
        // Topic id might be valid
    } else {
        // Get the topic id
        $posted_topic_id = intval($_POST['bbp_topic_id']);
        // Topic id is a negative number
        if (0 > $posted_topic_id) {
            bbp_add_error('bbp_reply_topic_id', __('<strong>ERROR</strong>: Topic ID cannot be a negative number.', 'bbpress'));
            // Topic does not exist
        } elseif (!bbp_get_topic($posted_topic_id)) {
            bbp_add_error('bbp_reply_topic_id', __('<strong>ERROR</strong>: Topic does not exist.', 'bbpress'));
            // Use the POST'ed topic id
        } else {
            $topic_id = $posted_topic_id;
        }
    }
    /** Forum ID **************************************************************/
    // Try to use the forum id of the topic
    if (!isset($_POST['bbp_forum_id']) && !empty($topic_id)) {
        $forum_id = bbp_get_topic_forum_id($topic_id);
        // Error check the POST'ed forum id
    } elseif (isset($_POST['bbp_forum_id'])) {
        // Empty Forum id was passed
        if (empty($_POST['bbp_forum_id'])) {
            bbp_add_error('bbp_reply_forum_id', __('<strong>ERROR</strong>: Forum ID is missing.', 'bbpress'));
            // Forum id is not a number
        } elseif (!is_numeric($_POST['bbp_forum_id'])) {
            bbp_add_error('bbp_reply_forum_id', __('<strong>ERROR</strong>: Forum ID must be a number.', 'bbpress'));
            // Forum id might be valid
        } else {
            // Get the forum id
            $posted_forum_id = intval($_POST['bbp_forum_id']);
            // Forum id is empty
            if (0 === $posted_forum_id) {
                bbp_add_error('bbp_topic_forum_id', __('<strong>ERROR</strong>: Forum ID is missing.', 'bbpress'));
                // Forum id is a negative number
            } elseif (0 > $posted_forum_id) {
                bbp_add_error('bbp_topic_forum_id', __('<strong>ERROR</strong>: Forum ID cannot be a negative number.', 'bbpress'));
                // Forum does not exist
            } elseif (!bbp_get_forum($posted_forum_id)) {
                bbp_add_error('bbp_topic_forum_id', __('<strong>ERROR</strong>: Forum does not exist.', 'bbpress'));
                // Use the POST'ed forum id
            } else {
                $forum_id = $posted_forum_id;
            }
        }
    }
    // Forum exists
    if (!empty($forum_id)) {
        // Forum is a category
        if (bbp_is_forum_category($forum_id)) {
            bbp_add_error('bbp_new_reply_forum_category', __('<strong>ERROR</strong>: This forum is a category. No replies can be created in this forum.', '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_new_reply_forum_closed', __('<strong>ERROR</strong>: This forum has been closed to new replies.', '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_new_reply_forum_private', __('<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new replies 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_new_reply_forum_hidden', __('<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new replies in it.', 'bbpress'));
                }
            }
        }
    }
    /** Unfiltered HTML *******************************************************/
    // 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_reply']) && wp_create_nonce('bbp-unfiltered-html-reply_' . $topic_id) === $_POST['_bbp_unfiltered_html_reply']) {
        remove_filter('bbp_new_reply_pre_title', 'wp_filter_kses');
        remove_filter('bbp_new_reply_pre_content', 'bbp_encode_bad', 10);
        remove_filter('bbp_new_reply_pre_content', 'bbp_filter_kses', 30);
    }
    /** Reply Title ***********************************************************/
    if (!empty($_POST['bbp_reply_title'])) {
        $reply_title = esc_attr(strip_tags($_POST['bbp_reply_title']));
    }
    // Filter and sanitize
    $reply_title = apply_filters('bbp_new_reply_pre_title', $reply_title);
    /** Reply Content *********************************************************/
    if (!empty($_POST['bbp_reply_content'])) {
        $reply_content = $_POST['bbp_reply_content'];
    }
    // Filter and sanitize
    $reply_content = apply_filters('bbp_new_reply_pre_content', $reply_content);
    // No reply content
    if (empty($reply_content)) {
        bbp_add_error('bbp_reply_content', __('<strong>ERROR</strong>: Your reply cannot be empty.', 'bbpress'));
    }
    /** Reply Flooding ********************************************************/
    if (!bbp_check_for_flood($anonymous_data, $reply_author)) {
        bbp_add_error('bbp_reply_flood', __('<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress'));
    }
    /** Reply Duplicate *******************************************************/
    if (!bbp_check_for_duplicate(array('post_type' => bbp_get_reply_post_type(), 'post_author' => $reply_author, 'post_content' => $reply_content, 'post_parent' => $topic_id, 'anonymous_data' => $anonymous_data))) {
        bbp_add_error('bbp_reply_duplicate', __('<strong>ERROR</strong>: Duplicate reply detected; it looks as though you&#8217;ve already said that!', 'bbpress'));
    }
    /** Reply Blacklist *******************************************************/
    if (!bbp_check_for_blacklist($anonymous_data, $reply_author, $reply_title, $reply_content)) {
        bbp_add_error('bbp_reply_blacklist', __('<strong>ERROR</strong>: Your reply cannot be created at this time.', 'bbpress'));
    }
    /** Reply Status **********************************************************/
    // Maybe put into moderation
    if (!bbp_check_for_moderation($anonymous_data, $reply_author, $reply_title, $reply_content)) {
        $reply_status = bbp_get_pending_status_id();
        // Default
    } else {
        $reply_status = bbp_get_public_status_id();
    }
    /** Reply To **************************************************************/
    // Handle Reply To of the reply; $_REQUEST for non-JS submissions
    if (isset($_REQUEST['bbp_reply_to'])) {
        $reply_to = bbp_validate_reply_to($_REQUEST['bbp_reply_to']);
    }
    /** Topic Closed **********************************************************/
    // If topic is closed, moderators can still reply
    if (bbp_is_topic_closed($topic_id) && !current_user_can('moderate')) {
        bbp_add_error('bbp_reply_topic_closed', __('<strong>ERROR</strong>: Topic is closed.', 'bbpress'));
    }
    /** Topic Tags ************************************************************/
    // Either replace terms
    if (bbp_allow_topic_tags() && current_user_can('assign_topic_tags') && !empty($_POST['bbp_topic_tags'])) {
        $terms = esc_attr(strip_tags($_POST['bbp_topic_tags']));
        // ...or remove them.
    } elseif (isset($_POST['bbp_topic_tags'])) {
        $terms = '';
        // Existing terms
    } else {
        $terms = bbp_get_topic_tag_names($topic_id);
    }
    /** Additional Actions (Before Save) **************************************/
    do_action('bbp_new_reply_pre_extras', $topic_id, $forum_id);
    // Bail if errors
    if (bbp_has_errors()) {
        return;
    }
    /** No Errors *************************************************************/
    // Add the content of the form to $reply_data as an array
    // Just in time manipulation of reply data before being created
    $reply_data = apply_filters('bbp_new_reply_pre_insert', array('post_author' => $reply_author, 'post_title' => $reply_title, 'post_content' => $reply_content, 'post_status' => $reply_status, 'post_parent' => $topic_id, 'post_type' => bbp_get_reply_post_type(), 'comment_status' => 'closed', 'menu_order' => bbp_get_topic_reply_count($topic_id, false) + 1));
    // Insert reply
    $reply_id = wp_insert_post($reply_data);
    /** No Errors *************************************************************/
    // Check for missing reply_id or error
    if (!empty($reply_id) && !is_wp_error($reply_id)) {
        /** Topic Tags ********************************************************/
        // Just in time manipulation of reply terms before being edited
        $terms = apply_filters('bbp_new_reply_pre_set_terms', $terms, $topic_id, $reply_id);
        // Insert terms
        $terms = wp_set_post_terms($topic_id, $terms, bbp_get_topic_tag_tax_id(), false);
        // Term error
        if (is_wp_error($terms)) {
            bbp_add_error('bbp_reply_tags', __('<strong>ERROR</strong>: There was a problem adding the tags to the topic.', 'bbpress'));
        }
        /** Trash Check *******************************************************/
        // If this reply starts as trash, add it to pre_trashed_replies
        // for the topic, so it is properly restored.
        if (bbp_is_topic_trash($topic_id) || $reply_data['post_status'] === bbp_get_trash_status_id()) {
            // Trash the reply
            wp_trash_post($reply_id);
            // Only add to pre-trashed array if topic is trashed
            if (bbp_is_topic_trash($topic_id)) {
                // Get pre_trashed_replies for topic
                $pre_trashed_replies = get_post_meta($topic_id, '_bbp_pre_trashed_replies', true);
                // Add this reply to the end of the existing replies
                $pre_trashed_replies[] = $reply_id;
                // Update the pre_trashed_reply post meta
                update_post_meta($topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies);
            }
            /** Spam Check ********************************************************/
            // If reply or topic are spam, officially spam this reply
        } elseif (bbp_is_topic_spam($topic_id) || $reply_data['post_status'] === bbp_get_spam_status_id()) {
            add_post_meta($reply_id, '_bbp_spam_meta_status', bbp_get_public_status_id());
            // Only add to pre-spammed array if topic is spam
            if (bbp_is_topic_spam($topic_id)) {
                // Get pre_spammed_replies for topic
                $pre_spammed_replies = get_post_meta($topic_id, '_bbp_pre_spammed_replies', true);
                // Add this reply to the end of the existing replies
                $pre_spammed_replies[] = $reply_id;
                // Update the pre_spammed_replies post meta
                update_post_meta($topic_id, '_bbp_pre_spammed_replies', $pre_spammed_replies);
            }
        }
        /** Update counts, etc... *********************************************/
        do_action('bbp_new_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author, false, $reply_to);
        /** Additional Actions (After Save) ***********************************/
        do_action('bbp_new_reply_post_extras', $reply_id);
        /** Redirect **********************************************************/
        // Redirect to
        $redirect_to = bbp_get_redirect_to();
        // Get the reply URL
        $reply_url = bbp_get_reply_url($reply_id, $redirect_to);
        // Allow to be filtered
        $reply_url = apply_filters('bbp_new_reply_redirect_to', $reply_url, $redirect_to, $reply_id);
        /** Successful Save ***************************************************/
        // Redirect back to new reply
        wp_safe_redirect($reply_url);
        // For good measure
        exit;
        /** Errors ****************************************************************/
    } else {
        $append_error = is_wp_error($reply_id) && $reply_id->get_error_message() ? $reply_id->get_error_message() . ' ' : '';
        bbp_add_error('bbp_reply_error', __('<strong>ERROR</strong>: The following problem(s) have been found with your reply:' . $append_error . 'Please try again.', 'bbpress'));
    }
}
예제 #5
0
/**
 * Return the topics link of the forum
 *
 * @since bbPress (r2883)
 *
 * @param int $forum_id Optional. Topic id
 * @uses bbp_get_forum_id() To get the forum id
 * @uses bbp_get_forum() To get the forum
 * @uses bbp_get_forum_topic_count() To get the forum topic count
 * @uses bbp_get_forum_permalink() To get the forum permalink
 * @uses remove_query_arg() To remove args from the url
 * @uses bbp_get_forum_topic_count_hidden() To get the forum hidden
 *                                           topic count
 * @uses current_user_can() To check if the current user can edit others
 *                           topics
 * @uses add_query_arg() To add custom args to the url
 * @uses apply_filters() Calls 'bbp_get_forum_topics_link' with the
 *                        topics link and forum id
 */
function bbp_get_forum_topics_link($forum_id = 0)
{
    $forum = bbp_get_forum($forum_id);
    $forum_id = $forum->ID;
    $topics = bbp_get_forum_topic_count($forum_id);
    $topics = sprintf(_n('%s topic', '%s topics', $topics, 'bbpress'), $topics);
    $retval = '';
    // First link never has view=all
    if (bbp_get_view_all('edit_others_topics')) {
        $retval .= "<a href='" . esc_url(bbp_remove_view_all(bbp_get_forum_permalink($forum_id))) . "'>{$topics}</a>";
    } else {
        $retval .= $topics;
    }
    // Get deleted topics
    $deleted = bbp_get_forum_topic_count_hidden($forum_id);
    // This forum has hidden topics
    if (!empty($deleted) && current_user_can('edit_others_topics')) {
        // Extra text
        $extra = sprintf(__(' (+ %d hidden)', 'bbpress'), $deleted);
        // No link
        if (bbp_get_view_all()) {
            $retval .= " {$extra}";
            // Link
        } else {
            $retval .= " <a href='" . esc_url(bbp_add_view_all(bbp_get_forum_permalink($forum_id), true)) . "'>{$extra}</a>";
        }
    }
    return apply_filters('bbp_get_forum_topics_link', $retval, $forum_id);
}
예제 #6
0
 /**
  * AJAX handler to Subscribe/Unsubscribe a user from a forum
  *
  * @since bbPress (r5155)
  *
  * @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_forum() To get the forum
  * @uses wp_verify_nonce() To verify the nonce
  * @uses bbp_is_user_subscribed() To check if the forum is in user's subscriptions
  * @uses bbp_remove_user_subscriptions() To remove the forum from user's subscriptions
  * @uses bbp_add_user_subscriptions() To add the forum from user's subscriptions
  * @uses bbp_ajax_response() To return JSON
  */
 public function ajax_forum_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 forum.', 'bbpress'), 301);
     }
     // Get user and forum 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 forum
     $forum = bbp_get_forum($id);
     // Bail if forum cannot be found
     if (empty($forum)) {
         bbp_ajax_response(false, __('The forum 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_' . $forum->ID)) {
         bbp_ajax_response(false, __('Are you sure you meant to do that?', 'bbpress'), 304);
     }
     // Take action
     $status = bbp_is_user_subscribed($user_id, $forum->ID) ? bbp_remove_user_subscription($user_id, $forum->ID) : bbp_add_user_subscription($user_id, $forum->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('forum_id' => $forum->ID, 'user_id' => $user_id);
     // Action succeeded
     bbp_ajax_response(true, bbp_get_forum_subscription_link($attrs, $user_id, false), 200);
 }
예제 #7
0
/**
 * Add a forum to user's subscriptions
 *
 * @since bbPress (r5156)
 *
 * @param int $user_id Optional. User id
 * @param int $forum_id Optional. forum id
 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
 * @uses bbp_get_forum() To get the forum
 * @uses update_user_option() To update the user's subscriptions
 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id
 * @return bool Always true
 */
function bbp_add_user_forum_subscription($user_id = 0, $forum_id = 0)
{
    if (empty($user_id) || empty($forum_id)) {
        return false;
    }
    $forum = bbp_get_forum($forum_id);
    if (empty($forum)) {
        return false;
    }
    $subscriptions = (array) bbp_get_user_subscribed_forum_ids($user_id);
    if (!in_array($forum_id, $subscriptions)) {
        $subscriptions[] = $forum_id;
        $subscriptions = implode(',', wp_parse_id_list(array_filter($subscriptions)));
        update_user_option($user_id, '_bbp_forum_subscriptions', $subscriptions);
        wp_cache_delete('bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users');
    }
    do_action('bbp_add_user_forum_subscription', $user_id, $forum_id);
    return true;
}
예제 #8
0
 public function get_object_by_id($forum_id)
 {
     return bbp_get_forum($forum_id);
 }
예제 #9
0
 /**
  * @see Walker::start_el()
  *
  * @since bbPress (r2514)
  *
  * @param string $output Passed by reference. Used to append additional
  *                        content.
  * @param object $forum Page data object.
  * @param int $depth Depth of page. Used for padding.
  * @param int $current_forum Page ID.
  * @param array $args
  */
 public function start_el(&$output, $forum, $depth, $args, $current_forum)
 {
     $indent = $depth ? str_repeat("\t", $depth) : '';
     extract($args, EXTR_SKIP);
     $css_class = array('bbp-forum-item', 'bbp-forum-item-' . $forum->ID);
     if (!empty($current_forum)) {
         $_current_page = bbp_get_forum($current_forum);
         if (isset($_current_page->ancestors) && in_array($forum->ID, (array) $_current_page->ancestors)) {
             $css_class[] = 'bbp-current-forum-ancestor';
         }
         if ($forum->ID == $current_forum) {
             $css_class[] = 'bbp_current_forum_item';
         } elseif ($_current_page && $forum->ID == $_current_page->post_parent) {
             $css_class[] = 'bbp-current-forum-parent';
         }
     } elseif ($forum->ID == get_option('page_for_posts')) {
         $css_class[] = 'bbp-current-forum-parent';
     }
     $css_class = implode(' ', apply_filters('bbp_forum_css_class', $css_class, $forum));
     $output .= $indent . '<li class="' . $css_class . '"><a href="' . bbp_get_forum_permalink($forum->ID) . '" title="' . esc_attr(wp_strip_all_tags(apply_filters('the_title', $forum->post_title, $forum->ID))) . '">' . $link_before . apply_filters('the_title', $forum->post_title, $forum->ID) . $link_after . '</a>';
     if (!empty($show_date)) {
         $time = 'modified' == $show_date ? $forum->post_modified : ($time = $forum->post_date);
         $output .= " " . mysql2date($date_format, $time);
     }
 }
예제 #10
0
    ?>


            <?php 
    do_action('bbp_template_before_forums_loop');
    ?>
                    <?php 
    while (bbp_forums()) {
        bbp_the_forum();
        ?>
                        <!-- forum loop -->
                        <?php 
        /**
         * Forums Loop - Single Forum
         */
        $cur_forum_obj = bbp_get_forum('');
        if (bbp_is_forum_category()) {
            //is a category - print the header and output the forums
            ?>
                            <div class="clearfix"></div>
                            <ul class="td-forum-list-head td-forum-list-table">
                                <li class="td-forum-category-title">
                                    <span class="td-forum-category-name">
                                    <?php 
            echo $cur_forum_obj->post_title;
            ?>
                                    </span>
                                </li><li class="td-forum-topics-replies">
                                    Topics/Replies
                                </li><li class="td-forum-last-comment">
                                    Last comment
if (bbp_has_forums($buddypress_id)) {
    $topics = false;
    while (bbp_forums()) {
        bbp_the_forum();
        if ($forum_id != bbp_get_forum_id()) {
            continue;
        }
        $topics = bbp_has_topics(array('post_parent' => $forum_id, 'posts_per_page' => 11));
        if (!$topics) {
            _e('This forum does not have topics', 'qode');
            break;
        }
        $counter = 0;
        if (bp_group_is_visible($group)) {
            global $post;
            $post = bbp_get_forum($forum_id);
            $counter = 0;
            while (bbp_topics()) {
                bbp_the_topic();
                if (++$counter == 12) {
                    break;
                }
                ?>
										            <div class="topics_list_single_topic <?php 
                $postUser = new WP_User(bbp_get_topic_author_id());
                echo $postUser->has_cap('bbp_keymaster') || $postUser->has_cap('bbp_moderator') ? "isAdmin" : "";
                ?>
"
										                id="topic-<?php 
                echo bbp_get_topic_id();
                ?>
예제 #12
0
/**
 * Add a forum to user's subscriptions
 *
 * @since 2.5.0 bbPress (r5156)
 *
 * @param int $user_id Optional. User id
 * @param int $forum_id Optional. forum id
 * @uses bbp_get_forum() To get the forum
 * @uses bbp_add_user_subscription() To add the user subscription
 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id
 * @return bool Always true
 */
function bbp_add_user_forum_subscription($user_id = 0, $forum_id = 0)
{
    // Bail if not enough info
    if (empty($user_id) || empty($forum_id)) {
        return false;
    }
    // Bail if no forum
    $forum = bbp_get_forum($forum_id);
    if (empty($forum)) {
        return false;
    }
    // Bail if already subscribed
    if (bbp_is_user_subscribed($user_id, $forum_id)) {
        return false;
    }
    // Bail if add fails
    if (!bbp_add_user_subscription($user_id, $forum_id)) {
        return false;
    }
    do_action('bbp_add_user_forum_subscription', $user_id, $forum_id);
    return true;
}
예제 #13
0
        /**
         * Toggle forum notices
         *
         * Display the success/error notices from
         * {@link BBP_Admin::toggle_forum()}
         *
         * @since 2.6.0 bbPress (r5254)
         *
         * @uses bbp_get_forum() To get the forum
         * @uses bbp_get_forum_title() To get the forum title of the forum
         * @uses esc_html() To sanitize the forum title
         * @uses apply_filters() Calls 'bbp_toggle_forum_notice_admin' with
         *                        message, forum id, notice and is it a failure
         */
        public function toggle_forum_notice()
        {
            if ($this->bail()) {
                return;
            }
            // Only proceed if GET is a forum toggle action
            if (bbp_is_get_request() && !empty($_GET['bbp_forum_toggle_notice']) && in_array($_GET['bbp_forum_toggle_notice'], array('opened', 'closed')) && !empty($_GET['forum_id'])) {
                $notice = $_GET['bbp_forum_toggle_notice'];
                // Which notice?
                $forum_id = (int) $_GET['forum_id'];
                // What's the forum id?
                $is_failure = !empty($_GET['failed']) ? true : false;
                // Was that a failure?
                // Bail if no forum_id or notice
                if (empty($notice) || empty($forum_id)) {
                    return;
                }
                // Bail if forum is missing
                $forum = bbp_get_forum($forum_id);
                if (empty($forum)) {
                    return;
                }
                $forum_title = bbp_get_forum_title($forum->ID);
                switch ($notice) {
                    case 'opened':
                        $message = $is_failure === true ? sprintf(__('There was a problem opening the forum "%1$s".', 'bbpress'), $forum_title) : sprintf(__('Forum "%1$s" successfully opened.', 'bbpress'), $forum_title);
                        break;
                    case 'closed':
                        $message = $is_failure === true ? sprintf(__('There was a problem closing the forum "%1$s".', 'bbpress'), $forum_title) : sprintf(__('Forum "%1$s" successfully closed.', 'bbpress'), $forum_title);
                        break;
                }
                // Do additional forum toggle notice filters (admin side)
                $message = apply_filters('bbp_toggle_forum_notice_admin', $message, $forum->ID, $notice, $is_failure);
                ?>

			<div id="message" class="<?php 
                echo $is_failure === true ? 'error' : 'updated';
                ?>
 fade">
				<p style="line-height: 150%"><?php 
                echo esc_html($message);
                ?>
</p>
			</div>

			<?php 
            }
        }
예제 #14
0
 /**
  * Set forums' status to match the privacy status of the associated group
  *
  * Fired whenever a group is saved
  *
  * @param BP_Groups_Group $group Group object.
  */
 public static function update_group_forum_visibility(BP_Groups_Group $group)
 {
     // Get group forum IDs
     $forum_ids = bbp_get_group_forum_ids($group->id);
     // Bail if no forum IDs available
     if (empty($forum_ids)) {
         return;
     }
     // Loop through forum IDs
     foreach ($forum_ids as $forum_id) {
         // Get forum from ID
         $forum = bbp_get_forum($forum_id);
         // Check for change
         if ($group->status !== $forum->post_status) {
             switch ($group->status) {
                 // Changed to hidden
                 case 'hidden':
                     bbp_hide_forum($forum_id, $forum->post_status);
                     break;
                     // Changed to private
                 // Changed to private
                 case 'private':
                     bbp_privatize_forum($forum_id, $forum->post_status);
                     break;
                     // Changed to public
                 // Changed to public
                 case 'public':
                 default:
                     bbp_publicize_forum($forum_id, $forum->post_status);
                     break;
             }
         }
     }
 }
예제 #15
0
/**
 * Handles the front end edit forum submission
 *
 * @param string $action The requested action to compare this function to
 * @uses bbPress:errors::add() To log various error messages
 * @uses bbp_get_forum() To get the forum
 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
 * @uses bbp_is_forum_anonymous() To check if forum is by an anonymous user
 * @uses current_user_can() To check if the current user can edit the forum
 * @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_forum_pre_title' with the title and
 *                        forum id
 * @uses apply_filters() Calls 'bbp_edit_forum_pre_content' with the content
 *                        and forum id
 * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
 * @uses wp_save_post_revision() To save a forum revision
 * @uses bbp_update_forum_revision_log() To update the forum revision log
 * @uses wp_update_post() To update the forum
 * @uses do_action() Calls 'bbp_edit_forum' with the forum id, forum id,
 *                    anonymous data and reply author
 * @uses bbp_move_forum_handler() To handle movement of a forum from one forum
 *                                 to another
 * @uses bbp_get_forum_permalink() To get the forum permalink
 * @uses wp_safe_redirect() To redirect to the forum link
 * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
 *                                              messages
 */
function bbp_edit_forum_handler($action = '')
{
    // Bail if action is not bbp-edit-forum
    if ('bbp-edit-forum' !== $action) {
        return;
    }
    // Define local variable(s)
    $anonymous_data = array();
    $forum = $forum_id = $forum_parent_id = 0;
    $forum_title = $forum_content = $forum_edit_reason = '';
    /** Forum *****************************************************************/
    // Forum id was not passed
    if (empty($_POST['bbp_forum_id'])) {
        bbp_add_error('bbp_edit_forum_id', __('<strong>ERROR</strong>: Forum ID not found.', 'bbpress'));
        return;
        // Forum id was passed
    } elseif (is_numeric($_POST['bbp_forum_id'])) {
        $forum_id = (int) $_POST['bbp_forum_id'];
        $forum = bbp_get_forum($forum_id);
    }
    // Nonce check
    if (!bbp_verify_nonce_request('bbp-edit-forum_' . $forum_id)) {
        bbp_add_error('bbp_edit_forum_nonce', __('<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress'));
        return;
        // Forum does not exist
    } elseif (empty($forum)) {
        bbp_add_error('bbp_edit_forum_not_found', __('<strong>ERROR</strong>: The forum you want to edit was not found.', 'bbpress'));
        return;
        // User cannot edit this forum
    } elseif (!current_user_can('edit_forum', $forum_id)) {
        bbp_add_error('bbp_edit_forum_permissions', __('<strong>ERROR</strong>: You do not have permission to edit that forum.', '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_forum']) && wp_create_nonce('bbp-unfiltered-html-forum_' . $forum_id) === $_POST['_bbp_unfiltered_html_forum']) {
        remove_filter('bbp_edit_forum_pre_title', 'wp_filter_kses');
        remove_filter('bbp_edit_forum_pre_content', 'bbp_encode_bad', 10);
        remove_filter('bbp_edit_forum_pre_content', 'bbp_filter_kses', 30);
    }
    /** Forum Parent ***********************************************************/
    // Forum parent id was passed
    if (!empty($_POST['bbp_forum_parent_id'])) {
        $forum_parent_id = bbp_get_forum_id($_POST['bbp_forum_parent_id']);
    }
    // Current forum this forum is in
    $current_parent_forum_id = bbp_get_forum_parent_id($forum_id);
    // Forum exists
    if (!empty($forum_parent_id) && $forum_parent_id !== $current_parent_forum_id) {
        // Forum is closed and user cannot access
        if (bbp_is_forum_closed($forum_parent_id) && !current_user_can('edit_forum', $forum_parent_id)) {
            bbp_add_error('bbp_edit_forum_forum_closed', __('<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress'));
        }
        // Forum is private and user cannot access
        if (bbp_is_forum_private($forum_parent_id) && !current_user_can('read_private_forums')) {
            bbp_add_error('bbp_edit_forum_forum_private', __('<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress'));
        }
        // Forum is hidden and user cannot access
        if (bbp_is_forum_hidden($forum_parent_id) && !current_user_can('read_hidden_forums')) {
            bbp_add_error('bbp_edit_forum_forum_hidden', __('<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress'));
        }
    }
    /** Forum Title ***********************************************************/
    if (!empty($_POST['bbp_forum_title'])) {
        $forum_title = esc_attr(strip_tags($_POST['bbp_forum_title']));
    }
    // Filter and sanitize
    $forum_title = apply_filters('bbp_edit_forum_pre_title', $forum_title, $forum_id);
    // No forum title
    if (empty($forum_title)) {
        bbp_add_error('bbp_edit_forum_title', __('<strong>ERROR</strong>: Your forum needs a title.', 'bbpress'));
    }
    /** Forum Content *********************************************************/
    if (!empty($_POST['bbp_forum_content'])) {
        $forum_content = $_POST['bbp_forum_content'];
    }
    // Filter and sanitize
    $forum_content = apply_filters('bbp_edit_forum_pre_content', $forum_content, $forum_id);
    // No forum content
    if (empty($forum_content)) {
        bbp_add_error('bbp_edit_forum_content', __('<strong>ERROR</strong>: Your forum description cannot be empty.', 'bbpress'));
    }
    /** Forum Blacklist *******************************************************/
    if (!bbp_check_for_blacklist($anonymous_data, bbp_get_forum_author_id($forum_id), $forum_title, $forum_content)) {
        bbp_add_error('bbp_forum_blacklist', __('<strong>ERROR</strong>: Your forum cannot be edited at this time.', 'bbpress'));
    }
    /** Forum Moderation ******************************************************/
    $post_status = bbp_get_public_status_id();
    if (!bbp_check_for_moderation($anonymous_data, bbp_get_forum_author_id($forum_id), $forum_title, $forum_content)) {
        $post_status = bbp_get_pending_status_id();
    }
    /** Additional Actions (Before Save) **************************************/
    do_action('bbp_edit_forum_pre_extras', $forum_id);
    // Bail if errors
    if (bbp_has_errors()) {
        return;
    }
    /** No Errors *************************************************************/
    // Add the content of the form to $forum_data as an array
    // Just in time manipulation of forum data before being edited
    $forum_data = apply_filters('bbp_edit_forum_pre_insert', array('ID' => $forum_id, 'post_title' => $forum_title, 'post_content' => $forum_content, 'post_status' => $post_status, 'post_parent' => $forum_parent_id));
    // Insert forum
    $forum_id = wp_update_post($forum_data);
    /** Revisions *************************************************************/
    /**
    * @todo omitted for 2.1
    	// Revision Reason
    	if ( !empty( $_POST['bbp_forum_edit_reason'] ) )
    		$forum_edit_reason = esc_attr( strip_tags( $_POST['bbp_forum_edit_reason'] ) );
    
    	// Update revision log
    	if ( !empty( $_POST['bbp_log_forum_edit'] ) && ( "1" === $_POST['bbp_log_forum_edit'] ) && ( $revision_id = wp_save_post_revision( $forum_id ) ) ) {
    		bbp_update_forum_revision_log( array(
    			'forum_id'    => $forum_id,
    			'revision_id' => $revision_id,
    			'author_id'   => bbp_get_current_user_id(),
    			'reason'      => $forum_edit_reason
    		) );
    	}
    */
    /** No Errors *************************************************************/
    if (!empty($forum_id) && !is_wp_error($forum_id)) {
        // Update counts, etc...
        do_action('bbp_edit_forum', array('forum_id' => $forum_id, 'post_parent' => $forum_parent_id, 'forum_author' => $forum->post_author, 'last_topic_id' => 0, 'last_reply_id' => 0, 'last_active_id' => 0, 'last_active_time' => 0, 'last_active_status' => bbp_get_public_status_id()));
        // If the new forum parent id is not equal to the old forum parent
        // id, run the bbp_move_forum action and pass the forum's parent id
        // as the first arg and new forum parent id as the second.
        // @todo implement
        //if ( $forum_id !== $forum->post_parent )
        //	bbp_move_forum_handler( $forum_parent_id, $forum->post_parent, $forum_id );
        /** Additional Actions (After Save) ***********************************/
        do_action('bbp_edit_forum_post_extras', $forum_id);
        /** Redirect **********************************************************/
        // Redirect to
        $redirect_to = bbp_get_redirect_to();
        // View all?
        $view_all = bbp_get_view_all();
        // Get the forum URL
        $forum_url = bbp_get_forum_permalink($forum_id, $redirect_to);
        // Add view all?
        if (!empty($view_all)) {
            $forum_url = bbp_add_view_all($forum_url);
        }
        // Allow to be filtered
        $forum_url = apply_filters('bbp_edit_forum_redirect_to', $forum_url, $view_all, $redirect_to);
        /** Successful Edit ***************************************************/
        // Redirect back to new forum
        wp_safe_redirect($forum_url);
        // For good measure
        exit;
        /** Errors ****************************************************************/
    } else {
        $append_error = is_wp_error($forum_id) && $forum_id->get_error_message() ? $forum_id->get_error_message() . ' ' : '';
        bbp_add_error('bbp_forum_error', __('<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error . 'Please try again.', 'bbpress'));
    }
}
예제 #16
0
 /**
  * Save the Group Forum data on edit
  *
  * @since bbPress (r3465)
  * @param int $group_id (to handle Group Admin UI hook bp_group_admin_edit_after )
  * @uses bbp_new_forum_handler() To check for forum creation
  * @uses bbp_edit_forum_handler() To check for forum edit
  */
 public function edit_screen_save($group_id = 0)
 {
     // Bail if not a POST action
     if (!bbp_is_post_request()) {
         return;
     }
     // Admin Nonce check
     if (is_admin()) {
         check_admin_referer('groups_edit_save_' . $this->slug, 'forum_group_admin_ui');
         // Theme-side Nonce check
     } elseif (!bbp_verify_nonce_request('groups_edit_save_' . $this->slug)) {
         bbp_add_error('bbp_edit_group_forum_screen_save', __('<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress'));
         return;
     }
     $edit_forum = !empty($_POST['bbp-edit-group-forum']) ? true : false;
     $forum_id = 0;
     $group_id = !empty($group_id) ? $group_id : bp_get_current_group_id();
     // Keymasters have the ability to reconfigure forums
     if (bbp_is_user_keymaster()) {
         $forum_ids = !empty($_POST['bbp_group_forum_id']) ? (array) (int) $_POST['bbp_group_forum_id'] : array();
         // Use the existing forum IDs
     } else {
         $forum_ids = array_values(bbp_get_group_forum_ids($group_id));
     }
     // Normalize group forum relationships now
     if (!empty($forum_ids)) {
         // Loop through forums, and make sure they exist
         foreach ($forum_ids as $forum_id) {
             // Look for forum
             $forum = bbp_get_forum($forum_id);
             // No forum exists, so break the relationship
             if (empty($forum)) {
                 $this->remove_forum(array('forum_id' => $forum_id));
                 unset($forum_ids[$forum_id]);
             }
         }
         // No support for multiple forums yet
         $forum_id = (int) (is_array($forum_ids) ? $forum_ids[0] : $forum_ids);
     }
     // Update the group ID and forum ID relationships
     bbp_update_group_forum_ids($group_id, (array) $forum_ids);
     bbp_update_forum_group_ids($forum_id, (array) $group_id);
     // Update the group forum setting
     $group = $this->toggle_group_forum($group_id, $edit_forum);
     // Create a new forum
     if (empty($forum_id) && true === $edit_forum) {
         // Set the default forum status
         switch ($group->status) {
             case 'hidden':
                 $status = bbp_get_hidden_status_id();
                 break;
             case 'private':
                 $status = bbp_get_private_status_id();
                 break;
             case 'public':
             default:
                 $status = bbp_get_public_status_id();
                 break;
         }
         // Create the initial forum
         $forum_id = bbp_insert_forum(array('post_parent' => bbp_get_group_forums_root_id(), 'post_title' => $group->name, 'post_content' => $group->description, 'post_status' => $status));
         // Setup forum args with forum ID
         $new_forum_args = array('forum_id' => $forum_id);
         // If in admin, also include the group ID
         if (is_admin() && !empty($group_id)) {
             $new_forum_args['group_id'] = $group_id;
         }
         // Run the BP-specific functions for new groups
         $this->new_forum($new_forum_args);
     }
     // Redirect after save when not in admin
     if (!is_admin()) {
         bp_core_redirect(trailingslashit(bp_get_group_permalink(buddypress()->groups->current_group) . '/admin/' . $this->slug));
     }
 }
예제 #17
0
/**
 * Handles the front end topic submission
 *
 * @param string $action The requested action to compare this function to
 * @uses bbp_add_error() To add an error message
 * @uses bbp_verify_nonce_request() To verify the nonce and check the referer
 * @uses bbp_is_anonymous() To check if an anonymous post is being made
 * @uses current_user_can() To check if the current user can publish topic
 * @uses bbp_get_current_user_id() To get the current user id
 * @uses bbp_filter_anonymous_post_data() To filter anonymous data
 * @uses bbp_set_current_anonymous_user_data() To set the anonymous user cookies
 * @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 bbp_check_for_flood() To check for flooding
 * @uses bbp_check_for_duplicate() To check for duplicates
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses remove_filter() To remove kses filters if needed
 * @uses apply_filters() Calls 'bbp_new_topic_pre_title' with the content
 * @uses apply_filters() Calls 'bbp_new_topic_pre_content' with the content
 * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
 * @uses wp_insert_post() To insert the topic
 * @uses do_action() Calls 'bbp_new_topic' with the topic id, forum id,
 *                    anonymous data and reply author
 * @uses bbp_stick_topic() To stick or super stick the topic
 * @uses bbp_unstick_topic() To unstick the topic
 * @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_new_topic_handler($action = '')
{
    // Bail if action is not bbp-new-topic
    if ('bbp-new-topic' !== $action) {
        return;
    }
    // Nonce check
    if (!bbp_verify_nonce_request('bbp-new-topic')) {
        bbp_add_error('bbp_new_topic_nonce', __('<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress'));
        return;
    }
    // Define local variable(s)
    $view_all = false;
    $forum_id = $topic_author = $anonymous_data = 0;
    $topic_title = $topic_content = '';
    $terms = array(bbp_get_topic_tag_tax_id() => array());
    /** Topic Author **********************************************************/
    // User is anonymous
    if (bbp_is_anonymous()) {
        // Filter anonymous data
        $anonymous_data = bbp_filter_anonymous_post_data();
        // Anonymous data checks out, so set cookies, etc...
        if (!empty($anonymous_data) && is_array($anonymous_data)) {
            bbp_set_current_anonymous_user_data($anonymous_data);
        }
        // User is logged in
    } else {
        // User cannot create topics
        if (!current_user_can('publish_topics')) {
            bbp_add_error('bbp_topic_permissions', __('<strong>ERROR</strong>: You do not have permission to create new topics.', 'bbpress'));
            return;
        }
        // Topic author is current user
        $topic_author = bbp_get_current_user_id();
    }
    // 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_new') === $_POST['_bbp_unfiltered_html_topic']) {
        remove_filter('bbp_new_topic_pre_title', 'wp_filter_kses');
        remove_filter('bbp_new_topic_pre_content', 'bbp_encode_bad', 10);
        remove_filter('bbp_new_topic_pre_content', 'bbp_filter_kses', 30);
    }
    /** 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_new_topic_pre_title', $topic_title);
    // No topic title
    if (empty($topic_title)) {
        bbp_add_error('bbp_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_new_topic_pre_content', $topic_content);
    // No topic content
    if (empty($topic_content)) {
        bbp_add_error('bbp_topic_content', __('<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress'));
    }
    /** Topic Forum ***********************************************************/
    // Error check the POST'ed topic id
    if (isset($_POST['bbp_forum_id'])) {
        // Empty Forum id was passed
        if (empty($_POST['bbp_forum_id'])) {
            bbp_add_error('bbp_topic_forum_id', __('<strong>ERROR</strong>: Forum ID is missing.', 'bbpress'));
            // Forum id is not a number
        } elseif (!is_numeric($_POST['bbp_forum_id'])) {
            bbp_add_error('bbp_topic_forum_id', __('<strong>ERROR</strong>: Forum ID must be a number.', 'bbpress'));
            // Forum id might be valid
        } else {
            // Get the forum id
            $posted_forum_id = intval($_POST['bbp_forum_id']);
            // Forum id is empty
            if (0 === $posted_forum_id) {
                bbp_add_error('bbp_topic_forum_id', __('<strong>ERROR</strong>: Forum ID is missing.', 'bbpress'));
                // Forum id is a negative number
            } elseif (0 > $posted_forum_id) {
                bbp_add_error('bbp_topic_forum_id', __('<strong>ERROR</strong>: Forum ID cannot be a negative number.', 'bbpress'));
                // Forum does not exist
            } elseif (!bbp_get_forum($posted_forum_id)) {
                bbp_add_error('bbp_topic_forum_id', __('<strong>ERROR</strong>: Forum does not exist.', 'bbpress'));
                // Use the POST'ed forum id
            } else {
                $forum_id = $posted_forum_id;
            }
        }
    }
    // Forum exists
    if (!empty($forum_id)) {
        // Forum is a category
        if (bbp_is_forum_category($forum_id)) {
            bbp_add_error('bbp_new_topic_forum_category', __('<strong>ERROR</strong>: This forum is a category. No topics can be created in this forum.', '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_new_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_new_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_new_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 Flooding ********************************************************/
    if (!bbp_check_for_flood($anonymous_data, $topic_author)) {
        bbp_add_error('bbp_topic_flood', __('<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress'));
    }
    /** Topic Duplicate *******************************************************/
    if (!bbp_check_for_duplicate(array('post_type' => bbp_get_topic_post_type(), 'post_author' => $topic_author, 'post_content' => $topic_content, 'anonymous_data' => $anonymous_data))) {
        bbp_add_error('bbp_topic_duplicate', __('<strong>ERROR</strong>: Duplicate topic detected; it looks as though you&#8217;ve already said that!', '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 created at this time.', 'bbpress'));
    }
    /** Topic Status **********************************************************/
    // Maybe put into moderation
    if (!bbp_check_for_moderation($anonymous_data, $topic_author, $topic_title, $topic_content)) {
        $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'];
        // Default to published if nothing else
    } else {
        $topic_status = bbp_get_public_status_id();
    }
    /** Topic Tags ************************************************************/
    if (bbp_allow_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);
    }
    /** Additional Actions (Before Save) **************************************/
    do_action('bbp_new_topic_pre_extras', $forum_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 created
    $topic_data = apply_filters('bbp_new_topic_pre_insert', array('post_author' => $topic_author, 'post_title' => $topic_title, 'post_content' => $topic_content, 'post_status' => $topic_status, 'post_parent' => $forum_id, 'post_type' => bbp_get_topic_post_type(), 'tax_input' => $terms, 'comment_status' => 'closed'));
    // Insert topic
    $topic_id = wp_insert_post($topic_data);
    /** No Errors *************************************************************/
    if (!empty($topic_id) && !is_wp_error($topic_id)) {
        /** Trash Check *******************************************************/
        // If the forum is trash, or the topic_status is switched to
        // trash, trash it properly
        if (get_post_field('post_status', $forum_id) === bbp_get_trash_status_id() || $topic_data['post_status'] === bbp_get_trash_status_id()) {
            // Trash the reply
            wp_trash_post($topic_id);
            // Force view=all
            $view_all = true;
        }
        /** Spam Check ********************************************************/
        // If reply or topic are spam, officially spam this reply
        if ($topic_data['post_status'] === bbp_get_spam_status_id()) {
            add_post_meta($topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id());
            // Force view=all
            $view_all = true;
        }
        /** Update counts, etc... *********************************************/
        do_action('bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author);
        /** Stickies **********************************************************/
        // 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('stick', 'super', 'unstick'))) {
            // What's the caps?
            if (current_user_can('moderate')) {
                // 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;
                }
            }
        }
        /** Additional Actions (After Save) ***********************************/
        do_action('bbp_new_topic_post_extras', $topic_id);
        /** Redirect **********************************************************/
        // Redirect to
        $redirect_to = bbp_get_redirect_to();
        // Get the topic URL
        $redirect_url = bbp_get_topic_permalink($topic_id, $redirect_to);
        // Add view all?
        if (bbp_get_view_all() || !empty($view_all)) {
            // User can moderate, so redirect to topic with view all set
            if (current_user_can('moderate')) {
                $redirect_url = bbp_add_view_all($redirect_url);
                // User cannot moderate, so redirect to forum
            } else {
                $redirect_url = bbp_get_forum_permalink($forum_id);
            }
        }
        // Allow to be filtered
        $redirect_url = apply_filters('bbp_new_topic_redirect_to', $redirect_url, $redirect_to, $topic_id);
        /** Successful Save ***************************************************/
        // Redirect back to new topic
        wp_safe_redirect($redirect_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, 'bbpress'));
    }
}
예제 #18
0
파일: group.php 프로젝트: hscale/webento
 /**
  * Save the Group Forum data on edit
  *
  * @since bbPress (r3465)
  * @uses bbp_new_forum_handler() To check for forum creation
  * @uses bbp_edit_forum_handler() To check for forum edit
  */
 public function edit_screen_save()
 {
     // Bail if not a POST action
     if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
         return;
     }
     check_admin_referer('groups_edit_save_' . $this->slug);
     $edit_forum = !empty($_POST['bbp-edit-group-forum']) ? true : false;
     $forum_id = 0;
     $group_id = bp_get_current_group_id();
     $forum_ids = array_values(bbp_get_group_forum_ids($group_id));
     // Normalize group forum relationships now
     if (!empty($forum_ids)) {
         // Loop through forums, and make sure they exist
         foreach ($forum_ids as $forum_id) {
             // Look for forum
             $forum = bbp_get_forum($forum_id);
             // No forum exists, so break the relationship
             if (empty($forum)) {
                 $this->remove_forum(array('forum_id' => $forum_id));
                 unset($forum_ids[$forum_id]);
             }
         }
         // No support for multiple forums yet
         $forum_id = (int) (is_array($forum_ids) ? $forum_ids[0] : $forum_ids);
     }
     // Update the group forum setting
     $group = new BP_Groups_Group($group_id);
     $group->enable_forum = $edit_forum;
     $group->save();
     // Create a new forum
     if (empty($forum_id) && true === $edit_forum) {
         // Set the default forum status
         switch ($group->status) {
             case 'hidden':
                 $status = bbp_get_hidden_status_id();
                 break;
             case 'private':
                 $status = bbp_get_private_status_id();
                 break;
             case 'public':
             default:
                 $status = bbp_get_public_status_id();
                 break;
         }
         // Create the initial forum
         $forum_id = bbp_insert_forum(array('post_parent' => bbp_get_group_forums_root_id(), 'post_title' => $group->name, 'post_content' => $group->description, 'post_status' => $status));
         // Run the BP-specific functions for new groups
         $this->new_forum(array('forum_id' => $forum_id));
     }
     // Redirect after save
     bp_core_redirect(trailingslashit(bp_get_group_permalink(buddypress()->groups->current_group) . '/admin/' . $this->slug));
 }