[ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
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 78 $set_ids = array(); 79 $translation_sets = array(); 80 81 $i = 0; 82 foreach ( $translations as $translation ) { 83 if ( in_array( $translation->translation_set_id, $set_ids, true ) ) { 84 continue; 85 } 86 87 $set_ids[] = $translation->translation_set_id; 88 89 $set = GP::$translation_set->find_one( array( 'id' => (int) $translation->translation_set_id ) ); 90 91 if ( $set ) { 92 $translation_set = $this->get_translation_set( $set ); 93 94 if ( ! $translation_set ) { 95 continue; 96 } 97 98 $translation_set->set_id = $set->id; 99 $translation_set->last_updated = $translation->date_added; 100 $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 ) ); 101 102 $translation_sets[] = $translation_set; 103 104 $i++; 105 106 // Bail early if we have already the amount requested 107 if ( $i >= $amount ) { 108 break; 109 } 110 } 111 } 112 113 return $translation_sets; 114 } 115 116 /** 117 * Gets an array of locales the user has contributed to. 118 * 119 * @param WP_User $user A user object. 120 * 121 * @return array $locales An array of locale objects 122 */ 123 private function locales_known( $user ) { 124 global $wpdb; 125 126 $translations = GP::$translation_set->many_no_map( 127 "SELECT ts.locale, count(*) AS count 128 FROM $wpdb->gp_translations as t 129 INNER JOIN $wpdb->gp_translation_sets AS ts ON ts.id = t.translation_set_id 130 WHERE user_id = %s 131 GROUP BY ts.locale 132 ORDER BY count DESC", 133 $user->ID 134 ); 135 136 $locales = array(); 137 138 foreach ( $translations as $data ) { 139 $locale = GP_Locales::by_slug( $data->locale ); 140 141 $locales[ $locale->english_name ] = array( 142 'locale' => $data->locale, 143 'count' => (int) $data->count, 144 ); 145 } 146 147 return $locales; 148 } 149 150 /** 151 * Retrieve a users permissions. 152 * 153 * @param WP_User $user The user object. 154 * 155 * @return array Array of permissions 156 */ 157 private function get_permissions( $user ) { 158 $permissions = GP::$permission->find_many_no_map( 159 array( 160 'user_id' => $user->ID, 161 'action' => 'approve', 162 ) 163 ); 164 165 foreach ( $permissions as $key => &$permission ) { 166 $object_id = GP::$validator_permission->project_id_locale_slug_set_slug( $permission->object_id ); 167 168 // Skip admin permissions 169 if ( ! isset( $object_id[1] ) ) { 170 unset( $permissions[ $key ] ); 171 continue; 172 } 173 174 $set = GP::$translation_set->find_one( 175 array( 176 'project_id' => $object_id[0], 177 'locale' => $object_id[1], 178 'slug' => $object_id[2], 179 ) 180 ); 181 182 // Skip permissions for non existing sets 183 if ( ! $set ) { 184 unset( $permissions[ $key ] ); 185 continue; 186 } 187 188 unset( $permission->id, $permission->action, $permission->object_type, $permission->object_id ); 189 190 $translation_set = $this->get_translation_set( $set ); 191 192 if ( $set && $translation_set ) { 193 $permission = (object) array_merge( (array) $permission, (array) $translation_set ); 194 $permission->set_id = $set->id; 195 } else { 196 unset( $permissions[ $key ] ); 197 } 198 } 199 200 return $permissions; 201 } 202 203 /** 204 * Gets an object representing the project id, project url and translation set name of the supplied set. 205 * 206 * @param GP_Translation_set $set A translation set object. 207 * 208 * @return object 209 */ 210 private function get_translation_set( $set ) { 211 if ( ! isset( $this->projects[ $set->project_id ] ) ) { 212 $this->projects[ $set->project_id ] = GP::$project->get( $set->project_id ); 213 } 214 215 $project = $this->projects[ $set->project_id ]; 216 217 if ( ! $project ) { 218 return false; 219 } 220 221 $project_url = gp_url_project( $project, gp_url_join( $set->locale, $set->slug ) ); 222 $set_name = gp_project_names_from_root( $project ) . ' | ' . $set->name_with_locale(); 223 224 return (object) array( 225 'project_id' => $project->id, 226 'project_url' => $project_url, 227 'set_name' => $set_name, 228 ); 229 } 230 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:01:07 2024 | Cross-referenced by PHPXref 0.7.1 |