PluginProbe
Groups – Memberships and Access Control / 1.10.0
Groups – Memberships and Access Control v1.10.0
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.10.0, at lib/views/class-groups-shortcodes.php

608 lines 17.7 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 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 /**
27 * Shortcode handlers
28 */
29 class Groups_Shortcodes {
30
31 /**
32 * Adds shortcodes.
33 */
34 public static function init() {
35 // login
36 add_shortcode( 'groups_login', array( __CLASS__, 'groups_login' ) );
37 // logout
38 add_shortcode( 'groups_logout', array( __CLASS__, 'groups_logout' ) );
39 // group info
40 add_shortcode( 'groups_group_info', array( __CLASS__, 'groups_group_info' ) );
41 // user groups
42 add_shortcode( 'groups_user_groups', array( __CLASS__, 'groups_user_groups' ) );
43 // groups
44 add_shortcode( 'groups_groups', array( __CLASS__, 'groups_groups' ) );
45 // join a group
46 add_shortcode( 'groups_join', array( __CLASS__, 'groups_join' ) );
47 // leave a group
48 add_shortcode( 'groups_leave', array( __CLASS__, 'groups_leave' ) );
49 }
50
51 /**
52 * Renders the Groups login form.
53 *
54 * The user is redirected to the current page after login by default.
55 * The user can be redirected to a specific URL after login by
56 * indicating the <code>redirect</code> attribute.
57 *
58 * @param array $atts
59 * @param string $content
60 * @return string the rendered form or empty
61 */
62 public static function groups_login( $atts, $content = null ) {
63 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
64 extract(
65 shortcode_atts(
66 array(
67 'redirect' => $current_url,
68 'show_logout' => 'no'
69 ),
70 $atts
71 )
72 );
73 $redirect = trim( $redirect );
74 $show_logout = trim( strtolower( $show_logout ) );
75 $output = '';
76 if ( !is_user_logged_in() ) {
77 $output .= wp_login_form(
78 array(
79 'echo' => false,
80 'redirect' => $redirect
81 )
82 );
83 } else {
84 if ( $show_logout == 'yes' ) {
85 $output .= self::groups_logout(
86 array(
87 'redirect' => $redirect
88 )
89 );
90 }
91 }
92 return $output;
93 }
94
95 /**
96 * Renders the Groups logout link.
97 *
98 * The link is rendered if the user is logged in.
99 * The user is redirected to the current page after logout by default.
100 * The user can be redirected to a specific URL after logout by
101 * indicating the <code>redirect</code> attribute.
102 *
103 * @param array $atts
104 * @param string $content not used
105 * @return string logout link, is empty if not logged in
106 */
107 public static function groups_logout( $atts, $content = null ) {
108 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
109 extract(
110 shortcode_atts(
111 array(
112 'redirect' => $current_url
113 ),
114 $atts
115 )
116 );
117 $redirect = trim( $redirect );
118 $output = '';
119 if ( is_user_logged_in() ) {
120 $output .= sprintf( '<a href="%s">', esc_url( wp_logout_url( $redirect ) ) );
121 $output .= __( 'Log out', GROUPS_PLUGIN_DOMAIN );
122 $output .= '</a>';
123 }
124 return $output;
125 }
126
127 /**
128 * Renders information about a group.
129 * Attributes:
130 * - "group" : group name or id
131 * - "show" : what to show, can be "name", "description", "count"
132 * - "format" :
133 * - "single" : used with show="count", single form, defaults to '1'
134 * - "plural" : used with show="count", plural form, defaults to '%d', must contain %d to show number
135 *
136 * @param array $atts attributes
137 * @param string $content content to render
138 * @return rendered information
139 */
140 public static function groups_group_info( $atts, $content = null ) {
141 global $wpdb;
142 $output = "";
143 $options = shortcode_atts(
144 array(
145 'group' => '',
146 'show' => '',
147 'format' => '',
148 'single' => '1',
149 'plural' => '%d'
150 ),
151 $atts
152 );
153 $group = trim( $options['group'] );
154 $current_group = Groups_Group::read( $group );
155 if ( !$current_group ) {
156 $current_group = Groups_Group::read_by_name( $group );
157 }
158 if ( $current_group ) {
159 switch( $options['show'] ) {
160 case 'name' :
161 $output .= wp_filter_nohtml_kses( $current_group->name );
162 break;
163 case 'description' :
164 $output .= wp_filter_nohtml_kses( $current_group->description );
165 break;
166 case 'count' :
167 $user_group_table = _groups_get_tablename( "user_group" );
168 $count = $wpdb->get_var( $wpdb->prepare(
169 "SELECT COUNT(*) FROM $user_group_table WHERE group_id = %d",
170 Groups_Utility::id( $current_group->group_id )
171 ) );
172 if ( $count === null ) {
173 $count = 0;
174 } else {
175 $count = intval( $count );
176 }
177 $output .= _n( $options['single'], sprintf( $options['plural'], $count ), $count, GROUPS_PLUGIN_DOMAIN );
178 break;
179 // @todo experimental - could use pagination, sorting, link to profile, ...
180 case 'users' :
181 $user_group_table = _groups_get_tablename( "user_group" );
182 $users = $wpdb->get_results( $wpdb->prepare(
183 "SELECT * FROM $wpdb->users LEFT JOIN $user_group_table ON $wpdb->users.ID = $user_group_table.user_id WHERE $user_group_table.group_id = %d",
184 Groups_Utility::id( $current_group->group_id )
185 ) );
186 if ( $users ) {
187 $output .= '<ul>';
188 foreach( $users as $user ) {
189 $output .= '<li>' . wp_filter_nohtml_kses( $user->user_login ) . '</li>';
190 }
191 $output .= '</ul>';
192 }
193
194 break;
195 }
196 }
197 return $output;
198 }
199
200 /**
201 * Renders the current or a specific user's groups.
202 * Attributes:
203 * - "user_id" OR "user_login" OR "user_email" to identify the user, if none given assumes the current user
204 * - "format" : one of "list" "div" "ul" or "ol" - "list" and "ul" are equivalent
205 * - "list_class" : defaults to "groups"
206 * - "item_class" : defaults to "name"
207 * - "order_by" : defaults to "name", also accepts "group_id"
208 * - "order" : default to "ASC", also accepts "asc", "desc" and "DESC"
209 *
210 * @param array $atts attributes
211 * @param string $content not used
212 * @return rendered groups for current user
213 */
214 public static function groups_user_groups( $atts, $content = null ) {
215 $output = "";
216 $options = shortcode_atts(
217 array(
218 'user_id' => null,
219 'user_login' => null,
220 'user_email' => null,
221 'format' => 'list',
222 'list_class' => 'groups',
223 'item_class' => 'name',
224 'order_by' => 'name',
225 'order' => 'ASC',
226 'group' => null,
227 'exclude_group' => null
228 ),
229 $atts
230 );
231 $user_id = null;
232 if ( $options['user_id'] !== null ) {
233 if ( $user = get_user_by( 'id', $options['user_id'] ) ) {
234 $user_id = $user->ID;
235 }
236 } else if ( $options['user_id'] !== null ) {
237 if ( $user = get_user_by( 'login', $options['user_login'] ) ) {
238 $user_id = $user->ID;
239 }
240 } else if ( $options['user_email'] !== null ) {
241 if ( $user = get_user_by( 'email', $options['user_login'] ) ) {
242 $user_id = $user->ID;
243 }
244 }
245 if ( $user_id === null ) {
246 $user_id = get_current_user_id();
247 }
248 if ( $user_id !== null ) {
249 $user = new Groups_User( $user_id );
250 $groups = $user->groups;
251
252 if ( !empty( $groups ) ) {
253 // group attr
254 if ( $options['group'] !== null ) {
255 $groups = array();
256 $groups_incl = explode( ",", $options['group'] );
257 foreach ( $groups_incl as $group_incl ) {
258 $group = trim( $group_incl );
259 $current_group = Groups_Group::read( $group );
260 if ( !$current_group ) {
261 $current_group = Groups_Group::read_by_name( $group );
262 }
263 if ( $current_group ) {
264 if ( Groups_User_Group::read( $user_id, $current_group->group_id ) ) {
265 $groups[] = $current_group;
266 }
267 }
268 }
269 }
270 // exclude_group attr
271 if ( $options['exclude_group'] !== null ) {
272 $groups_excl = explode( ",", $options['exclude_group'] );
273 foreach ( $groups_excl as $key => $group_excl ) {
274 $group = trim( $group_excl );
275 $current_group = Groups_Group::read( $group );
276 if ( !$current_group ) {
277 $current_group = Groups_Group::read_by_name( $group );
278 }
279 if ( $current_group ) {
280 $groups_excl[$key] = $current_group->group_id;
281 } else {
282 unset( $groups_excl[$key] );
283 }
284 }
285 foreach ( $groups as $key => $group ) {
286 if ( in_array( $group->group_id, $groups_excl ) ) {
287 unset( $groups[$key] );
288 }
289 }
290 }
291 switch( $options['order_by'] ) {
292 case 'group_id' :
293 usort( $groups, array( __CLASS__, 'sort_id' ) );
294 break;
295 default :
296 usort( $groups, array( __CLASS__, 'sort_name' ) );
297 }
298 switch( $options['order'] ) {
299 case 'desc' :
300 case 'DESC' :
301 $groups = array_reverse( $groups );
302 break;
303 }
304
305 switch( $options['format'] ) {
306 case 'list' :
307 case 'ul' :
308 $output .= '<ul class="' . esc_attr( $options['list_class'] ) . '">';
309 break;
310 case 'ol' :
311 $output .= '<ol class="' . esc_attr( $options['list_class'] ) . '">';
312 break;
313 default :
314 $output .= '<div class="' . esc_attr( $options['list_class'] ) . '">';
315 }
316 foreach( $groups as $group ) {
317 switch( $options['format'] ) {
318 case 'list' :
319 case 'ul' :
320 case 'ol' :
321 $output .= '<li class="' . esc_attr( $options['item_class'] ) . '">' . $group->name . '</li>';
322 break;
323 default :
324 $output .= '<div class="' . esc_attr( $options['item_class'] ) . '">' . $group->name . '</div>';
325 }
326 }
327 switch( $options['format'] ) {
328 case 'list' :
329 case 'ul' :
330 $output .= '</ul>';
331 break;
332 case 'ol' :
333 $output .= '</ol>';
334 break;
335 default :
336 $output .= '</div>';
337 }
338 }
339 }
340 return $output;
341 }
342
343 /**
344 * Group comparison by group_id.
345 *
346 * @param Groups_Group $a
347 * @param Groups_Group $b
348 * @return int
349 */
350 public static function sort_id( $a, $b ) {
351 return $a->group_id - $b->group_id;
352 }
353
354 /**
355 * Group comparison by name.
356 *
357 * @param Groups_Group $a
358 * @param Groups_Group $b
359 * @return int
360 */
361 public static function sort_name( $a, $b ) {
362 return strcmp( $a->name, $b->name );
363 }
364
365 /**
366 * Renders a list of the site's groups.
367 * Attributes:
368 * - "format" : one of "list" "div" "ul" or "ol" - "list" and "ul" are equivalent
369 * - "list_class" : defaults to "groups"
370 * - "item_class" : defaults to "name"
371 * - "order_by" : defaults to "name", also accepts "group_id"
372 * - "order" : default to "ASC", also accepts "asc", "desc" and "DESC"
373 *
374 * @param array $atts attributes
375 * @param string $content not used
376 * @return rendered groups
377 */
378 public static function groups_groups( $atts, $content = null ) {
379 global $wpdb;
380 $output = "";
381 $options = shortcode_atts(
382 array(
383 'format' => 'list',
384 'list_class' => 'groups',
385 'item_class' => 'name',
386 'order_by' => 'name',
387 'order' => 'ASC'
388 ),
389 $atts
390 );
391 switch( $options['order_by'] ) {
392 case 'group_id' :
393 case 'name' :
394 $order_by = $options['order_by'];
395 break;
396 default :
397 $order_by = 'name';
398 }
399 switch( $options['order'] ) {
400 case 'asc' :
401 case 'ASC' :
402 case 'desc' :
403 case 'DESC' :
404 $order = strtoupper( $options['order'] );
405 break;
406 default :
407 $order = 'ASC';
408 }
409 $group_table = _groups_get_tablename( "group" );
410 if ( $groups = $wpdb->get_results(
411 "SELECT group_id FROM $group_table ORDER BY $order_by $order"
412 ) ) {
413 switch( $options['format'] ) {
414 case 'list' :
415 case 'ul' :
416 $output .= '<ul class="' . esc_attr( $options['list_class'] ) . '">';
417 break;
418 case 'ol' :
419 $output .= '<ol class="' . esc_attr( $options['list_class'] ) . '">';
420 break;
421 default :
422 $output .= '<div class="' . esc_attr( $options['list_class'] ) . '">';
423 }
424 foreach( $groups as $group ) {
425 $group = new Groups_Group( $group->group_id );
426 switch( $options['format'] ) {
427 case 'list' :
428 case 'ul' :
429 case 'ol' :
430 $output .= '<li class="' . esc_attr( $options['item_class'] ) . '">' . $group->name . '</li>';
431 break;
432 default :
433 $output .= '<div class="' . esc_attr( $options['item_class'] ) . '">' . $group->name . '</div>';
434 }
435 }
436 switch( $options['format'] ) {
437 case 'list' :
438 case 'ul' :
439 $output .= '</ul>';
440 break;
441 case 'ol' :
442 $output .= '</ol>';
443 break;
444 default :
445 $output .= '</div>';
446 }
447 }
448 return $output;
449 }
450
451 /**
452 * Renders a form that lets a user join a group.
453 * * Attributes:
454 * - "group" : (required) group name or id
455 *
456 * @param array $atts attributes
457 * @param string $content not used
458 */
459 public static function groups_join( $atts, $content = null ) {
460 $nonce_action = 'groups_action';
461 $nonce = 'nonce_join';
462 $output = "";
463
464 $options = shortcode_atts(
465 array(
466 'group' => '',
467 'display_message' => true,
468 'display_is_member' => false,
469 'submit_text' => __( 'Join the %s group', GROUPS_PLUGIN_DOMAIN )
470 ),
471 $atts
472 );
473 extract( $options );
474
475 if ( $display_message === 'false' ) {
476 $display_message = false;
477 }
478 if ( $display_is_member === 'true' ) {
479 $display_is_member = true;
480 }
481
482 $group = trim( $options['group'] );
483 $current_group = Groups_Group::read( $group );
484 if ( !$current_group ) {
485 $current_group = Groups_Group::read_by_name( $group );
486 }
487 if ( $current_group ) {
488 if ( $user_id = get_current_user_id() ) {
489 $submitted = false;
490 $invalid_nonce = false;
491 if ( !empty( $_POST['groups_action'] ) && $_POST['groups_action'] == 'join' ) {
492 $submitted = true;
493 if ( !wp_verify_nonce( $_POST[$nonce], $nonce_action ) ) {
494 $invalid_nonce = true;
495 }
496 }
497 if ( $submitted && !$invalid_nonce ) {
498 // add user to group
499 if ( isset( $_POST['group_id'] ) ) {
500 $join_group = Groups_Group::read( $_POST['group_id'] );
501 Groups_User_Group::create(
502 array(
503 'group_id' => $join_group->group_id,
504 'user_id' => $user_id
505 )
506 );
507 }
508 }
509 if ( !Groups_User_Group::read( $user_id, $current_group->group_id ) ) {
510 $submit_text = sprintf( $options['submit_text'], wp_filter_nohtml_kses( $current_group->name ) );
511 $output .= '<div class="groups-join">';
512 $output .= '<form action="#" method="post">';
513 $output .= '<input type="hidden" name="groups_action" value="join" />';
514 $output .= '<input type="hidden" name="group_id" value="' . esc_attr( $current_group->group_id ) . '" />';
515 $output .= '<input type="submit" value="' . $submit_text . '" />';
516 $output .= wp_nonce_field( $nonce_action, $nonce, true, false );
517 $output .= '</form>';
518 $output .= '</div>';
519 } else if ( $display_message ) {
520 if ( $submitted && !$invalid_nonce && isset( $join_group ) && $join_group->group_id === $current_group->group_id ) {
521 $output .= '<div class="groups-join joined">';
522 $output .= sprintf( __( 'You have joined the %s group.', GROUPS_PLUGIN_DOMAIN ), wp_filter_nohtml_kses( $join_group->name ) );
523 $output .= '</div>';
524 }
525 else if ( $display_is_member && isset( $current_group ) && $current_group !== false ) {
526 $output .= '<div class="groups-join member">';
527 $output .= sprintf( __( 'You are a member of the %s group.', GROUPS_PLUGIN_DOMAIN ), wp_filter_nohtml_kses( $current_group->name ) );
528 $output .= '</div>';
529 }
530 }
531 }
532 }
533 return $output;
534 }
535
536 /**
537 * Renders a form that lets a user leave a group.
538 * * Attributes:
539 * - "group" : (required) group name or id
540 *
541 * @param array $atts attributes
542 * @param string $content not used
543 */
544 public static function groups_leave( $atts, $content = null ) {
545 $nonce_action = 'groups_action';
546 $nonce = 'nonce_leave';
547 $output = "";
548
549 $options = shortcode_atts(
550 array(
551 'group' => '',
552 'display_message' => true,
553 'submit_text' => __( 'Leave the %s group', GROUPS_PLUGIN_DOMAIN ),
554 ),
555 $atts
556 );
557 extract( $options );
558
559 if ( $display_message === 'false' ) {
560 $display_message = false;
561 }
562
563 $group = trim( $options['group'] );
564 $current_group = Groups_Group::read( $group );
565 if ( !$current_group ) {
566 $current_group = Groups_Group::read_by_name( $group );
567 }
568 if ( $current_group ) {
569 if ( $user_id = get_current_user_id() ) {
570 $submitted = false;
571 $invalid_nonce = false;
572 if ( !empty( $_POST['groups_action'] ) && $_POST['groups_action'] == 'leave' ) {
573 $submitted = true;
574 if ( !wp_verify_nonce( $_POST[$nonce], $nonce_action ) ) {
575 $invalid_nonce = true;
576 }
577 }
578 if ( $submitted && !$invalid_nonce ) {
579 // remove user from group
580 if ( isset( $_POST['group_id'] ) ) {
581 $leave_group = Groups_Group::read( $_POST['group_id'] );
582 Groups_User_Group::delete( $user_id, $leave_group->group_id );
583 }
584 }
585 if ( Groups_User_Group::read( $user_id, $current_group->group_id ) ) {
586 $submit_text = sprintf( $options['submit_text'], wp_filter_nohtml_kses( $current_group->name ) );
587 $output .= '<div class="groups-join">';
588 $output .= '<form action="#" method="post">';
589 $output .= '<input type="hidden" name="groups_action" value="leave" />';
590 $output .= '<input type="hidden" name="group_id" value="' . esc_attr( $current_group->group_id ) . '" />';
591 $output .= '<input type="submit" value="' . $submit_text . '" />';
592 $output .= wp_nonce_field( $nonce_action, $nonce, true, false );
593 $output .= '</form>';
594 $output .= '</div>';
595 } else if ( $display_message ) {
596 if ( $submitted && !$invalid_nonce && isset( $leave_group ) && $leave_group->group_id === $current_group->group_id ) {
597 $output .= '<div class="groups-join left">';
598 $output .= sprintf( __( 'You have left the %s group.', GROUPS_PLUGIN_DOMAIN ), wp_filter_nohtml_kses( $leave_group->name ) );
599 $output .= '</div>';
600 }
601 }
602 }
603 }
604 return $output;
605 }
606 }
607 Groups_Shortcodes::init();
608