| @@ -1,41 +1,56 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Delete Topic functionality | |
| 4 | + * | |
| 5 | + * @package Forumax | |
| 6 | + */ | |
| 7 | + | |
| 8 | +defined( 'ABSPATH' ) || exit; | |
| 9 | + | |
| 2 | 10 | // AJAX handler to delete a topic |
| 3 | -add_action( 'wp_ajax_bbp_delete_topic', 'delete_topic' ); | |
| 4 | -function delete_topic() { | |
| 11 | +add_action( 'wp_ajax_forumax_delete_topic', 'forumax_delete_topic' ); | |
| 5 | 12 | |
| 6 | - // Check the nonce | |
| 13 | +/** | |
| 14 | + * Delete a topic and its replies. | |
| 15 | + */ | |
| 16 | +function forumax_delete_topic() { | |
| 17 | + | |
| 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 | - } | |
| 12 | - | |
| 13 | - $topic_id = ! empty ( $_POST['topic_id'] ) ? absint( $_POST['topic_id'] ) : 0; | |
| 14 | - if ( empty( $topic_id ) ) { | |
| 15 | - wp_send_json_error( 'Topic ID cannot be empty.' ); | |
| 16 | - } | |
| 17 | - | |
| 21 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 22 | + wp_send_json_error( 'Unauthorized request.' ); | |
| 23 | + } | |
| 24 | + | |
| 25 | + $topic_id = ! empty( $_POST['topic_id'] ) ? absint( $_POST['topic_id'] ) : 0; | |
| 26 | + if ( empty( $topic_id ) ) { | |
| 27 | + wp_send_json_error( 'Topic ID cannot be empty.' ); | |
| 28 | + } | |
| 29 | + | |
| 18 | 30 | $replies = get_children( [ |
| 19 | - 'post_parent' => $topic_id, | |
| 20 | - 'post_type' => 'reply', | |
| 21 | - 'orderby' => 'menu_order', | |
| 22 | - 'order' => 'asc', | |
| 31 | + 'post_parent' => $topic_id, | |
| 32 | + 'post_type' => 'reply', | |
| 33 | + 'orderby' => 'menu_order', | |
| 34 | + 'order' => 'asc', | |
| 23 | 35 | ] ); |
| 24 | 36 | |
| 25 | 37 | $reply_ids = ''; |
| 26 | - if ( is_array( $replies ) ) : | |
| 27 | - foreach ( $replies as $reply ) : | |
| 38 | + if ( is_array( $replies ) ) { | |
| 39 | + foreach ( $replies as $reply ) { | |
| 28 | 40 | $reply_ids .= $reply->ID . ','; |
| 29 | - endforeach; | |
| 30 | - endif; | |
| 41 | + } | |
| 42 | + } | |
| 31 | 43 | |
| 32 | - $topic_posts = $topic_id . ',' . $reply_ids; | |
| 33 | - $topic_posts = explode( ',', $topic_posts ); | |
| 34 | - $topic_posts_int = array_map( 'intval', $topic_posts ); | |
| 44 | + $topic_posts = $topic_id . ',' . $reply_ids; | |
| 45 | + $topic_posts = explode( ',', $topic_posts ); | |
| 46 | + $topic_posts_int = array_map( 'intval', $topic_posts ); | |
| 47 | + | |
| 35 | 48 | foreach ( $topic_posts_int as $delete_topic ) { |
| 36 | - wp_trash_post( $delete_topic, true ); | |
| 49 | + if ( ! empty( $delete_topic ) ) { | |
| 50 | + wp_trash_post( $delete_topic ); | |
| 51 | + } | |
| 37 | 52 | } |
| 38 | - | |
| 53 | + | |
| 39 | 54 | wp_send_json_success( 'Topic Deleted successfully.' ); |
| 40 | 55 | wp_die(); |
| 41 | -} | |
| 56 | +} | |