Esempio n. 1
0
 /**
  * @covers ::bbp_is_reply_spam
  */
 public function test_bbp_is_reply_spam()
 {
     $forum_id = $this->factory->forum->create();
     $topic_id = $this->factory->topic->create(array('post_parent' => $forum_id, 'topic_meta' => array('forum_id' => $forum_id)));
     $reply_id = $this->factory->reply->create(array('post_parent' => $topic_id, 'reply_meta' => array('forum_id' => $forum_id, 'topic_id' => $topic_id)));
     $r = $this->factory->reply->create(array('post_parent' => $topic_id, 'reply_meta' => array('forum_id' => $forum_id, 'topic_id' => $topic_id)));
     bbp_spam_reply($r);
     $reply_spam = bbp_is_reply_spam($r);
     $this->assertTrue($reply_spam);
     bbp_unspam_reply($r);
     $reply_spam = bbp_is_reply_spam($r);
     $this->assertFalse($reply_spam);
 }
Esempio n. 2
0
 /**
  * Reply Row actions
  *
  * Remove the quick-edit action link under the reply title and add the
  * content and spam link
  *
  * @since 2.0.0 bbPress (r2577)
  *
  * @param array $actions Actions
  * @param array $reply Reply object
  * @uses bbp_get_reply_post_type() To get the reply post type
  * @uses bbp_reply_content() To output reply content
  * @uses bbp_get_reply_url() To get the reply link
  * @uses bbp_get_reply_title() To get the reply title
  * @uses current_user_can() To check if the current user can edit or
  *                           delete the reply
  * @uses bbp_is_reply_spam() To check if the reply is marked as spam
  * @uses get_post_type_object() To get the reply post type object
  * @uses add_query_arg() To add custom args to the url
  * @uses remove_query_arg() To remove custom args from the url
  * @uses wp_nonce_url() To nonce the url
  * @uses get_delete_post_link() To get the delete post link of the reply
  * @return array $actions Actions
  */
 public function row_actions($actions, $reply)
 {
     if ($this->bail()) {
         return $actions;
     }
     unset($actions['inline hide-if-no-js']);
     // Reply view links to topic
     $actions['view'] = '<a href="' . esc_url(bbp_get_reply_url($reply->ID)) . '" title="' . esc_attr(sprintf(__('View &#8220;%s&#8221;', 'bbpress'), bbp_get_reply_title($reply->ID))) . '" rel="permalink">' . esc_html__('View', 'bbpress') . '</a>';
     // User cannot view replies in trash
     if (bbp_get_trash_status_id() === $reply->post_status && !current_user_can('view_trash')) {
         unset($actions['view']);
     }
     // Only show the actions if the user is capable of viewing them
     if (current_user_can('moderate', $reply->ID)) {
         // Show the 'approve' link on pending posts only and 'unapprove' on published posts only
         $approve_uri = wp_nonce_url(add_query_arg(array('reply_id' => $reply->ID, 'action' => 'bbp_toggle_reply_approve'), remove_query_arg(array('bbp_reply_toggle_notice', 'reply_id', 'failed', 'super'))), 'approve-reply_' . $reply->ID);
         if (bbp_is_reply_published($reply->ID)) {
             $actions['unapproved'] = '<a href="' . esc_url($approve_uri) . '" title="' . esc_attr__('Unapprove this reply', 'bbpress') . '">' . _x('Unapprove', 'Unapprove reply', 'bbpress') . '</a>';
         } elseif (!bbp_is_reply_private($reply->ID)) {
             $actions['approved'] = '<a href="' . esc_url($approve_uri) . '" title="' . esc_attr__('Approve this reply', 'bbpress') . '">' . _x('Approve', 'Approve reply', 'bbpress') . '</a>';
         }
         // Show the 'spam' link on published and pending replies and 'not spam' on spammed replies
         if (in_array($reply->post_status, array(bbp_get_public_status_id(), bbp_get_pending_status_id(), bbp_get_spam_status_id()))) {
             $spam_uri = wp_nonce_url(add_query_arg(array('reply_id' => $reply->ID, 'action' => 'bbp_toggle_reply_spam'), remove_query_arg(array('bbp_reply_toggle_notice', 'reply_id', 'failed', 'super'))), 'spam-reply_' . $reply->ID);
             if (bbp_is_reply_spam($reply->ID)) {
                 $actions['spam'] = '<a href="' . esc_url($spam_uri) . '" title="' . esc_attr__('Mark the reply as not spam', 'bbpress') . '">' . esc_html__('Not spam', 'bbpress') . '</a>';
             } else {
                 $actions['spam'] = '<a href="' . esc_url($spam_uri) . '" title="' . esc_attr__('Mark this reply as spam', 'bbpress') . '">' . esc_html__('Spam', 'bbpress') . '</a>';
             }
         }
     }
     // Trash
     if (current_user_can('delete_reply', $reply->ID)) {
         if (bbp_get_trash_status_id() === $reply->post_status) {
             $post_type_object = get_post_type_object(bbp_get_reply_post_type());
             $actions['untrash'] = "<a title='" . esc_attr__('Restore this item from the Trash', 'bbpress') . "' href='" . esc_url(add_query_arg(array('_wp_http_referer' => add_query_arg(array('post_type' => bbp_get_reply_post_type()), admin_url('edit.php'))), wp_nonce_url(admin_url(sprintf($post_type_object->_edit_link . '&amp;action=untrash', $reply->ID)), 'untrash-' . $reply->post_type . '_' . $reply->ID))) . "'>" . esc_html__('Restore', 'bbpress') . "</a>";
         } elseif (EMPTY_TRASH_DAYS) {
             $actions['trash'] = "<a class='submitdelete' title='" . esc_attr__('Move this item to the Trash', 'bbpress') . "' href='" . esc_url(add_query_arg(array('_wp_http_referer' => add_query_arg(array('post_type' => bbp_get_reply_post_type()), admin_url('edit.php'))), get_delete_post_link($reply->ID))) . "'>" . esc_html__('Trash', 'bbpress') . "</a>";
         }
         if (bbp_get_trash_status_id() === $reply->post_status || !EMPTY_TRASH_DAYS) {
             $actions['delete'] = "<a class='submitdelete' title='" . esc_attr__('Delete this item permanently', 'bbpress') . "' href='" . esc_url(add_query_arg(array('_wp_http_referer' => add_query_arg(array('post_type' => bbp_get_reply_post_type()), admin_url('edit.php'))), get_delete_post_link($reply->ID, '', true))) . "'>" . esc_html__('Delete Permanently', 'bbpress') . "</a>";
         } elseif (bbp_get_spam_status_id() === $reply->post_status) {
             unset($actions['trash']);
         }
     }
     return $actions;
 }
