| @@ -1,45 +1,61 @@ | ||
| 1 | -<?php | |
| 2 | -namespace Admin; | |
| 3 | - | |
| 4 | -class Create_Topic { | |
| 5 | - /** | |
| 6 | - * Create_Forum constructor. | |
| 7 | - */ | |
| 8 | - public function __construct() { | |
| 9 | - add_action( 'admin_init', [ $this, 'bbp_create_topic' ] ); | |
| 10 | - } | |
| 11 | - | |
| 12 | - /** | |
| 13 | - * Create parent Doc post | |
| 14 | - */ | |
| 15 | - public function bbp_create_topic() { | |
| 16 | - | |
| 17 | - if ( isset ( $_GET['is_bbp_section'] ) && ! empty ( $_GET['is_bbp_section'] ) ) { | |
| 18 | - | |
| 19 | - $parentID = ! empty ( $_GET['bbp_parentID'] ) ? absint( $_GET['bbp_parentID'] ) : 0; | |
| 20 | - $section_title = ! empty ( $_GET['is_bbp_section'] ) ? sanitize_text_field( $_GET['is_bbp_section'] ) : ''; | |
| 21 | - $parent_item = get_children( array( | |
| 22 | - 'post_parent' => $parentID, | |
| 23 | - 'post_type' => 'topic' | |
| 24 | - ) ); | |
| 25 | - | |
| 26 | - $add = 2; | |
| 27 | - $order = count( $parent_item ); | |
| 28 | - $order = $order + $add; | |
| 29 | - | |
| 30 | - // Create post object | |
| 31 | - $post = array( | |
| 32 | - 'post_title' => $section_title, | |
| 33 | - 'post_parent' => $parentID, | |
| 34 | - 'post_content' => '', | |
| 35 | - 'post_type' => 'topic', | |
| 36 | - 'post_status' => 'publish', | |
| 37 | - 'menu_order' => $order | |
| 38 | - ); | |
| 39 | - wp_insert_post( $post, $wp_error = '' ); | |
| 40 | - wp_safe_redirect( admin_url('admin.php?page=bbp-core') ); | |
| 41 | - } | |
| 42 | - | |
| 43 | - } | |
| 44 | -} | |
| 45 | -new Create_Topic(); | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Create Topic functionality | |
| 4 | + * | |
| 5 | + * @package Forumax | |
| 6 | + */ | |
| 7 | + | |
| 8 | +defined( 'ABSPATH' ) || exit; | |
| 9 | + | |
| 10 | +// AJAX handler to create a topic | |
| 11 | +add_action( 'wp_ajax_forumax_create_topic', 'forumax_create_topic' ); | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Create a new topic. | |
| 15 | + */ | |
| 16 | +function forumax_create_topic() { | |
| 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_topic_title = isset( $_POST['bbp_topic_title'] ) ? sanitize_text_field( wp_unslash( $_POST['bbp_topic_title'] ) ) : ''; | |
| 26 | + if ( empty( $bbp_topic_title ) ) { | |
| 27 | + wp_send_json_error( 'Topic title cannot be empty.' ); | |
| 28 | + } | |
| 29 | + | |
| 30 | + $forum_id = ! empty( $_POST['forum_id'] ) ? absint( $_POST['forum_id'] ) : 0; | |
| 31 | + $topic_author = bbp_get_current_user_id(); | |
| 32 | + | |
| 33 | + $parent_item = get_children( [ | |
| 34 | + 'post_parent' => $forum_id, | |
| 35 | + 'post_type' => 'topic', | |
| 36 | + ] ); | |
| 37 | + | |
| 38 | + $add = 2; | |
| 39 | + $order = count( $parent_item ); | |
| 40 | + $order = $order + $add; | |
| 41 | + | |
| 42 | + // Create topic object | |
| 43 | + $topic_data = apply_filters( 'bbp_new_topic_pre_insert', [ | |
| 44 | + 'post_author' => $topic_author, | |
| 45 | + 'post_title' => $bbp_topic_title, | |
| 46 | + 'post_parent' => $forum_id, | |
| 47 | + 'post_content' => '', | |
| 48 | + 'post_type' => bbp_get_topic_post_type(), | |
| 49 | + 'post_status' => 'publish', | |
| 50 | + 'menu_order' => $order, | |
| 51 | + ] ); | |
| 52 | + | |
| 53 | + $topic_id = wp_insert_post( $topic_data, true ); | |
| 54 | + | |
| 55 | + if ( is_wp_error( $topic_id ) ) { | |
| 56 | + wp_send_json_error( 'Failed to create the topic.' ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + do_action( 'bbp_new_topic', $topic_id, $forum_id, '', $topic_author ); | |
| 60 | + wp_send_json_success( 'Topic created successfully.' ); | |
| 61 | +} | |