WordPress.org

Making WordPress.org

Changeset 3997


Ignore:
Timestamp:
09/07/2016 07:13:46 PM (5 years ago)
Author:
jmdodd
Message:

Support Forums: Enable new/edit reviews.

These will be visible once the review forum is opened.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-directory-compat.php

    r3995 r3997  
    127127            // Instantiate WPORG_Ratings compat mode for reviews.
    128128            if ( class_exists( 'WPORG_Ratings' ) && class_exists( 'WordPressdotorg\Forums\Ratings_Compat' ) ) {
    129                 $this->ratings = new Ratings_Compat( $this->compat(), $this->slug(), $this->get_object( $this->slug() ) );
     129                $this->ratings = new Ratings_Compat( $this->compat(), $this->slug(), $this->taxonomy(), $this->get_object( $this->slug() ) );
    130130            }
    131131
     
    155155                // Instantiate WPORG_Ratings compat mode for reviews.
    156156                if ( class_exists( 'WPORG_Ratings' ) && class_exists( 'WordPressdotorg\Forums\Ratings_Compat' ) ) {
    157                     $this->ratings = new Ratings_Compat( $this->compat(), $this->slug(), $this->get_object( $this->slug() ) );
     157                    $this->ratings = new Ratings_Compat( $this->compat(), $this->slug(), $this->taxonomy(), $this->get_object( $this->slug() ) );
    158158                }
    159159
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-ratings-compat.php

    r3969 r3997  
    55class Ratings_Compat {
    66
    7     var $compat = null;
    8     var $slug   = null;
    9     var $object = null;
     7    var $compat   = null;
     8    var $slug     = null;
     9    var $object   = null;
     10    var $taxonomy = null;
    1011
    1112    var $filter = false;
    1213
    13     public function __construct( $compat, $slug, $object, $rating = 0 ) {
     14    public function __construct( $compat, $slug, $taxonomy, $object ) {
    1415        if ( ! class_exists( 'WPORG_Ratings' ) ) {
    1516            return;
    1617        }
    1718
    18         if ( empty( $compat ) || empty( $slug ) || empty( $object ) ) {
    19             return;
    20         }
    21 
    22         $this->compat = $compat;
    23         $this->slug   = $slug;
    24         $this->object = $object;
    25         $this->rating = $rating;
    26 
    27         $this->reviews_count = \WPORG_Ratings::get_rating_count( $this->compat, $this->slug, $this->rating );
     19        if ( empty( $compat ) || empty( $slug ) || empty( $taxonomy ) || empty( $object ) ) {
     20            return;
     21        }
     22
     23        $this->compat   = $compat;
     24        $this->slug     = $slug;
     25        $this->taxonomy = $taxonomy;
     26        $this->object   = $object;
     27
    2828        $this->ratings_counts = \WPORG_Ratings::get_rating_counts( $this->compat, $this->slug );
    2929        $this->avg_rating = \WPORG_Ratings::get_avg_rating( $this->compat, $this->slug );
     
    4141        }
    4242
     43        // Total reviews count. Can be altered using $this->filter if needed.
     44        $this->reviews_count = \WPORG_Ratings::get_rating_count( $this->compat, $this->slug, 0 );
     45
    4346        // Set up the single topic.
    4447        add_action( 'bbp_template_before_lead_topic', array( $this, 'add_topic_stars' ) );
     
    4750        add_action( 'wporg_compat_before_single_view', array( $this, 'do_view_header' ) );
    4851        add_filter( 'bbp_get_topic_title', array( $this, 'get_topic_title' ), 10, 2 );
     52
     53        // Add the topic form as either a new or edit form.
     54        add_action( 'wporg_compat_new_review_notice', array( $this, 'do_template_notice' ) );
     55        add_action( 'wporg_compat_after_single_view', array( $this, 'add_topic_form' ) );
     56        add_action( 'bbp_theme_before_topic_form_content', array( $this, 'add_topic_form_stars' ), 8 );
     57
     58        // Check to see if a topic is being created/edited.
     59        add_action( 'bbp_new_topic_post_extras', array( $this, 'topic_post_extras' ) );
     60        add_action( 'bbp_edit_topic_post_extras', array( $this, 'topic_post_extras' ) );
    4961    }
    5062
     
    153165        <?php
    154166            if ( is_user_logged_in() ) {
    155                 echo '<a href="#postform" class="btn">';
     167                echo '<a href="#new-post" class="btn">';
    156168                _e( 'Add your own review', 'wporg-forums' );
    157169                echo '</a>';
     
    184196    <?php
    185197    }
     198
     199    /**
     200     * Set the rating on topic creation or edit.
     201     *
     202     * @param int $topic_id The topic id
     203     */
     204    public function topic_post_extras( $topic_id ) {
     205        if ( isset( $_POST['rating'] ) && in_array( (int) $_POST['rating'], array( 1, 2, 3, 4, 5 ) ) ) {
     206            if (
     207                Plugin::REVIEWS_FORUM_ID == bbp_get_topic_forum_id( $topic_id )
     208            &&
     209                bbp_get_topic_author_id( $topic_id ) == get_current_user_id()
     210            ) {
     211                \WPORG_Ratings::set_rating( $topic_id, $this->compat, $this->slug, bbp_get_topic_author_id( $topic_id ), absint( $_POST['rating'] ) );
     212            }
     213        }
     214    }
     215
     216    public function add_topic_form() {
     217        if ( ! $this->is_rating_view() ) {
     218            return;
     219        }
     220
     221        if ( ! is_user_logged_in() ) {
     222            echo '<p>';
     223            printf(
     224            __( 'You must %s to submit a review.You can also log in or register using the form near the top of this page.' ),
     225            sprintf( '<a href="https://login.wordpress.org/">%s</a>', esc_html_x( 'log in', 'verb: You must log in to submit a review.', 'wporg-forums' ) ) );
     226            echo '</p>';
     227            return;
     228        }
     229
     230        if ( bbp_has_topics( array(
     231            'author'       => get_current_user_id(),
     232            'post_status'  => 'any',
     233            'post_type'    => bbp_get_topic_post_type(),
     234            'post_parent'  => Plugin::REVIEWS_FORUM_ID,
     235            'tax_query'    => array( array(
     236                'taxonomy' => $this->taxonomy,
     237                'field'    => 'slug',
     238                'terms'    => $this->slug,
     239            ) ),
     240            'no_found_rows' => true,
     241            'orderby'       => 'ID',
     242        ) ) ) {
     243            bbp_the_topic();
     244            add_filter( 'bbp_is_topic_edit', '__return_true' );
     245        } else {
     246            add_filter( 'bbp_is_topic_edit', '__return_false' );
     247        }
     248
     249        bbp_get_template_part( 'form', 'topic' );
     250    }
     251
     252    public function do_template_notice() {
     253        $report = $rate = '';
     254        switch( $this->compat ) {
     255            case 'plugin' :
     256                $report = __( 'If you are reporting an issue with this plugin, please post %s instead.', 'wporg-forums' );
     257                $rate   = __( 'In order to rate a plugin, you must also submit a review.', 'wporg-forums' );
     258                break;
     259            case 'theme' :
     260                $report = __( 'If you are reporting an issue with this theme, please post %s instead.', 'wporg-forums' );
     261                $rate   = __( 'In order to rate a theme, you must also submit a review.', 'wporg-forums' );
     262                break;
     263        }
     264        ?>
     265        <p><?php _e( 'When posting a review, follow these guidelines:', 'wporg-forums' ); ?></p>
     266        <ul>
     267            <li><?php printf( esc_html( $report ), sprintf( '<a href="%s">%s</a>',
     268                    esc_url( sprintf( 'https://wordpress.org/support/%s/%s/', $this->compat, $this->slug ) ),
     269                    _x( 'here', 'please post here instead', 'wporg-forums' )
     270                ) ); ?></li>
     271            <li><?php echo esc_html( $rate ); ?></li>
     272            <li><?php esc_html_e( 'Please provide as much detail as you can to justify your rating and to help others.', 'wporg-forums' ); ?></li>
     273        </ul>
     274        <?php
     275    }
     276
     277    public function add_topic_form_stars() {
     278        if ( ! $this->is_rating_view() ) {
     279            return;
     280        }
     281
     282        if ( bbp_is_topic_edit() && bbp_get_topic_author_id() != get_current_user_id() ) {
     283            return;
     284        }
     285
     286        \WPORG_Ratings::get_dashicons_form( $this->compat, $this->slug, true );
     287    }
     288
     289    public function is_rating_view() {
     290        if ( bbp_is_single_view() && 'reviews' == bbp_get_view_id() ) {
     291            return true;
     292        }
     293
     294        if ( bbp_is_topic_edit() && bbp_get_topic_forum_id() == Plugin::REVIEWS_FORUM_ID ) {
     295            return true;
     296        }
     297
     298        return false;
     299    }
    186300}
Note: See TracChangeset for help on using the changeset viewer.