/** * Handles the front end opening/closing, spamming/unspamming, * sticking/unsticking and trashing/untrashing/deleting of topics * * @since 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 wp_safe_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'); // 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 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); } wp_safe_redirect($redirect); // For good measure exit; // Handle errors } else { bbp_add_error('bbp_toggle_topic', $failure); } }
/** * 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); } }
/** * This method converts old closed topics to bbPress closed topics. * * @since 2.6.0 bbPress (r5425) * * @uses bbp_close_topic() to close topics properly * */ public function convert_topic_closed_topics($start) { $has_update = false; if (!empty($this->sync_table)) { $query = 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_old_closed_status_id" AND meta_value = "closed" LIMIT ' . $start . ', ' . $this->max_rows; } else { $query = 'SELECT post_id AS value_id, meta_value FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_old_closed_status_id" AND meta_value = "closed" LIMIT ' . $start . ', ' . $this->max_rows; } update_option('_bbp_converter_query', $query); $closed_topic = $this->wpdb->get_results($query); foreach ((array) $closed_topic as $row) { bbp_close_topic($row->value_id); $has_update = true; } return !$has_update; }
/** * 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); }
/** * @covers ::bbp_get_user_topic_count_raw */ public function test_bbp_get_user_topic_count_raw() { $u = $this->factory->user->create(); $t = $this->factory->topic->create_many(3, array('post_author' => $u)); $count = bbp_get_user_topic_count_raw($u); $this->assertSame(3, $count); $t = $this->factory->topic->create_many(3, array('post_author' => $u)); $count = bbp_get_user_topic_count_raw($u); $this->assertSame(6, $count); $t = $this->factory->topic->create(array('post_author' => $u)); bbp_close_topic($t); $count = bbp_get_user_topic_count_raw($u); $this->assertSame(7, $count); }