| @@ -1,20 +1,32 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Delete Forum functionality | |
| 4 | + * | |
| 5 | + * @package Forumax | |
| 6 | + */ | |
| 7 | + | |
| 8 | +defined( 'ABSPATH' ) || exit; | |
| 9 | + | |
| 2 | 10 | // AJAX handler to delete a forum |
| 3 | -add_action( 'wp_ajax_bbp_delete_forum', 'bbp_delete_forums' ); | |
| 4 | -function bbp_delete_forums() { | |
| 5 | - | |
| 11 | +add_action( 'wp_ajax_forumax_delete_forum', 'forumax_delete_forums' ); | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Delete a forum and its topics/replies. | |
| 15 | + */ | |
| 16 | +function forumax_delete_forums() { | |
| 17 | + | |
| 6 | 18 | // Check the nonce |
| 7 | 19 | check_ajax_referer( 'bbpc-admin-nonce', 'bbpc_nonce' ); |
| 8 | 20 | |
| 9 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 10 | - wp_send_json_error( 'Unauthorized request.' ); | |
| 11 | - } | |
| 21 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 22 | + wp_send_json_error( 'Unauthorized request.' ); | |
| 23 | + } | |
| 12 | 24 | |
| 13 | - $forum_id = ! empty ( $_POST['forum_id'] ) ? absint( $_POST['forum_id'] ) : 0; | |
| 14 | - if ( empty( $forum_id ) ) { | |
| 15 | - wp_send_json_error( 'Forum ID cannot be empty.' ); | |
| 16 | - } | |
| 25 | + $forum_id = ! empty( $_POST['forum_id'] ) ? absint( $_POST['forum_id'] ) : 0; | |
| 26 | + if ( empty( $forum_id ) ) { | |
| 27 | + wp_send_json_error( 'Forum ID cannot be empty.' ); | |
| 28 | + } | |
| 17 | 29 | |
| 18 | 30 | $children = get_children( [ |
| 19 | 31 | 'post_parent' => $forum_id, |
| 20 | 32 | 'post_type' => 'topic', |
| @@ -24,10 +36,10 @@ | ||
| 24 | 36 | |
| 25 | 37 | $topics = ''; |
| 26 | 38 | $topic_replies = ''; |
| 27 | 39 | |
| 28 | - if ( is_array( $children ) ) : | |
| 29 | - foreach ( $children as $child ) : | |
| 40 | + if ( is_array( $children ) ) { | |
| 41 | + foreach ( $children as $child ) { | |
| 30 | 42 | $replies = get_children( [ |
| 31 | 43 | 'post_parent' => $child->ID, |
| 32 | 44 | 'post_type' => 'reply', |
| 33 | 45 | 'post_status' => [ 'publish', 'draft' ], |
| @@ -33,21 +45,25 @@ | ||
| 33 | 45 | 'post_status' => [ 'publish', 'draft' ], |
| 34 | 46 | ] ); |
| 35 | 47 | |
| 36 | 48 | $topics .= $child->ID . ','; |
| 37 | - if ( is_array( $replies ) ) : | |
| 38 | - foreach ( $replies as $reply ) : | |
| 49 | + if ( is_array( $replies ) ) { | |
| 50 | + foreach ( $replies as $reply ) { | |
| 39 | 51 | $topic_replies .= $reply->ID . ','; |
| 40 | - endforeach; | |
| 41 | - endif; | |
| 42 | - endforeach; | |
| 43 | - endif; | |
| 52 | + } | |
| 53 | + } | |
| 54 | + } | |
| 55 | + } | |
| 44 | 56 | |
| 45 | - $forum_posts = $forum_id . ',' . $topic_replies . $topics; | |
| 46 | - $forum_posts = explode( ',', $forum_posts ); | |
| 47 | - $forum_posts_int = array_map( 'intval', $forum_posts ); | |
| 57 | + $forum_posts = $forum_id . ',' . $topic_replies . $topics; | |
| 58 | + $forum_posts = explode( ',', $forum_posts ); | |
| 59 | + $forum_posts_int = array_map( 'intval', $forum_posts ); | |
| 60 | + | |
| 48 | 61 | foreach ( $forum_posts_int as $deletes ) { |
| 49 | - wp_trash_post( $deletes, true ); | |
| 62 | + if ( ! empty( $deletes ) ) { | |
| 63 | + wp_trash_post( $deletes ); | |
| 64 | + } | |
| 50 | 65 | } |
| 66 | + | |
| 51 | 67 | wp_send_json_success( 'Forum Deleted successfully.' ); |
| 52 | 68 | wp_die(); |
| 53 | -} | |
| 69 | +} | |