PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.8.2
GenerateBlocks v1.8.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / includes / general.php
generateblocks / includes Last commit date
blocks 2 years ago class-do-css.php 2 years ago class-dynamic-content.php 2 years ago class-enqueue-css.php 3 years ago class-legacy-attributes.php 4 years ago class-map-deprecated-attributes.php 2 years ago class-plugin-update.php 5 years ago class-query-loop.php 3 years ago class-render-blocks.php 3 years ago class-rest.php 2 years ago class-settings.php 4 years ago dashboard.php 3 years ago defaults.php 2 years ago functions.php 2 years ago general.php 2 years ago
general.php
429 lines
1 <?php
2 /**
3 * General actions and filters.
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 add_action( 'enqueue_block_editor_assets', 'generateblocks_do_block_editor_assets' );
13 /**
14 * Enqueue Gutenberg block assets for backend editor.
15 *
16 * @uses {wp-blocks} for block type registration & related functions.
17 * @uses {wp-element} for WP Element abstraction — structure of blocks.
18 * @uses {wp-i18n} to internationalize the block's text.
19 * @uses {wp-editor} for WP editor styles.
20 * @since 0.1
21 */
22 function generateblocks_do_block_editor_assets() {
23 global $pagenow;
24
25 $generateblocks_deps = array( 'wp-blocks', 'wp-i18n', 'wp-editor', 'wp-element', 'wp-compose', 'wp-data' );
26
27 if ( 'widgets.php' === $pagenow ) {
28 unset( $generateblocks_deps[2] );
29 }
30
31 $assets_file = GENERATEBLOCKS_DIR . 'dist/blocks.asset.php';
32 $compiled_assets = file_exists( $assets_file )
33 ? require $assets_file
34 : false;
35
36 $assets =
37 isset( $compiled_assets['dependencies'] ) &&
38 isset( $compiled_assets['version'] )
39 ? $compiled_assets
40 : [
41 'dependencies' => $generateblocks_deps,
42 'version' => filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.js' ),
43 ];
44
45 wp_enqueue_script(
46 'generateblocks',
47 GENERATEBLOCKS_DIR_URL . 'dist/blocks.js',
48 $assets['dependencies'],
49 $assets['version'],
50 true
51 );
52
53 if ( function_exists( 'wp_set_script_translations' ) ) {
54 wp_set_script_translations( 'generateblocks', 'generateblocks' );
55 }
56
57 wp_enqueue_style(
58 'generateblocks',
59 GENERATEBLOCKS_DIR_URL . 'dist/blocks.css',
60 array( 'wp-edit-blocks' ),
61 filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.css' )
62 );
63
64 $image_sizes = get_intermediate_image_sizes();
65 $image_sizes = array_diff( $image_sizes, array( '1536x1536', '2048x2048' ) );
66 $image_sizes[] = 'full';
67
68 wp_localize_script(
69 'generateblocks',
70 'generateBlocksInfo',
71 array(
72 'imageSizes' => $image_sizes,
73 'svgShapes' => generateblocks_get_svg_shapes(),
74 'syncResponsivePreviews' => generateblocks_get_option( 'sync_responsive_previews' ),
75 'excerptLength' => apply_filters( 'excerpt_length', 55 ), // phpcs:ignore -- Core filter.
76 'excerptMore' => apply_filters( 'excerpt_more', ' ' . '[&hellip;]' ), // phpcs:ignore -- Core filter.
77 'imagePlaceholders' => array(
78 'standard' => GENERATEBLOCKS_DIR_URL . 'assets/images/image-placeholder.png',
79 'square' => GENERATEBLOCKS_DIR_URL . 'assets/images/square-image-placeholder.png',
80 ),
81 'globalContainerWidth' => generateblocks_get_global_container_width(),
82 'queryLoopEditorPostsCap' => apply_filters( 'generateblocks_query_loop_editor_posts_cap', 50 ),
83 'disableGoogleFonts' => generateblocks_get_option( 'disable_google_fonts' ),
84 'typographyFontFamilyList' => generateblocks_get_font_family_list(),
85 )
86 );
87
88 if ( function_exists( 'generate_get_color_defaults' ) ) {
89 $color_settings = wp_parse_args(
90 get_option( 'generate_settings', array() ),
91 generate_get_color_defaults()
92 );
93
94 $generatepressDefaultStyling = apply_filters(
95 'generateblocks_gp_default_styling',
96 array(
97 'buttonBackground' => $color_settings['form_button_background_color'],
98 'buttonBackgroundHover' => $color_settings['form_button_background_color_hover'],
99 'buttonText' => $color_settings['form_button_text_color'],
100 'buttonTextHover' => $color_settings['form_button_text_color_hover'],
101 'buttonPaddingTop' => '10px',
102 'buttonPaddingRight' => '20px',
103 'buttonPaddingBottom' => '10px',
104 'buttonPaddingLeft' => '20px',
105 )
106 );
107
108 $css = sprintf(
109 '.gb-button.button {
110 background-color: %1$s;
111 color: %2$s;
112 padding-top: %3$s;
113 padding-right: %4$s;
114 padding-bottom: %5$s;
115 padding-left: %6$s;
116 }',
117 $generatepressDefaultStyling['buttonBackground'],
118 $generatepressDefaultStyling['buttonText'],
119 $generatepressDefaultStyling['buttonPaddingTop'],
120 $generatepressDefaultStyling['buttonPaddingRight'],
121 $generatepressDefaultStyling['buttonPaddingBottom'],
122 $generatepressDefaultStyling['buttonPaddingLeft']
123 );
124
125 $css .= sprintf(
126 '.gb-button.button:active, .gb-button.button:hover, .gb-button.button:focus {
127 background-color: %1$s;
128 color: %2$s;
129 }',
130 $generatepressDefaultStyling['buttonBackgroundHover'],
131 $generatepressDefaultStyling['buttonTextHover']
132 );
133
134 wp_add_inline_style( 'generateblocks', $css );
135 }
136
137 $defaults = generateblocks_get_block_defaults();
138
139 wp_localize_script(
140 'generateblocks',
141 'generateBlocksDefaults',
142 $defaults
143 );
144
145 wp_localize_script(
146 'generateblocks',
147 'generateBlocksStyling',
148 generateblocks_get_default_styles()
149 );
150
151 wp_localize_script(
152 'generateblocks',
153 'generateBlocksLegacyDefaults',
154 array(
155 'v_1_4_0' => GenerateBlocks_Legacy_Attributes::get_defaults( '1.4.0' ),
156 )
157 );
158 }
159
160 if ( version_compare( $GLOBALS['wp_version'], '5.8-alpha-1', '<' ) ) {
161 add_filter( 'block_categories', 'generateblocks_do_category' );
162 } else {
163 add_filter( 'block_categories_all', 'generateblocks_do_category' );
164 }
165 /**
166 * Add GeneratePress category to Gutenberg.
167 *
168 * @param array $categories Existing categories.
169 * @since 0.1
170 */
171 function generateblocks_do_category( $categories ) {
172 return array_merge(
173 array(
174 array(
175 'slug' => 'generateblocks',
176 'title' => __( 'GenerateBlocks', 'generateblocks' ),
177 ),
178 ),
179 $categories
180 );
181 }
182
183 add_action( 'wp_enqueue_scripts', 'generateblocks_do_google_fonts' );
184 add_action( 'enqueue_block_editor_assets', 'generateblocks_do_google_fonts' );
185 /**
186 * Do Google Fonts.
187 *
188 * @since 0.1
189 */
190 function generateblocks_do_google_fonts() {
191 if ( generateblocks_get_option( 'disable_google_fonts' ) ) {
192 return;
193 }
194
195 $fonts_url = generateblocks_get_google_fonts_uri();
196
197 if ( $fonts_url ) {
198 wp_enqueue_style( 'generateblocks-google-fonts', $fonts_url, array(), null, 'all' ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
199 }
200 }
201
202 add_filter( 'generateblocks_css_print_method', 'generateblocks_set_css_print_method' );
203 /**
204 * Set our CSS print method.
205 *
206 * @param string $method Existing method.
207 */
208 function generateblocks_set_css_print_method( $method ) {
209 $method = generateblocks_get_option( 'css_print_method' );
210
211 if ( is_single() ) {
212 $method = 'inline';
213 }
214
215 return $method;
216 }
217
218 add_filter( 'excerpt_allowed_blocks', 'generateblocks_set_excerpt_allowed_blocks' );
219 /**
220 * Add blocks that can be displayed in post excerpts.
221 *
222 * @param array $allowed Existing allowed blocks.
223 * @since 1.0
224 */
225 function generateblocks_set_excerpt_allowed_blocks( $allowed ) {
226 $allowed[] = 'generateblocks/headline';
227 $allowed[] = 'generateblocks/container';
228
229 return $allowed;
230 }
231
232 add_filter( 'excerpt_allowed_wrapper_blocks', 'generateblocks_set_excerpt_allowed_wrapper_blocks' );
233 /**
234 * Allows excerpts to be generated from the `innerBlocks` of these wrappers.
235 *
236 * @param array $allowed Existing allowed wrapper blocks.
237 * @since 1.5.0
238 */
239 function generateblocks_set_excerpt_allowed_wrapper_blocks( $allowed ) {
240 $allowed[] = 'generateblocks/container';
241
242 return $allowed;
243 }
244
245 add_filter( 'generateblocks_before_container_close', 'generateblocks_do_shape_divider', 10, 2 );
246 /**
247 * Add shape divider to Container.
248 *
249 * @since 1.2.0
250 * @param string $output The current block output.
251 * @param array $attributes The current block attributes.
252 */
253 function generateblocks_do_shape_divider( $output, $attributes ) {
254 $defaults = generateblocks_get_block_defaults();
255
256 $settings = wp_parse_args(
257 $attributes,
258 $defaults['container']
259 );
260
261 if ( ! empty( $settings['shapeDividers'] ) ) {
262 $shapes = generateblocks_get_svg_shapes();
263 $shape_values = array();
264
265 foreach ( $shapes as $group => $data ) {
266 if ( ! empty( $data['svgs'] ) && is_array( $data['svgs'] ) ) {
267 foreach ( $data['svgs'] as $key => $shape ) {
268 $shape_values[ $key ] = $shape['icon'];
269 }
270 }
271 }
272
273 $output .= '<div class="gb-shapes">';
274
275 foreach ( (array) $settings['shapeDividers'] as $index => $option ) {
276 if ( ! empty( $option['shape'] ) ) {
277 if ( isset( $shape_values[ $option['shape'] ] ) ) {
278 $shapeNumber = $index + 1;
279
280 $output .= sprintf(
281 '<div class="gb-shape gb-shape-' . $shapeNumber . '">%s</div>',
282 $shape_values[ $option['shape'] ]
283 );
284 }
285 }
286 }
287
288 $output .= '</div>';
289 }
290
291 return $output;
292 }
293
294 add_filter( 'generateblocks_do_content', 'generateblocks_do_widget_styling' );
295 /**
296 * Process all widget content for potential styling.
297 *
298 * @since 1.3.4
299 * @param string $content The existing content to process.
300 */
301 function generateblocks_do_widget_styling( $content ) {
302 $widget_blocks = get_option( 'widget_block' );
303
304 foreach ( (array) $widget_blocks as $block ) {
305 if ( isset( $block['content'] ) ) {
306 $content .= $block['content'];
307 }
308 }
309
310 return $content;
311 }
312
313 add_filter( 'generateblocks_attr_container', 'generateblocks_set_inline_background_style', 10, 2 );
314 /**
315 * Add our background image attribute to the Container.
316 *
317 * @since 1.5.0
318 * @param array $attributes Existing attributes.
319 * @param array $settings Block settings.
320 */
321 function generateblocks_set_inline_background_style( $attributes, $settings ) {
322 if ( generateblocks_has_background_image( $settings ) && $settings['bgImageInline'] ) {
323 $url = generateblocks_get_background_image_url( $settings );
324
325 if ( $url ) {
326 $attribute_name = 'background-image';
327
328 if ( 'element' !== $settings['bgOptions']['selector'] ) {
329 $attribute_name = '--' . $attribute_name;
330 }
331
332 $attributes['style'] = $attribute_name . ': url(' . esc_url( $url ) . ');';
333 }
334 }
335
336 return $attributes;
337 }
338
339 add_filter( 'generateblocks_block_css_selector', 'generateblocks_set_block_css_selectors', 10, 3 );
340 /**
341 * Change our block selectors if needed.
342 *
343 * @param string $selector Existing selector.
344 * @param string $name The block name.
345 * @param array $attributes The block attributes.
346 */
347 function generateblocks_set_block_css_selectors( $selector, $name, $attributes ) {
348 $blockVersion = ! empty( $attributes['blockVersion'] ) ? $attributes['blockVersion'] : 1;
349 $defaults = generateblocks_get_block_defaults();
350
351 if ( 'button' === $name ) {
352 $settings = wp_parse_args(
353 $attributes,
354 $defaults['button']
355 );
356
357 if ( $blockVersion < 3 ) {
358 // Old versions of the this block used this backwards logic
359 // to determine whether to remove the "a" to the selector.
360 $clean_selector = $selector;
361 $selector = 'a' . $selector;
362
363 if ( isset( $attributes['hasUrl'] ) && ! $attributes['hasUrl'] ) {
364 $selector = $clean_selector;
365 }
366 } else {
367 $is_link = (
368 ! empty( $settings['hasUrl'] ) ||
369 ! empty( $settings['dynamicLinkType'] )
370 ) && 'link' === $settings['buttonType'];
371
372 if ( $is_link ) {
373 $selector = 'a' . $selector;
374 }
375
376 if ( 'button' === $settings['buttonType'] ) {
377 $selector = 'button' . $selector;
378 }
379 }
380
381 if ( $settings['hasButtonContainer'] || $blockVersion < 3 ) {
382 $selector = '.gb-button-wrapper ' . $selector;
383 } elseif ( isset( $settings['isPagination'] ) && $settings['isPagination'] ) {
384 $selector = '.gb-query-loop-pagination ' . $selector;
385 }
386 }
387
388 if ( 'headline' === $name ) {
389 $settings = wp_parse_args(
390 $attributes,
391 $defaults['headline']
392 );
393
394 if ( apply_filters( 'generateblocks_headline_selector_tagname', true, $attributes ) ) {
395 $selector = $settings['element'] . $selector;
396 }
397 }
398
399 return $selector;
400 }
401
402 add_action( 'init', 'generateblocks_register_user_meta' );
403 /**
404 * Register GenerateBlocks custom user meta fields.
405 *
406 * @return void
407 */
408 function generateblocks_register_user_meta() {
409 register_meta(
410 'user',
411 GenerateBlocks_Rest::ONBOARDING_META_KEY,
412 array(
413 'type' => 'object',
414 'single' => true,
415 'show_in_rest' => array(
416 'schema' => array(
417 'type' => 'object',
418 'properties' => array(
419 'insert_inner_container' => array( 'type' => 'boolean' ),
420 ),
421 'additionalProperties' => array(
422 'type' => 'boolean',
423 ),
424 ),
425 ),
426 )
427 );
428 }
429