Changeset 2262
- Timestamp:
- 01/10/2016 12:16:28 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-rosetta-roles/wporg-gp-rosetta-roles.php
r2110 r2262 29 29 30 30 /** 31 * Holds the meta key of the project access list. 32 * 33 * @var string 34 */ 35 public $project_access_meta_key = 'translation_editor_project_access_list'; 31 * Database table for translation editors. 32 */ 33 const TRANSLATION_EDITORS_TABLE = 'translate_translation_editors'; 34 35 /** 36 * Role of a per project translation editor. 37 */ 38 const TRANSLATION_EDITOR_ROLE = 'translation_editor'; 39 40 /** 41 * Role of a general translation editor. 42 */ 43 const GENERAL_TRANSLATION_EDITOR_ROLE = 'general_translation_editor'; 36 44 37 45 /** … … 39 47 */ 40 48 public function __construct() { 49 $GLOBALS['wpdb']->wporg_translation_editors = self::TRANSLATION_EDITORS_TABLE; 50 41 51 add_filter( 'gp_pre_can_user', array( $this, 'pre_can_user' ), 9 , 2 ); 42 52 add_action( 'gp_project_created', array( $this, 'project_created' ) ); … … 144 154 */ 145 155 public function is_approver_for_locale( $user_id, $locale_slug ) { 156 static $cache = null; 157 158 if ( null === $cache ) { 159 $cache = array(); 160 } 161 162 if ( isset( $cache[ $user_id ][ $locale_slug ] ) ) { 163 return $cache[ $user_id ][ $locale_slug ]; 164 } 165 166 if ( ! isset( $cache[ $user_id ] ) ) { 167 $cache[ $user_id ] = array(); 168 } 169 146 170 // Get blog prefix of the associated Rosetta site. 147 171 if ( ! $blog_prefix = $this->get_blog_prefix( $locale_slug ) ) { 172 $cache[ $user_id ][ $locale_slug ] = false; 148 173 return false; 149 174 } … … 151 176 $user = get_user_by( 'id', $user_id ); 152 177 153 // Check if current user has the approver role. 154 $user->cap_key = $blog_prefix . 'capabilities'; 155 $user->caps = &$user->{$user->cap_key}; 156 if ( ! is_array( $user->caps ) ) { 157 $user->caps = array(); 158 } 159 $user->get_role_caps(); 160 161 return $user->has_cap( $this->approver_role ); 178 $cap_key = $blog_prefix . 'capabilities'; 179 if ( ! isset( $user->{$cap_key} ) ) { 180 $cache[ $user_id ][ $locale_slug ] = false; 181 return false; 182 } 183 184 $is_approver = in_array( self::TRANSLATION_EDITOR_ROLE, $user->$cap_key ) || in_array( self::GENERAL_TRANSLATION_EDITOR_ROLE, $user->$cap_key ); 185 $cache[ $user_id ][ $locale_slug ] = $is_approver; 186 187 return $is_approver; 162 188 } 163 189 … … 175 201 */ 176 202 public function get_project_id_access_list( $user_id, $locale_slug, $include_children = false ) { 177 $user = get_user_by( 'id', $user_id ); 178 179 // Get blog prefix of the associated Rosetta site. 180 if ( ! $blog_prefix = $this->get_blog_prefix( $locale_slug ) ) { 181 return false; 182 } 183 184 // Get IDs of projects which the user can approve. 185 $meta_key = $blog_prefix . $this->project_access_meta_key; 186 if ( empty( $user->$meta_key ) || ! is_array( $user->$meta_key ) ) { 187 return false; 188 } 189 190 $project_access_list = $user->$meta_key; 203 global $wpdb; 204 static $cache = null; 205 206 if ( null === $cache ) { 207 $cache = array(); 208 } 209 210 if ( isset( $cache[ $user_id ][ $locale_slug ] ) ) { 211 $project_access_list = $cache[ $user_id ][ $locale_slug ]; 212 } else { 213 $project_access_list = $wpdb->get_col( $wpdb->prepare( " 214 SELECT project_id FROM 215 {$wpdb->wporg_translation_editors} 216 WHERE user_id = %d AND locale = %s 217 ", $user_id, $locale_slug ) ); 218 219 if ( ! isset( $cache[ $user_id ] ) ) { 220 $cache[ $user_id ] = array(); 221 } 222 223 $cache[ $user_id ][ $locale_slug ] = $project_access_list; 224 } 225 226 if ( ! $project_access_list ) { 227 return false; 228 } 229 230 if ( in_array( '0', $project_access_list, true ) ) { 231 $project_access_list = array( 'all' ); 232 } 191 233 192 234 // If we don't want the children, or the user has access to all projects.
Note: See TracChangeset
for help on using the changeset viewer.