/var/www/hkosl.com/aga/wp-content/plugins/siteorigin-panels/inc/styles-admin.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php

class SiteOrigin_Panels_Styles_Admin {

    function 
__construct() {
        
add_action'wp_ajax_so_panels_style_form', array( $this'action_style_form' ) );

        
add_filter'siteorigin_panels_data', array( $this'convert_data' ) );
        
add_filter'siteorigin_panels_prebuilt_layout', array( $this'convert_data' ) );
    }

    public static function 
single() {
        static 
$single;
        return empty( 
$single ) ? $single = new self() : $single;
    }

    
/**
     * Admin action for handling fetching the style fields
     */
    
function action_style_form() {
        if ( empty( 
$_REQUEST['_panelsnonce'] ) || ! wp_verify_nonce$_REQUEST['_panelsnonce'], 'panels_action' ) ) {
            
wp_die(
                
__'The supplied nonce is invalid.''siteorigin-panels' ),
                
__'Invalid nonce.''siteorigin-panels' ),
                
403
            
);
        }
        
        
$type $_REQUEST['type'];
        
        if ( ! 
in_array$type, array( 'row''cell''widget' ) ) ) {
            
wp_die(
                
__'Please specify the type of style form to be rendered.''siteorigin-panels' ),
                
__'Missing style form type.''siteorigin-panels' ),
                
400
            
);
        }

        
$current = isset( $_REQUEST['style'] ) ? $_REQUEST['style'] : array();
        
$post_id = empty( $_REQUEST['postId'] ) ? $_REQUEST['postId'];

        
$args = ! empty( $_POST['args'] ) ? json_decodestripslashes$_POST['args'] ), true ) : array();

        switch ( 
$type ) {
            case 
'row':
                
$this->render_styles_fields'row''<h3>' __'Row Styles''siteorigin-panels' ) . '</h3>'''$current$post_id$args );
                break;

            case 
'cell':
                
$cell_number = isset( $args['index'] ) ? ' ' . ( intval$args['index'] ) + ) : '';
                
$this->render_styles_fields'cell''<h3>' sprintf__'Cell%s Styles''siteorigin-panels' ), $cell_number ) . '</h3>'''$current$post_id$args );
                break;

            case 
'widget':
                
$this->render_styles_fields'widget''<h3>' __'Widget Styles''siteorigin-panels' ) . '</h3>'''$current$post_id$args );
                break;
        }

        
wp_die();
    }

    
/**
     * Render all the style fields
     *
     * @param $section
     * @param string $before
     * @param string $after
     * @param array $current
     * @param int $post_id
     * @param array $args Arguments passed by the builder
     *
     * @return bool
     */
    
function render_styles_fields$section$before ''$after ''$current = array(), $post_id 0$args = array() ) {
        
$fields = array();
        
$fields apply_filters'siteorigin_panels_' $section '_style_fields'$fields$post_id$args );
        
$fields apply_filters'siteorigin_panels_general_style_fields'$fields$post_id$args );
        if ( empty( 
$fields ) ) {
            return 
false;
        }

        
$groups = array(
            
'attributes' => array(
                
'name'     => __'Attributes''siteorigin-panels' ),
                
'priority' => 5
            
),
            
'layout'     => array(
                
'name'     => __'Layout''siteorigin-panels' ),
                
'priority' => 10
            
),
            
'design'     => array(
                
'name'     => __'Design''siteorigin-panels' ),
                
'priority' => 15
            
),
        );

        
// Check if we need a default group
        
foreach ( $fields as $field_id => $field ) {
            if ( empty( 
$field['group'] ) || $field['group'] == 'theme' ) {
                if ( empty( 
$groups['theme'] ) ) {
                    
$groups['theme'] = array(
                        
'name'     => __'Theme''siteorigin-panels' ),
                        
'priority' => 10
                    
);
                }
                
$fields$field_id ]['group'] = 'theme';
            }
        }
        
$groups apply_filters'siteorigin_panels_' $section '_style_groups'$groups$post_id$args );
        
$groups apply_filters'siteorigin_panels_general_style_groups'$groups$post_id$args );

        
// Sort the style fields and groups by priority
        
uasort$fields, array( $this'sort_fields' ) );
        
uasort$groups, array( $this'sort_fields' ) );

        echo 
$before;

        
$group_counts = array();
        foreach ( 
$fields as $field_id => $field ) {
            if ( empty( 
$group_counts$field['group'] ] ) ) {
                
$group_counts$field['group'] ] = 0;
            }
            