Esempio n. 3
0
/**
 * Return the spam link of the reply
 *
 * @since bbPress (r2740)
 *
 * @param mixed $args This function supports these arguments:
 *  - id: Reply id
 *  - link_before: HTML before the link
 *  - link_after: HTML after the link
 *  - spam_text: Spam text
 *  - unspam_text: Unspam text
 * @uses bbp_get_reply_id() To get the reply id
 * @uses bbp_get_reply() To get the reply
 * @uses current_user_can() To check if the current user can edit the
 *                           reply
 * @uses bbp_is_reply_spam() To check if the reply 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 bbp_get_reply_edit_url() To get the reply edit url
 * @uses apply_filters() Calls 'bbp_get_reply_spam_link' with the reply
 *                        spam link and args
 * @return string Reply spam link
 */
function bbp_get_reply_spam_link($args = '')
{
    // Parse arguments against default values
    $r = bbp_parse_args($args, array('id' => 0, 'link_before' => '', 'link_after' => '', 'spam_text' => esc_html__('Spam', 'bbpress'), 'unspam_text' => esc_html__('Unspam', 'bbpress')), 'get_reply_spam_link');
    $reply = bbp_get_reply(bbp_get_reply_id((int) $r['id']));
    if (empty($reply) || !current_user_can('moderate', $reply->ID)) {
        return;
    }
    $display = bbp_is_reply_spam($reply->ID) ? $r['unspam_text'] : $r['spam_text'];
    $uri = add_query_arg(array('action' => 'bbp_toggle_reply_spam', 'reply_id' => $reply->ID));
    $uri = wp_nonce_url($uri, 'spam-reply_' . $reply->ID);
    $retval = $r['link_before'] . '<a href="' . esc_url($uri) . '" class="bbp-reply-spam-link">' . $display . '</a>' . $r['link_after'];
    return apply_filters('bbp_get_reply_spam_link', $retval, $r);
}
/**
 * Handles the front end spamming/unspamming and trashing/untrashing/deleting of
 * replies
 *
 * @since bbPress (r2740)
 *
 * @param string $action The requested action to compare this function to
 * @uses bbp_get_reply() To get the reply
 * @uses current_user_can() To check if the user is capable of editing or
 *                           deleting the reply
 * @uses check_ajax_referer() To verify the nonce and check the referer
 * @uses bbp_get_reply_post_type() To get the reply post type
 * @uses bbp_is_reply_spam() To check if the reply is marked as spam
 * @uses bbp_spam_reply() To make the reply as spam
 * @uses bbp_unspam_reply() To unmark the reply as spam
 * @uses wp_trash_post() To trash the reply
 * @uses wp_untrash_post() To untrash the reply
 * @uses wp_delete_post() To delete the reply
 * @uses do_action() Calls 'bbp_toggle_reply_handler' with success, post data
 *                    and action
 * @uses bbp_get_reply_url() To get the reply url
 * @uses wp_safe_redirect() To redirect to the reply
 * @uses bbPress::errors:add() To log the error messages
 */
