Changeset 1684 for sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-routes/routes/locale.php
- Timestamp:
- 06/23/2015 10:33:30 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-routes/routes/locale.php
r1674 r1684 9 9 /** 10 10 * Prints all exisiting locales as cards. 11 *12 11 */ 13 12 public function get_locales() { … … 17 16 $locales[] = GP_Locales::by_slug( $locale ); 18 17 } 19 usort( $locales, array( $this, ' sort_locales') );18 usort( $locales, array( $this, '_sort_english_name_callback') ); 20 19 unset( $existing_locales ); 21 20 … … 34 33 35 34 /** 36 * Prints translation sets of a top level project.35 * Prints projects/translation sets of a top level project. 37 36 * 38 37 * @param string $locale_slug Slug of the locale. 39 * @param string $ current_set_slugSlug 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' ) {38 * @param string $set_slug Slug of the translation set. 39 * @param string $project_path Path of a project. 40 */ 41 public function get_locale_projects( $locale_slug, $set_slug = 'default', $project_path = 'wp' ) { 43 42 $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 ( 'default' !== $current_set_slug && ! isset( $set_slugs[ $current_set_slug ] ) ) { 126 return $this->die_with_404(); 127 } 128 129 if ( $set_slugs ) { 130 // Make default the first item. 131 if ( ! empty( $set_slugs[ 'default' ] ) ) { 132 $default = $set_slugs[ 'default' ]; 133 unset( $set_slugs[ 'default' ] ); 134 array_unshift( $set_slugs, $default ); 135 } 136 137 foreach ( $set_slugs as $set ) { 138 if ( 'default' == $set->slug ) { 139 if ( 'default' != $current_set_slug ) { 140 $set_list[ $set->slug ] = gp_link_get( gp_url_join( '/languages', $locale->slug ), __( 'Default' ) ); 141 } else { 142 $set_list[ $set->slug ] = __( 'Default' ); 143 } 144 } else { 145 if ( $set->slug != $current_set_slug ) { 146 $set_list[ $set->slug ] = gp_link_get( gp_url_join('/languages', $locale->slug, $set->slug ), esc_html( $set->name ) ); 147 } else { 148 $set_list[ $set->slug ] = esc_html( $set->name ); 149 } 150 } 151 } 152 } 43 if ( ! $locale ) { 44 return $this->die_with_404(); 45 } 46 47 $project = GP::$project->by_path( $project_path ); 48 if ( ! $project ) { 49 return $this->die_with_404(); 50 } 51 52 $sub_projects = $this->get_active_sub_projects( $project ); 53 if ( ! $sub_projects ) { 54 return $this->die_with_404(); 55 } 56 57 usort( $sub_projects, array( $this, '_sort_name_callback' ) ); 58 59 $project_status = $project_icons = array(); 60 foreach ( $sub_projects as $key => $sub_project ) { 61 $status = $this->get_project_status( $sub_project, $locale_slug, $set_slug ); 62 if ( ! $status->sub_projects_count ) { 63 unset( $sub_projects[ $key ] ); 64 } 65 66 $project_status[ $sub_project->id ] = $status; 67 $project_icons[ $sub_project->id ] = $this->get_project_icon( $project, $sub_project ); 68 } 69 70 $contributors_count = wp_cache_get( 'contributors-count', 'wporg-translate' ); 71 if ( false === $contributors_count ) { 72 $contributors_count = array(); 73 } 74 75 $top_level_projects = GP::$project->top_level(); 76 usort( $top_level_projects, array( $this, '_sort_reverse_name_callback' ) ); 77 78 $variants = $this->get_locale_variants( $locale_slug, array_keys( $project_status ) ); 153 79 154 80 $this->tmpl( 'locale', get_defined_vars() ); 155 81 } 156 82 157 private function set_data( $set, $project, $set_data = null ) { 158 if ( ! $set_data ) { 159 $set_data = new stdClass; 160 161 $set_data->slug = $set->slug; 162 $set_data->project_path = $project->path; 163 $set_data->waiting_count = $set->waiting_count(); 164 $set_data->current_count = $set->current_count(); 165 $set_data->fuzzy_count = $set->fuzzy_count(); 166 $set_data->all_count = $set->all_count(); 167 } 168 else { 169 $set_data->waiting_count += $set->waiting_count(); 170 $set_data->current_count += $set->current_count(); 171 $set_data->fuzzy_count += $set->fuzzy_count(); 172 $set_data->all_count += $set->all_count(); 173 } 174 175 if ( ! isset( $set_data->name ) ) { 176 $set_data->name = $project->name; 177 } 178 179 return $set_data; 180 } 181 182 private function sort_locales( $a, $b ) { 83 /** 84 * Prints projects/translation sets of a sub project. 85 * 86 * @param string $locale_slug Slug of the locale. 87 * @param string $set_slug Slug of the translation set. 88 * @param string $project_path Path of a project. 89 * @param string $sub_project_path Path of a sub project. 90 */ 91 public function get_locale_project( $locale_slug, $set_slug, $project_path, $sub_project_path ) { 92 $locale = GP_Locales::by_slug( $locale_slug ); 93 if ( ! $locale ) { 94 return $this->die_with_404(); 95 } 96 97 $project = GP::$project->by_path( $project_path ); 98 if ( ! $project ) { 99 return $this->die_with_404(); 100 } 101 102 $sub_project = GP::$project->by_path( $project_path . '/' . $sub_project_path ); 103 if ( ! $sub_project ) { 104 return $this->die_with_404(); 105 } 106 107 $project_status = $this->get_project_status( $sub_project, $locale_slug, $set_slug ); 108 $sub_project_status = $this->get_project_status( $sub_project, $locale_slug, $set_slug, null, false ); 109 110 $project_icon = $this->get_project_icon( $project, $sub_project, 64 ); 111 112 $contributors_count = wp_cache_get( 'contributors-count', 'wporg-translate' ); 113 if ( false === $contributors_count ) { 114 $contributors_count = array(); 115 } 116 117 $sub_projects = $this->get_active_sub_projects( $sub_project, true ); 118 if ( $sub_projects ) { 119 $sub_project_statuses = array(); 120 foreach ( $sub_projects as $key => $_sub_project ) { 121 $status = $this->get_project_status( $_sub_project, $locale_slug, $set_slug ); 122 if ( ! $status->all_count ) { 123 unset( $sub_projects[ $key ] ); 124 } 125 126 $sub_project_statuses[ $_sub_project->id ] = $status; 127 } 128 129 $variants = $this->get_locale_variants( $locale_slug, array_keys( $sub_project_statuses ) ); 130 } else { 131 $variants = $this->get_locale_variants( $locale_slug, array( $sub_project->id ) ); 132 } 133 134 $this->tmpl( 'locale-project', get_defined_vars() ); 135 } 136 137 /** 138 * Returns markup for project icons. 139 * 140 * @param GP_Project $project A GlotPress project. 141 * @param GP_Project $sub_project A sub project of a GlotPress project. 142 * @param int $size Size of icon. 143 * @return string HTML markup of an icon. 144 */ 145 private function get_project_icon( $project, $sub_project, $size = 128 ) { 146 switch( $project->slug ) { 147 case 'wp': 148 return '<div class="wordpress-icon"><span class="dashicons dashicons-wordpress-alt"></span></div>'; 149 case 'bbpress': 150 case 'buddypress': 151 require_once WPORGPATH . 'extend/plugins-plugins/_plugin-icons.php'; 152 if ( function_exists( 'wporg_get_plugin_icon' ) ) { 153 return wporg_get_plugin_icon( $project->slug, $size ); 154 } 155 case 'wp-plugins': 156 require_once WPORGPATH . 'extend/plugins-plugins/_plugin-icons.php'; 157 if ( function_exists( 'wporg_get_plugin_icon' ) ) { 158 return wporg_get_plugin_icon( $sub_project->slug, $size ); 159 } 160 default: 161 return '<div class="default-icon"><span class="dashicons dashicons-translation"></span></div>'; 162 } 163 } 164 165 /** 166 * Retrieves non-default slugs of translation sets for a list of 167 * project IDs. 168 * 169 * @param string $locale Slug of a GlotPress locale. 170 * @param array $project_ids List of project IDs. 171 * @return array List of non-default slugs. 172 */ 173 private function get_locale_variants( $locale, $project_ids ) { 174 global $gpdb; 175 176 $table = GP::$translation_set->table; 177 $project_ids = implode( ',', $project_ids ); 178 $slugs = $gpdb->get_col( $gpdb->prepare( " 179 SELECT DISTINCT(slug), name 180 FROM $table 181 WHERE 182 project_id IN( $project_ids ) 183 AND locale = %s 184 ", $locale ) ); 185 186 return $slugs; 187 } 188 189 /** 190 * Calculates the status of a project. 191 * 192 * @param GP_Project $project The GlotPress project. 193 * @param string $locale Slug of GlotPress locale. 194 * @param string $set_slug Slug of the translation set. 195 * @param object $status The status object. 196 * @param bool $calc_sub_projects Whether sub projects should be calculated too. 197 * Default true. 198 * @return object The status of a project. 199 */ 200 private function get_project_status( $project, $locale, $set_slug, $status = null, $calc_sub_projects = true ) { 201 if ( null === $status ) { 202 $status = new stdClass; 203 $status->sub_projects_count = 0; 204 $status->waiting_count = 0; 205 $status->current_count = 0; 206 $status->fuzzy_count = 0; 207 $status->all_count = 0; 208 } 209 210 $set = GP::$translation_set->by_project_id_slug_and_locale( 211 $project->id, 212 $set_slug, 213 $locale 214 ); 215 216 if ( $set ) { 217 $status->sub_projects_count += 1; 218 $status->waiting_count += (int) $set->waiting_count(); 219 $status->current_count += (int) $set->current_count(); 220 $status->fuzzy_count += (int) $set->fuzzy_count(); 221 $status->all_count += (int) $set->all_count(); 222 } 223 224 if ( $calc_sub_projects ) { 225 $sub_projects = $this->get_active_sub_projects( $project, true ); 226 if ( $sub_projects ) { 227 foreach ( $sub_projects as $sub_project ) { 228 $this->get_project_status( $sub_project, $locale, $set_slug, $status ); 229 } 230 } 231 } 232 233 return $status; 234 } 235 236 /** 237 * Retrieves active sub projects. 238 * 239 * @param GP_Project $project The parent project 240 * @param bool $with_sub_projects Whether sub projects should be fetched too. 241 * Default false. 242 * @return array List of sub projects. 243 */ 244 private function get_active_sub_projects( $project, $with_sub_projects = false ) { 245 $_projects = $project->many( " 246 SELECT * 247 FROM $project->table 248 WHERE 249 parent_project_id = %d AND 250 active = 1 251 ORDER BY id ASC 252 ", $project->id ); 253 254 $projects = array(); 255 foreach ( $_projects as $project ) { 256 $projects[ $project->id ] = $project; 257 258 if ( $with_sub_projects ) { 259 // e.g. wp/dev/admin/network 260 $sub_projects = $project->many( " 261 SELECT * 262 FROM $project->table 263 WHERE 264 parent_project_id = %d AND 265 active = 1 266 ORDER BY id ASC 267 ", $project->id ); 268 269 foreach ( $sub_projects as $sub_project ) { 270 $projects[ $sub_project->id ] = $sub_project; 271 } 272 unset( $sub_projects); 273 } 274 } 275 unset( $_projects ); 276 277 return $projects; 278 } 279 280 private function _sort_reverse_name_callback( $a, $b ) { 281 return - strcasecmp( $a->name, $b->name ); 282 } 283 284 private function _sort_name_callback( $a, $b ) { 285 return strcasecmp( $a->name, $b->name ); 286 } 287 288 private function _sort_english_name_callback( $a, $b ) { 183 289 return $a->english_name > $b->english_name; 184 290 } 185 186 private function sort_sets_by_project_id( $a, $b ) {187 return $a->project_id > $b->project_id;188 }189 291 }
Note: See TracChangeset
for help on using the changeset viewer.