PluginProbe
Groups – Memberships and Access Control / 4.7.1
Groups – Memberships and Access Control v4.7.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 / access / class-groups-access-shortcodes.php

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

218 lines 6.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-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
22 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 /**
27 * Shortcode handlers.
28 */
29 class Groups_Access_Shortcodes {
30
31 /**
32 * Defines access shortcodes.
33 */
34 public static function init() {
35
36 // group restrictions
37 add_shortcode( 'groups_member', array( __CLASS__, 'groups_member' ) );
38 add_shortcode( 'groups_non_member', array( __CLASS__, 'groups_non_member' ) );
39
40 // capabilities
41 add_shortcode( 'groups_can', array( __CLASS__, 'groups_can' ) );
42 add_shortcode( 'groups_can_not', array( __CLASS__, 'groups_can_not' ) );
43 }
44
45 /**
46 * Takes one attribute "group" which is a comma-separated list of group
47 * names or ids (can be mixed).
48 *
49 * The content is shown if the current user belongs to the group(s).
50 *
51 * @param array $atts attributes
52 * @param string $content content to render
53 *
54 * @return string
55 */
56 public static function groups_member( $atts, $content = null ) {
57
58 if ( !Groups_Shortcodes::validate( 'groups_member', $atts, $content ) ) {
59 return '';
60 }
61
62 $output = '';
63 $options = shortcode_atts( array( 'group' => '' ), $atts );
64 $show_content = false;
65 if ( $content !== null ) {
66 $groups_user = new Groups_User( get_current_user_id() );
67 $groups = explode( ',', $options['group'] );
68 foreach ( $groups as $group ) {
69 $group = addslashes( trim( $group ) );
70 $current_group = Groups_Group::read( $group );
71 if ( !$current_group ) {
72 $current_group = Groups_Group::read_by_name( $group );
73 }
74 if ( $current_group ) {
75 if ( $groups_user->is_member( $current_group->group_id ) ) {
76 $show_content = true;
77 break;
78 }
79 }
80 }
81 if ( $show_content ) {
82 remove_shortcode( 'groups_member' );
83 $content = do_shortcode( $content );
84 add_shortcode( 'groups_member', array( __CLASS__, 'groups_member' ) );
85 $output = $content;
86 }
87 }
88 return $output;
89 }
90
91 /**
92 * Takes one attribute "group" which is a comma-separated list of group
93 * names or ids (can be mixed).
94 *
95 * The content is shown if the current user does NOT belong to the group(s).
96 *
97 * @param array $atts attributes
98 * @param string $content content to render
99 *
100 * @return string
101 */
102 public static function groups_non_member( $atts, $content = null ) {
103
104 if ( !Groups_Shortcodes::validate( 'groups_non_member', $atts, $content ) ) {
105 return '';
106 }
107
108 $output = '';
109 $options = shortcode_atts( array( 'group' => '' ), $atts );
110 $show_content = true;
111 if ( $content !== null ) {
112 $groups_user = new Groups_User( get_current_user_id() );
113 $groups = explode( ',', $options['group'] );
114 foreach ( $groups as $group ) {
115 $group = addslashes( trim( $group ) );
116 $current_group = Groups_Group::read( $group );
117 if ( !$current_group ) {
118 $current_group = Groups_Group::read_by_name( $group );
119 }
120 if ( $current_group ) {
121 if ( $groups_user->is_member( $current_group->group_id ) ) {
122 $show_content = false;
123 break;
124 }
125 }
126 }
127 if ( $show_content ) {
128 remove_shortcode( 'groups_non_member' );
129 $content = do_shortcode( $content );
130 add_shortcode( 'groups_non_member', array( __CLASS__, 'groups_non_member' ) );
131 $output = $content;
132 }
133 }
134 return $output;
135 }
136
137 /**
138 * Takes one attribute "capability" that must be a valid capability label
139 * or a list of capabilities separated by comma.
140 *
141 * The content is shown if the current user has one of the capabilities.
142 *
143 * @param array $atts attributes
144 * @param string $content content to render
145 *
146 * @return string
147 */
148 public static function groups_can( $atts, $content = null ) {
149
150 if ( !Groups_Shortcodes::validate( 'groups_can', $atts, $content ) ) {
151 return '';
152 }
153
154 $output = '';
155 $options = shortcode_atts( array( 'capability' => '' ), $atts );
156 if ( $content !== null ) {
157 $groups_user = new Groups_User( get_current_user_id() );
158 $capability = $options['capability'];
159 $capabilities = array_map( 'trim', explode( ',', $capability ) );
160 $show_content = false;
161 foreach ( $capabilities as $capability ) {
162 if ( $groups_user->can( $capability ) ) {
163 $show_content = true;
164 break;
165 }
166 }
167 if ( $show_content ) {
168 remove_shortcode( 'groups_can' );
169 $content = do_shortcode( $content );
170 add_shortcode( 'groups_can', array( __CLASS__, 'groups_can' ) );
171 $output = $content;
172 }
173 }
174 return $output;
175 }
176
177 /**
178 * Takes one attribute "capability" that must be a valid capability label,
179 * or a comma-separated list of those.
180 *
181 * The content is shown if the current user has none of the capabilities.
182 *
183 * @param array $atts attributes
184 * @param string $content content to render
185 *
186 * @return string
187 */
188 public static function groups_can_not( $atts, $content = null ) {
189
190 if ( !Groups_Shortcodes::validate( 'groups_can_not', $atts, $content ) ) {
191 return '';
192 }
193
194 $output = '';
195 $options = shortcode_atts( array( 'capability' => '' ), $atts );
196 if ( $content !== null ) {
197 $groups_user = new Groups_User( get_current_user_id() );
198 $capability = $options['capability'];
199 $capabilities = array_map( 'trim', explode( ',', $capability ) );
200 $show_content = true;
201 foreach ( $capabilities as $capability ) {
202 if ( $groups_user->can( $capability ) ) {
203 $show_content = false;
204 break;
205 }
206 }
207 if ( $show_content ) {
208 remove_shortcode( 'groups_can_not' );
209 $content = do_shortcode( $content );
210 add_shortcode( 'groups_can_not', array( __CLASS__, 'groups_can_not' ) );
211 $output = $content;
212 }
213 }
214 return $output;
215 }
216 }
217 Groups_Access_Shortcodes::init();
218