Making WordPress.org


Ignore:
Timestamp:
11/18/2015 05:52:19 PM (9 years ago)
Author:
ocean90
Message:

Translate: Improve the function for transforming a flat array into a hierarchy tree to avoid execution timeouts.

File:
1 edited

Legend:

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

    r1866 r2090  
    258258        $projects = array();
    259259        foreach ( $_projects as $project ) {
     260            $project->sub_projects = array();
    260261            $projects[ $project->id ] = $project;
    261262        }
     
    277278
    278279        $projects = $this->get_all_projects();
    279         $project_tree = $this->_get_project_tree( $projects );
     280
     281        $project_tree = array();
     282        foreach ( $projects as $project_id => $project ) {
     283            $projects[ $project->parent_project_id ]->sub_projects[ $project_id ] = &$projects[ $project_id ];
     284            if ( ! $project->parent_project_id ) {
     285                $project_tree[ $project_id ] = &$projects[ $project_id ];
     286            }
     287        }
    280288
    281289        return $project_tree;
    282     }
    283 
    284     /**
    285      * Transforms a flat array to a hierarchy tree.
    286      *
    287      * @param array $projects  The projects
    288      * @param int   $parent_id Optional. Parent ID. Default 0.
    289      * @param int   $max_depth Optional. Max depth to avoid endless recursion. Default 5.
    290      * @return array The project tree.
    291      */
    292     private function _get_project_tree( $projects, $parent_id = 0, $max_depth = 5 ) {
    293         if ( $max_depth < 0 ) { // Avoid an endless recursion.
    294             return;
    295         }
    296 
    297         $tree = array();
    298         foreach ( $projects as $project ) {
    299             if ( $project->parent_project_id == $parent_id ) {
    300                 $sub_projects = $this->_get_project_tree( $projects, $project->id, $max_depth - 1 );
    301                 if ( $sub_projects ) {
    302                     $project->sub_projects = $sub_projects;
    303                 }
    304 
    305                 $tree[ $project->id ] = $project;
    306             }
    307         }
    308         return $tree;
    309290    }
    310291
Note: See TracChangeset for help on using the changeset viewer.