PluginProbe ʕ •ᴥ•ʔ
Widget Options – Advanced Conditional Visibility for Gutenberg Blocks & Classic Widgets / 4.0.7
Widget Options – Advanced Conditional Visibility for Gutenberg Blocks & Classic Widgets v4.0.7
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 1 year ago lib 1 year ago packages 1 year ago src 1 year ago README.md 1 year ago changelog.txt 1 year ago gutenberg-toolbar.php 1 year ago gutenberg.php 1 year ago package-lock.json 1 year ago package.json 1 year ago post-content.php 1 year ago readme.txt 1 year ago
gutenberg-toolbar.php
1054 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
173 unset($block[0]['attrs']['extended_widget_opts_block']);
174 $instance['content'] = serialize_blocks($block);
175 }
176 }
177 }
178 } else {
179 //this is for legacy widget saving
180 //for first time saving of legacy widget, after this frontend will hanlde the saving in block editor
181 //for already exist widget
182 if (!empty($new_instance['extended_widget_opts-' . $obj->id])) {
183 $instance['extended_widget_opts-' . $obj->id] = $new_instance['extended_widget_opts-' . $obj->id];
184 }
185
186 //for new created widget
187 if (isset($new_instance['extended_widget_opts-undefined']) && empty($new_instance['extended_widget_opts-' . $obj->id])) {
188 $instance['extended_widget_opts-' . $obj->id] = $new_instance['extended_widget_opts-undefined'];
189
190 array_pop($base);
191 $new_base = implode('-', $base);
192
193 $instance['extended_widget_opts-' . $obj->id]['id_base'] = $base === false ? '-1' : $new_base;
194 unset($new_instance['extended_widget_opts-undefined']);
195 if (isset($instance['extended_widget_opts-undefined'])) {
196 unset($instance['extended_widget_opts-undefined']);
197 }
198 }
199 }
200
201 return $instance;
202 }, 100, 4);
203 }
204
205 add_filter('render_block', function ($block_content, $parsed_block, $obj) {
206 if (!is_admin()) {
207 add_filter("render_block_{$obj->name}", "blockopts_filter_before_display", 100, 3);
208 }
209 return $block_content;
210 }, 100, 3);
211
212 function blockopts_filter_before_display($block_content, $parsed_block, $obj)
213 {
214 //for freeform filter and assignment
215 if (is_null($parsed_block['blockName']) || empty($parsed_block['blockName'])) {
216 if (!isset($parsed_block['attrs']) || empty($parsed_block['attrs'])) {
217 $result = null;
218 if (isset($parsed_block['innerContent']) && !empty($parsed_block['innerContent'][0])) {
219 $is_okay = preg_match("/<!--start_widgetopts[\s]+[{\":,}\w\W]*[\s]+end_widgetopts-->/", $parsed_block['innerContent'][0], $result);
220 if ($is_okay === 1) {
221 if (!is_null($result) && is_array($result)) {
222 $content = str_replace('<!--start_widgetopts', '', $result[0]);
223 $content = str_replace('end_widgetopts-->', '', $content);
224 $content = trim($content);
225 $parsed_block['attrs']['extended_widget_opts'] = json_decode($content, true);
226 }
227 }
228 }
229 }
230 }
231
232 if (isset($parsed_block) && isset($parsed_block['attrs']) && (isset($parsed_block['attrs']['extended_widget_opts']) || isset($parsed_block['attrs']['extended_widget_opts_block']))) {
233 global $widget_options, $current_user;
234 $instance = $parsed_block['attrs'];
235
236 //if idbase is not -1 it is a widget
237 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) {
238 return $block_content;
239 }
240
241 // WPML FIX
242 $hasWPML = has_filter('wpml_current_language');
243 $hasWPML = (function_exists('pll_the_languages')) ? false : $hasWPML;
244 $default_language = $hasWPML ? apply_filters('wpml_default_language', NULL) : false;
245
246 $hidden = false;
247 $opts = (isset($instance['extended_widget_opts'])) ? $instance['extended_widget_opts'] : (isset($instance['extended_widget_opts_block']) ? $instance['extended_widget_opts_block'] : array());
248 $visibility = array('show' => array(), 'hide' => array());
249 $tax_opts = (isset($widget_options['settings']) && isset($widget_options['settings']['taxonomies_keys'])) ? $widget_options['settings']['taxonomies_keys'] : array();
250
251 $visibility = isset($opts['visibility']) ? $opts['visibility'] : array();
252 $visibility_opts = isset($opts['visibility']['options']) ? $opts['visibility']['options'] : 'hide';
253 $authorPageSelection = "";
254
255 //wordpress pages
256 $is_misc = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['misc'])) ? true : false;
257 $is_types = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['post_type'])) ? true : false;
258 $is_tax = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['taxonomies'])) ? true : false;
259 $is_inherit = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['inherit'])) ? true : false;
260
261 //WOOCOMMERCE
262 $isWooPage = false;
263 if (class_exists('WooCommerce')) {
264 $wooPageID = 0;
265
266 $wooPageID = (is_shop()) ? get_option('woocommerce_shop_page_id') : $wooPageID;
267 if ($wooPageID) {
268 $isWooPage = true;
269
270 $visibility['pages'] = !empty($visibility['pages']) ? $visibility['pages'] : [];
271 if ($visibility_opts == 'hide' && (array_key_exists($wooPageID, $visibility['pages']) || in_array($wooPageID, $visibility['pages']))) {
272 $hidden = true; //hide if exists on hidden pages
273 } elseif ($visibility_opts == 'show' && (!array_key_exists($wooPageID, $visibility['pages']) && !in_array($wooPageID, $visibility['pages']))) {
274 $hidden = true; //hide if doesn't exists on visible pages
275 }
276
277 //do return to bypass other conditions
278 $hidden = apply_filters('widget_options_visibility_page_block', $hidden);
279
280 if ($hidden) {
281 return false;
282 }
283 }
284 }
285
286 // Normal Pages
287 if (!$isWooPage) {
288 if ($is_misc && ((is_home() && is_front_page()) || is_front_page())) {
289 if (isset($visibility['misc']['home']) && $visibility_opts == 'hide') {
290 $hidden = true; //hide if checked on hidden pages
291 } elseif (!isset($visibility['misc']['home']) && $visibility_opts == 'show') {
292 $hidden = true; //hide if not checked on visible pages
293 }
294
295 if (isset($visibility['misc']['home']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
296 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
297 }
298
299 //do return to bypass other conditions
300 $hidden = apply_filters('widget_options_visibility_home_block', $hidden);
301 if ($hidden) {
302 return false;
303 }
304 } elseif ($is_misc && is_home()) { //filter for blog page
305 if (isset($visibility['misc']['blog']) && $visibility_opts == 'hide') {
306 $hidden = true; //hide if checked on hidden pages
307 } elseif (!isset($visibility['misc']['blog']) && $visibility_opts == 'show') {
308 $hidden = true; //hide if not checked on visible pages
309 }
310
311 if (isset($visibility['misc']['blog']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
312 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
313 }
314
315 //do return to bypass other conditions
316 $hidden = apply_filters('widget_options_visibility_blog_block', $hidden);
317 if ($hidden) {
318 return false;
319 }
320 } elseif ($is_tax && is_category() && is_array($tax_opts) && in_array('category', $tax_opts)) {
321 if (!isset($visibility['categories'])) {
322 $visibility['categories'] = array();
323 }
324
325 $cat_lists = array();
326
327 if (isset($visibility['tax_terms']['category'])) {
328 $cat_lists = $visibility['tax_terms']['category'];
329 } elseif (isset($visibility['categories'])) {
330 $cat_lists = $visibility['categories'];
331 }
332
333 // WPML TRANSLATION OBJECT FIX
334 $category_id = ($hasWPML) ? apply_filters('wpml_object_id', get_query_var('cat'), 'category', true, $default_language) : get_query_var('cat');
335
336 if (!isset($visibility['taxonomies']['category']) && $visibility_opts == 'hide' && (array_key_exists($category_id, $cat_lists) || in_array($category_id, $cat_lists))) {
337 $hidden = true; //hide if exists on hidden pages
338 } elseif (!isset($visibility['taxonomies']['category']) && $visibility_opts == 'show' && (!array_key_exists($category_id, $cat_lists) && !in_array($category_id, $cat_lists))) {
339 $hidden = true; //hide if doesn't exists on visible pages
340 } elseif (isset($visibility['taxonomies']['category']) && $visibility_opts == 'hide') {
341 $hidden = true; //hide to all categories
342 } elseif (isset($visibility['taxonomies']['category']) && $visibility_opts == 'show') {
343 $hidden = false; //hide to all categories
344 }
345
346 if (isset($visibility['taxonomies']['category']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
347 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
348 }
349
350 //do return to bypass other conditions
351 $hidden = apply_filters('widget_options_visibility_categories_block', $hidden);
352 if ($hidden) {
353 return false;
354 }
355 } elseif ($is_tax && is_tag() && is_array($tax_opts) && in_array('post_tag', $tax_opts)) {
356 if (!isset($visibility['tags'])) {
357 $visibility['tags'] = array();
358 }
359
360 $tag_lists = (isset($visibility['tax_terms']['post_tag'])) ? $visibility['tax_terms']['post_tag'] : array();
361
362 // WPML TRANSLATION OBJECT FIX
363 $tag_id = ($hasWPML) ? apply_filters('wpml_object_id', get_query_var('tag_id'), 'post_tag', true, $default_language) : get_query_var('tag_id');
364
365 if (!isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'hide' && (array_key_exists($tag_id, $tag_lists) || in_array($tag_id, $tag_lists))) {
366 $hidden = true; //hide if exists on hidden pages
367 } elseif (!isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'show' && (!array_key_exists($tag_id, $tag_lists) && !in_array($tag_id, $tag_lists))) {
368 $hidden = true; //hide if doesn't exists on visible pages
369 } elseif (isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'hide') {
370 $hidden = true; //hide to all tags
371 } elseif (isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'show') {
372 $hidden = false; //hide to all tags
373 }
374
375 if (isset($visibility['taxonomies']['post_tag']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
376 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
377 }
378
379 //do return to bypass other conditions
380 $hidden = apply_filters('widget_options_visibility_tags_block', $hidden);
381 if ($hidden) {
382 return false;
383 }
384 } elseif ($is_tax && is_tax()) {
385 $term = get_queried_object();
386 $term_lists = array();
387
388 if (isset($visibility['tax_terms']) && isset($visibility['tax_terms'][$term->taxonomy])) {
389 $term_lists = $visibility['tax_terms'][$term->taxonomy];
390 }
391
392 // WPML TRANSLATION OBJECT FIX
393 $term_id = ($hasWPML) ? apply_filters('wpml_object_id', $term->term_id, $term->taxonomy, true, $default_language) : $term->term_id;
394
395 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))) {
396 $hidden = true; //hide if exists on hidden pages
397 } 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))) {
398 $hidden = true; //hide if doesn't exists on visible pages
399 } elseif (isset($visibility['taxonomies']) && isset($visibility['taxonomies'][$term->taxonomy]) && $visibility_opts == 'hide') {
400 $hidden = true; //hide to all tags
401 } elseif (isset($visibility['taxonomies']) && isset($visibility['taxonomies'][$term->taxonomy]) && $visibility_opts == 'show') {
402 $hidden = false; //hide to all tags
403 } 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))) {
404 $hidden = true; //hide if exists on hidden pages
405 } 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))) {
406 $hidden = false; //hide if doesn't exists on visible pages
407 } elseif (!isset($visibility['taxonomies']) && $visibility_opts == 'show') {
408 $hidden = true; //hide if checked on hidden pages
409 }
410
411 if (isset($visibility['taxonomies']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
412 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
413 }
414
415 //do return to bypass other conditions
416 $hidden = apply_filters('widget_options_visibility_taxonomies_block', $hidden);
417 if ($hidden) {
418 return false;
419 }
420 } elseif ($is_misc && is_archive()) {
421 if (isset($visibility['misc']['archives']) && $visibility_opts == 'hide') {
422 $hidden = true; //hide if checked on hidden pages
423 } elseif (!isset($visibility['misc']['archives']) && $visibility_opts == 'show') {
424 $hidden = true; //hide if not checked on visible pages
425 } else if (isset($visibility['misc']['archives']) && (!empty($authorPageSelection) && $authorPageSelection == '3') && $visibility_opts == 'show') {
426 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
427 }
428
429 //do return to bypass other conditions
430 $hidden = apply_filters('widget_options_visibility_archives_block', $hidden);
431
432 if ($hidden) {
433 return false;
434 }
435 } elseif (is_post_type_archive()) {
436 if (!isset($visibility['types']) || ($is_types && !isset($visibility['types']))) {
437 $visibility['types'] = array();
438 }
439
440 $current_type_archive = get_post_type();
441 if (!empty($current_type_archive)) {
442 if ($visibility_opts == 'hide' && array_key_exists($current_type_archive, $visibility['types'])) {
443 $hidden = true; //hide if exists on hidden pages
444 } elseif ($visibility_opts == 'show' && !array_key_exists($current_type_archive, $visibility['types'])) {
445 $hidden = true; //hide if doesn't exists on visible pages
446 }
447 }
448
449 if (array_key_exists($current_type_archive, $visibility['types']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
450 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
451 }
452
453 //do return to bypass other conditions
454 $hidden = apply_filters('widget_options_visibility_post_type_archive_block', $hidden);
455 if ($hidden) {
456 return false;
457 }
458 } elseif ($is_misc && is_404()) {
459 if (isset($visibility['misc']['404']) && $visibility_opts == 'hide') {
460 $hidden = true; //hide if checked on hidden pages
461 } elseif (!isset($visibility['misc']['404']) && $visibility_opts == 'show') {
462 $hidden = true; //hide if not checked on visible pages
463 }
464
465 if (isset($visibility['misc']['404']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
466 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
467 }
468
469 //do return to bypass other conditions
470 $hidden = apply_filters('widget_options_visibility_404_block', $hidden);
471 if ($hidden) {
472 return false;
473 }
474 } elseif ($is_misc && is_search()) {
475 if (isset($visibility['misc']['search']) && $visibility_opts == 'hide') {
476 $hidden = true; //hide if checked on hidden pages
477 } elseif (!isset($visibility['misc']['search']) && $visibility_opts == 'show') {
478 $hidden = true; //hide if not checked on visible pages
479 }
480
481 if (isset($visibility['misc']['search']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
482 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
483 }
484
485 //do return to bypass other conditions
486 $hidden = apply_filters('widget_options_visibility_search_block', $hidden);
487 if ($hidden) {
488 return false;
489 }
490 } elseif (is_single() && !is_page()) {
491 global $post;
492 if (!isset($visibility['types']) || ($is_types && !isset($visibility['types']))) {
493 $visibility['types'] = array();
494 }
495 if ($visibility_opts == 'hide' && array_key_exists($post->post_type, $visibility['types'])) {
496 $hidden = true; //hide if exists on hidden pages
497 } elseif ($visibility_opts == 'show' && !array_key_exists($post->post_type, $visibility['types'])) {
498 $hidden = true; //hide if doesn't exists on visible pages
499 }
500
501 if ((!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
502 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
503 }
504
505 // do return to bypass other conditions
506 $hidden = apply_filters('widget_options_visibility_types_block', $hidden);
507
508 $taxonomy_names = get_post_taxonomies();
509 $array_intersect = array_intersect($tax_opts, $taxonomy_names);
510 // print_r( $tax_opts );
511 if (!isset($visibility['tax_terms']['category']) && isset($visibility['categories'])) {
512 $visibility['tax_terms']['category'] = $visibility['categories'];
513 }
514
515 // WPML FIX
516 $postID = ($hasWPML) ? apply_filters('wpml_object_id', $post->ID, $post->post_type, true, $default_language) : $post->ID;
517
518 if (!empty($array_intersect)) {
519 foreach ($array_intersect as $tax_key => $tax_value) {
520 if (in_array($tax_value, $tax_opts) && isset($visibility['tax_terms']) && isset($visibility['tax_terms'][$tax_value]) && !empty($visibility['tax_terms'][$tax_value])) {
521 $term_list = wp_get_post_terms($postID, $tax_value, array("fields" => "ids"));
522
523 // WPML TRANSLATION OBJECT FIX
524 if ($hasWPML) {
525 $temp_term_list = [];
526 foreach ($term_list as $index => $termID) {
527 $temp_term_list[] = apply_filters('wpml_object_id', $termID, $tax_value, true, $default_language);
528 }
529 $term_list = (!empty($temp_term_list)) ? $temp_term_list : $term_list;
530 }
531
532 if (is_array($term_list) && !empty($term_list)) {
533 $checked_terms = array_keys($visibility['tax_terms'][$tax_value]);
534 $checked_terms = (intval($checked_terms[0]) == 0) ? $visibility['tax_terms'][$tax_value] : $checked_terms;
535 $intersect = array_intersect($term_list, $checked_terms);
536 if (!empty($intersect) && $visibility_opts == 'hide') {
537 $hidden = true;
538 } elseif (!empty($intersect) && $visibility_opts == 'show') {
539 $hidden = false;
540 }
541 }
542 }
543 // do return to bypass other conditions
544 $hidden = apply_filters('widget_options_visibility_single_block_' . $tax_value, $hidden);
545 }
546 }
547
548
549 if ($hidden) {
550 return false;
551 }
552 // echo $type;
553 } elseif ($is_types && is_page()) {
554 global $post;
555
556 //do post type condition first
557 if (isset($visibility['types']) && isset($visibility['types']['page'])) {
558 if ($visibility_opts == 'hide' && array_key_exists('page', $visibility['types'])) {
559 $hidden = true; //hide if exists on hidden pages
560 } elseif ($visibility_opts == 'show' && !array_key_exists('page', $visibility['types'])) {
561 $hidden = true; //hide if doesn't exists on visible pages
562 }
563 } else {
564 //do per pages condition
565 if (!isset($visibility['pages'])) {
566 $visibility['pages'] = array();
567 }
568
569 // WPML FIX
570 $page_id = get_queried_object_id();
571 $parent_id = wp_get_post_parent_id($page_id);
572
573 $pageID = ($hasWPML) ? apply_filters('wpml_object_id', $page_id, 'page', true, $default_language) : $page_id;
574 $parentID = ($hasWPML) ? apply_filters('wpml_object_id', $parent_id, 'page', true, $default_language) : $parent_id;
575
576 $page_in_array = in_array($pageID, $visibility['pages']);
577 //for the compatibility of the data of lower version 3.8.10 and below
578 if (array_key_exists($pageID, $visibility['pages'])) {
579 if ($visibility['pages'][$pageID] == 1) {
580 $page_in_array = true;
581 }
582 }
583
584 //add parent inherit option
585 if ($is_inherit && $parentID && (array_key_exists($parentID, $visibility['pages']) || in_array($pageID, $visibility['pages']))) {
586 $visibility['pages'][] = $pageID;
587 // print_r( $visibility['pages'] );
588 }
589
590 if ($visibility_opts == 'hide' && $page_in_array) {
591 $hidden = true; //hide if exists on hidden pages
592 } elseif ($visibility_opts == 'show' && !$page_in_array) {
593 $hidden = true; //hide if doesn't exists on visible pages
594 }
595 }
596
597 if ((!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') {
598 $hidden = true; //hide if checked on visible pages but the visibilty_opts is show
599 }
600
601 //do return to bypass other conditions
602 $hidden = apply_filters('widget_options_visibility_page_block', $hidden);
603 if ($hidden) {
604 return false;
605 }
606 }
607 }
608 //end wordpress pages
609
610 //ACF
611 if (isset($widget_options['acf']) && 'activate' == $widget_options['acf']) {
612 if (isset($visibility['acf']['field']) && !empty($visibility['acf']['field'])) {
613 $acf = get_field_object($visibility['acf']['field']);
614 if ($acf && is_array($acf)) {
615 $acf_visibility = (isset($visibility['acf']) && isset($visibility['acf']['visibility'])) ? $visibility['acf']['visibility'] : 'hide';
616 //handle repeater fields
617 if (isset($acf['value'])) {
618 if (is_array($acf['value'])) {
619 $acf['value'] = implode(', ', array_map(function ($acf_array_value) {
620 if (!is_array($acf_array_value)) return $acf_array_value;
621
622 $acf_implode = implode(',', array_filter($acf_array_value));
623 return $acf_implode;
624 }, $acf['value']));
625 }
626 }
627 switch ($visibility['acf']['condition']) {
628 case 'equal':
629 if (isset($acf['value'])) {
630 if ('show' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) {
631 $hidden = false;
632 } else if ('show' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) {
633 $hidden = true;
634 } else if ('hide' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) {
635 $hidden = true;
636 } else if ('hide' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) {
637 $hidden = false;
638 }
639 }
640 break;
641 case 'not_equal':
642 if (isset($acf['value'])) {
643 if ('show' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) {
644 $hidden = true;
645 } else if ('show' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) {
646 $hidden = false;
647 } else if ('hide' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) {
648 $hidden = false;
649 } else if ('hide' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) {
650 $hidden = true;
651 }
652 }
653 break;
654 case 'contains':
655 if (isset($acf['value'])) {
656 if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) {
657 $hidden = false;
658 } else if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) {
659 $hidden = true;
660 } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) {
661 $hidden = true;
662 } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) {
663 $hidden = false;
664 }
665 }
666 break;
667 case 'not_contains':
668 if (isset($acf['value'])) {
669 if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) {
670 $hidden = true;
671 } else if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) {
672 $hidden = false;
673 } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) {
674 $hidden = false;
675 } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) {
676 $hidden = true;
677 }
678 }
679 break;
680 case 'empty':
681 if ('show' == $acf_visibility && empty($acf['value'])) {
682 $hidden = false;
683 } else if ('show' == $acf_visibility && !empty($acf['value'])) {
684 $hidden = true;
685 } elseif ('hide' == $acf_visibility && empty($acf['value'])) {
686 $hidden = true;
687 } else if ('hide' == $acf_visibility && !empty($acf['value'])) {
688 $hidden = false;
689 }
690 break;
691 case 'not_empty':
692 if ('show' == $acf_visibility && empty($acf['value'])) {
693 $hidden = true;
694 } else if ('show' == $acf_visibility && !empty($acf['value'])) {
695 $hidden = false;
696 } elseif ('hide' == $acf_visibility && empty($acf['value'])) {
697 $hidden = false;
698 } else if ('hide' == $acf_visibility && !empty($acf['value'])) {
699 $hidden = true;
700 }
701 break;
702
703 default:
704 # code...
705 break;
706 }
707
708 // //do return to bypass other conditions
709 $hidden = apply_filters('widget_options_visibility_acf_block', $hidden);
710 if ($hidden) {
711 return false;
712 }
713 }
714 }
715 }
716
717 //login state
718 if (isset($widget_options['state']) && 'activate' == $widget_options['state'] && isset($opts['roles'])) {
719 if (isset($opts['roles']['state']) && !empty($opts['roles']['state'])) {
720 //do state action here
721 if ($opts['roles']['state'] == 'out' && is_user_logged_in()) {
722 return false;
723 } else if ($opts['roles']['state'] == 'in' && !is_user_logged_in()) {
724 return false;
725 }
726 }
727 }
728
729 if ('activate' == $widget_options['logic']) {
730 // display widget logic
731 if (isset($opts['class']) && isset($opts['class']['logic']) && !empty($opts['class']['logic'])) {
732 $display_logic = stripslashes(trim($opts['class']['logic']));
733 $display_logic = apply_filters('widget_options_logic_override_block', $display_logic);
734 $display_logic = apply_filters('extended_widget_options_logic_override_block', $display_logic);
735 if ($display_logic === false) {
736 return false;
737 }
738 if ($display_logic === true) {
739 return true;
740 }
741 // if (stristr($display_logic, "return") === false) {
742 // $display_logic = "return (" . $display_logic . ");";
743 // }
744 $display_logic = htmlspecialchars_decode($display_logic, ENT_QUOTES);
745 if (!widgetopts_safe_eval($display_logic)) {
746 return false;
747 }
748 }
749 }
750
751 if ('activate' == $widget_options['hide_title']) {
752 //hide widget title
753 if (isset($instance['title']) && isset($opts['class']) && isset($opts['class']['title']) && '1' == $opts['class']['title']) {
754 $instance['title'] = '';
755 }
756 }
757
758 $block_content = widgetopts_add_classes_post_block($block_content, $parsed_block, $obj);
759 }
760
761 return $block_content;
762 }
763
764 /*
765 * Add custom classes on widget
766 */
767 function widgetopts_add_classes_post_block($block_content, $parsed_block, $obj)
768 {
769 global $widget_options, $wp_registered_widget_controls;
770 $classe_to_add = '';
771 $id_to_add = '';
772 $widget_id_set = '';
773 $data_attr = '';
774 $instance = $parsed_block['attrs'];
775
776 if (isset($instance)) {
777 $opts = (isset($instance['extended_widget_opts'])) ? $instance['extended_widget_opts'] : (isset($instance['extended_widget_opts_block']) ? $instance['extended_widget_opts_block'] : array());
778 } else {
779 $opts = array();
780 }
781
782 $custom_class = isset($opts['class']) ? $opts['class'] : '';
783
784 if ('activate' == $widget_options['classes'] && isset($widget_options['settings']['classes'])) {
785 //don't add the IDs when the setting is set to NO
786 if (isset($widget_options['settings']['classes']['id'])) {
787 if (is_array($custom_class) && isset($custom_class['id']) && !empty($custom_class['id'])) {
788 $id_to_add = $custom_class['id'];
789 $widget_id_set = $custom_class['id'];
790 }
791 }
792 }
793
794 $get_classes = widgetopts_classes_generator($opts, $widget_options, $widget_options['settings']);
795
796 //double check array
797 if (!is_array($get_classes)) {
798 $get_classes = array();
799 }
800
801 if (!empty($get_classes)) {
802 $classe_to_add .= (implode(' ', $get_classes)) . ' ';
803 //$block_content = preg_replace('class="', $classes, $block_content, 1);
804 }
805
806 // $params[0]['before_widget'] = str_replace('class="', ' data-animation="asdf" class="', $params[0]['before_widget']);
807
808 $match = [];
809 $has_match = preg_match('/<\w*[^>]*>/', $block_content, $match);
810
811 if ($has_match == 1) {
812 if (!empty($id_to_add)) {
813 $has_match_id = preg_match('/[id="]/', $match[0]);
814 if ($has_match_id == 1) {
815 $block_content = preg_replace('/id="[^"]*/', "id=\"{$id_to_add}", $block_content, 1);
816 } else {
817 $block_content = preg_replace('/>/', " id=\"{$id_to_add}\">", $block_content, 1);
818 }
819 }
820
821 if (!empty($classe_to_add)) {
822 $has_match_class = preg_match('/[class="]/', $match[0]);
823 if ($has_match_class == 1) {
824 $block_content = preg_replace('/class="/', "class=\"{$classe_to_add}", $block_content, 1);
825 } else {
826 $block_content = preg_replace('/>/', " class=\"{$classe_to_add}\">", $block_content, 1);
827 }
828 }
829
830 if (!empty($data_attr)) {
831 $block_content = preg_replace('/>/', " {$data_attr}>", $block_content, 1);
832 }
833 }
834
835 return $block_content;
836 }
837
838
839 /**
840 * Gutenberg ajax functions
841 */
842 function widgetopts_get_types()
843 {
844 global $widgetopts_types;
845 if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) {
846 die;
847 }
848
849 wp_send_json_success(((!empty($widgetopts_types)) ? $widgetopts_types : widgetopts_global_types()));
850 die;
851 }
852 add_action('wp_ajax_widgetopts_get_types', 'widgetopts_get_types');
853
854
855 function widgetopts_get_taxonomies()
856 {
857 global $widgetopts_taxonomies;
858 if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) {
859 die;
860 }
861
862 wp_send_json_success(((!empty($widgetopts_taxonomies)) ? $widgetopts_taxonomies : widgetopts_global_taxonomies()));
863 die;
864 }
865 add_action('wp_ajax_widgetopts_get_taxonomies', 'widgetopts_get_taxonomies');
866
867 function widgetopts_acf_get_field_groups()
868 {
869 if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) {
870 die;
871 }
872
873 $fields = array();
874 if (function_exists('acf_get_field_groups')) {
875 $groups = acf_get_field_groups();
876 if (is_array($groups)) {
877 foreach ($groups as $group) {
878 $fields[$group['ID']] = array('title' => $group['title'], 'fields' => acf_get_fields($group));
879 }
880 }
881 } else {
882 $groups = apply_filters('acf/get_field_groups', array());
883 if (is_array($groups)) {
884 foreach ($groups as $group) {
885 $fields[$group['id']] = array('title' => $group['title'], 'fields' => apply_filters('acf/field_group/get_fields', array(), $group['id']));
886 }
887 }
888 }
889
890 wp_send_json_success($fields);
891 die;
892 }
893 add_action('wp_ajax_widgetopts_acf_get_field_groups', 'widgetopts_acf_get_field_groups');
894
895 function widgetopts_get_legacy_data()
896 {
897 if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) {
898 die;
899 }
900
901 if (isset($_POST['id_base'])) {
902 wp_send_json_success(array());
903 die;
904 }
905 $settings = array();
906 $settings = get_option('widget_' + sanitize_key($_POST['id_base']));
907
908 if (false === $settings) {
909 return $settings;
910 }
911
912 if (!is_array($settings) && !($settings instanceof ArrayObject || $settings instanceof ArrayIterator)) {
913 $settings = array();
914 }
915
916 wp_send_json_success($settings);
917 die;
918 }
919 add_action('wp_ajax_widgetopts_get_legacy_data', 'widgetopts_get_legacy_data');
920
921 function widgetopts_get_settings_ajax()
922 {
923 if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) {
924 die;
925 }
926 $settings = widgetopts_get_settings();
927
928 if (!current_user_can('administrator')) {
929 $settings['logic'] = 'deactivate';
930 }
931
932 wp_send_json_success($settings);
933 die;
934 }
935 add_action('wp_ajax_widgetopts_get_settings_ajax', 'widgetopts_get_settings_ajax');
936
937 function widgetopts_get_pages()
938 {
939 if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) {
940 die;
941 }
942
943 $pages = [];
944
945 $pargs = array(
946 'hierarchical' => true,
947 'child_of' => 0, // Display all pages regardless of parent
948 'parent' => -1, // Display all pages regardless of parent
949 'sort_order' => 'ASC',
950 'sort_column' => 'menu_order, post_title'
951 );
952
953 $pageLoop = get_pages($pargs);
954
955 if ($pageLoop) {
956 foreach ($pageLoop as $objPage) {
957 $depth = count(get_ancestors($objPage->ID, 'page'));
958 // Determine indentation for hierarchical display
959 $indent = str_repeat('-', $depth);
960 $objPage->post_title = $indent . "" . $objPage->post_title;
961 }
962 $pages = $pageLoop;
963 }
964
965 wp_send_json_success($pages);
966 die;
967 }
968 add_action('wp_ajax_widgetopts_get_pages', 'widgetopts_get_pages');
969
970 function widgetopts_get_terms()
971 {
972 if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) {
973 die;
974 }
975
976 $terms = array();
977
978 $_terms = array();
979 $_terms = get_terms();
980
981 if (!is_wp_error($_terms)) {
982 foreach ($_terms as $t) {
983 $terms[$t->name][] = $t;
984 }
985 }
986
987 wp_send_json_success($terms);
988 die;
989 }
990 add_action('wp_ajax_widgetopts_get_terms', 'widgetopts_get_terms');
991
992 function widgetopts_get_users()
993 {
994 global $wp_version;
995
996 if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) {
997 die;
998 }
999
1000 $authors = array();
1001
1002 $args = array();
1003
1004 $is_6_3_and_above = version_compare($wp_version, '6.3', '>=');
1005 if ($is_6_3_and_above) {
1006 $args['cache_results'] = apply_filters('cache_widgetopts_ajax_taxonomy_search', true);
1007 }
1008
1009 $_authors = get_users($args);
1010
1011 if (!empty($_authors)) {
1012
1013 if (is_iterable($_authors)) {
1014 foreach ($_authors as $a) {
1015 $displayname = isset($a->display_name) ? $a->display_name : (isset($a->data) && isset($a->data->display_name) ? $a->data->display_name : '');
1016 $authors[] = ["ID" => $a->ID, "display_name" => $displayname];
1017 }
1018 }
1019 }
1020
1021 wp_send_json_success($authors);
1022 die;
1023 }
1024 add_action('wp_ajax_widgetopts_get_users', 'widgetopts_get_users');
1025
1026 function widgetopts_ajax_roles_search_block()
1027 {
1028 if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) {
1029 die;
1030 }
1031 $response = [
1032 'results' => [],
1033 'pagination' => ['more' => false]
1034 ];
1035
1036 $term = isset($_POST['term']) && !empty($_POST['term']) ? $_POST['term'] : '';
1037
1038 $roles = get_editable_roles();
1039 if (!empty($roles)) {
1040 foreach ($roles as $role_name => $role_info) {
1041 // if ((!empty($term) && stristr($role_name, $term) !== false) || stristr($role_name, $role_info['name']) !== false) {
1042 $response['results'][] = [
1043 'id' => $role_name,
1044 'text' => $role_info['name']
1045 ];
1046 // }
1047 }
1048 }
1049
1050 wp_send_json_success($response);
1051 die;
1052 }
1053 add_action('wp_ajax_widgetopts_ajax_roles_search_block', 'widgetopts_ajax_roles_search_block');
1054