Exemple #1
0
 /**
  * Toggle topic
  *
  * Handles the admin-side opening/closing, sticking/unsticking and
  * spamming/unspamming of topics
  *
  * @since 2.0.0 bbPress (r2727)
  *
  * @uses bbp_get_topic() To get the topic
  * @uses current_user_can() To check if the user is capable of editing
  *                           the topic
  * @uses wp_die() To die if the user isn't capable or the post wasn't
  *                 found
  * @uses check_admin_referer() To verify the nonce and check referer
  * @uses bbp_is_topic_open() To check if the topic is open
  * @uses bbp_close_topic() To close the topic
  * @uses bbp_open_topic() To open the topic
  * @uses bbp_is_topic_sticky() To check if the topic is a sticky or
  *                              super sticky
  * @uses bbp_unstick_topic() To unstick the topic
  * @uses bbp_stick_topic() To stick the topic
  * @uses bbp_is_topic_spam() To check if the topic is marked as spam
  * @uses bbp_unspam_topic() To unmark the topic as spam
  * @uses bbp_spam_topic() To mark the topic as spam
  * @uses do_action() Calls 'bbp_toggle_topic_admin' with success, post
  *                    data, action and message
  * @uses add_query_arg() To add custom args to the url
  * @uses bbp_redirect() Redirect the page to custom url
  */
 public function toggle_topic()
 {
     if ($this->bail()) {
         return;
     }
     // Only proceed if GET is a topic toggle action
     if (bbp_is_get_request() && !empty($_GET['action']) && in_array($_GET['action'], array('bbp_toggle_topic_close', 'bbp_toggle_topic_stick', 'bbp_toggle_topic_spam', 'bbp_toggle_topic_approve')) && !empty($_GET['topic_id'])) {
         $action = $_GET['action'];
         // What action is taking place?
         $topic_id = (int) $_GET['topic_id'];
         // What's the topic id?
         $success = false;
         // Flag
         $post_data = array('ID' => $topic_id);
         // Prelim array
         $topic = bbp_get_topic($topic_id);
         // Verify the topic id
         // Bail if topic is missing
         if (empty($topic)) {
             wp_die(__('The topic was not found!', 'bbpress'));
         }
         // What is the user doing here?
         if (!current_user_can('moderate', $topic->ID)) {
             wp_die(__('You do not have the permission to do that!', 'bbpress'));
         }
         switch ($action) {
             case 'bbp_toggle_topic_approve':
                 check_admin_referer('approve-topic_' . $topic_id);
                 $is_approve = bbp_is_topic_pending($topic_id);
                 $message = true === $is_approve ? 'approved' : 'unapproved';
                 $success = true === $is_approve ? bbp_approve_topic($topic_id) : bbp_unapprove_topic($topic_id);
                 break;
             case 'bbp_toggle_topic_close':
                 check_admin_referer('close-topic_' . $topic_id);
                 $is_open = bbp_is_topic_open($topic_id);
                 $message = true === $is_open ? 'closed' : 'opened';
                 $success = true === $is_open ? bbp_close_topic($topic_id) : bbp_open_topic($topic_id);
                 break;
             case 'bbp_toggle_topic_stick':
                 check_admin_referer('stick-topic_' . $topic_id);
                 $is_sticky = bbp_is_topic_sticky($topic_id);
                 $is_super = false === $is_sticky && !empty($_GET['super']) && "1" === $_GET['super'] ? true : false;
                 $message = true === $is_sticky ? 'unstuck' : 'stuck';
                 $message = true === $is_super ? 'super_sticky' : $message;
                 $success = true === $is_sticky ? bbp_unstick_topic($topic_id) : bbp_stick_topic($topic_id, $is_super);
                 break;
             case 'bbp_toggle_topic_spam':
                 check_admin_referer('spam-topic_' . $topic_id);
                 $is_spam = bbp_is_topic_spam($topic_id);
                 $message = true === $is_spam ? 'unspammed' : 'spammed';
                 $success = true === $is_spam ? bbp_unspam_topic($topic_id) : bbp_spam_topic($topic_id);
                 break;
         }
         $message = array('bbp_topic_toggle_notice' => $message, 'topic_id' => $topic->ID);
         if (false === $success || is_wp_error($success)) {
             $message['failed'] = '1';
         }
         // Do additional topic toggle actions (admin side)
         do_action('bbp_toggle_topic_admin', $success, $post_data, $action, $message);
         // Redirect back to the topic
         $redirect = add_query_arg($message, remove_query_arg(array('action', 'topic_id')));
         bbp_redirect($redirect);
     }
 }