$group_counts$field['group'] ] ++;
        }

        foreach ( 
$groups as $group_id => $group ) {

            if ( empty( 
$group_counts$group_id ] ) ) {
                continue;
            }

            
?>
            <div class="style-section-wrapper">
                <div class="style-section-head">
                    <h4><?php echo esc_html$group['name'] ) ?></h4>
                </div>
                <div class="style-section-fields" style="display: none">
                    <?php
                    
foreach ( $fields as $field_id => $field ) {
                        
$default = isset( $field'default' ] ) ? $field'default' ] : false;

                        if ( 
$field['group'] == $group_id ) {
                            
?>
                            <div class="style-field-wrapper">
                                <?php if ( ! empty( $field['name'] ) ) : ?>
                                    <label><?php echo $field['name'?></label>
                                <?php endif; ?>
                                <div
                                    class="style-field style-field-<?php echo sanitize_html_class$field['type'] ) ?>">
                                    <?php $this->render_style_field$field, isset( $current$field_id ] ) ? $current$field_id ] : $default$field_id$current ?>
                                </div>
                            </div>
                            <?php

                        
}

                    }
                    
?>
                </div>
            </div>
            <?php
        
}

        echo 
$after;
    }

    
/**
     * Generate the style field
     *
     * @param array $field Everything needed to display the field
     * @param $current
     * @param $field_id
     * @param $current_styles
     */
    
function render_style_field$field$current$field_id$current_styles ) {
        
$field_name 'style[' $field_id ']';

        echo 
'<div class="style-input-wrapper">';
        switch ( 
$field['type'] ) {
            case 
'measurement' :

                if ( ! empty( 
$field['multiple'] ) ) {
                    
?>
                    <div class="measurement-inputs">
                        <div class="measurement-wrapper">
                            <input type="text" class="measurement-value measurement-top"
                                   placeholder="<?php _e'Top''siteorigin-panels' ?>"/>
                        </div>
                        <div class="measurement-wrapper">
                            <input type="text" class="measurement-value measurement-right"
                                   placeholder="<?php _e'Right''siteorigin-panels' ?>"/>
                        </div>
                        <div class="measurement-wrapper">
                            <input type="text" class="measurement-value measurement-bottom"
                                   placeholder="<?php _e'Bottom''siteorigin-panels' ?>"/>
                        </div>
                        <div class="measurement-wrapper">
                            <input type="text" class="measurement-value measurement-left"
                                   placeholder="<?php _e'Left''siteorigin-panels' ?>"/>
                        </div>
                    </div>
                    <?php
                
} else {
                    
?><input type="text" class="measurement-value measurement-value-single"/><?php
                
}

                
?>
                <select
                    class="measurement-unit measurement-unit-<?php echo ! empty( $field['multiple'] ) ? 'multiple' 'single' ?>">
                    <?php foreach ( $this->measurements_list() as $measurement ): ?>
                        <option
                            value="<?php echo esc_html$measurement ?>"><?php echo esc_html$measurement ?></option>
                    <?php endforeach ?>
                </select>
                <input type="hidden" name="<?php echo esc_attr$field_name ?>"
                       value="<?php echo esc_attr$current ?>"/>
                <?php
                
break;

            case 
'color' :
                
?>
                <input type="text" name="<?php echo esc_attr$field_name ?>"
                       value="<?php echo esc_attr$current ?>" class="so-wp-color-field"/>
                <?php
                
break;

            case 
'image' :
                
$image false;
                if ( ! empty( 
$current ) ) {
                    
$image SiteOrigin_Panels_Styles::get_attachment_image_src$current'thumbnail' );
                }
                
                
$fallback_url = ( ! empty( $current_styles$field_id '_fallback' ] ) &&  $current_styles$field_id '_fallback' ] !== 'false' $current_styles$field_id '_fallback' ] : '' );
                
$fallback_field_name 'style[' $field_id '_fallback]';

                
?>
                <div class="so-image-selector">
                    <div class="current-image" <?php if ( ! empty( $image ) ) {
                        echo 
'style="background-image: url(' esc_url$image[0] ) . ');"';
                    } 
?>>
                    </div>

                    <div class="select-image">
                        <?php _e'Select Image''siteorigin-panels' ?>
                    </div>
                    <input type="hidden" name="<?php echo esc_attr$field_name ?>"
                           value="<?php echo intval$current ?>"/>
                </div>
                <a href="#" class="remove-image<?php if ( empty( $current ) ) echo ' hidden' ?>"><?php _e'Remove''siteorigin-panels' ?></a>
                
                <input type="text" value="<?php echo esc_url$fallback_url ?>"
                       placeholder="<?php esc_attr_e'External URL''siteorigin-panels' ?>"
                       name="<?php echo esc_attr$fallback_field_name ?>"
                       class="image-fallback widefat" />
                <?php
                
