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
bbp-core / includes / admin / menu / Create_Forum.php

Create_Forum.php in Forumax – AI Powered Advanced Community Forum Plugin 2.5.1, at includes/admin/menu/Create_Forum.php

133 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'posts_per_page' => 1,
34 'fields' => 'ids',
35 'update_post_meta_cache' => false,
36 'update_post_term_cache' => false,
37 ];
38
39 // Only the count is needed.
40 $query = new \WP_Query( $args );
41 $total = $query->found_posts;
42 $add = 2;
43 $order = $total + $add;
44
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 }
133