PluginProbe
Groups – Memberships and Access Control / 3.9.0
Groups – Memberships and Access Control v3.9.0
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 / blocks / src / class-groups-blocks.php

class-groups-blocks.php in Groups – Memberships and Access Control 3.9.0, at lib/blocks/src/class-groups-blocks.php

302 lines 7.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-blocks.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 Denitsa Slavcheva
18 * @package groups
19 * @since groups 2.8.0
20 */
21
22 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 require_once GROUPS_ACCESS_LIB . '/class-groups-access-meta-boxes.php';
27
28 /**
29 * In charge of registering, controlling and rendering Groups' blocks.
30 */
31 class Groups_Blocks {
32
33 /**
34 * Adds handlers to register blocks, REST and block categories.
35 */
36 public static function init() {
37 add_action( 'init', array( __CLASS__, 'groups_blocks_block_init' ) );
38 add_action( 'rest_api_init', array( __CLASS__, 'groups_rest' ) );
39 // @since 2.14.0 check for this function which is available since WordPress 5.8.0; as of then, the block_categories filter is deprecated and block_categories_all should be used instead
40 if ( function_exists( 'get_default_block_categories' ) ) {
41 add_filter( 'block_categories_all', array( __CLASS__, 'block_categories_all' ), 10, 2 );
42 } else {
43 add_filter( 'block_categories', array( __CLASS__, 'groups_block_categories' ), 10, 2 );
44 }
45 }
46
47 /**
48 * Create the REST API endpoints.
49 */
50 public static function groups_rest() {
51 register_rest_route(
52 // namespace - TODO version portion, change when merging to Groups plugin.
53 'groups/groups-blocks',
54 // resource path
55 '/groups',
56 array(
57 // Get the list of existing groups.
58 array(
59 'methods' => 'GET',
60 'callback' => array( __CLASS__, 'get_groups' ),
61 // Restrict access for the endpoint only to users that can administrate groups restrictions.
62 'permission_callback' => function () {
63 return Groups_Access_Meta_Boxes::user_can_restrict(); // nosemgrep: scanner.php.wp.security.rest-route.permission-callback.incorrect-return
64 },
65 ),
66 )
67 );
68 }
69
70 /**
71 * Callback function to retrieve existing groups.
72 *
73 * @return array
74 */
75 public static function get_groups() {
76 $groups_options = array();
77 if ( Groups_Access_Meta_Boxes::user_can_restrict() ) {
78 $include = Groups_Access_Meta_Boxes::get_user_can_restrict_group_ids();
79 $groups = Groups_Group::get_groups(
80 array(
81 'order_by' => 'name',
82 'order' => 'ASC',
83 'include' => $include,
84 )
85 );
86 foreach ( $groups as $group ) {
87 $groups_options[] = array(
88 'value' => $group->group_id,
89 'label' => $group->name ? stripslashes( wp_filter_nohtml_kses( $group->name ) ) : '',
90 );
91 }
92 } else {
93 $groups_options = esc_html__( 'You cannot set any access restrictions.', 'groups' );
94 }
95 return $groups_options;
96 }
97
98 /**
99 * Register the Groups block category.
100 *
101 * @since 2.14.0
102 *
103 * @param array $block_categories
104 * @param WP_Block_Editor_Context $block_editor_context
105 *
106 * @return array
107 */
108 public static function block_categories_all( $block_categories, $block_editor_context ) {
109 $block_categories = array_merge(
110 $block_categories,
111 array(
112 array(
113 'slug' => 'groups',
114 'title' => 'Groups' // do NOT translate
115 )
116 )
117 );
118 return $block_categories;
119 }
120
121 /**
122 * Adds a new block category for 'groups' in the block editor.
123 *
124 * @deprecated as of 2.14.0 with WordPress 5.8.0 using the block_categories_all filter instead
125 *
126 * @param array $categories Array of block categories.
127 * @param WP_Post $post Post being loaded.
128 *
129 * @return array
130 */
131 public static function groups_block_categories( $categories, $post ) {
132 $categories = array_merge(
133 $categories,
134 array(
135 array(
136 'slug' => 'groups',
137 'title' => 'Groups',
138 ),
139 )
140 );
141 return $categories;
142 }
143
144 /**
145 * Registers our blocks.
146 */
147 public static function groups_blocks_block_init() {
148 // Skip block registration if Gutenberg is not enabled/merged.
149 if ( ! function_exists( 'register_block_type' ) ) {
150 return;
151 }
152
153 $asset_file = include GROUPS_BLOCKS_LIB . '/build/index.asset.php';
154
155 $editor_dependencies = array_merge(
156 $asset_file['dependencies'],
157 array()
158 );
159
160 // @todo if 'wp-edit-widgets' or 'wp-customize-widgets' script then don't use wp-editor ... so ?
161 // Scripts.
162 wp_register_script(
163 'groups_blocks-block-js', // Handle.
164 GROUPS_PLUGIN_URL . 'lib/blocks/build/index.js',
165 $editor_dependencies,
166 GROUPS_CORE_VERSION
167 );
168
169 wp_set_script_translations(
170 'groups_blocks-block-js',
171 'groups'
172 );
173
174 // Frontend Styles - currently none required.
175 // wp_register_style(
176 // 'groups_blocks-style-css', // Handle.
177 // GROUPS_PLUGIN_URL . 'lib/blocks/dist/blocks.style.build.css',
178 // array(), // Dependency to include the CSS after it.
179 // GROUPS_CORE_VERSION
180 // );
181
182 // Editor Styles.
183 wp_register_style(
184 'groups_blocks-block-editor-css', // Handle.
185 GROUPS_PLUGIN_URL . 'lib/blocks/build/index.css',
186 array( 'wp-edit-blocks' ), // Dependency to include the CSS after it.
187 GROUPS_CORE_VERSION
188 );
189 register_block_type(
190 'groups/groups-member',
191 array(
192 'editor_script' => 'groups_blocks-block-js',
193 'editor_style' => 'groups_blocks-block-editor-css',
194 'style' => 'groups_blocks-style-css',
195 'render_callback' => array( __CLASS__, 'groups_member_render_content' ),
196 )
197 );
198 register_block_type(
199 'groups/groups-non-member',
200 array(
201 'editor_script' => 'groups_blocks-block-js',
202 'editor_style' => 'groups_blocks-block-editor-css',
203 'style' => 'groups_blocks-style-css',
204 'render_callback' => array( __CLASS__, 'groups_non_member_render_content' ),
205 )
206 );
207 }
208
209 /**
210 * Rendering callback for our Groups Member block.
211 *
212 * @param array $attributes
213 * @param string $content
214 *
215 * @return string
216 */
217 public static function groups_member_render_content( $attributes, $content ) {
218
219 $output = '';
220 $show_content = false;
221 $selected_groups = array();
222
223 if ( isset( $attributes['groups_select'] ) ) {
224 $decoded_groups = json_decode( $attributes['groups_select'] );
225 if ( ! empty( $decoded_groups ) ) {
226 foreach ( $decoded_groups as $group ) {
227 $selected_groups[] = $group->value;
228 }
229 }
230 }
231
232 $groups_user = new Groups_User( get_current_user_id() );
233 foreach ( $selected_groups as $group ) {
234 $current_group = Groups_Group::read( $group );
235 if ( ! $current_group ) {
236 $current_group = Groups_Group::read_by_name( $group );
237 }
238
239 if ( $current_group ) {
240 if ( $groups_user->is_member( $current_group->group_id ) ) {
241 $show_content = true;
242 break;
243 }
244 }
245 }
246
247 if ( $show_content ) {
248 $output = '<div class="groups-member-block-content">' . $content . '</div>';
249 }
250
251 return $output;
252 }
253
254 /**
255 * Rendering callback for our Groups Non-member block.
256 *
257 * @param array $attributes
258 * @param string $content
259 *
260 * @return string
261 */
262 public static function groups_non_member_render_content( $attributes, $content ) {
263
264 $output = '';
265 $show_content = true;
266 $selected_groups = array();
267
268 if ( isset( $attributes['groups_select'] ) ) {
269 $decoded_groups = json_decode( $attributes['groups_select'] );
270 if ( ! empty( $decoded_groups ) ) {
271 foreach ( $decoded_groups as $group ) {
272 $selected_groups[] = $group->value;
273 }
274 }
275 }
276
277 $groups_user = new Groups_User( get_current_user_id() );
278 foreach ( $selected_groups as $group ) {
279 $current_group = Groups_Group::read( $group );
280 if ( ! $current_group ) {
281 $current_group = Groups_Group::read_by_name( $group );
282 }
283
284 if ( $current_group ) {
285 if ( $groups_user->is_member( $current_group->group_id ) ) {
286 $show_content = false;
287 break;
288 }
289 }
290 }
291
292 if ( $show_content ) {
293 $output = '<div class="groups-non-member-block-content">' . $content . '</div>';
294 }
295
296 return $output;
297 }
298
299 }
300
301 Groups_Blocks::init();
302