Making WordPress.org


Ignore:
Timestamp:
07/07/2017 05:38:42 PM (7 years ago)
Author:
SergeyBiryukov
Message:

Support Forums: Add rewrite rules and parse query arguments for user's "Active Topics" and "Topics Replied To" views.

Move user's "Reviews Written" view handling to Users class.

See #2470.

File:
1 edited

Legend:

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

    r5605 r5625  
    1818        // Only allow 3 published topics from a user in the first 24 hours.
    1919        add_action( 'bbp_new_topic_pre_insert', array( $this, 'limit_new_user_topics' ) );
     20
     21        // Add query vars and rewrite rules for user's topic and review queries.
     22        add_filter( 'query_vars',            array( $this, 'add_query_vars' ) );
     23        add_action( 'bbp_add_rewrite_rules', array( $this, 'add_rewrite_rules' ) );
     24
     25        // Parse user's topic and review queries.
     26        add_action( 'parse_query',                     array( $this, 'parse_user_topics_query' ) );
     27        add_filter( 'posts_groupby',                   array( $this, 'parse_user_topics_posts_groupby' ), 10, 2 );
     28        add_filter( 'bbp_after_has_topics_parse_args', array( $this, 'parse_user_topics_query_args' ) );
     29        add_filter( 'bbp_topic_pagination',            array( $this, 'parse_user_topics_pagination_args' ) );
     30        add_filter( 'bbp_replies_pagination',          array( $this, 'parse_user_topics_pagination_args' ) );
     31        add_filter( 'bbp_before_title_parse_args',     array( $this, 'parse_user_topics_title_args' ) );
    2032    }
    2133
     
    88100    }
    89101
     102    /**
     103     * Add query vars for user's "Reviews Written", "Active Topics",
     104     * and "Topics Replied To" pages.
     105     *
     106     * @param array $query_vars Query vars.
     107     * @return array Filtered query vars.
     108     */
     109    public function add_query_vars( $query_vars ) {
     110        $query_vars[] = 'wporg_single_user_reviews';
     111        $query_vars[] = 'wporg_single_user_active_topics';
     112        $query_vars[] = 'wporg_single_user_topics_replied_to';
     113        return $query_vars;
     114    }
     115
     116    /**
     117     * Add rewrite rules for user's "Reviews Written", "Active Topics",
     118     * and "Topics Replied To" pages.
     119     */
     120    public function add_rewrite_rules() {
     121        $priority   = 'top';
     122
     123        $user_reviews_rule           = bbp_get_user_slug() . '/([^/]+)/reviews/';
     124        $user_active_topics_rule     = bbp_get_user_slug() . '/([^/]+)/active/';
     125        $user_topics_replied_to_rule = bbp_get_user_slug() . '/([^/]+)/replied-to/';
     126
     127        $feed_id    = 'feed';
     128        $user_id    = bbp_get_user_rewrite_id();
     129        $paged_id   = bbp_get_paged_rewrite_id();
     130
     131        $feed_slug  = 'feed';
     132        $paged_slug = bbp_get_paged_slug();
     133
     134        $base_rule  = '?$';
     135        $feed_rule  = $feed_slug . '/?$';
     136        $paged_rule = $paged_slug . '/?([0-9]{1,})/?$';
     137
     138        // Add user's "Reviews Written" page rewrite rules.
     139        add_rewrite_rule( $user_reviews_rule . $base_rule,  'index.php?' . $user_id . '=$matches[1]&wporg_single_user_reviews=1',                               $priority );
     140        add_rewrite_rule( $user_reviews_rule . $paged_rule, 'index.php?' . $user_id . '=$matches[1]&wporg_single_user_reviews=1&' . $paged_id . '=$matches[2]', $priority );
     141        add_rewrite_rule( $user_reviews_rule . $feed_rule,  'index.php?' . $user_id . '=$matches[1]&wporg_single_user_reviews=1&' . $feed_id  . '=$matches[2]', $priority );
     142
     143        // Add user's "Active Topics" page rewrite rules.
     144        add_rewrite_rule( $user_active_topics_rule . $base_rule,  'index.php?' . $user_id . '=$matches[1]&wporg_single_user_active_topics=1',                               $priority );
     145        add_rewrite_rule( $user_active_topics_rule . $paged_rule, 'index.php?' . $user_id . '=$matches[1]&wporg_single_user_active_topics=1&' . $paged_id . '=$matches[2]', $priority );
     146        add_rewrite_rule( $user_active_topics_rule . $feed_rule,  'index.php?' . $user_id . '=$matches[1]&wporg_single_user_active_topics=1&' . $feed_id  . '=$matches[2]', $priority );
     147
     148        // Add user's "Topics Replied To" page rewrite rules.
     149        add_rewrite_rule( $user_topics_replied_to_rule . $base_rule,  'index.php?' . $user_id . '=$matches[1]&wporg_single_user_topics_replied_to=1',                               $priority );
     150        add_rewrite_rule( $user_topics_replied_to_rule . $paged_rule, 'index.php?' . $user_id . '=$matches[1]&wporg_single_user_topics_replied_to=1&' . $paged_id . '=$matches[2]', $priority );
     151        add_rewrite_rule( $user_topics_replied_to_rule . $feed_rule,  'index.php?' . $user_id . '=$matches[1]&wporg_single_user_topics_replied_to=1&' . $feed_id  . '=$matches[2]', $priority );
     152    }
     153
     154    /**
     155     * Set WP_Query::bbp_is_single_user_profile to false on user's "Reviews Written",
     156     * "Active Topics", and "Topics Replied To" pages.
     157     *
     158     * @param WP_Query $query Current query object.
     159     */
     160    public function parse_user_topics_query( $query ) {
     161        if (
     162            get_query_var( 'wporg_single_user_reviews' )
     163        ||
     164            get_query_var( 'wporg_single_user_active_topics' )
     165        ||
     166            get_query_var( 'wporg_single_user_topics_replied_to' )
     167        ) {
     168            $query->bbp_is_single_user_profile = false;
     169        }
     170    }
     171
     172    /**
     173     * Filter the GROUP BY clause on user's "Topics Replied To" page
     174     * in order to group replies by topic.
     175     *
     176     * @param string   $groupby The GROUP BY clause of the query.
     177     * @param WP_Query $query   The WP_Query instance.
     178     * @return string Filtered GROUP BY clause.
     179     */
     180    public function parse_user_topics_posts_groupby( $groupby, $query ) {
     181        global $wpdb;
     182
     183        if ( 'reply' === $query->get( 'post_type' ) && get_query_var( 'wporg_single_user_topics_replied_to' ) ) {
     184            $groupby = "$wpdb->posts.post_parent";
     185        }
     186
     187        return $groupby;
     188    }
     189
     190    /**
     191     * Set forum ID for user's Reviews query.
     192     *
     193     * @param array $args WP_Query arguments.
     194     * @return array Filtered query arguments.
     195     */
     196    public function parse_user_topics_query_args( $args ) {
     197        if ( get_query_var( 'wporg_single_user_reviews' ) ) {
     198            $args['post_parent'] = Plugin::REVIEWS_FORUM_ID;
     199        } elseif ( bbp_is_single_user_topics() || get_query_var( 'wporg_single_user_active_topics' ) ) {
     200            $args['post_parent__not_in'] = array( Plugin::REVIEWS_FORUM_ID );
     201        }
     202
     203        return $args;
     204    }
     205
     206    /**
     207     * Set 'base' argument for pagination links on user's "Reviews Written",
     208     * "Active Topics", and "Topics Replied To" pages.
     209     *
     210     * @param array $args Pagination arguments.
     211     * @return array Filtered pagination arguments.
     212     */
     213    public function parse_user_topics_pagination_args( $args ) {
     214        if ( get_query_var( 'wporg_single_user_reviews' ) ) {
     215            $args['base']  = bbp_get_user_profile_url( bbp_get_displayed_user_id() ) . 'reviews/';
     216            $args['base'] .= bbp_get_paged_slug() . '/%#%/';
     217        }
     218
     219        if ( get_query_var( 'wporg_single_user_active_topics' ) ) {
     220            $args['base']  = bbp_get_user_profile_url( bbp_get_displayed_user_id() ) . 'active/';
     221            $args['base'] .= bbp_get_paged_slug() . '/%#%/';
     222        }
     223
     224        if ( get_query_var( 'wporg_single_user_topics_replied_to' ) ) {
     225            $args['base']  = bbp_get_user_profile_url( bbp_get_displayed_user_id() ) . 'replied-to/';
     226            $args['base'] .= bbp_get_paged_slug() . '/%#%/';
     227        }
     228
     229        return $args;
     230    }
     231
     232    /**
     233     * Set title for user's "Reviews Written", "Active Topics",
     234     * and "Topics Replied To" pages.
     235     *
     236     * @param array $title Title parts.
     237     * @return array Filtered title parts.
     238     */
     239    public function parse_user_topics_title_args( $title ) {
     240        if ( get_query_var( 'wporg_single_user_reviews' ) ) {
     241            if ( bbp_is_user_home() ) {
     242                $title['text'] = __( 'Your Reviews Written', 'wporg-forums' );
     243            } else {
     244                $title['text'] = get_userdata( bbp_get_user_id() )->display_name;
     245                /* translators: user's display name */
     246                $title['format'] = __( "%s's Reviews Written", 'wporg-forums' );
     247            }
     248        }
     249
     250        if ( get_query_var( 'wporg_single_user_active_topics' ) ) {
     251            if ( bbp_is_user_home() ) {
     252                $title['text'] = __( 'Your Active Topics', 'wporg-forums' );
     253            } else {
     254                $title['text'] = get_userdata( bbp_get_user_id() )->display_name;
     255                /* translators: user's display name */
     256                $title['format'] = __( "%s's Active Topics", 'wporg-forums' );
     257            }
     258        }
     259
     260        if ( get_query_var( 'wporg_single_user_topics_replied_to' ) ) {
     261            if ( bbp_is_user_home() ) {
     262                $title['text'] = __( "Topics You've Replied To", 'wporg-forums' );
     263            } else {
     264                $title['text'] = get_userdata( bbp_get_user_id() )->display_name;
     265                /* translators: user's display name */
     266                $title['format'] = __( 'Topics %s Has Replied To', 'wporg-forums' );
     267            }
     268        }
     269
     270        return $title;
     271    }
     272
    90273}
Note: See TracChangeset for help on using the changeset viewer.