Making WordPress.org


Ignore:
Timestamp:
01/10/2016 12:16:28 AM (11 years ago)
Author:
ocean90
Message:

Translate: Update roles plugin (WP version) to use the new translation_editors table.

See #1240, #1352.

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  
    2929
    3030        /**
    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';
    3644
    3745        /**
    … …  
    3947         */
    4048        public function __construct() {
     49                $GLOBALS['wpdb']->wporg_translation_editors = self::TRANSLATION_EDITORS_TABLE;
     50
    4151                add_filter( 'gp_pre_can_user', array( $this, 'pre_can_user' ), 9 , 2 );
    4252                add_action( 'gp_project_created', array( $this, 'project_created' ) );
    … …  
    144154         */
    145155        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
    146170                // Get blog prefix of the associated Rosetta site.
    147171                if ( ! $blog_prefix = $this->get_blog_prefix( $locale_slug ) ) {
     172                        $cache[ $user_id ][ $locale_slug ] = false;
    148173                        return false;
    149174                }
    … …  
    151176                $user = get_user_by( 'id', $user_id );
    152177
    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;
    162188        }
    163189
    … …  
    175201         */
    176202        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                }
    191233
    192234                // 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.