Changeset 1785 for sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-rosetta-roles/wporg-rosetta-roles.php
- Timestamp:
- 07/26/2015 04:22:58 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-rosetta-roles/wporg-rosetta-roles.php
r1703 r1785 6 6 */ 7 7 class GP_WPorg_Rosetta_Roles extends GP_Plugin { 8 9 /** 10 * Cache group. 11 * 12 * @var string 13 */ 14 public $cache_group = 'wporg-translate'; 15 8 16 /** 9 17 * Holds the plugin ID. … … 106 114 } 107 115 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. 111 117 $allowed_sub_project_ids = array(); 112 118 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 ); 114 120 if ( $sub_project_ids ) { 115 121 $allowed_sub_project_ids = array_merge( $allowed_sub_project_ids, $sub_project_ids ); … … 132 138 public function get_all_projects() { 133 139 global $gpdb; 134 static $projects ;135 136 if ( isset( $projects )) {140 static $projects = null; 141 142 if ( null !== $projects ) { 137 143 return $projects; 138 144 } 139 140 $table = GP::$project->table;141 145 142 146 $_projects = $gpdb->get_results( " 143 147 SELECT 144 148 id, parent_project_id 145 FROM $table149 FROM {$gpdb->projects} 146 150 ORDER BY id 147 151 " ); … … 153 157 154 158 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; 155 177 } 156 178 … … 163 185 * @return array The project tree. 164 186 */ 165 p ublic functionget_project_tree( $projects, $parent_id = 0, $max_depth = 5 ) {187 private function _get_project_tree( $projects, $parent_id = 0, $max_depth = 5 ) { 166 188 if ( $max_depth < 0 ) { // Avoid an endless recursion. 167 189 return; … … 171 193 foreach ( $projects as $project ) { 172 194 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 ); 174 196 if ( $sub_projects ) { 175 197 $project->sub_projects = $sub_projects; … … 177 199 178 200 $tree[ $project->id ] = $project; 179 unset( $projects[ $project->id ] );180 201 } 181 202 } … … 186 207 * Returns all sub project IDs of a parent ID. 187 208 * 188 * @param int $project_id Parent ID. 189 * @param array $projects Hierarchy tree of projects. 209 * @param int $project_id Parent ID. 190 210 * @return array IDs of the sub projects. 191 211 */ 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 195 229 return $project_ids; 196 230 } … … 213 247 } 214 248 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 } 218 254 } 219 255 } … … 291 327 $keys[] = $key; 292 328 293 if ( is _array( $value->$childs_key ) ) {329 if ( isset( $value->$childs_key ) && is_array( $value->$childs_key ) ) { 294 330 $keys = array_merge( $keys, self::array_keys_multi( $value->$childs_key ) ); 295 331 }
Note: See TracChangeset
for help on using the changeset viewer.