| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-utility.php |
| 4 |
* |
| 5 |
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com |
| 6 |
* |
| 7 |
* This code is released under the GNU General Public License. |
| 8 |
* See COPYRIGHT.txt and LICENSE.txt. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* This header and all notices must be kept intact. |
| 16 |
* |
| 17 |
* @author Karim Rahimpur |
| 18 |
* @package groups |
| 19 |
* @since groups 1.0.0 |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* Utility functions. |
| 24 |
*/ |
| 25 |
class Groups_Utility { |
| 26 |
|
| 27 |
/** |
| 28 |
* Checks an id (0 is accepted => anonymous). |
| 29 |
* |
| 30 |
* @param string|int $id |
| 31 |
* @return int|boolean if validated, the id as an int, otherwise false |
| 32 |
*/ |
| 33 |
public static function id( $id ) { |
| 34 |
$result = false; |
| 35 |
if ( is_numeric( $id ) ) { |
| 36 |
$id = intval( $id ); |
| 37 |
//if ( $id > 0 ) { |
| 38 |
if ( $id >= 0 ) { // 0 => anonymous |
| 39 |
$result = $id; |
| 40 |
} |
| 41 |
} |
| 42 |
return $result; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns an array of blog_ids for current blogs. |
| 47 |
* @return array of int with blog ids |
| 48 |
*/ |
| 49 |
public static function get_blogs() { |
| 50 |
global $wpdb; |
| 51 |
$result = array(); |
| 52 |
if ( is_multisite() ) { |
| 53 |
$blogs = $wpdb->get_results( $wpdb->prepare( |
| 54 |
"SELECT blog_id FROM $wpdb->blogs WHERE site_id = %d AND archived = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", |
| 55 |
$wpdb->siteid |
| 56 |
) ); |
| 57 |
if ( is_array( $blogs ) ) { |
| 58 |
foreach( $blogs as $blog ) { |
| 59 |
$result[] = $blog->blog_id; |
| 60 |
} |
| 61 |
} |
| 62 |
} else { |
| 63 |
$result[] = get_current_blog_id(); |
| 64 |
} |
| 65 |
return $result; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
public static function get_group_tree( &$tree = null ) { |
| 70 |
global $wpdb; |
| 71 |
$group_table = _groups_get_tablename( 'group' ); |
| 72 |
|
| 73 |
if ( $tree === null ) { |
| 74 |
$tree = array(); |
| 75 |
$root_groups = $wpdb->get_results( "SELECT group_id FROM $group_table WHERE parent_id IS NULL" ); |
| 76 |
if ( $root_groups ) { |
| 77 |
foreach( $root_groups as $root_group ) { |
| 78 |
$group_id = Groups_Utility::id( $root_group->group_id ); |
| 79 |
$tree[$group_id] = array(); |
| 80 |
} |
| 81 |
} |
| 82 |
self::get_group_tree( $tree ); |
| 83 |
} else { |
| 84 |
foreach( $tree as $group_id => $nodes ) { |
| 85 |
$children = $wpdb->get_results( $wpdb->prepare( |
| 86 |
"SELECT group_id FROM $group_table WHERE parent_id = %d", |
| 87 |
Groups_Utility::id( $group_id ) |
| 88 |
) ); |
| 89 |
foreach( $children as $child ) { |
| 90 |
$tree[$group_id][$child->group_id] = array(); |
| 91 |
} |
| 92 |
self::get_group_tree( $tree[$group_id] ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return $tree; |
| 97 |
} |
| 98 |
|
| 99 |
public static function render_group_tree( &$tree, &$output ) { |
| 100 |
$output .= '<ul style="padding-left:1em">'; |
| 101 |
foreach( $tree as $group_id => $nodes ) { |
| 102 |
$output .= '<li>'; |
| 103 |
$group = Groups_Group::read( $group_id ); |
| 104 |
if ( $group ) { |
| 105 |
$output .= $group->name; |
| 106 |
} |
| 107 |
if ( !empty( $nodes ) ) { |
| 108 |
self::render_group_tree( $nodes, $output ); |
| 109 |
} |
| 110 |
$output .= '</li>'; |
| 111 |
} |
| 112 |
$output .= '</ul>'; |
| 113 |
} |
| 114 |
} |