bbp_toggle_reply_handler( string $action = '' )

Handles the front end spamming/unspamming and trashing/untrashing/deleting of replies

Description

Parameters

$action

(Optional) The requested action to compare this function to

Default value: ''

Source

File: bp-forums/replies/functions.php

1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
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!', 'buddyboss' ) );
        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!', 'buddyboss' ) : __( '<strong>ERROR</strong>: There was a problem marking the reply as spam!', 'buddyboss' );
            $view_all = !$is_spam;
 
            break;
 
        // 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!', 'buddyboss' );
 
                    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!', 'buddyboss' );
 
                    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!', 'buddyboss' );
 
                    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 );
    }
}

Changelog

Changelog
Version Description
bbPress (r2740) Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.