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


Generated: Fri Nov 27 01:01:59 2020 Cross-referenced by PHPXref 0.7.1