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.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc
Files:
1 added
1 copied

Legend:

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

    r2999 r3002  
    11<?php
    2 /**
    3  * Plugin name: GlotPress: Custom Routes
    4  * Description: Provides custom routes like <code>/locale</code> or <code>/stats</code> for translate.wordpress.org.
    5  * Version:     1.0
    6  * Author:      WordPress.org
    7  * Author URI:  http://wordpress.org/
    8  * License:     GPLv2 or later
    9  */
    102
    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';
     3namespace WordPressdotorg\GlotPress\Routes;
    184
    19 class WPorg_GP_Routes {
     5use GP;
     6use GP_Locales;
     7use WP_CLI;
    208
    21     public function __construct() {
     9class Plugin {
     10
     11    /**
     12     * @var Plugin The singleton instance.
     13     */
     14    private static $instance;
     15
     16    /**
     17     *
     18     * @var Sync\Translation_Sync
     19     */
     20    public $translation_sync = null;
     21
     22    /**
     23     * Returns always the same instance of this plugin.
     24     *
     25     * @return Plugin
     26     */
     27    public static function get_instance() {
     28        if ( ! ( self::$instance instanceof Plugin ) ) {
     29            self::$instance = new Plugin();
     30        }
     31        return self::$instance;
     32    }
     33
     34    /**
     35     * Instantiates a new Plugin object.
     36     */
     37    private function __construct() {
     38        add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
     39    }
     40
     41    /**
     42     * Initializes the plugin.
     43     */
     44    public function plugins_loaded() {
     45        if ( file_exists( WPORGPATH . 'extend/plugins-plugins/_plugin-icons.php' ) ) {
     46            include_once WPORGPATH . 'extend/plugins-plugins/_plugin-icons.php';
     47        }
     48
    2249        add_action( 'template_redirect', array( $this, 'register_routes' ), 5 );
    2350
     
    6592            GP::$router->remove( "/languages/$locale" );
    6693            GP::$router->remove( "/languages/$locale/$path" );
    67             GP::$router->remove( "/profile" );
     94            GP::$router->remove( '/profile' );
    6895            GP::$router->remove( "/profile/$path" );
    6996
    7097            // 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' ) );
     98            GP::$router->prepend( '/languages', array( __NAMESPACE__ . '\Routes\Redirector', 'redirect_languages' ) );
     99            GP::$router->prepend( "/languages/$path", array( __NAMESPACE__ . '\Routes\Redirector', 'redirect_languages' ) );
     100            GP::$router->prepend( '/projects/wp-plugins/?', array( __NAMESPACE__ . '\Routes\Redirector', 'redirect_index' ) );
     101            GP::$router->prepend( '/projects/wp-themes/?', array( __NAMESPACE__ . '\Routes\Redirector', 'redirect_index' ) );
    75102
    76103            // 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' ) );
     104            GP::$router->prepend( '/', array( __NAMESPACE__ . '\Routes\Index', 'get_locales' ) );
     105            GP::$router->prepend( "/locale/$locale", array( __NAMESPACE__ . '\Routes\Locale', 'get_locale_projects' ) );
     106            GP::$router->prepend( "/locale/$locale/$path", array( __NAMESPACE__ . '\Routes\Locale', 'get_locale_projects' ) );
     107            GP::$router->prepend( "/locale/$locale/$path/$path", array( __NAMESPACE__ . '\Routes\Locale', 'get_locale_projects' ) );
     108            GP::$router->prepend( "/locale/$locale/$path/$path/$path", array( __NAMESPACE__ . '\Routes\Locale', 'get_locale_project' ) );
     109            GP::$router->prepend( '/stats/?', array( __NAMESPACE__ . '\Routes\Stats', 'get_stats_overview' ) );
    83110            $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' ) );
     111            GP::$router->prepend( "/projects/wp-plugins/$project", array( __NAMESPACE__ . '\Routes\WP_Plugins', 'get_plugin_projects' ) );
     112            GP::$router->prepend( "/projects/wp-plugins/$project/contributors", array( __NAMESPACE__ . '\Routes\WP_Plugins', 'get_plugin_contributors' ) );
     113            GP::$router->prepend( "/projects/wp-plugins/$project/language-packs", array( __NAMESPACE__ . '\Routes\WP_Plugins', 'get_plugin_language_packs' ) );
     114            GP::$router->prepend( "/projects/wp-themes/$project", array( __NAMESPACE__ . '\Routes\WP_Themes', 'get_theme_projects' ) );
     115            GP::$router->prepend( "/projects/wp-themes/$project/contributors", array( __NAMESPACE__ . '\Routes\WP_Themes', 'get_theme_contributors' ) );
     116            GP::$router->prepend( "/projects/wp-themes/$project/language-packs", array( __NAMESPACE__ . '\Routes\WP_Themes', 'get_theme_language_packs' ) );
    90117        }
    91118    }
     
    95122     */
    96123    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' );
     124        WP_CLI::add_command( 'wporg-translate update-cache', __NAMESPACE__ . '\CLI\Update_Caches' );
    100125    }
    101126}
    102 
    103 function wporg_gp_routes() {
    104     global $wporg_gp_routes;
    105 
    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' );
Note: See TracChangeset for help on using the changeset viewer.