Making WordPress.org

Changeset 4377


Ignore:
Timestamp:
11/19/2016 01:51:52 PM (8 years ago)
Author:
ocean90
Message:

Translate, Rosetta Roles: Don't use get_project_id_access_list() to check if a user can approve translations for a sub project.

Instead, check if one of the parent project IDs is the access list. This scales much better with 56k projects.

See #2255.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-rosetta-roles/inc/class-plugin.php

    r4183 r4377  
    143143
    144144        // If current project is a parent ID.
    145         if ( in_array( $locale_and_project_id->project_id, $project_access_list ) ) {
     145        if ( in_array( $locale_and_project_id->project_id, $project_access_list, true ) ) {
    146146            return true;
    147147        }
    148148
    149149        // A user is allowed to approve sub projects as well.
    150         $project_access_list = $this->get_project_id_access_list( $args['user_id'], $locale_and_project_id->locale, /* $include_children = */ true );
    151         if ( in_array( $locale_and_project_id->project_id, $project_access_list ) ) {
     150        $parent_project_ids = $this->get_parent_project_ids( $locale_and_project_id->project_id );
     151        if ( array_intersect( $project_access_list, $parent_project_ids ) ) {
    152152            return true;
    153153        }
    154154
    155155        return false;
     156    }
     157
     158    /**
     159     * Retrieves all parent project IDs of a project.
     160     *
     161     * @param int $project_id ID of a project.
     162     * @return array List of project IDs that are parents of a project.
     163     */
     164    private function get_parent_project_ids( $project_id ) {
     165        $last_changed = wp_cache_get_last_changed( self::$cache_group );
     166
     167        $cache_key = 'project:' . $project_id . ':parents:' . $last_changed;
     168        $cache = wp_cache_get( $cache_key, self::$cache_group );
     169        if ( false !== $cache ) {
     170            return $cache;
     171        }
     172
     173        $parent_project_ids = [];
     174
     175        $parent_project = GP::$project->get( $project_id );
     176        $parent_project_id = $parent_project->parent_project_id;
     177        while ( $parent_project_id ) {
     178            $parent_project_ids[] = $parent_project_id;
     179
     180            $parent_project = GP::$project->get( $parent_project_id );
     181            $parent_project_id = $parent_project->parent_project_id;
     182        }
     183
     184        wp_cache_set( $cache_key, $parent_project_ids, self::$cache_group );
     185
     186        return $parent_project_ids;
    156187    }
    157188
     
    436467     */
    437468    public function clear_project_cache() {
     469        wp_cache_set( 'last_changed', microtime(), self::$cache_group );
     470
    438471        $projects = $this->get_all_projects();
    439472
Note: See TracChangeset for help on using the changeset viewer.