beaver.php
1037 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Plugin Name: Widget Options for Beaver Builder |
| 5 | * Plugin URI: https://widget-options.com/ |
| 6 | * Description: <strong>Requires <a href="https://wordpress.org/plugins/widget-options/" target="_blank">Widget Options Plugin</a></strong>! Extend functionalities to Beaver Builder for more visibility restriction options. |
| 7 | * Version: 1.0 |
| 8 | * Author: Widget Options Team |
| 9 | * Author URI: https://widget-options.com/ |
| 10 | * Text Domain: widget-options |
| 11 | * Domain Path: languages |
| 12 | * |
| 13 | * @category Widgets |
| 14 | * @author Widget Options Team |
| 15 | * @version 1.0 |
| 16 | */ |
| 17 | // Exit if accessed directly. |
| 18 | if (!defined('ABSPATH')) exit; |
| 19 | if (!class_exists('WP_Widget_Options_Beaver')) : |
| 20 | |
| 21 | /** |
| 22 | * Main WP_Widget_Options_Beaver Class. |
| 23 | * |
| 24 | * @since 1.0 |
| 25 | */ |
| 26 | class WP_Widget_Options_Beaver |
| 27 | { |
| 28 | |
| 29 | public static function init() |
| 30 | { |
| 31 | $class = __CLASS__; |
| 32 | new $class; |
| 33 | } |
| 34 | |
| 35 | function __construct() |
| 36 | { |
| 37 | global $widget_options; |
| 38 | |
| 39 | add_filter('fl_builder_register_settings_form', array(&$this, 'widgetopts_beaver_settings'), 10, 2); |
| 40 | add_action('fl_builder_control_widgetopts-beaver-tabnav', array(&$this, 'fl_widgetopts_beaver_tabnav'), 1, 4); |
| 41 | add_action('wp_enqueue_scripts', array(&$this, 'fl_widgetopts_beaver_scripts')); |
| 42 | add_action('fl_builder_control_widgetopts-select2', array(&$this, 'fl_widgetopts_beaver_select2'), 1, 4); |
| 43 | add_action('fl_builder_control_widgetopts-upgrade', array(&$this, 'fl_widgetopts_upgrade'), 1, 4); |
| 44 | // add_action( 'admin_notices', array( &$this, 'widgetopts_plugin_check' ) ); |
| 45 | |
| 46 | add_filter('fl_builder_is_node_visible', array(&$this, 'widgetopts_beaver_is_node_visible'), 10, 2); |
| 47 | add_action('fl_builder_control_widgetopts-beaver-legacy', array(&$this, 'fl_widgetopts_beaver_legacy_control'), 1, 4); |
| 48 | } |
| 49 | |
| 50 | function widgetopts_beaver_settings($form, $id) |
| 51 | { |
| 52 | if (!isset($form['widgetopts']) && !is_admin()) { |
| 53 | //fix not registering global values |
| 54 | if (!function_exists('widgetopts_register_globals')) { |
| 55 | require_once WIDGETOPTS_PLUGIN_DIR . 'includes/admin/globals.php'; |
| 56 | widgetopts_register_globals(); |
| 57 | } |
| 58 | |
| 59 | global $widget_options, $widgetopts_taxonomies, $widgetopts_types, $widgetopts_categories; |
| 60 | $widgetopts_pages = widgetopts_global_pages(); |
| 61 | |
| 62 | |
| 63 | $sections = array(); |
| 64 | $pages = (!empty($widgetopts_pages)) ? $widgetopts_pages : array(); |
| 65 | $taxonomies = (!empty($widgetopts_taxonomies)) ? $widgetopts_taxonomies : array(); |
| 66 | $types = (!empty($widgetopts_types)) ? $widgetopts_types : array(); |
| 67 | $categories = (!empty($widgetopts_categories)) ? $widgetopts_categories : array(); |
| 68 | |
| 69 | $get_terms = array(); |
| 70 | if (!empty($widget_options['settings']['taxonomies']) && is_array($widget_options['settings']['taxonomies'])) { |
| 71 | foreach ($widget_options['settings']['taxonomies'] as $tax_opt => $vall) { |
| 72 | $tax_name = 'widgetopts_taxonomy_' . str_replace('-', '__', $tax_opt); |
| 73 | // global $$tax_name; |
| 74 | if (isset($GLOBALS[$tax_name])) { |
| 75 | $get_terms[$tax_opt] = $GLOBALS[$tax_name]; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // print_r( $pages ); die(); |
| 81 | |
| 82 | $sections['widgetopts-fields'] = array( |
| 83 | 'fields' => array( |
| 84 | 'widgetopts-tabnav' => array( |
| 85 | 'type' => 'widgetopts-beaver-tabnav', |
| 86 | ) |
| 87 | ) |
| 88 | ); |
| 89 | |
| 90 | if (isset($widget_options['visibility']) && 'activate' == $widget_options['visibility']) { |
| 91 | $visibility_fld = array(); |
| 92 | |
| 93 | $visibility_fld['widgetopts_visibility_show'] = array( |
| 94 | 'type' => 'select', |
| 95 | 'label' => __('Show or Hide', 'widget-options'), |
| 96 | 'options' => array( |
| 97 | 'hide' => __('Hide on Selected Pages', 'widget-options'), |
| 98 | 'show' => __('Show on Selected Pages', 'widget-options') |
| 99 | ) |
| 100 | ); |
| 101 | |
| 102 | if (isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['post_type']) && '1' == $widget_options['settings']['visibility']['post_type']) { |
| 103 | |
| 104 | if (!empty($pages)) { |
| 105 | $pages_array = array(); |
| 106 | foreach ($pages as $page) { |
| 107 | $pages_array[$page->ID] = $page->post_title; |
| 108 | } |
| 109 | |
| 110 | $visibility_fld['widgetopts_visibility_pages'] = array( |
| 111 | 'type' => 'widgetopts-select2', |
| 112 | 'label' => __('Pages', 'widget-options'), |
| 113 | 'options' => $pages_array, |
| 114 | 'multi-select' => true, |
| 115 | 'description' => __('Click to search or select', 'widget-options'), |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | if (!empty($types)) { |
| 120 | $types_array = array(); |
| 121 | foreach ($types as $ptype => $type) { |
| 122 | $types_array[$ptype] = $type->labels->name; |
| 123 | } |
| 124 | |
| 125 | $visibility_fld['widgetopts_visibility_types'] = array( |
| 126 | 'type' => 'widgetopts-select2', |
| 127 | 'label' => __('Post Types', 'widget-options'), |
| 128 | 'options' => $types_array, |
| 129 | 'multi-select' => true, |
| 130 | 'description' => __('Click to search or select', 'widget-options') |
| 131 | ); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['taxonomies']) && '1' == $widget_options['settings']['visibility']['taxonomies']) { |
| 136 | // if( !empty( $widget_options['settings']['taxonomies'] ) && is_array( $widget_options['settings']['taxonomies'] ) ){ |
| 137 | // foreach ( $widget_options['settings']['taxonomies'] as $tax_opt => $vallue ) { |
| 138 | // $term_array = array(); |
| 139 | // // if( !empty( $get_terms ) ){ |
| 140 | // foreach ( $get_terms[ $tax_opt ] as $get_term ) { |
| 141 | // $term_array[ $get_term->term_id ] = $get_term->name; |
| 142 | // } |
| 143 | |
| 144 | // $visibility_fld['widgetopts_visibility_tax_'. $tax_opt] = array( |
| 145 | // 'type' => 'widgetopts-select2', |
| 146 | // 'label' => $taxonomies[ $tax_opt ]->label, |
| 147 | // 'options' => $term_array, |
| 148 | // 'multi-select' => true, |
| 149 | // 'description' => __( 'Click to search or select', 'widget-options' ) |
| 150 | // ); |
| 151 | // // } |
| 152 | // } |
| 153 | // } |
| 154 | |
| 155 | if (!empty($categories)) { |
| 156 | $cat_array = array(); |
| 157 | foreach ($categories as $cat) { |
| 158 | $cat_array[$cat->cat_ID] = $cat->cat_name; |
| 159 | } |
| 160 | |
| 161 | $visibility_fld['widgetopts_visibility_tax_category'] = array( |
| 162 | 'type' => 'widgetopts-select2', |
| 163 | 'label' => __('Categories', 'widget-options'), |
| 164 | 'options' => $cat_array, |
| 165 | 'multi-select' => true, |
| 166 | 'description' => __('Click to search or select', 'widget-options') |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | if (!empty($taxonomies)) { |
| 171 | $tax_array = array(); |
| 172 | foreach ($taxonomies as $taxonomy) { |
| 173 | $tax_array[$taxonomy->name] = $taxonomy->label; |
| 174 | } |
| 175 | |
| 176 | $visibility_fld['widgetopts_visibility_taxonomies'] = array( |
| 177 | 'type' => 'widgetopts-select2', |
| 178 | 'label' => __('Taxonomies', 'widget-options'), |
| 179 | 'options' => $tax_array, |
| 180 | 'multi-select' => true, |
| 181 | 'description' => __('Click to search or select', 'widget-options') |
| 182 | ); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['misc']) && '1' == $widget_options['settings']['visibility']['misc']) { |
| 187 | $visibility_fld['widgetopts_visibility_misc'] = array( |
| 188 | 'type' => 'widgetopts-select2', |
| 189 | 'label' => __('Miscellaneous', 'widget-options'), |
| 190 | 'options' => array( |
| 191 | 'home' => __('Home/Front', 'widget-options'), |
| 192 | 'blog' => __('Blog', 'widget-options'), |
| 193 | 'archives' => __('Archives', 'widget-options'), |
| 194 | '404' => __('404', 'widget-options'), |
| 195 | 'search' => __('Search', 'widget-options') |
| 196 | ), |
| 197 | 'multi-select' => true, |
| 198 | 'description' => __('Click to search or select', 'widget-options') |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | $sections['widgetopts-visibility'] = array( |
| 203 | 'fields' => $visibility_fld |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | if ( |
| 208 | isset($widget_options['logic']) && 'activate' == $widget_options['logic'] || |
| 209 | isset($widget_options['acf']) && 'activate' == $widget_options['acf'] |
| 210 | ) { |
| 211 | $settings_fld = array(); |
| 212 | |
| 213 | if (isset($widget_options['logic']) && 'activate' == $widget_options['logic']) { |
| 214 | // Get available snippets for dropdown |
| 215 | $snippet_options = array('' => __('— No Logic (Always Show) —', 'widget-options')); |
| 216 | if (class_exists('WidgetOpts_Snippets_CPT')) { |
| 217 | $snippets = WidgetOpts_Snippets_CPT::get_all_snippets(); |
| 218 | foreach ($snippets as $snippet) { |
| 219 | $snippet_options[$snippet['id']] = $snippet['title']; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | $snippet_description = __('Select a logic snippet to control when this widget is displayed.', 'widget-options'); |
| 224 | if (current_user_can('manage_options')) { |
| 225 | $snippet_description .= '<br><a href="' . admin_url('edit.php?post_type=widgetopts_snippet') . '" target="_blank" style="display:inline-block;margin-top:5px;padding:3px 8px;background:#0073aa;color:#fff;text-decoration:none;border-radius:3px;font-size:11px;">' . __('Manage Snippets', 'widget-options') . ' →</a>'; |
| 226 | } |
| 227 | |
| 228 | // Hidden field to ensure BB includes legacy logic value in settings |
| 229 | $settings_fld['widgetopts_settings_logic'] = array( |
| 230 | 'type' => 'hidden', |
| 231 | 'default' => '', |
| 232 | ); |
| 233 | |
| 234 | // Hidden flag: set to '1' when user clicks Clear button |
| 235 | $settings_fld['widgetopts_logic_cleared'] = array( |
| 236 | 'type' => 'hidden', |
| 237 | 'default' => '', |
| 238 | ); |
| 239 | |
| 240 | // Widget Options version stamp |
| 241 | $settings_fld['widgetopts_wopt_version'] = array( |
| 242 | 'type' => 'hidden', |
| 243 | 'default' => '', |
| 244 | ); |
| 245 | |
| 246 | // Per-node legacy logic display (shown only when this node has legacy code) |
| 247 | $settings_fld['widgetopts_legacy_logic_display'] = array( |
| 248 | 'type' => 'widgetopts-beaver-legacy', |
| 249 | ); |
| 250 | |
| 251 | $settings_fld['widgetopts_logic_snippet_id'] = array( |
| 252 | 'type' => 'widgetopts-select2', |
| 253 | 'label' => __('Display Logic Snippet', 'widget-options'), |
| 254 | 'options' => $snippet_options, |
| 255 | 'description' => $snippet_description |
| 256 | ); |
| 257 | } |
| 258 | |
| 259 | //ACF |
| 260 | if (isset($widget_options['acf']) && 'activate' == $widget_options['acf']) { |
| 261 | $fields = array('' => __('Select Field', 'widget-options')); |
| 262 | |
| 263 | if (function_exists('acf_get_field_groups')) { |
| 264 | $groups = acf_get_field_groups(); |
| 265 | if (is_array($groups)) { |
| 266 | foreach ($groups as $group) { |
| 267 | $fields_group = acf_get_fields($group); |
| 268 | if (!empty($fields_group)) { |
| 269 | foreach ($fields_group as $k => $fg) { |
| 270 | $fields[$fg['key']] = $fg['label']; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } else { |
| 276 | $groups = apply_filters('acf/get_field_groups', array()); |
| 277 | if (is_array($groups)) { |
| 278 | foreach ($groups as $group) { |
| 279 | $fields_group = apply_filters('acf/field_group/get_fields', array(), $group['id']); |
| 280 | if (!empty($fields_group)) { |
| 281 | foreach ($fields_group as $k => $fg) { |
| 282 | $fields[$fg['key']] = $fg['label']; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | $settings_fld['widgetopts_acf_visibility'] = array( |
| 290 | 'type' => 'select', |
| 291 | 'label' => __('Show or Hide based on Advanced Custom Field', 'widget-options'), |
| 292 | 'options' => array( |
| 293 | 'hide' => __('Hide when Condition\'s Met', 'widget-options'), |
| 294 | 'show' => __('Show when Condition\'s Met', 'widget-options') |
| 295 | ) |
| 296 | ); |
| 297 | |
| 298 | $settings_fld['widgetopts_acf_field'] = array( |
| 299 | 'type' => 'select', |
| 300 | 'label' => __('Select ACF Field', 'widget-options'), |
| 301 | 'options' => $fields |
| 302 | ); |
| 303 | |
| 304 | $settings_fld['widgetopts_acf_condition'] = array( |
| 305 | 'type' => 'select', |
| 306 | 'label' => __('Select Condition', 'widget-options'), |
| 307 | 'options' => array( |
| 308 | '' => __('Select Condition', 'widget-options'), |
| 309 | 'equal' => __('Is Equal To', 'widget-options'), |
| 310 | 'not_equal' => __('Is Not Equal To', 'widget-options'), |
| 311 | 'contains' => __('Contains', 'widget-options'), |
| 312 | 'not_contains' => __('Does Not Contain', 'widget-options'), |
| 313 | 'empty' => __('Is Empty', 'widget-options'), |
| 314 | 'not_empty' => __('Is Not Empty', 'widget-options') |
| 315 | ) |
| 316 | ); |
| 317 | $settings_fld['widgetopts_acf_value'] = array( |
| 318 | 'type' => 'textarea', |
| 319 | 'label' => __('Conditional Value', 'widget-options'), |
| 320 | 'description' => __('Add your Conditional Value here if you selected Equal to, Not Equal To or Contains on the selection above.', 'widget-options') |
| 321 | ); |
| 322 | } |
| 323 | |
| 324 | $sections['widgetopts-settings'] = array( |
| 325 | 'fields' => $settings_fld |
| 326 | ); |
| 327 | } |
| 328 | |
| 329 | $upgrade_fld = array(); |
| 330 | $upgrade_fld['widgetopts_upgrade_section'] = array( |
| 331 | 'type' => 'widgetopts-upgrade' |
| 332 | ); |
| 333 | |
| 334 | $sections['widgetopts-upgrade'] = array( |
| 335 | 'fields' => $upgrade_fld |
| 336 | ); |
| 337 | |
| 338 | $form['widgetopts'] = array( |
| 339 | 'title' => __('Widget Options', 'widget-options'), |
| 340 | 'sections' => $sections |
| 341 | ); |
| 342 | } |
| 343 | |
| 344 | return $form; |
| 345 | } |
| 346 | |
| 347 | function fl_widgetopts_beaver_tabnav($name, $value, $field, $settings) |
| 348 | { |
| 349 | global $widget_options; |
| 350 | ?> |
| 351 | <div class="fl-builder-widgetopts-tab"> |
| 352 | <?php if (isset($widget_options['visibility']) && 'activate' == $widget_options['visibility']) { ?> |
| 353 | <a onclick="widgetoptsBeaverModule.navClick(event)" href="#fl-builder-settings-section-widgetopts-visibility" class="widgetopts-s-active"><span class="dashicons dashicons-visibility"></span><?php _e('Visibility', 'widget-options'); ?></a> |
| 354 | <?php } ?> |
| 355 | <?php if (isset($widget_options['logic']) && 'activate' == $widget_options['logic']) { ?> |
| 356 | <a onclick="widgetoptsBeaverModule.navClick(event)" href="#fl-builder-settings-section-widgetopts-settings" class=""><span class="dashicons dashicons-admin-generic"></span><?php _e('Settings', 'widget-options'); ?></a> |
| 357 | <?php } ?> |
| 358 | <a onclick="widgetoptsBeaverModule.navClick(event)" href="#fl-builder-settings-section-widgetopts-upgrade"><span class="dashicons dashicons-plus"></span><?php _e('More', 'widget-options'); ?></a> |
| 359 | </div> |
| 360 | <?php } |
| 361 | |
| 362 | function fl_widgetopts_beaver_scripts() |
| 363 | { |
| 364 | if (class_exists('FLBuilderModel') && FLBuilderModel::is_builder_active()) { |
| 365 | $js_dir = WIDGETOPTS_PLUGIN_URL . 'assets/js/'; |
| 366 | $css_dir = WIDGETOPTS_PLUGIN_URL . 'assets/css/'; |
| 367 | |
| 368 | // Use minified libraries if SCRIPT_DEBUG is turned off |
| 369 | $suffix = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min'; |
| 370 | |
| 371 | wp_enqueue_style('widgetopts-beaver-css', $css_dir . 'beaver-widgetopts.css', array(), WIDGETOPTS_VERSION); |
| 372 | wp_enqueue_style('widgetopts-beaver-select2-css', $css_dir . 'select2.min.css', array(), WIDGETOPTS_VERSION); |
| 373 | wp_enqueue_style('widgetopts-jquery-ui', $css_dir . '/jqueryui/1.11.4/themes/ui-lightness/jquery-ui.css', array(), WIDGETOPTS_VERSION); |
| 374 | wp_enqueue_style('jquery-ui'); |
| 375 | |
| 376 | wp_enqueue_script( |
| 377 | 'beaver-widgetopts', |
| 378 | $js_dir . 'jquery.widgetopts.beaver' . $suffix . '.js', |
| 379 | array('jquery'), |
| 380 | WIDGETOPTS_VERSION, |
| 381 | true |
| 382 | ); |
| 383 | wp_enqueue_script( |
| 384 | 'beaver-widgetopts-select2', |
| 385 | $js_dir . 'select2.min.js', |
| 386 | array('jquery', 'beaver-widgetopts'), |
| 387 | WIDGETOPTS_VERSION, |
| 388 | true |
| 389 | ); |
| 390 | wp_enqueue_script( |
| 391 | 'beaver-widgetopts-s2', |
| 392 | $js_dir . 'select2-settings' . $suffix . '.js', |
| 393 | array('jquery', 'beaver-widgetopts'), |
| 394 | WIDGETOPTS_VERSION, |
| 395 | true |
| 396 | ); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | function fl_widgetopts_beaver_get_fld_options($settings, $field, $options = array()) |
| 401 | { |
| 402 | if (!is_object($settings) && !is_array($settings)) return $options; |
| 403 | |
| 404 | foreach ($settings as $key => $val) { |
| 405 | if ($key === $field) { |
| 406 | if (is_array($val)) { |
| 407 | foreach ($val as $v) { |
| 408 | $options[$v] = $v; |
| 409 | } |
| 410 | } else { |
| 411 | $options[$val] = $val; |
| 412 | } |
| 413 | } else { |
| 414 | if (is_array($val) || is_object($val)) { |
| 415 | $options = $this->fl_widgetopts_beaver_get_fld_options($val, $field, $options); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return array_unique($options); |
| 421 | } |
| 422 | |
| 423 | function fl_widgetopts_beaver_select2($name, $value, $field, $settings) |
| 424 | { |
| 425 | $options = ($field['options']) ? $field['options'] : array(); |
| 426 | |
| 427 | if (isset($field['options_from_field'])) { |
| 428 | $options_field = $field['options_from_field']; |
| 429 | $post_data = FLBuilderModel::get_post_data(); |
| 430 | $parent_settings = $post_data['node_settings']; |
| 431 | |
| 432 | $options = $this->fl_widgetopts_beaver_get_fld_options($parent_settings, $options_field, $options); |
| 433 | } |
| 434 | |
| 435 | // Create attributes |
| 436 | $attributes = ''; |
| 437 | if (isset($field['attributes']) && is_array($field['attributes'])) { |
| 438 | foreach ($field['attributes'] as $key => $val) { |
| 439 | $attributes .= $key . '="' . $val . '" '; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | if (!empty($options) && $value && is_array($value)) { |
| 444 | uksort($options, function ($key1, $key2) use ($value) { |
| 445 | return array_search($key1, $value) <=> array_search($key2, $value); |
| 446 | }); |
| 447 | } |
| 448 | if (!isset($field['class'])) { |
| 449 | $field['class'] = ''; |
| 450 | } |
| 451 | |
| 452 | // Show the select field |
| 453 | ?> |
| 454 | <select name="<?php echo $name; |
| 455 | if (isset($field['multi-select'])) echo '[]'; ?>" class="widgetopts-select2 <?php echo $field['class']; ?>" <?php if (isset($field['multi-select'])) echo 'multiple '; |
| 456 | echo $attributes; ?> placeholder="<?php _e('Click to search or select', 'widget-options'); ?>"> |
| 457 | <option value=""><?php echo isset($options['']) ? esc_html(is_array($options['']) ? $options['']['label'] : $options['']) : ''; ?></option> |
| 458 | <?php |
| 459 | foreach ($options as $option_key => $option_val) : |
| 460 | |
| 461 | if ($option_key === '') { |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | if (is_array($option_val) && isset($option_val['premium']) && $option_val['premium'] && true === FL_BUILDER_LITE) { |
| 466 | continue; |
| 467 | } |
| 468 | |
| 469 | $label = is_array($option_val) ? $option_val['label'] : $option_val; |
| 470 | |
| 471 | if (is_array($value) && in_array($option_key, $value)) { |
| 472 | $selected = ' selected="selected"'; |
| 473 | } else if (!is_array($value) && selected($value, $option_key, true)) { |
| 474 | $selected = ' selected="selected"'; |
| 475 | } else { |
| 476 | $selected = ''; |
| 477 | } |
| 478 | |
| 479 | ?> |
| 480 | <option value="<?php echo $option_key; ?>" <?php echo $selected; ?>><?php echo $label; ?></option> |
| 481 | <?php endforeach; ?> |
| 482 | </select> |
| 483 | <?php } |
| 484 | |
| 485 | /** |
| 486 | * Custom control: show legacy logic readonly textarea + warning per-node |
| 487 | */ |
| 488 | function fl_widgetopts_beaver_legacy_control($name, $value, $field, $settings) |
| 489 | { |
| 490 | // Don't show if snippet_id is already set (migration done) |
| 491 | if (!empty($settings->widgetopts_logic_snippet_id)) { |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | // Only render if this specific node has legacy logic code |
| 496 | $legacy_code = ''; |
| 497 | if (isset($settings->widgetopts_settings_logic) && !empty($settings->widgetopts_settings_logic)) { |
| 498 | $legacy_code = $settings->widgetopts_settings_logic; |
| 499 | } elseif (isset($settings->widgetopts_settings_logic_backup) && !empty($settings->widgetopts_settings_logic_backup)) { |
| 500 | $legacy_code = $settings->widgetopts_settings_logic_backup; |
| 501 | } |
| 502 | |
| 503 | if (empty($legacy_code)) { |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | $migration_url = admin_url('options-general.php?page=widgetopts_migration'); |
| 508 | ?> |
| 509 | <div class="widgetopts-legacy-logic-wrap" style="margin-bottom:15px;"> |
| 510 | <p><strong><?php _e('Legacy Display Logic Code', 'widget-options'); ?></strong></p> |
| 511 | <textarea readonly="readonly" rows="4" style="width:100%;background:#f9f2f4;color:#c7254e;font-family:monospace;font-size:12px;cursor:not-allowed;resize:vertical;"><?php echo esc_textarea($legacy_code); ?></textarea> |
| 512 | <p style="margin-top:8px;padding:8px 12px;background:#fff3cd;border-left:4px solid #ffc107;color:#856404;font-size:12px;"> |
| 513 | <?php _e('This module uses legacy inline display logic. Please migrate it to the new snippet-based system.', 'widget-options'); ?> |
| 514 | <?php if (current_user_can(WIDGETOPTS_MIGRATION_PERMISSIONS)) : ?> |
| 515 | <br><a href="<?php echo esc_url($migration_url); ?>" target="_blank"><?php _e('Go to Migration Page', 'widget-options'); ?> →</a> |
| 516 | <?php endif; ?> |
| 517 | </p> |
| 518 | <button type="button" class="fl-builder-button fl-builder-button-primary widgetopts-bb-clear-legacy" style="margin-top:8px;padding:5px 15px;background:#dc3545;color:#fff;border:none;border-radius:3px;cursor:pointer;font-size:12px;" onclick="(function(btn){var form=jQuery(btn).closest('form,.fl-builder-settings');if(form.length){form.find('input[name=widgetopts_settings_logic]').val('');form.find('input[name=widgetopts_logic_cleared]').val('1');jQuery(btn).closest('.widgetopts-legacy-logic-wrap').slideUp();form.find('#fl-field-widgetopts_logic_snippet_id').show();}})( this)"> |
| 519 | <?php _e('Clear Legacy Logic', 'widget-options'); ?> |
| 520 | </button> |
| 521 | </div> |
| 522 | <script> |
| 523 | jQuery(function(){ |
| 524 | var wrap = jQuery('.widgetopts-legacy-logic-wrap'); |
| 525 | if (wrap.length && wrap.is(':visible')) { |
| 526 | wrap.closest('form,.fl-builder-settings').find('#fl-field-widgetopts_logic_snippet_id').hide(); |
| 527 | } |
| 528 | }); |
| 529 | </script> |
| 530 | <?php |
| 531 | } |
| 532 | |
| 533 | function fl_widgetopts_upgrade($name, $value, $field, $settings) |
| 534 | { ?> |
| 535 | <div class="extended-widget-opts-tabcontent extended-widget-opts-tabcontent-gopro"> |
| 536 | <p class="widgetopts-unlock-features"> |
| 537 | <span class="dashicons dashicons-lock"></span><?php _e('Unlock all Options', 'widget-options'); ?> |
| 538 | </p> |
| 539 | <p> |
| 540 | <?php _e('Get the world\'s most complete widget management now with Beaver Builder integration! Upgrade to extended version to get:', 'widget-options'); ?> |
| 541 | </p> |
| 542 | <ul> |
| 543 | <li> |
| 544 | <span class="dashicons dashicons-lock"></span> <?php _e('Animation Options', 'widget-options'); ?> |
| 545 | </li> |
| 546 | <li> |
| 547 | <span class="dashicons dashicons-lock"></span> <?php _e('Custom Styling Options', 'widget-options'); ?> |
| 548 | </li> |
| 549 | <li> |
| 550 | <span class="dashicons dashicons-lock"></span> <?php _e('Set Alignment per Devices', 'widget-options'); ?> |
| 551 | </li> |
| 552 | <li> |
| 553 | <span class="dashicons dashicons-lock"></span> <?php _e('User Roles Visibility Restriction', 'widget-options'); ?> |
| 554 | </li> |
| 555 | <li> |
| 556 | <span class="dashicons dashicons-lock"></span> <?php _e('Fixed/Sticky Widget Options', 'widget-options'); ?> |
| 557 | </li> |
| 558 | <li> |
| 559 | <span class="dashicons dashicons-lock"></span> <?php _e('Days and Date Range Restriction', 'widget-options'); ?> |
| 560 | </li> |
| 561 | <li> |
| 562 | <span class="dashicons dashicons-lock"></span> <?php _e('Link Module Block Options', 'widget-options'); ?> |
| 563 | </li> |
| 564 | <li> |
| 565 | <span class="dashicons dashicons-lock"></span> <?php _e('Extended Taxonomy and Post Types Support', 'widget-options'); ?> |
| 566 | </li> |
| 567 | <li> |
| 568 | <span class="dashicons dashicons-lock"></span> <?php _e('Target URLs and Wildcard Restrictions', 'widget-options'); ?> |
| 569 | </li> |
| 570 | <li> |
| 571 | <span class="dashicons dashicons-lock"></span> <?php _e('Beaver Builder Support', 'widget-options'); ?> |
| 572 | </li> |
| 573 | </ul> |
| 574 | <p><strong><a href="<?php echo apply_filters('widget_options_site_url', trailingslashit(WIDGETOPTS_PLUGIN_WEBSITE) . '?utm_source=beaverbuilder&utm_medium=learnmore&utm_campaign=widgetoptsprotab'); ?>" class="button-primary" target="_blank"><?php _e('Learn More', 'widget-options'); ?></a></strong></p> |
| 575 | </div> |
| 576 | <?php } |
| 577 | |
| 578 | function widgetopts_beaver_is_node_visible($is_visible, $node) |
| 579 | { |
| 580 | |
| 581 | //return if editing |
| 582 | if (class_exists('FLBuilderModel') && FLBuilderModel::is_builder_active()) { |
| 583 | return $is_visible; |
| 584 | } |
| 585 | |
| 586 | global $widget_options; |
| 587 | |
| 588 | $settings = $node->settings; |
| 589 | $hidden = false; |
| 590 | $visibility_opts = isset($settings->widgetopts_visibility_show) ? $settings->widgetopts_visibility_show : 'hide'; |
| 591 | |
| 592 | $tax_opts = (isset($widget_options['settings']) && isset($widget_options['settings']['taxonomies_keys'])) ? $widget_options['settings']['taxonomies_keys'] : array(); |
| 593 | $is_misc = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['misc'])) ? true : false; |
| 594 | $is_types = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['post_type'])) ? true : false; |
| 595 | $is_tax = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['taxonomies'])) ? true : false; |
| 596 | $is_inherit = ('activate' == $widget_options['visibility'] && isset($widget_options['settings']['visibility']) && isset($widget_options['settings']['visibility']['inherit'])) ? true : false; |
| 597 | |
| 598 | // echo '<pre>'; |
| 599 | // print_r( $settings ); |
| 600 | // echo '</pre>'; |
| 601 | |
| 602 | //pages |
| 603 | if ($is_misc && ((is_home() && is_front_page()) || is_front_page())) { |
| 604 | if (isset($settings->widgetopts_visibility_misc) && is_array($settings->widgetopts_visibility_misc) && in_array('home', $settings->widgetopts_visibility_misc) && $visibility_opts == 'hide') { |
| 605 | $hidden = true; //hide if checked on hidden pages |
| 606 | } elseif ((!isset($settings->widgetopts_visibility_misc) || (isset($settings->widgetopts_visibility_misc) && is_array($settings->widgetopts_visibility_misc) && !in_array('home', $settings->widgetopts_visibility_misc))) && $visibility_opts == 'show') { |
| 607 | $hidden = true; //hide if not checked on visible pages |
| 608 | } |
| 609 | |
| 610 | //do return to bypass other conditions |
| 611 | $hidden = apply_filters('widgetopts_beaver_visibility_home', $hidden); |
| 612 | if ($hidden) { |
| 613 | return false; |
| 614 | } |
| 615 | } elseif ($is_misc && is_home()) { |
| 616 | //NOT CHECKED YET |
| 617 | if (isset($settings->widgetopts_visibility_misc) && is_array($settings->widgetopts_visibility_misc) && in_array('blog', $settings->widgetopts_visibility_misc) && $visibility_opts == 'hide') { |
| 618 | $hidden = true; //hide if checked on hidden pages |
| 619 | } elseif ((!isset($settings->widgetopts_visibility_misc) || (isset($settings->widgetopts_visibility_misc) && is_array($settings->widgetopts_visibility_misc) && !in_array('blog', $settings->widgetopts_visibility_misc))) && $visibility_opts == 'show') { |
| 620 | $hidden = true; //hide if not checked on visible pages |
| 621 | } |
| 622 | |
| 623 | //do return to bypass other conditions |
| 624 | $hidden = apply_filters('widgetopts_beaver_visibility_blog', $hidden); |
| 625 | if ($hidden) { |
| 626 | return false; |
| 627 | } |
| 628 | } elseif ($is_tax && is_tag()) { |
| 629 | |
| 630 | if (!isset($settings->widgetopts_visibility_taxonomies) || (isset($settings->widgetopts_visibility_taxonomies) && !is_array($settings->widgetopts_visibility_taxonomies))) { |
| 631 | $settings->widgetopts_visibility_taxonomies = array(); |
| 632 | } |
| 633 | |
| 634 | if (in_array('post_tag', $settings->widgetopts_visibility_taxonomies) && $visibility_opts == 'hide') { |
| 635 | $hidden = true; //hide to all tags |
| 636 | } elseif (in_array('post_tag', $settings->widgetopts_visibility_taxonomies) && $visibility_opts == 'show') { |
| 637 | $hidden = false; //hide to all tags |
| 638 | } |
| 639 | // |
| 640 | // //do return to bypass other conditions |
| 641 | $hidden = apply_filters('widgetopts_beaver_visibility_tags', $hidden); |
| 642 | if ($hidden) { |
| 643 | return false; |
| 644 | } |
| 645 | } elseif ($is_tax && is_tax()) { |
| 646 | $term = get_queried_object(); |
| 647 | //taxonomies page |
| 648 | if (!isset($settings->widgetopts_visibility_taxonomies) || (isset($settings->widgetopts_visibility_taxonomies) && !is_array($settings->widgetopts_visibility_taxonomies))) { |
| 649 | $settings->widgetopts_visibility_taxonomies = array(); |
| 650 | } |
| 651 | // print_r( $term_lists ); |
| 652 | if (in_array($term->taxonomy, $settings->widgetopts_visibility_taxonomies) && $visibility_opts == 'hide') { |
| 653 | $hidden = true; //hide to all tags |
| 654 | } elseif (!in_array($term->taxonomy, $settings->widgetopts_visibility_taxonomies) && $visibility_opts == 'show') { |
| 655 | $hidden = true; //hide to all tags |
| 656 | } |
| 657 | |
| 658 | //do return to bypass other conditions |
| 659 | $hidden = apply_filters('widgetopts_beaver_visibility_taxonomies', $hidden); |
| 660 | if ($hidden) { |
| 661 | return false; |
| 662 | } |
| 663 | } elseif ($is_misc && is_archive()) { |
| 664 | //archives page |
| 665 | if (isset($settings->widgetopts_visibility_misc) && is_array($settings->widgetopts_visibility_misc) && in_array('archives', $settings->widgetopts_visibility_misc) && $visibility_opts == 'hide') { |
| 666 | $hidden = true; //hide if checked on hidden pages |
| 667 | } elseif ((!isset($settings->widgetopts_visibility_misc) || (isset($settings->widgetopts_visibility_misc) && is_array($settings->widgetopts_visibility_misc) && !in_array('archives', $settings->widgetopts_visibility_misc))) && $visibility_opts == 'show') { |
| 668 | $hidden = true; //hide if not checked on visible pages |
| 669 | } |
| 670 | |
| 671 | //do return to bypass other conditions |
| 672 | $hidden = apply_filters('widgetopts_beaver_visibility_archives', $hidden); |
| 673 | if ($hidden) { |
| 674 | return false; |
| 675 | } |
| 676 | } elseif ($is_misc && is_404()) { |
| 677 | //404 page |
| 678 | if (isset($settings->widgetopts_visibility_misc) && is_array($settings->widgetopts_visibility_misc) && in_array('404', $settings->widgetopts_visibility_misc) && $visibility_opts == 'hide') { |
| 679 | $hidden = true; //hide if checked on hidden pages |
| 680 | } elseif ((!isset($settings->widgetopts_visibility_misc) || (isset($settings->widgetopts_visibility_misc) && is_array($settings->widgetopts_visibility_misc) && !in_array('404', $settings->widgetopts_visibility_misc))) && $visibility_opts == 'show') { |
| 681 | $hidden = true; //hide if not checked on visible pages |
| 682 | } |
| 683 | |
| 684 | //do return to bypass other conditions |
| 685 | $hidden = apply_filters('widgetopts_beaver_visibility_404', $hidden); |
| 686 | if ($hidden) { |
| 687 | return false; |
| 688 | } |
| 689 | } elseif ($is_misc && is_search()) { |
| 690 | if (isset($settings->widgetopts_visibility_misc) && is_array($settings->widgetopts_visibility_misc) && in_array('search', $settings->widgetopts_visibility_misc) && $visibility_opts == 'hide') { |
| 691 | $hidden = true; //hide if checked on hidden pages |
| 692 | } elseif ((!isset($settings->widgetopts_visibility_misc) || (isset($settings->widgetopts_visibility_misc) && is_array($settings->widgetopts_visibility_misc) && !in_array('search', $settings->widgetopts_visibility_misc))) && $visibility_opts == 'show') { |
| 693 | $hidden = true; |
| 694 | } |
| 695 | |
| 696 | //do return to bypass other conditions |
| 697 | $hidden = apply_filters('widgetopts_beaver_visibility_search', $hidden); |
| 698 | if ($hidden) { |
| 699 | return false; |
| 700 | } |
| 701 | } elseif (is_single() && !is_page()) { |
| 702 | global $wp_query; |
| 703 | $post = $wp_query->post; |
| 704 | |
| 705 | if (!isset($settings->widgetopts_visibility_types) || ($is_types && !isset($settings->widgetopts_visibility_types)) || !is_array($settings->widgetopts_visibility_types)) { |
| 706 | $settings->widgetopts_visibility_types = array(); |
| 707 | } |
| 708 | |
| 709 | if ($visibility_opts == 'hide' && in_array($post->post_type, $settings->widgetopts_visibility_types)) { |
| 710 | $hidden = true; //hide if exists on hidden pages |
| 711 | } elseif ($visibility_opts == 'show' && !in_array($post->post_type, $settings->widgetopts_visibility_types)) { |
| 712 | $hidden = true; //hide if doesn't exists on visible pages |
| 713 | } |
| 714 | |
| 715 | // do return to bypass other conditions |
| 716 | $hidden = apply_filters('widgetopts_beaver_visibility_single', $hidden); |
| 717 | |
| 718 | |
| 719 | // $taxonomy_names = get_post_taxonomies( $post->ID ); |
| 720 | // $array_intersect = array_intersect( $tax_opts, $taxonomy_names ); |
| 721 | |
| 722 | if (!isset($settings->widgetopts_visibility_tax_category)) { |
| 723 | $settings->widgetopts_visibility_tax_category = array(); |
| 724 | } |
| 725 | |
| 726 | if (isset($settings->widgetopts_visibility_tax_category) && !empty($settings->widgetopts_visibility_tax_category)) { |
| 727 | $cats = wp_get_post_categories($post->ID); |
| 728 | |
| 729 | if (is_array($cats) && !empty($cats)) { |
| 730 | $checked_cats = $settings->widgetopts_visibility_tax_category; |
| 731 | $intersect = array_intersect($cats, $checked_cats); |
| 732 | if (!empty($intersect) && $visibility_opts == 'hide') { |
| 733 | $hidden = true; |
| 734 | } elseif (!empty($intersect) && $visibility_opts == 'show') { |
| 735 | $hidden = false; |
| 736 | } |
| 737 | |
| 738 | $hidden = apply_filters('widgetopts_beaver_visibility_single_category', $hidden); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | if ($hidden) { |
| 743 | return false; |
| 744 | } |
| 745 | } elseif ($is_types && is_page()) { |
| 746 | global $wp_query; |
| 747 | |
| 748 | $post = $wp_query->post; |
| 749 | |
| 750 | //do post type condition first |
| 751 | if (isset($settings->widgetopts_visibility_types) && is_array($settings->widgetopts_visibility_types) && in_array('page', $settings->widgetopts_visibility_types)) { |
| 752 | |
| 753 | if (!is_array($settings->widgetopts_visibility_types)) { |
| 754 | $settings->widgetopts_visibility_types = array(); |
| 755 | } |
| 756 | |
| 757 | if ($visibility_opts == 'hide' && in_array('page', $settings->widgetopts_visibility_types)) { |
| 758 | $hidden = true; //hide if exists on hidden pages |
| 759 | } elseif ($visibility_opts == 'show' && !in_array('page', $settings->widgetopts_visibility_types)) { |
| 760 | $hidden = true; //hide if doesn't exists on visible pages |
| 761 | } |
| 762 | } else { |
| 763 | // print_r( $settings['widgetopts_pages'] ); |
| 764 | //do per pages condition |
| 765 | if (!isset($settings->widgetopts_visibility_pages) || (isset($settings->widgetopts_visibility_pages) && !is_array($settings->widgetopts_visibility_pages))) { |
| 766 | $settings->widgetopts_visibility_pages = array(); |
| 767 | } |
| 768 | |
| 769 | if ($visibility_opts == 'hide' && in_array($post->ID, $settings->widgetopts_visibility_pages)) { |
| 770 | $hidden = true; //hide if exists on hidden pages |
| 771 | } elseif ($visibility_opts == 'show' && !in_array($post->ID, $settings->widgetopts_visibility_pages)) { |
| 772 | $hidden = true; //hide if doesn't exists on visible pages |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | // //do return to bypass other conditions |
| 777 | $hidden = apply_filters('widgetopts_beaver_visibility_page', $hidden); |
| 778 | if ($hidden) { |
| 779 | return false; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | //ACF |
| 784 | if (isset($widget_options['acf']) && 'activate' == $widget_options['acf']) { |
| 785 | if (isset($settings->widgetopts_acf_field) && !empty($settings->widgetopts_acf_field)) { |
| 786 | $acf = get_field_object($settings->widgetopts_acf_field); |
| 787 | if ($acf && is_array($acf)) { |
| 788 | $acf_visibility = isset($settings->widgetopts_acf_visibility) ? $settings->widgetopts_acf_visibility : 'hide'; |
| 789 | |
| 790 | //handle repeater fields |
| 791 | if (isset($acf['value'])) { |
| 792 | if (is_array($acf['value'])) { |
| 793 | $acf['value'] = implode(', ', array_map(function ($acf_array_value) { |
| 794 | $acf_implode = ''; |
| 795 | if (is_array($acf_array_value)) { |
| 796 | $acf_implode = implode(',', array_filter($acf_array_value)); |
| 797 | } else { |
| 798 | $acf_implode = $acf_array_value; |
| 799 | } |
| 800 | return $acf_implode; |
| 801 | }, $acf['value'])); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | switch ($settings->widgetopts_acf_condition) { |
| 806 | case 'equal': |
| 807 | if (isset($acf['value'])) { |
| 808 | if ('show' == $acf_visibility && $acf['value'] == $settings->widgetopts_acf_value) { |
| 809 | $hidden = false; |
| 810 | } else if ('show' == $acf_visibility && $acf['value'] != $settings->widgetopts_acf_value) { |
| 811 | $hidden = true; |
| 812 | } else if ('hide' == $acf_visibility && $acf['value'] == $settings->widgetopts_acf_value) { |
| 813 | $hidden = true; |
| 814 | } else if ('hide' == $acf_visibility && $acf['value'] != $settings->widgetopts_acf_value) { |
| 815 | $hidden = false; |
| 816 | } |
| 817 | } |
| 818 | break; |
| 819 | |
| 820 | case 'not_equal': |
| 821 | if (isset($acf['value'])) { |
| 822 | if ('show' == $acf_visibility && $acf['value'] == $settings->widgetopts_acf_value) { |
| 823 | $hidden = true; |
| 824 | } else if ('show' == $acf_visibility && $acf['value'] != $settings->widgetopts_acf_value) { |
| 825 | $hidden = false; |
| 826 | } else if ('hide' == $acf_visibility && $acf['value'] == $settings->widgetopts_acf_value) { |
| 827 | $hidden = false; |
| 828 | } else if ('hide' == $acf_visibility && $acf['value'] != $settings->widgetopts_acf_value) { |
| 829 | $hidden = true; |
| 830 | } |
| 831 | } |
| 832 | break; |
| 833 | |
| 834 | case 'contains': |
| 835 | if (isset($acf['value'])) { |
| 836 | if ('show' == $acf_visibility && strpos($acf['value'], $settings->widgetopts_acf_value) !== false) { |
| 837 | $hidden = false; |
| 838 | } else if ('show' == $acf_visibility && strpos($acf['value'], $settings->widgetopts_acf_value) === false) { |
| 839 | $hidden = true; |
| 840 | } else if ('hide' == $acf_visibility && strpos($acf['value'], $settings->widgetopts_acf_value) !== false) { |
| 841 | $hidden = true; |
| 842 | } else if ('hide' == $acf_visibility && strpos($acf['value'], $settings->widgetopts_acf_value) === false) { |
| 843 | $hidden = false; |
| 844 | } |
| 845 | } |
| 846 | break; |
| 847 | |
| 848 | case 'not_contains': |
| 849 | if (isset($acf['value'])) { |
| 850 | if ('show' == $acf_visibility && strpos($acf['value'], $settings->widgetopts_acf_value) !== false) { |
| 851 | $hidden = true; |
| 852 | } else if ('show' == $acf_visibility && strpos($acf['value'], $settings->widgetopts_acf_value) === false) { |
| 853 | $hidden = false; |
| 854 | } else if ('hide' == $acf_visibility && strpos($acf['value'], $settings->widgetopts_acf_value) !== false) { |
| 855 | $hidden = false; |
| 856 | } else if ('hide' == $acf_visibility && strpos($acf['value'], $settings->widgetopts_acf_value) === false) { |
| 857 | $hidden = true; |
| 858 | } |
| 859 | } |
| 860 | break; |
| 861 | |
| 862 | case 'empty': |
| 863 | if ('show' == $acf_visibility && empty($acf['value'])) { |
| 864 | $hidden = false; |
| 865 | } else if ('show' == $acf_visibility && !empty($acf['value'])) { |
| 866 | $hidden = true; |
| 867 | } elseif ('hide' == $acf_visibility && empty($acf['value'])) { |
| 868 | $hidden = true; |
| 869 | } else if ('hide' == $acf_visibility && !empty($acf['value'])) { |
| 870 | $hidden = false; |
| 871 | } |
| 872 | break; |
| 873 | |
| 874 | case 'not_empty': |
| 875 | if ('show' == $acf_visibility && empty($acf['value'])) { |
| 876 | $hidden = true; |
| 877 | } else if ('show' == $acf_visibility && !empty($acf['value'])) { |
| 878 | $hidden = false; |
| 879 | } elseif ('hide' == $acf_visibility && empty($acf['value'])) { |
| 880 | $hidden = false; |
| 881 | } else if ('hide' == $acf_visibility && !empty($acf['value'])) { |
| 882 | $hidden = true; |
| 883 | } |
| 884 | break; |
| 885 | |
| 886 | default: |
| 887 | # code... |
| 888 | break; |
| 889 | } |
| 890 | |
| 891 | // //do return to bypass other conditions |
| 892 | $hidden = apply_filters('widgetopts_beaver_visibility_acf', $hidden); |
| 893 | if ($hidden) { |
| 894 | return false; |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | //widget logic |
| 901 | if (isset($widget_options['logic']) && 'activate' == $widget_options['logic']) { |
| 902 | // New snippet-based system |
| 903 | if (isset($settings->widgetopts_logic_snippet_id) && !empty($settings->widgetopts_logic_snippet_id)) { |
| 904 | $snippet_id = $settings->widgetopts_logic_snippet_id; |
| 905 | if (class_exists('WidgetOpts_Snippets_API')) { |
| 906 | $result = WidgetOpts_Snippets_API::execute_snippet($snippet_id); |
| 907 | if ($result === false) { |
| 908 | return false; |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | // Legacy support for old inline logic |
| 913 | elseif (isset($settings->widgetopts_settings_logic) && !empty($settings->widgetopts_settings_logic)) { |
| 914 | // Flag that legacy migration is needed |
| 915 | if (!get_option('wopts_display_logic_migration_required', false)) { |
| 916 | update_option('wopts_display_logic_migration_required', true); |
| 917 | } |
| 918 | |
| 919 | $display_logic = stripslashes(trim($settings->widgetopts_settings_logic)); |
| 920 | $display_logic = apply_filters('widget_options_logic_override', $display_logic); |
| 921 | $display_logic = apply_filters('extended_widget_options_logic_override', $display_logic); |
| 922 | if ($display_logic === false) { |
| 923 | return false; |
| 924 | } |
| 925 | if ($display_logic === true) { |
| 926 | return true; |
| 927 | } |
| 928 | $display_logic = htmlspecialchars_decode($display_logic, ENT_QUOTES); |
| 929 | try { |
| 930 | if (!widgetopts_safe_eval($display_logic)) { |
| 931 | return false; |
| 932 | } |
| 933 | } catch (ParseError $e) { |
| 934 | return false; |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | return $is_visible; |
| 940 | } |
| 941 | function widgetopts_plugin_check() |
| 942 | { |
| 943 | if (!defined('WIDGETOPTS_PLUGIN_NAME')) { ?> |
| 944 | <div class="widgetopts_activated_notice notice-error notice" style="box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);"> |
| 945 | <p> |
| 946 | <?php _e('<strong>Widget Options Plugin</strong> is required for the <em>Widget Options for Beaver Builder</em> to work properly. Please get the plugin <a href="https://wordpress.org/plugins/widget-options/" target="_blank">here</a>. Thanks!', 'widget-options'); ?> |
| 947 | </p> |
| 948 | </div> |
| 949 | <?php } |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | add_action('plugins_loaded', array('WP_Widget_Options_Beaver', 'init')); |
| 954 | // new WP_Widget_Options_Beaver(); |
| 955 | |
| 956 | /** |
| 957 | * Protect legacy display logic from modification/injection on BB save. |
| 958 | * Always restores the DB value for widgetopts_settings_logic per node. |
| 959 | * Prevents code injection via console AND data loss before migration. |
| 960 | * |
| 961 | * @since 5.1 |
| 962 | */ |
| 963 | add_filter('fl_builder_before_save_layout', function($data) { |
| 964 | if (!is_array($data) || !class_exists('FLBuilderModel')) { |
| 965 | return $data; |
| 966 | } |
| 967 | |
| 968 | $post_id = FLBuilderModel::get_post_id(); |
| 969 | if (!$post_id) { |
| 970 | return $data; |
| 971 | } |
| 972 | |
| 973 | // Build map: node_id => old widgetopts_settings_logic from DB |
| 974 | $old_logic = array(); |
| 975 | foreach (array('_fl_builder_data', '_fl_builder_draft') as $mk) { |
| 976 | $old_nodes = get_post_meta($post_id, $mk, true); |
| 977 | if (is_array($old_nodes)) { |
| 978 | foreach ($old_nodes as $nid => $node) { |
| 979 | if (is_object($node) && isset($node->settings) && is_object($node->settings) |
| 980 | && !empty($node->settings->widgetopts_settings_logic)) { |
| 981 | $old_logic[$nid] = $node->settings->widgetopts_settings_logic; |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | // Protect each node |
| 988 | foreach ($data as $nid => &$node) { |
| 989 | if (!is_object($node) || !isset($node->settings) || !is_object($node->settings)) { |
| 990 | continue; |
| 991 | } |
| 992 | |
| 993 | $wopt_ver = isset($node->settings->widgetopts_wopt_version) ? $node->settings->widgetopts_wopt_version : ''; |
| 994 | $has_snippet = !empty($node->settings->widgetopts_logic_snippet_id); |
| 995 | $has_legacy = !empty($node->settings->widgetopts_settings_logic); |
| 996 | |
| 997 | // If wopt_version >= 4.2: legacy logic is obsolete — clear it |
| 998 | if ($wopt_ver !== '' && version_compare($wopt_ver, '4.2', '>=')) { |
| 999 | if ($has_legacy) { |
| 1000 | $node->settings->widgetopts_settings_logic = ''; |
| 1001 | } |
| 1002 | continue; |
| 1003 | } |
| 1004 | |
| 1005 | // Auto-clear legacy logic if snippet_id is already set (migration done) |
| 1006 | if ($has_snippet && $has_legacy) { |
| 1007 | $node->settings->widgetopts_settings_logic = ''; |
| 1008 | $node->settings->widgetopts_wopt_version = WIDGETOPTS_VERSION; |
| 1009 | continue; |
| 1010 | } |
| 1011 | |
| 1012 | // User intentionally cleared legacy logic via Clear button |
| 1013 | if (!empty($node->settings->widgetopts_logic_cleared)) { |
| 1014 | $node->settings->widgetopts_settings_logic = ''; |
| 1015 | unset($node->settings->widgetopts_logic_cleared); |
| 1016 | $node->settings->widgetopts_wopt_version = WIDGETOPTS_VERSION; |
| 1017 | continue; |
| 1018 | } |
| 1019 | |
| 1020 | $old_val = isset($old_logic[$nid]) ? $old_logic[$nid] : ''; |
| 1021 | $new_val = isset($node->settings->widgetopts_settings_logic) ? $node->settings->widgetopts_settings_logic : ''; |
| 1022 | |
| 1023 | if ($new_val !== $old_val) { |
| 1024 | if ($old_val !== '') { |
| 1025 | $node->settings->widgetopts_settings_logic = $old_val; |
| 1026 | } else { |
| 1027 | unset($node->settings->widgetopts_settings_logic); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | } |
| 1032 | |
| 1033 | return $data; |
| 1034 | }, 10, 1); |
| 1035 | |
| 1036 | endif; |
| 1037 |