| @@ -1,45 +1,122 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Create Forum/Subforum functionality | |
| 4 | + * | |
| 5 | + * @package Forumax | |
| 6 | + */ | |
| 7 | + | |
| 8 | +defined( 'ABSPATH' ) || exit; | |
| 9 | + | |
| 2 | 10 | // AJAX handler to create a forum |
| 3 | -add_action( 'wp_ajax_bbp_create_forum', 'bbp_create_forum' ); | |
| 4 | -function bbp_create_forum() { | |
| 5 | - | |
| 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 | + | |
| 6 | 18 | // Check the nonce |
| 7 | 19 | 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 | 20 | |
| 18 | - $args = [ | |
| 19 | - 'post_type' => 'forum', | |
| 20 | - 'post_parent' => 0 | |
| 21 | - ]; | |
| 21 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 22 | + wp_send_json_error( 'Unauthorized request.' ); | |
| 23 | + } | |
| 22 | 24 | |
| 23 | - $query = new \WP_Query( $args ); | |
| 24 | - $total = $query->found_posts; | |
| 25 | - $add = 2; | |
| 26 | - $order = $total + $add; | |
| 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 | + } | |
| 27 | 29 | |
| 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 | - ) ); | |
| 30 | + $args = [ | |
| 31 | + 'post_type' => 'forum', | |
| 32 | + 'post_parent' => 0, | |
| 33 | + ]; | |
| 38 | 34 | |
| 39 | - if ( is_wp_error( $post_id ) ) { | |
| 40 | - wp_send_json_error( 'Failed to create the forum.' ); | |
| 41 | - } | |
| 35 | + $query = new \WP_Query( $args ); | |
| 36 | + $total = $query->found_posts; | |
| 37 | + $add = 2; | |
| 38 | + $order = $total + $add; | |
| 42 | 39 | |
| 43 | - // Return success | |
| 44 | - wp_send_json_success( 'Forum created successfully.' ); | |
| 45 | -} | |
| 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 | +} | |