Making WordPress.org


Ignore:
Timestamp:
12/17/2015 11:59:45 AM (9 years ago)
Author:
ocean90
Message:

Translate: Use the new translation_editors table to retrieve the project ID access list of a user.

See #1240.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-rosetta-roles/wporg-rosetta-roles.php

    r2093 r2196  
    2222
    2323    /**
    24      * Holds the role of an approver.
    25      *
    26      * @var string
    27      */
    28     public $approver_role = 'translation_editor';
     24     * Database table for translation editors.
     25     */
     26    const TRANSLATION_EDITORS_TABLE = 'translate_translation_editors';
     27
     28    /**
     29     * Role of a per project translation editor.
     30     */
     31    const TRANSLATION_EDITOR_ROLE = 'translation_editor';
     32
     33    /**
     34     * Role of a general translation editor.
     35     */
     36    const GENERAL_TRANSLATION_EDITOR_ROLE = 'general_translation_editor';
    2937
    3038    /**
     
    4048    public function __construct() {
    4149        parent::__construct();
     50
     51        $GLOBALS['gpdb']->wporg_translation_editors = self::TRANSLATION_EDITORS_TABLE;
     52
    4253        $this->add_filter( 'pre_can_user', array( 'args' => 2, 'priority' => 9 ) );
    4354        $this->add_action( 'project_created' );
     
    152163     */
    153164    public function is_approver_for_locale( $user_id, $locale_slug ) {
     165        static $cache = null;
     166
     167        if ( null === $cache ) {
     168            $cache = array();
     169        }
     170
     171        if ( isset( $cache[ $user_id ][ $locale_slug ] ) ) {
     172            return $cache[ $user_id ][ $locale_slug ];
     173        }
     174
    154175        if ( ! class_exists( 'BP_Roles' ) ) {
    155176            require_once( BACKPRESS_PATH . 'class.bp-roles.php' );
     
    174195        $user->get_role_caps();
    175196
    176         return $user->has_cap( $this->approver_role );
    177 
     197        $is_approver = $user->has_cap( self::TRANSLATION_EDITOR_ROLE ) || $user->has_cap( self::GENERAL_TRANSLATION_EDITOR_ROLE );
     198
     199        if ( ! isset( $cache[ $user_id ] ) ) {
     200            $cache[ $user_id ] = array();
     201        }
     202
     203        $cache[ $user_id ][ $locale_slug ] = $is_approver;
     204        return $is_approver;
    178205    }
    179206
     
    191218     */
    192219    public function get_project_id_access_list( $user_id, $locale_slug, $include_children = false ) {
    193         if ( ! class_exists( 'BP_Roles' ) ) {
    194             require_once( BACKPRESS_PATH . 'class.bp-roles.php' );
    195         }
    196         if ( ! class_exists( 'BP_User' ) ) {
    197             require_once( BACKPRESS_PATH . 'class.bp-user.php' );
    198         }
    199 
    200         $user = new BP_User( $user_id );
    201 
    202         // Get blog prefix of the associated Rosetta site.
    203         if ( ! $blog_prefix = $this->get_blog_prefix( $locale_slug ) ) {
    204             return false;
    205         }
    206 
    207         // Get IDs of projects which the user can approve.
    208         $meta_key = $blog_prefix . $this->project_access_meta_key;
    209         if ( empty( $user->$meta_key ) || ! is_array( $user->$meta_key ) ) {
    210             return false;
    211         }
    212 
    213         $project_access_list = $user->$meta_key;
     220        global $gpdb;
     221        static $cache = null;
     222
     223        if ( null === $cache ) {
     224            $cache = array();
     225        }
     226
     227        if ( isset( $cache[ $user_id ][ $locale_slug ] ) ) {
     228            $project_access_list = $cache[ $user_id ][ $locale_slug ];
     229        } else {
     230            $project_access_list = $gpdb->get_col( $gpdb->prepare( "
     231                SELECT project_id FROM
     232                {$gpdb->wporg_translation_editors}
     233                WHERE user_id = %d AND locale = %s
     234            ", $user_id, $locale_slug ) );
     235
     236            if ( ! isset( $cache[ $user_id ] ) ) {
     237                $cache[ $user_id ] = array();
     238            }
     239
     240            $cache[ $user_id ][ $locale_slug ] = $project_access_list;
     241        }
     242
     243        if ( ! $project_access_list ) {
     244            return false;
     245        }
     246
     247        if ( in_array( '0', $project_access_list, true ) ) {
     248            $project_access_list = array( 'all' );
     249        }
    214250
    215251        // 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.