forked from daudmabena/siteorigin-panels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiteorigin-panels.php
More file actions
1632 lines (1393 loc) · 58.7 KB
/
siteorigin-panels.php
File metadata and controls
1632 lines (1393 loc) · 58.7 KB
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Page Builder by SiteOrigin
Plugin URI: https://siteorigin.com/page-builder/
Description: A drag and drop, responsive page builder that simplifies building your website.
Version: dev
Author: SiteOrigin
Author URI: https://siteorigin.com
License: GPL3
License URI: http://www.gnu.org/licenses/gpl.html
Donate link: http://siteorigin.com/page-builder/#donate
*/
define('SITEORIGIN_PANELS_VERSION', 'dev');
if ( ! defined('SITEORIGIN_PANELS_JS_SUFFIX' ) ) {
define('SITEORIGIN_PANELS_JS_SUFFIX', '');
}
define('SITEORIGIN_PANELS_VERSION_SUFFIX', '');
define('SITEORIGIN_PANELS_BASE_FILE', __FILE__);
// All the basic settings
require_once plugin_dir_path(__FILE__) . 'settings/settings.php';
// Include all the basic widgets
require_once plugin_dir_path(__FILE__) . 'widgets/basic.php';
require_once plugin_dir_path(__FILE__) . 'widgets/migration.php';
require_once plugin_dir_path(__FILE__) . 'inc/css.php';
require_once plugin_dir_path(__FILE__) . 'inc/revisions.php';
require_once plugin_dir_path(__FILE__) . 'inc/styles.php';
require_once plugin_dir_path(__FILE__) . 'inc/default-styles.php';
require_once plugin_dir_path(__FILE__) . 'inc/widgets.php';
require_once plugin_dir_path(__FILE__) . 'inc/plugin-activation.php';
require_once plugin_dir_path(__FILE__) . 'inc/admin-actions.php';
if( defined('SITEORIGIN_PANELS_DEV') && SITEORIGIN_PANELS_DEV ) include plugin_dir_path(__FILE__).'inc/debug.php';
/**
* Hook for activation of Page Builder.
*/
function siteorigin_panels_activate(){
add_option('siteorigin_panels_initial_version', SITEORIGIN_PANELS_VERSION, '', 'no');
}
register_activation_hook(__FILE__, 'siteorigin_panels_activate');
/**
* Initialize the Page Builder.
*/
function siteorigin_panels_init(){
if(
siteorigin_panels_setting('bundled-widgets') &&
! defined('SITEORIGIN_PANELS_LEGACY_WIDGETS_ACTIVE') &&
( ! is_admin() || basename( $_SERVER["SCRIPT_FILENAME"] ) != 'plugins.php' )
) {
// Include the bundled widgets if the Legacy Widgets plugin isn't active.
include plugin_dir_path(__FILE__).'widgets/widgets.php';
}
if(
! is_admin() &&
siteorigin_panels_setting( 'sidebars-emulator' ) &&
( ! get_option('permalink_structure') || get_option('rewrite_rules') )
) {
// Include the sidebars emulator
require_once plugin_dir_path(__FILE__) . 'inc/sidebars-emulator.php';
}
}
add_action('plugins_loaded', 'siteorigin_panels_init');
/**
* Initialize the language files
*/
function siteorigin_panels_init_lang(){
load_plugin_textdomain('siteorigin-panels', false, dirname( plugin_basename( __FILE__ ) ). '/lang/');
}
add_action('plugins_loaded', 'siteorigin_panels_init_lang');
/**
* Add the admin menu entries
*/
function siteorigin_panels_admin_menu(){
if( !siteorigin_panels_setting( 'home-page' ) ) return;
add_theme_page(
__( 'Custom Home Page Builder', 'siteorigin-panels' ),
__( 'Home Page', 'siteorigin-panels' ),
'edit_theme_options',
'so_panels_home_page',
'siteorigin_panels_render_admin_home_page'
);
}
add_action('admin_menu', 'siteorigin_panels_admin_menu');
/**
* Render the page used to build the custom home page.
*/
function siteorigin_panels_render_admin_home_page(){
// We need a global post for some features in Page Builder (eg history)
global $post;
$home_page_id = get_option( 'page_on_front' );
if( empty($home_page_id) ) $home_page_id = get_option( 'siteorigin_panels_home_page_id' );
$home_page = get_post( $home_page_id );
if( !empty($home_page) && get_post_meta( $home_page->ID, 'panels_data', true ) != '' ) {
$post = $home_page;
}
$panels_data = siteorigin_panels_get_current_admin_panels_data();
include plugin_dir_path(__FILE__).'tpl/admin-home-page.php';
}
/**
* Callback to register the Page Builder Metaboxes
*/
function siteorigin_panels_metaboxes() {
foreach( siteorigin_panels_setting( 'post-types' ) as $type ){
add_meta_box(
'so-panels-panels',
__( 'Page Builder', 'siteorigin-panels' ),
'siteorigin_panels_metabox_render',
$type,
'advanced',
'high'
);
}
}
add_action( 'add_meta_boxes', 'siteorigin_panels_metaboxes' );
/**
* Save home page
*/
function siteorigin_panels_save_home_page(){
if( !isset($_POST['_sopanels_home_nonce'] ) || !wp_verify_nonce($_POST['_sopanels_home_nonce'], 'save') ) return;
if( !current_user_can('edit_theme_options') ) return;
if( !isset( $_POST['panels_data'] ) ) return;
// Check that the home page ID is set and the home page exists
$page_id = get_option( 'page_on_front' );
if( empty($page_id) ) $page_id = get_option( 'siteorigin_panels_home_page_id' );
$post_content = wp_unslash( $_POST['post_content'] );
if ( !$page_id || get_post_meta( $page_id, 'panels_data', true ) == '' ) {
// Lets create a new page
$page_id = wp_insert_post( array(
// TRANSLATORS: This is the default name given to a user's home page
'post_title' => __( 'Home Page', 'siteorigin-panels' ),
'post_status' => !empty($_POST['siteorigin_panels_home_enabled']) ? 'publish' : 'draft',
'post_type' => 'page',
'post_content' => $post_content,
'comment_status' => 'closed',
) );
update_option( 'page_on_front', $page_id );
update_option( 'siteorigin_panels_home_page_id', $page_id );
// Action triggered when creating a new home page through the custom home page interface
do_action( 'siteorigin_panels_create_home_page', $page_id );
} else {
// `wp_insert_post` does it's own sanitization, but it seems `wp_update_post` doesn't.
$post_content = sanitize_post_field( 'post_content', $post_content, $page_id, 'db' );
// Update the post with changed content to save revision if necessary.
wp_update_post( array( 'ID' => $page_id, 'post_content' => $post_content ) );
}
$page = get_post( $page_id );
// Save the updated page data
$panels_data = json_decode( wp_unslash( $_POST['panels_data'] ), true);
$panels_data['widgets'] = siteorigin_panels_process_raw_widgets($panels_data['widgets']);
$panels_data = siteorigin_panels_styles_sanitize_all( $panels_data );
$panels_data = apply_filters( 'siteorigin_panels_data_pre_save', $panels_data, $page, $page_id );
update_post_meta( $page_id, 'panels_data', $panels_data );
$template = get_post_meta( $page_id, '_wp_page_template', true );
$home_template = siteorigin_panels_setting( 'home-template' );
if( ( $template == '' || $template == 'default' ) && !empty($home_template) ) {
// Set the home page template
update_post_meta( $page_id, '_wp_page_template', $home_template );
}
if( !empty( $_POST['siteorigin_panels_home_enabled'] ) ) {
update_option('show_on_front', 'page');
update_option('page_on_front', $page_id);
update_option('siteorigin_panels_home_page_id', $page_id);
wp_publish_post($page_id);
}
else {
// We're disabling this home page
update_option( 'show_on_front', 'posts' );
// Change the post status to draft
$post = get_post($page_id);
if($post->post_status != 'draft') {
global $wpdb;
$wpdb->update( $wpdb->posts, array( 'post_status' => 'draft' ), array( 'ID' => $post->ID ) );
clean_post_cache( $post->ID );
$old_status = $post->post_status;
$post->post_status = 'draft';
wp_transition_post_status( 'draft', $old_status, $post );
do_action( 'edit_post', $post->ID, $post );
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
do_action( 'save_post', $post->ID, $post, true );
do_action( 'wp_insert_post', $post->ID, $post, true );
}
}
}
add_action('admin_init', 'siteorigin_panels_save_home_page');
/**
* After the theme is switched, change the template on the home page if the theme supports home page functionality.
*/
function siteorigin_panels_update_home_on_theme_change(){
$page_id = get_option( 'page_on_front' );
if( empty($page_id) ) $page_id = get_option( 'siteorigin_panels_home_page_id' );
if( siteorigin_panels_setting( 'home-page' ) && siteorigin_panels_setting( 'home-template' ) && $page_id && get_post_meta( $page_id, 'panels_data', true ) !== '' ) {
// Lets update the home page to use the home template that this theme supports
update_post_meta( $page_id, '_wp_page_template', siteorigin_panels_setting( 'home-template' ) );
}
}
add_action('after_switch_theme', 'siteorigin_panels_update_home_on_theme_change');
/**
* @return mixed|void Are we currently viewing the home page
*/
function siteorigin_panels_is_home(){
$home = ( is_front_page() && is_page() && get_option('show_on_front') == 'page' && get_option('page_on_front') == get_the_ID() && get_post_meta( get_the_ID(), 'panels_data' ) );
return apply_filters('siteorigin_panels_is_home', $home);
}
/**
* Check if we're currently viewing a page builder page.
*
* @param bool $can_edit Also check if the user can edit this page
* @return bool
*/
function siteorigin_panels_is_panel($can_edit = false){
// Check if this is a panel
$is_panel = ( siteorigin_panels_is_home() || ( is_singular() && get_post_meta(get_the_ID(), 'panels_data', false) ) );
return $is_panel && (!$can_edit || ( (is_singular() && current_user_can('edit_post', get_the_ID())) || ( siteorigin_panels_is_home() && current_user_can('edit_theme_options') ) ));
}
/**
* Render a panel metabox.
*
* @param $post
*/
function siteorigin_panels_metabox_render( $post ) {
$panels_data = siteorigin_panels_get_current_admin_panels_data();
include plugin_dir_path(__FILE__) . 'tpl/metabox-panels.php';
}
/**
* Check if we should load the SiteOrigin scripts and styles
*
* @return mixed|void
*/
function siteorigin_panels_is_admin_page(){
$screen = get_current_screen();
$is_panels_page = ( $screen->base == 'post' && in_array( $screen->id, siteorigin_panels_setting('post-types') ) ) || $screen->base == 'appearance_page_so_panels_home_page' || $screen->base == 'widgets' || $screen->base == 'customize';
return apply_filters('siteorigin_panels_is_admin_page', $is_panels_page );
}
/**
* Enqueue the panels admin scripts
*
* @param string $prefix
* @param bool $force Should we force the enqueues
*
* @action admin_print_scripts-post-new.php
* @action admin_print_scripts-post.php
* @action admin_print_scripts-appearance_page_so_panels_home_page
*/
function siteorigin_panels_admin_enqueue_scripts( $prefix = '', $force = false ) {
$screen = get_current_screen();
if ( $force || siteorigin_panels_is_admin_page() ) {
// Media is required for row styles
wp_enqueue_media();
wp_enqueue_script( 'so-panels-admin', plugin_dir_url(__FILE__) . 'js/siteorigin-panels' . SITEORIGIN_PANELS_VERSION_SUFFIX . SITEORIGIN_PANELS_JS_SUFFIX . '.js', array( 'jquery', 'jquery-ui-resizable', 'jquery-ui-sortable', 'jquery-ui-draggable', 'underscore', 'backbone', 'plupload', 'plupload-all' ), SITEORIGIN_PANELS_VERSION, true );
add_action( 'admin_footer', 'siteorigin_panels_js_templates' );
$widgets = siteorigin_panels_get_widgets();
$directory_enabled = get_user_meta( get_current_user_id(), 'so_panels_directory_enabled', true );
wp_localize_script( 'so-panels-admin', 'panelsOptions', array(
'ajaxurl' => wp_nonce_url( admin_url('admin-ajax.php'), 'panels_action', '_panelsnonce' ),
'widgets' => $widgets,
'widget_dialog_tabs' => apply_filters( 'siteorigin_panels_widget_dialog_tabs', array(
0 => array(
'title' => __('All Widgets', 'siteorigin-panels'),
'filter' => array(
'installed' => true,
'groups' => ''
)
)
) ),
'row_layouts' => apply_filters( 'siteorigin_panels_row_layouts', array() ),
'directory_enabled' => !empty( $directory_enabled ),
'copy_content' => siteorigin_panels_setting( 'copy-content' ),
// Settings for the contextual menu
'contextual' => array(
// Developers can change which widgets are displayed by default using this filter
'default_widgets' => apply_filters( 'siteorigin_panels_contextual_default_widgets', array(
'SiteOrigin_Widget_Editor_Widget',
'SiteOrigin_Widget_Button_Widget',
'SiteOrigin_Widget_Image_Widget',
'SiteOrigin_Panels_Widgets_Layout',
) )
),
// General localization messages
'loc' => array(
'missing_widget' => array(
'title' => __('Missing Widget', 'siteorigin-panels'),
'description' => __("Page Builder doesn't know about this widget.", 'siteorigin-panels'),
),
'time' => array(
// TRANSLATORS: Number of seconds since
'seconds' => __('%d seconds', 'siteorigin-panels'),
// TRANSLATORS: Number of minutes since
'minutes' => __('%d minutes', 'siteorigin-panels'),
// TRANSLATORS: Number of hours since
'hours' => __('%d hours', 'siteorigin-panels'),
// TRANSLATORS: A single second since
'second' => __('%d second', 'siteorigin-panels'),
// TRANSLATORS: A single minute since
'minute' => __('%d minute', 'siteorigin-panels'),
// TRANSLATORS: A single hour since
'hour' => __('%d hour', 'siteorigin-panels'),
// TRANSLATORS: Time ago - eg. "1 minute before".
'ago' => __('%s before', 'siteorigin-panels'),
'now' => __('Now', 'siteorigin-panels'),
),
'history' => array(
// History messages
'current' => __('Current', 'siteorigin-panels'),
'revert' => __('Original', 'siteorigin-panels'),
'restore' => __('Version restored', 'siteorigin-panels'),
'back_to_editor' => __('Converted to editor', 'siteorigin-panels'),
// Widgets
// TRANSLATORS: Message displayed in the history when a widget is deleted
'widget_deleted' => __('Widget deleted', 'siteorigin-panels'),
// TRANSLATORS: Message displayed in the history when a widget is added
'widget_added' => __('Widget added', 'siteorigin-panels'),
// TRANSLATORS: Message displayed in the history when a widget is edited
'widget_edited' => __('Widget edited', 'siteorigin-panels'),
// TRANSLATORS: Message displayed in the history when a widget is duplicated
'widget_duplicated' => __('Widget duplicated', 'siteorigin-panels'),
// TRANSLATORS: Message displayed in the history when a widget position is changed
'widget_moved' => __('Widget moved', 'siteorigin-panels'),
// Rows
// TRANSLATORS: Message displayed in the history when a row is deleted
'row_deleted' => __('Row deleted', 'siteorigin-panels'),
// TRANSLATORS: Message displayed in the history when a row is added
'row_added' => __('Row added', 'siteorigin-panels'),
// TRANSLATORS: Message displayed in the history when a row is edited
'row_edited' => __('Row edited', 'siteorigin-panels'),
// TRANSLATORS: Message displayed in the history when a row position is changed
'row_moved' => __('Row moved', 'siteorigin-panels'),
// TRANSLATORS: Message displayed in the history when a row is duplicated
'row_duplicated' => __('Row duplicated', 'siteorigin-panels'),
// Cells
'cell_resized' => __('Cell resized', 'siteorigin-panels'),
// Prebuilt
'prebuilt_loaded' => __('Prebuilt layout loaded', 'siteorigin-panels'),
),
// general localization
'prebuilt_loading' => __('Loading prebuilt layout', 'siteorigin-panels'),
'confirm_use_builder' => __("Would you like to copy this editor's existing content to Page Builder?", 'siteorigin-panels'),
'confirm_stop_builder' => __("Would you like to clear your Page Builder content and revert to using the standard visual editor?", 'siteorigin-panels'),
// TRANSLATORS: This is the title for a widget called "Layout Builder"
'layout_widget' => __('Layout Builder Widget', 'siteorigin-panels'),
// TRANSLATORS: A standard confirmation message
'dropdown_confirm' => __('Are you sure?', 'siteorigin-panels'),
// TRANSLATORS: When a layout file is ready to be inserted. %s is the filename.
'ready_to_insert' => __('%s is ready to insert.', 'siteorigin-panels'),
// Everything for the contextual menu
'contextual' => array(
'add_widget_below' => __('Add Widget Below', 'siteorigin-panels'),
'add_widget_cell' => __('Add Widget to Cell', 'siteorigin-panels'),
'search_widgets' => __('Search Widgets', 'siteorigin-panels'),
'add_row' => __('Add Row', 'siteorigin-panels'),
'column' => __('Column', 'siteorigin-panels'),
'widget_actions' => __( 'Widget Actions', 'siteorigin-panels' ),
'widget_edit' => __( 'Edit Widget', 'siteorigin-panels' ),
'widget_duplicate' => __( 'Duplicate Widget', 'siteorigin-panels' ),
'widget_delete' => __( 'Delete Widget', 'siteorigin-panels' ),
'row_actions' => __( 'Row Actions', 'siteorigin-panels' ),
'row_edit' => __( 'Edit Row', 'siteorigin-panels' ),
'row_duplicate' => __( 'Duplicate Row', 'siteorigin-panels' ),
'row_delete' => __( 'Delete Row', 'siteorigin-panels' ),
),
'draft' => __( 'Draft', 'siteorigin-panels' ),
),
'plupload' => array(
'max_file_size' => wp_max_upload_size().'b',
'url' => wp_nonce_url( admin_url('admin-ajax.php'), 'panels_action', '_panelsnonce' ),
'flash_swf_url' => includes_url('js/plupload/plupload.flash.swf'),
'silverlight_xap_url' => includes_url('js/plupload/plupload.silverlight.xap'),
'filter_title' => __('Page Builder layouts', 'siteorigin-panels'),
'error_message' => __('Error uploading or importing file.', 'siteorigin-panels'),
),
'wpColorPickerOptions' => apply_filters('siteorigin_panels_wpcolorpicker_options', array()),
'prebuiltDefaultScreenshot' => plugin_dir_url( __FILE__ ) . 'css/images/prebuilt-default.png',
));
if( $screen->base != 'widgets' ) {
// Render all the widget forms. A lot of widgets use this as a chance to enqueue their scripts
$original_post = isset($GLOBALS['post']) ? $GLOBALS['post'] : null; // Make sure widgets don't change the global post.
foreach($GLOBALS['wp_widget_factory']->widgets as $class => $widget_obj){
ob_start();
$return = $widget_obj->form( array() );
do_action_ref_array( 'in_widget_form', array( &$widget_obj, &$return, array() ) );
ob_clean();
}
$GLOBALS['post'] = $original_post;
}
// This gives panels a chance to enqueue scripts too, without having to check the screen ID.
if( $screen->base != 'widgets' && $screen->base != 'customize' ) {
do_action( 'siteorigin_panel_enqueue_admin_scripts' );
do_action( 'sidebar_admin_setup' );
}
}
}
add_action( 'admin_print_scripts-post-new.php', 'siteorigin_panels_admin_enqueue_scripts' );
add_action( 'admin_print_scripts-post.php', 'siteorigin_panels_admin_enqueue_scripts' );
add_action( 'admin_print_scripts-appearance_page_so_panels_home_page', 'siteorigin_panels_admin_enqueue_scripts' );
add_action( 'admin_print_scripts-widgets.php', 'siteorigin_panels_admin_enqueue_scripts' );
/**
* Print templates when in customizer
*/
function siteorigin_panels_customize_controls_print_footer_scripts() {
siteorigin_panels_js_templates();
}
add_action( 'customize_controls_print_footer_scripts', 'siteorigin_panels_customize_controls_print_footer_scripts' );
/**
* Get an array of all the available widgets.
*
* @return array
*/
function siteorigin_panels_get_widgets(){
global $wp_widget_factory;
$widgets = array();
foreach($wp_widget_factory->widgets as $class => $widget_obj) {
$widgets[$class] = array(
'class' => $class,
'title' => !empty($widget_obj->name) ? $widget_obj->name : __('Untitled Widget', 'siteorigin-panels'),
'description' => !empty($widget_obj->widget_options['description']) ? $widget_obj->widget_options['description'] : '',
'installed' => true,
'groups' => array(),
);
// Get Page Builder specific widget options
if( isset($widget_obj->widget_options['panels_title']) ) {
$widgets[$class]['panels_title'] = $widget_obj->widget_options['panels_title'];
}
if( isset($widget_obj->widget_options['panels_groups']) ) {
$widgets[$class]['groups'] = $widget_obj->widget_options['panels_groups'];
}
if( isset($widget_obj->widget_options['panels_icon']) ) {
$widgets[$class]['icon'] = $widget_obj->widget_options['panels_icon'];
}
}
// Other plugins can manipulate the list of widgets. Possibly to add recommended widgets
$widgets = apply_filters('siteorigin_panels_widgets', $widgets);
// Sort the widgets alphabetically
uasort($widgets, 'siteorigin_panels_widgets_sorter');
return $widgets;
}
/**
* @param $a
* @param $b
*
* @return int
*/
function siteorigin_panels_widgets_sorter($a, $b){
if( empty($a['title']) ) return -1;
if( empty($b['title']) ) return 1;
return $a['title'] > $b['title'] ? 1 : -1;
}
/**
* Display the templates for JS in the footer
*/
function siteorigin_panels_js_templates(){
include plugin_dir_path(__FILE__).'tpl/js-templates.php';
}
/**
* Enqueue the admin panel styles
*
* @param string $prefix
* @param bool $force Should we force the enqueue
*
* @action admin_print_styles-post-new.php
* @action admin_print_styles-post.php
*/
function siteorigin_panels_admin_enqueue_styles( $prefix = '', $force = false ) {
if ( $force || siteorigin_panels_is_admin_page() ) {
wp_enqueue_style( 'so-panels-admin', plugin_dir_url(__FILE__) . 'css/admin.css', array( 'wp-color-picker' ), SITEORIGIN_PANELS_VERSION );
do_action( 'siteorigin_panel_enqueue_admin_styles' );
}
}
add_action( 'admin_print_styles-post-new.php', 'siteorigin_panels_admin_enqueue_styles' );
add_action( 'admin_print_styles-post.php', 'siteorigin_panels_admin_enqueue_styles' );
add_action( 'admin_print_styles-appearance_page_so_panels_home_page', 'siteorigin_panels_admin_enqueue_styles' );
add_action( 'admin_print_styles-widgets.php', 'siteorigin_panels_admin_enqueue_styles' );
/**
* Add a help tab to pages with panels.
*/
function siteorigin_panels_add_help_tab($prefix) {
$screen = get_current_screen();
if(
( $screen->base == 'post' && ( in_array( $screen->id, siteorigin_panels_setting( 'post-types' ) ) || $screen->id == '') )
|| ($screen->id == 'appearance_page_so_panels_home_page')
) {
$screen->add_help_tab( array(
'id' => 'panels-help-tab', //unique id for the tab
'title' => __( 'Page Builder', 'siteorigin-panels' ), //unique visible title for the tab
'callback' => 'siteorigin_panels_add_help_tab_content'
) );
}
}
add_action('load-page.php', 'siteorigin_panels_add_help_tab', 12);
add_action('load-post-new.php', 'siteorigin_panels_add_help_tab', 12);
add_action('load-appearance_page_so_panels_home_page', 'siteorigin_panels_add_help_tab', 12);
/**
* Display the content for the help tab.
*/
function siteorigin_panels_add_help_tab_content(){
include plugin_dir_path(__FILE__) . 'tpl/help.php';
}
/**
* Save the panels data
*
* @param $post_id
* @param $post
*
* @action save_post
*/
function siteorigin_panels_save_post( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( empty( $_POST['_sopanels_nonce'] ) || !wp_verify_nonce( $_POST['_sopanels_nonce'], 'save' ) ) return;
if ( !current_user_can( 'edit_post', $post_id ) ) return;
if ( !isset( $_POST['panels_data'] ) ) return;
if ( !wp_is_post_revision($post_id) ) {
$panels_data = json_decode( wp_unslash( $_POST['panels_data'] ), true);
$panels_data['widgets'] = siteorigin_panels_process_raw_widgets($panels_data['widgets']);
$panels_data = siteorigin_panels_styles_sanitize_all( $panels_data );
$panels_data = apply_filters( 'siteorigin_panels_data_pre_save', $panels_data, $post, $post_id );
if( !empty( $panels_data['widgets'] ) || !empty($panels_data['grids']) ) {
update_post_meta( $post_id, 'panels_data', $panels_data );
}
else {
// There are no widgets or rows, so delete the panels data
delete_post_meta( $post_id, 'panels_data' );
}
}
else {
// When previewing, we don't need to wp_unslash the panels_data post variable.
$panels_data = json_decode( wp_unslash( $_POST['panels_data'] ), true);
$panels_data['widgets'] = siteorigin_panels_process_raw_widgets($panels_data['widgets']);
$panels_data = siteorigin_panels_styles_sanitize_all( $panels_data );
$panels_data = apply_filters( 'siteorigin_panels_data_pre_save', $panels_data, $post, $post_id );
// Because of issue #20299, we are going to save the preview into a different variable so we don't overwrite the actual data.
// https://core.trac.wordpress.org/ticket/20299
if( !empty( $panels_data['widgets'] ) ) {
update_post_meta( $post_id, '_panels_data_preview', $panels_data );
}
else {
delete_post_meta( $post_id, '_panels_data_preview' );
}
}
}
add_action( 'save_post', 'siteorigin_panels_save_post', 10, 2 );
/**
* @param $value
* @param $post_id
* @param $meta_key
*
* @return mixed
*/
function siteorigin_panels_view_post_preview($value, $post_id, $meta_key){
if( $meta_key == 'panels_data' && is_preview() && current_user_can( 'edit_post', $post_id ) ) {
$panels_preview = get_post_meta($post_id, '_panels_data_preview');
return !empty($panels_preview) ? $panels_preview : $value;
}
return $value;
}
add_filter('get_post_metadata', 'siteorigin_panels_view_post_preview', 10, 3);
/**
* Process raw widgets that have come from the Page Builder front end.
*
* @param $widgets
*
* @return array
*/
function siteorigin_panels_process_raw_widgets($widgets) {
if( empty( $widgets ) || ! is_array( $widgets ) ) {
return array();
}
global $wp_widget_factory;
for($i = 0; $i < count($widgets); $i++) {
if( !is_array( $widgets[$i] ) ) {
continue;
}
if( is_array( $widgets[$i] ) ) {
$info = (array) ( is_array( $widgets[$i]['panels_info'] ) ? $widgets[$i]['panels_info'] : $widgets[$i]['info'] );
}
else {
$info = array();
}
unset($widgets[$i]['info']);
if( !empty($info['raw']) ) {
if ( isset( $wp_widget_factory->widgets[ $info['class'] ] ) && method_exists( $info['class'], 'update' ) ) {
$the_widget = $wp_widget_factory->widgets[ $info['class'] ];
$instance = $the_widget->update( $widgets[$i], $widgets[$i] );
$instance = apply_filters ( 'widget_update_callback', $instance, $widgets[$i], $widgets[$i], $the_widget );
$widgets[$i] = $instance;
unset($info['raw']);
}
}
$info['class'] = addslashes( $info['class'] );
$widgets[$i]['panels_info'] = $info;
}
return $widgets;
}
/**
* Get the home page panels layout data.
*
* @return mixed|void
*/
function siteorigin_panels_get_home_page_data(){
$page_id = get_option( 'page_on_front' );
if( empty($page_id) ) $page_id = get_option( 'siteorigin_panels_home_page_id' );
if( empty($page_id) ) return false;
$panels_data = get_post_meta( $page_id, 'panels_data', true );
if( is_null( $panels_data ) ){
// Load the default layout
$layouts = apply_filters( 'siteorigin_panels_prebuilt_layouts', array() );
$panels_data = !empty($layouts['default_home']) ? $layouts['default_home'] : current($layouts);
}
return $panels_data;
}
/**
* Get the Page Builder data for the current admin page.
*
* @return array
*/
function siteorigin_panels_get_current_admin_panels_data( ){
$screen = get_current_screen();
// Localize the panels with the panels data
if($screen->base == 'appearance_page_so_panels_home_page'){
$home_page_id = get_option( 'page_on_front' );
if( empty($home_page_id) ) $home_page_id = get_option( 'siteorigin_panels_home_page_id' );
$panels_data = !empty($home_page_id) ? get_post_meta( $home_page_id, 'panels_data', true ) : null;
if( is_null( $panels_data ) ){
// Load the default layout
$layouts = apply_filters( 'siteorigin_panels_prebuilt_layouts', array() );
$home_name = siteorigin_panels_setting('home-page-default') ? siteorigin_panels_setting('home-page-default') : 'home';
$panels_data = !empty($layouts[$home_name]) ? $layouts[$home_name] : current($layouts);
}
elseif( empty( $panels_data ) ) {
// The current page_on_front isn't using page builder
return false;
}
$panels_data = apply_filters( 'siteorigin_panels_data', $panels_data, 'home');
}
else{
global $post;
$panels_data = get_post_meta( $post->ID, 'panels_data', true );
$panels_data = apply_filters( 'siteorigin_panels_data', $panels_data, $post->ID );
}
if ( empty( $panels_data ) ) $panels_data = array();
return $panels_data;
}
/**
* Generate the CSS for the page layout.
*
* @param $post_id
* @param $panels_data
* @return string
*/
function siteorigin_panels_generate_css($post_id, $panels_data = false){
// Exit if we don't have panels data
if( empty($panels_data) ) {
$panels_data = get_post_meta( $post_id, 'panels_data', true );
$panels_data = apply_filters( 'siteorigin_panels_data', $panels_data, $post_id );
}
if ( empty( $panels_data ) || empty( $panels_data['grids'] ) ) return;
// Get some of the default settings
$settings = siteorigin_panels_setting();
$panels_tablet_width = $settings['tablet-width'];
$panels_mobile_width = $settings['mobile-width'];
$panels_margin_bottom = $settings['margin-bottom'];
$panels_margin_bottom_last_row = $settings['margin-bottom-last-row'];
$css = new SiteOrigin_Panels_Css_Builder();
$ci = 0;
foreach ( $panels_data['grids'] as $gi => $grid ) {
$cell_count = intval( $grid['cells'] );
$grid_id = !empty( $grid['style']['id'] ) ? (string) sanitize_html_class( $grid['style']['id'] ) : intval( $gi );
// Add the cell sizing
for ( $i = 0; $i < $cell_count; $i++ ) {
$cell = $panels_data['grid_cells'][$ci++];
if ( $cell_count > 1 ) {
$width = round( $cell['weight'] * 100, 3 ) . '%';
$width = apply_filters('siteorigin_panels_css_cell_width', $width, $grid, $gi, $cell, $ci - 1, $panels_data, $post_id);
// Add the width and ensure we have correct formatting for CSS.
$css->add_cell_css($post_id, $grid_id, $i, '', array(
'width' => str_replace(',', '.', $width)
));
}
}
// Add the bottom margin to any grids that aren't the last
if($gi != count($panels_data['grids'])-1 || !empty($grid['style']['bottom_margin']) || !empty($panels_margin_bottom_last_row)){
// Filter the bottom margin for this row with the arguments
$css->add_row_css($post_id, $grid_id, '', array(
'margin-bottom' => apply_filters('siteorigin_panels_css_row_margin_bottom', $panels_margin_bottom.'px', $grid, $gi, $panels_data, $post_id)
));
}
$collapse_order = !empty( $grid['style']['collapse_order'] ) ? $grid['style']['collapse_order'] : ( !is_rtl() ? 'left-top' : 'right-top' );
if ( $cell_count > 1 ) {
$css->add_cell_css($post_id, $grid_id, false, '', array(
// Float right for RTL
'float' => $collapse_order == 'left-top' ? 'left' : 'right'
) );
}
else {
$css->add_cell_css($post_id, $grid_id, false, '', array(
// Float right for RTL
'float' => 'none'
) );
}
if ( $settings['responsive'] ) {
if( $settings['tablet-layout'] && $cell_count >= 3 && $panels_tablet_width > $panels_mobile_width ) {
// Tablet Responsive
$css->add_cell_css($post_id, $grid_id, false, '', array(
'width' => '50%'
), $panels_tablet_width);
}
// Mobile Responsive
$css->add_cell_css($post_id, $grid_id, false, '', array(
'float' => 'none',
'width' => 'auto'
), $panels_mobile_width);
for ( $i = 0; $i < $cell_count; $i++ ) {
if ( ( $collapse_order == 'left-top' && $i != $cell_count - 1 ) || ( $collapse_order == 'right-top' && $i !== 0 ) ) {
$css->add_cell_css($post_id, $grid_id, $i, '', array(
'margin-bottom' => $panels_margin_bottom . 'px',
), $panels_mobile_width);
}
}
}
}
// Add the bottom margins
$css->add_cell_css($post_id, false, false, '.so-panel', array(
'margin-bottom' => apply_filters('siteorigin_panels_css_cell_margin_bottom', $panels_margin_bottom.'px', $grid, $gi, $panels_data, $post_id)
));
$css->add_cell_css($post_id, false, false, '.so-panel:last-child', array(
'margin-bottom' => apply_filters('siteorigin_panels_css_cell_last_margin_bottom', '0px', $grid, $gi, $panels_data, $post_id)
));
if( $settings['responsive'] ) {
// Add CSS to prevent overflow on mobile resolution.
$css->add_row_css($post_id, false, '', array(
'margin-left' => 0,
'margin-right' => 0,
), $panels_mobile_width);
$css->add_cell_css($post_id, false, false, '', array(
'padding' => 0,
), $panels_mobile_width);
// Hide empty cells on mobile
$css->add_row_css($post_id, false, '.panel-grid-cell-empty', array(
'display' => 'none',
), $panels_mobile_width);
// Hide empty cells on mobile
$css->add_row_css($post_id, false, '.panel-grid-cell-mobile-last', array(
'margin-bottom' => '0px',
), $panels_mobile_width);
}
// Let other plugins customize various aspects of the rows (grids)
foreach ( $panels_data['grids'] as $gi => $grid ) {
$grid_id = !empty( $grid['style']['id'] ) ? (string) sanitize_html_class( $grid['style']['id'] ) : intval( $gi );
// Let other themes and plugins change the gutter.
$gutter = apply_filters('siteorigin_panels_css_row_gutter', $settings['margin-sides'].'px', $grid, $gi, $panels_data);
if( !empty($gutter) ) {
// We actually need to find half the gutter.
preg_match('/([0-9\.,]+)(.*)/', $gutter, $match);
if( !empty( $match[1] ) ) {
$margin_half = (floatval($match[1])/2) . $match[2];
$css->add_row_css($post_id, $grid_id, '', array(
'margin-left' => '-' . $margin_half,
'margin-right' => '-' . $margin_half,
) );
$css->add_cell_css($post_id, $grid_id, false, '', array(
'padding-left' => $margin_half,
'padding-right' => $margin_half,
) );
}
}
}
foreach ($panels_data['widgets'] as $widget_id => $widget) {
if (!empty($widget['panels_info']['style']['link_color'])) {
$selector = '#panel-' . $post_id . '-' . $widget['panels_info']['grid'] . '-' . $widget['panels_info']['cell'] . '-' . $widget['panels_info']['cell_index'] . ' a';
$css->add_css( $selector, array(
'color' => $widget['panels_info']['style']['link_color']
) );
}
}
// Let other plugins and components filter the CSS object.
$css = apply_filters('siteorigin_panels_css_object', $css, $panels_data, $post_id);
return $css->get_css();
}
/**
* Filter the content of the panel, adding all the widgets.
*
* @param $content
* @return string
*
* @filter the_content
*/
function siteorigin_panels_filter_content( $content ) {
global $post;
if ( empty( $post ) ) return $content;
if ( !apply_filters( 'siteorigin_panels_filter_content_enabled', true ) ) return $content;
// Check if this post has panels_data
$panels_data = get_post_meta( $post->ID, 'panels_data', true );
if ( !empty( $panels_data ) ) {
$panel_content = siteorigin_panels_render( $post->ID );
if ( !empty( $panel_content ) ) {
$content = $panel_content;
if( !is_singular() ) {
// This is an archive page, so try strip out anything after the more text
if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
$content = explode( $matches[0], $content, 2 );
$content = $content[0];
$content = force_balance_tags( $content );
if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) ) {
$more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
}
else {
$more_link_text = __('Read More', 'siteorigin-panels');
}
$more_link = apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text );
$content .= '<p>' . $more_link . '</p>';
}
}
}
}
return $content;
}
add_filter( 'the_content', 'siteorigin_panels_filter_content' );
/**
* Render the panels
*
* @param int|string|bool $post_id The Post ID or 'home'.
* @param bool $enqueue_css Should we also enqueue the layout CSS.
* @param array|bool $panels_data Existing panels data. By default load from settings or post meta.
* @return string
*/
function siteorigin_panels_render( $post_id = false, $enqueue_css = true, $panels_data = false ) {
if( empty($post_id) ) $post_id = get_the_ID();
global $siteorigin_panels_current_post;
$old_current_post = $siteorigin_panels_current_post;
$siteorigin_panels_current_post = $post_id;
// Try get the cached panel from in memory cache.
global $siteorigin_panels_cache;
if(!empty($siteorigin_panels_cache) && !empty($siteorigin_panels_cache[$post_id]))
return $siteorigin_panels_cache[$post_id];
if( empty($panels_data) ) {
if( strpos($post_id, 'prebuilt:') === 0) {
list($null, $prebuilt_id) = explode(':', $post_id, 2);
$layouts = apply_filters('siteorigin_panels_prebuilt_layouts', array());
$panels_data = !empty($layouts[$prebuilt_id]) ? $layouts[$prebuilt_id] : array();
}
else if($post_id == 'home'){
$page_id = get_option( 'page_on_front' );
if( empty($page_id) ) $page_id = get_option( 'siteorigin_panels_home_page_id' );
$panels_data = !empty($page_id) ? get_post_meta( $page_id, 'panels_data', true ) : null;
if( is_null($panels_data) ){
// Load the default layout
$layouts = apply_filters('siteorigin_panels_prebuilt_layouts', array());
$prebuilt_id = siteorigin_panels_setting('home-page-default') ? siteorigin_panels_setting('home-page-default') : 'home';
$panels_data = !empty($layouts[$prebuilt_id]) ? $layouts[$prebuilt_id] : current($layouts);
}
}
else{
if ( post_password_required($post_id) ) return false;
$panels_data = get_post_meta( $post_id, 'panels_data', true );
}
}
$panels_data = apply_filters( 'siteorigin_panels_data', $panels_data, $post_id );
if( empty( $panels_data ) || empty( $panels_data['grids'] ) ) return '';
// Filter the widgets to add indexes
if ( !empty( $panels_data['widgets'] ) ) {
$last_gi = 0;
$last_ci = 0;
$last_wi = 0;
foreach ( $panels_data['widgets'] as $wid => &$widget_info ) {
if ( $widget_info['panels_info']['grid'] != $last_gi ) {
$last_gi = $widget_info['panels_info']['grid'];