Changeset 1586
- Timestamp:
- 05/15/2015 07:19:57 PM (9 years ago)
- Location:
- sites/trunk/translate.wordpress.org
- Files:
-
- 2 added
- 1 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-routes/routes/index.php
r1580 r1586 1 1 <?php 2 2 /** 3 * This plugins overrides some of the default routes of GlotPress.3 * Index Route Class. 4 4 * 5 * @author ocean905 * Provides the route for translate.wordpress.org/. 6 6 */ 7 class GP_WPorg_Routes extends GP_Plugin {8 public $id = 'wporg-routes';9 10 public function __construct() {11 parent::__construct();12 $this->add_action( 'init' );13 }14 15 public function init() {16 /*17 * Unset default routes.18 * The `routes` filter can't be used, see https://glotpress.trac.wordpress.org/ticket/249.19 */20 unset( GP::$router->urls['/'] );21 unset( GP::$router->urls['get:/languages'] );22 23 GP::$router->add( '/', array( 'GP_WPorg_Route_Index', 'index' ) );24 GP::$router->add( '/languages', array( 'GP_WPorg_Route_Locale', 'locales_get' ) );25 }26 }27 28 class GP_WPorg_Route_Locale extends GP_Route {29 30 public function locales_get() {31 $locales = array();32 $existing_locales = GP::$translation_set->existing_locales();33 foreach ( $existing_locales as $locale ) {34 $locales[] = GP_Locales::by_slug( $locale );35 }36 usort( $locales, array( $this, 'sort_locales') );37 unset( $existing_locales );38 39 $contributors_count = wp_cache_get( 'contributors-count', 'wporg-translate' );40 if ( false === $contributors_count ) {41 $contributors_count = array();42 }43 44 $translation_status = wp_cache_get( 'translation-status', 'wporg-translate' );45 if ( false === $translation_status ) {46 $translation_status = array();47 }48 49 $this->tmpl( 'locales', get_defined_vars() );50 }51 52 private function sort_locales( $a, $b ) {53 return $a->english_name > $b->english_name;54 }55 }56 57 7 class GP_WPorg_Route_Index extends GP_Route { 58 8 59 public function index() {9 public function get_index() { 60 10 $this->redirect( gp_url( '/languages' ) ); 61 11 } 62 12 } 63 64 GP::$plugins->wporg_routes = new GP_WPorg_Routes; -
sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-routes/routes/locale.php
r1580 r1586 1 1 <?php 2 2 /** 3 * This plugins overrides some of the default routes of GlotPress.3 * Locale Route Class. 4 4 * 5 * @author ocean905 * Provides the route for translate.wordpress.org/languages. 6 6 */ 7 class GP_WPorg_Routes extends GP_Plugin {8 public $id = 'wporg-routes';9 10 public function __construct() {11 parent::__construct();12 $this->add_action( 'init' );13 }14 15 public function init() {16 /*17 * Unset default routes.18 * The `routes` filter can't be used, see https://glotpress.trac.wordpress.org/ticket/249.19 */20 unset( GP::$router->urls['/'] );21 unset( GP::$router->urls['get:/languages'] );22 23 GP::$router->add( '/', array( 'GP_WPorg_Route_Index', 'index' ) );24 GP::$router->add( '/languages', array( 'GP_WPorg_Route_Locale', 'locales_get' ) );25 }26 }27 28 7 class GP_WPorg_Route_Locale extends GP_Route { 29 8 30 public function locales_get() { 9 /** 10 * Prints all exisiting locales as cards. 11 * 12 */ 13 public function get_locales() { 31 14 $locales = array(); 32 15 $existing_locales = GP::$translation_set->existing_locales(); … … 50 33 } 51 34 35 /** 36 * Prints translation sets of a top level project. 37 * 38 * @param string $locale_slug Slug of the locale. 39 * @param string $current_set_slug Slug of the translation set. 40 * @param string $project_path Path of a project 41 */ 42 public function get_locale_projects( $locale_slug, $current_set_slug = 'default', $project_path = 'wp' ) { 43 $locale = GP_Locales::by_slug( $locale_slug ); 44 $sets = GP::$translation_set->by_locale( $locale_slug ); 45 usort( $sets, array( $this, 'sort_sets_by_project_id' ) ); 46 47 /*if ( $project_path ) { 48 $project = GP::$project->by_path( $project_path ); 49 50 if ( ! $project ) { 51 return $this->die_with_404(); 52 } 53 }*/ 54 55 $locale_projects = $projects_data = $projects = $parents = $set_slugs = $set_list = array(); 56 57 //TODO: switch to wp_list_pluck 58 foreach ( $sets as $key => $value ) { 59 $locale_projects[ $key ] = $value->project_id; 60 } 61 62 foreach ( $sets as $set ) { 63 $set_slugs[ $set->slug ] = $set; 64 65 if ( $current_set_slug != $set->slug ) { 66 continue; 67 } 68 69 // Store project data for later use 70 if ( isset( $projects[ $set->project_id ] ) ) { 71 $set_project = $projects[$set->project_id]; 72 } else { 73 $set_project = GP::$project->get( $set->project_id ); 74 $projects[$set->project_id] = $set_project; 75 } 76 77 // We only want to list active projects 78 if ( ! isset( $set_project->active ) || $set_project->active == false ) { 79 continue; 80 } 81 82 $parent_id = is_null( $set_project->parent_project_id ) ? $set_project->id : $set_project->parent_project_id; 83 84 // Store parent project data for later use 85 if ( isset( $projects[$parent_id] ) ) { 86 $parent_project = $projects[$parent_id]; 87 } else { 88 $parent_project = GP::$project->get( $parent_id ); 89 $projects[$parent_id] = $parent_project; 90 } 91 92 // Store parent id for 93 $parents[$set_project->id] = $parent_id; 94 95 if ( ! in_array( $set_project->parent_project_id, $locale_projects ) ) { 96 $projects_data[$parent_id][$set_project->id]['project'] = $set_project; 97 $projects_data[$parent_id][$set_project->id]['sets'][$set->id] = $this->set_data( $set, $set_project ); 98 $projects_data[$parent_id][$set_project->id]['totals'] = $this->set_data( $set, $set_project ); 99 100 if ( ! isset( $projects_data[$parent_id][$set_project->id]['project'] ) ) { 101 $projects_data[$parent_id][$set_project->id]['project'] = $set_project; 102 } 103 } else { 104 while ( ! in_array( $parent_id, array_keys( $projects_data ) ) && isset( $parents[$parent_id] ) ) { 105 $previous_parent = $parent_id; 106 $parent_id = $parents[$parent_id]; 107 } 108 109 //Orphan project - a sub project is set to active, while it's parent isn't 110 if ( ! isset( $projects_data[$parent_id] ) ) { 111 continue; 112 } 113 114 //For when root project has sets, and sub projects. 115 if ( ! isset( $previous_parent ) || ! isset( $projects_data[$parent_id][$previous_parent] ) ) { 116 $previous_parent = $parent_id; 117 } 118 119 $set_data = $projects_data[$parent_id][$previous_parent]['totals']; 120 $projects_data[$parent_id][$previous_parent]['sets'][$set->id] = $this->set_data( $set, $set_project ); 121 $projects_data[$parent_id][$previous_parent]['totals'] = $this->set_data( $set, $set_project, $set_data ); 122 } 123 } 124 125 if ( $set_slugs ) { 126 // Make default the first item. 127 if ( ! empty( $set_slugs[ 'default' ] ) ) { 128 $default = $set_slugs[ 'default' ]; 129 unset( $set_slugs[ 'default' ] ); 130 array_unshift( $set_slugs, $default ); 131 } 132 133 foreach ( $set_slugs as $set ) { 134 if ( 'default' == $set->slug ) { 135 if ( 'default' != $current_set_slug ) { 136 $set_list[ $set->slug ] = gp_link_get( gp_url_join( '/languages', $locale->slug ), __( 'Default' ) ); 137 } else { 138 $set_list[ $set->slug ] = __( 'Default' ); 139 } 140 } else { 141 if ( $set->slug != $current_set_slug ) { 142 $set_list[ $set->slug ] = gp_link_get( gp_url_join('/languages', $locale->slug, $set->slug ), esc_html( $set->name ) ); 143 } else { 144 $set_list[ $set->slug ] = esc_html( $set->name ); 145 } 146 } 147 } 148 } 149 150 $this->tmpl( 'locale', get_defined_vars() ); 151 } 152 153 private function set_data( $set, $project, $set_data = null ) { 154 if ( ! $set_data ) { 155 $set_data = new stdClass; 156 157 $set_data->slug = $set->slug; 158 $set_data->project_path = $project->path; 159 $set_data->waiting_count = $set->waiting_count(); 160 $set_data->current_count = $set->current_count(); 161 $set_data->fuzzy_count = $set->fuzzy_count(); 162 $set_data->all_count = $set->all_count(); 163 } 164 else { 165 $set_data->waiting_count += $set->waiting_count(); 166 $set_data->current_count += $set->current_count(); 167 $set_data->fuzzy_count += $set->fuzzy_count(); 168 $set_data->all_count += $set->all_count(); 169 } 170 171 if ( ! isset( $set_data->name ) ) { 172 $set_data->name = $project->name; 173 } 174 175 return $set_data; 176 } 177 52 178 private function sort_locales( $a, $b ) { 53 179 return $a->english_name > $b->english_name; 54 180 } 55 }56 181 57 class GP_WPorg_Route_Index extends GP_Route { 58 59 public function index() { 60 $this->redirect( gp_url( '/languages' ) ); 182 private function sort_sets_by_project_id( $a, $b ) { 183 return $a->project_id > $b->project_id; 61 184 } 62 185 } 63 64 GP::$plugins->wporg_routes = new GP_WPorg_Routes; -
sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-routes/wporg-routes.php
r1530 r1586 5 5 * @author ocean90 6 6 */ 7 8 require_once __DIR__ . '/routes/index.php'; 9 require_once __DIR__ . '/routes/locale.php'; 10 7 11 class GP_WPorg_Routes extends GP_Plugin { 8 12 public $id = 'wporg-routes'; … … 14 18 15 19 public function init() { 20 $locale = '(' . implode( '|', array_map( function( $locale ) { return $locale->slug; }, GP_Locales::locales() ) ) . ')'; 21 $set_slug = '(' . implode( '|', GP::$translation_set->existing_slugs() ) . ')'; 22 16 23 /* 17 24 * Unset default routes. … … 19 26 */ 20 27 unset( GP::$router->urls['/'] ); 28 unset( GP::$router->urls["get:/languages/$locale/$set_slug"] ); 29 unset( GP::$router->urls["get:/languages/$locale"] ); 21 30 unset( GP::$router->urls['get:/languages'] ); 22 31 23 GP::$router->add( '/', array( 'GP_WPorg_Route_Index', 'index' ) ); 24 GP::$router->add( '/languages', array( 'GP_WPorg_Route_Locale', 'locales_get' ) ); 25 } 26 } 27 28 class GP_WPorg_Route_Locale extends GP_Route { 29 30 public function locales_get() { 31 $locales = array(); 32 $existing_locales = GP::$translation_set->existing_locales(); 33 foreach ( $existing_locales as $locale ) { 34 $locales[] = GP_Locales::by_slug( $locale ); 35 } 36 usort( $locales, array( $this, 'sort_locales') ); 37 unset( $existing_locales ); 38 39 $contributors_count = wp_cache_get( 'contributors-count', 'wporg-translate' ); 40 if ( false === $contributors_count ) { 41 $contributors_count = array(); 42 } 43 44 $translation_status = wp_cache_get( 'translation-status', 'wporg-translate' ); 45 if ( false === $translation_status ) { 46 $translation_status = array(); 47 } 48 49 $this->tmpl( 'locales', get_defined_vars() ); 50 } 51 52 private function sort_locales( $a, $b ) { 53 return $a->english_name > $b->english_name; 54 } 55 } 56 57 class GP_WPorg_Route_Index extends GP_Route { 58 59 public function index() { 60 $this->redirect( gp_url( '/languages' ) ); 32 GP::$router->add( '/', array( 'GP_WPorg_Route_Index', 'get_index' ) ); 33 GP::$router->add( '/languages', array( 'GP_WPorg_Route_Locale', 'get_locales' ) ); 34 GP::$router->add( "/languages/$locale/$set_slug/(.+?)", array( 'GP_WPorg_Route_Locale', 'get_locale_projects' ) ); 35 GP::$router->add( "/languages/$locale/$set_slug", array( 'GP_WPorg_Route_Locale', 'get_locale_projects' ) ); 36 GP::$router->add( "/languages/$locale", array( 'GP_WPorg_Route_Locale', 'get_locale_projects' ) ); 61 37 } 62 38 }
Note: See TracChangeset
for help on using the changeset viewer.