[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/routes/ -> locale.php (source)

   1  <?php
   2  /**
   3   * Routes: GP_Route_Locale class
   4   *
   5   * @package GlotPress
   6   * @subpackage Routes
   7   * @since 1.0.0
   8   */
   9  
  10  /**
  11   * Core class used to implement the locale route.
  12   *
  13   * @since 1.0.0
  14   */
  15  class GP_Route_Locale extends GP_Route_Main {
  16  
  17  	public function locales_get() {
  18          if ( NULL !== gp_get( 'all', NULL ) ) {
  19              $locales = GP_Locales::locales();
  20              usort( $locales, array( $this, 'sort_locales' ) );
  21          }
  22          else {
  23              $existing_locales = GP::$translation_set->existing_locales();
  24              $locales = array();
  25  
  26              foreach ( $existing_locales as $locale ) {
  27                  $locales[] = GP_Locales::by_slug( $locale );
  28              }
  29  
  30              usort( $locales, array( $this, 'sort_locales') );
  31          }
  32  
  33          $this->tmpl( 'locales', get_defined_vars() );
  34      }
  35  
  36  	public function single( $locale_slug, $current_set_slug = 'default' ) {
  37          $locale = GP_Locales::by_slug( $locale_slug );
  38          $sets = GP::$translation_set->by_locale( $locale_slug );
  39  
  40          usort( $sets, array( $this, 'sort_sets_by_project_id' ) );
  41  
  42          $projects_data = $projects = $parents = $set_slugs = $set_list = array();
  43          $locale_projects = wp_list_pluck( $sets, 'project_id' );
  44  
  45          foreach ( $sets as $set ) {
  46              $set_slugs[ $set->slug ] = $set;
  47  
  48              if ( $current_set_slug != $set->slug ) {
  49                  continue;
  50              }
  51  
  52              // Store project data for later use
  53              if ( isset( $projects[ $set->project_id ] ) ) {
  54                  $set_project = $projects[$set->project_id];
  55              } else {
  56                  $set_project = GP::$project->get( $set->project_id );
  57                  $projects[$set->project_id] = $set_project;
  58              }
  59  
  60              // We only want to list active projects
  61              if ( ! isset( $set_project->active ) || $set_project->active == false ) {
  62                  continue;
  63              }
  64  
  65              $parent_id = is_null( $set_project->parent_project_id ) ? $set_project->id : $set_project->parent_project_id;
  66  
  67              // Store parent project data for later use
  68              if ( isset( $projects[$parent_id] ) ) {
  69                  $parent_project = $projects[$parent_id];
  70              } else {
  71                  $parent_project = GP::$project->get( $parent_id );
  72                  $projects[$parent_id] = $parent_project;
  73              }
  74  
  75              // Store parent id for
  76              $parents[$set_project->id] = $parent_id;
  77  
  78              if ( ! in_array( $set_project->parent_project_id, $locale_projects ) ) {
  79                  $projects_data[$parent_id][$set_project->id]['project'] = $set_project;
  80                  $projects_data[$parent_id][$set_project->id]['sets'][$set->id] = $this->set_data( $set, $set_project );
  81                  $projects_data[$parent_id][$set_project->id]['totals'] = $this->set_data( $set, $set_project );
  82  
  83                  if ( ! isset( $projects_data[$parent_id][$set_project->id]['project'] ) ) {
  84                      $projects_data[$parent_id][$set_project->id]['project'] = $set_project;
  85                  }
  86              } else {
  87                  while ( ! in_array( $parent_id, array_keys( $projects_data ) ) && isset( $parents[$parent_id] ) ) {
  88                      $previous_parent = $parent_id;
  89                      $parent_id = $parents[$parent_id];
  90                  }
  91  
  92                  //Orphan project - a sub project is set to active, while it's parent isn't
  93                  if ( ! isset( $projects_data[$parent_id] ) ) {
  94                      continue;
  95                  }
  96  
  97                  //For when root project has sets, and sub projects.
  98                  if ( ! isset( $previous_parent ) || ! isset( $projects_data[$parent_id][$previous_parent] ) ) {
  99                      $previous_parent = $parent_id;
 100                  }
 101  
 102                  $set_data = $projects_data[$parent_id][$previous_parent]['totals'];
 103                  $projects_data[$parent_id][$previous_parent]['sets'][$set->id] = $this->set_data( $set, $set_project  );
 104                  $projects_data[$parent_id][$previous_parent]['totals'] = $this->set_data( $set, $set_project, $set_data );
 105              }
 106          }
 107  
 108          if ( 'default' !== $current_set_slug && ! isset( $set_slugs[ $current_set_slug ] ) ) {
 109              return $this->die_with_404();
 110          }
 111  
 112          if ( ! empty( $set_slugs ) ) {
 113              // Make default the first item.
 114              if ( ! empty( $set_slugs[ 'default' ] ) ) {
 115                  $default = $set_slugs[ 'default' ];
 116                  unset( $set_slugs[ 'default' ] );
 117                  array_unshift( $set_slugs, $default );
 118              }
 119  
 120              foreach ( $set_slugs as $set ) {
 121                  if ( 'default' == $set->slug ) {
 122                      if ( 'default' != $current_set_slug ) {
 123                          $set_list[ $set->slug ] = gp_link_get( gp_url( gp_url_join( '/languages', $locale->slug ) ), __( 'Default', 'glotpress' ) );
 124                      } else {
 125                          $set_list[ $set->slug ] = __( 'Default', 'glotpress' );
 126                      }
 127                  } else {
 128                      if ( $set->slug != $current_set_slug ) {
 129                          $set_list[ $set->slug ] = gp_link_get( gp_url( gp_url_join( '/languages', $locale->slug, $set->slug ) ), esc_html( $set->name ) );
 130                      } else {
 131                          $set_list[ $set->slug ] = esc_html( $set->name );
 132                      }
 133                  }
 134              }
 135          }
 136  
 137          $can_create_locale_glossary = GP::$permission->current_user_can( 'admin' );
 138          $locale_glossary_translation_set = GP::$translation_set->by_project_id_slug_and_locale( 0, $current_set_slug, $locale_slug );
 139          $locale_glossary = GP::$glossary->by_set_id( $locale_glossary_translation_set->id );
 140  
 141          $this->tmpl( 'locale', get_defined_vars() );
 142      }
 143  
 144  	private function set_data( $set, $project, $set_data = null ) {
 145          if ( ! $set_data ) {
 146              $set_data = new stdClass;
 147  
 148              $set_data->slug = $set->slug;
 149              $set_data->project_path = $project->path;
 150              $set_data->waiting_count = $set->waiting_count();
 151              $set_data->current_count = $set->current_count();
 152              $set_data->fuzzy_count   = $set->fuzzy_count();
 153              $set_data->all_count     = $set->all_count();
 154          }
 155          else {
 156              $set_data->waiting_count += $set->waiting_count();
 157              $set_data->current_count += $set->current_count();
 158              $set_data->fuzzy_count   += $set->fuzzy_count();
 159              $set_data->all_count     += $set->all_count();
 160          }
 161  
 162          if ( ! isset( $set_data->name ) ) {
 163              $set_data->name = $project->name;
 164          }
 165  
 166          return $set_data;
 167      }
 168  
 169  	private function sort_locales( $a, $b ) {
 170          return $a->english_name > $b->english_name;
 171      }
 172  
 173  	private function sort_sets_by_project_id( $a, $b ) {
 174          return $a->project_id > $b->project_id;
 175      }
 176  }


Generated: Thu Aug 22 01:02:09 2019 Cross-referenced by PHPXref 0.7.1