← All changes
|
includes/controls/select-input/dynamic-select.php
+42
-10
4.1.17
→
4.5.7
View file →
| @@ -104,18 +104,50 @@ | ||
| 104 | 104 | * Used to register and enqueue custom scripts and styles used by the control. |
| 105 | 105 | * @since 5.8.0 |
| 106 | 106 | */ |
| 107 | 107 | public function enqueue() { |
| 108 | - wp_enqueue_script('upk-dynamic-select', BDTUPK_URL . 'includes/controls/assets/js/upk-dynamic-select.min.js', array('jquery'), BDTUPK_VER); | |
| 109 | - wp_localize_script( | |
| 110 | - 'upk-dynamic-select', | |
| 111 | - 'upk_dynamic_select', | |
| 112 | - [ | |
| 113 | - 'nonce' => wp_create_nonce('upk_dynamic_select'), | |
| 114 | - 'action' => 'upk_dynamic_select_input_data', | |
| 115 | - 'ajax_url' => admin_url('admin-ajax.php') | |
| 116 | - ] | |
| 117 | - ); | |
| 108 | + static $done = false; | |
| 109 | + | |
| 110 | + if ($done) { | |
| 111 | + return; | |
| 112 | + } | |
| 113 | + $done = true; | |
| 114 | + | |
| 115 | + $settings = [ | |
| 116 | + 'nonce' => wp_create_nonce('upk_dynamic_select'), | |
| 117 | + 'action' => 'upk_dynamic_select_input_data', | |
| 118 | + 'ajax_url' => admin_url('admin-ajax.php') | |
| 119 | + ]; | |
| 120 | + | |
| 121 | + /** | |
| 122 | + * Ship the control view inline, right after Elementor's own editor | |
| 123 | + * script, instead of as a separate file. | |
| 124 | + * | |
| 125 | + * As a separate file it is printed after the editor loader (which | |
| 126 | + * calls elementor.start()), and on some sites the tag never runs at | |
| 127 | + * all: optimizers, CDNs or a stray crossorigin attribute can block | |
| 128 | + * or rewrite the request. The control then falls back to a plain | |
| 129 | + * <select> with no Select2. Inline, there is no request to block, | |
| 130 | + * and the view is registered before the editor starts. | |
| 131 | + */ | |
| 132 | + $script_path = BDTUPK_PATH . 'includes/controls/assets/js/upk-dynamic-select.min.js'; | |
| 133 | + | |
| 134 | + if (wp_script_is('elementor-editor', 'registered') && is_readable($script_path)) { | |
| 135 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- reading a bundled local plugin file, not a remote URL. | |
| 136 | + $script = file_get_contents($script_path); | |
| 137 | + | |
| 138 | + if ($script) { | |
| 139 | + wp_add_inline_script( | |
| 140 | + 'elementor-editor', | |
| 141 | + 'var upk_dynamic_select = ' . wp_json_encode($settings) . ';' . PHP_EOL . $script, | |
| 142 | + 'after' | |
| 143 | + ); | |
| 144 | + return; | |
| 145 | + } | |
| 146 | + } | |
| 147 | + | |
| 148 | + wp_enqueue_script('upk-dynamic-select', BDTUPK_URL . 'includes/controls/assets/js/upk-dynamic-select.min.js', array('jquery'), BDTUPK_VER, true); | |
| 149 | + wp_localize_script('upk-dynamic-select', 'upk_dynamic_select', $settings); | |
| 118 | 150 | } |
| 119 | 151 | } |
| 120 | 152 | |
| 121 | 153 | add_action('elementor/controls/register', function () { |