Making WordPress.org

Changeset 5625


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.

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

Legend:

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

    r5563 r5625  
    371371            ( bbp_is_single_view() && ! in_array( bbp_get_view_id(), $this->compat_views() ) )
    372372        ||
    373             ( bbp_is_single_user_topics() || bbp_is_single_user_replies() || get_query_var( 'wporg_single_user_reviews' ) )
     373            ( bbp_is_single_user_topics() || bbp_is_single_user_replies() )
     374        ||
     375            get_query_var( 'wporg_single_user_reviews' )
     376        ||
     377            get_query_var( 'wporg_single_user_active_topics' )
     378        ||
     379            get_query_var( 'wporg_single_user_topics_replied_to' )
    374380        ) {
    375381            $terms = get_the_terms( $topic_id, $this->taxonomy() );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-performance-optimizations.php

    r5162 r5625  
    199199                    add_filter( 'posts_where', array( $this, 'posts_in_last_year' ) );
    200200                }
     201                return $r;
     202            }
     203
     204            // Only look at the last year of topics for user's "Active Topics" view.
     205            if ( get_query_var( 'wporg_single_user_active_topics' ) ) {
     206                add_filter( 'posts_where', array( $this, 'posts_in_last_year' ) );
    201207                return $r;
    202208            }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-support-compat.php

    r5286 r5625  
    3030            add_filter( 'bbp_request', array( $this, 'request' ), 9 );
    3131
    32             // Add views for plugin committer/contributor.
     32            // Add query vars and rewrite rules for plugin committer/contributor.
    3333            add_filter( 'query_vars',            array( $this, 'add_query_var' ) );
    3434            add_action( 'bbp_add_rewrite_rules', array( $this, 'add_rewrite_rules' ) );
     
    3737            // on parse_query at priority 2.
    3838            add_action( 'parse_query',           array( $this, 'parse_query' ), 0 );
    39 
    40             // Parse user's reviews query.
    41             add_action( 'parse_query',                     array( $this, 'parse_user_reviews_query' ) );
    42             add_filter( 'bbp_after_has_topics_parse_args', array( $this, 'parse_user_reviews_query_args' ) );
    43             add_filter( 'bbp_topic_pagination',            array( $this, 'parse_user_reviews_pagination_args' ) );
    44             add_filter( 'bbp_before_title_parse_args',     array( $this, 'parse_user_reviews_title_args' ) );
    4539
    4640            // Exclude compat forums from forum dropdown.
     
    215209
    216210    /**
    217      * Set WP_Query::bbp_is_single_user_profile to false on user's reviews page.
    218      *
    219      * @param WP_Query $query Current query object.
    220      */
    221     public function parse_user_reviews_query( $query ) {
    222         if ( get_query_var( 'wporg_single_user_reviews' ) ) {
    223             $query->bbp_is_single_user_profile = false;
    224         }
    225     }
    226 
    227     /**
    228      * Set forum ID for user's reviews query.
    229      *
    230      * @param array $args WP_Query arguments.
    231      * @return array Filtered query arguments.
    232      */
    233     public function parse_user_reviews_query_args( $args ) {
    234         if ( get_query_var( 'wporg_single_user_reviews' ) ) {
    235             $args['post_parent'] = Plugin::REVIEWS_FORUM_ID;
    236         } elseif ( bbp_is_single_user_topics() ) {
    237             $args['post_parent__not_in'] = array( Plugin::REVIEWS_FORUM_ID );
    238         }
    239 
    240         return $args;
    241     }
    242 
    243     /**
    244      * Set 'base' argument for pagination links on user's reviews page.
    245      *
    246      * @param array $args Pagination arguments.
    247      * @return array Filtered pagination arguments.
    248      */
    249     public function parse_user_reviews_pagination_args( $args ) {
    250         if ( get_query_var( 'wporg_single_user_reviews' ) ) {
    251             $args['base']  = bbp_get_user_profile_url( bbp_get_displayed_user_id() ) . 'reviews/';
    252             $args['base'] .= bbp_get_paged_slug() . '/%#%/';
    253         }
    254 
    255         return $args;
    256     }
    257 
    258     /**
    259      * Set title for user's reviews page.
    260      *
    261      * @param array $title Title parts.
    262      * @return array Filtered title parts.
    263      */
    264     public function parse_user_reviews_title_args( $title ) {
    265         if ( get_query_var( 'wporg_single_user_reviews' ) ) {
    266             if ( bbp_is_user_home() ) {
    267                 $title['text'] = __( 'Your Reviews', 'wporg-forums' );
    268             } else {
    269                 $title['text'] = get_userdata( bbp_get_user_id() )->display_name;
    270                 /* translators: user's display name */
    271                 $title['format'] = __( "%s's Reviews", 'wporg-forums' );
    272             }
    273         }
    274 
    275         return $title;
    276     }
    277 
     211     * Add query vars for plugin committer/contributor views.
     212     *
     213     * @param array $query_vars Query vars.
     214     * @return array Filtered query vars.
     215     */
    278216    public function add_query_var( $query_vars ) {
    279217        $query_vars[] = 'wporg_user_login';
    280         $query_vars[] = 'wporg_single_user_reviews';
    281218        return $query_vars;
    282219    }
    283220
     221    /**
     222     * Add rewrite rules for plugin committer/contributor views.
     223     */
    284224    public function add_rewrite_rules() {
    285225        $priority   = 'top';
     
    287227        $plugin_committer_rule   = bbp_get_view_slug() . '/plugin-committer/([^/]+)/';
    288228        $plugin_contributor_rule = bbp_get_view_slug() . '/plugin-contributor/([^/]+)/';
    289         $user_reviews_rule       = bbp_get_user_slug() . '/([^/]+)/reviews/';
    290229
    291230        $feed_id    = 'feed';
    292231        $view_id    = bbp_get_view_rewrite_id();
    293         $user_id    = bbp_get_user_rewrite_id();
    294232        $paged_id   = bbp_get_paged_rewrite_id();
    295233
     
    310248        add_rewrite_rule( $plugin_contributor_rule . $paged_rule, 'index.php?' . $view_id . '=plugin-contributor&wporg_user_login=$matches[1]&' . $paged_id . '=$matches[2]', $priority );
    311249        add_rewrite_rule( $plugin_contributor_rule . $feed_rule,  'index.php?' . $view_id . '=plugin-contributor&wporg_user_login=$matches[1]&' . $feed_id  . '=$matches[2]', $priority );
    312 
    313         // Add user's reviews rewrite rules.
    314         add_rewrite_rule( $user_reviews_rule . $base_rule,  'index.php?' . $user_id . '=$matches[1]&wporg_single_user_reviews=1',                               $priority );
    315         add_rewrite_rule( $user_reviews_rule . $paged_rule, 'index.php?' . $user_id . '=$matches[1]&wporg_single_user_reviews=1&' . $paged_id . '=$matches[2]', $priority );
    316         add_rewrite_rule( $user_reviews_rule . $feed_rule,  'index.php?' . $user_id . '=$matches[1]&wporg_single_user_reviews=1&' . $feed_id  . '=$matches[2]', $priority );
    317250    }
    318251
  • 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.