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

138 lines 3.6 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 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 /**
27 * Utility functions.
28 */
29 class Groups_Utility {
30
31 /**
32 * Checks an id (0 is accepted => anonymous).
33 *
34 * @param string|int $id
35 * @return int|boolean if validated, the id as an int, otherwise false
36 */
37 public static function id( $id ) {
38 $result = false;
39 if ( is_numeric( $id ) ) {
40 $id = intval( $id );
41 //if ( $id > 0 ) {
42 if ( $id >= 0 ) { // 0 => anonymous
43 $result = $id;
44 }
45 }
46 return $result;
47 }
48
49 /**
50 * Returns an array of blog_ids for current blogs.
51 * @return array of int with blog ids
52 */
53 public static function get_blogs() {
54 global $wpdb;
55 $result = array();
56 if ( is_multisite() ) {
57 $blogs = $wpdb->get_results( $wpdb->prepare(
58 "SELECT blog_id FROM $wpdb->blogs WHERE site_id = %d AND archived = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC",
59 $wpdb->siteid
60 ) );
61 if ( is_array( $blogs ) ) {
62 foreach( $blogs as $blog ) {
63 $result[] = $blog->blog_id;
64 }
65 }
66 } else {
67 $result[] = get_current_blog_id();
68 }
69 return $result;
70 }
71
72
73 public static function get_group_tree( &$tree = null ) {
74 global $wpdb;
75 $group_table = _groups_get_tablename( 'group' );
76
77 if ( $tree === null ) {
78 $tree = array();
79 $root_groups = $wpdb->get_results( "SELECT group_id FROM $group_table WHERE parent_id IS NULL" );
80 if ( $root_groups ) {
81 foreach( $root_groups as $root_group ) {
82 $group_id = Groups_Utility::id( $root_group->group_id );
83 $tree[$group_id] = array();
84 }
85 }
86 self::get_group_tree( $tree );
87 } else {
88 foreach( $tree as $group_id => $nodes ) {
89 $children = $wpdb->get_results( $wpdb->prepare(
90 "SELECT group_id FROM $group_table WHERE parent_id = %d",
91 Groups_Utility::id( $group_id )
92 ) );
93 foreach( $children as $child ) {
94 $tree[$group_id][$child->group_id] = array();
95 }
96 self::get_group_tree( $tree[$group_id] );
97 }
98 }
99
100 return $tree;
101 }
102
103 public static function render_group_tree( &$tree, &$output ) {
104 $output .= '<ul style="padding-left:1em">';
105 foreach( $tree as $group_id => $nodes ) {
106 $output .= '<li>';
107 $group = Groups_Group::read( $group_id );
108 if ( $group ) {
109 $output .= $group->name;
110 }
111 if ( !empty( $nodes ) ) {
112 self::render_group_tree( $nodes, $output );
113 }
114 $output .= '</li>';
115 }
116 $output .= '</ul>';
117 }
118
119 /**
120 * Compares the two object's names, used for groups and
121 * capabilities, i.e. Groups_Group and Groups_Capability can be compared
122 * if both are of the same class. Otherwise this will return 0.
123 *
124 * @param Groups_Group|Groups_Capability $o1
125 * @param Groups_Group|Groups_Capability $o2 must match the class of $o1
126 * @return number
127 */
128 public static function cmp( $o1, $o2 ) {
129 $result = 0;
130 if ( $o1 instanceof Groups_Group && $o2 instanceof Groups_Group ) {
131 $result = strcmp( $o1->name, $o2->name );
132 } else if ( $o1 instanceof Groups_Capability && $o2 instanceof Groups_Capability ) {
133 $result = strcmp( $o1->capability->capability, $o2->capability->capability );
134 }
135 return $result;
136 }
137 }
138