| 1 |
<?php |
| 2 |
/** |
| 3 |
* Delete Topic functionality |
| 4 |
* |
| 5 |
* @package Forumax |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
// AJAX handler to delete a topic |
| 11 |
add_action( 'wp_ajax_forumax_delete_topic', 'forumax_delete_topic' ); |
| 12 |
|
| 13 |
/** |
| 14 |
* Delete a topic and its replies. |
| 15 |
*/ |
| 16 |
function forumax_delete_topic() { |
| 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 |
$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 |
|
| 30 |
if ( ! bbp_is_topic( $topic_id ) ) { |
| 31 |
wp_send_json_error( 'Invalid topic.' ); |
| 32 |
} |
| 33 |
|
| 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.' ); |
| 37 |
} |
| 38 |
|
| 39 |
wp_send_json_success( 'Topic Deleted successfully.' ); |
| 40 |
wp_die(); |
| 41 |
} |
| 42 |
|