-
Notifications
You must be signed in to change notification settings - Fork 12
/
class.mesh.php
1160 lines (950 loc) · 33.8 KB
/
class.mesh.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
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
/**
* Meat of most of Mesh Functionality
*
* @since 1.0
* @package Mesh
*/
/**
* Class Mesh
*/
class Mesh {
/**
* Store available templates.
*
* @var array
*/
public $templates = array();
/**
* Store all our TinyMCE Editors
*
* @var array
*/
public $tinymce_editors = array();
/**
* Store the available blocks per template.
*
* @since 0.3.5
*
* @var array
*/
public $template_data = array();
/**
* __construct function.
*
* @access public
*/
function __construct() {
$this->template_data = array(
'mesh-columns-1.php' => array(
'label' => esc_html__( '1 Columns', 'mesh' ),
'blocks' => 1,
'widths' => array( 12 ),
),
'mesh-columns-2.php' => array(
'label' => esc_html__( '2 Columns', 'mesh' ),
'blocks' => 2,
'widths' => array( 6, 6 ),
),
'mesh-columns-3.php' => array(
'label' => esc_html__( '3 Columns', 'mesh' ),
'blocks' => 3,
'widths' => array( 4, 4, 4 ),
),
);
add_action( 'init', array( $this, 'init' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
// Edit form is static so it can be called elsewhere.
add_action( 'edit_form_after_editor', array( 'Mesh', 'edit_page_form' ) );
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
add_action( 'wp_trash_post', array( $this, 'wp_trash_post' ) );
add_action( 'before_delete_post', array( $this, 'before_delete_post' ) );
add_action( 'untrash_post', array( $this, 'untrash_post' ) );
add_action( 'loop_end', array( $this, 'loop_end' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_styles' ) );
add_filter( 'content_edit_pre', array( $this, 'the_content' ) );
add_filter( 'the_content', array( $this, 'the_content' ), 5 );
add_filter( 'post_class', array( $this, 'post_class' ) );
add_filter( 'edit_form_after_title', array( $this, 'output_debug_post_info' ) );
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
include_once 'class.mesh-ajax.php';
include_once 'class.mesh-templates-ajax.php';
}
// Adjust TinyMCE and Media buttons.
add_filter( 'tiny_mce_before_init', array( $this, 'tiny_mce_before_init' ) );
// Add Screen Options.
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_filter( 'get_edit_post_link', array( $this, 'get_edit_post_link' ), 10, 3 );
// @since 1.1.3.
add_action( 'after_setup_theme', array( $this, 'after_theme_setup' ) );
}
/**
* Get the edit post links
*
* @param string $link The link we are manipulating.
* @param int $post_id Post ID.
* @param string $context Link context.
*
* @return mixed
*/
public function get_edit_post_link( $link, $post_id, $context ) {
global $post;
if ( empty( $post->post_parent ) ) {
return $link;
}
if ( 'mesh_section' !== get_post_type( $post->ID ) ) {
return $link;
}
if ( 'mesh_section' !== get_post_type( $post->post_parent ) ) {
return $link;
}
$parents = get_post_ancestors( $post->ID );
$id = ( ! empty( $parents ) ) ? $parents[ count( $parents ) - 1 ] : $post->post_parent;
if ( 'mesh_section' !== get_post_type( $id ) ) {
return '#';
}
return '#';
}
/**
* Output some useful information about posts.
*/
function output_debug_post_info() {
global $post;
}
/**
* Add Screen Options to the Plugin
*/
function admin_menu() {
add_action( 'load-post.php', array( $this, 'add_screen_options' ) );
add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 );
}
/**
* Save our custom display options
*
* @since 0.4.4
*
* @param string $status Save status.
* @param array $option Option we're saving.
* @param mixed $value Value to save.
*
* @return mixed
*/
function set_screen_option( $status, $option, $value ) {
if ( 'linchpin_mesh_section_kitchensink' === $option ) {
return $value;
}
return '';
}
/**
* Add Toggleable Options to show or hide controls
*
* @since 0.4.4
*/
function add_screen_options() {
$screen = get_current_screen();
if ( ! is_object( $screen ) || 'page' !== $screen->id ) {
return;
}
$args = array(
'label' => esc_html__( 'Show Extra Mesh Section Controls?', 'mesh' ),
'default' => 0,
'option' => 'linchpin_mesh_section_kitchensink',
);
add_screen_option( 'linchpin_mesh_section_kitchensink', $args );
}
/**
* Update our tiny MCE w/ our own settings.
*
* @param array $in Input Object used by TinyMCE.
*
* @return array $in Input Object
*/
function tiny_mce_before_init( $in ) {
global $post;
if ( empty( $post ) ) {
return $in;
}
// Exclude the default editor from our customizations.
if ( isset( $in['selector'] ) && '#content' === $in['selector'] ) {
return $in;
}
$in['remove_linebreaks'] = false;
$in['gecko_spellcheck'] = false;
$in['keep_styles'] = true;
$in['accessibility_focus'] = true;
$in['tabfocus_elements'] = 'major-publishing-actions';
$in['media_strict'] = false;
$in['paste_remove_styles'] = false;
$in['paste_remove_spans'] = false;
$in['paste_strip_class_attributes'] = 'none';
$in['paste_text_use_dialog'] = true;
$in['wpeditimage_disable_captions'] = true;
$in['allow_script_urls'] = true;
$in['plugins'] = 'tabfocus,paste,media,wordpress,wpgallery,wplink'; // WPCS: spelling ok.
// Only add in our editor styles if we have the file.
if ( file_exists( get_template_directory_uri() . '/editor-style.css' ) ) {
$in['content_css'] = get_template_directory_uri() . '/editor-style.css';
}
$in['wpautop'] = true;
$in['apply_source_formatting'] = false;
$in['block_formats'] = 'Paragraph=p; Heading 3=h3; Heading 4=h4';
$in['toolbar1'] = 'bold,italic,bullist,numlist,hr,alignleft,aligncenter,alignright,alignjustify,link,wp_adv ';
$in['toolbar2'] = 'formatselect,underline,strikethrough,forecolor,pastetext,removeformat ';
$in['toolbar3'] = '';
$in['toolbar4'] = '';
return apply_filters( 'mesh_tiny_mce_before_init', $in );
}
/**
* Init function.
*
* @access public
* @return void
*/
function init() {
load_plugin_textdomain( 'mesh', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
$labels = array(
'name' => esc_html_x( 'Content Section', 'Content Section', 'mesh' ),
'singular_name' => esc_html_x( 'Content Section', 'Content Section', 'mesh' ),
'menu_name' => esc_html__( 'Content Section', 'mesh' ),
'name_admin_bar' => esc_html__( 'Content Section', 'mesh' ),
'parent_item_colon' => esc_html__( 'Parent Content Section:', 'mesh' ),
'all_items' => esc_html__( 'All Content Sections', 'mesh' ),
'add_new_item' => esc_html__( 'Add New Content Section', 'mesh' ),
'add_new' => esc_html__( 'Add New', 'mesh' ),
'new_item' => esc_html__( 'New Content Section', 'mesh' ),
'edit_item' => esc_html__( 'Edit Content Section', 'mesh' ),
'update_item' => esc_html__( 'Update Content Section', 'mesh' ),
'view_item' => esc_html__( 'View Content Section', 'mesh' ),
'search_items' => esc_html__( 'Search Content Sections', 'mesh' ),
'not_found' => esc_html__( 'Not found', 'mesh' ),
'not_found_in_trash' => esc_html__( 'Not found in Trash', 'mesh' ),
);
register_post_type( 'mesh_section', array(
'label' => esc_html__( 'Content Section', 'mesh' ),
'description' => esc_html__( 'Content Section', 'mesh' ),
'labels' => $labels,
'public' => false,
'hierarchical' => true,
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'excerpt',
'page-attributes',
'revisions',
),
'capability_type' => 'post',
'has_archive' => false,
'show_in_menus' => false,
'show_in_nav_menus' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'menu_icon' => 'dashicons-feedback',
'show_ui' => LINCHPIN_MESH_DEBUG_MODE,
'rewrite' => false,
) );
}
/**
* The first time the plugin is activated, we need to add some default options.
*/
function admin_init() {
if ( false === get_option( 'mesh_version' ) ) {
update_option( 'mesh_post_types', array(
'page' => 1,
'mesh_template' => 1,
) );
update_option( 'mesh_version', LINCHPIN_MESH_VERSION );
}
}
/**
* Edit form.
*
* @access public
*
* @todo Add the ability to select which post can have Multple Content Sections
*
* @param object $post WordPress Post Object.
*
* @return void
*/
public static function edit_page_form( $post ) {
$allowed_post_types = get_option( 'mesh_post_types' );
if ( empty( $allowed_post_types ) ) {
return;
}
if ( ! array_key_exists( $post->post_type, $allowed_post_types ) ) {
return;
}
$content_sections = mesh_get_sections( $post->ID, 'array', array( 'publish', 'draft' ) );
$mesh_notifications = get_user_option( 'linchpin_mesh_notifications', get_current_user_id() );
?>
<div id="mesh-container" class="<?php echo esc_attr( $post->post_type ); ?>">
<?php wp_nonce_field( 'mesh_content_sections_nonce', 'mesh_content_sections_nonce' ); ?>
<?php
$hide = '';
if ( empty( $content_sections ) ) {
$hide = ' hide';
}
?>
<div class="notice below-h2 mesh-row mesh-main-ua-row<?php echo esc_attr( $hide ); ?>">
<div class="mesh-columns-2 columns">
<p class="title mesh-admin-title"><?php esc_html_e( 'Mesh', 'mesh' ); ?></p>
</div>
<div class="mesh-columns-10 columns text-right">
<?php include LINCHPIN_MESH___PLUGIN_DIR . 'admin/controls.php'; ?>
</div>
</div>
<?php if ( empty( $content_sections ) ) : ?>
<div id="mesh-description" class="description notice below-h2 text-center lead empty-sections-message">
<?php include LINCHPIN_MESH___PLUGIN_DIR . 'admin/sections-empty.php'; ?>
</div>
<?php else : ?>
<?php if ( empty( $mesh_notifications['intro'] ) ) : ?>
<div id="mesh-description" class="description collapse notice is-dismissible notice-info below-h2" data-type="intro">
<p><?php esc_html_e( 'Mesh allows you to easily break up your page into different blocks of content/markup.', 'mesh' ); ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
<div id="mesh-sections-container">
<?php foreach ( $content_sections as $key => $section ) : ?>
<?php mesh_add_section_admin_markup( $section, true ); ?>
<?php endforeach; ?>
</div>
<div class="notice below-h2 mesh-row mesh-main-ua-row bottom<?php echo esc_attr( $hide ); ?>">
<div class="mesh-columns-12 columns text-right">
<?php include LINCHPIN_MESH___PLUGIN_DIR . 'admin/controls.php'; ?>
</div>
</div>
</div>
<?php
}
/**
* Save post.
*
* @access public
*
* @param mixed $post_id Post ID.
* @param object $post Post Object.
*
* @return void
*/
public function save_post( $post_id, $post ) {
// Skip revisions and autosaves.
if ( wp_is_post_revision( $post_id ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
return;
}
// Users should have the ability to edit listings.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! isset( $_POST['mesh_content_sections_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['mesh_content_sections_nonce'] ), 'mesh_content_sections_nonce' ) ) { // Input var okay. WPCS: CSRF okay.
return;
}
if ( ! isset( $_POST['mesh-sections'] ) || empty( $_POST['mesh-sections'] ) ) { // Input var okay.
return;
}
remove_action( 'save_post', array( $this, 'save_post' ), 10 );
/**
* Process Section Meta
*/
$default_section_meta = array(
'css_class',
'row_class',
'section_id',
'lp_equal',
'title_display',
'push_pull',
'collapse',
'blocks',
'post_title',
'post_status',
'template',
'menu_order',
'featured_image',
'centered',
'columns',
);
/*
* This filter is used to remove or add elements to the default section meta
* @todo "meta" related to a section
*/
$default_section_meta = apply_filters( 'mesh_default_section_meta_fields', $default_section_meta );
$default_section_values = array(
'css_class' => '',
'row_class' => '',
'section_id' => '',
'lp_equal' => false,
'title_display' => '',
'push_pull' => false,
'collapse' => false,
'blocks' => [],
'post_title' => '',
'post_status' => '',
'template' => '',
'menu_order' => 0,
'featured_image' => '',
'centered' => false,
'columns' => 1,
);
$count = 0;
$column_title_pattern = '/(no (section|column) title)( - ([0-9]*))?/mi'; // Match both old format and new format including - POST_ID also match column or sections
// Check if we are doing a section update via AJAX.
$saving_section_via_ajax = false;
$ajax_section_id = 0;
if ( ( ( isset( $_POST['action'] ) && isset( $_POST['mesh_section_id'] ) ) && ! empty( $_POST['action'] ) ) && 'mesh_save_section' === sanitize_text_field( wp_unslash( $_POST['action'] ) ) ) { // Input var okay.
$saving_section_via_ajax = true;
$ajax_section_id = wp_unslash( absint( $_POST['mesh_section_id'] ) ); // Input var okay.
}
if ( ! isset( $_POST['mesh-sections'] ) ) { // Input var okay.
return;
}
foreach ( $_POST['mesh-sections'] as $section_id => $section_data ) { // Input var okay. sanitization ok.
// If using AJAX, make sure we only update the section we want to save.
if ( $saving_section_via_ajax && $ajax_section_id !== $section_id ) {
continue;
}
$section_id = (int) $section_id;
$section = get_post( $section_id );
if ( 'mesh_section' !== $section->post_type ) {
continue;
}
$status = sanitize_post_field( 'post_status', $section_data['post_status'], $post_id, 'attribute' );
if ( ! in_array( $status, array( 'publish', 'draft' ), true ) ) {
$status = 'draft';
}
$updates = array(
'ID' => (int) $section_id,
'post_content' => '', // Sections don't have content.
'post_status' => $status,
'menu_order' => ( empty( $section_data['menu_order'] ) ) ? $count : absint( $section_data['menu_order'] ),
);
preg_match_all( $column_title_pattern, $section_data['post_title'], $section_title_matches, PREG_SET_ORDER, 0 );
// If empty or matches "No Section Title" or "No Column Title" and does not have a post_id
// Set a new title that includes the post_id suffix
if ( empty( $section_data['post_title'] ) || ! empty( $section_title_matches ) ) {
if ( ! empty( $section_title_matches[1] ) && empty( $section_title_matches[3] ) ) {
$updates['post_title'] = 'No Section Title - ' . $section_id;
}
} else {
$updates['post_title'] = sanitize_text_field( $section_data['post_title'] );
}
// double catch
if ( empty( $updates['post_title'] ) ) {
$updates['post_title'] = 'No Section Title - ' . $section_id;
}
wp_update_post( $updates );
$count++;
// Save Template.
$template = sanitize_text_field( $section_data['template'] );
Mesh_Common::update_delete_section_meta( $section->ID, '_mesh_template', $template );
// Save Section CSS Classes.
$css_classes = explode( ' ', $section_data['css_class'] );
$sanitized_css_classes = array();
foreach ( $css_classes as $css ) {
$sanitized_css_classes[] = sanitize_html_class( $css );
}
$sanitized_css_classes = implode( ' ', $sanitized_css_classes );
Mesh_Common::update_delete_section_meta( $section->ID, '_mesh_css_class', $sanitized_css_classes );
// Save Row CSS Classes.
$row_classes = explode( ' ', $section_data['row_class'] );
$sanitized_row_classes = array();
foreach ( $row_classes as $css ) {
$sanitized_row_classes[] = sanitize_html_class( $css );
}
$sanitized_row_classes = implode( ' ', $sanitized_row_classes );
Mesh_Common::update_delete_section_meta( $section->ID, '_mesh_row_class', $sanitized_row_classes );
// Save Section ID
$mesh_section_id = $section_data['section_id'];
$mesh_section_id = sanitize_html_class( $mesh_section_id );
Mesh_Common::update_delete_section_meta( $section->ID, '_mesh_section_id', $mesh_section_id );
// Save Featured Image
$featured_image = ( isset( $section_data['featured_image'] ) ) ? (int) $section_data['featured_image'] : '';
Mesh_Common::update_delete_section_meta( $section->ID, '_thumbnail_id', (int) $featured_image );
// Save LP Equal.
$lp_equal = '';
if ( ! empty( $section_data['lp_equal'] ) ) {
$lp_equal = sanitize_text_field( $section_data['lp_equal'] );
}
Mesh_Common::update_delete_section_meta( $section->ID, '_mesh_lp_equal', $lp_equal );
// Save Title Display.
$section_title = ( isset( $section_data['title_display'] ) ) ? $section_data['title_display'] : '';
Mesh_Common::update_delete_section_meta( $section->ID, '_mesh_title_display', $section_title );
// Save Push / Pull.
$push_pull = ( isset( $section_data['push_pull'] ) ) ? $section_data['push_pull'] : '';
Mesh_Common::update_delete_section_meta( $section->ID, '_mesh_push_pull', $push_pull );
// Save Collapse.
$collapse = ( isset( $section_data['collapse'] ) ) ? $section_data['collapse'] : '';
Mesh_Common::update_delete_section_meta( $section->ID, '_mesh_collapse', $collapse );
// Save Centered.
$centered = ( isset( $section_data['centered'] ) ) ? $section_data['centered'] : '';
Mesh_Common::update_delete_section_meta( $section->ID, '_mesh_centered', $centered );
// Process our custom meta.
foreach ( $section_data as $data_key => $data_field ) {
// Do not process default keys.
if ( in_array( $data_key, $default_section_meta, true ) ) {
continue;
}
// Save Custom Meta Field.
Mesh_Common::update_delete_section_meta( $section->ID, '_mesh_' . $data_key, $section_data[ $data_key ] );
}
// Process the section's blocks.
$blocks = array();
if ( ! empty( $section_data['blocks'] ) ) {
$blocks = $section_data['blocks'];
}
foreach ( $blocks as $block_id => $block_data ) {
$block = get_post( (int) $block_id );
if ( empty( $block ) || 'mesh_section' !== $block->post_type || $section->ID !== $block->post_parent ) {
continue;
}
if ( empty( $status ) ) {
$status = 'draft';
}
$updates = array(
'ID' => (int) $block_id,
'post_content' => wp_kses( $block_data['post_content'], mesh_get_allowed_html() ),
'post_status' => $status,
'menu_order' => (int) $block_data['menu_order'],
);
$updates['post_title'] = Mesh_Common::validate_section_title( $block_data['post_title'], $block_id, esc_html__( 'Column', 'mesh' ) );
wp_update_post( $updates );
$block_column_width = (int) $section_data['blocks'][ $block_id ]['column_width'];
// If we don't have a column width defined or we are using a 1 column layout clear our saved widths.
Mesh_Common::update_delete_section_meta( $block_id, '_mesh_column_width', $block_column_width );
// Save Column Offset.
$offset = ( isset( $section_data['blocks'][ $block_id ]['offset'] ) ) ? absint( $section_data['blocks'][ $block_id ]['offset'] ) : 0;
Mesh_Common::update_delete_section_meta( $block_id, '_mesh_offset', $offset );
// @todo: optimize this loop into a utility method
$block_css_class = $section_data['blocks'][ $block_id ]['css_class'];
// Save CSS Classes.
$css_classes = explode( ' ', $block_css_class );
$sanitized_css_classes = array();
foreach ( $css_classes as $css ) {
$sanitized_css_classes[] = sanitize_html_class( $css );
}
$sanitized_css_classes = implode( ' ', $sanitized_css_classes );
Mesh_Common::update_delete_section_meta( $block_id, '_mesh_css_class', $sanitized_css_classes );
// Blocks Center
if ( ! isset( $section_data['blocks'][ $block_id ]['centered'] ) ) {
$block_centered = false;
} else {
$block_centered = true;
}
Mesh_Common::update_delete_section_meta( $block_id, '_mesh_centered', $block_centered );
// Save Featured Image
$featured_image = '';
if ( isset( $block_data['featured_image'] ) && '' !== $block_data['featured_image'] ) {
$featured_image = $block_data['featured_image'];
}
Mesh_Common::update_delete_section_meta( $block_id, '_thumbnail_id', (int) $featured_image );
}
}
// If this is an AJAX request, and we are saving a section,
// we need to make sure we process the sections based on the parent.
if ( 'mesh_section' === $post->post_type ) {
$page_id = $post->post_parent;
} else {
$page_id = $post_id;
}
// Save a block's content into its section, and then into it's page.
$section_posts = mesh_get_sections( $page_id );
if ( ! empty( $section_posts ) ) {
foreach ( $section_posts as $p ) {
$section_content = array();
$blocks = mesh_get_section_blocks( $p->ID );
foreach ( $blocks as $block ) {
$block_title = strip_tags( $block->post_title );
if ( ! empty( $block_title ) ) {
$section_content[] = $block_title;
}
if ( ! empty( $block->post_content ) ) {
$section_content[] = $block->post_content;
}
}
// Update section to include its block's content.
wp_update_post( array(
'ID' => $p->ID,
'post_content' => implode( ' ', $section_content ),
) );
}
// Get the sections again.
$section_posts = mesh_get_sections( $page_id );
$page_content_sections = array();
$page_content_sections[] = '<div id="mesh-section-content">';
foreach ( $section_posts as $p ) {
if ( 'publish' !== $p->post_status ) {
continue;
}
$p_title = strip_tags( $p->post_title );
preg_match_all( $column_title_pattern, $p_title, $p_matches, PREG_SET_ORDER, 0 );
if ( ! empty( $p_title ) ) {
$page_content_sections[] = $p_title;
}
if ( ! empty( $p->post_content ) ) {
$page_content_sections[] = $p->post_content;
}
}
$page_content_sections[] = '</div>';
$current_page = get_post( $page_id );
$content = $current_page->post_content;
$pos = strpos( $content, '<div id="mesh-section-content">' );
if ( false !== $pos ) {
$content = substr( $content, 0, ( strlen( $content ) - $pos ) * - 1 );
}
wp_update_post( array(
'ID' => $page_id,
'post_content' => $content . implode( ' ', $page_content_sections ),
) );
}
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
}
/**
* When supported post type is trashed, trash its sections and blocks.
*
* @param int $post_id ID of the Post to trash.
*/
public function wp_trash_post( $post_id ) {
$post_to_trash = get_post( $post_id );
$supported_post_types = get_option( 'mesh_post_types', array() );
if ( empty( $supported_post_types[ $post_to_trash->post_type ] ) ) {
return;
}
$sections = mesh_get_sections( $post_id, 'array', array( 'any' ) );
if ( empty( $sections ) ) {
return;
}
foreach ( $sections as $section ) {
$blocks = mesh_get_sections( $section->ID, 'array', array( 'any' ) );
if ( ! empty( $blocks ) ) {
foreach ( $blocks as $block ) {
wp_trash_post( $block->ID );
}
}
wp_trash_post( $section->ID );
}
}
/**
* When a supported post type is deleted, delete its sections and blocks.
*
* @param int $post_id ID of the post to be deleted.
*/
public function before_delete_post( $post_id ) {
$post_to_delete = get_post( $post_id );
$supported_post_types = get_option( 'mesh_post_types', array() );
if ( empty( $supported_post_types[ $post_to_delete->post_type ] ) ) {
return;
}
$sections = mesh_get_sections( $post_id, 'array', array( 'publish', 'draft', 'trash' ) );
if ( empty( $sections ) ) {
return;
}
foreach ( $sections as $section ) {
$blocks = mesh_get_sections( $section->ID, 'array', array( 'publish', 'draft', 'trash' ) );
if ( ! empty( $blocks ) ) {
foreach ( $blocks as $block ) {
wp_delete_post( $block->ID );
}
}
wp_delete_post( $section->ID );
}
}
/**
* When a post is untrashed, untrash any sections or blocks that belong to it.
*
* @param int $post_id The trashed Post's ID.
*/
public function untrash_post( $post_id ) {
$trashed_post = get_post( $post_id );
$supported_post_types = get_option( 'mesh_post_types', array() );
if ( empty( $supported_post_types[ $trashed_post->post_type ] ) ) {
return;
}
$sections = mesh_get_sections( $post_id, 'array', array( 'trash' ) );
if ( empty( $sections ) ) {
return;
}
foreach ( $sections as $section ) {
$blocks = mesh_get_sections( $section->ID, 'array', array( 'trash' ) );
if ( ! empty( $blocks ) ) {
foreach ( $blocks as $block ) {
wp_untrash_post( $block->ID );
}
}
wp_untrash_post( $section->ID );
}
}
/**
* Simple loop to get our sections
*
* @param string $content Post content.
*
* @return string Return the content that has been filtered.
*/
public function the_content( $content ) {
$pos = strpos( $content, '<div id="mesh-section-content">' );
if ( false !== $pos ) {
$content = substr( $content, 0, ( strlen( $content ) - $pos ) * - 1 );
}
return $content;
}
/**
* Update post classes.
*
* Filter custom classes to section container
*
* @param array $classes CSS Class array.
*
* @return array
*/
public function post_class( $classes ) {
$custom_class = get_post_meta( get_the_ID(), '_mesh_css_class', true );
if ( ! empty( $custom_class ) ) {
$classes[] = esc_attr( $custom_class );
}
return $classes;
}
/**
* At the end of a page, let's show sections.
*
* @since 1.0
*
* @example "add_filter( 'mesh_loop_end', '__return_empty_string' );" return nothing and add my sections manually
*
* @param object $wp_query WordPress Query Object.
*/
public function loop_end( $wp_query ) {
if ( ! $wp_query->is_main_query() ) {
return;
}
if ( ! is_singular() ) {
return;
}
// Do not show content on password protected posts.
if ( post_password_required() ) {
return;
}
$sections = mesh_display_sections( $wp_query->post->ID, false );
echo apply_filters( 'mesh_loop_end', $sections, $wp_query->post->ID ); // WPCS: XSS ok, sanitization ok.
}
/**
* Enqueue Admin scripts function.
*
* @access public
* @return void
*/
public function admin_enqueue_scripts() {
global $current_screen, $post;
$mesh_screens = array( 'post', 'edit', 'settings_page_mesh' );
if ( ! in_array( $current_screen->base, $mesh_screens, true ) ) {
return;
}
if ( 'edit' === $current_screen->base && 'edit-mesh_template' !== $current_screen->id ) {
return;
}
wp_enqueue_script( 'admin-mesh', plugins_url( 'js/admin-mesh.js', __FILE__ ), array(
'jquery',
'jquery-ui-draggable',
'jquery-ui-droppable',
'jquery-ui-slider',
'wp-pointer',
), '1.0', true );
$strings = array(
'reorder_warn' => esc_html__( 'Be sure to save the order of your sections once your changes are complete.', 'mesh' ),
'description' => esc_html__( 'Mesh allow you to easily break up your page into different blocks of content/markup.', 'mesh' ),
'add_image' => esc_html__( 'Set Background Image', 'mesh' ),
'remove_image' => esc_html__( 'Remove Background', 'mesh' ),
'expand_all' => esc_html__( 'Expand All', 'mesh' ),
'collapse_all' => esc_html__( 'Collapse All', 'mesh' ),
'default_title' => esc_html__( 'No Section Title', 'mesh' ),
'select_section_bg' => esc_html__( 'Select Section Background', 'mesh' ),
'select_bg' => esc_html__( 'Select Background', 'mesh' ),
'select_block_bg' => esc_html__( 'Select Column Background', 'mesh' ),
'published' => esc_html__( 'Status: Published', 'mesh' ),
'draft' => esc_html__( 'Status: Draft', 'mesh' ),
'confirm_remove' => esc_html__( 'Are you sure you want to remove this section?', 'mesh' ),
'save_order' => esc_html__( 'Save Order', 'mesh' ),
'reorder' => esc_html__( 'Reorder Sections', 'mesh' ),
'confirm_template_section_update' => esc_html__( 'Apply template changes to posts/pages?', 'mesh' ),
);
$tinymce_defaults = mesh_get_tinymce_defaults();
$strings = apply_filters( 'mesh_strings', $strings ); // Allow filtering of localization strings.
$localized_data = array(
'post_id' => ! is_null( $post ) ? $post->ID : 0,
'post_type' => ! is_null( $post ) ? $post->post_type : $current_screen->post_type,
'site_uri' => site_url(),
'screen' => $current_screen->base,
'max_columns' => apply_filters( 'mesh_max_columns', 12 ),
'choose_layout_nonce' => wp_create_nonce( 'mesh_choose_layout_nonce' ),
'remove_section_nonce' => wp_create_nonce( 'mesh_remove_section_nonce' ),
'add_section_nonce' => wp_create_nonce( 'mesh_add_section_nonce' ),
'save_section_nonce' => wp_create_nonce( 'mesh_save_section_nonce' ),
'reorder_section_nonce' => wp_create_nonce( 'mesh_reorder_section_nonce' ),
'featured_image_nonce' => wp_create_nonce( 'mesh_featured_image_nonce' ),
'reorder_blocks_nonce' => wp_create_nonce( 'mesh_reorder_blocks_nonce' ),
'dismiss_nonce' => wp_create_nonce( 'mesh_dismiss_notification_nonce' ),
'choose_template_nonce' => wp_create_nonce( 'mesh_choose_template_nonce' ),
'content_css' => apply_filters( 'mesh_content_css', get_stylesheet_directory_uri() . '/css/admin-editor.css', 'editor_path' ),
'strings' => $strings,
'tinymce_options' => $tinymce_defaults,
);
$localized_data = apply_filters( 'mesh_data', $localized_data ); // Allow filtering of the entire localized dataset.
wp_localize_script( 'admin-mesh', 'mesh_data', $localized_data );
}
/**
* Enqueue admin styles
*
* @access public
* @return void
*/
public function admin_enqueue_styles() {
wp_enqueue_style( 'admin-mesh', plugins_url( 'css/admin-mesh.css', __FILE__ ), array(), LINCHPIN_MESH_VERSION );
}
/**