/**
 * Do the actual topic toggling
 *
 * This function is used by `bbp_toggle_topic_handler()` to do the actual heavy
 * lifting when it comes to toggling topic. It only really makes sense to call
 * within that context, so if you need to call this function directly, make sure
 * you're also doing what the handler does too.
 *
 * @since 2.6.0  bbPress (r6133)
 * @access private
 *
 * @param array $args
 */
function bbp_toggle_topic($args = array())
{
    // Parse the arguments
    $r = bbp_parse_args($args, array('id' => 0, 'action' => '', 'sub_action' => '', 'data' => array()));
    // Build the nonce suffix
    $nonce_suffix = bbp_get_topic_post_type() . '_' . (int) $r['id'];
    // Default return values
    $retval = array('status' => 0, 'message' => '', 'redirect_to' => bbp_get_topic_permalink($r['id'], bbp_get_redirect_to()), 'view_all' => false);
    // What action are we trying to perform?
    switch ($r['action']) {
        // Toggle approve/unapprove
        case 'bbp_toggle_topic_approve':
            check_ajax_referer("approve-{$nonce_suffix}");
            $is_pending = bbp_is_topic_pending($r['id']);
            $retval['view_all'] = !$is_pending;
            // Toggle
            $retval['status'] = true === $is_pending ? bbp_approve_topic($r['id']) : bbp_unapprove_topic($r['id']);
            // Feedback
            $retval['message'] = true === $is_pending ? __('<strong>ERROR</strong>: There was a problem approving the topic.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem unapproving the topic.', 'bbpress');
            break;
            // Toggle open/close
        // Toggle open/close
        case 'bbp_toggle_topic_close':
            check_ajax_referer("close-{$nonce_suffix}");
            $is_open = bbp_is_topic_open($r['id']);
            // Toggle
            $retval['status'] = true === $is_open ? bbp_close_topic($r['id']) : bbp_open_topic($r['id']);
            // Feedback
            $retval['message'] = true === $is_open ? __('<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress');
            break;
            // Toggle sticky/super-sticky/unstick
        // Toggle sticky/super-sticky/unstick
        case 'bbp_toggle_topic_stick':
            check_ajax_referer("stick-{$nonce_suffix}");
            $is_sticky = bbp_is_topic_sticky($r['id']);
            $is_super = false === $is_sticky && !empty($_GET['super']) && "1" === $_GET['super'] ? true : false;
            // Toggle
            $retval['status'] = true === $is_sticky ? bbp_unstick_topic($r['id']) : bbp_stick_topic($r['id'], $is_super);
            // Feedback
            $retval['message'] = true === $is_sticky ? __('<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem sticking the topic.', 'bbpress');
            break;
            // Toggle spam
        // Toggle spam
        case 'bbp_toggle_topic_spam':
            check_ajax_referer("spam-{$nonce_suffix}");
            $is_spam = bbp_is_topic_spam($r['id']);
            $retval['view_all'] = !$is_spam;
            // Toggle
            $retval['status'] = true === $is_spam ? bbp_unspam_topic($r['id']) : bbp_spam_topic($r['id']);
            // Feedback
            $retval['message'] = true === $is_spam ? __('<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem marking the topic as spam.', 'bbpress');
            break;
            // Toggle trash
        // Toggle trash
        case 'bbp_toggle_topic_trash':
            switch ($r['sub_action']) {
                case 'trash':
                    check_ajax_referer("trash-{$nonce_suffix}");
                    $retval['view_all'] = true;
                    $retval['status'] = wp_trash_post($r['id']);
                    $retval['message'] = __('<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress');
                    break;
                case 'untrash':
                    check_ajax_referer("untrash-{$nonce_suffix}");
                    $retval['status'] = wp_untrash_post($r['id']);
                    $retval['message'] = __('<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress');
                    break;
                case 'delete':
                    check_ajax_referer("delete-{$nonce_suffix}");
                    $retval['status'] = wp_delete_post($r['id']);
                    $retval['message'] = __('<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress');
                    break;
            }
            break;
    }
    // Maybe redirect back to the topic's forum
    if (isset($r['sub_action']) && 'delete' === $r['sub_action']) {
        $retval['redirect_to'] = bbp_get_forum_permalink($retval['status']->post_parent);
    }
    // Add view all if needed
    if (!empty($retval['view_all'])) {
        $retval['redirect_to'] = bbp_add_view_all($retval['redirect_to'], true);
    }
    // Filter & return
    return apply_filters('bbp_toggle_topic', $retval, $r, $args);
}
Exemple #3
0
/**
 * Handles the front end opening/closing, spamming/unspamming,
 * sticking/unsticking and trashing/untrashing/deleting of topics
 *
 * @since 2.0.0 bbPress (r2727)
 *
 * @param string $action The requested action to compare this function to
 * @uses bbp_get_topic() To get the topic
 * @uses current_user_can() To check if the user is capable of editing or
 *                           deleting the topic
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses check_ajax_referer() To verify the nonce and check the referer
 * @uses bbp_is_topic_open() To check if the topic is open
 * @uses bbp_close_topic() To close the topic
 * @uses bbp_open_topic() To open the topic
 * @uses bbp_is_topic_sticky() To check if the topic is a sticky
 * @uses bbp_unstick_topic() To unstick the topic
 * @uses bbp_stick_topic() To stick the topic
 * @uses bbp_is_topic_spam() To check if the topic is marked as spam
 * @uses bbp_spam_topic() To make the topic as spam
 * @uses bbp_unspam_topic() To unmark the topic as spam
 * @uses wp_trash_post() To trash the topic
 * @uses wp_untrash_post() To untrash the topic
 * @uses wp_delete_post() To delete the topic
 * @uses do_action() Calls 'bbp_toggle_topic_handler' with success, post data
 *                    and action
 * @uses bbp_get_forum_permalink() To get the forum link
 * @uses bbp_get_topic_permalink() To get the topic link
 * @uses bbp_redirect() To redirect to the topic
 * @uses bbPress::errors:add() To log the error messages
 */