break;

            case 
'url' :
            case 
'text' :
                
?><input type="text" name="<?php echo esc_attr$field_name ?>"
                         value="<?php echo esc_attr$current ?>" class="widefat" /><?php
                
break;

            case 
'checkbox' :
                
$current = (bool) $current;
                
?>
                <label class="so-checkbox-label">
                    <input type="checkbox" name="<?php echo esc_attr$field_name ?><?php checked$current ?> />
                    <?php echo esc_html( isset( $field['label'] ) ? $field['label'] : __'Enabled''siteorigin-panels' ) ) ?>
                </label>
                <?php
                
break;

            case 
'select' :
                
?>
                <select name="<?php echo esc_attr$field_name ?>">
                    <?php foreach ( $field['options'] as $k => $v ) : ?>
                        <option
                            value="<?php echo esc_attr$k ?><?php selected$current$k ?>><?php echo esc_html$v ?></option>
                    <?php endforeach; ?>
                </select>
                <?php
                
break;

            case 
'radio' :
                
$radio_id $field_name '-' uniqid();
                foreach ( 
$field['options'] as $k => $v ) :
                    
?>
                    <label for="<?php echo esc_attr$radio_id '-' $k ?>">
                        <input type="radio" name="<?php echo esc_attr$radio_id ?>"
                           id="<?php echo esc_attr$radio_id '-' $k ?>"
                           value="<?php echo esc_attr$k ?><?php checked$k$current ?><?php echo esc_html$v ?>
                    </label>
                    <?php
                
endforeach;
                break;

            case 
'textarea' :
            case 
'code' :
                
?><textarea type="text" name="<?php echo esc_attr$field_name ?>"
                            class="widefat <?php if ( $field['type'] == 'code' ) {
                                echo 
'so-field-code';
                            } 
?>" rows="4"><?php echo esc_textarea$current ?></textarea><?php
                
break;
        }

        echo 
'</div>';

        if ( ! empty( 
$field['description'] ) ) {
            
?><p class="so-description"><?php echo wp_kses_post$field['description'] ) ?></p><?php
        
}
    }

    
/**
     * Sanitize the style fields in panels_data
     *
     * @param $panels_data
     *
     * @return mixed
     */
    
function sanitize_all$panels_data ) {
        if ( ! empty( 
$panels_data['widgets'] ) ) {
            
// Sanitize the widgets
            
for ( $i 0$i count$panels_data['widgets'] ); $i ++ ) {
                if ( empty( 
$panels_data['widgets'][ $i ]['panels_info']['style'] ) ) {
                    continue;
                }
                
$panels_data['widgets'][ $i ]['panels_info']['style'] = $this->sanitize_style_fields'widget'$panels_data['widgets'][ $i ]['panels_info']['style'] );
            }
        }

        if ( ! empty( 
$panels_data['grids'] ) ) {
            
// The rows
            
for ( $i 0$i count$panels_data['grids'] ); $i ++ ) {
                if ( empty( 
$panels_data['grids'][ $i ]['style'] ) ) {
                    continue;
                }
                
$panels_data['grids'][ $i ]['style'] = $this->sanitize_style_fields'row'$panels_data['grids'][ $i ]['style'] );
            }
        }

        if ( ! empty( 
$panels_data['grid_cells'] ) ) {
            
// And finally, the cells
            
for ( $i 0$i count$panels_data['grid_cells'] ); $i ++ ) {
                if ( empty( 
$panels_data['grid_cells'][ $i ]['style'] ) ) {
                    continue;
                }
                
$panels_data['grid_cells'][ $i ]['style'] = $this->sanitize_style_fields'cell'$panels_data['grid_cells'][ $i ]['style'] );
            }
        }

        return 
$panels_data;
    }

    
/**
     * Sanitize style fields.
     *
     * @param $section
     * @param $styles
     *
     * @return array Sanitized styles
     */
    
function sanitize_style_fields$section$styles ) {
        
// Use the filter to get the fields for this section.
        
if ( empty( $fields_cache$section ] ) ) {
            
// This filter doesn't pass in the arguments $post_id and $args
            // Plugins looking to extend fields, should always add their fields if these are empty
            
$fields_cache$section ] = array();
            
