Skip to:
Content

bbPress.org

Changeset 6101


Ignore:
Timestamp:
10/13/2016 03:27:09 PM (4 years ago)
Author:
netweb
Message:

General - Administration: Introduce bulk actions to spam and unspam topics and replies in wp-admin

This Friday #yolo changeset comes care of the upcoming WordPress 4.7 release via #WP16031 / wp:changeset:38647

Props jmdodd.
See #2246.

Location:
trunk/src/includes/admin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/replies.php

    r6056 r6101  
    4848     *
    4949     * @since 2.0.0 bbPress (r2646)
     50     * @since 2.6.0 bbPress (r6101) Added bulk actions
    5051     *
    5152     * @access private
     
    6465        // Messages
    6566        add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) );
     67
     68        // Reply bulk actions, added in WordPress 4.7, see #WP16031
     69        if ( bbp_get_major_wp_version() >= 4.7 ) {
     70            add_filter( 'bulk_actions-edit-reply',        array( $this, 'bulk_actions' ) );
     71            add_filter( 'handle_bulk_actions-edit-reply', array( $this, 'handle_bulk_actions' ), 10, 3 );
     72            add_filter( 'bulk_post_updated_messages',     array( $this, 'bulk_post_updated_messages' ), 10, 2 );
     73        }
    6674
    6775        // Reply column headers.
     
    175183            'title'     => __( 'Bulk Actions', 'bbpress' ),
    176184            'content'   =>
    177                 '<p>' . __( 'You can also edit or move multiple replies to the trash at once. Select the replies you want to act on using the checkboxes, then select the action you want to take from the Bulk Actions menu and click Apply.',           'bbpress' ) . '</p>' .
     185                '<p>' . __( 'You can also edit, spam, or move multiple replies to the trash at once. Select the replies you want to act on using the checkboxes, then select the action you want to take from the Bulk Actions menu and click Apply.',           'bbpress' ) . '</p>' .
    178186                '<p>' . __( 'When using Bulk Edit, you can change the metadata (categories, author, etc.) for all selected replies at once. To remove a reply from the grouping, just click the x next to its name in the Bulk Edit area that appears.', 'bbpress' ) . '</p>'
    179187        ) );
     
    245253            '<p>' . __( '<a href="https://bbpress.org/forums/" target="_blank">bbPress Support Forums</a>', 'bbpress' ) . '</p>'
    246254        );
     255    }
     256
     257    /**
     258     * Add spam/unspam bulk actions to the bulk action dropdown.
     259     *
     260     * @since 2.6.0 bbPress (r6101)
     261     *
     262     * @param array $actions The list of bulk actions.
     263     * @return array The filtered list of bulk actions.
     264     */
     265    public function bulk_actions( $actions ) {
     266
     267        if ( $this->bail() ) {
     268            return $actions;
     269        }
     270
     271        if ( current_user_can( 'moderate' ) ) {
     272            if ( bbp_get_spam_status_id() === get_query_var( 'post_status' ) ) {
     273                $actions['unspam'] = __( 'Unspam', 'bbpress' );
     274            } else {
     275                $actions['spam'] = __( 'Spam', 'bbpress' );
     276            }
     277        }
     278
     279        return $actions;
     280    }
     281
     282    /**
     283     * Add custom bulk action updated messages for replies.
     284     *
     285     * @since 2.6.0 bbPress (r6101)
     286     *
     287     * @param array $bulk_messages Arrays of messages, each keyed by the corresponding post type.
     288     * @param array $bulk_counts   Array of item counts for each message, used to build internationalized strings.
     289     */
     290    public function bulk_post_updated_messages( $bulk_messages, $bulk_counts ) {
     291
     292        if ( $this->bail() ) {
     293            return $bulk_messages;
     294        }
     295
     296        $bulk_messages['reply']['updated'] = _n( '%s reply updated.', '%s replies updated.', $bulk_counts['updated'] );
     297        $bulk_messages['reply']['locked']  = ( 1 === $bulk_counts['locked'] ) ? __( '1 reply not updated, somebody is editing it.' ) :
     298                                                  _n( '%s reply not updated, somebody is editing it.', '%s replies not updated, somebody is editing them.', $bulk_counts['locked'] );
     299        return $bulk_messages;
     300    }
     301
     302    /**
     303     * Handle spam/unspam bulk actions.
     304     *
     305     * @since 2.6.0 bbPress (r6101)
     306     *
     307     * @param string $sendback The sendback URL.
     308     * @param string $doaction The action to be taken.
     309     * @param array  $post_ids The post IDS to take the action on.
     310     * @return string The sendback URL.
     311     */
     312    public function handle_bulk_actions( $sendback, $doaction, $post_ids ) {
     313
     314        if ( $this->bail() ) {
     315            return $sendback;
     316        }
     317
     318        $sendback = remove_query_arg( array( 'spam', 'unspam' ), $sendback );
     319        $updated = $locked = 0;
     320
     321        if ( 'spam' === $doaction ) {
     322
     323            foreach ( (array) $post_ids as $post_id ) {
     324                if ( ! current_user_can( 'moderate', $post_id ) ) {
     325                    wp_die( __( 'Sorry, you are not allowed to spam this item.', 'bbpress' ) );
     326                }
     327
     328                if ( wp_check_post_lock( $post_id ) ) {
     329                    $locked++;
     330                    continue;
     331                }
     332
     333                if ( ! bbp_spam_reply( $post_id ) ) {
     334                    wp_die( __( 'Error in spamming reply.', 'bbpress' ) );
     335                }
     336
     337                $updated++;
     338            }
     339
     340            $sendback = add_query_arg( array( 'updated' => $updated, 'ids' => join( ',', $post_ids ), 'locked' => $locked ), $sendback );
     341
     342        } elseif ( 'unspam' === $doaction ) {
     343
     344            foreach ( (array) $post_ids as $post_id ) {
     345                if ( ! current_user_can( 'moderate', $post_id ) ) {
     346                    wp_die( __( 'Sorry, you are not allowed to unspam this reply.', 'bbpress' ) );
     347                }
     348
     349                if ( wp_check_post_lock( $post_id ) ) {
     350                    $locked++;
     351                    continue;
     352                }
     353
     354                if ( ! bbp_unspam_reply( $post_id ) ) {
     355                    wp_die( __( 'Error in unspamming reply.', 'bbpress' ) );
     356                }
     357
     358                $updated++;
     359            }
     360
     361            $sendback = add_query_arg( array( 'updated' => $updated, 'ids' => join( ',', $post_ids ), 'locked' => $locked ), $sendback );
     362        }
     363
     364        return $sendback;
    247365    }
    248366
  • trunk/src/includes/admin/topics.php

    r6099 r6101  
    4848     *
    4949     * @since 2.0.0 bbPress (r2646)
     50     * @since 2.6.0 bbPress (r6101) Added bulk actions
    5051     *
    5152     * @access private
     
    6465        // Messages
    6566        add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) );
     67
     68        // Topic bulk actions, added in WordPress 4.7, see #WP16031.
     69        if ( bbp_get_major_wp_version() >= 4.7 ) {
     70            add_filter( 'bulk_actions-edit-topic',        array( $this, 'bulk_actions' ) );
     71            add_filter( 'handle_bulk_actions-edit-topic', array( $this, 'handle_bulk_actions' ), 10, 3 );
     72            add_filter( 'bulk_post_updated_messages',     array( $this, 'bulk_post_updated_messages' ), 10, 2 );
     73        }
    6674
    6775        // Topic column headers.
     
    178186            'title'     => __( 'Bulk Actions', 'bbpress' ),
    179187            'content'   =>
    180                 '<p>' . __( 'You can also edit or move multiple topics to the trash at once. Select the topics you want to act on using the checkboxes, then select the action you want to take from the Bulk Actions menu and click Apply.',           'bbpress' ) . '</p>' .
     188                '<p>' . __( 'You can also edit, spam, or move multiple topics to the trash at once. Select the topics you want to act on using the checkboxes, then select the action you want to take from the Bulk Actions menu and click Apply.',           'bbpress' ) . '</p>' .
    181189                '<p>' . __( 'When using Bulk Edit, you can change the metadata (categories, author, etc.) for all selected topics at once. To remove a topic from the grouping, just click the x next to its name in the Bulk Edit area that appears.', 'bbpress' ) . '</p>'
    182190        ) );
     
    249257    }
    250258
     259    /**
     260     * Add spam/unspam bulk actions to the bulk action dropdown.
     261     *
     262     * @since 2.6.0 bbPress (r6101)
     263     *
     264     * @param array $actions The list of bulk actions.
     265     * @return array The filtered list of bulk actions.
     266     */
     267    public function bulk_actions( $actions ) {
     268
     269        if ( $this->bail() ) {
     270            return $actions;
     271        }
     272
     273        if ( current_user_can( 'moderate' ) ) {
     274            if ( bbp_get_spam_status_id() === get_query_var( 'post_status' ) ) {
     275                $actions['unspam'] = __( 'Unspam', 'bbpress' );
     276            } else {
     277                $actions['spam'] = __( 'Spam', 'bbpress' );
     278            }
     279        }
     280
     281        return $actions;
     282    }
     283
     284    /**
     285     * Add custom bulk action updated messages for topics.
     286     *
     287     * @since 2.6.0 bbPress (r6101)
     288     *
     289     * @param array $bulk_messages Arrays of messages, each keyed by the corresponding post type.
     290     * @param array $bulk_counts   Array of item counts for each message, used to build internationalized strings.
     291     */
     292    public function bulk_post_updated_messages( $bulk_messages, $bulk_counts ) {
     293
     294        if ( $this->bail() ) {
     295            return $bulk_messages;
     296        }
     297
     298        $bulk_messages['topic']['updated'] = _n( '%s topic updated.', '%s topics updated.', $bulk_counts['updated'] );
     299        $bulk_messages['topic']['locked']  = ( 1 === $bulk_counts['locked'] ) ? __( '1 topic not updated, somebody is editing it.' ) :
     300                                                  _n( '%s topic not updated, somebody is editing it.', '%s topics not updated, somebody is editing them.', $bulk_counts['locked'] );
     301        return $bulk_messages;
     302    }
     303
     304    /**
     305     * Handle spam/unspam bulk actions.
     306     *
     307     * @since 2.6.0 bbPress (r6101)
     308     *
     309     * @param string $sendback The sendback URL.
     310     * @param string $doaction The action to be taken.
     311     * @param array  $post_ids The post IDS to take the action on.
     312     * @return string The sendback URL.
     313     */
     314    public function handle_bulk_actions( $sendback, $doaction, $post_ids ) {
     315
     316        if ( $this->bail() ) {
     317            return $sendback;
     318        }
     319
     320        $sendback = remove_query_arg( array( 'spam', 'unspam' ), $sendback );
     321        $updated = $locked = 0;
     322
     323        if ( 'spam' === $doaction ) {
     324
     325            foreach ( (array) $post_ids as $post_id ) {
     326                if ( ! current_user_can( 'moderate', $post_id ) ) {
     327                    wp_die( __( 'Sorry, you are not allowed to spam this item.', 'bbpress' ) );
     328                }
     329
     330                if ( wp_check_post_lock( $post_id ) ) {
     331                    $locked++;
     332                    continue;
     333                }
     334
     335                if ( ! bbp_spam_topic( $post_id ) ) {
     336                    wp_die( __( 'Error in spamming topic.', 'bbpress' ) );
     337                }
     338
     339                $updated++;
     340            }
     341
     342            $sendback = add_query_arg( array( 'updated' => $updated, 'ids' => join( ',', $post_ids ), 'locked' => $locked ), $sendback );
     343
     344        } elseif ( 'unspam' === $doaction ) {
     345
     346            foreach ( (array) $post_ids as $post_id ) {
     347                if ( ! current_user_can( 'moderate', $post_id ) ) {
     348                    wp_die( __( 'Sorry, you are not allowed to unspam this topic.', 'bbpress' ) );
     349                }
     350
     351                if ( wp_check_post_lock( $post_id ) ) {
     352                    $locked++;
     353                    continue;
     354                }
     355
     356                if ( ! bbp_unspam_topic( $post_id ) ) {
     357                    wp_die( __( 'Error in unspamming topic.', 'bbpress' ) );
     358                }
     359
     360                $updated++;
     361            }
     362
     363            $sendback = add_query_arg( array( 'updated' => $updated, 'ids' => join( ',', $post_ids ), 'locked' => $locked ), $sendback );
     364        }
     365
     366        return $sendback;
     367    }
    251368    /**
    252369     * Add the topic attributes metabox
Note: See TracChangeset for help on using the changeset viewer.