function bbp_toggle_topic_handler($action = '')
{
    // Bail if required GET actions aren't passed
    if (empty($_GET['topic_id'])) {
        return;
    }
    // Setup possible get actions
    $possible_actions = array('bbp_toggle_topic_close', 'bbp_toggle_topic_stick', 'bbp_toggle_topic_spam', 'bbp_toggle_topic_trash', 'bbp_toggle_topic_approve');
    // Bail if actions aren't meant for this function
    if (!in_array($action, $possible_actions)) {
        return;
    }
    $failure = '';
    // Empty failure string
    $view_all = false;
    // Assume not viewing all
    $topic_id = (int) $_GET['topic_id'];
    // What's the topic id?
    $success = false;
    // Flag
    $post_data = array('ID' => $topic_id);
    // Prelim array
    $redirect = '';
    // Empty redirect URL
    // Make sure topic exists
    $topic = bbp_get_topic($topic_id);
    if (empty($topic)) {
        return;
    }
    // What is the user doing here?
    if (!current_user_can('edit_topic', $topic->ID) || 'bbp_toggle_topic_trash' === $action && !current_user_can('delete_topic', $topic->ID)) {
        bbp_add_error('bbp_toggle_topic_permission', __('<strong>ERROR:</strong> You do not have the permission to do that.', 'bbpress'));
        return;
    }
    // What action are we trying to perform?
    switch ($action) {
        // Toggle approve/unapprove
        case 'bbp_toggle_topic_approve':
            check_ajax_referer('approve-topic_' . $topic_id);
            $is_pending = bbp_is_topic_pending($topic_id);
            $success = true === $is_pending ? bbp_approve_topic($topic_id) : bbp_unapprove_topic($topic_id);
            $failure = true === $is_pending ? __('<strong>ERROR</strong>: There was a problem approving the topic.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem unapproving the topic.', 'bbpress');
            break;
            // Toggle open/close
        // Toggle open/close
        case 'bbp_toggle_topic_close':
            check_ajax_referer('close-topic_' . $topic_id);
            $is_open = bbp_is_topic_open($topic_id);
            $success = true === $is_open ? bbp_close_topic($topic_id) : bbp_open_topic($topic_id);
            $failure = true === $is_open ? __('<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress');
            break;
            // Toggle sticky/super-sticky/unstick
        // Toggle sticky/super-sticky/unstick
        case 'bbp_toggle_topic_stick':
            check_ajax_referer('stick-topic_' . $topic_id);
            $is_sticky = bbp_is_topic_sticky($topic_id);
            $is_super = false === $is_sticky && !empty($_GET['super']) && "1" === $_GET['super'] ? true : false;
            $success = true === $is_sticky ? bbp_unstick_topic($topic_id) : bbp_stick_topic($topic_id, $is_super);
            $failure = true === $is_sticky ? __('<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem sticking the topic.', 'bbpress');
            break;
            // Toggle spam
        // Toggle spam
        case 'bbp_toggle_topic_spam':
            check_ajax_referer('spam-topic_' . $topic_id);
            $is_spam = bbp_is_topic_spam($topic_id);
            $success = true === $is_spam ? bbp_unspam_topic($topic_id) : bbp_spam_topic($topic_id);
            $failure = true === $is_spam ? __('<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem marking the topic as spam.', 'bbpress');
            $view_all = !$is_spam;
            break;
            // Toggle trash
        // Toggle trash
        case 'bbp_toggle_topic_trash':
            $sub_action = !empty($_GET['sub_action']) && in_array($_GET['sub_action'], array('trash', 'untrash', 'delete')) ? $_GET['sub_action'] : false;
            if (empty($sub_action)) {
                break;
            }
            switch ($sub_action) {
                case 'trash':
                    check_ajax_referer('trash-' . bbp_get_topic_post_type() . '_' . $topic_id);
                    $view_all = true;
                    $success = wp_trash_post($topic_id);
                    $failure = __('<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress');
                    break;
                case 'untrash':
                    check_ajax_referer('untrash-' . bbp_get_topic_post_type() . '_' . $topic_id);
                    $success = wp_untrash_post($topic_id);
                    $failure = __('<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress');
                    break;
                case 'delete':
                    check_ajax_referer('delete-' . bbp_get_topic_post_type() . '_' . $topic_id);
                    $success = wp_delete_post($topic_id);
                    $failure = __('<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress');
                    break;
            }
            break;
    }
    // Do additional topic toggle actions
    do_action('bbp_toggle_topic_handler', $success, $post_data, $action);
    // No errors
    if (false !== $success && !is_wp_error($success)) {
        // Redirect back to the topic's forum
        if (isset($sub_action) && 'delete' === $sub_action) {
            $redirect = bbp_get_forum_permalink($success->post_parent);
            // Redirect back to the topic
        } else {
            // Get the redirect detination
            $permalink = bbp_get_topic_permalink($topic_id);
            $redirect = bbp_add_view_all($permalink, $view_all);
        }
        bbp_redirect($redirect);
        // Handle errors
    } else {
        bbp_add_error('bbp_toggle_topic', $failure);
    }
}
Exemple #4
0
 /**
  * @covers ::bbp_get_forum_freshness_link
  */
 public function test_bbp_get_forum_freshness_link_with_unpublished_replies()
 {
     if (is_multisite()) {
         $this->markTestSkipped('Skipping URL tests in multiste for now.');
     }
     $now = time();
     $post_date_t1 = date('Y-m-d H:i:s', $now - 60 * 60 * 18);
     // 18 hours ago
     $post_date_t2 = date('Y-m-d H:i:s', $now - 60 * 60 * 16);
     // 16 hours ago
     $post_date_t3 = date('Y-m-d H:i:s', $now - 60 * 60 * 14);
     // 14 hours ago
     $post_date_t4 = date('Y-m-d H:i:s', $now - 60 * 60 * 12);
     // 12 hours ago
     $post_date_t5 = date('Y-m-d H:i:s', $now - 60 * 60 * 10);
     // 1o hours ago
     $f = $this->factory->forum->create();
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('No Topics', $link);
     $t1 = $this->factory->topic->create(array('post_title' => 'Topic 1', 'post_parent' => $f, 'post_date' => $post_date_t1, 'topic_meta' => array('forum_id' => $f)));
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('<a href="http://' . WP_TESTS_DOMAIN . '/?topic=topic-1" title="Topic 1">18 hours ago</a>', $link);
     $t2 = $this->factory->topic->create(array('post_title' => 'Topic 2', 'post_parent' => $f, 'post_date' => $post_date_t2, 'topic_meta' => array('forum_id' => $f)));
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('<a href="http://' . WP_TESTS_DOMAIN . '/?topic=topic-2" title="Topic 2">16 hours ago</a>', $link);
     bbp_spam_topic($t2);
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('<a href="http://' . WP_TESTS_DOMAIN . '/?topic=topic-1" title="Topic 1">18 hours ago</a>', $link);
     $t3 = $this->factory->topic->create(array('post_title' => 'Topic 3', 'post_parent' => $f, 'post_date' => $post_date_t3, 'topic_meta' => array('forum_id' => $f)));
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('<a href="http://' . WP_TESTS_DOMAIN . '/?topic=topic-3" title="Topic 3">14 hours ago</a>', $link);
     // Todo: Use bbp_trash_topic() and not wp_trash_post()
     wp_trash_post($t3);
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('<a href="http://' . WP_TESTS_DOMAIN . '/?topic=topic-1" title="Topic 1">18 hours ago</a>', $link);
     $t4 = $this->factory->topic->create(array('post_title' => 'Topic 4', 'post_parent' => $f, 'post_date' => $post_date_t4, 'topic_meta' => array('forum_id' => $f)));
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('<a href="http://' . WP_TESTS_DOMAIN . '/?topic=topic-4" title="Topic 4">12 hours ago</a>', $link);
     bbp_unapprove_topic($t4);
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('<a href="http://' . WP_TESTS_DOMAIN . '/?topic=topic-1" title="Topic 1">18 hours ago</a>', $link);
     bbp_unspam_topic($t2);
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('<a href="http://' . WP_TESTS_DOMAIN . '/?topic=topic-2" title="Topic 2">16 hours ago</a>', $link);
     // Todo: Use bbp_untrash_topic() and not wp_untrash_post()
     wp_untrash_post($t3);
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('<a href="http://' . WP_TESTS_DOMAIN . '/?topic=topic-3" title="Topic 3">14 hours ago</a>', $link);
     bbp_approve_topic($t4);
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('<a href="http://' . WP_TESTS_DOMAIN . '/?topic=topic-4" title="Topic 4">12 hours ago</a>', $link);
     $t5 = $this->factory->topic->create(array('post_title' => 'Topic 5', 'post_parent' => $f, 'post_date' => $post_date_t5, 'topic_meta' => array('forum_id' => $f)));
     $link = bbp_get_forum_freshness_link($f);
     $this->assertSame('<a href="http://' . WP_TESTS_DOMAIN . '/?topic=topic-5" title="Topic 5">10 hours ago</a>', $link);
 }