$fields_cache$section ] = apply_filters'siteorigin_panels_' $section '_style_fields'$fields_cache$section ], falsefalse );
            
$fields_cache$section ] = apply_filters'siteorigin_panels_general_style_fields'$fields_cache$section ], falsefalse );
        }
        
$fields $fields_cache$section ];
        
        if ( empty( 
$fields ) ) {
            return array();
        }

        
$return = array();
        foreach ( 
$fields as $k => $field ) {
            
// Skip this if no field type is set
            
if ( empty( $field['type'] ) ) {
                continue;
            }

            
// Handle the special case of a checkbox
            
if ( $field['type'] == 'checkbox' ) {
                
$return$k ] = ! empty( $styles$k ] ) ? true '';
                continue;
            }

            
// Ignore this if we don't even have a value for the style, unless 'image' field which might have a fallback.
            
if ( ! isset( $styles$k ] ) || ( $field['type'] != 'image' && $styles$k ] == '' ) ) {
                continue;
            }

            switch ( 
$field['type'] ) {
                case 
'color' :
                    
$color $styles$k ];
                    if ( 
preg_match'|^#([A-Fa-f0-9]{3,8})$|'$color ) ) {
                        
$return$k ] = $color;
                    } else {
                        
$return$k ] = '';
                    }
                    break;
                case 
'image' :
                    
$return$k ] = ! empty( $styles$k ] ) ? sanitize_text_field$styles$k ] ) : false;
                    
$fallback_name $k '_fallback';
                    if ( empty( 
$styles$k ] ) && empty( $styles$fallback_name ] ) ) {
                        break;
                    }
                    
$return$fallback_name ] = ! empty( $styles$fallback_name ] ) ? esc_url_raw$styles$fallback_name ] ) : false;
                    break;
                case 
'url' :
                    
$return$k ] = esc_url_raw$styles$k ] );
                    break;
                case 
'measurement' :
                    
$measurements array_map'preg_quote'$this->measurements_list() );
                    if ( ! empty( 
$field['multiple'] ) ) {
                        if ( 
preg_match_all'/(?:(-?[0-9\.,]+).*?(' implode'|'$measurements ) . ')+)/'$styles$k ], $match ) ) {
                            
$return$k ] = $styles$k ];
                        } else {
                            
$return$k ] = '';
                        }
                    } else {
                        if ( 
preg_match'/([-?0-9\.,]+).*?(' implode'|'$measurements ) . ')/'$styles$k ], $match ) ) {
                            
$return$k ] = $match[1] . $match[2];
                        } else {
                            
$return$k ] = '';
                        }
                    }
                    break;
                case 
'select' :
                case 
'radio' :
                    if ( ! empty( 
$styles$k ] ) && in_array$styles$k ], array_keys$field['options'] ) ) ) {
                        
$return$k ] = $styles$k ];
                    }
                    break;
                default:
                    
// Just pass the value through.
                    
$return$k ] = $styles$k ];
                    break;

            }
        }

        return 
$return;
    }

    
/**
     * Convert the single string attribute of the grid style into an array.
     *
     * @param $panels_data
     *
     * @return mixed
     */
    
function convert_data$panels_data ) {
        if ( empty( 
$panels_data ) || empty( $panels_data['grids'] ) || ! is_array$panels_data['grids'] ) ) {
            return 
$panels_data;
        }

        foreach( 
$panels_data['grids'] as & $grid ) {
            if ( ! 
is_array$grid ) || empty( $grid ) || empty( $grid['style'] ) ) {
                continue;
            }

            if ( 
is_string$grid['style'] ) ) {
                
$grid['style'] = array(
                    
$grid['style']
                );
            }
        }

        return 
$panels_data;
    }

    
/**
     * Get list of supported mesurements
     *
     * @return array
     */
    
function measurements_list() {
        
$measurements = array(
            
'px',
            
'%',
            
'in',
            
'cm',
            
'mm',
            
'em',
            
'ex',
            
'pt',
            
'pc',
            
'rem'
        
);

        
// Allow themes and plugins to trim or enhance the list.
        
return apply_filters'siteorigin_panels_style_get_measurements_list'$measurements );
    }

    
/**
     * User sort function to sort by the priority key value.
     *
     * @param $a
     * @param $b
     *
     * @return int
     */
    
static function sort_fields$a$b ) {
        return ( ( isset( 
$a['priority'] ) ? $a['priority'] : 10 ) > ( isset( $b['priority'] ) ? $b['priority'] : 10 ) ) ? : - 1;
    }
}

// Initialise all the default styling
SiteOrigin_Panels_Styles::single();