PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.2.0
Forumax – AI Powered Advanced Community Forum Plugin v2.2.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 2.0.0 2.1.0 All 29 releases
← All changes | includes/admin/menu/Create_Forum.php +122 -43 1.2.52.2.0 View file →
@@ -1,43 +1,122 @@
1 -<?php
2 -namespace Admin;
3 -
4 -class Create_Forum {
5 - /**
6 - * Create_Forum constructor.
7 - */
8 - public function __construct() {
9 - add_action( 'admin_init', [ $this, 'bbp_create_forum' ] );
10 - }
11 -
12 - /**
13 - * Create parent Doc post
14 - */
15 - public function bbp_create_forum() {
16 - if ( ! empty ( $_GET['bbp_parent_title'] ) ) {
17 - $bbp_parent_title = ! empty ( $_GET['bbp_parent_title'] ) ? sanitize_text_field( $_GET['bbp_parent_title'] ) : '';
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 - // Create post object
29 - $post = wp_insert_post( array(
30 - 'post_title' => $bbp_parent_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 - wp_insert_post( $post, $wp_error = '' );
39 - wp_safe_redirect( admin_url('admin.php?page=bbp-core') );
40 - }
41 - }
42 -}
43 -new Create_Forum();
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 +}