Making WordPress.org

Changeset 2988


Ignore:
Timestamp:
04/20/2016 12:21:41 PM (10 years ago)
Author:
ocean90
Message:

Translate: Open-source the CLI command to initialize a new locale.

Also, rewrite the Customizations plugin to use an autoloader.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations
Files:
7 added
1 edited

Legend:

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

    r2953 r2988  
    33 * Plugin name: GlotPress: WordPress.org Customizations
    44 * Description: Provides general customizations 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 class WPorg_GP_Customizations {
     11namespace WordPressdotorg\GlotPress\Customizations;
    1212
    13         function __construct() {
    14                 add_filter( 'gp_url_profile', array( $this, 'worg_profile_url' ), 10, 2 );
    15                 add_filter( 'gp_projects', array( $this, 'natural_sort_projects' ), 10, 2 );
    16                 add_action( 'gp_project_created', array( $this, 'update_projects_last_updated' ) );
    17                 add_action( 'gp_project_saved', array( $this, 'update_projects_last_updated' ) );
    18                 add_filter( 'pre_handle_404', array( $this, 'short_circuit_handle_404' ) );
    19                 add_action( 'wp_default_scripts', array( $this, 'bump_script_versions' ) );
    20         }
     13use WordPressdotorg\Autoload;
    2114
    22         /**
    23          * Changes the versions of some default scripts for cache bust.
    24          *
    25          * @see https://wordpress.slack.com/archives/meta-i18n/p1460626195000251
    26          *
    27          * @param WP_Scripts &$scripts WP_Scripts instance, passed by reference.
    28          */
    29         public function bump_script_versions( &$scripts ) {
    30                 foreach ( [ 'gp-editor', 'gp-glossary' ] as $handle ) {
    31                         if ( isset( $scripts->registered[ $handle ] ) && '20160329' === $scripts->registered[ $handle ]->ver ) {
    32                                 $scripts->registered[ $handle ]->ver = '20160329a';
    33                         }
    34                 }
    35         }
     15// Store the root plugin file for usage with functions which use the plugin basename.
     16define( __NAMESPACE__ . '\PLUGIN_FILE', __FILE__ );
    3617
    37         /**
    38          * Short circuits WordPress' status handler to prevent unnecessary headers
    39          * which prevent caching.
    40          *
    41          * @return bool True if a request for GlotPress, false if not.
    42          */
    43         public function short_circuit_handle_404() {
    44                 if ( is_glotpress() ) {
    45                         return true;
    46                 }
    47 
    48                 return false;
    49         }
    50 
    51         /**
    52          * Stores the timestamp of the last update for projects.
    53          *
    54          * Used by the Rosetta Roles plugin to invalidate local caches.
    55          */
    56         public function update_projects_last_updated() {
    57                 update_option( 'wporg_projects_last_updated', time() );
    58         }
    59 
    60         /**
    61          * Returns the profile.wordpress.org URL of a user.
    62          *
    63          * @param string $url      Current profile URL.
    64          * @param string $nicename The URL-friendly user name.
    65          * @return string profile.wordpress.org URL
    66          */
    67         public function worg_profile_url( $url, $nicename ) {
    68                 return 'https://profiles.wordpress.org/' . $nicename;
    69         }
    70 
    71         /**
    72          * Natural sorting for sub-projects, and attach whitelisted meta.
    73          *
    74          * @param GP_Project[] $sub_projects List of sub-projects.
    75          * @param int          $parent_id    Parent project ID.
    76          * @return [<description>]GP_Project[] Filtered sub-projects.
    77          */
    78         public function natural_sort_projects( $sub_projects, $parent_id ) {
    79                 if ( in_array( $parent_id, array( 1, 13, 58 ) ) ) { // 1 = WordPress, 13 = BuddyPress, 58 = bbPress
    80                         usort( $sub_projects, function( $a, $b ) {
    81                                 return - strcasecmp( $a->name, $b->name );
    82                         } );
    83                 }
    84 
    85                 if ( in_array( $parent_id, array( 17, 523 ) ) ) { // 17 = Plugins, 523 = Themes
    86                         usort( $sub_projects, function( $a, $b ) {
    87                                 return strcasecmp( $a->name, $b->name );
    88                         } );
    89                 }
    90 
    91                 // Attach wp-themes meta keys
    92                 if ( 523 == $parent_id ) {
    93                         foreach ( $sub_projects as $project ) {
    94                                 $project->non_db_field_names = array_merge( $project->non_db_field_names, array( 'version', 'screenshot' ) );
    95                                 $project->version = gp_get_meta( 'wp-themes', $project->id, 'version' );
    96                                 $project->screenshot = esc_url( gp_get_meta( 'wp-themes', $project->id, 'screenshot' ) );
    97                         }
    98                 }
    99 
    100                 return $sub_projects;
    101         }
     18if ( ! class_exists( '\WordPressdotorg\Autoload\Autoloader', false ) ) {
     19        include __DIR__ . '/vendor/wordpressdotorg/class-autoloader.php';
    10220}
    10321
    104 function wporg_gp_customizations() {
    105         global $wporg_gp_customizations;
     22// Register an Autoloader for all files.
     23Autoload\register_class_path( __NAMESPACE__, __DIR__ . '/inc' );
    10624
    107         if ( ! isset( $wporg_gp_customizations ) ) {
    108                 $wporg_gp_customizations = new WPorg_GP_Customizations();
    109         }
    110 
    111         return $wporg_gp_customizations;
    112 }
    113 add_action( 'plugins_loaded', 'wporg_gp_customizations' );
     25// Instantiate the Plugin.
     26Plugin::get_instance();
Note: See TracChangeset for help on using the changeset viewer.