function bbp_toggle_reply_handler($action = '')
{
    // Bail if required GET actions aren't passed
    if (empty($_GET['reply_id'])) {
        return;
    }
    // Setup possible get actions
    $possible_actions = array('bbp_toggle_reply_spam', 'bbp_toggle_reply_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
    $reply_id = (int) $_GET['reply_id'];
    // What's the reply id?
    $success = false;
    // Flag
    $post_data = array('ID' => $reply_id);
    // Prelim array
    // Make sure reply exists
    $reply = bbp_get_reply($reply_id);
    if (empty($reply)) {
        return;
    }
    // What is the user doing here?
    if (!current_user_can('edit_reply', $reply->ID) || 'bbp_toggle_reply_trash' === $action && !current_user_can('delete_reply', $reply->ID)) {
        bbp_add_error('bbp_toggle_reply_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 spam
        case 'bbp_toggle_reply_spam':
            check_ajax_referer('spam-reply_' . $reply_id);
            $is_spam = bbp_is_reply_spam($reply_id);
            $success = $is_spam ? bbp_unspam_reply($reply_id) : bbp_spam_reply($reply_id);
            $failure = $is_spam ? __('<strong>ERROR</strong>: There was a problem unmarking the reply as spam!', 'bbpress') : __('<strong>ERROR</strong>: There was a problem marking the reply as spam!', 'bbpress');
            $view_all = !$is_spam;
            break;
            // Toggle trash
        // Toggle trash
        case 'bbp_toggle_reply_trash':
            $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_reply_post_type() . '_' . $reply_id);
                    $view_all = true;
                    $success = wp_trash_post($reply_id);
                    $failure = __('<strong>ERROR</strong>: There was a problem trashing the reply!', 'bbpress');
                    break;
                case 'untrash':
                    check_ajax_referer('untrash-' . bbp_get_reply_post_type() . '_' . $reply_id);
                    $success = wp_untrash_post($reply_id);
                    $failure = __('<strong>ERROR</strong>: There was a problem untrashing the reply!', 'bbpress');
                    break;
                case 'delete':
                    check_ajax_referer('delete-' . bbp_get_reply_post_type() . '_' . $reply_id);
                    $success = wp_delete_post($reply_id);
                    $failure = __('<strong>ERROR</strong>: There was a problem deleting the reply!', 'bbpress');
                    break;
            }
            break;
    }
    // Do additional reply toggle actions
    do_action('bbp_toggle_reply_handler', $success, $post_data, $action);
    // No errors
    if (false !== $success && !is_wp_error($success)) {
        /** Redirect **********************************************************/
        // Redirect to
        $redirect_to = bbp_get_redirect_to();
        // Get the reply URL
        $reply_url = bbp_get_reply_url($reply_id, $redirect_to);
        // Add view all if needed
        if (!empty($view_all)) {
            $reply_url = bbp_add_view_all($reply_url, true);
        }
        // Redirect back to reply
        wp_safe_redirect($reply_url);
        // For good measure
        exit;
        // Handle errors
    } else {
        bbp_add_error('bbp_toggle_reply', $failure);
    }
}
Esempio n. 5
0
/**
 * Return the spam link of the reply
 *
 * @since bbPress (r2740)
 *
 * @param mixed $args This function supports these arguments:
 *  - id: Reply id
 *  - link_before: HTML before the link
 *  - link_after: HTML after the link
 *  - spam_text: Spam text
 *  - unspam_text: Unspam text
 * @uses bbp_get_reply_id() To get the reply id
 * @uses bbp_get_reply() To get the reply
 * @uses current_user_can() To check if the current user can edit the
 *                           reply
 * @uses bbp_is_reply_spam() To check if the reply 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 bbp_get_reply_edit_url() To get the reply edit url
 * @uses apply_filters() Calls 'bbp_get_reply_spam_link' with the reply
 *                        spam link and args
 * @return string Reply spam link
 */
