Making WordPress.org

Changeset 3797


Ignore:
Timestamp:
08/10/2016 05:06:22 PM (8 years ago)
Author:
jmdodd
Message:

Support: Allow front-end updating of topic resolution.

Lets moderators and topic authors change the topic resolution status.

See #1544, #1873.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-topic-resolution
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-topic-resolution/inc/class-plugin.php

    r3783 r3797  
    44
    55class Plugin {
    6 
    7     /**
    8      * @todo Edit enabled forums - will need to be updateable from the admin interface for administrators during forum setup
    9      * Decisions: default topic resolution is 'no'
    10      */
    116
    127    /**
     
    4338        add_filter( 'bbp_get_topic_title', array( $this, 'get_topic_title' ), 10, 2 );
    4439
    45         // Display the form for new/edited topics.
     40        // Display the form for new/edit topics.
    4641        add_action( 'bbp_theme_before_topic_form_content', array( $this, 'form_topic_resolution_dropdown' ) );
    4742        add_action( 'bbp_theme_before_topic_form_subscriptions', array( $this, 'form_topic_resolution_checkbox' ) );
     
    5146        add_action( 'bbp_edit_topic_post_extras', array( $this, 'topic_post_extras' ) );
    5247
     48        // Add form for moderator/user topic resolution update.
     49        // @todo Add AJAX processing to front-end topic resolution updates.
     50        add_action( 'bbp_post_request', array( $this, 'topic_resolution_handler' ) );
     51
    5352        // Register views.
    5453        add_action( 'bbp_register_views', array( $this, 'register_views' ) );
     
    5857        add_action( 'manage_forum_posts_custom_column', array( $this, 'add_forum_topic_resolution_value' ), 10, 2 );
    5958
    60         // Process field submission
     59        // Process changes to a forums support status.
    6160        // @todo Bulk actions aren't filterable, so this might be hacky.
    6261    }
     
    8281        }
    8382
    84         // Only display on topic edits
     83        // Only display on topic edit.
    8584        if ( ! bbp_is_topic_edit() ) {
    8685            return;
     
    152151            'resolution' => $resolution,
    153152        ) );
     153    }
     154
     155    /**
     156     * Output topic resolution as a string or a selection dropdown.
     157     */
     158    public function get_topic_resolution_form() {
     159        // Only display on forums where this is enabled
     160        if ( ! $this->is_enabled_on_forum() ) {
     161            return;
     162        }
     163
     164        // Only display on single topic.
     165        if ( ! bbp_is_single_topic() && ! bbp_is_topic_edit() ) {
     166            return;
     167        }
     168
     169        // Get topic and user data.
     170        $topic_id = intval( bbp_get_topic_id() );
     171        $topic = bbp_get_topic( $topic_id );
     172        if ( ! $topic_id || ! $topic ) {
     173            return;
     174        }
     175
     176        // Get the current resolution of this topic.
     177        $resolution = $this->get_topic_resolution( array( 'id' => bbp_get_topic_id() ) );
     178        if ( empty( $resolution ) ) {
     179            $resolution = $this->get_default_topic_resolution();
     180        }
     181        $resolutions = $this->get_topic_resolutions();
     182
     183        // Display the current topic resolution if the user can't update it.
     184        $user_id = get_current_user_id();
     185        if ( bbp_is_topic_edit() || ! $this->user_can_resolve( $user_id, $topic_id ) ) {
     186            printf( esc_html__( 'Status: %s', 'wporg-forums' ), $resolutions[ $resolution ] );
     187
     188        // Display the form to update the topic resolution.
     189        } else {
     190            ?>
     191            <form method="POST">
     192            <input type="hidden" name="action" value="wporg_bbp_topic_resolution" />
     193            <input type="hidden" name="topic_id" value="<?php echo esc_attr( $topic->ID ); ?>" />
     194            <?php wp_nonce_field( 'toggle-topic-resolution_' . $topic->ID ); ?>
     195            <label for="topic-resolved"><?php esc_html_e( 'Status:', 'wporg-forums' ); ?></label>
     196
     197            <select name="<?php echo esc_attr( self::META_KEY ); ?>" id="topic-resolved">
     198
     199            <?php foreach ( $resolutions as $key => $label ) : ?>
     200
     201                <option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $resolution ); ?>><?php echo esc_html( $label ); ?></option>
     202
     203            <?php endforeach; ?>
     204
     205            </select>
     206            <input type="submit" name="submit" value="<?php esc_attr_e( 'Update', 'wporg-forums' ); ?>" />
     207            </form></span>
     208            <?php
     209        }
     210    }
     211
     212    /**
     213     * Handle a user setting the topic resolution on a given topic.
     214     *
     215     * @param string $action The requested action to compare this function to
     216     */
     217    public function topic_resolution_handler( $action = '' ) {
     218        if ( ! $this->is_enabled_on_forum() ) {
     219            return false;
     220        }
     221
     222        // Bail if the action isn't meant for this function.
     223        if ( $action != 'wporg_bbp_topic_resolution' ) {
     224            return;
     225        }
     226
     227        // Bail if no topic id or resolution is passed.
     228        if ( empty( $_POST['topic_id'] ) || empty( $_POST[ self::META_KEY ] ) ) {
     229            return;
     230        }
     231
     232        // Get required data.
     233        $topic_id   = intval( $_POST['topic_id'] );
     234        $topic      = bbp_get_topic( $topic_id );
     235        $user_id    = get_current_user_id();
     236        $resolution = $_POST[ self::META_KEY ];
     237
     238        // Check for empty topic id.
     239        if ( empty( $topic_id ) || ! $topic ) {
     240            bbp_add_error( 'wporg_bbp_topic_resolution_topic_id', __( '<strong>ERROR</strong>: No topic was found!', 'wporg-forums' ) );
     241
     242        // Check valid resolution.
     243        } elseif ( ! $this->is_valid_topic_resolution( $resolution ) ) {
     244            bbp_add_error( 'wporg_bbp_topic_resolution_invalid', __( '<strong>ERROR</srong>: That is not a valid topic resolution!', 'wporg-forums' ) );
     245
     246        // Check user permissions.
     247        } elseif ( ! $this->user_can_resolve( $user_id, $topic->ID ) ) {
     248            bbp_add_error( 'wporg_bbp_topic_resolution_permissions', __( '<strong>ERROR</strong>: You don\t have permission to do this!', 'wporg-forums' ) );
     249
     250        // Check nonce.
     251        } elseif ( ! bbp_verify_nonce_request( 'toggle-topic-resolution_' . $topic->ID ) ) {
     252            bbp_add_error( 'wporg_bbp_topic_resolution_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'wporg-forums' ) );
     253        }
     254
     255        if ( bbp_has_errors() ) {
     256            return;
     257        }
     258
     259        // Update the topic resolution.
     260        $this->set_topic_resolution( array(
     261            'id'         => $topic->ID,
     262            'resolution' => $resolution,
     263        ) );
     264
     265        bbp_redirect( get_permalink( $topic->ID ) );
    154266    }
    155267
     
    308420    public function get_topic_resolutions() {
    309421        return apply_filters( 'wporg_bbp_get_topic_resolutions', array(
    310                 'no'  => __( 'not resolved', 'wporg-forums' ),
    311                 'yes' => __( 'resolved', 'wporg-forums' ),
    312                 'mu'  => __( 'not a support question', 'wporg-forums' ),
     422            'no'  => __( 'not resolved', 'wporg-forums' ),
     423            'yes' => __( 'resolved', 'wporg-forums' ),
     424            'mu'  => __( 'not a support question', 'wporg-forums' ),
    313425        ) );
     426    }
     427
     428    /**
     429     * Is a topic resolution string in the array of possible resolutions?
     430     *
     431     * @param string $resolution The resolution to check
     432     * @return bool True if in the array, false if not
     433     */
     434    public function is_valid_topic_resolution( $resolution ) {
     435        $resolutions = $this->get_topic_resolutions();
     436        $retval = in_array( $resolution, array_keys( $resolutions ) );
     437        return apply_filters( 'wporg_bbp_is_valid_topic_resolution', $retval, $resolution );
    314438    }
    315439
     
    330454        return apply_filters( 'wporg_bbp_sanitize_topic_resolution', $retval );
    331455    }
     456
     457    /**
     458     * Is a given user is allowed to update the resolution of a topic?
     459     *
     460     * @param int $user_id The user id
     461     * @param int $topic_id The topic id
     462     * @return bool True if allowed, false if not
     463     */
     464    public function user_can_resolve( $user_id, $topic_id ) {
     465        $topic_id = bbp_get_topic_id();
     466        if ( $topic_id ) {
     467            $post = get_post( $topic_id );
     468        }
     469
     470        if ( $user_id && $post && ( user_can( $user_id, 'moderate' ) || $user_id == $post->post_author ) ) {
     471            $retval = true;
     472        } else {
     473            $retval = false;
     474        }
     475        return apply_filters( 'wporg_bbp_user_can_resolve', $retval, $user_id, $topic_id );
     476    }
    332477}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-topic-resolution/wporg-bbp-topic-resolution.php

    r3666 r3797  
    2727
    2828// Easy access for templates
    29 function get_topic_resolution( $topic_id ) {
    30     return Plugin::get_topic_resolution( array( 'id' => $topic_id ) );
     29function get_topic_resolution_form( $topic_id ) {
     30    return Plugin::get_instance()->get_topic_resolution_form( array( 'id' => $topic_id ) );
    3131}
Note: See TracChangeset for help on using the changeset viewer.