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
|
<?php
class SiteOrigin_Panels_Compat_Layout_Block { const BLOCK_NAME = 'siteorigin-panels/layout-block'; /** * Get the singleton instance * * @return SiteOrigin_Panels_Compat_Layout_Block */ public static function single() { static $single; return empty( $single ) ? $single = new self() : $single; } public function __construct() { add_action( 'init', array( $this, 'register_layout_block' ) ); // This action is slightly later than `enqueue_block_editor_assets`, // which we need to use to ensure our templates are loaded at the right time. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_layout_block_editor_assets' ) ); } public function register_layout_block() { register_block_type( self::BLOCK_NAME, array( 'render_callback' => array( $this, 'render_layout_block' ), ) ); } public function enqueue_layout_block_editor_assets() { if ( SiteOrigin_Panels_Admin::is_block_editor() ) { $panels_admin = SiteOrigin_Panels_Admin::single(); $panels_admin->enqueue_admin_scripts(); $panels_admin->enqueue_admin_styles(); $panels_admin->js_templates(); wp_enqueue_script( 'siteorigin-panels-layout-block', plugins_url( 'js/siteorigin-panels-layout-block' . SITEORIGIN_PANELS_JS_SUFFIX . '.js', __FILE__ ), array( 'wp-editor', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-compose', 'so-panels-admin' ), SITEORIGIN_PANELS_VERSION ); $current_screen = get_current_screen(); $is_panels_post_type = in_array( $current_screen->id, siteorigin_panels_setting( 'post-types' ) ); wp_localize_script( 'siteorigin-panels-layout-block', 'soPanelsBlockEditorAdmin', array( 'sanitizeUrl' => wp_nonce_url( admin_url( 'admin-ajax.php' ), 'layout-block-sanitize', '_panelsnonce' ), 'previewUrl' => wp_nonce_url( admin_url( 'admin-ajax.php' ), 'layout-block-preview', '_panelsnonce' ), 'defaultMode' => siteorigin_panels_setting( 'layout-block-default-mode' ), 'showAddButton' => $is_panels_post_type, ) ); // This is only available in WP5. if ( function_exists( 'wp_set_script_translations' ) ) { wp_set_script_translations( 'siteorigin-panels-layout-block', 'siteorigin-panels' ); } SiteOrigin_Panels_Styles::register_scripts(); wp_enqueue_script( 'siteorigin-panels-front-styles' ); // Enqueue front end scripts for our widgets bundle. if ( class_exists( 'SiteOrigin_Widgets_Bundle' ) ) { $sowb = SiteOrigin_Widgets_Bundle::single(); $sowb->register_general_scripts(); if ( method_exists( $sowb, 'enqueue_registered_widgets_scripts' ) ) { $sowb->enqueue_registered_widgets_scripts( true, false ); } } } } public function render_layout_block( $attributes ) { if ( empty( $attributes['panelsData'] ) ) { return '<div>'. __( "You need to add a widget, row, or prebuilt layout before you'll see anything here. :)", 'siteorigin-panels' ) . '</div>'; } $panels_data = $attributes['panelsData']; $panels_data = $this->sanitize_panels_data( $panels_data ); $builder_id = isset( $attributes['builder_id'] ) ? $attributes['builder_id'] : uniqid( 'gb' . get_the_ID() . '-' );
// Support for custom CSS classes $add_custom_class_name = function( $class_names ) use ($attributes) { if ( ! empty( $attributes['className'] ) ) { $class_names[] = $attributes['className']; } return $class_names; }; add_filter( 'siteorigin_panels_layout_classes', $add_custom_class_name ); $rendered_layout = SiteOrigin_Panels::renderer()->render( $builder_id, true, $panels_data ); remove_filter( 'siteorigin_panels_layout_classes', $add_custom_class_name ); return $rendered_layout; } private function sanitize_panels_data( $panels_data ) { // We force calling widgets' update functions here, but a better solution is to ensure these are called when // the block is saved, but there is currently no simple method to do so. $panels_data['widgets'] = SiteOrigin_Panels_Admin::single()->process_raw_widgets( $panels_data['widgets'], false, true ); $panels_data = SiteOrigin_Panels_Styles_Admin::single()->sanitize_all( $panels_data ); return $panels_data; } }
|