/**
* Process the user's bbPress topic selections when the post is saved
*/
function process_topic_option($post_ID, $post)
{
/** Don't process on AJAX-based auto-drafts */
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
/** Don't process on initial page load auto drafts */
if ($post->post_status == 'auto-draft') {
return;
}
/** Only process for post types we specify */
if (!in_array($post->post_type, apply_filters('bbppt_eligible_post_types', array('post', 'page')))) {
return;
}
// TODO: combine existing topic and draft settings branches
if (isset($_POST['bbpress_topic'])) {
/**
* Post was created through the full post editor form
* Check the POST for settings
*/
if (!empty($_POST['bbpress_topic']['enabled'])) {
$bbppt_options = $_POST['bbpress_topic'];
$create_topic = true;
$use_defaults = isset($bbppt_options['use_defaults']);
bbppt_debug('Processing a topic for regular post ' . $post_ID . ' with the following settings: ' . print_r($bbppt_options, true));
bbppt_debug('Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults);
} else {
$create_topic = false;
$bbppt_options = array();
bbppt_debug('NOT creating a topic for regular post ' . $post_ID);
}
} else {
if (get_post_meta($post_ID, 'bbpress_discussion_topic_id', true)) {
/**
* If post has an existing topic, use its settings
*/
$bbppt_options = $this->get_topic_options_for_post($post_ID);
$create_topic = !empty($bbppt_options['enabled']);
$use_defaults = !empty($bbppt_options['use_defaults']);
bbppt_debug('Processing topic for existing post ' . $post_ID . ' with the following settings: ' . print_r($bbppt_options, true));
bbppt_debug('Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults);
} else {
if ($this->has_draft_settings($post)) {
/**
* If the post has draft settings saved, use draft settings
*/
$bbppt_options = $this->get_draft_settings($post);
$create_topic = !empty($bbppt_options['enabled']);
$use_defaults = !empty($bbppt_options['use_defaults']);
bbppt_debug('Processing a topic for draft post ' . $post_ID . ' with the following settings: ' . print_r($bbppt_options, true));
bbppt_debug('Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults);
} else {
/**
* Post was created through some other means (including XML-RPC) - use defaults
*/
$bbppt_options = get_option('bbpress_discussion_defaults');
$create_topic = !empty($bbppt_options['enabled']);
$use_defaults = true;
$bbppt_options['use_defaults'] = $use_defaults;
bbppt_debug('Processing a topic for unattended post ' . $post_ID . ' with the following settings: ' . print_r($bbppt_options, true));
bbppt_debug('Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults);
}
}
}
/** If post is saved as a draft, store selected settings for publication later */
if (in_array($post->post_status, apply_filters('bbppt_draft_post_status', array('draft', 'future')))) {
$this->set_draft_settings($bbppt_options, $post);
return;
}
/** Only process further when the post is published */
if (!in_array($post->post_status, apply_filters('bbppt_eligible_post_status', array('publish', 'private')))) {
return;
}
/**
* The user requested to use a bbPress topic for discussion
*/
if ($create_topic) {
if (!function_exists('bbp_has_forums')) {
?>
<br /><p><?php
_e('bbPress Topics for Posts cannot process this request because it cannot detect your bbPress setup.', 'bbpress-post-topics');
?>
</p><?php
return;
}
$topic_slug = isset($bbppt_options['slug']) ? $bbppt_options['slug'] : '';
$topic_forum = isset($bbppt_options['forum_id']) ? (int) $bbppt_options['forum_id'] : 0;
if (!$use_defaults) {
$topic_display = isset($bbppt_options['display']) ? $bbppt_options['display'] : 'topic';
/** Store extra data for xreplies, as well as any custom display formats */
$topic_display_extras = apply_filters('bbppt_store_display_extras', $bbppt_options['display-extras'], $post);
}
if (!empty($topic_slug)) {
/** if user has selected an existing topic */
if (is_numeric($topic_slug)) {
$topic = bbp_get_topic((int) $topic_slug);
} else {
//.........这里部分代码省略.........
/**
* Unspams a topic
*
* @since bbPress (r2740)
*
* @param int $topic_id Topic id
* @uses bbp_get_topic() To get the topic
* @uses do_action() Calls 'bbp_unspam_topic' with the topic id
* @uses get_post_meta() To get the previous status
* @uses delete_post_meta() To delete the previous status meta
* @uses wp_update_post() To update the topic with the new status
* @uses do_action() Calls 'bbp_unspammed_topic' with the topic id
* @return mixed False or {@link WP_Error} on failure, topic id on success
*/
function bbp_unspam_topic($topic_id = 0)
{
// Get the topic
$topic = bbp_get_topic($topic_id);
if (empty($topic)) {
return $topic;
}
// Bail if already not spam
if (bbp_get_spam_status_id() !== $topic->post_status) {
return false;
}
// Execute pre unspam code
do_action('bbp_unspam_topic', $topic_id);
/** Untrash Replies *******************************************************/
// Get the replies that were not previously trashed
$pre_spammed_replies = get_post_meta($topic_id, '_bbp_pre_spammed_replies', true);
// There are replies to untrash
if (!empty($pre_spammed_replies)) {
// Maybe reverse the trashed replies array
if (is_array($pre_spammed_replies)) {
$pre_spammed_replies = array_reverse($pre_spammed_replies);
}
// Loop through replies
foreach ((array) $pre_spammed_replies as $reply) {
wp_untrash_post($reply);
}
}
/** Topic Tags ************************************************************/
// Get pre-spam topic tags
$terms = get_post_meta($topic_id, '_bbp_spam_topic_tags', true);
// Topic had tags before it was spammed
if (!empty($terms)) {
// Set the tax_input of the topic
$topic->tax_input = array(bbp_get_topic_tag_tax_id() => $terms);
// Delete pre-spam topic tag meta
delete_post_meta($topic_id, '_bbp_spam_topic_tags');
}
/** Topic Status **********************************************************/
// Get pre spam status
$topic_status = get_post_meta($topic_id, '_bbp_spam_meta_status', true);
// If no previous status, default to publish
if (empty($topic_status)) {
$topic_status = bbp_get_public_status_id();
}
// Set post status to pre spam
$topic->post_status = $topic_status;
// Delete pre spam meta
delete_post_meta($topic_id, '_bbp_spam_meta_status');
// No revisions
remove_action('pre_post_update', 'wp_save_post_revision');
// Update the topic
$topic_id = wp_update_post($topic);
// Execute post unspam code
do_action('bbp_unspammed_topic', $topic_id);
// Return topic_id
return $topic_id;
}
/**
* Move reply handler
*
* Handles the front end move reply submission
*
* @since bbPress (r4521)
*
* @param string $action The requested action to compare this function to
* @uses bbp_add_error() To add an error message
* @uses bbp_get_reply() To get the reply
* @uses bbp_get_topic() To get the topics
* @uses bbp_verify_nonce_request() To verify the nonce and check the request
* @uses current_user_can() To check if the current user can edit the reply and topics
* @uses bbp_get_topic_post_type() To get the topic post type
* @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
* @uses do_action() Calls 'bbp_pre_move_reply' with the from reply id, source
* and destination topic ids
* @uses bbp_get_reply_post_type() To get the reply post type
* @uses wpdb::prepare() To prepare our sql query
* @uses wpdb::get_results() To execute the sql query and get results
* @uses wp_update_post() To update the replies
* @uses bbp_update_reply_topic_id() To update the reply topic id
* @uses bbp_get_topic_forum_id() To get the topic forum id
* @uses bbp_update_reply_forum_id() To update the reply forum id
* @uses do_action() Calls 'bbp_split_topic_reply' with the reply id and
* destination topic id
* @uses bbp_update_topic_last_reply_id() To update the topic last reply id
* @uses bbp_update_topic_last_active_time() To update the topic last active meta
* @uses do_action() Calls 'bbp_post_split_topic' with the destination and
* source topic ids and source topic's forum id
* @uses bbp_get_topic_permalink() To get the topic permalink
* @uses wp_safe_redirect() To redirect to the topic link
*/
function bbp_move_reply_handler($action = '')
{
// Bail if action is not 'bbp-move-reply'
if ('bbp-move-reply' !== $action) {
return;
}
// Prevent debug notices
$move_reply_id = $destination_topic_id = 0;
$destination_topic_title = '';
$destination_topic = $move_reply = $source_topic = '';
/** Move Reply ***********************************************************/
if (empty($_POST['bbp_reply_id'])) {
bbp_add_error('bbp_move_reply_reply_id', __('<strong>ERROR</strong>: Reply ID to move not found!', 'bbpress'));
} else {
$move_reply_id = (int) $_POST['bbp_reply_id'];
}
$move_reply = bbp_get_reply($move_reply_id);
// Reply exists
if (empty($move_reply)) {
bbp_add_error('bbp_mover_reply_r_not_found', __('<strong>ERROR</strong>: The reply you want to move was not found.', 'bbpress'));
}
/** Topic to Move From ***************************************************/
// Get the reply's current topic
$source_topic = bbp_get_topic($move_reply->post_parent);
// No topic
if (empty($source_topic)) {
bbp_add_error('bbp_move_reply_source_not_found', __('<strong>ERROR</strong>: The topic you want to move from was not found.', 'bbpress'));
}
// Nonce check failed
if (!bbp_verify_nonce_request('bbp-move-reply_' . $move_reply->ID)) {
bbp_add_error('bbp_move_reply_nonce', __('<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress'));
return;
}
// Use cannot edit topic
if (!current_user_can('edit_topic', $source_topic->ID)) {
bbp_add_error('bbp_move_reply_source_permission', __('<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress'));
}
// How to move
if (!empty($_POST['bbp_reply_move_option'])) {
$move_option = (string) trim($_POST['bbp_reply_move_option']);
}
// Invalid move option
if (empty($move_option) || !in_array($move_option, array('existing', 'topic'))) {
bbp_add_error('bbp_move_reply_option', __('<strong>ERROR</strong>: You need to choose a valid move option.', 'bbpress'));
// Valid move option
} else {
// What kind of move
switch ($move_option) {
// Into an existing topic
case 'existing':
// Get destination topic id
if (empty($_POST['bbp_destination_topic'])) {
bbp_add_error('bbp_move_reply_destination_id', __('<strong>ERROR</strong>: Destination topic ID not found!', 'bbpress'));
} else {
$destination_topic_id = (int) $_POST['bbp_destination_topic'];
}
// Get the destination topic
$destination_topic = bbp_get_topic($destination_topic_id);
// No destination topic
if (empty($destination_topic)) {
bbp_add_error('bbp_move_reply_destination_not_found', __('<strong>ERROR</strong>: The topic you want to move to was not found!', 'bbpress'));
}
// User cannot edit the destination topic
if (!current_user_can('edit_topic', $destination_topic->ID)) {
bbp_add_error('bbp_move_reply_destination_permission', __('<strong>ERROR</strong>: You do not have the permissions to edit the destination topic!', 'bbpress'));
}
// Bump the reply position
//.........这里部分代码省略.........
/**
* Return the spam link of the topic
*
* @since bbPress (r2727)
*
* @param mixed $args This function supports these args:
* - id: Optional. Topic id
* - link_before: Before the link
* - link_after: After the link
* - spam_text: Spam text
* - unspam_text: Unspam text
* @uses bbp_get_topic_id() To get the topic id
* @uses bbp_get_topic() To get the topic
* @uses current_user_can() To check if the current user can edit the topic
* @uses bbp_is_topic_spam() To check if the topic is marked as spam
* @uses add_query_arg() To add custom args to the url
* @uses wp_nonce_url() To nonce the url
* @uses esc_url() To escape the url
* @uses apply_filters() Calls 'bbp_get_topic_spam_link' with the link
* and args
* @return string Topic spam link
*/
function bbp_get_topic_spam_link($args = '')
{
$defaults = array('id' => 0, 'link_before' => '', 'link_after' => '', 'sep' => ' | ', 'spam_text' => __('Spam', 'bbpress'), 'unspam_text' => __('Unspam', 'bbpress'));
$r = bbp_parse_args($args, $defaults, 'get_topic_spam_link');
extract($r);
$topic = bbp_get_topic(bbp_get_topic_id((int) $id));
if (empty($topic) || !current_user_can('moderate', $topic->ID)) {
return;
}
$display = bbp_is_topic_spam($topic->ID) ? $unspam_text : $spam_text;
$uri = add_query_arg(array('action' => 'bbp_toggle_topic_spam', 'topic_id' => $topic->ID));
$uri = esc_url(wp_nonce_url($uri, 'spam-topic_' . $topic->ID));
$retval = $link_before . '<a href="' . $uri . '">' . $display . '</a>' . $link_after;
return apply_filters('bbp_get_topic_spam_link', $retval, $args);
}
/**
* Unapproves a topic
*
* @since 2.6.0 bbPress (r5503)
*
* @param int $topic_id Topic id
* @uses bbp_get_topic() To get the topic
* @uses bbp_get_pending_status_id() To get the pending status id
* @uses do_action() Calls 'bbp_unapprove_topic' with the topic id
* @uses remove_action() To remove the auto save post revision action
* @uses wp_update_post() To update the topic with the new status
* @uses do_action() Calls 'bbp_unapproved_topic' with the topic id
* @return mixed False or {@link WP_Error} on failure, topic id on success
*/
function bbp_unapprove_topic($topic_id = 0)
{
// Get topic
$topic = bbp_get_topic($topic_id);
if (empty($topic)) {
return $topic;
}
// Bail if already pending
if (bbp_get_pending_status_id() === $topic->post_status) {
return false;
}
// Execute pre open code
do_action('bbp_unapprove_topic', $topic_id);
// Set pending status
$topic->post_status = bbp_get_pending_status_id();
// No revisions
remove_action('pre_post_update', 'wp_save_post_revision');
// Update topic
$topic_id = wp_update_post($topic);
// Execute post open code
do_action('bbp_unapproved_topic', $topic_id);
// Return topic_id
return $topic_id;
}
/**
* Add a topic to user's subscriptions
*
* @since 2.0.0 bbPress (r2668)
*
* @param int $user_id Optional. User id
* @param int $topic_id Optional. Topic id
* @uses bbp_get_topic() To get the topic
* @uses bbp_add_user_subscription() To add the subscription
* @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
* @return bool Always true
*/
function bbp_add_user_topic_subscription($user_id = 0, $topic_id = 0)
{
// Bail if not enough info
if (empty($user_id) || empty($topic_id)) {
return false;
}
// Bail if no topic
$topic = bbp_get_topic($topic_id);
if (empty($topic)) {
return false;
}
// Bail if already subscribed
if (bbp_is_user_subscribed_to_topic($user_id, $topic_id)) {
return false;
}
// Bail if add fails
if (!bbp_add_user_subscription($user_id, $topic_id)) {
return false;
}
do_action('bbp_add_user_topic_subscription', $user_id, $topic_id);
return true;
}
/**
* Send a Pushover Notification when a moderator is assigned to a topic
*/
function wi_bbp_send_pushover_notification_on_assignment()
{
if (isset($_POST['bbps_support_topic_assign'])) {
if (!function_exists('ckpn_send_notification')) {
return;
}
$user_id = absint($_POST['bbps_assign_list']);
$topic = bbp_get_topic($_POST['bbps_topic_id']);
if ($user_id > 0 && $user_id != get_current_user_id()) {
$title = __('Easy Digital Downloads: A forum topic has been assigned to you', 'wi_bbp');
$message = sprintf(__('You have been assigned to %1$s by another moderator', 'wi_bbp'), $topic->post_title);
$user_push_key = get_user_meta($user_id, 'ckpn_user_key', true);
if ($user_push_key) {
$url = $topic->guid;
$url_title = __('View Topic', 'wi_bbp');
$args = array('title' => $title, 'message' => $message, 'user' => $user_push_key, 'url' => $url, 'url_title' => $url_title);
ckpn_send_notification($args);
}
}
}
}
/**
* AJAX handler to Subscribe/Unsubscribe a user from a topic
*
* @since bbPress (r3732)
*
* @uses bbp_is_subscriptions_active() To check if the subscriptions are active
* @uses bbp_is_user_logged_in() To check if user is logged in
* @uses bbp_get_current_user_id() To get the current user id
* @uses current_user_can() To check if the current user can edit the user
* @uses bbp_get_topic() To get the topic
* @uses wp_verify_nonce() To verify the nonce
* @uses bbp_is_user_subscribed() To check if the topic is in user's subscriptions
* @uses bbp_remove_user_subscriptions() To remove the topic from user's subscriptions
* @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions
* @uses bbp_ajax_response() To return JSON
*/
public function ajax_subscription()
{
// Bail if subscriptions are not active
if (!bbp_is_subscriptions_active()) {
bbp_ajax_response(false, __('Subscriptions are no longer active.', 'bbpress'), 300);
}
// Bail if user is not logged in
if (!is_user_logged_in()) {
bbp_ajax_response(false, __('Please login to subscribe to this topic.', 'bbpress'), 301);
}
// Get user and topic data
$user_id = bbp_get_current_user_id();
$id = intval($_POST['id']);
// Bail if user cannot add favorites for this user
if (!current_user_can('edit_user', $user_id)) {
bbp_ajax_response(false, __('You do not have permission to do this.', 'bbpress'), 302);
}
// Get the topic
$topic = bbp_get_topic($id);
// Bail if topic cannot be found
if (empty($topic)) {
bbp_ajax_response(false, __('The topic could not be found.', 'bbpress'), 303);
}
// Bail if user did not take this action
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'toggle-subscription_' . $topic->ID)) {
bbp_ajax_response(false, __('Are you sure you meant to do that?', 'bbpress'), 304);
}
// Take action
$status = bbp_is_user_subscribed($user_id, $topic->ID) ? bbp_remove_user_subscription($user_id, $topic->ID) : bbp_add_user_subscription($user_id, $topic->ID);
// Bail if action failed
if (empty($status)) {
bbp_ajax_response(false, __('The request was unsuccessful. Please try again.', 'bbpress'), 305);
}
// Put subscription attributes in convenient array
$attrs = array('topic_id' => $topic->ID, 'user_id' => $user_id);
// Action succeeded
bbp_ajax_response(true, bbp_get_user_subscribe_link($attrs, $user_id, false), 200);
}
/**
* Handles the front end opening/closing, spamming/unspamming,
* sticking/unsticking and trashing/untrashing/deleting of topics
*
* @since bbPress (r2727)
*
* @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 add_query_arg() To add args to the url
* @uses wp_safe_redirect() To redirect to the topic
* @uses bbPress::errors:add() To log the error messages
*/
function bbp_toggle_topic_handler()
{
// Bail if not a GET action
if ('GET' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
return;
}
// Bail if required GET actions aren't passed
if (empty($_GET['topic_id']) || empty($_GET['action'])) {
return;
}
// Setup possible get actions
$possible_actions = array('bbp_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($_GET['action'], $possible_actions)) {
return;
}
$failure = '';
// Empty failure string
$view_all = false;
// Assume not viewing all
$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
$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 = $is_open ? bbp_close_topic($topic_id) : bbp_open_topic($topic_id);
$failure = $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 = empty($is_sticky) && !empty($_GET['super']) && 1 == (int) $_GET['super'] ? true : false;
$success = $is_sticky ? bbp_unstick_topic($topic_id) : bbp_stick_topic($topic_id, $is_super);
$failure = $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 = $is_spam ? bbp_unspam_topic($topic_id) : bbp_spam_topic($topic_id);
$failure = $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
//.........这里部分代码省略.........
请发表评论