PluginProbe
Groups – Memberships and Access Control / 4.7.0
Groups – Memberships and Access Control v4.7.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 4.7.0, at lib/blocks/src/class-groups-blocks.php

300 lines 7.8 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 // Provide groups.
52 // Access to the endpoint is restricted to users that can administer Groups restrictions.
53 register_rest_route(
54 'groups/groups-blocks', // namespace
55 '/groups', // route
56 array(
57 array(
58 'methods' => 'GET',
59 'callback' => array( __CLASS__, 'get_groups' ),
60 'permission_callback' => function () {
61 return Groups_Access_Meta_Boxes::user_can_restrict(); // nosemgrep: scanner.php.wp.security.rest-route.permission-callback.incorrect-return
62 },
63 ),
64 )
65 );
66 }
67
68 /**
69 * Callback function to retrieve existing groups.
70 *
71 * @return array
72 */
73 public static function get_groups() {
74 $groups_options = array();
75 if ( Groups_Access_Meta_Boxes::user_can_restrict() ) {
76 $include = Groups_Access_Meta_Boxes::get_user_can_restrict_group_ids();
77 $groups = Groups_Group::get_groups(
78 array(
79 'order_by' => 'name',
80 'order' => 'ASC',
81 'include' => $include,
82 )
83 );
84 foreach ( $groups as $group ) {
85 $groups_options[] = array(
86 'value' => $group->group_id,
87 'label' => $group->name ? stripslashes( wp_filter_nohtml_kses( $group->name ) ) : '',
88 );
89 }
90 }
91 return $groups_options;
92 }
93
94 /**
95 * Register the Groups block category.
96 *
97 * @since 2.14.0
98 *
99 * @param array $block_categories
100 * @param WP_Block_Editor_Context $block_editor_context
101 *
102 * @return array
103 */
104 public static function block_categories_all( $block_categories, $block_editor_context ) {
105 $block_categories = array_merge(
106 $block_categories,
107 array(
108 array(
109 'slug' => 'groups',
110 'title' => 'Groups' // do NOT translate
111 )
112 )
113 );
114 return $block_categories;
115 }
116
117 /**
118 * Adds a new block category for 'groups' in the block editor.
119 *
120 * @deprecated as of 2.14.0 with WordPress 5.8.0 using the block_categories_all filter instead
121 *
122 * @param array $categories Array of block categories.
123 * @param WP_Post $post Post being loaded.
124 *
125 * @return array
126 */
127 public static function groups_block_categories( $categories, $post ) {
128 $categories = array_merge(
129 $categories,
130 array(
131 array(
132 'slug' => 'groups',
133 'title' => 'Groups',
134 ),
135 )
136 );
137 return $categories;
138 }
139
140 /**
141 * Registers our blocks.
142 */
143 public static function groups_blocks_block_init() {
144 // Skip block registration if Gutenberg is not enabled/merged.
145 if ( ! function_exists( 'register_block_type' ) ) {
146 return;
147 }
148
149 $asset_file = include GROUPS_BLOCKS_LIB . '/build/index.asset.php';
150
151 $editor_dependencies = array_merge(
152 $asset_file['dependencies'],
153 array()
154 );
155
156 // Scripts.
157 wp_register_script( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
158 'groups_blocks-block-js', // Handle.
159 GROUPS_PLUGIN_URL . 'lib/blocks/build/index.js',
160 $editor_dependencies,
161 GROUPS_CORE_VERSION
162 );
163
164 wp_set_script_translations(
165 'groups_blocks-block-js',
166 'groups',
167 GROUPS_CORE_DIR . '/languages/js' // @since 3.10.0
168 );
169
170 // Frontend Styles - currently none required.
171 // wp_register_style(
172 // 'groups_blocks-style-css', // Handle.
173 // GROUPS_PLUGIN_URL . 'lib/blocks/dist/blocks.style.build.css',
174 // array(), // Dependency to include the CSS after it.
175 // GROUPS_CORE_VERSION
176 // );
177
178 // Editor Styles.
179 wp_register_style(
180 'groups_blocks-block-editor-css', // Handle.
181 GROUPS_PLUGIN_URL . 'lib/blocks/build/index.css',
182 array( 'wp-edit-blocks' ), // Dependency to include the CSS after it.
183 GROUPS_CORE_VERSION
184 );
185 register_block_type(
186 'groups/groups-member',
187 array(
188 'api_version' => '3',
189 'editor_script' => 'groups_blocks-block-js',
190 'editor_style' => 'groups_blocks-block-editor-css',
191 'style' => 'groups_blocks-style-css',
192 'render_callback' => array( __CLASS__, 'groups_member_render_content' ),
193 )
194 );
195 register_block_type(
196 'groups/groups-non-member',
197 array(
198 'api_version' => '3',
199 'editor_script' => 'groups_blocks-block-js',
200 'editor_style' => 'groups_blocks-block-editor-css',
201 'style' => 'groups_blocks-style-css',
202 'render_callback' => array( __CLASS__, 'groups_non_member_render_content' ),
203 )
204 );
205 }
206
207 /**
208 * Rendering callback for our Groups Member block.
209 *
210 * @param array $attributes
211 * @param string $content
212 *
213 * @return string
214 */
215 public static function groups_member_render_content( $attributes, $content ) {
216
217 $output = '';
218 $show_content = false;
219 $selected_groups = array();
220
221 if ( isset( $attributes['groups_select'] ) ) {
222 $decoded_groups = json_decode( $attributes['groups_select'] );
223 if ( ! empty( $decoded_groups ) ) {
224 foreach ( $decoded_groups as $group ) {
225 $selected_groups[] = $group->value;
226 }
227 }
228 }
229
230 $groups_user = new Groups_User( get_current_user_id() );
231 foreach ( $selected_groups as $group ) {
232 $current_group = Groups_Group::read( $group );
233 if ( ! $current_group ) {
234 $current_group = Groups_Group::read_by_name( $group );
235 }
236
237 if ( $current_group ) {
238 if ( $groups_user->is_member( $current_group->group_id ) ) {
239 $show_content = true;
240 break;
241 }
242 }
243 }
244
245 if ( $show_content ) {
246 $output = '<div class="groups-member-block-content">' . $content . '</div>';
247 }
248
249 return $output;
250 }
251
252 /**
253 * Rendering callback for our Groups Non-member block.
254 *
255 * @param array $attributes
256 * @param string $content
257 *
258 * @return string
259 */
260 public static function groups_non_member_render_content( $attributes, $content ) {
261
262 $output = '';
263 $show_content = true;
264 $selected_groups = array();
265
266 if ( isset( $attributes['groups_select'] ) ) {
267 $decoded_groups = json_decode( $attributes['groups_select'] );
268 if ( ! empty( $decoded_groups ) ) {
269 foreach ( $decoded_groups as $group ) {
270 $selected_groups[] = $group->value;
271 }
272 }
273 }
274
275 $groups_user = new Groups_User( get_current_user_id() );
276 foreach ( $selected_groups as $group ) {
277 $current_group = Groups_Group::read( $group );
278 if ( ! $current_group ) {
279 $current_group = Groups_Group::read_by_name( $group );
280 }
281
282 if ( $current_group ) {
283 if ( $groups_user->is_member( $current_group->group_id ) ) {
284 $show_content = false;
285 break;
286 }
287 }
288 }
289
290 if ( $show_content ) {
291 $output = '<div class="groups-non-member-block-content">' . $content . '</div>';
292 }
293
294 return $output;
295 }
296
297 }
298
299 Groups_Blocks::init();
300