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 / views / class-groups-shortcodes.php

class-groups-shortcodes.php in Groups – Memberships and Access Control 1.1.5, at lib/views/class-groups-shortcodes.php

304 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class-groups-shortcodes.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 * Shortcode handlers
24 */
25 class Groups_Shortcodes {
26
27 /**
28 * Adds shortcodes.
29 */
30 public static function init() {
31 // group info
32 add_shortcode( 'groups_group_info', array( __CLASS__, 'groups_group_info' ) );
33 // user groups
34 add_shortcode( 'groups_user_groups', array( __CLASS__, 'groups_user_groups' ) );
35 // groups
36 add_shortcode( 'groups_groups', array( __CLASS__, 'groups_groups' ) );
37 }
38
39 /**
40 * Renders information about a group.
41 * Attributes:
42 * - "group" : group name or id
43 * - "show" : what to show, can be "name", "description", "count"
44 *
45 * @param array $atts attributes
46 * @param string $content content to render
47 * @return rendered information
48 */
49 public static function groups_group_info( $atts, $content = null ) {
50 global $wpdb;
51 $output = "";
52 $options = shortcode_atts(
53 array(
54 'group' => '',
55 'show' => '',
56 'format' => '',
57 'single' => '1',
58 'plural' => '%d'
59 ),
60 $atts
61 );
62 $group = trim( $options['group'] );
63 $current_group = Groups_Group::read( $group );
64 if ( !$current_group ) {
65 $current_group = Groups_Group::read_by_name( $group );
66 }
67 if ( $current_group ) {
68 switch( $options['show'] ) {
69 case 'name' :
70 $output .= wp_filter_nohtml_kses( $current_group->name );
71 break;
72 case 'description' :
73 $output .= wp_filter_nohtml_kses( $current_group->description );
74 break;
75 case 'count' :
76 $user_group_table = _groups_get_tablename( "user_group" );
77 $count = $wpdb->get_var( $wpdb->prepare(
78 "SELECT COUNT(*) FROM $user_group_table WHERE group_id = %d",
79 Groups_Utility::id( $current_group->group_id )
80 ) );
81 if ( $count === null ) {
82 $count = 0;
83 } else {
84 $count = intval( $count );
85 }
86 $output .= _n( $options['single'], sprintf( $options['plural'], $count ), $count, GROUPS_PLUGIN_DOMAIN );
87 break;
88 }
89 }
90 return $output;
91 }
92
93 /**
94 * Renders the current or a specific user's groups.
95 * Attributes:
96 * - "user_id" OR "user_login" OR "user_email" to identify the user, if none given assumes the current user
97 * - "format" : one of "list" "div" "ul" or "ol" - "list" and "ul" are equivalent
98 * - "list_class" : defaults to "groups"
99 * - "item_class" : defaults to "name"
100 * - "order_by" : defaults to "name", also accepts "group_id"
101 * - "order" : default to "ASC", also accepts "asc", "desc" and "DESC"
102 *
103 * @param array $atts attributes
104 * @param string $content not used
105 * @return rendered groups for current user
106 */
107 public static function groups_user_groups( $atts, $content = null ) {
108 $output = "";
109 $options = shortcode_atts(
110 array(
111 'user_id' => null,
112 'user_login' => null,
113 'user_email' => null,
114 'format' => 'list',
115 'list_class' => 'groups',
116 'item_class' => 'name',
117 'order_by' => 'name',
118 'order' => 'ASC'
119 ),
120 $atts
121 );
122 $user_id = null;
123 if ( $options['user_id'] !== null ) {
124 if ( $user = get_user_by( 'id', $options['user_id'] ) ) {
125 $user_id = $user->ID;
126 }
127 } else if ( $options['user_id'] !== null ) {
128 if ( $user = get_user_by( 'login', $options['user_login'] ) ) {
129 $user_id = $user->ID;
130 }
131 } else if ( $options['user_email'] !== null ) {
132 if ( $user = get_user_by( 'email', $options['user_login'] ) ) {
133 $user_id = $user->ID;
134 }
135 }
136 if ( $user_id === null ) {
137 $user_id = get_current_user_id();
138 }
139 if ( $user_id !== null ) {
140 $user = new Groups_User( $user_id );
141 $groups = $user->__get( 'groups' );
142 if ( !empty( $groups ) ) {
143 switch( $options['order_by'] ) {
144 case 'group_id' :
145 usort( $groups, array( __CLASS__, 'sort_id' ) );
146 break;
147 default :
148 usort( $groups, array( __CLASS__, 'sort_name' ) );
149 }
150 switch( $options['order'] ) {
151 case 'desc' :
152 case 'DESC' :
153 $groups = array_reverse( $groups );
154 break;
155 }
156
157 switch( $options['format'] ) {
158 case 'list' :
159 case 'ul' :
160 $output .= '<ul class="' . esc_attr( $options['list_class'] ) . '">';
161 break;
162 case 'ol' :
163 $output .= '<ol class="' . esc_attr( $options['list_class'] ) . '">';
164 break;
165 default :
166 $output .= '<div class="' . esc_attr( $options['list_class'] ) . '">';
167 }
168 foreach( $groups as $group ) {
169 switch( $options['format'] ) {
170 case 'list' :
171 case 'ul' :
172 case 'ol' :
173 $output .= '<li class="' . esc_attr( $options['item_class'] ) . '">' . $group->__get( 'name' ) . '</li>';
174 break;
175 default :
176 $output .= '<div class="' . esc_attr( $options['item_class'] ) . '">' . $group->__get( 'name' ) . '</div>';
177 }
178 }
179 switch( $options['format'] ) {
180 case 'list' :
181 case 'ul' :
182 $output .= '</ul>';
183 break;
184 case 'ol' :
185 $output .= '</ol>';
186 break;
187 default :
188 $output .= '</div>';
189 }
190 }
191 }
192 return $output;
193 }
194
195 /**
196 * Group comparison by group_id.
197 *
198 * @param Groups_Group $a
199 * @param Groups_Group $b
200 * @return int
201 */
202 public static function sort_id( $a, $b ) {
203 return $a->group_id - $b->group_id;
204 }
205
206 /**
207 * Group comparison by name.
208 *
209 * @param Groups_Group $a
210 * @param Groups_Group $b
211 * @return int
212 */
213 public static function sort_name( $a, $b ) {
214 return strcmp( $a->name, $b->name );
215 }
216
217 /**
218 * Renders a list of the site's groups.
219 * Attributes:
220 * - "format" : one of "list" "div" "ul" or "ol" - "list" and "ul" are equivalent
221 * - "list_class" : defaults to "groups"
222 * - "item_class" : defaults to "name"
223 * - "order_by" : defaults to "name", also accepts "group_id"
224 * - "order" : default to "ASC", also accepts "asc", "desc" and "DESC"
225 *
226 * @param array $atts attributes
227 * @param string $content not used
228 * @return rendered groups
229 */
230 public static function groups_groups( $atts, $content = null ) {
231 global $wpdb;
232 $output = "";
233 $options = shortcode_atts(
234 array(
235 'format' => 'list',
236 'list_class' => 'groups',
237 'item_class' => 'name',
238 'order_by' => 'name',
239 'order' => 'ASC'
240 ),
241 $atts
242 );
243 switch( $options['order_by'] ) {
244 case 'group_id' :
245 case 'name' :
246 $order_by = $options['order_by'];
247 break;
248 default :
249 $order_by = 'name';
250 }
251 switch( $options['order'] ) {
252 case 'asc' :
253 case 'ASC' :
254 case 'desc' :
255 case 'DESC' :
256 $order = strtoupper( $options['order'] );
257 break;
258 default :
259 $order = 'ASC';
260 }
261 $group_table = _groups_get_tablename( "group" );
262 if ( $groups = $wpdb->get_results( $wpdb->prepare(
263 "SELECT group_id FROM $group_table ORDER BY $order_by $order"
264 ) ) ) {
265 switch( $options['format'] ) {
266 case 'list' :
267 case 'ul' :
268 $output .= '<ul class="' . esc_attr( $options['list_class'] ) . '">';
269 break;
270 case 'ol' :
271 $output .= '<ol class="' . esc_attr( $options['list_class'] ) . '">';
272 break;
273 default :
274 $output .= '<div class="' . esc_attr( $options['list_class'] ) . '">';
275 }
276 foreach( $groups as $group ) {
277 $group = new Groups_Group( $group->group_id );
278 switch( $options['format'] ) {
279 case 'list' :
280 case 'ul' :
281 case 'ol' :
282 $output .= '<li class="' . esc_attr( $options['item_class'] ) . '">' . $group->__get( 'name' ) . '</li>';
283 break;
284 default :
285 $output .= '<div class="' . esc_attr( $options['item_class'] ) . '">' . $group->__get( 'name' ) . '</div>';
286 }
287 }
288 switch( $options['format'] ) {
289 case 'list' :
290 case 'ul' :
291 $output .= '</ul>';
292 break;
293 case 'ol' :
294 $output .= '</ol>';
295 break;
296 default :
297 $output .= '</div>';
298 }
299 }
300 return $output;
301 }
302 }
303 Groups_Shortcodes::init();
304