admin
1 month ago
pagebuilders
1 month ago
snippets
1 month ago
widgets
1 month ago
ajax-functions.php
1 month ago
extras.php
1 month ago
install.php
1 month ago
scripts.php
1 month ago
transient.php
1 month ago
scripts.php
546 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Scripts |
| 5 | * |
| 6 | * @copyright Copyright (c) 2016, Jeffrey Carandang |
| 7 | * @since 3.0 |
| 8 | */ |
| 9 | |
| 10 | // Exit if accessed directly |
| 11 | if (!defined('ABSPATH')) exit; |
| 12 | |
| 13 | /** |
| 14 | * Load Scripts |
| 15 | * |
| 16 | * Enqueues the required scripts. |
| 17 | * |
| 18 | * @since 3.0 |
| 19 | * @return void |
| 20 | */ |
| 21 | |
| 22 | function widgetopts_load_scripts() |
| 23 | { |
| 24 | global $pagenow; |
| 25 | $css_dir = WIDGETOPTS_PLUGIN_URL . 'assets/css/'; |
| 26 | $js_dir = WIDGETOPTS_PLUGIN_URL . 'assets/js/'; |
| 27 | wp_enqueue_style('widgetopts-styles', $css_dir . 'widget-options.css', array(), WIDGETOPTS_VERSION); |
| 28 | |
| 29 | if (isset($pagenow) && $pagenow === 'customize.php') { |
| 30 | wp_add_inline_script( |
| 31 | 'customize-controls', |
| 32 | '(function(){ |
| 33 | document.querySelector("#save").onclick = function() { |
| 34 | setTimeout(function(){wp.customize.previewer.refresh();}, 3000); |
| 35 | } |
| 36 | })();', |
| 37 | 'after' |
| 38 | ); |
| 39 | } |
| 40 | } |
| 41 | add_action('wp_enqueue_scripts', 'widgetopts_load_scripts'); |
| 42 | add_action('customize_controls_enqueue_scripts', 'widgetopts_load_scripts'); |
| 43 | |
| 44 | /** |
| 45 | * Load Admin Scripts |
| 46 | * |
| 47 | * Enqueues the required admin scripts. |
| 48 | * |
| 49 | * @since 3.0 |
| 50 | * @global $widget_options |
| 51 | * @param string $hook Page hook |
| 52 | * @return void |
| 53 | */ |
| 54 | if (!function_exists('widgetopts_load_admin_scripts')) : |
| 55 | function widgetopts_load_admin_scripts($hook) |
| 56 | { |
| 57 | global $widget_options, $wp_version; |
| 58 | |
| 59 | $in_footer_args = false; |
| 60 | $is_6_3_and_above = version_compare($wp_version, '6.3', '>='); |
| 61 | if ($is_6_3_and_above) { |
| 62 | $in_footer_args = array( |
| 63 | 'in_footer' => true, |
| 64 | 'strategy' => 'defer', |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | $js_dir = WIDGETOPTS_PLUGIN_URL . 'assets/js/'; |
| 69 | $css_dir = WIDGETOPTS_PLUGIN_URL . 'assets/css/'; |
| 70 | $is_siteorigin = (isset($widget_options['siteorigin'])) ? $widget_options['siteorigin'] : ''; |
| 71 | |
| 72 | // Use minified libraries if SCRIPT_DEBUG is turned off |
| 73 | $suffix = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min'; |
| 74 | |
| 75 | wp_enqueue_style('widgetopts-admin-styles', $css_dir . 'admin.css', array(), WIDGETOPTS_VERSION); |
| 76 | |
| 77 | wp_enqueue_script( |
| 78 | 'widgetopts-global-script', |
| 79 | $js_dir . 'widgetopts.global.js', |
| 80 | array('jquery'), |
| 81 | WIDGETOPTS_VERSION, |
| 82 | ($is_6_3_and_above ? $in_footer_args : true) |
| 83 | ); |
| 84 | |
| 85 | //load only on admin pages with widgets |
| 86 | if (($is_siteorigin) || (!$is_siteorigin && in_array($hook, apply_filters('widgetopts_load_option-tabs_scripts', array('widgets.php', 'customize.php', 'nav-menus.php'))))) { |
| 87 | |
| 88 | if (!in_array($hook, apply_filters('widgetopts_exclude_jqueryui', array('toplevel_page_et_divi_options', 'toplevel_page_wpcf7', 'edit.php')))) { |
| 89 | wp_enqueue_style('jquery-ui'); |
| 90 | } |
| 91 | |
| 92 | if (in_array($hook, apply_filters('widgetopts_load_liveFilter_scripts', array('widgets.php', 'nav-menus.php')))) { |
| 93 | wp_enqueue_script( |
| 94 | 'jquery-liveFilter', |
| 95 | plugins_url('assets/js/jquery.liveFilter.js', dirname(__FILE__)), |
| 96 | array('jquery'), |
| 97 | WIDGETOPTS_VERSION, |
| 98 | ($is_6_3_and_above ? $in_footer_args : true) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | wp_enqueue_script( |
| 103 | 'jquery-widgetopts-option-tabs', |
| 104 | plugins_url('assets/js/wpWidgetOpts.js', dirname(__FILE__)), |
| 105 | array('jquery', 'jquery-ui-core', 'jquery-ui-tabs', 'jquery-ui-datepicker'), |
| 106 | WIDGETOPTS_VERSION, |
| 107 | ($is_6_3_and_above ? $in_footer_args : true) |
| 108 | ); |
| 109 | |
| 110 | |
| 111 | wp_enqueue_style('jquery-widgetopts-select2-css', plugins_url('assets/css/select2.min.css', dirname(__FILE__)), array(), WIDGETOPTS_VERSION); |
| 112 | |
| 113 | if (!wp_script_is('select2', 'enqueued')) { |
| 114 | wp_enqueue_script( |
| 115 | 'jquery-widgetopts-select2-script', |
| 116 | $js_dir . 'select2.min.js', |
| 117 | array('jquery'), |
| 118 | WIDGETOPTS_VERSION, |
| 119 | ($is_6_3_and_above ? $in_footer_args : true) |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | wp_enqueue_style('jquery-widgetopts-multiselect-css', plugins_url('assets/css/bootstrap-multiselect.min.css', dirname(__FILE__)), array(), WIDGETOPTS_VERSION); |
| 124 | wp_enqueue_script( |
| 125 | 'jquery-widgetopts-multiselect-script', |
| 126 | $js_dir . 'bootstrap-multiselect.min.js', |
| 127 | array('jquery'), |
| 128 | WIDGETOPTS_VERSION, |
| 129 | ($is_6_3_and_above ? $in_footer_args : true) |
| 130 | ); |
| 131 | |
| 132 | $form = '<div id="widgetopts-widgets-chooser"> |
| 133 | <label class="screen-reader-text" for="widgetopts-search-chooser">' . __('Search Sidebar', 'widget-options') . '</label> |
| 134 | <input type="text" id="widgetopts-search-chooser" class="widgetopts-widgets-search" placeholder="' . __('Search sidebar…', 'widget-options') . '" /> |
| 135 | <div class="widgetopts-search-icon" aria-hidden="true"></div> |
| 136 | <button type="button" class="widgetopts-clear-results"><span class="screen-reader-text">' . __('Clear Results', 'widget-options') . '</span></button> |
| 137 | <p class="screen-reader-text" id="widgetopts-chooser-desc">' . __('The search results will be updated as you type.', 'widget-options') . '</p> |
| 138 | </div>'; |
| 139 | |
| 140 | $btn_controls = ''; |
| 141 | if (isset($widget_options['move']) && 'activate' == $widget_options['move']) { |
| 142 | $btn_controls .= ' | <button type="button" class="button-link widgetopts-control" data-action="move">' . __('Move', 'widget-options') . '</button>'; |
| 143 | } |
| 144 | |
| 145 | $sidebaropts = ''; |
| 146 | if (isset($widget_options['widget_area']) && 'activate' == $widget_options['widget_area']) { |
| 147 | /* Updated by Haive Vistal - 04/20/2023 - Make sure no empty space in under the widgets if no activated links */ |
| 148 | $remove_widget_link = 0; |
| 149 | $download_backup_link = 0; |
| 150 | $delete_all_widget_link = 0; |
| 151 | |
| 152 | if (isset($widget_options['settings']['widget_area']) && isset($widget_options['settings']['widget_area']['remove']) && '1' == $widget_options['settings']['widget_area']['remove']) { |
| 153 | $remove_widget_link = 1; |
| 154 | } |
| 155 | if (isset($widget_options['settings']['widget_area']) && isset($widget_options['settings']['widget_area']['backup']) && '1' == $widget_options['settings']['widget_area']['backup']) { |
| 156 | $download_backup_link = 1; |
| 157 | } |
| 158 | |
| 159 | if (isset($widget_options['settings']['widget_area']) && isset($widget_options['settings']['widget_area']['remove']) && '1' == $widget_options['settings']['widget_area']['remove']) { |
| 160 | $delete_all_widget_link = 1; |
| 161 | } |
| 162 | |
| 163 | if ($remove_widget_link == 1 || $download_backup_link == 1 || $delete_all_widget_link == 1) { |
| 164 | $sidebaropts = '<div class="widgetopts-sidebaropts">'; |
| 165 | if ($remove_widget_link == 1) { |
| 166 | $sidebaropts .= '<a href="#" class="sidebaropts-clear"> |
| 167 | <span class="dashicons dashicons-warning"></span> ' . __('Remove All Widgets', 'widget-options') . ' |
| 168 | </a>'; |
| 169 | } |
| 170 | if ($download_backup_link == 1) { |
| 171 | $sidebaropts .= '<a href="' . esc_url(wp_nonce_url(admin_url('tools.php?page=widgetopts_migrator_settings&action=export&single_sidebar=__sidebaropts__'), 'widgeopts_export', 'widgeopts_nonce_export')) . '"> |
| 172 | <span class="dashicons dashicons-download"></span> ' . __('Download Backup', 'widget-options') . ' |
| 173 | </a>'; |
| 174 | } |
| 175 | if ($delete_all_widget_link == 1) { |
| 176 | $sidebaropts .= '<div class="sidebaropts-confirm"><p> |
| 177 | ' . __('Are you sure you want to DELETE ALL widgets associated to __sidebar_opts__?', 'widget-options') . ' |
| 178 | </p> |
| 179 | <button class="button">' . __('No', 'widget-options') . '</button> |
| 180 | <button class="button button-primary">' . __('Yes', 'widget-options') . '</button> |
| 181 | </div>'; |
| 182 | } |
| 183 | $sidebaropts .= '</div>'; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /* Added by Haive Vistal - 04/20/2023 - Default link for all widgets to go through widget options panel settings */ |
| 188 | // $sidebaropts .= '<div class="widgetopts-super widgetopts-sidebaropts">'; |
| 189 | // $sidebaropts .= '<a href="'. esc_url( wp_nonce_url( admin_url('options-general.php?page=widgetopts_plugin_settings'), 'widgeopts_setings', 'widgeopts_nonce_settings') ) .'"> |
| 190 | // <span class="dashicons dashicons-admin-settings"></span> '. __( 'Enable more Widget Options superpowers', 'widget-options' ) .' |
| 191 | // </a>'; |
| 192 | // $sidebaropts .= '</div>'; |
| 193 | |
| 194 | wp_localize_script('jquery-widgetopts-option-tabs', 'widgetopts10n', array('ajax_url' => admin_url('admin-ajax.php'), 'opts_page' => esc_url(admin_url('options-general.php?page=widgetopts_plugin_settings')), 'search_form' => $form, 'sidebaropts' => $sidebaropts, 'controls' => $btn_controls, 'translation' => array('manage_settings' => __('Manage Widget Options', 'widget-options'), 'search_chooser' => __('Search sidebar…', 'widget-options')), 'validate_expression_nonce' => wp_create_nonce('widgetopts-expression-nonce'), 'migration_nonce' => wp_create_nonce('widgetopts_migration_nonce'), 'migration_strings' => array('prompt_title' => __('Enter a name for the new snippet:', 'widget-options'), 'default_title' => __('Migrated Snippet', 'widget-options'), 'converting' => __('Converting...', 'widget-options'), 'success' => __('Converted successfully!', 'widget-options'), 'error' => __('Conversion failed.', 'widget-options')))); |
| 195 | } else { |
| 196 | wp_localize_script('widgetopts-global-script', 'widgetopts10n', array('ajax_url' => admin_url('admin-ajax.php'), 'opts_page' => esc_url(admin_url('options-general.php?page=widgetopts_plugin_settings')), 'translation' => array('manage_settings' => __('Manage Widget Options', 'widget-options'), 'search_chooser' => __('Search sidebar…', 'widget-options')), 'validate_expression_nonce' => wp_create_nonce('widgetopts-expression-nonce'))); |
| 197 | } |
| 198 | |
| 199 | if (in_array($hook, apply_filters('widgetopts_load_settings_scripts', array('settings_page_widgetopts_plugin_settings')))) { |
| 200 | wp_register_script( |
| 201 | 'jquery-widgetopts-settings', |
| 202 | $js_dir . 'settings' . $suffix . '.js', |
| 203 | array('jquery'), |
| 204 | WIDGETOPTS_VERSION, |
| 205 | ($is_6_3_and_above ? $in_footer_args : true) |
| 206 | ); |
| 207 | |
| 208 | $translation = array( |
| 209 | 'save_settings' => __('Save Settings', 'widget-options'), |
| 210 | 'close_settings' => __('Close', 'widget-options'), |
| 211 | 'show_settings' => __('Configure Settings', 'widget-options'), |
| 212 | 'hide_settings' => __('Hide Settings', 'widget-options'), |
| 213 | 'show_description' => __('Learn More', 'widget-options'), |
| 214 | 'hide_description' => __('Hide Details', 'widget-options'), |
| 215 | 'show_information' => __('Show Details', 'widget-options'), |
| 216 | 'activate' => __('Enable', 'widget-options'), |
| 217 | 'deactivate' => __('Disable', 'widget-options'), |
| 218 | 'successful_save' => __('Settings saved successfully for %1$s.', 'widget-options'), |
| 219 | 'deactivate_btn' => __('Deactivate License', 'widget-options'), |
| 220 | 'activate_btn' => __('Activate License', 'widget-options'), |
| 221 | 'status_valid' => __('Valid', 'widget-options'), |
| 222 | 'status_invalid' => __('Invalid', 'widget-options'), |
| 223 | ); |
| 224 | |
| 225 | wp_enqueue_script('jquery-widgetopts-settings'); |
| 226 | wp_localize_script('jquery-widgetopts-settings', 'widgetopts', array('translation' => $translation, 'ajax_action' => 'widgetopts_ajax_settings', 'ajax_nonce' => wp_create_nonce('widgetopts-settings-nonce'),)); |
| 227 | } |
| 228 | } |
| 229 | add_action('admin_enqueue_scripts', 'widgetopts_load_admin_scripts', 100); |
| 230 | endif; |
| 231 | |
| 232 | if (!function_exists('widgetopts_widgets_footer')) { |
| 233 | function widgetopts_widgets_footer() |
| 234 | { |
| 235 | global $widget_options; ?> |
| 236 | <div class="widgetsopts-chooser" style="display:none;"> |
| 237 | <?php if (isset($widget_options['search']) && 'activate' == $widget_options['search']) : ?> |
| 238 | <div id="widgetopts-widgets-chooser"> |
| 239 | <label class="screen-reader-text" for="widgetopts-search-chooser"><?php _e('Search Sidebar', 'widget-options'); ?></label> |
| 240 | <input type="text" id="widgetsopts-widgets-search" class="widgetopts-widgets-search widgetsopts-widgets-search" placeholder="Search sidebar…"> |
| 241 | <div class="widgetopts-search-icon" aria-hidden="true"></div> |
| 242 | <button type="button" class="widgetopts-clear-results"><span class="screen-reader-text"><?php _e('Clear Results', 'widget-options'); ?></span></button> |
| 243 | <p class="screen-reader-text" id="widgetopts-chooser-desc"><?php _e('The search results will be updated as you type.', 'widget-options'); ?></p> |
| 244 | </div> |
| 245 | <?php endif; ?> |
| 246 | <ul class="widgetopts-chooser-sidebars"></ul> |
| 247 | <div class="widgetsopts-chooser-actions"> |
| 248 | <button class="button widgetsopts-chooser-cancel"><?php _e('Cancel', 'widget-options'); ?></button> |
| 249 | <button class="button button-primary widgetopts-chooser-action"><span><?php _e('Move', 'widget-options'); ?></span> <?php _e('Widget', 'widget-options'); ?></button> |
| 250 | </div> |
| 251 | </div> |
| 252 | <?php } |
| 253 | add_action('admin_footer-widgets.php', 'widgetopts_widgets_footer'); |
| 254 | } |
| 255 | |
| 256 | if (!function_exists('widgetopts_widgets_footer_additional_script')) { |
| 257 | function widgetopts_widgets_footer_additional_script() |
| 258 | { |
| 259 | ?> |
| 260 | <script> |
| 261 | (function() { |
| 262 | /*widget option search option function*/ |
| 263 | function widgetopts_seach_button_function(e) { |
| 264 | let current_parent = e.target.closest('.extended-widget-opts-parent-option'); |
| 265 | let current_select = current_parent.querySelector('.extended-widget-opts-select2-dropdown'); |
| 266 | current_select.classList.add('select2-hidden-accessible'); |
| 267 | current_parent.querySelector('.select2-container').classList.remove('widgetopts-is-hidden'); |
| 268 | |
| 269 | jQuery(current_parent).find('.multiselect-native-select .btn-group').addClass('hide'); |
| 270 | jQuery(this).css({ |
| 271 | "background-color": "#3D434A", |
| 272 | color: "#fff" |
| 273 | }); |
| 274 | |
| 275 | jQuery(this).parent().find('.widgetopts-dropdown-option-btn').css({ |
| 276 | "background-color": "#fff", |
| 277 | color: "#777A80" |
| 278 | }); |
| 279 | } |
| 280 | |
| 281 | jQuery(document).on('click', '.widgetopts-search-option-btn', widgetopts_seach_button_function); |
| 282 | |
| 283 | // let search_btns = document.getElementsByClassName('widgetopts-search-option-btn'); |
| 284 | // for (let i = 0; i < search_btns.length; i++) { |
| 285 | // search_btns[i].addEventListener('click', widgetopts_seach_button_function, false); |
| 286 | // } /*end of widget option search option function*/ |
| 287 | |
| 288 | /*widget option dropdown option function*/ |
| 289 | function widgetopts_dropdown_button_function(e) { |
| 290 | let current_parent = e.target.closest('.extended-widget-opts-parent-option'); |
| 291 | let current_select = current_parent.querySelector('.extended-widget-opts-select2-dropdown'); |
| 292 | current_select.classList.remove('select2-hidden-accessible'); |
| 293 | current_parent.querySelector('.select2-container').classList.add('widgetopts-is-hidden'); |
| 294 | |
| 295 | jQuery(this).css({ |
| 296 | "background-color": "#3D434A", |
| 297 | color: "#fff" |
| 298 | }); |
| 299 | |
| 300 | jQuery(this).parent().find('.widgetopts-search-option-btn').css({ |
| 301 | "background-color": "#fff", |
| 302 | color: "#777A80" |
| 303 | }); |
| 304 | |
| 305 | if (jQuery(current_parent).find('.multiselect-native-select').length > 0) { |
| 306 | jQuery(current_parent).find('.multiselect-native-select .btn-group').removeClass('hide').find('.multiselect-container.dropdown-menu').removeClass('show'); |
| 307 | } else { |
| 308 | let spinner = '<span class="spinner multiple-spinner" style="visibility: visible; float: none; display: inline-block; vertical-align: bottom;"></span>'; |
| 309 | jQuery(e.target).parent().append(spinner); |
| 310 | jQuery(current_select).multiselect({ |
| 311 | onInitialized: function(select, container) { |
| 312 | setTimeout(function() { |
| 313 | jQuery('.multiple-spinner').remove(); |
| 314 | }, 500); |
| 315 | }, |
| 316 | onChange: function(option, checked, select) { |
| 317 | jQuery(option).parents('.extended-widget-opts-parent-option').children('div:nth-child(1)').append(spinner); |
| 318 | setTimeout(function() { |
| 319 | jQuery('.multiple-spinner').remove(); |
| 320 | }, 500); |
| 321 | }, |
| 322 | buttonText: () => 'Click to add more', |
| 323 | keepOrder: true |
| 324 | }); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | jQuery(document).on('click', '.widgetopts-dropdown-option-btn', widgetopts_dropdown_button_function); |
| 329 | |
| 330 | // let drop_btns = document.getElementsByClassName('widgetopts-dropdown-option-btn'); |
| 331 | // for (let i = 0; i < drop_btns.length; i++) { |
| 332 | // drop_btns[i].addEventListener('click', widgetopts_dropdown_button_function, false); |
| 333 | // } /*end of widget option dropdown option function*/ |
| 334 | |
| 335 | jQuery(document).on('click', '.multiselect-native-select .multiselect.dropdown-toggle', function() { |
| 336 | jQuery(this).parent().find('.multiselect-container.dropdown-menu').each(function() { |
| 337 | if (jQuery(this).hasClass('show')) { |
| 338 | jQuery(this).removeClass('show'); |
| 339 | } else { |
| 340 | jQuery(this).addClass('show'); |
| 341 | } |
| 342 | }); |
| 343 | |
| 344 | }); |
| 345 | |
| 346 | // Select2 AJAX config for dynamic snippet search |
| 347 | var snippetSelect2Config = { |
| 348 | placeholder: '<?php echo esc_js(__("— No Logic (Always Show) —", "widget-options")); ?>', |
| 349 | allowClear: true, |
| 350 | width: '100%', |
| 351 | minimumInputLength: 0, |
| 352 | ajax: { |
| 353 | url: ajaxurl, |
| 354 | type: 'POST', |
| 355 | dataType: 'json', |
| 356 | delay: 250, |
| 357 | data: function(params) { |
| 358 | return { |
| 359 | action: 'widgetopts_get_snippets_ajax', |
| 360 | search: params.term || '' |
| 361 | }; |
| 362 | }, |
| 363 | processResults: function(response) { |
| 364 | var results = [{id: '', text: '<?php echo esc_js(__("— No Logic (Always Show) —", "widget-options")); ?>'}]; |
| 365 | if (response.success && response.data && response.data.snippets) { |
| 366 | response.data.snippets.forEach(function(snippet) { |
| 367 | results.push({id: String(snippet.id), text: snippet.title, description: snippet.description || ''}); |
| 368 | }); |
| 369 | } |
| 370 | return { results: results }; |
| 371 | }, |
| 372 | cache: true |
| 373 | } |
| 374 | }; |
| 375 | |
| 376 | // Update snippet description text next to the select |
| 377 | function updateSnippetDesc($select, description) { |
| 378 | var $desc = $select.closest('.widget-opts-logic').find('.widgetopts-snippet-desc'); |
| 379 | if ($desc.length) { |
| 380 | if (description) { |
| 381 | $desc.text(description).show(); |
| 382 | } else { |
| 383 | $desc.text('').hide(); |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | // Init Select2 AJAX on a single select element |
| 389 | function initSelect2Ajax($select) { |
| 390 | if ($select.hasClass('select2-hidden-accessible')) { |
| 391 | $select.select2('destroy'); |
| 392 | } |
| 393 | var config = jQuery.extend({}, snippetSelect2Config, { |
| 394 | dropdownParent: $select.closest('.widget-content, .widget-inside, form') |
| 395 | }); |
| 396 | $select.select2(config); |
| 397 | $select.off('select2:select.woDesc select2:unselect.woDesc'); |
| 398 | $select.on('select2:select.woDesc', function(e) { |
| 399 | updateSnippetDesc($select, e.params.data.description || ''); |
| 400 | }); |
| 401 | $select.on('select2:unselect.woDesc', function() { |
| 402 | updateSnippetDesc($select, ''); |
| 403 | }); |
| 404 | } |
| 405 | |
| 406 | // Initialize Select2 for all uninitialized snippet dropdowns |
| 407 | function initSnippetSelect2() { |
| 408 | jQuery('.widgetopts-select2-snippets:not(.select2-hidden-accessible)').each(function() { |
| 409 | initSelect2Ajax(jQuery(this)); |
| 410 | }); |
| 411 | } |
| 412 | |
| 413 | // Init on page load |
| 414 | jQuery(document).ready(function() { |
| 415 | setTimeout(initSnippetSelect2, 500); |
| 416 | }); |
| 417 | |
| 418 | // Re-init when widget is added/updated (WordPress fires widget-updated event) |
| 419 | jQuery(document).on('widget-added widget-updated', function(e, widget) { |
| 420 | setTimeout(function() { |
| 421 | jQuery(widget).find('.widgetopts-select2-snippets').each(function() { |
| 422 | initSelect2Ajax(jQuery(this)); |
| 423 | }); |
| 424 | }, 300); |
| 425 | }); |
| 426 | |
| 427 | // Init when widget panel is opened (click on widget title) |
| 428 | jQuery(document).on('click', '.widget-title, .widget-top', function() { |
| 429 | setTimeout(initSnippetSelect2, 300); |
| 430 | }); |
| 431 | |
| 432 | // Init when Widget Options tabs are clicked |
| 433 | jQuery(document).on('click', '.extended-widget-opts-tabs a, .extended-widget-opts-settings-tabnav-ul a', function() { |
| 434 | setTimeout(initSnippetSelect2, 100); |
| 435 | }); |
| 436 | })(); |
| 437 | </script> |
| 438 | <?php } |
| 439 | add_action('admin_footer-widgets.php', 'widgetopts_widgets_footer_additional_script', 999); |
| 440 | } |
| 441 | |
| 442 | if (!function_exists('widgetopts_siteorigin_footer_script')) { |
| 443 | function widgetopts_siteorigin_footer_script() |
| 444 | { |
| 445 | global $widget_options; |
| 446 | // Only load for SiteOrigin pages |
| 447 | if (!isset($widget_options['siteorigin']) || $widget_options['siteorigin'] !== 'activate') { |
| 448 | return; |
| 449 | } |
| 450 | ?> |
| 451 | <script> |
| 452 | (function($) { |
| 453 | // Select2 AJAX config for dynamic snippet search |
| 454 | var soSnippetConfig = { |
| 455 | placeholder: '<?php echo esc_js(__("— No Logic (Always Show) —", "widget-options")); ?>', |
| 456 | allowClear: true, |
| 457 | width: '100%', |
| 458 | minimumInputLength: 0, |
| 459 | ajax: { |
| 460 | url: ajaxurl, |
| 461 | type: 'POST', |
| 462 | dataType: 'json', |
| 463 | delay: 250, |
| 464 | data: function(params) { |
| 465 | return { |
| 466 | action: 'widgetopts_get_snippets_ajax', |
| 467 | search: params.term || '' |
| 468 | }; |
| 469 | }, |
| 470 | processResults: function(response) { |
| 471 | var results = [{id: '', text: '<?php echo esc_js(__("— No Logic (Always Show) —", "widget-options")); ?>'}]; |
| 472 | if (response.success && response.data && response.data.snippets) { |
| 473 | response.data.snippets.forEach(function(snippet) { |
| 474 | results.push({id: String(snippet.id), text: snippet.title, description: snippet.description || ''}); |
| 475 | }); |
| 476 | } |
| 477 | return { results: results }; |
| 478 | }, |
| 479 | cache: true |
| 480 | } |
| 481 | }; |
| 482 | |
| 483 | // Update snippet description text next to the select |
| 484 | function updateSoSnippetDesc($select, description) { |
| 485 | var $desc = $select.closest('.widget-opts-logic').find('.widgetopts-snippet-desc'); |
| 486 | if ($desc.length) { |
| 487 | if (description) { |
| 488 | $desc.text(description).show(); |
| 489 | } else { |
| 490 | $desc.text('').hide(); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // Init Select2 AJAX on a single select element |
| 496 | function initSoSelect2Ajax($select) { |
| 497 | if ($select.hasClass('select2-hidden-accessible')) { |
| 498 | $select.select2('destroy'); |
| 499 | } |
| 500 | var config = $.extend({}, soSnippetConfig, { |
| 501 | dropdownParent: $select.closest('.so-content, .so-visual-styles, .widget-content, form') |
| 502 | }); |
| 503 | $select.select2(config); |
| 504 | $select.off('select2:select.woDesc select2:unselect.woDesc'); |
| 505 | $select.on('select2:select.woDesc', function(e) { |
| 506 | updateSoSnippetDesc($select, e.params.data.description || ''); |
| 507 | }); |
| 508 | $select.on('select2:unselect.woDesc', function() { |
| 509 | updateSoSnippetDesc($select, ''); |
| 510 | }); |
| 511 | } |
| 512 | |
| 513 | // Initialize Select2 for snippet dropdowns in SiteOrigin |
| 514 | function initSiteOriginSnippetSelect2() { |
| 515 | $('.widgetopts-select2-snippets:not(.select2-hidden-accessible)').each(function() { |
| 516 | initSoSelect2Ajax($(this)); |
| 517 | }); |
| 518 | } |
| 519 | |
| 520 | // SiteOrigin events |
| 521 | $(document).on('panelsopen', function() { |
| 522 | setTimeout(initSiteOriginSnippetSelect2, 300); |
| 523 | }); |
| 524 | |
| 525 | // Widget dialog opened |
| 526 | $(document).on('dialogopen', '.so-panels-dialog', function() { |
| 527 | setTimeout(initSiteOriginSnippetSelect2, 300); |
| 528 | }); |
| 529 | |
| 530 | // Form loaded via AJAX |
| 531 | $(document).ajaxComplete(function(event, xhr, settings) { |
| 532 | if (settings.url && settings.url.indexOf('so_panels') !== -1) { |
| 533 | setTimeout(initSiteOriginSnippetSelect2, 300); |
| 534 | } |
| 535 | }); |
| 536 | |
| 537 | // Click on widget options tabs |
| 538 | $(document).on('click', '.extended-widget-opts-tabs a, .extended-widget-opts-settings-tabnav-ul a', function() { |
| 539 | setTimeout(initSiteOriginSnippetSelect2, 100); |
| 540 | }); |
| 541 | })(jQuery); |
| 542 | </script> |
| 543 | <?php } |
| 544 | add_action('admin_footer', 'widgetopts_siteorigin_footer_script', 999); |
| 545 | } |
| 546 | ?> |