[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Routes: GP_Route_Profile class
   4   *
   5   * @package GlotPress
   6   * @subpackage Routes
   7   * @since 1.0.0
   8   */
   9  
  10  /**
  11   * Core class used to implement the profile route.
  12   *
  13   * @since 1.0.0
  14   */
  15  class GP_Route_Profile extends GP_Route_Main {
  16      // For caching purposes
  17      private $projects = array();
  18  
  19      /**
  20       * Displays the profile page, requires a user to be logged in.
  21       */
  22  	public function profile_get() {
  23          if ( ! is_user_logged_in() ) {
  24              $this->redirect( wp_login_url( gp_url_profile() ) );
  25              return;
  26          }
  27  
  28          $this->tmpl( 'profile' );
  29      }
  30  
  31      /**
  32       * Displays the profile page for a given user.
  33       *
  34       * @param string $user A user nicename.
  35       */
  36  	public function profile_view( $user = '' ) {
  37          if ( '' == $user ) { // WPCS: loose comparison ok.
  38              $user = wp_get_current_user();
  39          } else {
  40              $user = get_user_by( 'slug', $user );
  41          }
  42  
  43          if ( ! $user || ( is_object( $user ) && $user instanceof WP_User && ! $user->exists() ) ) {
  44              return $this->die_with_404();
  45          }
  46  
  47          $recent_projects = $this->get_recent_translation_sets( $user, 5 );
  48          $locales         = $this->locales_known( $user );
  49  
  50          //validate to
  51          $permissions = $this->get_permissions( $user );
  52  
  53          $this->tmpl( 'profile-public', get_defined_vars() );
  54      }
  55  
  56      /**
  57       * Gets an array of recent translation sets the user has worked on.
  58       *
  59       * @param WP_User $user   A user object.
  60       * @param int     $amount Number of recent translations to return.
  61       *
  62       * @return array $translation_sets An array of translation set objects
  63       */
  64  	private function get_recent_translation_sets( $user, $amount = 5 ) {
  65          global $wpdb;
  66  
  67          $translations = GP::$translation_set->many_no_map("
  68              SELECT translation_set_id, date_added
  69              FROM $wpdb->gp_translations as t
  70              WHERE
  71                  date_added >= DATE_SUB(NOW(), INTERVAL 2 MONTH) AND
  72                  user_id = %s AND
  73                  status != 'rejected'
  74              ORDER BY date_added DESC
  75          ", $user->ID );
  76  
  77          $set_ids          = array();
  78          $translation_sets = array();
  79  
  80          $i = 0;
  81          foreach ( $translations as $translation ) {
  82              if ( in_array( $translation->translation_set_id, $set_ids ) ) {
  83                  continue;
  84              }
  85  
  86              $set_ids[] = $translation->translation_set_id;
  87  
  88              $set = GP::$translation_set->find_one( array( 'id' => (int) $translation->translation_set_id ) );
  89  
  90              if ( $set ) {
  91                  $translation_set = $this->get_translation_set( $set );
  92  
  93                  if ( ! $translation_set ) {
  94                      continue;
  95                  }
  96  
  97                  $translation_set->set_id       = $set->id;
  98                  $translation_set->last_updated = $translation->date_added;
  99                  $translation_set->count        = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->gp_translations WHERE user_id = %s AND status != 'rejected' AND translation_set_id = %s", $user->ID, $translation->translation_set_id ) );
 100  
 101                  $translation_sets[] = $translation_set;
 102  
 103                  $i++;
 104  
 105                  // Bail early if we have already the amount requested
 106                  if ( $i >= $amount ) {
 107                      break;
 108                  }
 109              }
 110          }
 111  
 112          return $translation_sets;
 113      }
 114  
 115      /**
 116       * Gets an array of locales the user has contributed to.
 117       *
 118       * @param WP_User $user   A user object.
 119       *
 120       * @return array $locales An array of locale objects
 121       */
 122  	private function locales_known( $user ) {
 123          global $wpdb;
 124  
 125          $translations = GP::$translation_set->many_no_map("
 126              SELECT ts.locale, count(*) AS count
 127              FROM $wpdb->gp_translations as t
 128              INNER JOIN $wpdb->gp_translation_sets AS ts ON ts.id = t.translation_set_id
 129              WHERE user_id = %s
 130              GROUP BY ts.locale
 131              ORDER BY count DESC
 132          ", $user->ID );
 133  
 134          $locales = array();
 135  
 136          foreach ( $translations as $data ) {
 137              $locale = GP_Locales::by_slug( $data->locale );
 138  
 139              $locales[ $locale->english_name ] = array(
 140                  'locale' => $data->locale,
 141                  'count'  => (int) $data->count,
 142              );
 143          }
 144  
 145          return $locales;
 146      }
 147  
 148      /**
 149       * Retrieve a users permissions.
 150       *
 151       * @param WP_User $user The user object.
 152       *
 153       * @return array Array of permissions
 154       */
 155  	private function get_permissions( $user ) {
 156          $permissions = GP::$permission->find_many_no_map( array( 'user_id' => $user->ID, 'action' => 'approve' ) );
 157  
 158          foreach ( $permissions as $key => &$permission ) {
 159              $object_id = GP::$validator_permission->project_id_locale_slug_set_slug( $permission->object_id );
 160  
 161              // Skip admin permissions
 162              if ( ! isset(  $object_id[1] ) ) {
 163                  unset( $permissions[$key] );
 164                  continue;
 165              }
 166  
 167              $set = GP::$translation_set->find_one(
 168                  array(
 169                      'project_id' => $object_id[0],
 170                      'locale' => $object_id[1],
 171                      'slug' => $object_id[2]
 172                  )
 173              );
 174  
 175              // Skip permissions for non existing sets
 176              if ( ! $set ) {
 177                  unset( $permissions[$key] );
 178                  continue;
 179              }
 180  
 181              unset( $permission->id, $permission->action, $permission->object_type, $permission->object_id );
 182  
 183              $translation_set = $this->get_translation_set( $set );
 184  
 185              if ( $set && $translation_set ) {
 186                  $permission = (object) array_merge( (array) $permission, (array) $translation_set );
 187                  $permission->set_id = $set->id;
 188              } else {
 189                  unset( $permissions[$key] );
 190              }
 191          }
 192  
 193          return $permissions;
 194      }
 195  
 196      /**
 197       * Gets an object representing the project id, project url and translation set name of the supplied set.
 198       *
 199       * @param GP_Translation_set $set   A translation set object.
 200       *
 201       * @return object
 202       */
 203  	private function get_translation_set( $set ) {
 204          if ( ! isset( $this->projects[ $set->project_id ] ) ) {
 205               $this->projects[ $set->project_id ] = GP::$project->get( $set->project_id );
 206          }
 207  
 208          $project = $this->projects[$set->project_id];
 209  
 210          if ( ! $project ) {
 211              return false;
 212          }
 213  
 214          $project_url = gp_url_project( $project, gp_url_join( $set->locale, $set->slug ) );
 215          $set_name = gp_project_names_from_root( $project ) . ' | ' . $set->name_with_locale();
 216  
 217          return (object) array(
 218              'project_id' => $project->id,
 219              'project_url' => $project_url,
 220              'set_name' => $set_name
 221          );
 222      }
 223  }


Generated: Mon Nov 18 01:01:56 2019 Cross-referenced by PHPXref 0.7.1