PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.1.36
Gallery : FooGallery v3.1.36
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / gutenberg / class-foogallery-blocks.php
foogallery / gutenberg Last commit date
assets 2 months ago block.json 2 months ago class-foogallery-blocks.php 2 months ago class-foogallery-gutenberg.php 2 months ago class-foogallery-rest-routes.php 2 months ago
class-foogallery-blocks.php
287 lines
1 <?php
2 /**
3 * FooGallery Blocks Initializer
4 *
5 * Enqueue CSS/JS of all the FooGallery blocks.
6 *
7 * @since 1.0.0
8 * @package CGB
9 */
10
11 if ( ! class_exists( 'FooGallery_Blocks' ) ) {
12 class FooGallery_Blocks {
13
14 function __construct() {
15 //Backend editor block assets.
16 //add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
17 add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_editor_assets' ) );
18
19 add_action( 'init', array( $this, 'php_block_init' ) );
20
21 add_filter( 'foogallery_build_container_data_options', array( $this, 'add_data_options_for_block_editor' ), 10, 3 );
22 }
23
24 /**
25 * Enqueue Gutenberg block assets for backend editor.
26 *
27 * `wp-blocks`: includes block type registration and related functions.
28 * `wp-element`: includes the WordPress Element abstraction for describing the structure of your blocks.
29 * `wp-i18n`: To internationalize the block's text.
30 *
31 * @since 1.0.0
32 */
33 function enqueue_block_editor_assets() {
34
35 if ( !apply_filters( 'foogallery_gutenberg_enabled', true ) ) {
36 return;
37 }
38
39 if ( !is_admin() ) {
40 return;
41 }
42
43 //enqueue foogallery dependencies
44 wp_enqueue_script( 'lodash' );
45 wp_enqueue_script( 'masonry' );
46 foogallery_enqueue_core_gallery_template_script( array('jquery', 'masonry' ) );
47 foogallery_enqueue_core_gallery_template_style();
48
49 $path = FOOGALLERY_PATH . 'gutenberg/assets/blocks';
50 $url = FOOGALLERY_URL . 'gutenberg/assets/blocks';
51 $asset = require( $path . '.asset.php' );
52 if ( ! is_array( $asset ) ) {
53 $asset = array(
54 'dependencies' => array(),
55 'version' => false,
56 );
57 }
58
59 if ( isset( $asset['dependencies'] ) && is_array( $asset['dependencies'] ) ){
60 $asset['dependencies'][] = 'foogallery-core';
61 }
62
63 // Scripts.
64 wp_enqueue_script(
65 'foogallery-block-js', // Handle.
66 $url . '.js', // Block.build.js: We register the block here. Built with Webpack.
67 $asset[ 'dependencies' ], // Dependencies, defined above.
68 $asset[ 'version' ],
69 true // Enqueue the script in the footer.
70 );
71
72 // Styles.
73 wp_enqueue_style(
74 'foogallery-block-editor-css', // Handle.
75 $url . '.css', // Block editor CSS.
76 array( 'dashicons', 'wp-components', 'wp-edit-blocks', 'foogallery-core' ), // Dependency to include the CSS after it.
77 $asset[ 'version' ]
78 );
79
80 if ( function_exists( 'wp_set_script_translations' ) ) {
81 wp_set_script_translations( 'foogallery-block-js', 'foogallery' );
82 }
83
84 $block_js_data = apply_filters('foogallery_gutenberg_block_js_data', array(
85 "editGalleryUrl" => $this->get_edit_gallery_url(),
86 "dynamicOptions" => $this->get_dynamic_gallery_options()
87 ));
88
89 $inline_script = 'if ( typeof window.lodash === "undefined" && typeof window._ !== "undefined" ) { window.lodash = window._; }';
90 $inline_script .= PHP_EOL . 'if ( typeof window._ === "undefined" && typeof window.lodash !== "undefined" ) { window._ = window.lodash; }';
91 $inline_script .= PHP_EOL . 'window.FOOGALLERY_BLOCK = ' . json_encode( $block_js_data ) . ';';
92
93 wp_add_inline_script(
94 'foogallery-block-js',
95 $inline_script,
96 'before'
97 );
98 }
99
100
101 /**
102 * Get options used by the dynamic block inspector controls.
103 *
104 * @return array
105 */
106 function get_dynamic_gallery_options() {
107 $templates = array();
108 $gallery_templates = foogallery_gallery_templates();
109
110 if ( is_array( $gallery_templates ) ) {
111 foreach ( $gallery_templates as $gallery_template ) {
112 if ( ! is_array( $gallery_template ) || ! isset( $gallery_template['slug'] ) ) {
113 continue;
114 }
115
116 $slug = sanitize_key( $gallery_template['slug'] );
117 if ( empty( $slug ) ) {
118 continue;
119 }
120
121 $templates[ $slug ] = isset( $gallery_template['name'] ) ? sanitize_text_field( $gallery_template['name'] ) : $slug;
122 }
123 }
124
125 $default_template = foogallery_default_gallery_template();
126 if ( empty( $default_template ) || ! isset( $templates[ $default_template ] ) ) {
127 $default_template = 'default';
128 }
129
130 $lightboxes = foogallery_gallery_template_field_lightbox_choices();
131 if ( ! is_array( $lightboxes ) ) {
132 $lightboxes = array();
133 }
134
135 return array(
136 'templates' => $templates,
137 'lightboxes' => $lightboxes,
138 'defaultTemplate' => $default_template,
139 );
140 }
141
142 function get_edit_gallery_url() {
143 $post_type_object = get_post_type_object( "foogallery" );
144 if ( !$post_type_object )
145 return '';
146
147 if ( $post_type_object->_edit_link ) {
148 $link = admin_url( $post_type_object->_edit_link . '&action=edit' );
149 } else {
150 $link = '';
151 }
152
153 return apply_filters( 'foogallery_gutenberg_edit_gallery_url', $link );
154 }
155
156 /**
157 * Register our block and shortcode.
158 */
159 function php_block_init() {
160 if ( !apply_filters( 'foogallery_gutenberg_enabled', true ) ) {
161 return;
162 }
163
164 //get out quickly if no Gutenberg
165 if ( !function_exists( 'register_block_type' ) ) {
166 return;
167 }
168
169 if ( function_exists( 'register_block_type_from_metadata' ) ) {
170 register_block_type_from_metadata(
171 FOOGALLERY_PATH . 'gutenberg',
172 array(
173 'render_callback' => array( $this, 'render_block' ),
174 )
175 );
176 } else {
177 // Register our block, and explicitly define the attributes we accept.
178 register_block_type(
179 'fooplugins/foogallery', array(
180 'attributes' => array(
181 'id' => array(
182 'type' => 'number',
183 'default' => 0
184 ),
185 'attachment_ids' => array(
186 'type' => 'array',
187 'default' => array()
188 ),
189 'template' => array(
190 'type' => 'string',
191 'default' => ''
192 ),
193 'lightbox' => array(
194 'type' => 'string',
195 'default' => ''
196 ),
197 'paging_type' => array(
198 'type' => 'string',
199 'default' => ''
200 ),
201 'filtering_type' => array(
202 'type' => 'string',
203 'default' => ''
204 ),
205 'className' => array(
206 'type' => 'string'
207 ),
208 ),
209 'render_callback' => array( $this, 'render_block' ),
210 ));
211 }
212 }
213
214 /**
215 * Render the contents of the block
216 *
217 * @param $attributes
218 *
219 * @return false|string|null
220 */
221 function render_block( $attributes ) {
222 $attributes = is_array( $attributes ) ? $attributes : array();
223 $attributes['id'] = isset( $attributes['id'] ) ? absint( $attributes['id'] ) : 0;
224 $attachment_ids = array();
225 if ( isset( $attributes['attachment_ids'] ) && is_array( $attributes['attachment_ids'] ) ) {
226 $attachment_ids = array_values( array_filter( array_map( 'absint', $attributes['attachment_ids'] ) ) );
227 }
228
229 $is_dynamic_gallery = ! empty( $attachment_ids );
230
231 if ( $is_dynamic_gallery ) {
232 $attributes['attachment_ids'] = $attachment_ids;
233
234 foreach ( array( 'template', 'lightbox', 'paging_type', 'filtering_type' ) as $key ) {
235 if ( ! array_key_exists( $key, $attributes ) ) {
236 continue;
237 }
238
239 $attributes[ $key ] = sanitize_key( $attributes[ $key ] );
240 if ( '' === $attributes[ $key ] ) {
241 unset( $attributes[ $key ] );
242 }
243 }
244 } else {
245 unset( $attributes['attachment_ids'], $attributes['template'], $attributes['lightbox'], $attributes['paging_type'], $attributes['filtering_type'] );
246 }
247
248 //create new instance of template engine
249 $engine = new FooGallery_Template_Loader();
250
251 ob_start();
252
253 $engine->render_template( $attributes );
254
255 $output_string = ob_get_contents();
256 ob_end_clean();
257 return !empty($output_string) ? $output_string : null;
258 }
259
260 /**
261 * Returns true if the block editor is being used
262 *
263 * @return bool
264 */
265 function is_being_rendered_in_block_editor() {
266 return defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context'];
267 }
268
269 /**
270 * Add data options needed for lazy loading to work with the block editor
271 *
272 * @param $options
273 * @param $gallery FooGallery
274 * @param $attributes array
275 *
276 * @return array
277 */
278 function add_data_options_for_block_editor( $options, $gallery, $attributes ) {
279 if ( $this->is_being_rendered_in_block_editor() ) {
280 $options['scrollParent'] = '.edit-post-layout__content';
281 }
282
283 return $options;
284 }
285 }
286 }
287