PluginProbe
Groups – Memberships and Access Control / 1.1.5
Groups – Memberships and Access Control v1.1.5
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.5, at lib/core/class-groups-utility.php

114 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
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 }