PluginProbe ʕ •ᴥ•ʔ
Widget Options – Advanced Conditional Visibility for Gutenberg Blocks & Classic Widgets / trunk
Widget Options – Advanced Conditional Visibility for Gutenberg Blocks & Classic Widgets vtrunk
4.2.5 4.2.4 trunk 3.7.10 3.7.11 3.7.12 3.7.13 3.7.14 3.7.2 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8 3.8.1 3.8.10 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.8.8 3.8.9 3.8.9.1 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.5.1 4.0.6 4.0.6.1 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.1.3 4.2.0 4.2.1 4.2.2 4.2.3
widget-options / includes / widgets / widgets.php
widget-options / includes / widgets Last commit date
gutenberg 1 month ago option-tabs 2 months ago display.php 1 month ago extras.php 8 months ago widgets.php 3 months ago
widgets.php
281 lines
1 <?php
2
3 /**
4 * Add Widget Options
5 *
6 * Process Managing of Widget Options.
7 *
8 * @copyright Copyright (c) 2016, Jeffrey Carandang
9 * @since 2.0
10 */
11 // Exit if accessed directly
12 if (!defined('ABSPATH')) exit;
13
14
15
16 /**
17 * Output widget search filter textfield before widget lists
18 *
19 * @since 3.3
20 */
21 if (!function_exists('widgetopts_add_search_input')) :
22 function widgetopts_add_search_input()
23 {
24 global $widget_options;
25 if (isset($widget_options['search']) && 'activate' == $widget_options['search']) : ?>
26 <div id="widgetopts-widgets-filter">
27 <label class="screen-reader-text" for="widgetopts-widgets-search"><?php _e('Search Widgets', 'widget-options'); ?></label>
28 <input type="text" id="widgetopts-widgets-search" class="widgetopts-widgets-search" placeholder="<?php esc_attr_e('Search widgets&hellip;', 'widget-options') ?>" />
29 <div class="widgetopts-search-icon" aria-hidden="true"></div>
30 <button type="button" class="widgetopts-clear-results"><span class="screen-reader-text"><?php _e('Clear Results', 'widget-options'); ?></span></button>
31 <p class="screen-reader-text" id="widgetopts-search-desc"><?php _e('The search results will be updated as you type.', 'widget-options'); ?></p>
32 </div>
33 <?php
34 endif;
35 }
36 add_action('widgets_admin_page', 'widgetopts_add_search_input');
37 endif;
38
39 /**
40 * Add Options on in_widget_form action
41 *
42 * @since 2.0
43 * @return void
44 */
45
46 function widgetopts_in_widget_form($widget, $return, $instance)
47 {
48 global $widget_options, $wp_registered_widget_controls;
49 $width = (isset($wp_registered_widget_controls[$widget->id]['width'])) ? (int) $wp_registered_widget_controls[$widget->id]['width'] : 250;
50 $opts = (isset($instance['extended_widget_opts-' . $widget->id])) ? $instance['extended_widget_opts-' . $widget->id] : array();
51 $is_siteorigin = (isset($widget_options['siteorigin'])) ? $widget_options['siteorigin'] : '';
52
53 /* if $opts is empty, try to get data from blocks */
54 if (!wp_use_widgets_block_editor()) {
55 if (empty($instance['extended_widget_opts-' . $widget->id])) {
56 if (isset($instance['content']) && !empty($instance['content'])) {
57 $block = parse_blocks($instance['content']);
58 if (!empty($block[0]) && !empty($block[0]['attrs'])) {
59 if (!empty($block[0]['attrs']['extended_widget_opts'])) {
60 $opts = $block[0]['attrs']['extended_widget_opts'];
61 }
62 }
63 }
64 }
65 }
66
67 /** change widget names for SO Pagebuilder support **/
68 if (isset($widget->id) && 'temp' == $widget->id) {
69 $namespace = 'widgets[' . $widget->number . ']';
70 $optsname = 'widgets[' . $widget->number . '][extended_widget_opts_name]';
71 $opts = (isset($instance['extended_widget_opts'])) ? $instance['extended_widget_opts'] : array();
72 $widget->id = $widget->number;
73
74 //create siteorigin pagebuilder variable
75 echo '<input type="hidden" name="' . $namespace . '[siteorigin]" value="1" />';
76 } else {
77 $namespace = 'extended_widget_opts-' . $widget->id;
78 $optsname = 'extended_widget_opts_name';
79 }
80
81 $args = array(
82 'width' => $width,
83 'id' => $widget->id,
84 'params' => $opts,
85 'namespace' => $namespace
86 );
87 $selected = 0;
88 if (isset($opts['tabselect'])) {
89 $selected = $opts['tabselect'];
90 }
91
92 ?>
93
94 <input type="hidden" name="extended_widget_opts_name" value="extended_widget_opts-<?php echo $widget->id; ?>">
95 <input type="hidden" name="<?php echo $args['namespace']; ?>[extended_widget_opts][id_base]" value="<?php echo $widget->id; ?>" />
96 <div class="extended-widget-opts-form <?php if ($width > 480) {
97 echo 'extended-widget-opts-form-large';
98 } else if ($width <= 480) {
99 echo 'extended-widget-opts-form-small';
100 } ?>">
101 <div class="extended-widget-opts-tabs">
102 <ul class="extended-widget-opts-tabnav-ul">
103 <?php do_action('extended_widget_opts_tabs', $args); ?>
104 <div class="extended-widget-opts-clearfix"></div>
105 </ul>
106
107 <?php do_action('extended_widget_opts_tabcontent', $args); ?>
108 <input type="hidden" id="extended-widget-opts-selectedtab" value="<?php echo $selected; ?>" name="extended_widget_opts-<?php echo $args['id']; ?>[extended_widget_opts][tabselect]" />
109 <div class="extended-widget-opts-clearfix"></div>
110 </div><!-- end .extended-widget-opts-tabs -->
111 </div><!-- end .extended-widget-opts-form -->
112
113 <?php if ('activate' == $is_siteorigin) { ?>
114 <script type="text/javascript">
115 jQuery(document).ready(function($) {
116 if ($('.so-content .extended-widget-opts-tabs').length > 0) {
117 $('.extended-widget-opts-tabs').tabs({
118 active: 1
119 });
120 $('.extended-widget-opts-visibility-tabs').tabs({
121 active: 0
122 });
123 $('.extended-widget-opts-settings-tabs').tabs({
124 active: 0
125 });
126 }
127 });
128 </script>
129 <?php } else { ?>
130 <style type="text/css">
131 .so-content.panel-dialog .extended-widget-opts-form {
132 display: none;
133 }
134 </style>
135 <?php } ?>
136
137 <?php
138 }
139 add_action('in_widget_form', 'widgetopts_in_widget_form', 10, 3);
140
141 /*
142 * Update Options
143 */
144 function widgetopts_ajax_update_callback($instance, $new_instance, $old_instance, $this_widget)
145 {
146 global $widget_options;
147
148 if (
149 isset($_POST['extended_widget_opts_name']) ||
150 (!isset($_POST['extended_widget_opts_name']) && isset($new_instance['siteorigin']))
151 ) {
152 //check if from SO pagebuilder
153 if (is_array($new_instance) && isset($new_instance['extended_widget_opts']) && isset($new_instance['siteorigin'])) {
154 $name = 'extended_widget_opts';
155 $options = widgetopts_sanitize_array($new_instance);
156 } else {
157 $name = strip_tags($_POST['extended_widget_opts_name']);
158 $options = $_POST[$name];
159 }
160 if (isset($options['extended_widget_opts'])) {
161 // Remove legacy logic field - only snippet IDs are allowed now (security)
162 if (isset($options['extended_widget_opts']['class']['logic'])) {
163 unset($options['extended_widget_opts']['class']['logic']);
164 }
165
166 if (isset($options['extended_widget_opts']['class']['link']) && !empty($options['extended_widget_opts']['class']['link'])) {
167 $options['extended_widget_opts']['class']['link'] = widgetopts_addhttp($options['extended_widget_opts']['class']['link']);
168 }
169 $instance[$name] = widgetopts_sanitize_array($options['extended_widget_opts']);
170
171 //remove cache
172 if (isset($options['extended_widget_opts']['id_base']) && isset($widget_options['cache']) && 'activate' == $widget_options['cache']) {
173 $transient_name = 'widgetopts-cache_' . $options['extended_widget_opts']['id_base'];
174 delete_transient($transient_name);
175 }
176
177 //remove widgetopts attribute from blocks when it is classic editor
178 if (!empty($instance['content'])) {
179 $block = parse_blocks($instance['content']);
180 if (!empty($block[0]) && !empty($block[0]['attrs'])) {
181 if (!empty($block[0]['attrs']['extended_widget_opts'])) {
182 unset($block[0]['attrs']['extended_widget_opts']);
183 $instance['content'] = serialize_blocks($block);
184 }
185 }
186 }
187 }
188 }
189 return $instance;
190 }
191 add_filter('widget_update_callback', 'widgetopts_ajax_update_callback', 10, 4);
192
193 add_filter('widget_form_callback', function ($instance, $widget) {
194 /* if $opts is empty, try to get data from blocks */
195 if (!wp_use_widgets_block_editor()) {
196 if (empty($instance['extended_widget_opts-' . $widget->id])) {
197 if (isset($instance['content']) && !empty($instance['content'])) {
198 $block = parse_blocks($instance['content']);
199 if (!empty($block[0]) && !empty($block[0]['attrs'])) {
200 if (!empty($block[0]['attrs']['extended_widget_opts'])) {
201 $instance['extended_widget_opts-' . $widget->id] = $block[0]['attrs']['extended_widget_opts'];
202 }
203 }
204 }
205 }
206
207 //remove widgetopts attribute from blocks when it is classic editor
208 if (!empty($instance['content'])) {
209 $blocks = parse_blocks($instance['content']);
210 if (is_array($blocks) && is_iterable($blocks)) {
211
212 foreach ($blocks as &$block) {
213 if (!empty($block) && !empty($block['attrs'])) {
214 $is_there_a_changes = false;
215 $_block = widgetopts_unset_block_attributes($block);
216 if ($_block !== false) {
217 $block = $_block;
218 $is_there_a_changes = true;
219 }
220
221 //inner blocks
222 if (isset($block['innerBlocks']) && is_array($block['innerBlocks']) && !empty($block['innerBlocks'])) {
223
224 foreach ($block['innerBlocks'] as &$block2) {
225 $_block2 = widgetopts_unset_block_attributes($block2);
226 if ($_block2 !== false) {
227 $block2 = $_block2;
228
229 $is_there_a_changes = true;
230 }
231
232 //2nd level inner blocks
233 if (isset($block2['innerBlocks']) && is_array($block2['innerBlocks']) && !empty($block2['innerBlocks'])) {
234
235 foreach ($block2['innerBlocks'] as &$block3) {
236 $_block3 = widgetopts_unset_block_attributes($block3);
237 if ($_block3 !== false) {
238 $block3 = $_block3;
239 $is_there_a_changes = true;
240 }
241 }
242 }
243 }
244 }
245
246 if ($is_there_a_changes) {
247 $instance['content'] = serialize_blocks($blocks);
248 break;
249 }
250 }
251 }
252 }
253 }
254 }
255
256 return $instance;
257 }, 100, 2);
258
259 function widgetopts_unset_block_attributes($block)
260 {
261 $is_there_a_changes = false;
262 if (!empty($block) && !empty($block['attrs'])) {
263 if (!empty($block['attrs']['extended_widget_opts'])) {
264 unset($block['attrs']['extended_widget_opts']);
265 $is_there_a_changes = true;
266 }
267
268 if (!empty($block['attrs']['extended_widget_opts_state'])) {
269 unset($block['attrs']['extended_widget_opts_state']);
270 $is_there_a_changes = true;
271 }
272
273 if (!empty($block['attrs']['extended_widget_opts_clientid'])) {
274 unset($block['attrs']['extended_widget_opts_clientid']);
275 $is_there_a_changes = true;
276 }
277 }
278
279 return $is_there_a_changes ? $block : false;
280 }
281 ?>