gutenberg-toolbar.php
1325 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 | // Protect legacy display logic: always restore old DB value (prevents injection AND data loss) |
| 205 | if (isset($instance['extended_widget_opts-' . $obj->id]['class'])) { |
| 206 | $cls = &$instance['extended_widget_opts-' . $obj->id]['class']; |
| 207 | $wopt_ver = isset($cls['wopt_version']) ? $cls['wopt_version'] : ''; |
| 208 | $has_snippet = !empty($cls['logic_snippet_id']); |
| 209 | $has_legacy = !empty($cls['logic']); |
| 210 | |
| 211 | // If wopt_version >= 4.2: legacy logic is obsolete — clear it |
| 212 | if ($wopt_ver !== '' && version_compare($wopt_ver, '4.2', '>=')) { |
| 213 | if ($has_legacy) { |
| 214 | $cls['logic'] = ''; |
| 215 | } |
| 216 | } |
| 217 | // Auto-clear legacy logic if snippet_id is already set (migration done) |
| 218 | elseif ($has_snippet) { |
| 219 | $cls['logic'] = ''; |
| 220 | $cls['wopt_version'] = WIDGETOPTS_VERSION; |
| 221 | } |
| 222 | // User intentionally cleared legacy logic via Clear button |
| 223 | elseif (!empty($cls['logic_cleared'])) { |
| 224 | $cls['logic'] = ''; |
| 225 | unset($cls['logic_cleared']); |
| 226 | $cls['wopt_version'] = WIDGETOPTS_VERSION; |
| 227 | } else { |
| 228 | $old_logic = ''; |
| 229 | if (isset($old_instance['extended_widget_opts-' . $obj->id]['class']['logic']) && $old_instance['extended_widget_opts-' . $obj->id]['class']['logic'] !== '') { |
| 230 | $old_logic = $old_instance['extended_widget_opts-' . $obj->id]['class']['logic']; |
| 231 | } |
| 232 | $cls['logic'] = $old_logic; |
| 233 | } |
| 234 | unset($cls); |
| 235 | } |
| 236 | |
| 237 | return $instance; |
| 238 | }, 100, 4); |
| 239 | } |
| 240 | |
| 241 | add_filter('rest_pre_insert_post', 'widgetopts_rest_pre_insert', 10, 2); |
| 242 | add_filter('rest_pre_insert_page', 'widgetopts_rest_pre_insert', 10, 2); |
| 243 | |
| 244 | function widgetopts_rest_pre_insert($post, $request) |
| 245 | { |
| 246 | if (!current_user_can('edit_posts')) { |
| 247 | return $post; // Security check: Only users with permission can edit |
| 248 | } |
| 249 | |
| 250 | if (current_user_can('administrator')) { |
| 251 | // Admins can modify all attributes EXCEPT legacy display logic (security) |
| 252 | if (!empty($post->post_content) && !empty($post->ID)) { |
| 253 | $old_post = get_post($post->ID); |
| 254 | if ($old_post && !empty($old_post->post_content)) { |
| 255 | $old_blocks = parse_blocks($old_post->post_content); |
| 256 | $new_blocks = parse_blocks($post->post_content); |
| 257 | if (is_array($new_blocks) && !empty($new_blocks)) { |
| 258 | $old_blocks_lkp = []; |
| 259 | widgetopt_process_blocks_recursively($old_blocks, $old_blocks_lkp); |
| 260 | foreach ($new_blocks as &$nb) { |
| 261 | widgetopt_modify_block_attributes($nb, $old_blocks_lkp); |
| 262 | } |
| 263 | $post->post_content = serialize_blocks($new_blocks); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | return $post; |
| 268 | } |
| 269 | |
| 270 | if (empty($post->post_content) || empty($post->ID)) { |
| 271 | return $post; // Exit if no content or post ID |
| 272 | } |
| 273 | |
| 274 | // Get the old post content before editing |
| 275 | $old_post = get_post($post->ID); |
| 276 | |
| 277 | // Parse blocks from the old post content (recursively) |
| 278 | $old_blocks = !$old_post ? [] : parse_blocks($old_post->post_content); |
| 279 | // Parse blocks from the new post content |
| 280 | $new_blocks = parse_blocks($post->post_content); |
| 281 | |
| 282 | if (!is_array($new_blocks) || empty($new_blocks)) { |
| 283 | return $post; // Exit if no blocks found |
| 284 | } |
| 285 | |
| 286 | // Convert old blocks into a lookup table using anchor or unique ID (recursively process) |
| 287 | $old_blocks_lookup = []; |
| 288 | widgetopt_process_blocks_recursively($old_blocks, $old_blocks_lookup); |
| 289 | |
| 290 | foreach ($new_blocks as &$new_block) { |
| 291 | widgetopt_modify_block_attributes($new_block, $old_blocks_lookup); |
| 292 | } |
| 293 | |
| 294 | // Convert modified blocks back to post content |
| 295 | $post->post_content = serialize_blocks($new_blocks); |
| 296 | |
| 297 | return $post; |
| 298 | } |
| 299 | |
| 300 | // Recursively process blocks (both parent and nested inner blocks) |
| 301 | function widgetopt_process_blocks_recursively(&$blocks, &$old_blocks_lookup) |
| 302 | { |
| 303 | foreach ($blocks as &$block) { |
| 304 | if (!isset($block['blockName'])) { |
| 305 | continue; // Skip invalid blocks |
| 306 | } |
| 307 | |
| 308 | // Use anchor as the unique identifier or generate one if not available |
| 309 | $anchor = $block['attrs']['anchor'] ?? md5(json_encode($block['innerContent'])); // Generate unique ID if missing |
| 310 | |
| 311 | // Store or compare the block's attributes |
| 312 | $old_blocks_lookup[$anchor] = [ |
| 313 | 'attrs' => $block['attrs'] ?? [], |
| 314 | ]; |
| 315 | |
| 316 | // If the block has inner blocks, recurse through them |
| 317 | if (isset($block['innerBlocks']) && !empty($block['innerBlocks'])) { |
| 318 | widgetopt_process_blocks_recursively($block['innerBlocks'], $old_blocks_lookup); // Recursively process inner blocks |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // Recursively modify block attributes (parent and inner blocks) |
| 324 | function widgetopt_modify_block_attributes(&$block, $old_blocks_lookup) |
| 325 | { |
| 326 | if (!isset($block['blockName'])) { |
| 327 | return; // Skip invalid blocks |
| 328 | } |
| 329 | |
| 330 | // Find the old block using the anchor (or generated unique ID) |
| 331 | $anchor = $block['attrs']['anchor'] ?? md5(json_encode($block['innerContent'])); // Generate unique ID if missing |
| 332 | $old_data = $old_blocks_lookup[$anchor] ?? []; |
| 333 | $old_attrs = $old_data['attrs'] ?? []; |
| 334 | |
| 335 | //do the modification |
| 336 | if (isset($block['attrs']['extended_widget_opts'])) { |
| 337 | if (isset($block['attrs']['extended_widget_opts']['class'])) { |
| 338 | $cls = &$block['attrs']['extended_widget_opts']['class']; |
| 339 | $wopt_ver = isset($cls['wopt_version']) ? $cls['wopt_version'] : ''; |
| 340 | $has_snippet = !empty($cls['logic_snippet_id']); |
| 341 | $has_legacy = !empty($cls['logic']); |
| 342 | |
| 343 | // If wopt_version >= 4.2: legacy logic is obsolete — clear it |
| 344 | if ($wopt_ver !== '' && version_compare($wopt_ver, '4.2', '>=')) { |
| 345 | if ($has_legacy) { |
| 346 | $cls['logic'] = ''; |
| 347 | } |
| 348 | } |
| 349 | // Auto-clear legacy logic if snippet_id is already set (migration done) |
| 350 | elseif ($has_snippet) { |
| 351 | $cls['logic'] = ''; |
| 352 | $cls['wopt_version'] = WIDGETOPTS_VERSION; |
| 353 | } |
| 354 | // User intentionally cleared legacy logic via Clear button |
| 355 | elseif (!empty($cls['logic_cleared'])) { |
| 356 | $cls['logic'] = ''; |
| 357 | unset($cls['logic_cleared']); |
| 358 | $cls['wopt_version'] = WIDGETOPTS_VERSION; |
| 359 | } elseif (isset($old_attrs['extended_widget_opts']) && isset($old_attrs['extended_widget_opts']['class']) && isset($old_attrs['extended_widget_opts']['class']['logic']) && !empty($old_attrs['extended_widget_opts']['class']['logic'])) { |
| 360 | $cls['logic'] = $old_attrs['extended_widget_opts']['class']['logic']; |
| 361 | } else { |
| 362 | $cls['logic'] = ''; |
| 363 | } |
| 364 | unset($cls); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // If the block has inner blocks, recurse through them |
| 369 | if (isset($block['innerBlocks']) && !empty($block['innerBlocks'])) { |
| 370 | foreach ($block['innerBlocks'] as &$inner_block) { |
| 371 | widgetopt_modify_block_attributes($inner_block, $old_blocks_lookup); // Recursively modify inner blocks |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Protect legacy display logic from injection on ALL save paths |
| 378 | * (classic editor, programmatic saves, direct DB manipulation via forms). |
| 379 | * Covers cases that rest_pre_insert_post/page don't catch. |
| 380 | * |
| 381 | * @since 5.1 |
| 382 | */ |
| 383 | add_filter('wp_insert_post_data', function($data, $postarr) { |
| 384 | if (empty($data['post_content']) || empty($postarr['ID'])) { |
| 385 | return $data; |
| 386 | } |
| 387 | |
| 388 | // Only process if content has blocks with extended_widget_opts |
| 389 | if (strpos($data['post_content'], 'extended_widget_opts') === false) { |
| 390 | return $data; |
| 391 | } |
| 392 | |
| 393 | $old_post = get_post($postarr['ID']); |
| 394 | if (!$old_post || empty($old_post->post_content)) { |
| 395 | // New post — strip any logic fields entirely (no old data to preserve) |
| 396 | $new_blocks = parse_blocks($data['post_content']); |
| 397 | if (!is_array($new_blocks) || empty($new_blocks)) return $data; |
| 398 | $changed = false; |
| 399 | $strip_logic = function(&$blocks) use (&$strip_logic, &$changed) { |
| 400 | foreach ($blocks as &$block) { |
| 401 | if (isset($block['attrs']['extended_widget_opts']['class']['logic']) && $block['attrs']['extended_widget_opts']['class']['logic'] !== '') { |
| 402 | $block['attrs']['extended_widget_opts']['class']['logic'] = ''; |
| 403 | $changed = true; |
| 404 | } |
| 405 | if (!empty($block['innerBlocks'])) $strip_logic($block['innerBlocks']); |
| 406 | } |
| 407 | }; |
| 408 | $strip_logic($new_blocks); |
| 409 | if ($changed) { |
| 410 | $data['post_content'] = serialize_blocks($new_blocks); |
| 411 | } |
| 412 | return $data; |
| 413 | } |
| 414 | |
| 415 | $old_blocks = parse_blocks($old_post->post_content); |
| 416 | $new_blocks = parse_blocks($data['post_content']); |
| 417 | if (!is_array($new_blocks) || empty($new_blocks)) return $data; |
| 418 | |
| 419 | $old_blocks_lkp = []; |
| 420 | widgetopt_process_blocks_recursively($old_blocks, $old_blocks_lkp); |
| 421 | |
| 422 | foreach ($new_blocks as &$nb) { |
| 423 | widgetopt_modify_block_attributes($nb, $old_blocks_lkp); |
| 424 | } |
| 425 | |
| 426 | $data['post_content'] = serialize_blocks($new_blocks); |
| 427 | return $data; |
| 428 | }, 10, 2); |
| 429 | |
| 430 | add_filter('render_block', function ($block_content, $parsed_block, $obj) { |
| 431 | if (!is_admin()) { |
| 432 | add_filter("render_block_{$obj->name}", "blockopts_filter_before_display", 100, 3); |
| 433 | } |
| 434 | return $block_content; |
| 435 | }, 100, 3); |
| 436 | |
| 437 | function blockopts_filter_before_display($block_content, $parsed_block, $obj) |
| 438 | { |
| 439 | //for freeform filter and assignment |
| 440 | if (is_null($parsed_block['blockName']) || empty($parsed_block['blockName'])) { |
| 441 | if (!isset($parsed_block['attrs']) || empty($parsed_block['attrs'])) { |
| 442 | $result = null; |
| 443 | if (isset($parsed_block['innerContent']) && !empty($parsed_block['innerContent'][0])) { |
| 444 | $is_okay = preg_match("/<!--start_widgetopts[\s]+[{\":,}\w\W]*[\s]+end_widgetopts-->/", $parsed_block['innerContent'][0], $result); |
| 445 | if ($is_okay === 1) { |
| 446 | if (!is_null($result) && is_array($result)) { |
| 447 | $content = str_replace('<!--start_widgetopts', '', $result[0]); |
| 448 | $content = str_replace('end_widgetopts-->', '', $content); |
| 449 | $content = trim($content); |
| 450 | $parsed_block['attrs']['extended_widget_opts'] = json_decode($content, true); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (isset($parsed_block) && isset($parsed_block['attrs']) && (isset($parsed_block['attrs']['extended_widget_opts']) || isset($parsed_block['attrs']['extended_widget_opts_block']))) { |
| 458 | global $widget_options, $current_user; |
| 459 | $instance = $parsed_block['attrs']; |
| 460 | |
| 461 | //if idbase is not -1 it is a widget |
| 462 | 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) { |
| 463 | return $block_content; |
| 464 | } |
| 465 | |
| 466 | // WPML FIX |
| 467 | $hasWPML = has_filter('wpml_current_language'); |
| 468 | $hasWPML = (function_exists('pll_the_languages')) ? false : $hasWPML; |
| 469 | $default_language = $hasWPML ? apply_filters('wpml_default_language', NULL) : false; |
| 470 | |
| 471 | $hidden = false; |
| 472 | $opts = (isset($instance['extended_widget_opts'])) ? $instance['extended_widget_opts'] : (isset($instance['extended_widget_opts_block']) ? $instance['extended_widget_opts_block'] : array()); |
| 473 | $visibility = array('show' => array(), 'hide' => array()); |
| 474 | $tax_opts = (isset($widget_options['settings']) && isset($widget_options['settings']['taxonomies_keys'])) ? $widget_options['settings']['taxonomies_keys'] : array(); |
| 475 | |
| 476 | $visibility = isset($opts['visibility']) ? $opts['visibility'] : array(); |
| 477 | $visibility_opts = isset($opts['visibility']['options']) ? $opts['visibility']['options'] : 'hide'; |
| 478 | $authorPageSelection = ""; |
| 479 | |
| 480 | //wordpress pages |
| 481 | $is_misc = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['misc'])) ? true : false; |
| 482 | $is_types = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['post_type'])) ? true : false; |
| 483 | $is_tax = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['taxonomies'])) ? true : false; |
| 484 | $is_inherit = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['inherit'])) ? true : false; |
| 485 | |
| 486 | //WOOCOMMERCE |
| 487 | $isWooPage = false; |
| 488 | if (class_exists('WooCommerce')) { |
| 489 | $wooPageID = 0; |
| 490 | |
| 491 | $wooPageID = (is_shop()) ? get_option('woocommerce_shop_page_id') : $wooPageID; |
| 492 | if ($wooPageID) { |
| 493 | $isWooPage = true; |
| 494 | |
| 495 | $visibility['pages'] = !empty($visibility['pages']) ? $visibility['pages'] : []; |
| 496 | if ($visibility_opts == 'hide' && (array_key_exists($wooPageID, $visibility['pages']) || in_array($wooPageID, $visibility['pages']))) { |
| 497 | $hidden = true; //hide if exists on hidden pages |
| 498 | } elseif ($visibility_opts == 'show' && (!array_key_exists($wooPageID, $visibility['pages']) && !in_array($wooPageID, $visibility['pages']))) { |
| 499 | $hidden = true; //hide if doesn't exists on visible pages |
| 500 | } |
| 501 | |
| 502 | //do return to bypass other conditions |
| 503 | $hidden = apply_filters('widget_options_visibility_page_block', $hidden); |
| 504 | |
| 505 | if ($hidden) { |
| 506 | return false; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // Normal Pages |
| 512 | if (!$isWooPage) { |
| 513 | if ($is_misc && ((is_home() && is_front_page()) || is_front_page())) { |
| 514 | if (isset($visibility['misc']['home']) && $visibility_opts == 'hide') { |
| 515 | $hidden = true; //hide if checked on hidden pages |
| 516 | } elseif (!isset($visibility['misc']['home']) && $visibility_opts == 'show') { |
| 517 | $hidden = true; //hide if not checked on visible pages |
| 518 | } |
| 519 | |
| 520 | if (isset($visibility['misc']['home']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') { |
| 521 | $hidden = true; //hide if checked on visible pages but the visibilty_opts is show |
| 522 | } |
| 523 | |
| 524 | //do return to bypass other conditions |
| 525 | $hidden = apply_filters('widget_options_visibility_home_block', $hidden); |
| 526 | if ($hidden) { |
| 527 | return false; |
| 528 | } |
| 529 | } elseif ($is_misc && is_home()) { //filter for blog page |
| 530 | if (isset($visibility['misc']['blog']) && $visibility_opts == 'hide') { |
| 531 | $hidden = true; //hide if checked on hidden pages |
| 532 | } elseif (!isset($visibility['misc']['blog']) && $visibility_opts == 'show') { |
| 533 | $hidden = true; //hide if not checked on visible pages |
| 534 | } |
| 535 | |
| 536 | if (isset($visibility['misc']['blog']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') { |
| 537 | $hidden = true; //hide if checked on visible pages but the visibilty_opts is show |
| 538 | } |
| 539 | |
| 540 | //do return to bypass other conditions |
| 541 | $hidden = apply_filters('widget_options_visibility_blog_block', $hidden); |
| 542 | if ($hidden) { |
| 543 | return false; |
| 544 | } |
| 545 | } elseif ($is_tax && is_category() && is_array($tax_opts) && in_array('category', $tax_opts)) { |
| 546 | if (!isset($visibility['categories'])) { |
| 547 | $visibility['categories'] = array(); |
| 548 | } |
| 549 | |
| 550 | $cat_lists = array(); |
| 551 | |
| 552 | if (isset($visibility['tax_terms']['category'])) { |
| 553 | $cat_lists = $visibility['tax_terms']['category']; |
| 554 | } elseif (isset($visibility['categories'])) { |
| 555 | $cat_lists = $visibility['categories']; |
| 556 | } |
| 557 | |
| 558 | // WPML TRANSLATION OBJECT FIX |
| 559 | $category_id = ($hasWPML) ? apply_filters('wpml_object_id', get_query_var('cat'), 'category', true, $default_language) : get_query_var('cat'); |
| 560 | |
| 561 | if (!isset($visibility['taxonomies']['category']) && $visibility_opts == 'hide' && (array_key_exists($category_id, $cat_lists) || in_array($category_id, $cat_lists))) { |
| 562 | $hidden = true; //hide if exists on hidden pages |
| 563 | } elseif (!isset($visibility['taxonomies']['category']) && $visibility_opts == 'show' && (!array_key_exists($category_id, $cat_lists) && !in_array($category_id, $cat_lists))) { |
| 564 | $hidden = true; //hide if doesn't exists on visible pages |
| 565 | } elseif (isset($visibility['taxonomies']['category']) && $visibility_opts == 'hide') { |
| 566 | $hidden = true; //hide to all categories |
| 567 | } elseif (isset($visibility['taxonomies']['category']) && $visibility_opts == 'show') { |
| 568 | $hidden = false; //hide to all categories |
| 569 | } |
| 570 | |
| 571 | if (isset($visibility['taxonomies']['category']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') { |
| 572 | $hidden = true; //hide if checked on visible pages but the visibilty_opts is show |
| 573 | } |
| 574 | |
| 575 | //do return to bypass other conditions |
| 576 | $hidden = apply_filters('widget_options_visibility_categories_block', $hidden); |
| 577 | if ($hidden) { |
| 578 | return false; |
| 579 | } |
| 580 | } elseif ($is_tax && is_tag() && is_array($tax_opts) && in_array('post_tag', $tax_opts)) { |
| 581 | if (!isset($visibility['tags'])) { |
| 582 | $visibility['tags'] = array(); |
| 583 | } |
| 584 | |
| 585 | $tag_lists = (isset($visibility['tax_terms']['post_tag'])) ? $visibility['tax_terms']['post_tag'] : array(); |
| 586 | |
| 587 | // WPML TRANSLATION OBJECT FIX |
| 588 | $tag_id = ($hasWPML) ? apply_filters('wpml_object_id', get_query_var('tag_id'), 'post_tag', true, $default_language) : get_query_var('tag_id'); |
| 589 | |
| 590 | if (!isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'hide' && (array_key_exists($tag_id, $tag_lists) || in_array($tag_id, $tag_lists))) { |
| 591 | $hidden = true; //hide if exists on hidden pages |
| 592 | } elseif (!isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'show' && (!array_key_exists($tag_id, $tag_lists) && !in_array($tag_id, $tag_lists))) { |
| 593 | $hidden = true; //hide if doesn't exists on visible pages |
| 594 | } elseif (isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'hide') { |
| 595 | $hidden = true; //hide to all tags |
| 596 | } elseif (isset($visibility['taxonomies']['post_tag']) && $visibility_opts == 'show') { |
| 597 | $hidden = false; //hide to all tags |
| 598 | } |
| 599 | |
| 600 | if (isset($visibility['taxonomies']['post_tag']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') { |
| 601 | $hidden = true; //hide if checked on visible pages but the visibilty_opts is show |
| 602 | } |
| 603 | |
| 604 | //do return to bypass other conditions |
| 605 | $hidden = apply_filters('widget_options_visibility_tags_block', $hidden); |
| 606 | if ($hidden) { |
| 607 | return false; |
| 608 | } |
| 609 | } elseif ($is_tax && is_tax()) { |
| 610 | $term = get_queried_object(); |
| 611 | $term_lists = array(); |
| 612 | |
| 613 | if (isset($visibility['tax_terms']) && isset($visibility['tax_terms'][$term->taxonomy])) { |
| 614 | $term_lists = $visibility['tax_terms'][$term->taxonomy]; |
| 615 | } |
| 616 | |
| 617 | // WPML TRANSLATION OBJECT FIX |
| 618 | $term_id = ($hasWPML) ? apply_filters('wpml_object_id', $term->term_id, $term->taxonomy, true, $default_language) : $term->term_id; |
| 619 | |
| 620 | 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))) { |
| 621 | $hidden = true; //hide if exists on hidden pages |
| 622 | } 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))) { |
| 623 | $hidden = true; //hide if doesn't exists on visible pages |
| 624 | } elseif (isset($visibility['taxonomies']) && isset($visibility['taxonomies'][$term->taxonomy]) && $visibility_opts == 'hide') { |
| 625 | $hidden = true; //hide to all tags |
| 626 | } elseif (isset($visibility['taxonomies']) && isset($visibility['taxonomies'][$term->taxonomy]) && $visibility_opts == 'show') { |
| 627 | $hidden = false; //hide to all tags |
| 628 | } 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))) { |
| 629 | $hidden = true; //hide if exists on hidden pages |
| 630 | } 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))) { |
| 631 | $hidden = false; //hide if doesn't exists on visible pages |
| 632 | } elseif (!isset($visibility['taxonomies']) && $visibility_opts == 'show') { |
| 633 | $hidden = true; //hide if checked on hidden pages |
| 634 | } |
| 635 | |
| 636 | if (isset($visibility['taxonomies']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') { |
| 637 | $hidden = true; //hide if checked on visible pages but the visibilty_opts is show |
| 638 | } |
| 639 | |
| 640 | //do return to bypass other conditions |
| 641 | $hidden = apply_filters('widget_options_visibility_taxonomies_block', $hidden); |
| 642 | if ($hidden) { |
| 643 | return false; |
| 644 | } |
| 645 | } elseif ($is_misc && is_archive()) { |
| 646 | if (isset($visibility['misc']['archives']) && $visibility_opts == 'hide') { |
| 647 | $hidden = true; //hide if checked on hidden pages |
| 648 | } elseif (!isset($visibility['misc']['archives']) && $visibility_opts == 'show') { |
| 649 | $hidden = true; //hide if not checked on visible pages |
| 650 | } else if (isset($visibility['misc']['archives']) && (!empty($authorPageSelection) && $authorPageSelection == '3') && $visibility_opts == 'show') { |
| 651 | $hidden = true; //hide if checked on visible pages but the visibilty_opts is show |
| 652 | } |
| 653 | |
| 654 | //do return to bypass other conditions |
| 655 | $hidden = apply_filters('widget_options_visibility_archives_block', $hidden); |
| 656 | |
| 657 | if ($hidden) { |
| 658 | return false; |
| 659 | } |
| 660 | } elseif (is_post_type_archive()) { |
| 661 | if (!isset($visibility['types']) || ($is_types && !isset($visibility['types']))) { |
| 662 | $visibility['types'] = array(); |
| 663 | } |
| 664 | |
| 665 | $current_type_archive = get_post_type(); |
| 666 | if (!empty($current_type_archive)) { |
| 667 | if ($visibility_opts == 'hide' && array_key_exists($current_type_archive, $visibility['types'])) { |
| 668 | $hidden = true; //hide if exists on hidden pages |
| 669 | } elseif ($visibility_opts == 'show' && !array_key_exists($current_type_archive, $visibility['types'])) { |
| 670 | $hidden = true; //hide if doesn't exists on visible pages |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if (array_key_exists($current_type_archive, $visibility['types']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') { |
| 675 | $hidden = true; //hide if checked on visible pages but the visibilty_opts is show |
| 676 | } |
| 677 | |
| 678 | //do return to bypass other conditions |
| 679 | $hidden = apply_filters('widget_options_visibility_post_type_archive_block', $hidden); |
| 680 | if ($hidden) { |
| 681 | return false; |
| 682 | } |
| 683 | } elseif ($is_misc && is_404()) { |
| 684 | if (isset($visibility['misc']['404']) && $visibility_opts == 'hide') { |
| 685 | $hidden = true; //hide if checked on hidden pages |
| 686 | } elseif (!isset($visibility['misc']['404']) && $visibility_opts == 'show') { |
| 687 | $hidden = true; //hide if not checked on visible pages |
| 688 | } |
| 689 | |
| 690 | if (isset($visibility['misc']['404']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') { |
| 691 | $hidden = true; //hide if checked on visible pages but the visibilty_opts is show |
| 692 | } |
| 693 | |
| 694 | //do return to bypass other conditions |
| 695 | $hidden = apply_filters('widget_options_visibility_404_block', $hidden); |
| 696 | if ($hidden) { |
| 697 | return false; |
| 698 | } |
| 699 | } elseif ($is_misc && is_search()) { |
| 700 | if (isset($visibility['misc']['search']) && $visibility_opts == 'hide') { |
| 701 | $hidden = true; //hide if checked on hidden pages |
| 702 | } elseif (!isset($visibility['misc']['search']) && $visibility_opts == 'show') { |
| 703 | $hidden = true; //hide if not checked on visible pages |
| 704 | } |
| 705 | |
| 706 | if (isset($visibility['misc']['search']) && (!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') { |
| 707 | $hidden = true; //hide if checked on visible pages but the visibilty_opts is show |
| 708 | } |
| 709 | |
| 710 | //do return to bypass other conditions |
| 711 | $hidden = apply_filters('widget_options_visibility_search_block', $hidden); |
| 712 | if ($hidden) { |
| 713 | return false; |
| 714 | } |
| 715 | } elseif (is_single() && !is_page()) { |
| 716 | global $post; |
| 717 | if (!isset($visibility['types']) || ($is_types && !isset($visibility['types']))) { |
| 718 | $visibility['types'] = array(); |
| 719 | } |
| 720 | if ($visibility_opts == 'hide' && array_key_exists($post->post_type, $visibility['types'])) { |
| 721 | $hidden = true; //hide if exists on hidden pages |
| 722 | } elseif ($visibility_opts == 'show' && !array_key_exists($post->post_type, $visibility['types'])) { |
| 723 | $hidden = true; //hide if doesn't exists on visible pages |
| 724 | } |
| 725 | |
| 726 | if ((!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') { |
| 727 | $hidden = true; //hide if checked on visible pages but the visibilty_opts is show |
| 728 | } |
| 729 | |
| 730 | // do return to bypass other conditions |
| 731 | $hidden = apply_filters('widget_options_visibility_types_block', $hidden); |
| 732 | |
| 733 | $taxonomy_names = get_post_taxonomies(); |
| 734 | $array_intersect = array_intersect($tax_opts, $taxonomy_names); |
| 735 | // print_r( $tax_opts ); |
| 736 | if (!isset($visibility['tax_terms']['category']) && isset($visibility['categories'])) { |
| 737 | $visibility['tax_terms']['category'] = $visibility['categories']; |
| 738 | } |
| 739 | |
| 740 | // WPML FIX |
| 741 | $postID = ($hasWPML) ? apply_filters('wpml_object_id', $post->ID, $post->post_type, true, $default_language) : $post->ID; |
| 742 | |
| 743 | if (!empty($array_intersect)) { |
| 744 | foreach ($array_intersect as $tax_key => $tax_value) { |
| 745 | if (in_array($tax_value, $tax_opts) && isset($visibility['tax_terms']) && isset($visibility['tax_terms'][$tax_value]) && !empty($visibility['tax_terms'][$tax_value])) { |
| 746 | $term_list = wp_get_post_terms($postID, $tax_value, array("fields" => "ids")); |
| 747 | |
| 748 | // WPML TRANSLATION OBJECT FIX |
| 749 | if ($hasWPML) { |
| 750 | $temp_term_list = []; |
| 751 | foreach ($term_list as $index => $termID) { |
| 752 | $temp_term_list[] = apply_filters('wpml_object_id', $termID, $tax_value, true, $default_language); |
| 753 | } |
| 754 | $term_list = (!empty($temp_term_list)) ? $temp_term_list : $term_list; |
| 755 | } |
| 756 | |
| 757 | if (is_array($term_list) && !empty($term_list)) { |
| 758 | $checked_terms = array_keys($visibility['tax_terms'][$tax_value]); |
| 759 | $checked_terms = (intval($checked_terms[0]) == 0) ? $visibility['tax_terms'][$tax_value] : $checked_terms; |
| 760 | $intersect = array_intersect($term_list, $checked_terms); |
| 761 | if (!empty($intersect) && $visibility_opts == 'hide') { |
| 762 | $hidden = true; |
| 763 | } elseif (!empty($intersect) && $visibility_opts == 'show') { |
| 764 | $hidden = false; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | // do return to bypass other conditions |
| 769 | $hidden = apply_filters('widget_options_visibility_single_block_' . $tax_value, $hidden); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | |
| 774 | if ($hidden) { |
| 775 | return false; |
| 776 | } |
| 777 | // echo $type; |
| 778 | } elseif ($is_types && is_page()) { |
| 779 | global $post; |
| 780 | |
| 781 | //do post type condition first |
| 782 | if (isset($visibility['types']) && isset($visibility['types']['page'])) { |
| 783 | if ($visibility_opts == 'hide' && array_key_exists('page', $visibility['types'])) { |
| 784 | $hidden = true; //hide if exists on hidden pages |
| 785 | } elseif ($visibility_opts == 'show' && !array_key_exists('page', $visibility['types'])) { |
| 786 | $hidden = true; //hide if doesn't exists on visible pages |
| 787 | } |
| 788 | } else { |
| 789 | //do per pages condition |
| 790 | if (!isset($visibility['pages'])) { |
| 791 | $visibility['pages'] = array(); |
| 792 | } |
| 793 | |
| 794 | // WPML FIX |
| 795 | $page_id = get_queried_object_id(); |
| 796 | $parent_id = wp_get_post_parent_id($page_id); |
| 797 | |
| 798 | $pageID = ($hasWPML) ? apply_filters('wpml_object_id', $page_id, 'page', true, $default_language) : $page_id; |
| 799 | $parentID = ($hasWPML) ? apply_filters('wpml_object_id', $parent_id, 'page', true, $default_language) : $parent_id; |
| 800 | |
| 801 | $page_in_array = in_array($pageID, $visibility['pages']); |
| 802 | //for the compatibility of the data of lower version 3.8.10 and below |
| 803 | if (array_key_exists($pageID, $visibility['pages'])) { |
| 804 | if ($visibility['pages'][$pageID] == 1) { |
| 805 | $page_in_array = true; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | //add parent inherit option |
| 810 | if ($is_inherit && $parentID && (array_key_exists($parentID, $visibility['pages']) || in_array($pageID, $visibility['pages']))) { |
| 811 | $visibility['pages'][] = $pageID; |
| 812 | // print_r( $visibility['pages'] ); |
| 813 | } |
| 814 | |
| 815 | if ($visibility_opts == 'hide' && $page_in_array) { |
| 816 | $hidden = true; //hide if exists on hidden pages |
| 817 | } elseif ($visibility_opts == 'show' && !$page_in_array) { |
| 818 | $hidden = true; //hide if doesn't exists on visible pages |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | if ((!empty($authorPageSelection) && $authorPageSelection == '2') && $visibility_opts == 'show') { |
| 823 | $hidden = true; //hide if checked on visible pages but the visibilty_opts is show |
| 824 | } |
| 825 | |
| 826 | //do return to bypass other conditions |
| 827 | $hidden = apply_filters('widget_options_visibility_page_block', $hidden); |
| 828 | if ($hidden) { |
| 829 | return false; |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | //end wordpress pages |
| 834 | |
| 835 | //ACF |
| 836 | if (isset($widget_options['acf']) && 'activate' == $widget_options['acf']) { |
| 837 | if (isset($visibility['acf']['field']) && !empty($visibility['acf']['field'])) { |
| 838 | $acf = get_field_object($visibility['acf']['field']); |
| 839 | if ($acf && is_array($acf)) { |
| 840 | $acf_visibility = (isset($visibility['acf']) && isset($visibility['acf']['visibility'])) ? $visibility['acf']['visibility'] : 'hide'; |
| 841 | //handle repeater fields |
| 842 | if (isset($acf['value'])) { |
| 843 | if (is_array($acf['value'])) { |
| 844 | $acf['value'] = implode(', ', array_map(function ($acf_array_value) { |
| 845 | if (!is_array($acf_array_value)) return $acf_array_value; |
| 846 | |
| 847 | $acf_implode = implode(',', array_filter($acf_array_value)); |
| 848 | return $acf_implode; |
| 849 | }, $acf['value'])); |
| 850 | } |
| 851 | } |
| 852 | switch ($visibility['acf']['condition']) { |
| 853 | case 'equal': |
| 854 | if (isset($acf['value'])) { |
| 855 | if ('show' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) { |
| 856 | $hidden = false; |
| 857 | } else if ('show' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) { |
| 858 | $hidden = true; |
| 859 | } else if ('hide' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) { |
| 860 | $hidden = true; |
| 861 | } else if ('hide' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) { |
| 862 | $hidden = false; |
| 863 | } |
| 864 | } |
| 865 | break; |
| 866 | case 'not_equal': |
| 867 | if (isset($acf['value'])) { |
| 868 | if ('show' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) { |
| 869 | $hidden = true; |
| 870 | } else if ('show' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) { |
| 871 | $hidden = false; |
| 872 | } else if ('hide' == $acf_visibility && $acf['value'] == $visibility['acf']['value']) { |
| 873 | $hidden = false; |
| 874 | } else if ('hide' == $acf_visibility && $acf['value'] != $visibility['acf']['value']) { |
| 875 | $hidden = true; |
| 876 | } |
| 877 | } |
| 878 | break; |
| 879 | case 'contains': |
| 880 | if (isset($acf['value'])) { |
| 881 | if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) { |
| 882 | $hidden = false; |
| 883 | } else if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) { |
| 884 | $hidden = true; |
| 885 | } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) { |
| 886 | $hidden = true; |
| 887 | } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) { |
| 888 | $hidden = false; |
| 889 | } |
| 890 | } |
| 891 | break; |
| 892 | case 'not_contains': |
| 893 | if (isset($acf['value'])) { |
| 894 | if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) { |
| 895 | $hidden = true; |
| 896 | } else if ('show' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) { |
| 897 | $hidden = false; |
| 898 | } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) !== false) { |
| 899 | $hidden = false; |
| 900 | } else if ('hide' == $acf_visibility && strpos($acf['value'], $visibility['acf']['value']) === false) { |
| 901 | $hidden = true; |
| 902 | } |
| 903 | } |
| 904 | break; |
| 905 | case 'empty': |
| 906 | if ('show' == $acf_visibility && empty($acf['value'])) { |
| 907 | $hidden = false; |
| 908 | } else if ('show' == $acf_visibility && !empty($acf['value'])) { |
| 909 | $hidden = true; |
| 910 | } elseif ('hide' == $acf_visibility && empty($acf['value'])) { |
| 911 | $hidden = true; |
| 912 | } else if ('hide' == $acf_visibility && !empty($acf['value'])) { |
| 913 | $hidden = false; |
| 914 | } |
| 915 | break; |
| 916 | case 'not_empty': |
| 917 | if ('show' == $acf_visibility && empty($acf['value'])) { |
| 918 | $hidden = true; |
| 919 | } else if ('show' == $acf_visibility && !empty($acf['value'])) { |
| 920 | $hidden = false; |
| 921 | } elseif ('hide' == $acf_visibility && empty($acf['value'])) { |
| 922 | $hidden = false; |
| 923 | } else if ('hide' == $acf_visibility && !empty($acf['value'])) { |
| 924 | $hidden = true; |
| 925 | } |
| 926 | break; |
| 927 | |
| 928 | default: |
| 929 | # code... |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | // //do return to bypass other conditions |
| 934 | $hidden = apply_filters('widget_options_visibility_acf_block', $hidden); |
| 935 | if ($hidden) { |
| 936 | return false; |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | //login state |
| 943 | if (isset($widget_options['state']) && 'activate' == $widget_options['state'] && isset($opts['roles'])) { |
| 944 | if (isset($opts['roles']['state']) && !empty($opts['roles']['state'])) { |
| 945 | //do state action here |
| 946 | if ($opts['roles']['state'] == 'out' && is_user_logged_in()) { |
| 947 | return false; |
| 948 | } else if ($opts['roles']['state'] == 'in' && !is_user_logged_in()) { |
| 949 | return false; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | if ('activate' == $widget_options['logic']) { |
| 955 | // New snippet-based system |
| 956 | if (isset($opts['class']['logic_snippet_id']) && !empty($opts['class']['logic_snippet_id'])) { |
| 957 | $snippet_id = $opts['class']['logic_snippet_id']; |
| 958 | if (class_exists('WidgetOpts_Snippets_API')) { |
| 959 | $result = WidgetOpts_Snippets_API::execute_snippet($snippet_id); |
| 960 | if ($result === false) { |
| 961 | return false; |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | // Legacy support for old inline logic |
| 966 | elseif (isset($opts['class']) && isset($opts['class']['logic']) && !empty($opts['class']['logic'])) { |
| 967 | // Flag that legacy migration is needed |
| 968 | if (!get_option('wopts_display_logic_migration_required', false)) { |
| 969 | update_option('wopts_display_logic_migration_required', true); |
| 970 | } |
| 971 | |
| 972 | $display_logic = stripslashes(trim($opts['class']['logic'])); |
| 973 | $display_logic = apply_filters('widget_options_logic_override_block', $display_logic); |
| 974 | $display_logic = apply_filters('extended_widget_options_logic_override_block', $display_logic); |
| 975 | if ($display_logic === false) { |
| 976 | return false; |
| 977 | } |
| 978 | if ($display_logic === true) { |
| 979 | return true; |
| 980 | } |
| 981 | // if (stristr($display_logic, "return") === false) { |
| 982 | // $display_logic = "return (" . $display_logic . ");"; |
| 983 | // } |
| 984 | $display_logic = htmlspecialchars_decode($display_logic, ENT_QUOTES); |
| 985 | if (!widgetopts_safe_eval($display_logic)) { |
| 986 | return false; |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | if ('activate' == $widget_options['hide_title']) { |
| 992 | //hide widget title |
| 993 | if (isset($instance['title']) && isset($opts['class']) && isset($opts['class']['title']) && '1' == $opts['class']['title']) { |
| 994 | $instance['title'] = ''; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | $block_content = widgetopts_add_classes_post_block($block_content, $parsed_block, $obj); |
| 999 | } |
| 1000 | |
| 1001 | return $block_content; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * Add custom classes on widget |
| 1006 | */ |
| 1007 | function widgetopts_add_classes_post_block($block_content, $parsed_block, $obj) |
| 1008 | { |
| 1009 | global $widget_options, $wp_registered_widget_controls; |
| 1010 | $classe_to_add = ''; |
| 1011 | $id_to_add = ''; |
| 1012 | $widget_id_set = ''; |
| 1013 | $data_attr = ''; |
| 1014 | $instance = $parsed_block['attrs']; |
| 1015 | |
| 1016 | if (isset($instance)) { |
| 1017 | $opts = (isset($instance['extended_widget_opts'])) ? $instance['extended_widget_opts'] : (isset($instance['extended_widget_opts_block']) ? $instance['extended_widget_opts_block'] : array()); |
| 1018 | } else { |
| 1019 | $opts = array(); |
| 1020 | } |
| 1021 | |
| 1022 | $custom_class = isset($opts['class']) ? $opts['class'] : ''; |
| 1023 | |
| 1024 | if ('activate' == $widget_options['classes'] && isset($widget_options['settings']['classes'])) { |
| 1025 | //don't add the IDs when the setting is set to NO |
| 1026 | if (isset($widget_options['settings']['classes']['id'])) { |
| 1027 | if (is_array($custom_class) && isset($custom_class['id']) && !empty($custom_class['id'])) { |
| 1028 | $id_to_add = sanitize_html_class($custom_class['id']); |
| 1029 | $widget_id_set = sanitize_html_class($custom_class['id']); |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | $get_classes = widgetopts_classes_generator($opts, $widget_options, $widget_options['settings']); |
| 1035 | |
| 1036 | //double check array |
| 1037 | if (!is_array($get_classes)) { |
| 1038 | $get_classes = array(); |
| 1039 | } |
| 1040 | |
| 1041 | if (!empty($get_classes)) { |
| 1042 | $classe_to_add .= (implode(' ', $get_classes)) . ' '; |
| 1043 | //$block_content = preg_replace('class="', $classes, $block_content, 1); |
| 1044 | } |
| 1045 | |
| 1046 | // $params[0]['before_widget'] = str_replace('class="', ' data-animation="asdf" class="', $params[0]['before_widget']); |
| 1047 | |
| 1048 | $match = []; |
| 1049 | $has_match = preg_match('/<\w*[^>]*>/', $block_content, $match); |
| 1050 | |
| 1051 | if ($has_match == 1) { |
| 1052 | if (!empty($id_to_add)) { |
| 1053 | $has_match_id = preg_match('/[id="]/', $match[0]); |
| 1054 | if ($has_match_id == 1) { |
| 1055 | $block_content = preg_replace('/id="[^"]*/', "id=\"{$id_to_add}", $block_content, 1); |
| 1056 | } else { |
| 1057 | $block_content = preg_replace('/>/', " id=\"{$id_to_add}\">", $block_content, 1); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | if (!empty($classe_to_add)) { |
| 1062 | $has_match_class = preg_match('/[class="]/', $match[0]); |
| 1063 | if ($has_match_class == 1) { |
| 1064 | $block_content = preg_replace('/class="/', "class=\"{$classe_to_add}", $block_content, 1); |
| 1065 | } else { |
| 1066 | $block_content = preg_replace('/>/', " class=\"{$classe_to_add}\">", $block_content, 1); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | if (!empty($data_attr)) { |
| 1071 | $block_content = preg_replace('/>/', " {$data_attr}>", $block_content, 1); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | return $block_content; |
| 1076 | } |
| 1077 | |
| 1078 | |
| 1079 | /** |
| 1080 | * Gutenberg ajax functions |
| 1081 | */ |
| 1082 | function widgetopts_get_types() |
| 1083 | { |
| 1084 | global $widgetopts_types; |
| 1085 | if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) { |
| 1086 | die; |
| 1087 | } |
| 1088 | |
| 1089 | wp_send_json_success(((!empty($widgetopts_types)) ? $widgetopts_types : widgetopts_global_types())); |
| 1090 | die; |
| 1091 | } |
| 1092 | add_action('wp_ajax_widgetopts_get_types', 'widgetopts_get_types'); |
| 1093 | |
| 1094 | |
| 1095 | function widgetopts_get_taxonomies() |
| 1096 | { |
| 1097 | global $widgetopts_taxonomies; |
| 1098 | if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) { |
| 1099 | die; |
| 1100 | } |
| 1101 | |
| 1102 | wp_send_json_success(((!empty($widgetopts_taxonomies)) ? $widgetopts_taxonomies : widgetopts_global_taxonomies())); |
| 1103 | die; |
| 1104 | } |
| 1105 | add_action('wp_ajax_widgetopts_get_taxonomies', 'widgetopts_get_taxonomies'); |
| 1106 | |
| 1107 | function widgetopts_acf_get_field_groups() |
| 1108 | { |
| 1109 | if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) { |
| 1110 | die; |
| 1111 | } |
| 1112 | |
| 1113 | $fields = array(); |
| 1114 | if (function_exists('acf_get_field_groups')) { |
| 1115 | $groups = acf_get_field_groups(); |
| 1116 | if (is_array($groups)) { |
| 1117 | foreach ($groups as $group) { |
| 1118 | $fields[$group['ID']] = array('title' => $group['title'], 'fields' => acf_get_fields($group)); |
| 1119 | } |
| 1120 | } |
| 1121 | } else { |
| 1122 | $groups = apply_filters('acf/get_field_groups', array()); |
| 1123 | if (is_array($groups)) { |
| 1124 | foreach ($groups as $group) { |
| 1125 | $fields[$group['id']] = array('title' => $group['title'], 'fields' => apply_filters('acf/field_group/get_fields', array(), $group['id'])); |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | wp_send_json_success($fields); |
| 1131 | die; |
| 1132 | } |
| 1133 | add_action('wp_ajax_widgetopts_acf_get_field_groups', 'widgetopts_acf_get_field_groups'); |
| 1134 | |
| 1135 | function widgetopts_get_legacy_data() |
| 1136 | { |
| 1137 | if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) { |
| 1138 | die; |
| 1139 | } |
| 1140 | |
| 1141 | if (isset($_POST['id_base'])) { |
| 1142 | wp_send_json_success(array()); |
| 1143 | die; |
| 1144 | } |
| 1145 | $settings = array(); |
| 1146 | $settings = get_option('widget_' + sanitize_key($_POST['id_base'])); |
| 1147 | |
| 1148 | if (false === $settings) { |
| 1149 | return $settings; |
| 1150 | } |
| 1151 | |
| 1152 | if (!is_array($settings) && !($settings instanceof ArrayObject || $settings instanceof ArrayIterator)) { |
| 1153 | $settings = array(); |
| 1154 | } |
| 1155 | |
| 1156 | wp_send_json_success($settings); |
| 1157 | die; |
| 1158 | } |
| 1159 | add_action('wp_ajax_widgetopts_get_legacy_data', 'widgetopts_get_legacy_data'); |
| 1160 | |
| 1161 | function widgetopts_get_settings_ajax() |
| 1162 | { |
| 1163 | if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) { |
| 1164 | die; |
| 1165 | } |
| 1166 | $settings = widgetopts_get_settings(); |
| 1167 | |
| 1168 | if (!current_user_can('administrator')) { |
| 1169 | $settings['logic'] = 'deactivate'; |
| 1170 | } |
| 1171 | |
| 1172 | wp_send_json_success($settings); |
| 1173 | die; |
| 1174 | } |
| 1175 | add_action('wp_ajax_widgetopts_get_settings_ajax', 'widgetopts_get_settings_ajax'); |
| 1176 | |
| 1177 | function widgetopts_get_snippets_ajax() |
| 1178 | { |
| 1179 | if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) { |
| 1180 | die; |
| 1181 | } |
| 1182 | |
| 1183 | $search = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : ''; |
| 1184 | |
| 1185 | $snippets = array(); |
| 1186 | if (class_exists('WidgetOpts_Snippets_CPT')) { |
| 1187 | $all_snippets = WidgetOpts_Snippets_CPT::get_all_snippets($search); |
| 1188 | foreach ($all_snippets as $snippet) { |
| 1189 | $snippets[] = array( |
| 1190 | 'id' => $snippet['id'], |
| 1191 | 'title' => $snippet['title'], |
| 1192 | 'description' => $snippet['description'] |
| 1193 | ); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | // Return data with admin info for manage snippets link |
| 1198 | wp_send_json_success(array( |
| 1199 | 'snippets' => $snippets, |
| 1200 | 'can_manage' => current_user_can('manage_options'), |
| 1201 | 'manage_url' => admin_url('edit.php?post_type=widgetopts_snippet'), |
| 1202 | 'migration_url' => admin_url('options-general.php?page=widgetopts_migration') |
| 1203 | )); |
| 1204 | die; |
| 1205 | } |
| 1206 | add_action('wp_ajax_widgetopts_get_snippets_ajax', 'widgetopts_get_snippets_ajax'); |
| 1207 | |
| 1208 | function widgetopts_get_pages() |
| 1209 | { |
| 1210 | if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) { |
| 1211 | die; |
| 1212 | } |
| 1213 | |
| 1214 | $pages = []; |
| 1215 | |
| 1216 | $pargs = array( |
| 1217 | 'hierarchical' => true, |
| 1218 | 'child_of' => 0, // Display all pages regardless of parent |
| 1219 | 'parent' => -1, // Display all pages regardless of parent |
| 1220 | 'sort_order' => 'ASC', |
| 1221 | 'sort_column' => 'menu_order, post_title' |
| 1222 | ); |
| 1223 | |
| 1224 | $pageLoop = get_pages($pargs); |
| 1225 | |
| 1226 | if ($pageLoop) { |
| 1227 | foreach ($pageLoop as $objPage) { |
| 1228 | $depth = count(get_ancestors($objPage->ID, 'page')); |
| 1229 | // Determine indentation for hierarchical display |
| 1230 | $indent = str_repeat('-', $depth); |
| 1231 | $objPage->post_title = $indent . "" . $objPage->post_title; |
| 1232 | } |
| 1233 | $pages = $pageLoop; |
| 1234 | } |
| 1235 | |
| 1236 | wp_send_json_success($pages); |
| 1237 | die; |
| 1238 | } |
| 1239 | add_action('wp_ajax_widgetopts_get_pages', 'widgetopts_get_pages'); |
| 1240 | |
| 1241 | function widgetopts_get_terms() |
| 1242 | { |
| 1243 | if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) { |
| 1244 | die; |
| 1245 | } |
| 1246 | |
| 1247 | $terms = array(); |
| 1248 | |
| 1249 | $_terms = array(); |
| 1250 | $_terms = get_terms(); |
| 1251 | |
| 1252 | if (!is_wp_error($_terms)) { |
| 1253 | foreach ($_terms as $t) { |
| 1254 | $terms[$t->name][] = $t; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | wp_send_json_success($terms); |
| 1259 | die; |
| 1260 | } |
| 1261 | add_action('wp_ajax_widgetopts_get_terms', 'widgetopts_get_terms'); |
| 1262 | |
| 1263 | function widgetopts_get_users() |
| 1264 | { |
| 1265 | global $wp_version; |
| 1266 | |
| 1267 | if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) { |
| 1268 | die; |
| 1269 | } |
| 1270 | |
| 1271 | $authors = array(); |
| 1272 | |
| 1273 | $args = array(); |
| 1274 | |
| 1275 | $is_6_3_and_above = version_compare($wp_version, '6.3', '>='); |
| 1276 | if ($is_6_3_and_above) { |
| 1277 | $args['cache_results'] = apply_filters('cache_widgetopts_ajax_taxonomy_search', true); |
| 1278 | } |
| 1279 | |
| 1280 | $_authors = get_users($args); |
| 1281 | |
| 1282 | if (!empty($_authors)) { |
| 1283 | |
| 1284 | if (is_iterable($_authors)) { |
| 1285 | foreach ($_authors as $a) { |
| 1286 | $displayname = isset($a->display_name) ? $a->display_name : (isset($a->data) && isset($a->data->display_name) ? $a->data->display_name : ''); |
| 1287 | $authors[] = ["ID" => $a->ID, "display_name" => $displayname]; |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | wp_send_json_success($authors); |
| 1293 | die; |
| 1294 | } |
| 1295 | add_action('wp_ajax_widgetopts_get_users', 'widgetopts_get_users'); |
| 1296 | |
| 1297 | function widgetopts_ajax_roles_search_block() |
| 1298 | { |
| 1299 | if (!(current_user_can('edit_pages') || current_user_can('edit_posts') || current_user_can('edit_theme_options'))) { |
| 1300 | die; |
| 1301 | } |
| 1302 | $response = [ |
| 1303 | 'results' => [], |
| 1304 | 'pagination' => ['more' => false] |
| 1305 | ]; |
| 1306 | |
| 1307 | $term = isset($_POST['term']) && !empty($_POST['term']) ? $_POST['term'] : ''; |
| 1308 | |
| 1309 | $roles = get_editable_roles(); |
| 1310 | if (!empty($roles)) { |
| 1311 | foreach ($roles as $role_name => $role_info) { |
| 1312 | // if ((!empty($term) && stristr($role_name, $term) !== false) || stristr($role_name, $role_info['name']) !== false) { |
| 1313 | $response['results'][] = [ |
| 1314 | 'id' => $role_name, |
| 1315 | 'text' => $role_info['name'] |
| 1316 | ]; |
| 1317 | // } |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | wp_send_json_success($response); |
| 1322 | die; |
| 1323 | } |
| 1324 | add_action('wp_ajax_widgetopts_ajax_roles_search_block', 'widgetopts_ajax_roles_search_block'); |
| 1325 |