PluginProbe
Groups – Memberships and Access Control / trunk
Groups – Memberships and Access Control vtrunk
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 1.13.1 1.2.0 All 129 releases
groups / lib / access / class-groups-access-shortcodes.php

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

198 lines 5.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-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 $output = '';
58 $options = shortcode_atts( array( 'group' => '' ), $atts );
59 $show_content = false;
60 if ( $content !== null ) {
61 $groups_user = new Groups_User( get_current_user_id() );
62 $groups = explode( ',', $options['group'] );
63 foreach ( $groups as $group ) {
64 $group = addslashes( trim( $group ) );
65 $current_group = Groups_Group::read( $group );
66 if ( !$current_group ) {
67 $current_group = Groups_Group::read_by_name( $group );
68 }
69 if ( $current_group ) {
70 if ( $groups_user->is_member( $current_group->group_id ) ) {
71 $show_content = true;
72 break;
73 }
74 }
75 }
76 if ( $show_content ) {
77 remove_shortcode( 'groups_member' );
78 $content = do_shortcode( $content );
79 add_shortcode( 'groups_member', array( __CLASS__, 'groups_member' ) );
80 $output = $content;
81 }
82 }
83 return $output;
84 }
85
86 /**
87 * Takes one attribute "group" which is a comma-separated list of group
88 * names or ids (can be mixed).
89 *
90 * The content is shown if the current user does NOT belong to the group(s).
91 *
92 * @param array $atts attributes
93 * @param string $content content to render
94 *
95 * @return string
96 */
97 public static function groups_non_member( $atts, $content = null ) {
98 $output = '';
99 $options = shortcode_atts( array( 'group' => '' ), $atts );
100 $show_content = true;
101 if ( $content !== null ) {
102 $groups_user = new Groups_User( get_current_user_id() );
103 $groups = explode( ',', $options['group'] );
104 foreach ( $groups as $group ) {
105 $group = addslashes( trim( $group ) );
106 $current_group = Groups_Group::read( $group );
107 if ( !$current_group ) {
108 $current_group = Groups_Group::read_by_name( $group );
109 }
110 if ( $current_group ) {
111 if ( $groups_user->is_member( $current_group->group_id ) ) {
112 $show_content = false;
113 break;
114 }
115 }
116 }
117 if ( $show_content ) {
118 remove_shortcode( 'groups_non_member' );
119 $content = do_shortcode( $content );
120 add_shortcode( 'groups_non_member', array( __CLASS__, 'groups_non_member' ) );
121 $output = $content;
122 }
123 }
124 return $output;
125 }
126
127 /**
128 * Takes one attribute "capability" that must be a valid capability label
129 * or a list of capabilities separated by comma.
130 *
131 * The content is shown if the current user has one of the capabilities.
132 *
133 * @param array $atts attributes
134 * @param string $content content to render
135 *
136 * @return string
137 */
138 public static function groups_can( $atts, $content = null ) {
139 $output = '';
140 $options = shortcode_atts( array( 'capability' => '' ), $atts );
141 if ( $content !== null ) {
142 $groups_user = new Groups_User( get_current_user_id() );
143 $capability = $options['capability'];
144 $capabilities = array_map( 'trim', explode( ',', $capability ) );
145 $show_content = false;
146 foreach ( $capabilities as $capability ) {
147 if ( $groups_user->can( $capability ) ) {
148 $show_content = true;
149 break;
150 }
151 }
152 if ( $show_content ) {
153 remove_shortcode( 'groups_can' );
154 $content = do_shortcode( $content );
155 add_shortcode( 'groups_can', array( __CLASS__, 'groups_can' ) );
156 $output = $content;
157 }
158 }
159 return $output;
160 }
161
162 /**
163 * Takes one attribute "capability" that must be a valid capability label,
164 * or a comma-separated list of those.
165 *
166 * The content is shown if the current user has none of the capabilities.
167 *
168 * @param array $atts attributes
169 * @param string $content content to render
170 *
171 * @return string
172 */
173 public static function groups_can_not( $atts, $content = null ) {
174 $output = '';
175 $options = shortcode_atts( array( 'capability' => '' ), $atts );
176 if ( $content !== null ) {
177 $groups_user = new Groups_User( get_current_user_id() );
178 $capability = $options['capability'];
179 $capabilities = array_map( 'trim', explode( ',', $capability ) );
180 $show_content = true;
181 foreach ( $capabilities as $capability ) {
182 if ( $groups_user->can( $capability ) ) {
183 $show_content = false;
184 break;
185 }
186 }
187 if ( $show_content ) {
188 remove_shortcode( 'groups_can_not' );
189 $content = do_shortcode( $content );
190 add_shortcode( 'groups_can_not', array( __CLASS__, 'groups_can_not' ) );
191 $output = $content;
192 }
193 }
194 return $output;
195 }
196 }
197 Groups_Access_Shortcodes::init();
198