-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinsert-pages.php
1875 lines (1703 loc) · 71.1 KB
/
insert-pages.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
/**
* Plugin Name: Insert Pages
* Plugin URI: https://github.com/uhm-coe/insert-pages
* Description: Insert Pages lets you embed any WordPress content (e.g., pages, posts, custom post types) into other WordPress content using the Shortcode API.
* Author: Paul Ryan
* Text Domain: insert-pages
* Domain Path: /languages
* License: GPL2
* Requires at least: 3.3.0
* Version: 3.9.1
*
* @package insert-pages
*/
/*
Copyright 2011 Paul Ryan (email: [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Shortcode Format:
* [insert page='{slug}|{id}|{url}' display='title|link|excerpt|excerpt-only|content|title-content|post-thumbnail|all|{custom-template.php}' class='any-classes' id='any-id' [inline] [public] querystring='{url-encoded-values}' size='post-thumbnail|thumbnail|medium|large|full|{custom-size}']
*/
if ( ! class_exists( 'InsertPagesPlugin' ) ) {
/**
* Class InsertPagesPlugin
*/
class InsertPagesPlugin {
/**
* Stack tracking inserted pages (for loop detection).
*
* @var array Array of page ids inserted.
*/
protected $inserted_page_ids;
/**
* Flag to only render the TinyMCE plugin dialog once.
*
* @var boolean
*/
private static $link_dialog_printed = false;
/**
* Flag checked when rendering TinyMCE modal to ensure that required scripts
* and styles were enqueued (normally done in `admin_init` hook).
*
* @var boolean
*/
private static $is_admin_initialized = false;
/**
* Singleton plugin instance.
*
* @var object Plugin instance.
*/
protected static $instance = null;
/**
* Access this plugin's working instance.
*
* @return object Object of this class.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Disable constructor to enforce a single plugin instance..
*/
protected function __construct() {
}
/**
* Action hook: WordPress 'init'.
*
* @return void
*/
public function insert_pages_init() {
$options = get_option( 'wpip_settings' );
// Register the [insert] shortcode.
add_shortcode( 'insert', array( $this, 'insert_pages_handle_shortcode_insert' ) );
// Register the gutenberg block so we can populate it via server side
// rendering. Note: only register it once (some plugins, like Advanced
// Custom Fields, create a scenario where this init hook gets called
// multiple times).
if (
function_exists( 'register_block_type' ) &&
isset( $options['wpip_gutenberg_block'] ) &&
'enabled' === $options['wpip_gutenberg_block'] &&
class_exists( 'WP_Block_Type_Registry' ) &&
! WP_Block_Type_Registry::get_instance()->is_registered( 'insert-pages/block' )
) {
// Automatically load dependencies and version.
$asset_file = include plugin_dir_path( __FILE__ ) . 'lib/gutenberg-block/build/index.asset.php';
wp_register_script(
'insert-pages-gutenberg-block',
plugins_url( 'lib/gutenberg-block/build/index.js', __FILE__ ),
$asset_file['dependencies'],
$asset_file['version'],
array(
'in_footer' => false,
)
);
wp_register_style(
'insert-pages-gutenberg-block',
plugins_url( 'lib/gutenberg-block/build/index.css', __FILE__ ),
array(),
$asset_file['version']
);
register_block_type(
'insert-pages/block',
array(
'editor_style' => 'insert-pages-gutenberg-block',
'editor_script' => 'insert-pages-gutenberg-block',
'attributes' => array(
'url' => array(
'type' => 'string',
'default' => '',
),
'page' => array(
'type' => 'number',
'default' => 0,
),
'display' => array(
'type' => 'string',
'default' => 'title',
),
'template' => array(
'type' => 'string',
'default' => '',
),
'class' => array(
'type' => 'string',
'default' => '',
),
'id' => array(
'type' => 'string',
'default' => '',
),
'inline' => array(
'type' => 'bool',
'default' => false,
),
'public' => array(
'type' => 'bool',
'default' => false,
),
'querystring' => array(
'type' => 'string',
'default' => '',
),
'size' => array(
'type' => 'string',
'default' => '',
),
),
'render_callback' => array( $this, 'block_render_callback' ),
)
);
}
}
/**
* Renders the gutenberg block (using legacy server-side rendering).
*
* @param array $attr Array of block attributes.
* @return string Rendered inserted page.
*/
public function block_render_callback( $attr ) {
// Display attribute defaults to 'title'; otherwise it is the passed param,
// and if the display param is 'custom', it is the value of the 'template'
// param.
$display = 'title';
if ( isset( $attr['display'] ) && strlen( $attr['display'] ) > 0 ) {
$display = esc_attr( $attr['display'] );
}
if ( 'custom' === $display && isset( $attr['template'] ) && strlen( $attr['template'] ) > 0 ) {
$display = esc_attr( $attr['template'] );
}
$shortcode = sprintf(
'[insert page="%1$s" display="%2$s"%3$s%4$s%5$s%6$s%7$s%8$s]',
isset( $attr['page'] ) && strlen( $attr['page'] ) > 0 ? esc_attr( $attr['page'] ) : '0',
$display,
isset( $attr['class'] ) && strlen( $attr['class'] ) > 0 ? ' class="' . esc_attr( $attr['class'] ) . '"' : '',
isset( $attr['id'] ) && strlen( $attr['id'] ) > 0 ? ' id="' . esc_attr( $attr['id'] ) . '"' : '',
isset( $attr['querystring'] ) && strlen( $attr['querystring'] ) > 0 ? ' querystring="' . esc_attr( $attr['querystring'] ) . '"' : '',
isset( $attr['size'] ) && strlen( $attr['size'] ) > 0 ? ' size="' . esc_attr( $attr['size'] ) . '"' : '',
isset( $attr['inline'] ) && 'true' === $attr['inline'] ? ' inline' : '',
isset( $attr['public'] ) && 'true' === $attr['public'] ? ' public' : ''
);
$rendered_shortcode = do_shortcode( $shortcode );
// If we're in the block editor, enqueue any layout styles for blocks
// (normally this is done in core but since we're not in the main context,
// we need to do so manually). For example, the Grid block uses layout
// styles to set the number of columns.
// See: https://github.com/WordPress/WordPress/blob/6.6.2/wp-includes/block-supports/layout.php#L539-L551.
// See: https://developer.wordpress.org/reference/functions/wp_style_engine_get_stylesheet_from_css_rules/.
// See: https://developer.wordpress.org/reference/functions/wp_add_inline_style/.
$current_screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if (
! empty( $current_screen ) && $current_screen->is_block_editor() &&
! in_array( $display, array( 'title', 'link', 'post-thumbnail' ), true ) &&
function_exists( 'wp_style_engine_get_stylesheet_from_context' )
) {
$layout_styles = wp_style_engine_get_stylesheet_from_context( 'block-supports' );
if ( ! empty( $layout_styles ) ) {
wp_add_inline_style( 'wp-block-library', $layout_styles );
}
}
return $rendered_shortcode;
}
/**
* Load gutenberg block resources only when editing (only if Gutenberg block
* setting is enabled in Insert Pages settings).
*
* Action hook: enqueue_block_editor_assets
*
* @return void
*/
public function insert_pages_enqueue_block_editor_assets() {
$options = get_option( 'wpip_settings' );
if ( isset( $options['wpip_gutenberg_block'] ) && 'enabled' === $options['wpip_gutenberg_block'] ) {
}
}
/**
* Action hook: WordPress 'admin_init'.
*
* @return void
*/
public function insert_pages_admin_init() {
// Get options set in WordPress dashboard (Settings > Insert Pages).
$options = get_option( 'wpip_settings' );
if ( false === $options || ! is_array( $options ) || ! array_key_exists( 'wpip_format', $options ) || ! array_key_exists( 'wpip_wrapper', $options ) || ! array_key_exists( 'wpip_insert_method', $options ) || ! array_key_exists( 'wpip_tinymce_filter', $options ) ) {
$options = wpip_set_defaults();
}
// Register the TinyMCE toolbar button script.
wp_enqueue_script(
'wpinsertpages',
plugins_url( '/js/wpinsertpages.js', __FILE__ ),
array( 'wpdialogs' ),
'20221216',
false
);
wp_localize_script(
'wpinsertpages',
'wpInsertPagesL10n',
array(
'update' => __( 'Update', 'insert-pages' ),
'save' => __( 'Insert Page', 'insert-pages' ),
'noTitle' => __( '(no title)', 'insert-pages' ),
'noMatchesFound' => __( 'No matches found.', 'insert-pages' ),
'l10n_print_after' => 'try{convertEntities(wpInsertPagesL10n);}catch(e){};',
'format' => $options['wpip_format'],
'private' => __( 'Private', 'insert-pages' ),
'tinymce_state' => $this->get_tinymce_state(),
)
);
// Register the TinyMCE toolbar button styles.
wp_enqueue_style(
'wpinsertpagescss',
plugins_url( '/css/wpinsertpages.css', __FILE__ ),
array( 'wp-jquery-ui-dialog' ),
'20221216'
);
/**
* Register TinyMCE plugin for the toolbar button in normal mode (register
* TinyMCE plugin filters below before plugins_loaded in compatibility
* mode, to work around a SiteOrigin PageBuilder bug).
*
* @see https://wordpress.org/support/topic/button-in-the-toolbar-of-tinymce-disappear-conflict-page-builder/
*/
if ( 'normal' === $options['wpip_tinymce_filter'] ) {
add_filter( 'mce_external_plugins', array( $this, 'insert_pages_handle_filter_mce_external_plugins' ) );
add_filter( 'mce_buttons', array( $this, 'insert_pages_handle_filter_mce_buttons' ) );
}
// Load the translations.
load_plugin_textdomain(
'insert-pages',
false,
plugin_basename( __DIR__ ) . '/languages'
);
self::$is_admin_initialized = true;
}
/**
* Shortcode hook: Replace the [insert ...] shortcode with the inserted page's content.
*
* @param array $atts Shortcode attributes.
* @param string $content Content to replace shortcode.
* @return string Content to replace shortcode.
*/
public function insert_pages_handle_shortcode_insert( $atts, $content = null ) {
global $wp_query, $post, $wp_current_filter;
// Shortcode attributes.
$attributes = shortcode_atts(
array(
'page' => '0',
'display' => 'all',
'class' => '',
'id' => '',
'querystring' => '',
'size' => '',
'inline' => false,
'public' => false,
),
$atts,
'insert'
);
// Validation checks.
if ( '0' === $attributes['page'] ) {
return $content;
}
// Short circuit if trying to embed same page in itself.
if (
( is_object( $post ) && property_exists( $post, 'ID' ) && intval( $attributes['page'] ) === $post->ID ) ||
( is_object( $post ) && property_exists( $post, 'post_name' ) && $attributes['page'] === $post->post_name ) ||
( is_int( $post ) && intval( $attributes['page'] ) === $post )
) {
return $content;
}
// Get options set in WordPress dashboard (Settings > Insert Pages).
$options = get_option( 'wpip_settings' );
if ( false === $options || ! is_array( $options ) || ! array_key_exists( 'wpip_format', $options ) || ! array_key_exists( 'wpip_wrapper', $options ) || ! array_key_exists( 'wpip_insert_method', $options ) || ! array_key_exists( 'wpip_tinymce_filter', $options ) ) {
$options = wpip_set_defaults();
}
$attributes['inline'] = ( false !== $attributes['inline'] && 'false' !== $attributes['inline'] ) || array_search( 'inline', $atts, true ) === 0 || ( array_key_exists( 'wpip_wrapper', $options ) && 'inline' === $options['wpip_wrapper'] );
/**
* Filter the flag indicating whether to wrap the inserted content in inline tags (span).
*
* @param bool $use_inline_wrapper Indicates whether to wrap the content in span tags.
*/
$attributes['inline'] = apply_filters( 'insert_pages_use_inline_wrapper', $attributes['inline'] );
$attributes['wrapper_tag'] = $attributes['inline'] ? 'span' : 'div';
$attributes['public'] = ( false !== $attributes['public'] && 'false' !== $attributes['public'] ) || array_search( 'public', $atts, true ) === 0 || is_user_logged_in();
/**
* Filter the querystring values applied to every inserted page. Useful
* for admins who want to provide the same querystring value to all
* inserted pages sitewide.
*
* @since 3.2.9
*
* @param string $querystring The querystring value for the inserted page.
*/
$attributes['querystring'] = apply_filters(
'insert_pages_override_querystring',
str_replace( '{', '[', str_replace( '}', ']', htmlspecialchars_decode( $attributes['querystring'] ) ) )
);
$attributes['should_apply_the_content_filter'] = true;
/**
* Filter the flag indicating whether to apply the_content filter to post
* contents and excerpts that are being inserted.
*
* @param bool $apply_the_content_filter Indicates whether to apply the_content filter.
*/
$attributes['should_apply_the_content_filter'] = apply_filters( 'insert_pages_apply_the_content_filter', $attributes['should_apply_the_content_filter'] );
// Disable the_content filter if using inline tags, since wpautop
// inserts p tags and we can't have any inside inline elements.
if ( $attributes['inline'] ) {
$attributes['should_apply_the_content_filter'] = false;
}
/**
* Filter the chosen display method, where display can be one of:
* title, link, excerpt, excerpt-only, content, post-thumbnail, all, {custom-template.php}
* Useful for admins who want to restrict the display sitewide.
*
* @since 3.2.7
*
* @param string $display The display method for the inserted page.
*/
$attributes['display'] = apply_filters( 'insert_pages_override_display', $attributes['display'] );
// If a URL is provided, translate it to a post ID.
if ( filter_var( $attributes['page'], FILTER_VALIDATE_URL ) ) {
$attributes['page'] = url_to_postid( $attributes['page'] );
}
// Get the WP_Post object from the provided slug, or ID.
if ( ! is_numeric( $attributes['page'] ) ) {
// Get list of post types that can be inserted (page, post, custom
// types), excluding builtin types (nav_menu_item, attachment).
$insertable_post_types = array_filter(
get_post_types(),
array( $this, 'is_post_type_insertable' )
);
$inserted_page = get_page_by_path( $attributes['page'], OBJECT, $insertable_post_types );
// If get_page_by_path() didn't find the page, check to see if the slug
// was provided instead of the full path (useful for hierarchical pages
// that are nested under another page).
if ( is_null( $inserted_page ) ) {
global $wpdb;
$page = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE post_name = %s AND (post_status = 'publish' OR post_status = 'private') LIMIT 1",
$attributes['page']
)
);
if ( $page ) {
$inserted_page = get_post( $page );
}
}
$attributes['page'] = $inserted_page ? $inserted_page->ID : $attributes['page'];
} else {
$inserted_page = get_post( intval( $attributes['page'] ) );
}
// Integration: If WPML is enabled, ensure the inserted page matches the
// language of the parent page.
if ( is_object( $inserted_page ) && class_exists( 'Sitepress' ) ) {
$translated_inserted_page = apply_filters( 'wpml_object_id', $inserted_page->ID, 'any' );
if ( ! empty( $translated_inserted_page ) && intval( $translated_inserted_page ) !== $inserted_page->ID ) {
$inserted_page = get_post( intval( $translated_inserted_page ) );
}
}
// Prevent unprivileged users from inserting private posts from others.
if ( is_object( $inserted_page ) && 'publish' !== $inserted_page->post_status ) {
$post_type = get_post_type_object( $inserted_page->post_type );
$parent_post_author_id = intval( get_the_author_meta( 'ID' ) );
if ( ! user_can( $parent_post_author_id, $post_type->cap->read_post, $inserted_page->ID ) ) {
$inserted_page = null;
}
}
// If inserted page's status is private, don't show to anonymous users
// unless 'public' option is set.
if ( is_object( $inserted_page ) && 'private' === $inserted_page->post_status && ! $attributes['public'] ) {
$inserted_page = null;
}
// Integration: if Simple Membership plugin is used, check that the
// current user has permission to see the inserted post.
// See: https://simple-membership-plugin.com/simple-membership-miscellaneous-php-tweaks/
if ( class_exists( 'SwpmAccessControl' ) ) {
$access_ctrl = SwpmAccessControl::get_instance();
if ( ! $access_ctrl->can_i_read_post( $inserted_page ) && ! current_user_can( 'edit_files' ) ) {
$inserted_page = null;
$content = wp_kses_post( $access_ctrl->why() );
}
}
// Loop detection: check if the page we are inserting has already been
// inserted; if so, short circuit here.
if ( ! is_array( $this->inserted_page_ids ) ) {
// Initialize stack to the main page that contains inserted page(s).
$this->inserted_page_ids = array( get_the_ID() );
}
if ( isset( $inserted_page->ID ) ) {
if ( ! in_array( $inserted_page->ID, $this->inserted_page_ids, true ) ) {
// Add the page being inserted to the stack.
$this->inserted_page_ids[] = $inserted_page->ID;
} else {
// Loop detected, so exit without rendering this post.
return $content;
}
}
// Set any querystring params included in the shortcode.
parse_str( $attributes['querystring'], $querystring );
$original_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification
$original_request = $_REQUEST; // phpcs:ignore WordPress.Security.NonceVerification
foreach ( $querystring as $param => $value ) {
$_GET[ $param ] = $value;
$_REQUEST[ $param ] = $value;
}
$original_wp_query_vars = $GLOBALS['wp']->query_vars;
if (
! empty( $querystring ) &&
isset( $GLOBALS['wp'] ) &&
method_exists( $GLOBALS['wp'], 'parse_request' ) &&
empty( $GLOBALS['wp']->query_vars['rest_route'] )
) {
$GLOBALS['wp']->parse_request( $querystring );
}
// Use "Normal" insert method (get_post).
if ( 'legacy' !== $options['wpip_insert_method'] ) {
// If we couldn't retrieve the page, fire the filter hook showing a not-found message.
if ( null === $inserted_page ) {
/**
* Filter the html that should be displayed if an inserted page was not found.
*
* @param string $content html to be displayed. Defaults to an empty string.
*/
$content = apply_filters( 'insert_pages_not_found_message', $content );
// Short-circuit since we didn't find the page.
return $content;
}
// Start output buffering so we can save the output to a string.
ob_start();
// If Beaver Builder, SiteOrigin Page Builder, Elementor, or WPBakery
// Page Builder (Visual Composer) are enabled, load any cached styles
// associated with the inserted page.
// Note: Temporarily set the global $post->ID to the inserted page ID,
// since both builders rely on the id to load the appropriate styles.
if (
class_exists( 'UAGB_Post_Assets' ) ||
class_exists( 'FLBuilder' ) ||
class_exists( 'SiteOrigin_Panels' ) ||
class_exists( '\Elementor\Post_CSS_File' ) ||
defined( 'VCV_VERSION' ) ||
defined( 'WPB_VC_VERSION' )
) {
// If we're not in The Loop (i.e., global $post isn't assigned),
// temporarily populate it with the post to be inserted so we can
// retrieve generated styles for that post. Reset $post to null
// after we're done.
if ( is_null( $post ) ) {
$old_post_id = null;
$post = $inserted_page; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
} elseif ( is_int( $post ) ) {
$old_post_id = $post;
$post = $inserted_page->ID;
} else {
$old_post_id = $post->ID;
$post->ID = $inserted_page->ID;
}
// Enqueue assets for Ultimate Addons for Gutenberg.
// See: https://ultimategutenberg.com/docs/assets-api-third-party-plugins/.
if ( class_exists( 'UAGB_Post_Assets' ) ) {
$post_assets_instance = new UAGB_Post_Assets( $inserted_page->ID );
$post_assets_instance->enqueue_scripts();
}
if ( class_exists( 'FLBuilder' ) ) {
FLBuilder::enqueue_layout_styles_scripts( $inserted_page->ID );
}
if ( class_exists( 'SiteOrigin_Panels' ) ) {
$renderer = SiteOrigin_Panels::renderer();
$renderer->add_inline_css( $inserted_page->ID, $renderer->generate_css( $inserted_page->ID ) );
}
if ( class_exists( '\Elementor\Post_CSS_File' ) ) {
$css_file = new \Elementor\Post_CSS_File( $inserted_page->ID );
$css_file->enqueue();
}
// Enqueue custom style from WPBakery Page Builder (Visual Composer).
if ( defined( 'VCV_VERSION' ) ) {
wp_enqueue_style( 'vcv:assets:front:style' );
wp_enqueue_script( 'vcv:assets:runtime:script' );
wp_enqueue_script( 'vcv:assets:front:script' );
$bundle_url = get_post_meta( $inserted_page->ID, 'vcvSourceCssFileUrl', true );
if ( $bundle_url ) {
$version = get_post_meta( $inserted_page->ID, 'vcvSourceCssFileHash', true );
if ( ! preg_match( '/^http/', $bundle_url ) ) {
if ( ! preg_match( '/assets-bundles/', $bundle_url ) ) {
$bundle_url = '/assets-bundles/' . $bundle_url;
}
}
if ( preg_match( '/^http/', $bundle_url ) ) {
$bundle_url = set_url_scheme( $bundle_url );
} elseif ( defined( 'VCV_TF_ASSETS_IN_UPLOADS' ) && constant( 'VCV_TF_ASSETS_IN_UPLOADS' ) ) {
$upload_dir = wp_upload_dir();
$bundle_url = set_url_scheme( $upload_dir['baseurl'] . '/' . VCV_PLUGIN_ASSETS_DIRNAME . '/' . ltrim( $bundle_url, '/\\' ) );
} elseif ( class_exists( 'VisualComposer\Helpers\AssetsEnqueue' ) ) {
// These methods should work for Visual Composer 26.0.
// Enqueue custom css/js stored in vcvSourceAssetsFiles postmeta.
$vc = new \VisualComposer\Helpers\AssetsEnqueue;
if ( method_exists( $vc, 'enqueueAssets' ) ) {
$vc->enqueueAssets($inserted_page->ID);
}
// Enqueue custom CSS stored in vcvSourceCssFileUrl postmeta.
$upload_dir = wp_upload_dir();
$bundle_url = set_url_scheme( $upload_dir['baseurl'] . '/' . VCV_PLUGIN_ASSETS_DIRNAME . '/' . ltrim( $bundle_url, '/\\' ) );
} else {
$bundle_url = content_url() . '/' . VCV_PLUGIN_ASSETS_DIRNAME . '/' . ltrim( $bundle_url, '/\\' );
}
wp_enqueue_style(
'vcv:assets:source:main:styles:' . sanitize_title( $bundle_url ),
$bundle_url,
array(),
VCV_VERSION . '.' . $version
);
}
}
// Visual Composer custom CSS.
if ( defined( 'WPB_VC_VERSION' ) ) {
// Post custom CSS.
$post_custom_css = get_post_meta( $inserted_page->ID, '_wpb_post_custom_css', true );
if ( ! empty( $post_custom_css ) ) {
echo '<style type="text/css" data-type="vc_custom-css">';
echo esc_html( wp_strip_all_tags( $post_custom_css ) );
echo '</style>';
}
// Shortcodes custom CSS.
$shortcodes_custom_css = get_post_meta( $inserted_page->ID, '_wpb_shortcodes_custom_css', true );
if ( ! empty( $shortcodes_custom_css ) ) {
echo '<style type="text/css" data-type="vc_shortcodes-custom-css">';
echo esc_html( wp_strip_all_tags( $shortcodes_custom_css ) );
echo '</style>';
}
}
// GoodLayers page builder content (retrieved from post meta).
// See: https://docs.goodlayers.com/add-page-builder-in-product/.
do_action( 'gdlr_core_print_page_builder' );
if ( is_null( $old_post_id ) ) {
$post = null; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
} else {
$post->ID = $old_post_id;
}
}
/**
* Show either the title, link, content, everything, or everything via a
* custom template.
*
* Note: if the sharing_display filter exists, it means Jetpack is
* installed and Sharing is enabled; this plugin conflicts with Sharing,
* because Sharing assumes the_content and the_excerpt filters are only
* getting called once. The fix here is to disable processing of filters
* on the_content in the inserted page.
*
* @see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
*/
switch ( $attributes['display'] ) {
case 'title':
$title_tag = $attributes['inline'] ? 'span' : 'h1';
echo wp_kses_post( "<$title_tag class='insert-page-title'>" );
echo esc_html( get_the_title( $inserted_page->ID ) );
echo wp_kses_post( "</$title_tag>" );
break;
case 'title-content':
// Title.
$title_tag = $attributes['inline'] ? 'span' : 'h1';
echo wp_kses_post( "<$title_tag class='insert-page-title'>" );
echo esc_html( get_the_title( $inserted_page->ID ) );
echo wp_kses_post( "</$title_tag>" );
// Content.
// If Elementor is installed, try to render the page with it. If there is no Elementor content, fall back to normal rendering.
if ( class_exists( '\Elementor\Plugin' ) ) {
$elementor_content = \Elementor\Plugin::$instance->frontend->get_builder_content( $inserted_page->ID );
if ( strlen( $elementor_content ) > 0 ) {
echo $elementor_content;
break;
}
}
// Render the content normally.
$content = get_post_field( 'post_content', $inserted_page->ID );
if ( $attributes['should_apply_the_content_filter'] ) {
$content = apply_filters( 'the_content', $content );
}
echo $content;
break;
case 'link':
?><a href="<?php echo esc_url( get_permalink( $inserted_page->ID ) ); ?>"><?php echo get_the_title( $inserted_page->ID ); ?></a>
<?php
break;
case 'excerpt':
?><h1><a href="<?php echo esc_url( get_permalink( $inserted_page->ID ) ); ?>"><?php echo get_the_title( $inserted_page->ID ); ?></a></h1>
<?php
echo $this->insert_pages_trim_excerpt( get_post_field( 'post_excerpt', $inserted_page->ID ), $inserted_page->ID, $attributes['should_apply_the_content_filter'] );
break;
case 'excerpt-only':
echo $this->insert_pages_trim_excerpt( get_post_field( 'post_excerpt', $inserted_page->ID ), $inserted_page->ID, $attributes['should_apply_the_content_filter'] );
break;
case 'content':
// If Elementor is installed, try to render the page with it. If there is no Elementor content, fall back to normal rendering.
if ( class_exists( '\Elementor\Plugin' ) ) {
$elementor_content = \Elementor\Plugin::$instance->frontend->get_builder_content( $inserted_page->ID );
if ( strlen( $elementor_content ) > 0 ) {
echo $elementor_content;
break;
}
}
// Render the content normally.
$content = get_post_field( 'post_content', $inserted_page->ID );
if ( $attributes['should_apply_the_content_filter'] ) {
$content = apply_filters( 'the_content', $content );
}
echo $content;
break;
case 'post-thumbnail':
$size = empty( $attributes['size'] ) ? 'post-thumbnail' : $attributes['size'];
?><a href="<?php echo esc_url( get_permalink( $inserted_page->ID ) ); ?>"><?php echo get_the_post_thumbnail( $inserted_page->ID, $size ); ?></a>
<?php
break;
case 'all':
// Title.
$title_tag = $attributes['inline'] ? 'span' : 'h1';
echo wp_kses_post( "<$title_tag class='insert-page-title'>" );
echo esc_html( get_the_title( $inserted_page->ID ) );
echo wp_kses_post( "</$title_tag>" );
// Content.
$content = get_post_field( 'post_content', $inserted_page->ID );
if ( $attributes['should_apply_the_content_filter'] ) {
$content = apply_filters( 'the_content', $content );
}
echo $content;
$this->the_meta( $inserted_page->ID );
break;
default: // Display is either invalid, or contains a template file to use.
/**
* Legacy/compatibility code: In order to use custom templates,
* we use query_posts() to provide the template with the global
* state it requires for the inserted page (in other words, all
* template tags will work with respect to the inserted page
* instead of the parent page / main loop). Note that this may
* cause some compatibility issues with other plugins.
*
* @see https://codex.wordpress.org/Function_Reference/query_posts
*/
if ( is_numeric( $attributes['page'] ) ) {
$args = array(
'p' => intval( $attributes['page'] ),
'post_type' => get_post_types(),
);
} else {
$args = array(
'name' => esc_attr( $attributes['page'] ),
'post_type' => get_post_types(),
);
}
// We save the previous query state here instead of using
// wp_reset_query() because wp_reset_query() only has a single stack
// variable ($GLOBALS['wp_the_query']). This allows us to support
// pages inserted into other pages (multiple nested pages).
$old_query = $GLOBALS['wp_query'];
$inserted_page = query_posts( $args );
if ( have_posts() ) {
$template = locate_template( $attributes['display'] );
// Only allow templates that don't have any directory traversal in
// them (to prevent including php files that aren't in the active
// theme directory or the /wp-includes/theme-compat/ directory).
$path_in_theme_or_childtheme_or_compat = (
// Template is in current theme folder.
0 === strpos( realpath( $template ), realpath( get_stylesheet_directory() ) ) ||
// Template is in current or parent theme folder.
0 === strpos( realpath( $template ), realpath( get_template_directory() ) ) ||
// Template is in theme-compat folder.
0 === strpos( realpath( $template ), realpath( ABSPATH . WPINC . '/theme-compat/' ) )
);
if ( strlen( $template ) > 0 && $path_in_theme_or_childtheme_or_compat ) {
include $template; // Execute the template code.
} else { // Bad path, so fall back to printing a link to the page.
the_post();
?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
}
}
// Restore previous query and update the global template variables.
$GLOBALS['wp_query'] = $old_query; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
wp_reset_postdata();
}
// Save output buffer contents.
$content = ob_get_clean();
} else { // Use "Legacy" insert method (query_posts).
// Construct query_posts arguments.
if ( is_numeric( $attributes['page'] ) ) {
$args = array(
'p' => intval( $attributes['page'] ),
'post_type' => get_post_types(),
'post_status' => $attributes['public'] ? array( 'publish', 'private' ) : array( 'publish' ),
);
} else {
$args = array(
'name' => esc_attr( $attributes['page'] ),
'post_type' => get_post_types(),
'post_status' => $attributes['public'] ? array( 'publish', 'private' ) : array( 'publish' ),
);
}
// We save the previous query state here instead of using
// wp_reset_query() because wp_reset_query() only has a single stack
// variable ($GLOBALS['wp_the_query']). This allows us to support
// pages inserted into other pages (multiple nested pages).
$old_query = $GLOBALS['wp_query'];
$posts = query_posts( $args );
// Prevent unprivileged users from inserting private posts from others.
if ( have_posts() ) {
$can_read = true;
$parent_post_author_id = intval( get_the_author_meta( 'ID' ) );
foreach ( $posts as $post ) {
if ( is_object( $post ) && 'publish' !== $post->post_status ) {
$post_type = get_post_type_object( $post->post_type );
if ( ! user_can( $parent_post_author_id, $post_type->cap->read_post, $post->ID ) ) {
$can_read = false;
}
}
}
if ( ! $can_read ) {
// Force an empty query so we don't show any posts.
$posts = query_posts( array( 'post__in' => array( 0 ) ) );
}
}
if ( have_posts() ) {
// Start output buffering so we can save the output to string.
ob_start();
// If Beaver Builder, SiteOrigin Page Builder, Elementor, or WPBakery
// Page Builder (Visual Composer) are enabled, load any cached styles
// associated with the inserted page.
// Note: Temporarily set the global $post->ID to the inserted page ID,
// since both builders rely on the id to load the appropriate styles.
if (
class_exists( 'UAGB_Post_Assets' ) ||
class_exists( 'FLBuilder' ) ||
class_exists( 'SiteOrigin_Panels' ) ||
class_exists( '\Elementor\Post_CSS_File' ) ||
defined( 'VCV_VERSION' ) ||
defined( 'WPB_VC_VERSION' )
) {
// If we're not in The Loop (i.e., global $post isn't assigned),
// temporarily populate it with the post to be inserted so we can
// retrieve generated styles for that post. Reset $post to null
// after we're done.
if ( is_null( $post ) ) {
$old_post_id = null;
$post = $inserted_page; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
} elseif ( is_int( $post ) ) {
$old_post_id = $post;
$post = $inserted_page->ID;
} else {
$old_post_id = $post->ID;
$post->ID = $inserted_page->ID;
}
// Enqueue assets for Ultimate Addons for Gutenberg.
// See: https://ultimategutenberg.com/docs/assets-api-third-party-plugins/.
if ( class_exists( 'UAGB_Post_Assets' ) ) {
$post_assets_instance = new UAGB_Post_Assets( $inserted_page->ID );
$post_assets_instance->enqueue_scripts();
}
if ( class_exists( 'FLBuilder' ) ) {
FLBuilder::enqueue_layout_styles_scripts( $inserted_page->ID );
}
if ( class_exists( 'SiteOrigin_Panels' ) ) {
$renderer = SiteOrigin_Panels::renderer();
$renderer->add_inline_css( $inserted_page->ID, $renderer->generate_css( $inserted_page->ID ) );
}
if ( class_exists( '\Elementor\Post_CSS_File' ) ) {
$css_file = new \Elementor\Post_CSS_File( $inserted_page->ID );
$css_file->enqueue();
}
// Enqueue custom style from WPBakery Page Builder (Visual Composer).
if ( defined( 'VCV_VERSION' ) ) {
wp_enqueue_style( 'vcv:assets:front:style' );
wp_enqueue_script( 'vcv:assets:runtime:script' );
wp_enqueue_script( 'vcv:assets:front:script' );
$bundle_url = get_post_meta( $inserted_page->ID, 'vcvSourceCssFileUrl', true );
if ( $bundle_url ) {
$version = get_post_meta( $inserted_page->ID, 'vcvSourceCssFileHash', true );
if ( ! preg_match( '/^http/', $bundle_url ) ) {
if ( ! preg_match( '/assets-bundles/', $bundle_url ) ) {
$bundle_url = '/assets-bundles/' . $bundle_url;
}
}
if ( preg_match( '/^http/', $bundle_url ) ) {
$bundle_url = set_url_scheme( $bundle_url );
} elseif ( defined( 'VCV_TF_ASSETS_IN_UPLOADS' ) && constant( 'VCV_TF_ASSETS_IN_UPLOADS' ) ) {
$upload_dir = wp_upload_dir();
$bundle_url = set_url_scheme( $upload_dir['baseurl'] . '/' . VCV_PLUGIN_ASSETS_DIRNAME . '/' . ltrim( $bundle_url, '/\\' ) );
} elseif ( class_exists( 'VisualComposer\Helpers\AssetsEnqueue' ) ) {
// These methods should work for Visual Composer 26.0.
// Enqueue custom css/js stored in vcvSourceAssetsFiles postmeta.
$vc = new \VisualComposer\Helpers\AssetsEnqueue;
if ( method_exists( $vc, 'enqueueAssets' ) ) {
$vc->enqueueAssets( $inserted_page->ID );
}
// Enqueue custom CSS stored in vcvSourceCssFileUrl postmeta.
$upload_dir = wp_upload_dir();
$bundle_url = set_url_scheme( $upload_dir['baseurl'] . '/' . VCV_PLUGIN_ASSETS_DIRNAME . '/' . ltrim( $bundle_url, '/\\' ) );
} else {
$bundle_url = content_url() . '/' . VCV_PLUGIN_ASSETS_DIRNAME . '/' . ltrim( $bundle_url, '/\\' );
}
wp_enqueue_style(
'vcv:assets:source:main:styles:' . sanitize_title( $bundle_url ),
$bundle_url,
array(),
VCV_VERSION . '.' . $version
);
}
}
// Visual Composer custom CSS.
if ( defined( 'WPB_VC_VERSION' ) ) {
// Post custom CSS.
$post_custom_css = get_post_meta( $inserted_page->ID, '_wpb_post_custom_css', true );
if ( ! empty( $post_custom_css ) ) {
$post_custom_css = wp_strip_all_tags( $post_custom_css );
echo '<style type="text/css" data-type="vc_custom-css">';
echo $post_custom_css;
echo '</style>';
}
// Shortcodes custom CSS.
$shortcodes_custom_css = get_post_meta( $inserted_page->ID, '_wpb_shortcodes_custom_css', true );
if ( ! empty( $shortcodes_custom_css ) ) {
$shortcodes_custom_css = wp_strip_all_tags( $shortcodes_custom_css );
echo '<style type="text/css" data-type="vc_shortcodes-custom-css">';
echo $shortcodes_custom_css;
echo '</style>';
}
}
// GoodLayers page builder content (retrieved from post meta).
// See: https://docs.goodlayers.com/add-page-builder-in-product/.
do_action( 'gdlr_core_print_page_builder' );
if ( is_null( $old_post_id ) ) {
$post = null; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
} elseif ( is_int( $post ) ) {
$post = $old_post_id;
} else {
$post->ID = $old_post_id;
}
}
/**
* Show either the title, link, content, everything, or everything via a
* custom template.
*
* Note: if the sharing_display filter exists, it means Jetpack is
* installed and Sharing is enabled; this plugin conflicts with Sharing,
* because Sharing assumes the_content and the_excerpt filters are only
* getting called once. The fix here is to disable processing of filters
* on the_content in the inserted page.
*
* @see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
*/