Making WordPress.org

Changeset 5290


Ignore:
Timestamp:
04/09/2017 05:20:54 AM (8 years ago)
Author:
SergeyBiryukov
Message:

Support Forums: Convert Spam/Unspam, Unapprove/Approve toggle links to explicit actions.

By default, bbPress treats them as toggles, which may cause conflicts if the same action is performed twice by different moderators.

Fixes #2418.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-moderators.php

    r5123 r5290  
    3030        add_filter( 'bbp_topic_admin_links',            array( $this, 'admin_links' ), 10, 2 );
    3131        add_filter( 'bbp_reply_admin_links',            array( $this, 'admin_links' ), 10, 2 );
     32
     33        // Add valid topic and reply actions.
     34        add_filter( 'bbp_get_toggle_topic_actions',     array( $this, 'get_topic_actions' ) );
     35        add_filter( 'bbp_get_toggle_reply_actions',     array( $this, 'get_reply_actions' ) );
     36
     37        // Handle topic and reply actions.
     38        add_filter( 'bbp_toggle_topic',                 array( $this, 'handle_topic_actions' ), 10, 3 );
     39        add_filter( 'bbp_toggle_reply',                 array( $this, 'handle_reply_actions' ), 10, 3 );
     40
     41        // Convert toggle links to explicit actions.
     42        add_filter( 'bbp_get_topic_spam_link',          array( $this, 'convert_toggles_to_actions' ), 10, 3 );
     43        add_filter( 'bbp_get_topic_approve_link',       array( $this, 'convert_toggles_to_actions' ), 10, 3 );
     44        add_filter( 'bbp_get_reply_spam_link',          array( $this, 'convert_toggles_to_actions' ), 10, 3 );
     45        add_filter( 'bbp_get_reply_approve_link',       array( $this, 'convert_toggles_to_actions' ), 10, 3 );
    3246    }
    3347
     
    347361        );
    348362    }
     363
     364    /**
     365     * Add Spam, Unspam, Unapprove, Approve to the list of valid topic actions.
     366     *
     367     * @param array $actions List of topic actions.
     368     * @return array Filtered list of actions.
     369     */
     370    public function get_topic_actions( $actions ) {
     371        $actions = array_merge( $actions, array(
     372            'wporg_bbp_spam_topic',
     373            'wporg_bbp_unspam_topic',
     374            'wporg_bbp_unapprove_topic',
     375            'wporg_bbp_approve_topic',
     376        ) );
     377
     378        return $actions;
     379    }
     380
     381    /**
     382     * Add Spam, Unspam, Unapprove, Approve to the list of valid reply actions.
     383     *
     384     * @param array $actions List of reply actions.
     385     * @return array Filtered list of actions.
     386     */
     387    public function get_reply_actions( $actions ) {
     388        $actions = array_merge( $actions, array(
     389            'wporg_bbp_spam_reply',
     390            'wporg_bbp_unspam_reply',
     391            'wporg_bbp_unapprove_reply',
     392            'wporg_bbp_approve_reply',
     393        ) );
     394
     395        return $actions;
     396    }
     397
     398    /**
     399     * Handle Spam, Unspam, Unapprove, Approve topic actions.
     400     *
     401     * By default, bbPress treats them as toggles, which may cause conflicts if
     402     * the same action is performed twice by different moderators.
     403     *
     404     * @param array $retval {
     405     *    @type int    $status      Result of the action.
     406     *    @type string $message     Message displayed in case of an error.
     407     *    @type string $redirect_to URL to redirect to.
     408     *    @type bool   $view_all    Whether to append 'view=all' to the URL.
     409     * }
     410     * @param array  $r    Parsed arguments.
     411     * @param array  $args Raw arguments.
     412     * @return array
     413     */
     414    public function handle_topic_actions( $retval, $r, $args ) {
     415        $nonce_suffix = bbp_get_topic_post_type() . '_' . (int) $r['id'];
     416
     417        switch ( $r['action'] ) {
     418            case 'wporg_bbp_spam_topic':
     419                check_ajax_referer( "spam-{$nonce_suffix}" );
     420
     421                if ( ! bbp_is_topic_spam( $r['id'] ) ) {
     422                    $retval['status']   = bbp_spam_topic( $r['id'] );
     423                    $retval['message']  = __( '<strong>ERROR</strong>: There was a problem marking the topic as spam.', 'wporg-forums' );
     424                }
     425                $retval['view_all'] = true;
     426
     427                break;
     428
     429            case 'wporg_bbp_unspam_topic':
     430                check_ajax_referer( "spam-{$nonce_suffix}" );
     431
     432                if ( bbp_is_topic_spam( $r['id'] ) ) {
     433                    $retval['status']   = bbp_unspam_topic( $r['id'] );
     434                    $retval['message']  = __( '<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', 'wporg-forums' );
     435                }
     436                $retval['view_all'] = false;
     437
     438                break;
     439
     440            case 'wporg_bbp_unapprove_topic':
     441                check_ajax_referer( "approve-{$nonce_suffix}" );
     442
     443                if ( ! bbp_is_topic_pending( $r['id'] ) ) {
     444                    $retval['status']   = bbp_unapprove_topic( $r['id'] );
     445                    $retval['message']  = __( '<strong>ERROR</strong>: There was a problem unapproving the topic.', 'wporg-forums' );
     446                }
     447                $retval['view_all'] = true;
     448
     449                break;
     450
     451            case 'wporg_bbp_approve_topic':
     452                check_ajax_referer( "approve-{$nonce_suffix}" );
     453
     454                if ( bbp_is_topic_pending( $r['id'] ) ) {
     455                    $retval['status']   = bbp_approve_topic( $r['id'] );
     456                    $retval['message']  = __( '<strong>ERROR</strong>: There was a problem approving the topic.', 'wporg-forums' );
     457                }
     458                $retval['view_all'] = false;
     459
     460                break;
     461        }
     462
     463        // Add 'view=all' if needed
     464        if ( ! empty( $retval['view_all'] ) ) {
     465            $retval['redirect_to'] = bbp_add_view_all( $retval['redirect_to'], true );
     466        }
     467
     468        return $retval;
     469    }
     470
     471    /**
     472     * Handle Spam, Unspam, Unapprove, Approve reply actions.
     473     *
     474     * By default, bbPress treats them as toggles, which may cause conflicts if
     475     * the same action is performed twice by different moderators.
     476     *
     477     * @param array $retval {
     478     *    @type int    $status      Result of the action.
     479     *    @type string $message     Message displayed in case of an error.
     480     *    @type string $redirect_to URL to redirect to.
     481     *    @type bool   $view_all    Whether to append 'view=all' to the URL.
     482     * }
     483     * @param array  $r    Parsed arguments.
     484     * @param array  $args Raw arguments.
     485     * @return array
     486     */
     487    public function handle_reply_actions( $retval, $r, $args ) {
     488        $nonce_suffix = bbp_get_reply_post_type() . '_' . (int) $r['id'];
     489
     490        switch ( $r['action'] ) {
     491            case 'wporg_bbp_spam_reply':
     492                check_ajax_referer( "spam-{$nonce_suffix}" );
     493
     494                if ( ! bbp_is_reply_spam( $r['id'] ) ) {
     495                    $retval['status']   = bbp_spam_reply( $r['id'] );
     496                    $retval['message']  = __( '<strong>ERROR</strong>: There was a problem marking the reply as spam.', 'wporg-forums' );
     497                }
     498                $retval['view_all'] = true;
     499
     500                break;
     501
     502            case 'wporg_bbp_unspam_reply':
     503                check_ajax_referer( "spam-{$nonce_suffix}" );
     504
     505                if ( bbp_is_reply_spam( $r['id'] ) ) {
     506                    $retval['status']   = bbp_unspam_reply( $r['id'] );
     507                    $retval['message']  = __( '<strong>ERROR</strong>: There was a problem unmarking the reply as spam.', 'wporg-forums' );
     508                }
     509                $retval['view_all'] = false;
     510
     511                break;
     512
     513            case 'wporg_bbp_unapprove_reply':
     514                check_ajax_referer( "approve-{$nonce_suffix}" );
     515
     516                if ( ! bbp_is_reply_pending( $r['id'] ) ) {
     517                    $retval['status']   = bbp_unapprove_reply( $r['id'] );
     518                    $retval['message']  = __( '<strong>ERROR</strong>: There was a problem unapproving the reply.', 'wporg-forums' );
     519                }
     520                $retval['view_all'] = true;
     521
     522                break;
     523
     524            case 'wporg_bbp_approve_reply':
     525                check_ajax_referer( "approve-{$nonce_suffix}" );
     526
     527                if ( bbp_is_reply_pending( $r['id'] ) ) {
     528                    $retval['status']   = bbp_approve_reply( $r['id'] );
     529                    $retval['message']  = __( '<strong>ERROR</strong>: There was a problem approving the reply.', 'wporg-forums' );
     530                }
     531                $retval['view_all'] = false;
     532
     533                break;
     534        }
     535
     536        // Add 'view=all' if needed
     537        if ( ! empty( $retval['view_all'] ) ) {
     538            $retval['redirect_to'] = bbp_add_view_all( $retval['redirect_to'], true );
     539        }
     540
     541        return $retval;
     542    }
     543
     544    /**
     545     * Convert Spam/Unspam, Unapprove/Approve toggle links to explicit actions.
     546     *
     547     * @param string $link Link HTML.
     548     * @param array  $r    Parsed arguments.
     549     * @param array  $args Raw arguments.
     550     * @return string Filtered link.
     551     */
     552    public function convert_toggles_to_actions( $link, $r, $args ) {
     553        if ( false !== strpos( $link, 'bbp_toggle_topic_spam' ) ) {
     554            $action = ( bbp_is_topic_spam( $r['id'] ) ) ? 'wporg_bbp_unspam_topic' : 'wporg_bbp_spam_topic';
     555            $link   = str_replace( 'bbp_toggle_topic_spam', $action, $link );
     556
     557        } elseif ( false !== strpos( $link, 'bbp_toggle_topic_approve' ) ) {
     558            $action = ( bbp_is_topic_pending( $r['id'] ) ) ? 'wporg_bbp_approve_topic' : 'wporg_bbp_unapprove_topic';
     559            $link   = str_replace( 'bbp_toggle_topic_approve', $action, $link );
     560
     561        } elseif ( false !== strpos( $link, 'bbp_toggle_reply_spam' ) ) {
     562            $action = ( bbp_is_reply_spam( $r['id'] ) ) ? 'wporg_bbp_unspam_reply' : 'wporg_bbp_spam_reply';
     563            $link   = str_replace( 'bbp_toggle_reply_spam', $action, $link );
     564
     565        } elseif ( false !== strpos( $link, 'bbp_toggle_reply_approve' ) ) {
     566            $action = ( bbp_is_reply_pending( $r['id'] ) ) ? 'wporg_bbp_approve_reply' : 'wporg_bbp_unapprove_reply';
     567            $link   = str_replace( 'bbp_toggle_reply_approve', $action, $link );
     568
     569        }
     570
     571        return $link;
     572    }
    349573}
Note: See TracChangeset for help on using the changeset viewer.