| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create Forum/Subforum functionality |
| 4 |
* |
| 5 |
* @package Forumax |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
// AJAX handler to create a forum |
| 11 |
add_action( 'wp_ajax_forumax_create_forum', 'forumax_create_forum' ); |
| 12 |
|
| 13 |
/** |
| 14 |
* Create a new forum. |
| 15 |
*/ |
| 16 |
function forumax_create_forum() { |
| 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 |
$bbp_forum_title = isset( $_POST['bbp_forum_title'] ) ? sanitize_text_field( wp_unslash( $_POST['bbp_forum_title'] ) ) : ''; |
| 26 |
if ( empty( $bbp_forum_title ) ) { |
| 27 |
wp_send_json_error( 'Forum title cannot be empty.' ); |
| 28 |
} |
| 29 |
|
| 30 |
$args = [ |
| 31 |
'post_type' => 'forum', |
| 32 |
'post_parent' => 0, |
| 33 |
]; |
| 34 |
|
| 35 |
$query = new \WP_Query( $args ); |
| 36 |
$total = $query->found_posts; |
| 37 |
$add = 2; |
| 38 |
$order = $total + $add; |
| 39 |
|
| 40 |
// Insert the post |
| 41 |
$post_id = wp_insert_post( [ |
| 42 |
'post_title' => $bbp_forum_title, |
| 43 |
'post_parent' => 0, |
| 44 |
'post_content' => '', |
| 45 |
'post_type' => 'forum', |
| 46 |
'post_status' => 'publish', |
| 47 |
'post_author' => get_current_user_id(), |
| 48 |
'menu_order' => $order, |
| 49 |
] ); |
| 50 |
|
| 51 |
if ( is_wp_error( $post_id ) ) { |
| 52 |
wp_send_json_error( 'Failed to create the forum.' ); |
| 53 |
} |
| 54 |
|
| 55 |
// Return success |
| 56 |
wp_send_json_success( 'Forum created successfully.' ); |
| 57 |
} |
| 58 |
|
| 59 |
// AJAX handler to create a sub-forum |
| 60 |
add_action( 'wp_ajax_forumax_create_subforum', 'forumax_create_subforum' ); |
| 61 |
|
| 62 |
/** |
| 63 |
* Create a sub-forum under a parent forum. |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
function forumax_create_subforum() { |
| 68 |
|
| 69 |
// Check the nonce. |
| 70 |
check_ajax_referer( 'bbpc-admin-nonce', 'bbpc_nonce' ); |
| 71 |
|
| 72 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 73 |
wp_send_json_error( 'Unauthorized request.' ); |
| 74 |
} |
| 75 |
|
| 76 |
$bbp_subforum_title = isset( $_POST['bbp_subforum_title'] ) ? sanitize_text_field( wp_unslash( $_POST['bbp_subforum_title'] ) ) : ''; |
| 77 |
$parent_forum_id = isset( $_POST['parent_forum_id'] ) ? absint( $_POST['parent_forum_id'] ) : 0; |
| 78 |
|
| 79 |
if ( empty( $bbp_subforum_title ) ) { |
| 80 |
wp_send_json_error( 'Sub-forum title cannot be empty.' ); |
| 81 |
} |
| 82 |
|
| 83 |
if ( empty( $parent_forum_id ) ) { |
| 84 |
wp_send_json_error( 'Parent forum ID is required.' ); |
| 85 |
} |
| 86 |
|
| 87 |
// Verify parent forum exists. |
| 88 |
$parent_forum = get_post( $parent_forum_id ); |
| 89 |
if ( ! $parent_forum || 'forum' !== $parent_forum->post_type ) { |
| 90 |
wp_send_json_error( 'Invalid parent forum.' ); |
| 91 |
} |
| 92 |
|
| 93 |
// Get order for the new sub-forum. |
| 94 |
$args = [ |
| 95 |
'post_type' => 'forum', |
| 96 |
'post_parent' => $parent_forum_id, |
| 97 |
]; |
| 98 |
|
| 99 |
$query = new \WP_Query( $args ); |
| 100 |
$total = $query->found_posts; |
| 101 |
$order = $total + 1; |
| 102 |
|
| 103 |
// Insert the sub-forum. |
| 104 |
$post_id = wp_insert_post( |
| 105 |
[ |
| 106 |
'post_title' => $bbp_subforum_title, |
| 107 |
'post_parent' => $parent_forum_id, |
| 108 |
'post_content' => '', |
| 109 |
'post_type' => 'forum', |
| 110 |
'post_status' => 'publish', |
| 111 |
'post_author' => get_current_user_id(), |
| 112 |
'menu_order' => $order, |
| 113 |
] |
| 114 |
); |
| 115 |
|
| 116 |
if ( is_wp_error( $post_id ) ) { |
| 117 |
wp_send_json_error( 'Failed to create the sub-forum.' ); |
| 118 |
} |
| 119 |
|
| 120 |
// Return success. |
| 121 |
wp_send_json_success( 'Sub-forum created successfully.' ); |
| 122 |
} |
| 123 |
|