| 1 |
<?php |
| 2 |
// AJAX handler to create a forum |
| 3 |
add_action( 'wp_ajax_forumax_create_forum', 'forumax_create_forum' ); |
| 4 |
function forumax_create_forum() { |
| 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 |
$bbp_forum_title = isset( $_POST['bbp_forum_title'] ) ? sanitize_text_field( $_POST['bbp_forum_title'] ) : ''; |
| 14 |
if ( empty( $bbp_forum_title ) ) { |
| 15 |
wp_send_json_error( 'Forum title cannot be empty.' ); |
| 16 |
} |
| 17 |
|
| 18 |
$args = [ |
| 19 |
'post_type' => 'forum', |
| 20 |
'post_parent' => 0 |
| 21 |
]; |
| 22 |
|
| 23 |
$query = new \WP_Query( $args ); |
| 24 |
$total = $query->found_posts; |
| 25 |
$add = 2; |
| 26 |
$order = $total + $add; |
| 27 |
|
| 28 |
// Insert the post |
| 29 |
$post_id = wp_insert_post( array( |
| 30 |
'post_title' => $bbp_forum_title, |
| 31 |
'post_parent' => 0, |
| 32 |
'post_content' => '', |
| 33 |
'post_type' => 'forum', |
| 34 |
'post_status' => 'publish', |
| 35 |
'post_author' => get_current_user_id(), |
| 36 |
'menu_order' => $order, |
| 37 |
) ); |
| 38 |
|
| 39 |
if ( is_wp_error( $post_id ) ) { |
| 40 |
wp_send_json_error( 'Failed to create the forum.' ); |
| 41 |
} |
| 42 |
|
| 43 |
// Return success |
| 44 |
wp_send_json_success( 'Forum created successfully.' ); |
| 45 |
} |