Making WordPress.org


Ignore:
Timestamp:
08/31/2016 04:58:29 PM (10 years ago)
Author:
jmdodd
Message:

Support Forums: Move logic into parent class, and display-specific functions into child compat classes.

Theme and plugin views share many behaviors; to avoid trying to keep both updated, this moves taxonomy registration, tests for loading actions and filters, view registration, rewrite rules, and general display logic into the general directory compat class.

Constants, helper functions, and unique display methods have been separated into their respective plugin or theme compat class.

File:
1 edited

Legend:

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

    r3846 r3892  
    66
    77        abstract protected function compat();
     8        abstract protected function compat_title();
    89        abstract protected function slug();
    910        abstract protected function title();
    … …  
    1112        abstract protected function query_var();
    1213        abstract protected function taxonomy();
    13 
    14         public function __construct() {}
     14        abstract protected function parse_query();
     15        abstract protected function do_view_sidebar();
     16        abstract protected function do_topic_sidebar();
     17        abstract protected function get_view_header();
     18
     19        var $authors      = null;
     20        var $contributors = null;
    1521
    1622        public function init() {
     23                if ( defined( 'WPORG_SUPPORT_FORUMS_BLOGID' ) && get_current_blog_id() == WPORG_SUPPORT_FORUMS_BLOGID ) {
     24                        // Define the taxonomy and query vars for this view.
     25                        add_action( 'plugins_loaded', array( $this, 'always_load' ) );
     26
     27                        // We have to add the custom view before bbPress runs its own action
     28                        // on parse_query at priority 2.
     29                        add_action( 'parse_query', array( $this, 'parse_query' ), 0 );
     30
     31                        // And this still needs to happen before priority 2.
     32                        add_action( 'parse_query', array( $this, 'maybe_load' ), 1 );
     33
     34                        // Check to see if an individual topic is compat.
     35                        add_action( 'template_redirect', array( $this, 'check_topic_for_compat' ) );
     36                }
     37        }
     38
     39        public function always_load() {
     40                // Add filters necessary for determining which compat file to use.
    1741                add_action( 'bbp_init',              array( $this, 'register_taxonomy' ) );
    1842                add_filter( 'query_vars',            array( $this, 'add_query_var' ) );
    1943                add_action( 'bbp_add_rewrite_rules', array( $this, 'add_rewrite_rules' ) );
    2044
    21                 add_filter( 'bbp_get_view_link',     array( $this, 'get_view_link' ), 10, 2 );
    22                 add_filter( 'bbp_breadcrumbs',       array( $this, 'breadcrumbs' ) );
     45        }
     46
     47        public function maybe_load() {
     48                if ( false !== $this->slug() ) {
     49                        // This must run before bbPress's parse_query at priority 2.
     50                        $this->register_views();
     51
     52                        // Add theme-specific filters and actions.
     53                        add_action( 'wporg_compat_view_sidebar', array( $this, 'do_view_sidebar' ) );
     54
     55                        // Add output filters and actions.
     56                        add_filter( 'bbp_get_view_link',  array( $this, 'get_view_link' ), 10, 2 );
     57                        add_filter( 'bbp_breadcrumbs',    array( $this, 'breadcrumbs' ) );
     58                        add_filter( 'bbp_get_breadcrumb', array( $this, 'get_breadcrumb' ), 10, 3 );
     59                }
     60        }
     61
     62        public function check_topic_for_compat() {
     63                if ( bbp_is_single_topic() ) {
     64                        $slug = wp_get_object_terms( bbp_get_topic_id(), $this->taxonomy(), array( 'fields' => 'slugs' ) );
     65
     66                        // Match found for this compat.
     67                        if ( ! empty( $slug ) ) {
     68                                $slug = $slug[0];
     69
     70                                // Basic setup.
     71                                $this->slug              = $slug;
     72                                $this->{$this->compat()} = $this->get_object( $slug );
     73                                $this->authors           = $this->get_authors( $slug );
     74                                $this->contributors      = $this->get_contributors( $slug );
     75
     76                                // Add output filters and actions.
     77                                if ( ! empty( $this->authors ) || ! empty( $this->contributors ) ) {
     78                                        add_filter( 'bbp_get_topic_author_link', array( $this, 'author_link' ), 10, 2 );
     79                                        add_filter( 'bbp_get_reply_author_link', array( $this, 'author_link' ), 10, 2 );
     80                                }
     81                                add_action( 'wporg_compat_single_topic_sidebar_pre', array( $this, 'do_topic_sidebar' ) );
     82                        }
     83                }
    2384        }
    2485
    … …  
    2990                $root_var   = $this->query_var();
    3091                $review_id  = 'reviews';
     92                $active_id  = 'active';
    3193
    3294                $support_rule = $this->compat() . '/([^/]+)/';
    3395                $reviews_rule = $this->compat() . '/([^/]+)/' . $review_id . '/';
     96                $active_rule  = $this->compat() . '/([^/]+)/' . $active_id . '/';
    3497
    3598                $feed_id    = 'feed';
    … …  
    53116                add_rewrite_rule( $support_rule . $paged_rule, 'index.php?' . $view_id . '=' . $root_id . '&' . $root_var . '=$matches[1]&' . $paged_id . '=$matches[2]', $priority );
    54117                add_rewrite_rule( $support_rule . $feed_rule,  'index.php?' . $view_id . '=' . $root_id . '&' . $root_var . '=$matches[1]&' . $feed_id  . '=$matches[2]', $priority );
     118
     119                // Add active view rewrite rules.
     120                add_rewrite_rule( $active_rule . $base_rule,  'index.php?' . $view_id . '=' . $active_id . '&' . $root_var . '=$matches[1]',                               $priority );
     121                add_rewrite_rule( $active_rule . $paged_rule, 'index.php?' . $view_id . '=' . $active_id . '&' . $root_var . '=$matches[1]&' . $paged_id . '=$matches[2]', $priority );
     122                add_rewrite_rule( $active_rule . $feed_rule,  'index.php?' . $view_id . '=' . $active_id . '&' . $root_var . '=$matches[1]&' . $feed_id  . '=$matches[2]', $priority );
    55123        }
    56124
    … …  
    66134        }
    67135
     136        public function register_views() {
     137
     138                // Add support view.
     139                bbp_register_view(
     140                        $this->compat(),
     141                        $this->compat_title(),
     142                        array(
     143                                'post_parent'   => $this->forum_id(),
     144                                'tax_query'     => array( array(
     145                                        'taxonomy'  => $this->taxonomy(),
     146                                        'field'     => 'slug',
     147                                        'terms'     => $this->slug(),
     148                                ) ),
     149                                'show_stickies' => false,
     150                                'orderby'       => 'ID',
     151                        )
     152                );
     153
     154                // Add reviews view.
     155                bbp_register_view(
     156                        'reviews',
     157                        __( 'Reviews', 'wporg-forums' ),
     158                        array(
     159                                'post_parent'   => Plugin::REVIEWS_FORUM_ID,
     160                                'meta_query'    => array( array(
     161                                        'key'       => '_bbp_last_active_time',
     162                                        'type'      => 'DATETIME',
     163                                ) ),
     164                                'tax_query'     => array( array(
     165                                        'taxonomy'  => $this->taxonomy(),
     166                                        'field'     => 'slug',
     167                                        'terms'     => $this->slug(),
     168                                ) ),
     169                                'show_stickies' => false,
     170                                'orderby'       => 'meta_value',
     171                        )
     172                );
     173
     174                // Add recent activity view.
     175                bbp_register_view(
     176                        'active',
     177                        __( 'Recent Activity', 'wporg-forums' ),
     178                        array(
     179                                'post_parent'   => $this->forum_id(),
     180                                'meta_query'    => array( array(
     181                                        'key'       => '_bbp_last_active_time',
     182                                        'type'      => 'DATETIME',
     183                                ) ),
     184                                'tax_query'     => array( array(
     185                                        'taxonomy'  => $this->taxonomy(),
     186                                        'field'     => 'slug',
     187                                        'terms'     => $this->slug(),
     188                                ) ),
     189                                'show_stickies' => false,
     190                                'orderby'       => 'meta_value',
     191                        )
     192                );
     193        }
     194
    68195        /**
    69196         * Filter view links to provide prettier links for these subforum views.
    … …  
    73200
    74201                $view = bbp_get_view_id( $view );
    75                 if ( $view != $this->compat() ) {
     202                if ( ! in_array( $view, array( 'active', 'reviews', $this->compat() ) ) ) {
    76203                        return $url;
    77204                }
    … …  
    79206                // Pretty permalinks.
    80207                if ( $wp_rewrite->using_permalinks() ) {
    81                         $url = $wp_rewrite->root . $this->compat() . '/' . $this->slug();
     208                        switch ( $view ) {
     209                                case 'active' :
     210                                case 'reviews' :
     211                                        $url = $wp_rewrite->root . $this->compat() . '/' . $this->slug() . '/' . $view;
     212                                        break;
     213
     214                                default :
     215                                        $url = $wp_rewrite->root . $this->compat() . '/' . $this->slug();
     216                        }
    82217                        $url = home_url( user_trailingslashit( $url ) );
    83218
    … …  
    103238
    104239                $view = bbp_get_view_id();
    105                 if ( ! in_array( $view, array( $this->compat(), 'reviews' ) ) ) {
     240                if ( ! in_array( $view, array( $this->compat(), 'reviews', 'active' ) ) ) {
    106241                        return $r;
    107242                }
    … …  
    115250                return $r;
    116251        }
     252
     253        /**
     254         * Prefix a single view-specific header using the breadcrumb filter.
     255         */
     256        public function get_breadcrumb( $trail, $crumbs, $r ) {
     257                if ( bbp_is_single_view() && in_array( bbp_get_view_id(), array( 'theme', 'plugin', 'reviews' ) ) ) {
     258                        $view_header = $this->get_view_header();
     259                        $trail = $view_header . $trail;
     260                }
     261                return $trail;
     262        }
     263
     264        /**
     265         * @todo Add the author or contributor badge.
     266         */
     267        public function author_link( $author_link, $args ) {
     268                return $author_link;
     269        }
     270
     271        /**
     272         * Set up and cache the plugin or theme details.
     273         *
     274         * @param string $slug The object slug
     275         */
     276        public function get_object( $slug ) {
     277                global $wpdb;
     278
     279                if ( 'theme' == $this->compat() ) {
     280                        if ( ! is_null( $this->theme ) ) {
     281                                return $this->theme;
     282                        }
     283                } else {
     284                        if ( ! is_null( $this->plugin ) ) {
     285                                return $this->plugin;
     286                        }
     287                }
     288
     289                // Check the cache.
     290                $cache_key = "{$slug}";
     291                $cache_group = $this->compat() . '-objects';
     292                $compat_object = wp_cache_get( $cache_key, $cache_group );
     293                if ( false === $compat_object ) {
     294
     295                        // Get the object information from the correct table.
     296                        if ( $this->compat() == 'theme' ) {
     297                                $compat_object = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->base_prefix}%d_posts WHERE post_name = %s AND post_type = 'repopackage' LIMIT 1", WPORG_THEME_DIRECTORY_BLOGID, $slug ) );
     298                        } elseif ( $this->compat() == 'plugin' ) {
     299                                $compat_object = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->base_prefix}%d_posts WHERE post_name = %s AND post_type = 'plugin' LIMIT 1", WPORG_PLUGIN_DIRECTORY_BLOGID, $slug ) );
     300                        }
     301
     302                        wp_cache_set( $cache_key, $compat_object, $cache_group, 86400);
     303                }
     304                return $compat_object;
     305        }
     306
     307        public function get_authors( $slug ) {
     308                global $wpdb;
     309
     310                if ( null !== $this->authors ) {
     311                        return $this->authors;
     312                }
     313
     314                // Check the cache.
     315                $cache_key = "{$slug}";
     316                $cache_group = $this->compat() . '-authors';
     317                $authors = wp_cache_get( $cache_key, $cache_group );
     318                if ( false === $authors ) {
     319
     320                        if ( $this->compat() == 'theme' ) {
     321                                $theme = $this->theme;
     322                                $author = get_user_by( 'id', $this->theme->post_author );
     323                                $authors = array( $author->user_login );
     324                        } else {
     325                                $authors = $wpdb->get_col( $wpdb->prepare( " SELECT user FROM plugin_2_svn_access WHERE `path` = %s", '/' . $slug ) );
     326                        }
     327
     328                        wp_cache_set( $cache_key, $authors, $cache_group, 3600 );
     329                }
     330                return $authors;
     331        }
     332
     333        public function get_contributors( $slug ) {
     334                global $wpdb;
     335
     336                if ( null !== $this->contributors ) {
     337                        return $this->contributors;
     338                }
     339
     340                // Themes do not have contributors right now.
     341                if ( $this->compat() == 'theme' ) {
     342                        $contributors = array();
     343                        return $contributors;
     344                }
     345
     346                // Check the cache.
     347                $cache_key = "{$slug}";
     348                $cache_group = $this->compat() . '-contributors';
     349                $contributors = wp_cache_get( $cache_key, $cache_group );
     350                if ( false === $contributors ) {
     351                        $contributors = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->base_prefix}%d_postmeta WHERE post_id = %d AND meta_key = %s LIMIT 1", WPORG_PLUGIN_DIRECTORY_BLOGID, $this->plugin->ID, 'contributors' ) );
     352                        $contributors = maybe_unserialize( $contributors );
     353
     354                        wp_cache_set( $cache_key, $contributors, $cache_group, 3600 );
     355                }
     356                return $contributors;
     357        }
    117358}
Note: See TracChangeset for help on using the changeset viewer.