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