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

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