Skip to:
Content

bbPress.org

Opened 9 months ago

Closed 8 months ago

#3420 closed defect (fixed)

PHP Notice when editing non-published topic

Reported by: dd32 Owned by: johnjamesjacoby
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords:
Cc:

Description

It looks like the $topic_status variable ends up undefined when bbp_check_for_moderation() and bbp_is_topic_public() both return false.

It appears to be when editing a topic that is still pending review I guess.

The below patch is what was most obvious to me at first, but I guess the first if branch could also just get an additional else branch.

E_NOTICE: Undefined variable: topic_status in wp-content/plugins/bbpress/includes/topics/functions.php:622

Source: POST https://de.wordpress.org/support/?post_type=topic&p=101119
  • includes/topics/functions.php

    function bbp_edit_topic_handler( $action 
    547547        // No topic content
    548548        if ( empty( $topic_content ) ) {
    549549                bbp_add_error( 'bbp_edit_topic_content', __( '<strong>Error</strong>: Your topic cannot be empty.', 'bbpress' ) );
    550550        }
    551551
    552552        /** Topic Bad Words *******************************************************/
    553553
    554554        if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content, true ) ) {
    555555                bbp_add_error( 'bbp_topic_moderation', __( '<strong>Error</strong>: Your topic cannot be edited at this time.', 'bbpress' ) );
    556556        }
    557557
    558558        /** Topic Status **********************************************************/
    559559
    560560        // Get available topic statuses
    561561        $topic_statuses = bbp_get_topic_statuses( $topic_id );
     562        $topic_status   = $topic->post_status;
    562563
    563564        // Maybe put into moderation
    564565        if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
    565566
    566567                // Set post status to pending if public or closed
    567568                if ( bbp_is_topic_public( $topic->ID ) ) {
    568569                        $topic_status = bbp_get_pending_status_id();
    569570                }
    570571
    571572        // Check possible topic status ID's
    572573        } elseif ( ! empty( $_POST['bbp_topic_status'] ) && in_array( $_POST['bbp_topic_status'], array_keys( $topic_statuses ), true ) ) {
    573574                $topic_status = sanitize_key( $_POST['bbp_topic_status'] );
    574 
    575         // Use existing post_status
    576         } else {
    577                 $topic_status = $topic->post_status;
    578575        }
    579576
    580577        /** Topic Tags ************************************************************/
    581578
    582579        // Either replace terms
    583580        if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags', $topic_id ) && ! empty( $_POST['bbp_topic_tags'] ) ) {
    584581
    585582                // Escape tag input
    586583                $terms = sanitize_text_field( $_POST['bbp_topic_tags'] );
    587584
    588585                // Explode by comma
    589586                if ( strstr( $terms, ',' ) ) {
    590587                        $terms = explode( ',', $terms );
    591588                }

Attachments (1)

3420.patch (6.3 KB) - added by johnjamesjacoby 8 months ago.

Download all attachments as: .zip

Change History (4)

#1 @dd32
9 months ago

Or combining bbp_is_topic_public() into the first if ( bbp_check_for_moderation() ) conditional could also maybe be cleaner.

  • includes/topics/functions.php

     
    560560        // Get available topic statuses
    561561        $topic_statuses = bbp_get_topic_statuses( $topic_id );
    562562
    563         // Maybe put into moderation
    564         if ( ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) ) {
     563        // Maybe put into moderation, and set post status to pending if public or closed
     564        if (
     565                ! bbp_check_for_moderation( $anonymous_data, $topic_author, $topic_title, $topic_content ) &&
     566                bbp_is_topic_public( $topic->ID )
     567        ) {
     568                $topic_status = bbp_get_pending_status_id();
    565569
    566                 // Set post status to pending if public or closed
    567                 if ( bbp_is_topic_public( $topic->ID ) ) {
    568                         $topic_status = bbp_get_pending_status_id();
    569                 }
    570 
    571570        // Check possible topic status ID's
    572571        } elseif ( ! empty( $_POST['bbp_topic_status'] ) && in_array( $_POST['bbp_topic_status'], array_keys( $topic_statuses ), true ) ) {
    573572                $topic_status = sanitize_key( $_POST['bbp_topic_status'] );

#2 @johnjamesjacoby
8 months ago

In 7191:

Post Statuses: Audit forum/topic/reply, new/edit handlers.

This change normalizes the approaches between post types and actions, to ensure predictable behavior and avoid PHP notices from undefined status variables in certain situations.

It also adds a capability check to the bbp_topic_status condition, so that it is not engaged when the UI was not exposed in the posted form.

Props dd32.

In trunk for 2.7.0. See #3420.

#3 @johnjamesjacoby
8 months ago

  • Owner set to johnjamesjacoby
  • Resolution set to fixed
  • Status changed from new to closed

In 7192:

Post Statuses: Audit forum/topic/reply, new/edit handlers.

This change normalizes the approaches between post types and actions, to ensure predictable behavior and avoid PHP notices from undefined status variables in certain situations.

It also adds a capability check to the bbp_topic_status condition, so that it is not engaged when the UI was not exposed in the posted form.

Props dd32.

In branches/2.6 for 2.6.7. Fixes #3420.

Note: See TracTickets for help on using tickets.