Making WordPress.org

Changeset 1785


Ignore:
Timestamp:
07/26/2015 04:22:58 PM (7 years ago)
Author:
ocean90
Message:

Translate: Cache parent child relationships.

see #1101.

File:
1 edited

Legend:

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

    r1703 r1785  
    66 */
    77class GP_WPorg_Rosetta_Roles extends GP_Plugin {
     8
     9    /**
     10     * Cache group.
     11     *
     12     * @var string
     13     */
     14    public $cache_group = 'wporg-translate';
     15
    816    /**
    917     * Holds the plugin ID.
     
    106114        }
    107115
    108         // An user is allowed to approve potential sub projects as well.
    109         $projects = $this->get_all_projects(); // Flat array
    110         $project_tree = $this->get_project_tree( $projects );
     116        // A user is allowed to approve sub projects as well.
    111117        $allowed_sub_project_ids = array();
    112118        foreach ( $project_access_list as $project_id ) {
    113             $sub_project_ids = $this->get_sub_project_ids( $project_id, $project_tree );
     119            $sub_project_ids = $this->get_sub_project_ids( $project_id );
    114120            if ( $sub_project_ids ) {
    115121                $allowed_sub_project_ids = array_merge( $allowed_sub_project_ids, $sub_project_ids );
     
    132138    public function get_all_projects() {
    133139        global $gpdb;
    134         static $projects;
    135 
    136         if ( isset( $projects ) ) {
     140        static $projects = null;
     141
     142        if ( null !== $projects ) {
    137143            return $projects;
    138144        }
    139 
    140         $table = GP::$project->table;
    141145
    142146        $_projects = $gpdb->get_results( "
    143147            SELECT
    144148                id, parent_project_id
    145             FROM $table
     149            FROM {$gpdb->projects}
    146150            ORDER BY id
    147151        " );
     
    153157
    154158        return $projects;
     159    }
     160
     161    /**
     162     * Returns projects as a hierarchy tree.
     163     *
     164     * @return array The project tree.
     165     */
     166    public function get_project_tree() {
     167        static $project_tree = null;
     168
     169        if ( null !== $project_tree ) {
     170            return $project_tree;
     171        }
     172
     173        $projects = $this->get_all_projects();
     174        $project_tree = $this->_get_project_tree( $projects );
     175
     176        return $project_tree;
    155177    }
    156178
     
    163185     * @return array The project tree.
    164186     */
    165     public function get_project_tree( $projects, $parent_id = 0, $max_depth = 5 ) {
     187    private function _get_project_tree( $projects, $parent_id = 0, $max_depth = 5 ) {
    166188        if ( $max_depth < 0 ) { // Avoid an endless recursion.
    167189            return;
     
    171193        foreach ( $projects as $project ) {
    172194            if ( $project->parent_project_id == $parent_id ) {
    173                 $sub_projects = $this->get_project_tree( $projects, $project->id, $max_depth - 1 );
     195                $sub_projects = $this->_get_project_tree( $projects, $project->id, $max_depth - 1 );
    174196                if ( $sub_projects ) {
    175197                    $project->sub_projects = $sub_projects;
     
    177199
    178200                $tree[ $project->id ] = $project;
    179                 unset( $projects[ $project->id ] );
    180201            }
    181202        }
     
    186207     * Returns all sub project IDs of a parent ID.
    187208     *
    188      * @param int   $project_id Parent ID.
    189      * @param array $projects   Hierarchy tree of projects.
     209     * @param int $project_id Parent ID.
    190210     * @return array IDs of the sub projects.
    191211     */
    192     public function get_sub_project_ids( $project_id, $projects ) {
    193         $project_branch = $this->get_project_branch( $project_id, $projects );
    194         $project_ids = self::array_keys_multi( $project_branch->sub_projects, 'sub_projects' );
     212    public function get_sub_project_ids( $project_id ) {
     213        $cache_key = 'project:' . $project_id . ':childs';
     214        $cache = wp_cache_get( $cache_key, $this->cache_group );
     215        if ( false !== $cache ) {
     216            return $cache;
     217        }
     218
     219        $project_tree = $this->get_project_tree();
     220        $project_branch = $this->get_project_branch( $project_id, $project_tree );
     221
     222        $project_ids = array();
     223        if ( isset( $project_branch->sub_projects ) ) {
     224            $project_ids = self::array_keys_multi( $project_branch->sub_projects, 'sub_projects' );
     225        }
     226
     227        wp_cache_set( $cache_key, $project_ids, $this->cache_group );
     228
    195229        return $project_ids;
    196230    }
     
    213247            }
    214248
    215             $sub = $this->get_project_branch( $project_id, $project->sub_projects );
    216             if ( $sub ) {
    217                 return $sub;
     249            if ( isset( $project->sub_projects ) ) {
     250                $sub = $this->get_project_branch( $project_id, $project->sub_projects );
     251                if ( $sub ) {
     252                    return $sub;
     253                }
    218254            }
    219255        }
     
    291327            $keys[] = $key;
    292328
    293             if ( is_array( $value->$childs_key ) ) {
     329            if ( isset( $value->$childs_key ) && is_array( $value->$childs_key ) ) {
    294330                $keys = array_merge( $keys, self::array_keys_multi( $value->$childs_key ) );
    295331            }
Note: See TracChangeset for help on using the changeset viewer.