PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.5.1
Forumax – AI Powered Advanced Community Forum Plugin v2.5.1
2.5.1 2.5.0 2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 All 31 releases
← All changes | includes/admin/menu/Create_Forum.php +123 -36 1.4.1 → 2.5.1 View file →
@@ -1,45 +1,132 @@
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 + 'posts_per_page' => 1,
34 + 'fields' => 'ids',
35 + 'update_post_meta_cache' => false,
36 + 'update_post_term_cache' => false,
37 + ];
38 38
39 - if ( is_wp_error( $post_id ) ) {
40 - wp_send_json_error( 'Failed to create the forum.' );
41 - }
39 + // Only the count is needed.
40 + $query = new \WP_Query( $args );
41 + $total = $query->found_posts;
42 + $add = 2;
43 + $order = $total + $add;
42 44
43 - // Return success
44 - wp_send_json_success( 'Forum created successfully.' );
45 -}
45 + // Insert the post
46 + $post_id = wp_insert_post( [
47 + 'post_title' => $bbp_forum_title,
48 + 'post_parent' => 0,
49 + 'post_content' => '',
50 + 'post_type' => 'forum',
51 + 'post_status' => 'publish',
52 + 'post_author' => get_current_user_id(),
53 + 'menu_order' => $order,
54 + ] );
55 +
56 + if ( is_wp_error( $post_id ) ) {
57 + wp_send_json_error( 'Failed to create the forum.' );
58 + }
59 +
60 + // Return success
61 + wp_send_json_success( 'Forum created successfully.' );
62 +}
63 +
64 +// AJAX handler to create a sub-forum
65 +add_action( 'wp_ajax_forumax_create_subforum', 'forumax_create_subforum' );
66 +
67 +/**
68 + * Create a sub-forum under a parent forum.
69 + *
70 + * @return void
71 + */
72 +function forumax_create_subforum() {
73 +
74 + // Check the nonce.
75 + check_ajax_referer( 'bbpc-admin-nonce', 'bbpc_nonce' );
76 +
77 + if ( ! current_user_can( 'manage_options' ) ) {
78 + wp_send_json_error( 'Unauthorized request.' );
79 + }
80 +
81 + $bbp_subforum_title = isset( $_POST['bbp_subforum_title'] ) ? sanitize_text_field( wp_unslash( $_POST['bbp_subforum_title'] ) ) : '';
82 + $parent_forum_id = isset( $_POST['parent_forum_id'] ) ? absint( $_POST['parent_forum_id'] ) : 0;
83 +
84 + if ( empty( $bbp_subforum_title ) ) {
85 + wp_send_json_error( 'Sub-forum title cannot be empty.' );
86 + }
87 +
88 + if ( empty( $parent_forum_id ) ) {
89 + wp_send_json_error( 'Parent forum ID is required.' );
90 + }
91 +
92 + // Verify parent forum exists.
93 + $parent_forum = get_post( $parent_forum_id );
94 + if ( ! $parent_forum || 'forum' !== $parent_forum->post_type ) {
95 + wp_send_json_error( 'Invalid parent forum.' );
96 + }
97 +
98 + // Get order for the new sub-forum.
99 + $args = [
100 + 'post_type' => 'forum',
101 + 'post_parent' => $parent_forum_id,
102 + 'posts_per_page' => 1,
103 + 'fields' => 'ids',
104 + 'update_post_meta_cache' => false,
105 + 'update_post_term_cache' => false,
106 + ];
107 +
108 + // Only the count is needed.
109 + $query = new \WP_Query( $args );
110 + $total = $query->found_posts;
111 + $order = $total + 1;
112 +
113 + // Insert the sub-forum.
114 + $post_id = wp_insert_post(
115 + [
116 + 'post_title' => $bbp_subforum_title,
117 + 'post_parent' => $parent_forum_id,
118 + 'post_content' => '',
119 + 'post_type' => 'forum',
120 + 'post_status' => 'publish',
121 + 'post_author' => get_current_user_id(),
122 + 'menu_order' => $order,
123 + ]
124 + );
125 +
126 + if ( is_wp_error( $post_id ) ) {
127 + wp_send_json_error( 'Failed to create the sub-forum.' );
128 + }
129 +
130 + // Return success.
131 + wp_send_json_success( 'Sub-forum created successfully.' );
132 +}