| 1 |
<?php |
| 2 |
// AJAX handler to delete a forum |
| 3 |
add_action( 'wp_ajax_forumax_delete_forum', 'forumax_delete_forums' ); |
| 4 |
function forumax_delete_forums() { |
| 5 |
|
| 6 |
// Check the nonce |
| 7 |
check_ajax_referer( 'bbpc-admin-nonce', 'bbpc_nonce' ); |
| 8 |
|
| 9 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 10 |
wp_send_json_error( 'Unauthorized request.' ); |
| 11 |
} |
| 12 |
|
| 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 |
} |
| 17 |
|
| 18 |
$children = get_children( [ |
| 19 |
'post_parent' => $forum_id, |
| 20 |
'post_type' => 'topic', |
| 21 |
'orderby' => 'menu_order', |
| 22 |
'order' => 'asc', |
| 23 |
] ); |
| 24 |
|
| 25 |
$topics = ''; |
| 26 |
$topic_replies = ''; |
| 27 |
|
| 28 |
if ( is_array( $children ) ) : |
| 29 |
foreach ( $children as $child ) : |
| 30 |
$replies = get_children( [ |
| 31 |
'post_parent' => $child->ID, |
| 32 |
'post_type' => 'reply', |
| 33 |
'post_status' => [ 'publish', 'draft' ], |
| 34 |
] ); |
| 35 |
|
| 36 |
$topics .= $child->ID . ','; |
| 37 |
if ( is_array( $replies ) ) : |
| 38 |
foreach ( $replies as $reply ) : |
| 39 |
$topic_replies .= $reply->ID . ','; |
| 40 |
endforeach; |
| 41 |
endif; |
| 42 |
endforeach; |
| 43 |
endif; |
| 44 |
|
| 45 |
$forum_posts = $forum_id . ',' . $topic_replies . $topics; |
| 46 |
$forum_posts = explode( ',', $forum_posts ); |
| 47 |
$forum_posts_int = array_map( 'intval', $forum_posts ); |
| 48 |
foreach ( $forum_posts_int as $deletes ) { |
| 49 |
wp_trash_post( $deletes, true ); |
| 50 |
} |
| 51 |
wp_send_json_success( 'Forum Deleted successfully.' ); |
| 52 |
wp_die(); |
| 53 |
} |