bbp_new_topic_handler( string $action = '' )

Handles the front end topic submission

Description

Parameters

$action

(Optional) The requested action to compare this function to.

Default value: ''

Source

File: bp-forums/topics/functions.php

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
function bbp_new_topic_handler( $action = '' ) {
 
    // Bail if action is not bbp-new-topic
    if ( 'bbp-new-topic' !== $action )
        return;
 
    // Nonce check
    if ( ! bbp_verify_nonce_request( 'bbp-new-topic' ) ) {
        bbp_add_error( 'bbp_new_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'buddyboss' ) );
        return;
    }
 
    // Define local variable(s)
    $view_all = false;
    $forum_id = $topic_author = $anonymous_data = 0;
    $topic_title = $topic_content = '';
    $terms = array( bbp_get_topic_tag_tax_id() => array() );
 
    /** Topic Author **********************************************************/
 
    // User is anonymous
    if ( bbp_is_anonymous() ) {
 
        // Filter anonymous data
        $anonymous_data = bbp_filter_anonymous_post_data();
 
        // Anonymous data checks out, so set cookies, etc...
        if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
            bbp_set_current_anonymous_user_data( $anonymous_data );
        }
 
    // User is logged in
    } else {
 
        // User cannot create topics
        if ( !current_user_can( 'publish_topics' ) ) {
            bbp_add_error( 'bbp_topic_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new discussions.', 'buddyboss' ) );
            return;
        }
 
        // Topic author is current user
        $topic_author = bbp_get_current_user_id();
    }
 
    // Remove kses filters from title and content for capable users and if the nonce is verified
    if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_topic'] ) && wp_create_nonce( 'bbp-unfiltered-html-topic_new' ) === $_POST['_bbp_unfiltered_html_topic'] ) {
        remove_filter( 'bbp_new_topic_pre_title',   'wp_filter_kses'      );
        remove_filter( 'bbp_new_topic_pre_content', 'bbp_encode_bad',  10 );
        remove_filter( 'bbp_new_topic_pre_content', 'bbp_filter_kses', 30 );
    }
 
    /** Discussion Title ***********************************************************/
 
    if ( !empty( $_POST['bbp_topic_title'] ) )
        $topic_title = esc_attr( strip_tags( $_POST['bbp_topic_title'] ) );
 
    // Filter and sanitize
    $topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title );
 
    // No topic title
    if ( empty( $topic_title ) )
        bbp_add_error( 'bbp_topic_title', __( '<strong>ERROR</strong>: Your discussion needs a subject.', 'buddyboss' ) );
 
    /** Topic Content *********************************************************/
 
    if ( !empty( $_POST['bbp_topic_content'] ) )
        $topic_content = $_POST['bbp_topic_content'];
 
    // Filter and sanitize
    $topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content );
 
    // No topic content
    if ( empty( $topic_content ) )
        bbp_add_error( 'bbp_topic_content', __( '<strong>ERROR</strong>: Your discussion cannot be empty.', 'buddyboss' ) );
 
    /** Topic Forum ***********************************************************/
 
    // Error check the POST'ed topic id
    if ( isset( $_POST['bbp_forum_id'] ) ) {
 
        // Empty Forum id was passed
        if ( empty( $_POST['bbp_forum_id'] ) ) {
            bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'buddyboss' ) );
 
        // Forum id is not a number
        } elseif ( ! is_numeric( $_POST['bbp_forum_id'] ) ) {
            bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID must be a number.', 'buddyboss' ) );
 
        // Forum id might be valid
        } else {
 
            // Get the forum id
            $posted_forum_id = intval( $_POST['bbp_forum_id'] );
 
            // Forum id is empty
            if ( 0 === $posted_forum_id ) {
                bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'buddyboss' ) );
 
            // Forum id is a negative number
            } elseif ( 0 > $posted_forum_id ) {
                bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID cannot be a negative number.', 'buddyboss' ) );
 
            // Forum does not exist
            } elseif ( ! bbp_get_forum( $posted_forum_id ) ) {
                bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum does not exist.', 'buddyboss' ) );
 
            // Use the POST'ed forum id
            } else {
                $forum_id = $posted_forum_id;
            }
        }
    }
 
    // Forum exists
    if ( !empty( $forum_id ) ) {
 
        // Forum is a category
        if ( bbp_is_forum_category( $forum_id ) ) {
            bbp_add_error( 'bbp_new_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No discussions can be created in this forum.', 'buddyboss' ) );
 
        // Forum is not a category
        } else {
 
            // Forum is closed and user cannot access
            if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) ) {
                bbp_add_error( 'bbp_new_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new discussions.', 'buddyboss' ) );
            }
 
            // Forum is private and user cannot access
            if ( bbp_is_forum_private( $forum_id ) ) {
                if ( !current_user_can( 'read_private_forums' ) ) {
                    bbp_add_error( 'bbp_new_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new discussions in it.', 'buddyboss' ) );
                }
 
            // Forum is hidden and user cannot access
            } elseif ( bbp_is_forum_hidden( $forum_id ) ) {
                if ( !current_user_can( 'read_hidden_forums' ) ) {
                    bbp_add_error( 'bbp_new_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new discussions in it.', 'buddyboss' ) );
                }
            }
        }
    }
 
    /** Topic Flooding ********************************************************/
 
    if ( !bbp_check_for_flood( $anonymous_data, $topic_author ) )
        bbp_add_error( 'bbp_topic_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'buddyboss' ) );
 
    /** Topic Duplicate *******************************************************/
 
    if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_topic_post_type(), 'post_author' => $topic_author, 'post_content' => $topic_content, 'anonymous_data' => $anonymous_data ) ) )
        bbp_add_error( 'bbp_topic_duplicate', __( '<strong>ERROR</strong>: Duplicate discussion detected; it looks as though you\'ve already said that!', 'buddyboss' ) );
 
    /** Topic Blacklist *******************************************************/
 
    if ( !bbp_check_for_blacklist( $anonymous_data, $topic_author, $topic_title, $topic_content ) )
        bbp_add_error( 'bbp_topic_blacklist', __( '<strong>ERROR</strong>: Your discussion cannot be created at this time.', 'buddyboss' ) );
 
    /** Topic Status **********************************************************/
 
    // Maybe put into moderation
    if ( !bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
        $topic_status = bbp_get_pending_status_id();
 
    // Check a whitelist of possible topic status ID's
    } elseif ( !empty( $_POST['bbp_topic_status'] ) && in_array( $_POST['bbp_topic_status'], array_keys( bbp_get_topic_statuses() ) ) ) {
        $topic_status = $_POST['bbp_topic_status'];
 
    // Default to published if nothing else
    } else {
        $topic_status = bbp_get_public_status_id();
    }
 
    /** Topic Tags ************************************************************/
 
    if ( bbp_allow_topic_tags() && !empty( $_POST['bbp_topic_tags'] ) ) {
 
        // Escape tag input
        $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
 
        // Explode by comma
        if ( strstr( $terms, ',' ) ) {
            $terms = explode( ',', $terms );
        }
 
        // Add topic tag ID as main key
        $terms = array( bbp_get_topic_tag_tax_id() => $terms );
    }
 
    /** Additional Actions (Before Save) **************************************/
 
    do_action( 'bbp_new_topic_pre_extras', $forum_id );
 
    // Bail if errors
    if ( bbp_has_errors() )
        return;
 
    /** No Errors *************************************************************/
 
    // Add the content of the form to $topic_data as an array.
    // Just in time manipulation of topic data before being created
    $topic_data = apply_filters( 'bbp_new_topic_pre_insert', array(
        'post_author'    => $topic_author,
        'post_title'     => $topic_title,
        'post_content'   => $topic_content,
        'post_status'    => $topic_status,
        'post_parent'    => $forum_id,
        'post_type'      => bbp_get_topic_post_type(),
        'tax_input'      => $terms,
        'comment_status' => 'closed'
    ) );
 
    // Insert topic
    $topic_id = wp_insert_post( $topic_data );
 
    /** No Errors *************************************************************/
 
    if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) {
 
        /** Trash Check *******************************************************/
 
        // If the forum is trash, or the topic_status is switched to
        // trash, trash it properly
        if ( ( get_post_field( 'post_status', $forum_id ) === bbp_get_trash_status_id() ) || ( $topic_data['post_status'] === bbp_get_trash_status_id() ) ) {
 
            // Trash the reply
            wp_trash_post( $topic_id );
 
            // Force view=all
            $view_all = true;
        }
 
        /** Spam Check ********************************************************/
 
        // If reply or topic are spam, officially spam this reply
        if ( $topic_data['post_status'] === bbp_get_spam_status_id() ) {
            add_post_meta( $topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
 
            // Force view=all
            $view_all = true;
        }
 
        /** Update counts, etc... *********************************************/
 
        do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author );
 
        /** Stickies **********************************************************/
 
        // Sticky check after 'bbp_new_topic' action so forum ID meta is set
        if ( !empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array( 'stick', 'super', 'unstick' ) ) ) {
 
            // What's the caps?
            if ( current_user_can( 'moderate' ) ) {
 
                // What's the haps?
                switch ( $_POST['bbp_stick_topic'] ) {
 
                    // Sticky in this forum
                    case 'stick'   :
                        bbp_stick_topic( $topic_id );
                        break;
 
                    // Super sticky in all forums
                    case 'super'   :
                        bbp_stick_topic( $topic_id, true );
                        break;
 
                    // We can avoid this as it is a new topic
                    case 'unstick' :
                    default        :
                        break;
                }
            }
        }
 
        /** Additional Actions (After Save) ***********************************/
 
        do_action( 'bbp_new_topic_post_extras', $topic_id );
 
        /** Redirect **********************************************************/
 
        // Redirect to
        $redirect_to = bbp_get_redirect_to();
 
        // Get the topic URL
        $redirect_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
 
        // Add view all?
        if ( bbp_get_view_all() || !empty( $view_all ) ) {
 
            // User can moderate, so redirect to topic with view all set
            if ( current_user_can( 'moderate' ) ) {
                $redirect_url = bbp_add_view_all( $redirect_url );
 
            // User cannot moderate, so redirect to forum
            } else {
                $redirect_url = bbp_get_forum_permalink( $forum_id );
            }
        }
 
        // Allow to be filtered
        $redirect_url = apply_filters( 'bbp_new_topic_redirect_to', $redirect_url, $redirect_to, $topic_id );
 
        /** Successful Save ***************************************************/
 
        // Redirect back to new topic
        wp_safe_redirect( $redirect_url );
 
        // For good measure
        exit();
 
    // Errors
    } else {
        $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : '';
        bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error, 'buddyboss' ) );
    }
}

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.