Making WordPress.org


Ignore:
Timestamp:
04/25/2016 03:38:34 PM (10 years ago)
Author:
ocean90
Message:

Translate: Refactor the Custom Routes plugin to use an autoloader.

See #1682.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/wporg-gp-routes.php

    r2932 r3002  
    33 * Plugin name: GlotPress: Custom Routes
    44 * Description: Provides custom routes like <code>/locale</code> or <code>/stats</code> for translate.wordpress.org.
    5  * Version:     1.0
     5 * Version:     2.0
    66 * Author:      WordPress.org
    77 * Author URI:  http://wordpress.org/
     
    99 */
    1010
    11 require_once __DIR__ . '/routes/redirector.php';
    12 require_once __DIR__ . '/routes/index.php';
    13 require_once __DIR__ . '/routes/locale.php';
    14 require_once __DIR__ . '/routes/stats-overview.php';
    15 require_once __DIR__ . '/routes/wp-directory.php';
    16 require_once __DIR__ . '/routes/wp-plugins.php';
    17 require_once __DIR__ . '/routes/wp-themes.php';
     11namespace WordPressdotorg\GlotPress\Routes;
    1812
    19 class WPorg_GP_Routes {
     13use WordPressdotorg\Autoload;
    2014
    21         public function __construct() {
    22                 add_action( 'template_redirect', array( $this, 'register_routes' ), 5 );
     15// Store the root plugin file for usage with functions which use the plugin basename.
     16define( __NAMESPACE__ . '\PLUGIN_FILE', __FILE__ );
    2317
    24                 if ( defined( 'WP_CLI' ) && WP_CLI ) {
    25                         $this->register_cli_commands();
    26                 }
    27         }
    28 
    29         /**
    30          * Registers custom routes and removes default routes.
    31          *
    32          * Removes:
    33          *  - API: /languages/$locale
    34          *  - /languages/$locale
    35          *  - /languages/$locale
    36          *  - /languages/$locale/$path
    37          *  - /profile/$path
    38          *  - /projects/wp-plugins/?
    39          *  - /projects/wp-themes/?
    40          *
    41          * Adds:
    42          *  - /
    43          *  - /locale/$locale
    44          *  - /locale/$locale/$path
    45          *  - /locale/$locale/$path/$path
    46          *  - /locale/$locale/$path/$path/$path
    47          *  - /stats/?
    48          *  - /projects/wp-plugins/$project
    49          *  - /projects/wp-plugins/$project/contributors
    50          *  - /projects/wp-plugins/$project/language-packs
    51          *  - /projects/wp-themes/$project
    52          *  - /projects/wp-themes/$project/contributors
    53          *  - /projects/wp-themes/$project/language-packs
    54          */
    55         public function register_routes() {
    56                 $request_uri = GP::$router->request_uri();
    57                 $path = '(.+?)';
    58                 $locale = '(' . implode( '|', array_map( function( $locale ) { return $locale->slug; }, GP_Locales::locales() ) ) . ')';
    59 
    60                 if ( gp_startswith( $request_uri, '/' . GP::$router->api_prefix . '/' ) ) { // API requests.
    61                         // Delete default routes.
    62                         GP::$router->remove( "/languages/$locale" );
    63                 } else {
    64                         // Delete default routes.
    65                         GP::$router->remove( "/languages/$locale" );
    66                         GP::$router->remove( "/languages/$locale/$path" );
    67                         GP::$router->remove( "/profile" );
    68                         GP::$router->remove( "/profile/$path" );
    69 
    70                         // Redirect routes.
    71                         GP::$router->prepend( '/languages', array( 'WPorg_GP_Route_Redirector', 'redirect_languages' ) );
    72                         GP::$router->prepend( "/languages/$path", array( 'WPorg_GP_Route_Redirector', 'redirect_languages' ) );
    73                         GP::$router->prepend( '/projects/wp-plugins/?', array( 'WPorg_GP_Route_Redirector', 'redirect_index' ) );
    74                         GP::$router->prepend( '/projects/wp-themes/?', array( 'WPorg_GP_Route_Redirector', 'redirect_index' ) );
    75 
    76                         // Register custom routes.
    77                         GP::$router->prepend( '/', array( 'WPorg_GP_Route_Index', 'get_locales' ) );
    78                         GP::$router->prepend( "/locale/$locale", array( 'WPorg_GP_Route_Locale', 'get_locale_projects' ) );
    79                         GP::$router->prepend( "/locale/$locale/$path", array( 'WPorg_GP_Route_Locale', 'get_locale_projects' ) );
    80                         GP::$router->prepend( "/locale/$locale/$path/$path", array( 'WPorg_GP_Route_Locale', 'get_locale_projects' ) );
    81                         GP::$router->prepend( "/locale/$locale/$path/$path/$path", array( 'WPorg_GP_Route_Locale', 'get_locale_project' ) );
    82                         GP::$router->prepend( '/stats/?', array( 'WPorg_GP_Route_Stats', 'get_stats_overview' ) );
    83                         $project = '([^/]*)/?';
    84                         GP::$router->prepend( "/projects/wp-plugins/$project", array( 'WPorg_GP_Route_WP_Plugins', 'get_plugin_projects' ) );
    85                         GP::$router->prepend( "/projects/wp-plugins/$project/contributors", array( 'WPorg_GP_Route_WP_Plugins', 'get_plugin_contributors' ) );
    86                         GP::$router->prepend( "/projects/wp-plugins/$project/language-packs", array( 'WPorg_GP_Route_WP_Plugins', 'get_plugin_language_packs' ) );
    87                         GP::$router->prepend( "/projects/wp-themes/$project", array( 'WPorg_GP_Route_WP_Themes', 'get_theme_projects' ) );
    88                         GP::$router->prepend( "/projects/wp-themes/$project/contributors", array( 'WPorg_GP_Route_WP_Themes', 'get_theme_contributors' ) );
    89                         GP::$router->prepend( "/projects/wp-themes/$project/language-packs", array( 'WPorg_GP_Route_WP_Themes', 'get_theme_language_packs' ) );
    90                 }
    91         }
    92 
    93         /**
    94          * Registers CLI commands if WP-CLI is loaded.
    95          */
    96         function register_cli_commands() {
    97                 require_once __DIR__ . '/cli/update-caches.php';
    98 
    99                 WP_CLI::add_command( 'wporg-translate update-cache', 'WPorg_GP_CLI_Update_Caches' );
    100         }
     18if ( ! class_exists( '\WordPressdotorg\Autoload\Autoloader', false ) ) {
     19        include __DIR__ . '/vendor/wordpressdotorg/class-autoloader.php';
    10120}
    10221
    103 function wporg_gp_routes() {
    104         global $wporg_gp_routes;
     22// Register an Autoloader for all files.
     23Autoload\register_class_path( __NAMESPACE__, __DIR__ . '/inc' );
    10524
    106         if ( ! isset( $wporg_gp_routes ) ) {
    107                 $wporg_gp_routes = new WPorg_GP_Routes();
    108         }
    109 
    110         return $wporg_gp_routes;
    111 }
    112 add_action( 'plugins_loaded', 'wporg_gp_routes' );
     25// Instantiate the Plugin.
     26Plugin::get_instance();
Note: See TracChangeset for help on using the changeset viewer.