| @@ -26,30 +26,15 @@ | ||
| 26 | 26 | if ( empty( $topic_id ) ) { |
| 27 | 27 | wp_send_json_error( 'Topic ID cannot be empty.' ); |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - $replies = get_children( [ | |
| 31 | - 'post_parent' => $topic_id, | |
| 32 | - 'post_type' => 'reply', | |
| 33 | - 'orderby' => 'menu_order', | |
| 34 | - 'order' => 'asc', | |
| 35 | - ] ); | |
| 36 | - | |
| 37 | - $reply_ids = ''; | |
| 38 | - if ( is_array( $replies ) ) { | |
| 39 | - foreach ( $replies as $reply ) { | |
| 40 | - $reply_ids .= $reply->ID . ','; | |
| 41 | - } | |
| 30 | + if ( ! bbp_is_topic( $topic_id ) ) { | |
| 31 | + wp_send_json_error( 'Invalid topic.' ); | |
| 42 | 32 | } |
| 43 | 33 | |
| 44 | - $topic_posts = $topic_id . ',' . $reply_ids; | |
| 45 | - $topic_posts = explode( ',', $topic_posts ); | |
| 46 | - $topic_posts_int = array_map( 'intval', $topic_posts ); | |
| 47 | - | |
| 48 | - foreach ( $topic_posts_int as $delete_topic ) { | |
| 49 | - if ( ! empty( $delete_topic ) ) { | |
| 50 | - wp_trash_post( $delete_topic ); | |
| 51 | - } | |
| 34 | + // bbPress trashes the topic's replies on wp_trash_post and restores them on untrash. | |
| 35 | + if ( ! wp_trash_post( $topic_id ) ) { | |
| 36 | + wp_send_json_error( 'Failed to delete the topic.' ); | |
| 52 | 37 | } |
| 53 | 38 | |
| 54 | 39 | wp_send_json_success( 'Topic Deleted successfully.' ); |
| 55 | 40 | wp_die(); |