PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.5.3
GenerateBlocks v1.5.3
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
class-do-css.php 4 years ago class-dynamic-content.php 4 years ago class-enqueue-css.php 4 years ago class-legacy-attributes.php 4 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 4 years ago class-settings.php 4 years ago dashboard.php 4 years ago defaults.php 4 years ago functions.php 4 years ago general.php 4 years ago generate-css.php 4 years ago
general.php
330 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 )
82 );
83
84 if ( function_exists( 'generate_get_color_defaults' ) ) {
85 $color_settings = wp_parse_args(
86 get_option( 'generate_settings', array() ),
87 generate_get_color_defaults()
88 );
89
90 $generatepressDefaultStyling = apply_filters(
91 'generateblocks_gp_default_styling',
92 array(
93 'buttonBackground' => $color_settings['form_button_background_color'],
94 'buttonBackgroundHover' => $color_settings['form_button_background_color_hover'],
95 'buttonText' => $color_settings['form_button_text_color'],
96 'buttonTextHover' => $color_settings['form_button_text_color_hover'],
97 'buttonPaddingTop' => '10px',
98 'buttonPaddingRight' => '20px',
99 'buttonPaddingBottom' => '10px',
100 'buttonPaddingLeft' => '20px',
101 )
102 );
103
104 $css = sprintf(
105 '.gb-button.button {
106 background-color: %1$s;
107 color: %2$s;
108 padding-top: %3$s;
109 padding-right: %4$s;
110 padding-bottom: %5$s;
111 padding-left: %6$s;
112 }',
113 $generatepressDefaultStyling['buttonBackground'],
114 $generatepressDefaultStyling['buttonText'],
115 $generatepressDefaultStyling['buttonPaddingTop'],
116 $generatepressDefaultStyling['buttonPaddingRight'],
117 $generatepressDefaultStyling['buttonPaddingBottom'],
118 $generatepressDefaultStyling['buttonPaddingLeft']
119 );
120
121 $css .= sprintf(
122 '.gb-button.button:active, .gb-button.button:hover, .gb-button.button:focus {
123 background-color: %1$s;
124 color: %2$s;
125 }',
126 $generatepressDefaultStyling['buttonBackgroundHover'],
127 $generatepressDefaultStyling['buttonTextHover']
128 );
129
130 wp_add_inline_style( 'generateblocks', $css );
131 }
132
133 $defaults = generateblocks_get_block_defaults();
134
135 wp_localize_script(
136 'generateblocks',
137 'generateBlocksDefaults',
138 $defaults
139 );
140
141 wp_localize_script(
142 'generateblocks',
143 'generateBlocksStyling',
144 generateblocks_get_default_styles()
145 );
146
147 wp_localize_script(
148 'generateblocks',
149 'generateBlocksLegacyDefaults',
150 array(
151 'v_1_4_0' => GenerateBlocks_Legacy_Attributes::get_defaults( '1.4.0' ),
152 )
153 );
154 }
155
156 if ( version_compare( $GLOBALS['wp_version'], '5.8-alpha-1', '<' ) ) {
157 add_filter( 'block_categories', 'generateblocks_do_category' );
158 } else {
159 add_filter( 'block_categories_all', 'generateblocks_do_category' );
160 }
161 /**
162 * Add GeneratePress category to Gutenberg.
163 *
164 * @param array $categories Existing categories.
165 * @since 0.1
166 */
167 function generateblocks_do_category( $categories ) {
168 return array_merge(
169 array(
170 array(
171 'slug' => 'generateblocks',
172 'title' => __( 'GenerateBlocks', 'generateblocks' ),
173 ),
174 ),
175 $categories
176 );
177 }
178
179 add_action( 'wp_enqueue_scripts', 'generateblocks_do_google_fonts' );
180 add_action( 'enqueue_block_editor_assets', 'generateblocks_do_google_fonts' );
181 /**
182 * Do Google Fonts.
183 *
184 * @since 0.1
185 */
186 function generateblocks_do_google_fonts() {
187 $fonts_url = generateblocks_get_google_fonts_uri();
188
189 if ( $fonts_url ) {
190 wp_enqueue_style( 'generateblocks-google-fonts', $fonts_url, array(), null, 'all' ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
191 }
192 }
193
194 add_filter( 'generateblocks_css_print_method', 'generateblocks_set_css_print_method' );
195 /**
196 * Set our CSS print method.
197 *
198 * @param string $method Existing method.
199 */
200 function generateblocks_set_css_print_method( $method ) {
201 $method = generateblocks_get_option( 'css_print_method' );
202
203 if ( is_single() ) {
204 $method = 'inline';
205 }
206
207 return $method;
208 }
209
210 add_filter( 'excerpt_allowed_blocks', 'generateblocks_set_excerpt_allowed_blocks' );
211 /**
212 * Add blocks that can be displayed in post excerpts.
213 *
214 * @param array $allowed Existing allowed blocks.
215 * @since 1.0
216 */
217 function generateblocks_set_excerpt_allowed_blocks( $allowed ) {
218 $allowed[] = 'generateblocks/headline';
219 $allowed[] = 'generateblocks/container';
220
221 return $allowed;
222 }
223
224 add_filter( 'excerpt_allowed_wrapper_blocks', 'generateblocks_set_excerpt_allowed_wrapper_blocks' );
225 /**
226 * Allows excerpts to be generated from the `innerBlocks` of these wrappers.
227 *
228 * @param array $allowed Existing allowed wrapper blocks.
229 * @since 1.5.0
230 */
231 function generateblocks_set_excerpt_allowed_wrapper_blocks( $allowed ) {
232 $allowed[] = 'generateblocks/container';
233
234 return $allowed;
235 }
236
237 add_filter( 'generateblocks_before_container_close', 'generateblocks_do_shape_divider', 10, 2 );
238 /**
239 * Add shape divider to Container.
240 *
241 * @since 1.2.0
242 * @param string $output The current block output.
243 * @param array $attributes The current block attributes.
244 */
245 function generateblocks_do_shape_divider( $output, $attributes ) {
246 $defaults = generateblocks_get_block_defaults();
247
248 $settings = wp_parse_args(
249 $attributes,
250 $defaults['container']
251 );
252
253 if ( ! empty( $settings['shapeDividers'] ) ) {
254 $shapes = generateblocks_get_svg_shapes();
255 $shape_values = array();
256
257 foreach ( $shapes as $group => $data ) {
258 if ( ! empty( $data['svgs'] ) && is_array( $data['svgs'] ) ) {
259 foreach ( $data['svgs'] as $key => $shape ) {
260 $shape_values[ $key ] = $shape['icon'];
261 }
262 }
263 }
264
265 $output .= '<div class="gb-shapes">';
266
267 foreach ( (array) $settings['shapeDividers'] as $index => $option ) {
268 if ( ! empty( $option['shape'] ) ) {
269 if ( isset( $shape_values[ $option['shape'] ] ) ) {
270 $shapeNumber = $index + 1;
271
272 $output .= sprintf(
273 '<div class="gb-shape gb-shape-' . $shapeNumber . '">%s</div>',
274 $shape_values[ $option['shape'] ]
275 );
276 }
277 }
278 }
279
280 $output .= '</div>';
281 }
282
283 return $output;
284 }
285
286 add_filter( 'generateblocks_do_content', 'generateblocks_do_widget_styling' );
287 /**
288 * Process all widget content for potential styling.
289 *
290 * @since 1.3.4
291 * @param string $content The existing content to process.
292 */
293 function generateblocks_do_widget_styling( $content ) {
294 $widget_blocks = get_option( 'widget_block' );
295
296 foreach ( (array) $widget_blocks as $block ) {
297 if ( isset( $block['content'] ) ) {
298 $content .= $block['content'];
299 }
300 }
301
302 return $content;
303 }
304
305 add_filter( 'generateblocks_attr_container', 'generateblocks_set_inline_background_style', 10, 2 );
306 /**
307 * Add our background image attribute to the Container.
308 *
309 * @since 1.5.0
310 * @param array $attributes Existing attributes.
311 * @param array $settings Block settings.
312 */
313 function generateblocks_set_inline_background_style( $attributes, $settings ) {
314 if ( generateblocks_has_background_image( $settings ) && $settings['bgImageInline'] ) {
315 $url = generateblocks_get_background_image_url( $settings );
316
317 if ( $url ) {
318 $attribute_name = 'background-image';
319
320 if ( 'element' !== $settings['bgOptions']['selector'] ) {
321 $attribute_name = '--' . $attribute_name;
322 }
323
324 $attributes['style'] = $attribute_name . ': url(' . esc_url( $url ) . ');';
325 }
326 }
327
328 return $attributes;
329 }
330