PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.4.0
GenerateBlocks v1.4.0
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-enqueue-css.php 4 years ago class-legacy-attributes.php 4 years ago class-plugin-update.php 5 years ago class-render-blocks.php 4 years ago class-rest.php 5 years ago class-settings.php 5 years ago dashboard.php 5 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
291 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 wp_enqueue_script(
32 'generateblocks',
33 GENERATEBLOCKS_DIR_URL . 'dist/blocks.js',
34 $generateblocks_deps,
35 filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.js' ),
36 true
37 );
38
39 if ( function_exists( 'wp_set_script_translations' ) ) {
40 wp_set_script_translations( 'generateblocks', 'generateblocks' );
41 }
42
43 wp_enqueue_style(
44 'generateblocks',
45 GENERATEBLOCKS_DIR_URL . 'dist/blocks.css',
46 array( 'wp-edit-blocks' ),
47 filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.css' )
48 );
49
50 $image_sizes = get_intermediate_image_sizes();
51 $image_sizes = array_diff( $image_sizes, array( '1536x1536', '2048x2048' ) );
52 $image_sizes[] = 'full';
53
54 wp_localize_script(
55 'generateblocks',
56 'generateBlocksInfo',
57 array(
58 'isGeneratePress' => defined( 'GENERATE_VERSION' ),
59 'hasCustomFields' => post_type_supports( get_post_type(), 'custom-fields' ),
60 'imageSizes' => $image_sizes,
61 'svgShapes' => generateblocks_get_svg_shapes(),
62 'syncResponsivePreviews' => generateblocks_get_option( 'sync_responsive_previews' ),
63 )
64 );
65
66 if ( function_exists( 'generate_get_color_defaults' ) ) {
67 $color_settings = wp_parse_args(
68 get_option( 'generate_settings', array() ),
69 generate_get_color_defaults()
70 );
71
72 $generatepressDefaultStyling = apply_filters(
73 'generateblocks_gp_default_styling',
74 array(
75 'buttonBackground' => $color_settings['form_button_background_color'],
76 'buttonBackgroundHover' => $color_settings['form_button_background_color_hover'],
77 'buttonText' => $color_settings['form_button_text_color'],
78 'buttonTextHover' => $color_settings['form_button_text_color_hover'],
79 'buttonPaddingTop' => '10px',
80 'buttonPaddingRight' => '20px',
81 'buttonPaddingBottom' => '10px',
82 'buttonPaddingLeft' => '20px',
83 )
84 );
85
86 $css = sprintf(
87 '.gb-button.button {
88 background-color: %1$s;
89 color: %2$s;
90 padding-top: %3$s;
91 padding-right: %4$s;
92 padding-bottom: %5$s;
93 padding-left: %6$s;
94 }',
95 $generatepressDefaultStyling['buttonBackground'],
96 $generatepressDefaultStyling['buttonText'],
97 $generatepressDefaultStyling['buttonPaddingTop'],
98 $generatepressDefaultStyling['buttonPaddingRight'],
99 $generatepressDefaultStyling['buttonPaddingBottom'],
100 $generatepressDefaultStyling['buttonPaddingLeft']
101 );
102
103 $css .= sprintf(
104 '.gb-button.button:active, .gb-button.button:hover, .gb-button.button:focus {
105 background-color: %1$s;
106 color: %2$s;
107 }',
108 $generatepressDefaultStyling['buttonBackgroundHover'],
109 $generatepressDefaultStyling['buttonTextHover']
110 );
111
112 wp_add_inline_style( 'generateblocks', $css );
113 }
114
115 $defaults = generateblocks_get_block_defaults();
116
117 wp_localize_script(
118 'generateblocks',
119 'generateBlocksDefaults',
120 $defaults
121 );
122
123 wp_localize_script(
124 'generateblocks',
125 'generateBlocksStyling',
126 generateblocks_get_default_styles()
127 );
128
129 wp_localize_script(
130 'generateblocks',
131 'generateBlocksLegacyDefaults',
132 array(
133 'v_1_4_0' => GenerateBlocks_Legacy_Attributes::get_defaults( '1.4.0' ),
134 )
135 );
136 }
137
138 if ( version_compare( $GLOBALS['wp_version'], '5.8-alpha-1', '<' ) ) {
139 add_filter( 'block_categories', 'generateblocks_do_category' );
140 } else {
141 add_filter( 'block_categories_all', 'generateblocks_do_category' );
142 }
143 /**
144 * Add GeneratePress category to Gutenberg.
145 *
146 * @param array $categories Existing categories.
147 * @since 0.1
148 */
149 function generateblocks_do_category( $categories ) {
150 return array_merge(
151 array(
152 array(
153 'slug' => 'generateblocks',
154 'title' => __( 'GenerateBlocks', 'generateblocks' ),
155 ),
156 ),
157 $categories
158 );
159 }
160
161 add_action( 'wp_enqueue_scripts', 'generateblocks_do_google_fonts' );
162 add_action( 'enqueue_block_editor_assets', 'generateblocks_do_google_fonts' );
163 /**
164 * Do Google Fonts.
165 *
166 * @since 0.1
167 */
168 function generateblocks_do_google_fonts() {
169 $fonts_url = generateblocks_get_google_fonts_uri();
170
171 if ( $fonts_url ) {
172 wp_enqueue_style( 'generateblocks-google-fonts', $fonts_url, array(), null, 'all' ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
173 }
174 }
175
176 add_action( 'init', 'generateblocks_register_meta' );
177 /**
178 * Register our post meta.
179 *
180 * @since 0.1
181 */
182 function generateblocks_register_meta() {
183 register_meta(
184 'post',
185 '_generate-full-width-content',
186 array(
187 'show_in_rest' => true,
188 'auth_callback' => '__return_true',
189 'single' => true,
190 )
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( 'generateblocks_before_container_close', 'generateblocks_do_shape_divider', 10, 2 );
225 /**
226 * Add shape divider to Container.
227 *
228 * @since 1.2.0
229 * @param string $output The current block output.
230 * @param array $attributes The current block attributes.
231 */
232 function generateblocks_do_shape_divider( $output, $attributes ) {
233 $defaults = generateblocks_get_block_defaults();
234
235 $settings = wp_parse_args(
236 $attributes,
237 $defaults['container']
238 );
239
240 if ( ! empty( $settings['shapeDividers'] ) ) {
241 $shapes = generateblocks_get_svg_shapes();
242 $shape_values = array();
243
244 foreach ( $shapes as $group => $data ) {
245 if ( ! empty( $data['svgs'] ) && is_array( $data['svgs'] ) ) {
246 foreach ( $data['svgs'] as $key => $shape ) {
247 $shape_values[ $key ] = $shape['icon'];
248 }
249 }
250 }
251
252 $output .= '<div class="gb-shapes">';
253
254 foreach ( (array) $settings['shapeDividers'] as $index => $option ) {
255 if ( ! empty( $option['shape'] ) ) {
256 if ( isset( $shape_values[ $option['shape'] ] ) ) {
257 $shapeNumber = $index + 1;
258
259 $output .= sprintf(
260 '<div class="gb-shape gb-shape-' . $shapeNumber . '">%s</div>',
261 $shape_values[ $option['shape'] ]
262 );
263 }
264 }
265 }
266
267 $output .= '</div>';
268 }
269
270 return $output;
271 }
272
273 add_filter( 'generateblocks_do_content', 'generateblocks_do_widget_styling' );
274 /**
275 * Process all widget content for potential styling.
276 *
277 * @since 1.3.4
278 * @param string $content The existing content to process.
279 */
280 function generateblocks_do_widget_styling( $content ) {
281 $widget_blocks = get_option( 'widget_block' );
282
283 foreach ( (array) $widget_blocks as $block ) {
284 if ( isset( $block['content'] ) ) {
285 $content .= $block['content'];
286 }
287 }
288
289 return $content;
290 }
291