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