Exemple #5
0
 /**
  * Generic function to test the forum counts on a approved/unapproved topic
  */
 public function test_bbp_forum_approved_unapproved_topic_counts()
 {
     $f = $this->factory->forum->create();
     $t = $this->factory->topic->create_many(3, array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f)));
     $r1 = $this->factory->reply->create_many(2, array('post_parent' => $t[1], 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t[1])));
     $r2 = $this->factory->reply->create_many(2, array('post_parent' => $t[2], 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t[2])));
     $count = bbp_update_forum_topic_count($f);
     $this->assertSame(3, $count);
     $count = bbp_update_forum_topic_count_hidden($f);
     $this->assertSame(0, $count);
     $count = bbp_update_forum_reply_count($f);
     $this->assertSame(4, $count);
     bbp_unapprove_topic($t[2]);
     $count = bbp_get_forum_topic_count($f, true, true);
     $this->assertSame(2, $count);
     $count = bbp_get_forum_topic_count_hidden($f, true, true);
     $this->assertSame(1, $count);
     $count = bbp_get_forum_reply_count($f, true, true);
     $this->assertSame(2, $count);
     bbp_approve_topic($t[2]);
     $count = bbp_get_forum_topic_count($f, true, true);
     $this->assertSame(3, $count);
     $count = bbp_get_forum_topic_count_hidden($f, true, true);
     $this->assertSame(0, $count);
     $count = bbp_get_forum_reply_count($f, true, true);
     $this->assertSame(4, $count);
 }