PluginProbe ʕ •ᴥ•ʔ
Widget Options – Advanced Conditional Visibility for Gutenberg Blocks & Classic Widgets / 4.2.4
Widget Options – Advanced Conditional Visibility for Gutenberg Blocks & Classic Widgets v4.2.4
4.2.5 4.2.4 trunk 3.7.10 3.7.11 3.7.12 3.7.13 3.7.14 3.7.2 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8 3.8.1 3.8.10 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.8.8 3.8.9 3.8.9.1 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.5.1 4.0.6 4.0.6.1 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.1.3 4.2.0 4.2.1 4.2.2 4.2.3
widget-options / includes / widgets / gutenberg / gutenberg-toolbar.php
widget-options / includes / widgets / gutenberg Last commit date
build 2 months ago gutenberg-toolbar.php 2 months ago
gutenberg-toolbar.php
1357 lines
1 <?php
2
3 /**
4 * Add Widget Options in Gutenberg
5 *
6 * Process Managing of Widget Options.
7 *
8 * @copyright Copyright (c) 2023, Boholweb WP
9 * @since 5.0.1
10 */
11
12 use Automattic\Jetpack\Sync\Functions;
13
14 if (!defined('ABSPATH')) exit; // Exit if accessed directly
15
16 function widgetopts_toolbar_scripts()
17 {
18 global $widget_options, $pagenow;
19
20 if (($pagenow != 'widgets.php' && $pagenow != 'customize.php') &&
21 (!isset($widget_options["hide_page_and_post_block"]) || $widget_options["hide_page_and_post_block"] != "activate") ||
22 ($widget_options["hide_page_and_post_block"] == "activate" && isset($widget_options["settings"]) &&
23 isset($widget_options["settings"]["hide_page_and_post_block"]) &&
24 $widget_options["settings"]["hide_page_and_post_block"]["page_and_post_block"] == "1")
25 ) {
26 //do nothing
27 } else {
28 wp_register_script(
29 'widgetopts-gutenberg-toolbar',
30 WIDGETOPTS_PLUGIN_URL . 'includes/widgets/gutenberg/build/index.js',
31 ['wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-block-editor'],
32 filemtime(WIDGETOPTS_PLUGIN_DIR . 'includes/widgets/gutenberg/build/index.js')
33 );
34 wp_enqueue_script('widgetopts-gutenberg-toolbar');
35 wp_localize_script('widgetopts-gutenberg-toolbar', 'widgetoptsGutenberg', array(
36 'ajaxurl' => admin_url('admin-ajax.php'),
37 ));
38 }
39 }
40 add_action('enqueue_block_editor_assets', 'widgetopts_toolbar_scripts');
41
42 function widgetopts_check_widget_editor_referer($customizer = false)
43 {
44 // Check if the HTTP referer is set
45 if (isset($_SERVER['HTTP_REFERER'])) {
46 // Parse the referer URL
47 $referer_url = parse_url($_SERVER['HTTP_REFERER']);
48
49 $path = $customizer === true ? '/wp-admin/customize.php' : '/wp-admin/widgets.php';
50 // Check if the referer URL is from the widget editor
51 if (strpos($referer_url['path'], $path) !== false) {
52 // The request likely came from the widget editor
53 return true;
54 }
55 }
56
57 return false;
58 }
59
60 add_action('rest_api_init', function () {
61 global $wp_widget_factory;
62
63 if (isset($wp_widget_factory) && isset($wp_widget_factory->widgets)) {
64 // Modify the show_instance_in_rest option for each registered widget type
65 foreach ($wp_widget_factory->widgets as $widget) {
66 if (isset($widget->widget_options) && is_array($widget->widget_options)) {
67 // if (stristr($widget->id, 'tribe-widget-events-list')) {
68 $widget->widget_options['show_instance_in_rest'] = true;
69 // }
70 }
71 }
72 }
73 }, 999);
74
75 add_filter('register_block_type_args', function ($args, $name) {
76 global $orig_callback;
77 $plugin_names = apply_filters('widget_options_type_string_attribute', ['luckywp/tableofcontents']);
78 if (in_array($name, $plugin_names)) {
79 $args['attributes']['extended_widget_opts_block'] = array(
80 'type' => 'string',
81 'default' => json_encode((object)[])
82 );
83
84 $args['attributes']['extended_widget_opts'] = array(
85 'type' => 'string',
86 'default' => json_encode((object)[])
87 );
88 } else if ($name == 'events-calendar-shortcode/block') {
89 $args['attributes']['extended_widget_opts_block'] = array(
90 'type' => 'object'
91 );
92
93 $args['attributes']['extended_widget_opts'] = array(
94 'type' => 'object'
95 );
96
97 if (isset($args['render_callback'])) {
98 $orig_callback = $args['render_callback'];
99 $args['render_callback'] = function ($attributes) {
100 global $orig_callback;
101 if (isset($attributes['extended_widget_opts'])) {
102 $attributes['extended_widget_opts'] = json_encode($attributes['extended_widget_opts']);
103 }
104
105 if (isset($attributes['extended_widget_opts_block'])) {
106 $attributes['extended_widget_opts_block'] = json_encode($attributes['extended_widget_opts_block']);
107 }
108
109 if (function_exists('ecs_render_block')) {
110 return ecs_render_block($attributes);
111 }
112 return call_user_func($orig_callback, $attributes);
113 };
114 }
115 } else if (stripos($name, 'jetpack') !== false) {
116 $args['attributes']['extended_widget_opts_block'] = array(
117 'type' => 'object',
118 // 'default' => (object)[]
119 );
120
121 $args['attributes']['extended_widget_opts'] = array(
122 'type' => 'object',
123 // 'default' => (object)[]
124 );
125 } else {
126 //if block type is not luckywp/tableofcontents use type object
127 $args['attributes']['extended_widget_opts_block'] = array(
128 'type' => 'object',
129 'default' => (object)[]
130 );
131
132 $args['attributes']['extended_widget_opts'] = array(
133 'type' => 'object',
134 'default' => (object)[]
135 );
136 }
137
138 $args['attributes']['extended_widget_opts_state'] = array(
139 'type' => 'string',
140 'default' => ''
141 );
142
143 $args['attributes']['extended_widget_opts_clientid'] = array(
144 'type' => 'string',
145 'default' => ''
146 );
147
148 $args['attributes']['dateUpdated'] = array(
149 'type' => 'string',
150 'default' => ''
151 );
152
153 return $args;
154 }, 50, 2);
155
156 add_filter('widget_types_to_hide_from_legacy_widget_block', function ($notAllowed) {
157 return array();
158 }, 99999, 1);
159
160 if (wp_use_widgets_block_editor()) {
161 add_filter('widget_update_callback', function ($instance, $new_instance, $old_instance, $obj) {
162
163 $base = explode('-', $obj->id);
164 //this is for block widget saving
165 if (stristr($base[0], 'block') || $base[0] == 'block') {
166 //remove widgetopts attribute from blocks when it is classic editor
167 if (!empty($new_instance['content'])) {
168 $block = parse_blocks($new_instance['content']);
169 if (!empty($block[0]) && !empty($block[0]['attrs'])) {
170 if (!empty($block[0]['attrs']['extended_widget_opts_block'])) {
171 $instance['extended_widget_opts-' . $obj->id] = $block[0]['attrs']['extended_widget_opts_block'];
172 unset($block[0]['attrs']['extended_widget_opts_block']);
173 $instance['content'] = serialize_blocks($block);
174 }
175 }
176 }
177 } else {
178 //this is for legacy widget saving
179 //for first time saving of legacy widget, after this frontend will hanlde the saving in block editor
180 //for already exist widget
181 if (!empty($new_instance['extended_widget_opts-' . $obj->id])) {
182 $instance['extended_widget_opts-' . $obj->id] = $new_instance['extended_widget_opts-' . $obj->id];
183 }
184
185 //for new created widget
186 if (isset($new_instance['extended_widget_opts-undefined']) && empty($new_instance['extended_widget_opts-' . $obj->id])) {
187 $instance['extended_widget_opts-' . $obj->id] = $new_instance['extended_widget_opts-undefined'];
188
189 array_pop($base);
190 $new_base = implode('-', $base);
191
192 $instance['extended_widget_opts-' . $obj->id]['id_base'] = $base === false ? '-1' : $new_base;
193 unset($new_instance['extended_widget_opts-undefined']);
194 if (isset($instance['extended_widget_opts-undefined'])) {
195 unset($instance['extended_widget_opts-undefined']);
196 }
197 }
198 }
199
200 if (isset($instance['extended_widget_opts-' . $obj->id]) && $instance['extended_widget_opts-' . $obj->id]) {
201 $instance['extended_widget_opts-' . $obj->id] = widgetopts_sanitize_array($instance['extended_widget_opts-' . $obj->id]);
202 }
203
204 // Legacy display logic: admins keep as-is, non-admins have it stripped
205 if (isset($instance['extended_widget_opts-' . $obj->id]['class'])) {
206 if (!current_user_can('manage_options')) {
207 $instance['extended_widget_opts-' . $obj->id]['class']['logic'] = '';
208 }
209 }
210
211 return $instance;
212 }, 100, 4);
213 }
214
215 add_filter('rest_pre_insert_post', 'widgetopts_rest_pre_insert', 10, 2);
216 add_filter('rest_pre_insert_page', 'widgetopts_rest_pre_insert', 10, 2);
217
218 function widgetopts_rest_pre_insert($post, $request)
219 {
220 if (!current_user_can('edit_posts')) {
221 return $post;
222 }
223
224 // Admins: don't touch legacy logic at all (no parse/serialize cycle)
225 if (current_user_can('manage_options')) {
226 return $post;
227 }
228
229 // Non-admins: strip legacy logic from all blocks
230 if (empty($post->post_content)) {
231 return $post;
232 }
233
234 if (strpos($post->post_content, 'extended_widget_opts') === false
235 && strpos($post->post_content, 'start_widgetopts') === false) {
236 return $post;
237 }
238
239 $new_blocks = parse_blocks($post->post_content);
240 if (!is_array($new_blocks) || empty($new_blocks)) {
241 return $post;
242 }
243
244 $changed = false;
245 widgetopts_strip_logic_from_blocks($new_blocks, $changed);
246 if ($changed) {
247 $post->post_content = serialize_blocks($new_blocks);
248 }
249
250 return $post;
251 }
252
253 // Recursively process blocks (both parent and nested inner blocks)
254 function widgetopt_process_blocks_recursively(&$blocks, &$old_blocks_lookup)
255 {
256 foreach ($blocks as &$block) {
257 if (!isset($block['blockName'])) {
258 continue; // Skip invalid blocks
259 }
260
261 // Use anchor as the unique identifier or generate one if not available
262 $anchor = $block['attrs']['anchor'] ?? md5(json_encode($block['innerContent'])); // Generate unique ID if missing
263
264 // Store or compare the block's attributes
265 $old_blocks_lookup[$anchor] = [
266 'attrs' => $block['attrs'] ?? [],
267 ];
268
269 // If the block has inner blocks, recurse through them
270 if (isset($block['innerBlocks']) && !empty($block['innerBlocks'])) {
271 widgetopt_process_blocks_recursively($block['innerBlocks'], $old_blocks_lookup); // Recursively process inner blocks
272 }
273 }
274 }
275
276 function widgetopt_modify_block_attributes(&$block, $old_blocks_lookup)
277 {
278 if (!isset($block['blockName'])) {
279 return;
280 }
281
282 if (isset($block['attrs']['extended_widget_opts']['class']['logic'])
283 && $block['attrs']['extended_widget_opts']['class']['logic'] !== '') {
284 $block['attrs']['extended_widget_opts']['class']['logic'] = '';
285 }
286
287 if (isset($block['attrs']['extended_widget_opts_block']['class']['logic'])
288 && $block['attrs']['extended_widget_opts_block']['class']['logic'] !== '') {
289 $block['attrs']['extended_widget_opts_block']['class']['logic'] = '';
290 }
291
292 if (isset($block['innerBlocks']) && !empty($block['innerBlocks'])) {
293 foreach ($block['innerBlocks'] as &$inner_block) {
294 widgetopt_modify_block_attributes($inner_block, $old_blocks_lookup);
295 }
296 }
297 }
298
299 /**
300 * Recursively strip legacy logic from parsed blocks.
301 * Used for non-admin users to prevent logic injection.
302 *
303 * @param array &$blocks Parsed blocks array.
304 * @param bool &$changed Set to true if any logic was stripped.
305 */
306 function widgetopts_strip_logic_from_blocks(&$blocks, &$changed) {
307 foreach ($blocks as &$block) {
308 // Standard Gutenberg block attributes
309 if (isset($block['attrs']['extended_widget_opts']['class']['logic'])
310 && $block['attrs']['extended_widget_opts']['class']['logic'] !== '') {
311 $block['attrs']['extended_widget_opts']['class']['logic'] = '';
312 $changed = true;
313 }
314 if (isset($block['attrs']['extended_widget_opts_block']['class']['logic'])
315 && $block['attrs']['extended_widget_opts_block']['class']['logic'] !== '') {
316 $block['attrs']['extended_widget_opts_block']['class']['logic'] = '';
317 $changed = true;
318 }
319
320 // Legacy freeform format: <!--start_widgetopts {"class":{"logic":"..."}} end_widgetopts-->
321 // parse_blocks() stores this raw in innerContent (blockName = null, attrs = []),
322 // so the attribute checks above never fire for it.
323 if (empty($block['blockName']) && !empty($block['innerContent'])) {
324 foreach ($block['innerContent'] as &$chunk) {
325 if (!is_string($chunk) || strpos($chunk, 'start_widgetopts') === false) {
326 continue;
327 }
328 // Permissive outer pattern so crafted payloads like
329 // {...} <!--start_widgetopts end_widgetopts--> (parsing-differential
330 // attack) are also matched.
331 $chunk = preg_replace_callback(
332 '/<!--start_widgetopts\s+([\s\S]*?)\s*end_widgetopts-->/U',
333 static function ($m) use (&$changed) {
334 $raw = trim($m[1]);
335 $data = json_decode($raw, true);
336
337 if (!is_array($data)) {
338 // Trailing garbage after valid JSON (crafted payload).
339 // Find the last } and try decoding up to that point.
340 $pos = strrpos($raw, '}');
341 if ($pos !== false) {
342 $data = json_decode(substr($raw, 0, $pos + 1), true);
343 }
344 }
345
346 if (!is_array($data)) {
347 // Completely unrecoverable — remove entire marker.
348 $changed = true;
349 return '';
350 }
351
352 if (isset($data['class']['logic']) && $data['class']['logic'] !== '') {
353 $data['class']['logic'] = '';
354 $changed = true;
355 }
356 return '<!--start_widgetopts ' . wp_json_encode($data) . ' end_widgetopts-->';
357 },
358 $chunk
359 );
360 }
361 unset($chunk);
362 }
363
364 if (!empty($block['innerBlocks'])) {
365 widgetopts_strip_logic_from_blocks($block['innerBlocks'], $changed);
366 }
367 }
368 }
369
370 /**
371 * Strip legacy display logic for non-admin users on ALL save paths.
372 * Admins: no processing at all (no parse/serialize cycle).
373 * Non-admins: strip legacy logic fields to prevent injection.
374 *
375 * @since 5.1
376 */
377 add_filter('wp_insert_post_data', function($data, $postarr) {
378 if (current_user_can('manage_options')) {
379 return $data;
380 }
381
382 if (empty($data['post_content'])) {
383 return $data;
384 }
385
386 // wp_insert_post_data fires BEFORE wp_unslash() inside wp_insert_post(),
387 // so post_content still carries magic-quote backslashes (\" and \').
388 // Unslash before processing so parse_blocks sees clean JSON.
389 $content = wp_unslash($data['post_content']);
390
391 if (strpos($content, 'extended_widget_opts') === false
392 && strpos($content, 'start_widgetopts') === false) {
393 return $data;
394 }
395
396 $new_blocks = parse_blocks($content);
397 if (!is_array($new_blocks) || empty($new_blocks)) {
398 return $data;
399 }
400
401 $changed = false;
402 widgetopts_strip_logic_from_blocks($new_blocks, $changed);
403 if ($changed) {
404 // Re-slash so WordPress's subsequent wp_unslash() inside wp_insert_post()
405 // produces the correct clean string when writing to the database.
406 $data['post_content'] = wp_slash(serialize_blocks($new_blocks));
407 }
408 return $data;
409 }, 10, 2);
410
411 // Flag the block-renderer REST dispatch so render_block_data below can
412 // scope its sha256-allowlist work to that single route.
413 add_filter('rest_pre_dispatch', function ($result, $server, $request) {
414 if ($request instanceof WP_REST_Request
415 && strpos((string) $request->get_route(), '/wp/v2/block-renderer/') === 0) {
416 $GLOBALS['_widgetopts_in_block_renderer'] = true;
417 }
418 return $result;
419 }, 1, 3);
420
421 add_filter('rest_post_dispatch', function ($response, $server, $request) {
422 unset($GLOBALS['_widgetopts_in_block_renderer']);
423 return $response;
424 }, 1, 3);
425
426 // Block-renderer accepts user-supplied attributes without a save step.
427 // For non-admins, allowlist class.logic against a sha256 of values stored in
428 // the post's post_content; mismatched values are zeroed before render_callback.
429 add_filter('render_block_data', function ($parsed_block) {
430 if (!defined('REST_REQUEST') || !REST_REQUEST) {
431 return $parsed_block;
432 }
433 if (empty($GLOBALS['_widgetopts_in_block_renderer'])) {
434 return $parsed_block;
435 }
436
437 if (!is_array($parsed_block) || empty($parsed_block['attrs'])) {
438 return $parsed_block;
439 }
440 if (current_user_can('manage_options')) {
441 return $parsed_block;
442 }
443
444 $has_inline = (
445 (isset($parsed_block['attrs']['extended_widget_opts']['class']['logic'])
446 && $parsed_block['attrs']['extended_widget_opts']['class']['logic'] !== '')
447 || (isset($parsed_block['attrs']['extended_widget_opts_block']['class']['logic'])
448 && $parsed_block['attrs']['extended_widget_opts_block']['class']['logic'] !== '')
449 );
450 if (!$has_inline) {
451 return $parsed_block;
452 }
453
454 $post_id = 0;
455 $current = get_post();
456 if ($current instanceof WP_Post) {
457 $post_id = (int) $current->ID;
458 }
459 if (!$post_id && isset($_REQUEST['post_id'])) {
460 $post_id = absint($_REQUEST['post_id']);
461 }
462
463 $allow = $post_id ? widgetopts_get_post_logic_allowlist($post_id) : array();
464
465 foreach (array('extended_widget_opts', 'extended_widget_opts_block') as $key) {
466 if (isset($parsed_block['attrs'][$key]['class']['logic'])
467 && is_string($parsed_block['attrs'][$key]['class']['logic'])
468 && $parsed_block['attrs'][$key]['class']['logic'] !== '') {
469 $hash = hash('sha256', $parsed_block['attrs'][$key]['class']['logic']);
470 if (!isset($allow[$hash])) {
471 $parsed_block['attrs'][$key]['class']['logic'] = '';
472 }
473 }
474 }
475
476 return $parsed_block;
477 }, 5);
478
479 add_filter('render_block', function ($block_content, $parsed_block, $obj) {
480 if (!is_admin()) {
481 add_filter("render_block_{$obj->name}", "blockopts_filter_before_display", 100, 3);
482 }
483 return $block_content;
484 }, 100, 3);
485
486 function blockopts_filter_before_display($block_content, $parsed_block, $obj)
487 {
488 //for freeform filter and assignment
489 if (is_null($parsed_block['blockName']) || empty($parsed_block['blockName'])) {
490 if (!isset($parsed_block['attrs']) || empty($parsed_block['attrs'])) {
491 $result = null;
492 if (isset($parsed_block['innerContent']) && !empty($parsed_block['innerContent'][0])) {
493 $is_okay = preg_match("/<!--start_widgetopts[\s]+[{\":,}\w\W]*[\s]+end_widgetopts-->/", $parsed_block['innerContent'][0], $result);
494 if ($is_okay === 1) {
495 if (!is_null($result) && is_array($result)) {
496 $content = str_replace('<!--start_widgetopts', '', $result[0]);
497 $content = str_replace('end_widgetopts-->', '', $content);
498 $content = trim($content);
499 $parsed_block['attrs']['extended_widget_opts'] = json_decode($content, true);
500 }
501 }
502 }
503 }
504 }
505
506 if (isset($parsed_block) && isset($parsed_block['attrs']) && (isset($parsed_block['attrs']['extended_widget_opts']) || isset($parsed_block['attrs']['extended_widget_opts_block']))) {
507 global $widget_options, $current_user;
508 $instance = $parsed_block['attrs'];
509
510 //if idbase is not -1 it is a widget
511 if (isset($parsed_block['attrs']['extended_widget_opts']) && isset($parsed_block['attrs']['extended_widget_opts']['id_base']) && $parsed_block['attrs']['extended_widget_opts']['id_base'] != -1) {
512 return $block_content;
513 }
514
515 // WPML FIX
516 $hasWPML = has_filter('wpml_current_language');
517 $hasWPML = (function_exists('pll_the_languages')) ? false : $hasWPML;
518 $default_language = $hasWPML ? apply_filters('wpml_default_language', NULL) : false;
519
520 $hidden = false;
521 $opts = (isset($instance['extended_widget_opts'])) ? $instance['extended_widget_opts'] : (isset($instance['extended_widget_opts_block']) ? $instance['extended_widget_opts_block'] : array());
522 $visibility = array('show' => array(), 'hide' => array());
523 $tax_opts = (isset($widget_options['settings']) && isset($widget_options['settings']['taxonomies_keys'])) ? $widget_options['settings']['taxonomies_keys'] : array();
524
525 $visibility = isset($opts['visibility']) ? $opts['visibility'] : array();
526 $visibility_opts = isset($opts['visibility']['options']) ? $opts['visibility']['options'] : 'hide';
527 $authorPageSelection = "";
528
529 //wordpress pages
530 $is_misc = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['misc'])) ? true : false;
531 $is_types = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['post_type'])) ? true : false;
532 $is_tax = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['taxonomies'])) ? true : false;
533 $is_inherit = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['inherit'])) ? true : false;
534
535 //WOOCOMMERCE
536 $isWooPage = false;
537 if (class_exists('WooCommerce')) {
538 $wooPageID = 0;
539
540 $wooPageID = (is_shop()) ? get_option('woocommerce_shop_page_id') : $wooPageID;
541 if ($wooPageID) {
542 $isWooPage = true;
543
544 $visibility['pages'] = !empty($visibility['pages']) ? $visibility['pages'] : [];
545 if ($visibility_opts == 'hide' && (array_key_exists($wooPageID, $visibility['pages']) || in_array($wooPageID, $visibility['pages']))) {
546 $hidden = true; //hide if exists on hidden pages
547 } elseif ($visibility_opts == 'show' && (!array_key_exists($wooPageID, $visibility['pages']) && !in_array($wooPageID, $visibility['pages']))) {
548 $hidden = true; //hide if doesn't exists on visible pages
549 }
550
551 //do return to bypass other conditions
552 $hidden = apply_filters('widget_options_visibility_page_block', $hidden);
553
554 if ($hidden) {
555 return false;
556 }
557 }
558 }
559
560 // Normal Pages
561 if (!$isWooPage) {
562 if ($is_misc && ((is_home() && is_front_page()) || is_front_page())) {
563 if (isset($visibility['misc']['home']) && $visibility_opts == 'hide') {
564 $hidden = true; //hide if checked on hidden pages
565 } elseif (!isset($visibility['misc']['home']) && $visibility_opts == 'show') {
566 $hidden = true; //hide if not checked on visible pages
567 }
568
569 if (isset($visibility['misc']['home']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
570 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
571 }
572
573 //do return to bypass other conditions
574 $hidden = apply_filters('widget_options_visibility_home_block', $hidden);
575 if ($hidden) {
576 return false;
577 }
578 } elseif ($is_misc && is_home()) { //filter for blog page
579 if (isset($visibility['misc']['blog']) && $visibility_opts == 'hide') {
580 $hidden = true; //hide if checked on hidden pages
581 } elseif (!isset($visibility['misc']['blog']) && $visibility_opts == 'show') {
582 $hidden = true; //hide if not checked on visible pages
583 }
584
585 if (isset($visibility['misc']['blog']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
586 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
587 }
588
589 //do return to bypass other conditions
590 $hidden = apply_filters('widget_options_visibility_blog_block', $hidden);
591 if ($hidden) {
592 return false;
593 }
594 } elseif ($is_tax && is_category() && is_array($tax_opts) && in_array('category', $tax_opts)) {
595 if (!isset($visibility['categories'])) {
596 $visibility['categories'] = array();
597 }
598
599 $cat_lists = array();
600
601 if (isset($visibility['tax_terms']['category'])) {
602 $cat_lists = $visibility['tax_terms']['category'];
603 } elseif (isset($visibility['categories'])) {
604 $cat_lists = $visibility['categories'];
605 }
606
607 // WPML TRANSLATION OBJECT FIX
608 $category_id = ($hasWPML) ? apply_filters('wpml_object_id', get_query_var('cat'), 'category', true, $default_language) : get_query_var('cat');
609
610 if (!isset($visibility['taxonomies']['category']) && $visibility_opts == 'hide' && (array_key_exists($category_id, $cat_lists) || in_array($category_id, $cat_lists))) {
611 $hidden = true; //hide if exists on hidden pages
612 } elseif (!isset($visibility['taxonomies']['category']) && $visibility_opts == 'show' && (!array_key_exists($category_id, $cat_lists) && !in_array($category_id, $cat_lists))) {
613 $hidden = true; //hide if doesn't exists on visible pages
614 } elseif (isset($visibility['taxonomies']['category']) && $visibility_opts == 'hide') {
615 $hidden = true; //hide to all categories
616 } elseif (isset($visibility['taxonomies']['category']) && $visibility_opts == 'show') {
617 $hidden = false; //hide to all categories
618 }
619
620 if (isset($visibility['taxonomies']['category']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
621 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
622 }
623
624 //do return to bypass other conditions
625 $hidden = apply_filters('widget_options_visibility_categories_block', $hidden);
626 if ($hidden) {
627 return false;
628 }
629 } elseif ($is_tax && is_tag() && is_array($tax_opts) && in_array('post_tag', $tax_opts)) {
630 if (!isset($visibility['tags'])) {
631 $visibility['tags'] = array();
632 }
633
634 $tag_lists = (isset($visibility['tax_terms']['post_tag'])) ? $visibility['tax_terms']['post_tag'] : array();
635
636 // WPML TRANSLATION OBJECT FIX
637 $tag_id = ($hasWPML) ? apply_filters('wpml_object_id', get_query_var('tag_id'), 'post_tag', true, $default_language) : get_query_var('tag_id');
638
639 if (!isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'hide' && (array_key_exists($tag_id, $tag_lists) || in_array($tag_id, $tag_lists))) {
640 $hidden = true; //hide if exists on hidden pages
641 } elseif (!isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'show' && (!array_key_exists($tag_id, $tag_lists) && !in_array($tag_id, $tag_lists))) {
642 $hidden = true; //hide if doesn't exists on visible pages
643 } elseif (isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'hide') {
644 $hidden = true; //hide to all tags
645 } elseif (isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'show') {
646 $hidden = false; //hide to all tags
647 }
648
649 if (isset($visibility['taxonomies']['post_tag']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
650 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
651 }
652
653 //do return to bypass other conditions
654 $hidden = apply_filters('widget_options_visibility_tags_block', $hidden);
655 if ($hidden) {
656 return false;
657 }
658 } elseif ($is_tax && is_tax()) {
659 $term = get_queried_object();
660 $term_lists = array();
661
662 if (isset($visibility['tax_terms']) && isset($visibility['tax_terms'][$term->taxonomy])) {
663 $term_lists = $visibility['tax_terms'][$term->taxonomy];
664 }
665
666 // WPML TRANSLATION OBJECT FIX
667 $term_id = ($hasWPML) ? apply_filters('wpml_object_id', $term->term_id, $term->taxonomy, true, $default_language) : $term->term_id;
668
669 if (isset($visibility['taxonomies']) && !isset($visibility['taxonomies'][$term->taxonomy]) && $visibility_opts == 'hide' && (array_key_exists($term_id, $term_lists) || in_array($term_id, $term_lists))) {
670 $hidden = true; //hide if exists on hidden pages
671 } elseif (isset($visibility['taxonomies']) && !isset($visibility['taxonomies'][$term->taxonomy]) && $visibility_opts == 'show' && (!array_key_exists($term_id, $term_lists) && !in_array($term_id, $term_lists))) {
672 $hidden = true; //hide if doesn't exists on visible pages
673 } elseif (isset($visibility['taxonomies']) && isset($visibility['taxonomies'][$term->taxonomy]) && $visibility_opts == 'hide') {
674 $hidden = true; //hide to all tags
675 } elseif (isset($visibility['taxonomies']) && isset($visibility['taxonomies'][$term->taxonomy]) && $visibility_opts == 'show') {
676 $hidden = false; //hide to all tags
677 } elseif (isset($visibility['tax_terms']) && isset($visibility['tax_terms'][$term->taxonomy]) && $visibility_opts == 'hide' && (array_key_exists($term_id, $term_lists) || in_array($term_id, $term_lists))) {
678 $hidden = true; //hide if exists on hidden pages
679 } elseif (isset($visibility['tax_terms']) && isset($visibility['tax_terms'][$term->taxonomy]) && $visibility_opts == 'show' && (array_key_exists($term_id, $term_lists) || in_array($term_id, $term_lists))) {
680 $hidden = false; //hide if doesn't exists on visible pages
681 } elseif (!isset($visibility['taxonomies']) && $visibility_opts == 'show') {
682 $hidden = true; //hide if checked on hidden pages
683 }
684
685 if (isset($visibility['taxonomies']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
686 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
687 }
688
689 //do return to bypass other conditions
690 $hidden = apply_filters('widget_options_visibility_taxonomies_block', $hidden);
691 if ($hidden) {
692 return false;
693 }
694 } elseif ($is_misc && is_archive()) {
695 if (isset($visibility['misc']['archives']) && $visibility_opts == 'hide') {
696 $hidden = true; //hide if checked on hidden pages
697 } elseif (!isset($visibility['misc']['archives']) && $visibility_opts == 'show') {
698 $hidden = true; //hide if not checked on visible pages
699 } else if (isset($visibility['misc']['archives']) && (!empty($authorPageSelection) && $authorPageSelection == '3') && $visibility_opts == 'show') {
700 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
701 }
702
703 //do return to bypass other conditions
704 $hidden = apply_filters('widget_options_visibility_archives_block', $hidden);
705
706 if ($hidden) {
707 return false;
708 }
709 } elseif (is_post_type_archive()) {
710 if (!isset($visibility['types']) || ($is_types && !isset($visibility['types']))) {
711 $visibility['types'] = array();
712 }
713
714 $current_type_archive = get_post_type();
715 if (!empty($current_type_archive)) {
716 if ($visibility_opts == 'hide' && array_key_exists($current_type_archive, $visibility['types'])) {
717 $hidden = true; //hide if exists on hidden pages
718 } elseif ($visibility_opts == 'show' && !array_key_exists($current_type_archive, $visibility['types'])) {
719 $hidden = true; //hide if doesn't exists on visible pages
720 }
721 }
722
723 if (array_key_exists($current_type_archive, $visibility['types']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
724 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
725 }
726
727 //do return to bypass other conditions
728 $hidden = apply_filters('widget_options_visibility_post_type_archive_block', $hidden);
729 if ($hidden) {
730 return false;
731 }
732 } elseif ($is_misc && is_404()) {
733 if (isset($visibility['misc']['404']) && $visibility_opts == 'hide') {
734 $hidden = true; //hide if checked on hidden pages
735 } elseif (!isset($visibility['misc']['404']) && $visibility_opts == 'show') {
736 $hidden = true; //hide if not checked on visible pages
737 }
738
739 if (isset($visibility['misc']['404']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
740 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
741 }
742
743 //do return to bypass other conditions
744 $hidden = apply_filters('widget_options_visibility_404_block', $hidden);
745 if ($hidden) {
746 return false;
747 }
748 } elseif ($is_misc && is_search()) {
749 if (isset($visibility['misc']['search']) && $visibility_opts == 'hide') {
750 $hidden = true; //hide if checked on hidden pages
751 } elseif (!isset($visibility['misc']['search']) && $visibility_opts == 'show') {
752 $hidden = true; //hide if not checked on visible pages
753 }
754
755 if (isset($visibility['misc']['search']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
756 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
757 }
758
759 //do return to bypass other conditions
760 $hidden = apply_filters('widget_options_visibility_search_block', $hidden);
761 if ($hidden) {
762 return false;
763 }
764 } elseif (is_single() && !is_page()) {
765 global $post;
766 if (!isset($visibility['types']) || ($is_types && !isset($visibility['types']))) {
767 $visibility['types'] = array();
768 }
769 if ($visibility_opts == 'hide' && array_key_exists($post->post_type, $visibility['types'])) {
770 $hidden = true; //hide if exists on hidden pages
771 } elseif ($visibility_opts == 'show' && !array_key_exists($post->post_type, $visibility['types'])) {
772 $hidden = true; //hide if doesn't exists on visible pages
773 }
774
775 if ((!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
776 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
777 }
778
779 // do return to bypass other conditions
780 $hidden = apply_filters('widget_options_visibility_types_block', $hidden);
781
782 $taxonomy_names = get_post_taxonomies();
783 $array_intersect = array_intersect($tax_opts, $taxonomy_names);
784 // print_r( $tax_opts );
785 if (!isset($visibility['tax_terms']['category']) && isset($visibility['categories'])) {
786 $visibility['tax_terms']['category'] = $visibility['categories'];
787 }
788
789 // WPML FIX
790 $postID = ($hasWPML) ? apply_filters('wpml_object_id', $post->ID, $post->post_type, true, $default_language) : $post->ID;
791
792 if (!empty($array_intersect)) {
793 foreach ($array_intersect as $tax_key => $tax_value) {
794 if (in_array($tax_value, $tax_opts) && isset($visibility['tax_terms']) && isset($visibility['tax_terms'][$tax_value]) && !empty($visibility['tax_terms'][$tax_value])) {
795 $term_list = wp_get_post_terms($postID, $tax_value, array("fields" => "ids"));
796
797 // WPML TRANSLATION OBJECT FIX
798 if ($hasWPML) {
799 $temp_term_list = [];
800 foreach ($term_list as $index => $termID) {
801 $temp_term_list[] = apply_filters('wpml_object_id', $termID, $tax_value, true, $default_language);
802 }
803 $term_list = (!empty($temp_term_list)) ? $temp_term_list : $term_list;
804 }
805
806 if (is_array($term_list) && !empty($term_list)) {
807 $checked_terms = array_keys($visibility['tax_terms'][$tax_value]);
808 $checked_terms = (intval($checked_terms[0]) == 0) ? $visibility['tax_terms'][$tax_value] : $checked_terms;
809 $intersect = array_intersect($term_list, $checked_terms);
810 if (!empty($intersect) && $visibility_opts == 'hide') {
811 $hidden = true;
812 } elseif (!empty($intersect) && $visibility_opts == 'show') {
813 $hidden = false;
814 }
815 }
816 }
817 // do return to bypass other conditions
818 $hidden = apply_filters('widget_options_visibility_single_block_' . $tax_value, $hidden);
819 }
820 }
821
822
823 if ($hidden) {
824 return false;
825 }
826 // echo $type;
827 } elseif ($is_types && is_page()) {
828 global $post;
829
830 //do post type condition first
831 if (isset($visibility['types']) && isset($visibility['types']['page'])) {
832 if ($visibility_opts == 'hide' && array_key_exists('page', $visibility['types'])) {
833 $hidden = true; //hide if exists on hidden pages
834 } elseif ($visibility_opts == 'show' && !array_key_exists('page', $visibility['types'])) {
835 $hidden = true; //hide if doesn't exists on visible pages
836 }
837 } else {
838 //do per pages condition
839 if (!isset($visibility['pages'])) {
840 $visibility['pages'] = array();
841 }
842
843 // WPML FIX
844 $page_id = get_queried_object_id();
845 $parent_id = wp_get_post_parent_id($page_id);
846
847 $pageID = ($hasWPML) ? apply_filters('wpml_object_id', $page_id, 'page', true, $default_language) : $page_id;
848 $parentID = ($hasWPML) ? apply_filters('wpml_object_id', $parent_id, 'page', true, $default_language) : $parent_id;
849
850 $page_in_array = in_array($pageID, $visibility['pages']);
851 //for the compatibility of the data of lower version 3.8.10 and below
852 if (array_key_exists($pageID, $visibility['pages'])) {
853 if ($visibility['pages'][$pageID] == 1) {
854 $page_in_array = true;
855 }
856 }
857
858 //add parent inherit option
859 if ($is_inherit && $parentID && (array_key_exists($parentID, $visibility['pages']) || in_array($pageID, $visibility['pages']))) {
860 $visibility['pages'][] = $pageID;
861 // print_r( $visibility['pages'] );
862 }
863
864 if ($visibility_opts == 'hide' && $page_in_array) {
865 $hidden = true; //hide if exists on hidden pages
866 } elseif ($visibility_opts == 'show' && !$page_in_array) {
867 $hidden = true; //hide if doesn't exists on visible pages
868 }
869 }
870
871 if ((!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
872 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
873 }
874
875 //do return to bypass other conditions
876 $hidden = apply_filters('widget_options_visibility_page_block', $hidden);
877 if ($hidden) {
878 return false;
879 }
880 }
881 }
882 //end wordpress pages
883
884 //ACF
885 if (isset($widget_options['acf']) && 'activate' == $widget_options['acf']) {
886 if (isset($visibility['acf']['field']) && !empty($visibility['acf']['field'])) {
887 $acf = get_field_object($visibility['acf']['field']);
888 if ($acf && is_array($acf)) {
889 $acf_visibility = (isset($visibility['acf']) && isset($visibility['acf']['visibility'])) ? $visibility['acf']['visibility'] : 'hide';
890 //handle repeater fields
891 if (isset($acf['value'])) {
892 if (is_array($acf['value'])) {
893 $acf['value'] = implode(', ', array_map(function ($acf_array_value) {
894 if (!is_array($acf_array_value)) return $acf_array_value;
895
896 $acf_implode = implode(',', array_filter($acf_array_value));
897 return $acf_implode;
898 }, $acf['value']));
899 }
900 }
901 switch ($visibility['acf']['condition']) {
902 case 'equal':
903 if (isset($acf['value'])) {
904 if ('show' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) {
905 $hidden = false;
906 } else if ('show' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) {
907 $hidden = true;
908 } else if ('hide' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) {
909 $hidden = true;
910 } else if ('hide' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) {
911 $hidden = false;
912 }
913 }
914 break;
915 case 'not_equal':
916 if (isset($acf['value'])) {
917 if ('show' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) {
918 $hidden = true;
919 } else if ('show' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) {
920 $hidden = false;
921 } else if ('hide' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) {
922 $hidden = false;
923 } else if ('hide' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) {
924 $hidden = true;
925 }
926 }
927 break;
928 case 'contains':
929 if (isset($acf['value'])) {
930 if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) {
931 $hidden = false;
932 } else if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) {
933 $hidden = true;
934 } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) {
935 $hidden = true;
936 } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) {
937 $hidden = false;
938 }
939 }
940 break;
941 case 'not_contains':
942 if (isset($acf['value'])) {
943 if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) {
944 $hidden = true;
945 } else if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) {
946 $hidden = false;
947 } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) {
948 $hidden = false;
949 } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) {
950 $hidden = true;
951 }
952 }
953 break;
954 case 'empty':
955 if ('show' == $acf_visibility && empty($acf['value'])) {
956 $hidden = false;
957 } else if ('show' == $acf_visibility && !empty($acf['value'])) {
958 $hidden = true;
959 } elseif ('hide' == $acf_visibility && empty($acf['value'])) {
960 $hidden = true;
961 } else if ('hide' == $acf_visibility && !empty($acf['value'])) {
962 $hidden = false;
963 }
964 break;
965 case 'not_empty':
966 if ('show' == $acf_visibility && empty($acf['value'])) {
967 $hidden = true;
968 } else if ('show' == $acf_visibility && !empty($acf['value'])) {
969 $hidden = false;
970 } elseif ('hide' == $acf_visibility && empty($acf['value'])) {
971 $hidden = false;
972 } else if ('hide' == $acf_visibility && !empty($acf['value'])) {
973 $hidden = true;
974 }
975 break;
976
977 default:
978 # code...
979 break;
980 }
981
982 // //do return to bypass other conditions
983 $hidden = apply_filters('widget_options_visibility_acf_block', $hidden);
984 if ($hidden) {
985 return false;
986 }
987 }
988 }
989 }
990
991 //login state
992 if (isset($widget_options['state']) && 'activate' == $widget_options['state'] && isset($opts['roles'])) {
993 if (isset($opts['roles']['state']) && !empty($opts['roles']['state'])) {
994 //do state action here
995 if ($opts['roles']['state'] == 'out' && is_user_logged_in()) {
996 return false;
997 } else if ($opts['roles']['state'] == 'in' && !is_user_logged_in()) {
998 return false;
999 }
1000 }
1001 }
1002
1003 if ('activate' == $widget_options['logic']) {
1004 // New snippet-based system
1005 if (isset($opts['class']['logic_snippet_id']) && !empty($opts['class']['logic_snippet_id'])) {
1006 $snippet_id = $opts['class']['logic_snippet_id'];
1007 if (class_exists('WidgetOpts_Snippets_API')) {
1008 $result = WidgetOpts_Snippets_API::execute_snippet($snippet_id);
1009 if ($result === false) {
1010 return false;
1011 }
1012 }
1013 }
1014 // Legacy support for old inline logic
1015 elseif (isset($opts['class']) && isset($opts['class']['logic']) && !empty($opts['class']['logic'])) {
1016 // Flag that legacy migration is needed
1017 if (!get_option('wopts_display_logic_migration_required', false)) {
1018 update_option('wopts_display_logic_migration_required', true);
1019 }
1020
1021 $display_logic = stripslashes(trim($opts['class']['logic']));
1022 $display_logic = apply_filters('widget_options_logic_override_block', $display_logic);
1023 $display_logic = apply_filters('extended_widget_options_logic_override_block', $display_logic);
1024 if ($display_logic === false) {
1025 return false;
1026 }
1027 if ($display_logic === true) {
1028 return true;
1029 }
1030 // if (stristr($display_logic, "return") === false) {
1031 // $display_logic = "return (" . $display_logic . ");";
1032 // }
1033 $display_logic = htmlspecialchars_decode($display_logic, ENT_QUOTES);
1034 if (!widgetopts_safe_eval_trusted($display_logic)) {
1035 return false;
1036 }
1037 }
1038 }
1039
1040 if ('activate' == $widget_options['hide_title']) {
1041 //hide widget title
1042 if (isset($instance['title']) && isset($opts['class']) && isset($opts['class']['title']) && '1' == $opts['class']['title']) {
1043 $instance['title'] = '';
1044 }
1045 }
1046
1047 $block_content = widgetopts_add_classes_post_block($block_content, $parsed_block, $obj);
1048 }
1049
1050 return $block_content;
1051 }
1052
1053 /*
1054 * Add custom classes on widget
1055 */
1056 function widgetopts_add_classes_post_block($block_content, $parsed_block, $obj)
1057 {
1058 global $widget_options, $wp_registered_widget_controls;
1059 $classe_to_add = '';
1060 $id_to_add = '';
1061 $widget_id_set = '';
1062 $data_attr = '';
1063 $instance = $parsed_block['attrs'];
1064
1065 if (isset($instance)) {
1066 $opts = (isset($instance['extended_widget_opts'])) ? $instance['extended_widget_opts'] : (isset($instance['extended_widget_opts_block']) ? $instance['extended_widget_opts_block'] : array());
1067 } else {
1068 $opts = array();
1069 }
1070
1071 $custom_class = isset($opts['class']) ? $opts['class'] : '';
1072
1073 if ('activate' == $widget_options['classes'] && isset($widget_options['settings']['classes'])) {
1074 //don't add the IDs when the setting is set to NO
1075 if (isset($widget_options['settings']['classes']['id'])) {
1076 if (is_array($custom_class) && isset($custom_class['id']) && !empty($custom_class['id'])) {
1077 $id_to_add = sanitize_html_class($custom_class['id']);
1078 $widget_id_set = sanitize_html_class($custom_class['id']);
1079 }
1080 }
1081 }
1082
1083 $get_classes = widgetopts_classes_generator($opts, $widget_options, $widget_options['settings']);
1084
1085 //double check array
1086 if (!is_array($get_classes)) {
1087 $get_classes = array();
1088 }
1089
1090 if (!empty($get_classes)) {
1091 $classe_to_add .= (implode(' ', $get_classes)) . ' ';
1092 //$block_content = preg_replace('class="', $classes, $block_content, 1);
1093 }
1094
1095 // $params[0]['before_widget'] = str_replace('class="', ' data-animation="asdf" class="', $params[0]['before_widget']);
1096
1097 $match = [];
1098 $has_match = preg_match('/<\w*[^>]*>/', $block_content, $match);
1099
1100 if ($has_match == 1) {
1101 if (!empty($id_to_add)) {
1102 $has_match_id = preg_match('/[id="]/', $match[0]);
1103 if ($has_match_id == 1) {
1104 $block_content = preg_replace('/id="[^"]*/', "id=\"{$id_to_add}", $block_content, 1);
1105 } else {
1106 $block_content = preg_replace('/>/', " id=\"{$id_to_add}\">", $block_content, 1);
1107 }
1108 }
1109
1110 if (!empty($classe_to_add)) {
1111 $has_match_class = preg_match('/[class="]/', $match[0]);
1112 if ($has_match_class == 1) {
1113 $block_content = preg_replace('/class="/', "class=\"{$classe_to_add}", $block_content, 1);
1114 } else {
1115 $block_content = preg_replace('/>/', " class=\"{$classe_to_add}\">", $block_content, 1);
1116 }
1117 }
1118
1119 if (!empty($data_attr)) {
1120 $block_content = preg_replace('/>/', " {$data_attr}>", $block_content, 1);
1121 }
1122 }
1123
1124 return $block_content;
1125 }
1126
1127
1128 /**
1129 * Gutenberg ajax functions
1130 */
1131 function widgetopts_verify_gutenberg_ajax()
1132 {
1133 if (!current_user_can('edit_posts')) {
1134 wp_send_json_error('Permission denied.', 403);
1135 exit;
1136 }
1137 }
1138
1139 function widgetopts_get_types()
1140 {
1141 widgetopts_verify_gutenberg_ajax();
1142 global $widgetopts_types;
1143
1144 wp_send_json_success(((!empty($widgetopts_types)) ? $widgetopts_types : widgetopts_global_types()));
1145 die;
1146 }
1147 add_action('wp_ajax_widgetopts_get_types', 'widgetopts_get_types');
1148
1149
1150 function widgetopts_get_taxonomies()
1151 {
1152 widgetopts_verify_gutenberg_ajax();
1153 global $widgetopts_taxonomies;
1154
1155 wp_send_json_success(((!empty($widgetopts_taxonomies)) ? $widgetopts_taxonomies : widgetopts_global_taxonomies()));
1156 die;
1157 }
1158 add_action('wp_ajax_widgetopts_get_taxonomies', 'widgetopts_get_taxonomies');
1159
1160 function widgetopts_acf_get_field_groups()
1161 {
1162 widgetopts_verify_gutenberg_ajax();
1163
1164 $fields = array();
1165 if (function_exists('acf_get_field_groups')) {
1166 $groups = acf_get_field_groups();
1167 if (is_array($groups)) {
1168 foreach ($groups as $group) {
1169 $fields[$group['ID']] = array('title' => $group['title'], 'fields' => acf_get_fields($group));
1170 }
1171 }
1172 } else {
1173 $groups = apply_filters('acf/get_field_groups', array());
1174 if (is_array($groups)) {
1175 foreach ($groups as $group) {
1176 $fields[$group['id']] = array('title' => $group['title'], 'fields' => apply_filters('acf/field_group/get_fields', array(), $group['id']));
1177 }
1178 }
1179 }
1180
1181 wp_send_json_success($fields);
1182 die;
1183 }
1184 add_action('wp_ajax_widgetopts_acf_get_field_groups', 'widgetopts_acf_get_field_groups');
1185
1186 function widgetopts_get_legacy_data()
1187 {
1188 widgetopts_verify_gutenberg_ajax();
1189
1190 if (isset($_POST['id_base'])) {
1191 wp_send_json_success(array());
1192 die;
1193 }
1194 $settings = array();
1195 $settings = get_option('widget_' + sanitize_key($_POST['id_base']));
1196
1197 if (false === $settings) {
1198 return $settings;
1199 }
1200
1201 if (!is_array($settings) && !($settings instanceof ArrayObject || $settings instanceof ArrayIterator)) {
1202 $settings = array();
1203 }
1204
1205 wp_send_json_success($settings);
1206 die;
1207 }
1208 add_action('wp_ajax_widgetopts_get_legacy_data', 'widgetopts_get_legacy_data');
1209
1210 function widgetopts_get_settings_ajax()
1211 {
1212 widgetopts_verify_gutenberg_ajax();
1213 $settings = widgetopts_get_settings();
1214
1215 wp_send_json_success($settings);
1216 die;
1217 }
1218 add_action('wp_ajax_widgetopts_get_settings_ajax', 'widgetopts_get_settings_ajax');
1219
1220 function widgetopts_get_snippets_ajax()
1221 {
1222 widgetopts_verify_gutenberg_ajax();
1223
1224 $search = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
1225
1226 $snippets = array();
1227 if (class_exists('WidgetOpts_Snippets_CPT')) {
1228 $all_snippets = WidgetOpts_Snippets_CPT::get_all_snippets($search);
1229 foreach ($all_snippets as $snippet) {
1230 $snippets[] = array(
1231 'id' => $snippet['id'],
1232 'title' => $snippet['title'],
1233 'description' => $snippet['description']
1234 );
1235 }
1236 }
1237
1238 // Return data with admin info for manage snippets link
1239 wp_send_json_success(array(
1240 'snippets' => $snippets,
1241 'can_manage' => current_user_can('manage_options'),
1242 'manage_url' => admin_url('edit.php?post_type=widgetopts_snippet'),
1243 'migration_url' => admin_url('options-general.php?page=widgetopts_migration')
1244 ));
1245 die;
1246 }
1247 add_action('wp_ajax_widgetopts_get_snippets_ajax', 'widgetopts_get_snippets_ajax');
1248
1249 function widgetopts_get_pages()
1250 {
1251 widgetopts_verify_gutenberg_ajax();
1252
1253 $pages = [];
1254
1255 $pargs = array(
1256 'hierarchical' => true,
1257 'child_of' => 0, // Display all pages regardless of parent
1258 'parent' => -1, // Display all pages regardless of parent
1259 'sort_order' => 'ASC',
1260 'sort_column' => 'menu_order, post_title'
1261 );
1262
1263 $pageLoop = get_pages($pargs);
1264
1265 if ($pageLoop) {
1266 foreach ($pageLoop as $objPage) {
1267 $depth = count(get_ancestors($objPage->ID, 'page'));
1268 // Determine indentation for hierarchical display
1269 $indent = str_repeat('-', $depth);
1270 $objPage->post_title = $indent . "" . $objPage->post_title;
1271 }
1272 $pages = $pageLoop;
1273 }
1274
1275 wp_send_json_success($pages);
1276 die;
1277 }
1278 add_action('wp_ajax_widgetopts_get_pages', 'widgetopts_get_pages');
1279
1280 function widgetopts_get_terms()
1281 {
1282 widgetopts_verify_gutenberg_ajax();
1283
1284 $terms = array();
1285
1286 $_terms = array();
1287 $_terms = get_terms();
1288
1289 if (!is_wp_error($_terms)) {
1290 foreach ($_terms as $t) {
1291 $terms[$t->name][] = $t;
1292 }
1293 }
1294
1295 wp_send_json_success($terms);
1296 die;
1297 }
1298 add_action('wp_ajax_widgetopts_get_terms', 'widgetopts_get_terms');
1299
1300 function widgetopts_get_users()
1301 {
1302 widgetopts_verify_gutenberg_ajax();
1303 global $wp_version;
1304
1305 $authors = array();
1306
1307 $args = array();
1308
1309 $is_6_3_and_above = version_compare($wp_version, '6.3', '>=');
1310 if ($is_6_3_and_above) {
1311 $args['cache_results'] = apply_filters('cache_widgetopts_ajax_taxonomy_search', true);
1312 }
1313
1314 $_authors = get_users($args);
1315
1316 if (!empty($_authors)) {
1317
1318 if (is_iterable($_authors)) {
1319 foreach ($_authors as $a) {
1320 $displayname = isset($a->display_name) ? $a->display_name : (isset($a->data) && isset($a->data->display_name) ? $a->data->display_name : '');
1321 $authors[] = ["ID" => $a->ID, "display_name" => $displayname];
1322 }
1323 }
1324 }
1325
1326 wp_send_json_success($authors);
1327 die;
1328 }
1329 add_action('wp_ajax_widgetopts_get_users', 'widgetopts_get_users');
1330
1331 function widgetopts_ajax_roles_search_block()
1332 {
1333 widgetopts_verify_gutenberg_ajax();
1334 $response = [
1335 'results' => [],
1336 'pagination' => ['more' => false]
1337 ];
1338
1339 $term = isset($_POST['term']) && !empty($_POST['term']) ? $_POST['term'] : '';
1340
1341 $roles = get_editable_roles();
1342 if (!empty($roles)) {
1343 foreach ($roles as $role_name => $role_info) {
1344 // if ((!empty($term) && stristr($role_name, $term) !== false) || stristr($role_name, $role_info['name']) !== false) {
1345 $response['results'][] = [
1346 'id' => $role_name,
1347 'text' => $role_info['name']
1348 ];
1349 // }
1350 }
1351 }
1352
1353 wp_send_json_success($response);
1354 die;
1355 }
1356 add_action('wp_ajax_widgetopts_ajax_roles_search_block', 'widgetopts_ajax_roles_search_block');
1357