PluginProbe
Groups – Memberships and Access Control / 1.1.4
Groups – Memberships and Access Control v1.1.4
4.7.1 4.7.0 4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 All 131 releases
groups / lib / core / class-groups-utility.php

class-groups-utility.php in Groups – Memberships and Access Control 1.1.4, at lib/core/class-groups-utility.php

110 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 class Groups_Utility {
22
23 /**
24 * Checks an id (0 is accepted => anonymous).
25 *
26 * @param string|int $id
27 * @return int|boolean if validated, the id as an int, otherwise false
28 */
29 public static function id( $id ) {
30 $result = false;
31 if ( is_numeric( $id ) ) {
32 $id = intval( $id );
33 //if ( $id > 0 ) {
34 if ( $id >= 0 ) { // 0 => anonymous
35 $result = $id;
36 }
37 }
38 return $result;
39 }
40
41 /**
42 * Returns an array of blog_ids for current blogs.
43 * @return array of int with blog ids
44 */
45 public static function get_blogs() {
46 global $wpdb;
47 $result = array();
48 if ( is_multisite() ) {
49 $blogs = $wpdb->get_results( $wpdb->prepare(
50 "SELECT blog_id FROM $wpdb->blogs WHERE site_id = %d AND archived = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC",
51 $wpdb->siteid
52 ) );
53 if ( is_array( $blogs ) ) {
54 foreach( $blogs as $blog ) {
55 $result[] = $blog->blog_id;
56 }
57 }
58 } else {
59 $result[] = get_current_blog_id();
60 }
61 return $result;
62 }
63
64
65 public static function get_group_tree( &$tree = null ) {
66 global $wpdb;
67 $group_table = _groups_get_tablename( 'group' );
68
69 if ( $tree === null ) {
70 $tree = array();
71 $root_groups = $wpdb->get_results( "SELECT group_id FROM $group_table WHERE parent_id IS NULL" );
72 if ( $root_groups ) {
73 foreach( $root_groups as $root_group ) {
74 $group_id = Groups_Utility::id( $root_group->group_id );
75 $tree[$group_id] = array();
76 }
77 }
78 self::get_group_tree( $tree );
79 } else {
80 foreach( $tree as $group_id => $nodes ) {
81 $children = $wpdb->get_results( $wpdb->prepare(
82 "SELECT group_id FROM $group_table WHERE parent_id = %d",
83 Groups_Utility::id( $group_id )
84 ) );
85 foreach( $children as $child ) {
86 $tree[$group_id][$child->group_id] = array();
87 }
88 self::get_group_tree( $tree[$group_id] );
89 }
90 }
91
92 return $tree;
93 }
94
95 public static function render_group_tree( &$tree, &$output ) {
96 $output .= '<ul style="padding-left:1em">';
97 foreach( $tree as $group_id => $nodes ) {
98 $output .= '<li>';
99 $group = Groups_Group::read( $group_id );
100 if ( $group ) {
101 $output .= $group->name;
102 }
103 if ( !empty( $nodes ) ) {
104 self::render_group_tree( $nodes, $output );
105 }
106 $output .= '</li>';
107 }
108 $output .= '</ul>';
109 }
110 }