| @@ -1,64 +1,69 @@ | ||
| 1 | -<?php | |
| 2 | -namespace admin\menu; | |
| 3 | - | |
| 4 | -/** | |
| 5 | - * Class Delete_Post | |
| 6 | - * @package BBP-core\Admin\Menu | |
| 7 | - */ | |
| 8 | -class Delete_Forum { | |
| 9 | - | |
| 10 | - /** | |
| 11 | - * Create_Post constructor. | |
| 12 | - */ | |
| 13 | - public function __construct() { | |
| 14 | - add_action( 'admin_init', [ $this, 'delete_forum' ] ); | |
| 15 | - } | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * Delete Parent Doc | |
| 19 | - */ | |
| 20 | - public function delete_forum() { | |
| 21 | - if ( ! empty( $_GET['forum_ID'] ) ) { | |
| 22 | - $parent_forum_id = $_GET['forum_ID'] ?? ''; | |
| 23 | - $children = get_children( | |
| 24 | - [ | |
| 25 | - 'post_parent' => $_GET['forum_ID'], | |
| 26 | - 'post_type' => 'topic', | |
| 27 | - 'orderby' => 'menu_order', | |
| 28 | - 'order' => 'asc', | |
| 29 | - ] | |
| 30 | - ); | |
| 31 | - | |
| 32 | - $topics = ''; | |
| 33 | - $topic_replies = ''; | |
| 34 | - if ( is_array( $children ) ) : | |
| 35 | - foreach ( $children as $child ) : | |
| 36 | - $replies = get_children( | |
| 37 | - [ | |
| 38 | - 'post_parent' => $child->ID, | |
| 39 | - 'post_type' => 'reply', | |
| 40 | - 'post_status' => [ 'publish', 'draft' ], | |
| 41 | - ] | |
| 42 | - ); | |
| 43 | - | |
| 44 | - $topics .= $child->ID . ','; | |
| 45 | - if ( is_array( $replies ) ) : | |
| 46 | - foreach ( $replies as $reply ) : | |
| 47 | - $topic_replies .= $reply->ID . ','; | |
| 48 | - endforeach; | |
| 49 | - endif; | |
| 50 | - | |
| 51 | - endforeach; | |
| 52 | - endif; | |
| 53 | - | |
| 54 | - $forum_ids = $parent_forum_id . ',' . $topic_replies . $topics; | |
| 55 | - $forum_id = explode( ',', $forum_ids ); | |
| 56 | - $forum_id_int = array_map( 'intval', $forum_id ); | |
| 57 | - foreach ( $forum_id_int as $deletes ) { | |
| 58 | - wp_trash_post( $deletes, true ); | |
| 59 | - } | |
| 60 | - wp_safe_redirect( admin_url( 'admin.php?page=bbp-core' ) ); | |
| 61 | - } | |
| 62 | - } | |
| 63 | -} | |
| 64 | -new Delete_Forum(); | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Delete Forum functionality | |
| 4 | + * | |
| 5 | + * @package Forumax | |
| 6 | + */ | |
| 7 | + | |
| 8 | +defined( 'ABSPATH' ) || exit; | |
| 9 | + | |
| 10 | +// AJAX handler to delete a forum | |
| 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 | + | |
| 18 | + // Check the nonce | |
| 19 | + check_ajax_referer( 'bbpc-admin-nonce', 'bbpc_nonce' ); | |
| 20 | + | |
| 21 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 22 | + wp_send_json_error( 'Unauthorized request.' ); | |
| 23 | + } | |
| 24 | + | |
| 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 | + } | |
| 29 | + | |
| 30 | + $children = get_children( [ | |
| 31 | + 'post_parent' => $forum_id, | |
| 32 | + 'post_type' => 'topic', | |
| 33 | + 'orderby' => 'menu_order', | |
| 34 | + 'order' => 'asc', | |
| 35 | + ] ); | |
| 36 | + | |
| 37 | + $topics = ''; | |
| 38 | + $topic_replies = ''; | |
| 39 | + | |
| 40 | + if ( is_array( $children ) ) { | |
| 41 | + foreach ( $children as $child ) { | |
| 42 | + $replies = get_children( [ | |
| 43 | + 'post_parent' => $child->ID, | |
| 44 | + 'post_type' => 'reply', | |
| 45 | + 'post_status' => [ 'publish', 'draft' ], | |
| 46 | + ] ); | |
| 47 | + | |
| 48 | + $topics .= $child->ID . ','; | |
| 49 | + if ( is_array( $replies ) ) { | |
| 50 | + foreach ( $replies as $reply ) { | |
| 51 | + $topic_replies .= $reply->ID . ','; | |
| 52 | + } | |
| 53 | + } | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + $forum_posts = $forum_id . ',' . $topic_replies . $topics; | |
| 58 | + $forum_posts = explode( ',', $forum_posts ); | |
| 59 | + $forum_posts_int = array_map( 'intval', $forum_posts ); | |
| 60 | + | |
| 61 | + foreach ( $forum_posts_int as $deletes ) { | |
| 62 | + if ( ! empty( $deletes ) ) { | |
| 63 | + wp_trash_post( $deletes ); | |
| 64 | + } | |
| 65 | + } | |
| 66 | + | |
| 67 | + wp_send_json_success( 'Forum Deleted successfully.' ); | |
| 68 | + wp_die(); | |
| 69 | +} | |