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

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

160 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class-groups-access-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 class Groups_Access_Shortcodes {
22
23 /**
24 * Defines access shortcodes.
25 */
26 public static function init() {
27
28 // group restrictions
29 add_shortcode( 'groups_member', array( __CLASS__, 'groups_member' ) );
30 add_shortcode( 'groups_non_member', array( __CLASS__, 'groups_non_member' ) );
31
32 // capabilities
33 add_shortcode( 'groups_can', array( __CLASS__, 'groups_can' ) );
34 add_shortcode( 'groups_can_not', array( __CLASS__, 'groups_can_not' ) );
35 }
36
37 /**
38 * Takes one attribute "group" which is a comma-separated list of group
39 * names or ids (can be mixed).
40 * The content is shown if the current user belongs to the group(s).
41 *
42 * @param array $atts attributes
43 * @param string $content content to render
44 */
45 public static function groups_member( $atts, $content = null ) {
46 $output = "";
47 $options = shortcode_atts( array( "group" => "" ), $atts );
48 $show_content = false;
49 if ( $content !== null ) {
50 $groups_user = new Groups_User( get_current_user_id() );
51 $groups = explode( ",", $options['group'] );
52 foreach ( $groups as $group ) {
53 $group = trim( $group );
54 $current_group = Groups_Group::read( $group );
55 if ( !$current_group ) {
56 $current_group = Groups_Group::read_by_name( $group );
57 }
58 if ( $current_group ) {
59 if ( Groups_User_Group::read( $groups_user->user->ID , $current_group->group_id ) ) {
60 $show_content = true;
61 break;
62 }
63 }
64 }
65 if ( $show_content ) {
66 remove_shortcode( 'groups_member' );
67 $content = do_shortcode( $content );
68 add_shortcode( 'groups_member', array( __CLASS__, 'groups_member' ) );
69 $output = $content;
70 }
71 }
72 return $output;
73 }
74
75 /**
76 * Takes one attribute "group" which is a comma-separated list of group
77 * names or ids (can be mixed).
78 * The content is shown if the current user does NOT belong to the group(s).
79 *
80 * @param array $atts attributes
81 * @param string $content content to render
82 */
83 public static function groups_non_member( $atts, $content = null ) {
84 $output = "";
85 $options = shortcode_atts( array( "group" => "" ), $atts );
86 $show_content = true;
87 if ( $content !== null ) {
88 $groups_user = new Groups_User( get_current_user_id() );
89 $groups = explode( ",", $options['group'] );
90 foreach ( $groups as $group ) {
91 $group = trim( $group );
92 $current_group = Groups_Group::read( $group );
93 if ( !$current_group ) {
94 $current_group = Groups_Group::read_by_name( $group );
95 }
96 if ( $current_group ) {
97 if ( Groups_User_Group::read( $groups_user->user->ID , $current_group->group_id ) ) {
98 $show_content = false;
99 break;
100 }
101 }
102 }
103 if ( $show_content ) {
104 remove_shortcode( 'groups_non_member' );
105 $content = do_shortcode( $content );
106 add_shortcode( 'groups_non_member', array( __CLASS__, 'groups_non_member' ) );
107 $output = $content;
108 }
109 }
110 return $output;
111 }
112
113 /**
114 * Takes one attribute "capability" that must be a valid capability label.
115 * The content is shown if the current user has the capability.
116 *
117 * @param array $atts attributes
118 * @param string $content content to render
119 */
120 public static function groups_can( $atts, $content = null ) {
121 $output = "";
122 $options = shortcode_atts( array( "capability" => "" ), $atts );
123 if ( $content !== null ) {
124 $groups_user = new Groups_User( get_current_user_id() );
125 $capability = $options['capability'];
126 if ( $groups_user->can( $capability ) ) {
127 remove_shortcode( 'groups_can' );
128 $content = do_shortcode( $content );
129 add_shortcode( 'groups_can', array( __CLASS__, 'groups_can' ) );
130 $output = $content;
131 }
132 }
133 return $output;
134 }
135
136 /**
137 * Takes one attribute "capability" that must be a valid capability label.
138 * The content is shown if the current user does NOT have the capability.
139 *
140 * @param array $atts attributes
141 * @param string $content content to render
142 */
143 public static function groups_can_not( $atts, $content = null ) {
144 $output = "";
145 $options = shortcode_atts( array( "capability" => "" ), $atts );
146 if ( $content !== null ) {
147 $groups_user = new Groups_User( get_current_user_id() );
148 $capability = $options['capability'];
149 if ( !$groups_user->can( $capability ) ) {
150 remove_shortcode( 'groups_can_not' );
151 $content = do_shortcode( $content );
152 add_shortcode( 'groups_can_not', array( __CLASS__, 'groups_can_not' ) );
153 $output = $content;
154 }
155 }
156 return $output;
157 }
158 }
159 Groups_Access_Shortcodes::init();
160