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