function bbp_get_reply_spam_link($args = '')
{
    $defaults = array('id' => 0, 'link_before' => '', 'link_after' => '', 'spam_text' => __('Spam', 'bbpress'), 'unspam_text' => __('Unspam', 'bbpress'));
    $r = bbp_parse_args($args, $defaults, 'get_reply_spam_link');
    extract($r);
    $reply = bbp_get_reply(bbp_get_reply_id((int) $id));
    if (empty($reply) || !current_user_can('moderate', $reply->ID)) {
        return;
    }
    $display = bbp_is_reply_spam($reply->ID) ? $unspam_text : $spam_text;
    $uri = add_query_arg(array('action' => 'bbp_toggle_reply_spam', 'reply_id' => $reply->ID));
    $uri = esc_url(wp_nonce_url($uri, 'spam-reply_' . $reply->ID));
    $retval = $link_before . '<a href="' . $uri . '">' . $display . '</a>' . $link_after;
    return apply_filters('bbp_get_reply_spam_link', $retval, $args);
}
Esempio n. 6
0
/**
 * Do the actual reply toggling
 *
 * This function is used by `bbp_toggle_reply_handler()` to do the actual heavy
 * lifting when it comes to toggling replies. 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_reply($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_reply_post_type() . '_' . (int) $r['id'];
    // Default return values
    $retval = array('status' => 0, 'message' => '', 'redirect_to' => bbp_get_reply_url($r['id'], bbp_get_redirect_to()), 'view_all' => false);
    // What action are we trying to perform?
    switch ($r['action']) {
        // Toggle approve
        case 'bbp_toggle_reply_approve':
            check_ajax_referer("approve-{$nonce_suffix}");
            $is_approve = bbp_is_reply_pending($r['id']);
            $retval['status'] = $is_approve ? bbp_approve_reply($r['id']) : bbp_unapprove_reply($r['id']);
            $retval['message'] = $is_approve ? __('<strong>ERROR</strong>: There was a problem approving the reply.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem unapproving the reply.', 'bbpress');
            $retval['view_all'] = !$is_approve;
            break;
            // Toggle spam
        // Toggle spam
        case 'bbp_toggle_reply_spam':
            check_ajax_referer("spam-{$nonce_suffix}");
            $is_spam = bbp_is_reply_spam($r['id']);
            $retval['status'] = $is_spam ? bbp_unspam_reply($r['id']) : bbp_spam_reply($r['id']);
            $retval['message'] = $is_spam ? __('<strong>ERROR</strong>: There was a problem unmarking the reply as spam.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem marking the reply as spam.', 'bbpress');
            $retval['view_all'] = !$is_spam;
            break;
            // Toggle trash
        // Toggle trash
        case 'bbp_toggle_reply_trash':
            // Which subaction?
            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 reply.', '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 reply.', '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 reply.', 'bbpress');
                    break;
            }
            break;
    }
    // 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_reply', $retval, $r, $args);
}
Esempio n. 7
0
 /**
  * Add a Spam row action
  * 
  * @param unknown_type $actions
  * @param unknown_type $post
  */
 function post_row_actions($actions, $post)
 {
     global $wpdb;
     $the_id = $post->ID;
     // For replies:
     if ($post->post_type == 'reply' && $post->post_status == 'pending' && !array_key_exists('spam', $actions)) {
         // Mark posts as spam
         $spam_uri = esc_url(wp_nonce_url(add_query_arg(array('reply_id' => $the_id, 'action' => 'bbp_toggle_reply_spam'), remove_query_arg(array('bbp_reply_toggle_notice', 'reply_id', 'failed', 'super'))), 'spam-reply_' . $the_id));
         if (bbp_is_reply_spam($the_id)) {
             $actions['spam'] = '<a href="' . $spam_uri . '" title="' . esc_attr__('Mark the reply as not spam', 'bbpress') . '">' . __('Not spam', 'bbpress') . '</a>';
         } else {
             $actions['spam'] = '<a href="' . $spam_uri . '" title="' . esc_attr__('Mark this reply as spam', 'bbpress') . '">' . __('Spam', 'bbpress') . '</a>';
         }
     }
     // For Topics:
     if ($post->post_type == 'topic' && $post->post_status == 'pending' && !array_key_exists('spam', $actions)) {
         // Mark posts as spam
         $spam_uri = esc_url(wp_nonce_url(add_query_arg(array('topic_id' => $the_id, 'action' => 'bbp_toggle_topic_spam'), remove_query_arg(array('bbp_topic_toggle_notice', 'topic_id', 'failed', 'super'))), 'spam-topic_' . $the_id));
         if (bbp_is_topic_spam($the_id)) {
             $actions['spam'] = '<a href="' . $spam_uri . '" title="' . esc_attr__('Mark the topic as not spam', 'bbpress') . '">' . __('Not spam', 'bbpress') . '</a>';
         } else {
             $actions['spam'] = '<a href="' . $spam_uri . '" title="' . esc_attr__('Mark this topic as spam', 'bbpress') . '">' . __('Spam', 'bbpress') . '</a>';
         }
     }
     return $actions;
 }