Changeset 6133
- Timestamp:
- 12/08/2016 02:23:07 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/replies/functions.php
r6036 r6133 1616 1616 } 1617 1617 1618 // Setup possible get actions 1619 $possible_actions = array( 1620 'bbp_toggle_reply_spam', 1621 'bbp_toggle_reply_trash', 1622 'bbp_toggle_reply_approve' 1623 ); 1618 // Get possible reply-handler actions 1619 $possible_actions = bbp_get_reply_handler_actions(); 1624 1620 1625 1621 // Bail if actions aren't meant for this function 1626 if ( ! in_array( $action, $possible_actions ) ) {1622 if ( ! in_array( $action, $possible_actions, true ) ) { 1627 1623 return; 1628 1624 } 1629 1625 1630 $failure = ''; // Empty failure string1631 $view_all = false; // Assume not viewing all1632 1626 $reply_id = (int) $_GET['reply_id']; // What's the reply id? 1633 $success = false; // Flag1634 1627 $post_data = array( 'ID' => $reply_id ); // Prelim array 1635 1628 … … 1641 1634 1642 1635 // What is the user doing here? 1643 if ( ! current_user_can( 'edit_reply', $reply->ID ) || ( 'bbp_toggle_reply_trash' === $action && !current_user_can( 'delete_reply', $reply->ID ) ) ) {1636 if ( ! current_user_can( 'edit_reply', $reply->ID ) || ( 'bbp_toggle_reply_trash' === $action && ! current_user_can( 'delete_reply', $reply->ID ) ) ) { 1644 1637 bbp_add_error( 'bbp_toggle_reply_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that!', 'bbpress' ) ); 1645 1638 return; 1646 1639 } 1647 1640 1641 // Do the reply toggling 1642 $retval = bbp_toggle_reply( array( 1643 'id' => $reply_id, 1644 'action' => $action, 1645 'data' => $post_data 1646 ) ); 1647 1648 // Do additional reply toggle actions 1649 do_action( 'bbp_toggle_reply_handler', $retval['status'], $post_data, $action ); 1650 1651 // Redirect back to reply 1652 if ( ( false !== $retval['status'] ) && ! is_wp_error( $retval['status'] ) ) { 1653 bbp_redirect( $retval['redirect_to'] ); 1654 1655 // Handle errors 1656 } else { 1657 bbp_add_error( 'bbp_toggle_reply', $retval['message'] ); 1658 } 1659 } 1660 1661 /** 1662 * Do the actual reply toggling 1663 * 1664 * This function is used by `bbp_toggle_reply_handler()` to do the actual heavy 1665 * lifting when it comes to toggling replies. It only really makes sense to call 1666 * within that context, so if you need to call this function directly, make sure 1667 * you're also doing what the handler does too. 1668 * 1669 * @since 2.6.0 1670 * @access private 1671 * 1672 * @param array $args 1673 */ 1674 function bbp_toggle_reply( $args = array() ) { 1675 1676 // Parse the arguments 1677 $r = bbp_parse_args( $args, array( 1678 'id' => 0, 1679 'action' => '', 1680 'data' => array() 1681 ) ); 1682 1683 // Build the nonce suffix 1684 $nonce_suffix = bbp_get_reply_post_type() . '_' . (int) $r['id']; 1685 1686 // Default return values 1687 $retval = array( 1688 'status' => 0, 1689 'message' => '', 1690 'redirect_to' => bbp_get_reply_url( $r['id'], bbp_get_redirect_to() ), 1691 'view_all' => false 1692 ); 1693 1648 1694 // What action are we trying to perform? 1649 switch ( $ action) {1695 switch ( $r['action'] ) { 1650 1696 1651 1697 // Toggle approve 1652 1698 case 'bbp_toggle_reply_approve' : 1653 check_ajax_referer( 'approve-reply_' . $reply_id);1654 1655 $is_approve = bbp_is_reply_pending( $reply_id);1656 $ success = $is_approve ? bbp_approve_reply( $reply_id ) : bbp_unapprove_reply( $reply_id);1657 $ failure= $is_approve ? __( '<strong>ERROR</strong>: There was a problem approving the reply!', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem unapproving the reply!', 'bbpress' );1658 $ view_all= ! $is_approve;1699 check_ajax_referer( "approve-{$nonce_suffix}" ); 1700 1701 $is_approve = bbp_is_reply_pending( $r['id'] ); 1702 $retval['status'] = $is_approve ? bbp_approve_reply( $r['id'] ) : bbp_unapprove_reply( $r['id'] ); 1703 $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' ); 1704 $retval['view_all'] = ! $is_approve; 1659 1705 1660 1706 break; … … 1662 1708 // Toggle spam 1663 1709 case 'bbp_toggle_reply_spam' : 1664 check_ajax_referer( 'spam-reply_' . $reply_id);1665 1666 $is_spam = bbp_is_reply_spam( $reply_id);1667 $ success = $is_spam ? bbp_unspam_reply( $reply_id ) : bbp_spam_reply( $reply_id);1668 $ 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' );1669 $ view_all= ! $is_spam;1710 check_ajax_referer( "spam-{$nonce_suffix}" ); 1711 1712 $is_spam = bbp_is_reply_spam( $r['id'] ); 1713 $retval['status'] = $is_spam ? bbp_unspam_reply( $r['id'] ) : bbp_spam_reply( $r['id'] ); 1714 $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' ); 1715 $retval['view_all'] = ! $is_spam; 1670 1716 1671 1717 break; … … 1674 1720 case 'bbp_toggle_reply_trash' : 1675 1721 1676 $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false; 1677 1722 // Subaction? 1723 $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ), true ) 1724 ? $_GET['sub_action'] 1725 : false; 1726 1727 // Bail if no subaction 1678 1728 if ( empty( $sub_action ) ) { 1679 1729 break; 1680 1730 } 1681 1731 1732 // Which subaction? 1682 1733 switch ( $sub_action ) { 1683 1734 case 'trash': 1684 check_ajax_referer( 'trash-' . bbp_get_reply_post_type() . '_' . $reply_id);1685 1686 $ view_all= true;1687 $ success = wp_trash_post( $reply_id);1688 $ failure= __( '<strong>ERROR</strong>: There was a problem trashing the reply!', 'bbpress' );1735 check_ajax_referer( "trash-{$nonce_suffix}" ); 1736 1737 $retval['view_all'] = true; 1738 $retval['status'] = wp_trash_post( $r['id'] ); 1739 $retval['message'] = __( '<strong>ERROR</strong>: There was a problem trashing the reply!', 'bbpress' ); 1689 1740 1690 1741 break; 1691 1742 1692 1743 case 'untrash': 1693 check_ajax_referer( 'untrash-' . bbp_get_reply_post_type() . '_' . $reply_id);1694 1695 $ success = wp_untrash_post( $reply_id);1696 $ failure= __( '<strong>ERROR</strong>: There was a problem untrashing the reply!', 'bbpress' );1744 check_ajax_referer( "untrash-{$nonce_suffix}" ); 1745 1746 $retval['status'] = wp_untrash_post( $r['id'] ); 1747 $retval['message'] = __( '<strong>ERROR</strong>: There was a problem untrashing the reply!', 'bbpress' ); 1697 1748 1698 1749 break; 1699 1750 1700 1751 case 'delete': 1701 check_ajax_referer( 'delete-' . bbp_get_reply_post_type() . '_' . $reply_id);1702 1703 $ success = wp_delete_post( $reply_id);1704 $ failure= __( '<strong>ERROR</strong>: There was a problem deleting the reply!', 'bbpress' );1752 check_ajax_referer( "delete-{$nonce_suffix}" ); 1753 1754 $retval['status'] = wp_delete_post( $r['id'] ); 1755 $retval['message'] = __( '<strong>ERROR</strong>: There was a problem deleting the reply!', 'bbpress' ); 1705 1756 1706 1757 break; … … 1710 1761 } 1711 1762 1712 // Do additional reply toggle actions 1713 do_action( 'bbp_toggle_reply_handler', $success, $post_data, $action ); 1714 1715 // No errors 1716 if ( ( false !== $success ) && !is_wp_error( $success ) ) { 1717 1718 /** Redirect **********************************************************/ 1719 1720 // Redirect to 1721 $redirect_to = bbp_get_redirect_to(); 1722 1723 // Get the reply URL 1724 $reply_url = bbp_get_reply_url( $reply_id, $redirect_to ); 1725 1726 // Add view all if needed 1727 if ( ! empty( $view_all ) ) { 1728 $reply_url = bbp_add_view_all( $reply_url, true ); 1729 } 1730 1731 // Redirect back to reply 1732 bbp_redirect( $reply_url ); 1733 1734 // Handle errors 1735 } else { 1736 bbp_add_error( 'bbp_toggle_reply', $failure ); 1737 } 1763 // Add view all if needed 1764 if ( ! empty( $retval['view_all'] ) ) { 1765 $retval['redirect_to'] = bbp_add_view_all( $retval['redirect_to'], true ); 1766 } 1767 1768 // Filter & return 1769 return apply_filters( 'bbp_do_toggle_reply_handler', $retval, $r, $args ); 1738 1770 } 1739 1771 … … 1756 1788 bbp_get_pending_status_id() => _x( 'Pending', 'Mark reply as pending', 'bbpress' ), 1757 1789 ), $reply_id ); 1790 } 1791 1792 /** 1793 * Return array of available reply handler actions 1794 * 1795 * @since 2.6.0 bbPress (rxxxx) 1796 */ 1797 function bbp_get_reply_handler_actions() { 1798 return apply_filters( 'bbp_get_reply_handler_actions', array( 1799 'bbp_toggle_reply_spam', 1800 'bbp_toggle_reply_trash', 1801 'bbp_toggle_reply_approve' 1802 ) ); 1758 1803 } 1759 1804
Note: See TracChangeset
for help on using the changeset viewer.