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

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