PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / classes / assets-generator.php

assets-generator.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.9.1, at includes/classes/assets-generator.php

218 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks\Classes;
3
4 use ABlocks\Classes\FileUpload;
5 use ABlocks\Classes\RegisterScripts;
6
7 class AssetsGenerator {
8 public static function write_frontend_css_in_uploads_folder( $post_id ) {
9 $post = get_post( $post_id );
10 $post_type = get_post_type( $post_id );
11 $parse_blocks_content = '';
12
13 if ( empty( $post->post_content ) || $post_type === 'wp_template_part' || $post_type === 'wp_template' ) {
14 $parse_blocks_content = parse_blocks( $post->post_content );
15 } else {
16 $parse_blocks_content = parse_blocks( $post->post_content );
17 }
18
19 $style_depends = [];
20 $scripts_depends = [];
21 $aBlocks = [];
22 self::recursive_block_parser( $parse_blocks_content, $aBlocks, $style_depends, $scripts_depends );
23
24 $register_styles = RegisterScripts::get_register_styles();
25 $register_scripts = RegisterScripts::get_register_scripts();
26 $library_css = '';
27 $static_css = '';
28 $dynamic_css = '';
29 $static_js = '';
30 $library_js = '';
31
32 foreach ( $aBlocks as $aBlock ) {
33 if ( isset( $aBlock['static_js'] ) ) {
34 $static_js .= $aBlock['static_js'] . "\n";
35 }
36 if ( isset( $aBlock['dynamic_style'] ) ) {
37 $dynamic_css .= $aBlock['dynamic_style'] . "\n";
38 }
39
40 if ( isset( $aBlock['static_style'] ) ) {
41 $static_css .= self::minify_css( $aBlock['static_style'] ) . "\n";
42 }
43 }
44
45 // css
46 foreach ( $style_depends as $style_depend ) {
47 if ( isset( $register_styles[ $style_depend ]['path'] ) ) {
48 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
49 $library_css .= file_get_contents( $register_styles[ $style_depend ]['path'] ) . "\n";
50 }
51 }
52
53 // js
54 foreach ( $scripts_depends as $script_depend ) {
55 if ( isset( $register_scripts[ $script_depend ]['path'] ) ) {
56 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
57 $library_js .= file_get_contents( $register_scripts[ $script_depend ]['path'] ) . "\n";
58 }
59 }
60
61 $FileUpload = new FileUpload();
62 $destination_folder = $FileUpload->get_upload_dir();
63 self::copy_build_image_folder_to_uploads( $destination_folder );
64 $FileUpload->create_file( $post_id . '.min.css', $library_css . $static_css . $dynamic_css );
65 $FileUpload->create_file( $post_id . '.min.js', $library_js . $static_js );
66
67 return $aBlocks;
68 }
69
70 public static function recursive_block_parser( $parse_content, &$aBlocks, &$style_depends, &$scripts_depends ) {
71 if ( count( $parse_content ) > 0 ) {
72 foreach ( $parse_content as $item ) {
73 if ( ! empty( $item['blockName'] ) ) {
74 // Handle reusable blocks or patterns using "ref"
75 if ( $item['blockName'] === 'core/block' && ! empty( $item['attrs']['ref'] ) ) {
76 $ref_post_id = $item['attrs']['ref'];
77 $ref_post = get_post( $ref_post_id ); // Get the reusable block or pattern
78 if ( $ref_post ) {
79 $ref_content = parse_blocks( $ref_post->post_content ); // Parse the reusable block's content
80 self::recursive_block_parser( $ref_content, $aBlocks, $style_depends, $scripts_depends ); // Recursively parse the referenced block/pattern
81 }
82 } elseif ( strpos( $item['blockName'], 'ablocks' ) !== false ) {
83 $block_name_class = str_replace( ' ', '', ucwords( str_replace( '-', ' ', explode( '/', $item['blockName'] )[1] ) ) );
84
85 $dynamic_class = '\\ABlocks\\Blocks\\' . $block_name_class . '\\Block';
86 if ( ! class_exists( $dynamic_class ) ) {
87 $dynamic_class = '\\ABlocksPro\\Blocks\\' . $block_name_class . '\\Block';
88 }
89
90 if ( class_exists( $dynamic_class ) ) {
91 $instance = new $dynamic_class( true );
92 $attributes = wp_parse_args( $item['attrs'], array_map( function( $attr ) {
93 return isset( $attr['default'] ) ? $attr['default'] : '';
94 }, $instance->get_attributes() ));
95
96 // Capture static CSS/JS
97 if ( ! isset( $aBlocks[ $item['blockName'] ] ) ) {
98 $aBlocks[ $item['blockName'] ] = [
99 'static_style' => $instance->get_static_css(),
100 'static_js' => $instance->get_static_js(),
101 ];
102 }
103
104 // Capture library scripts
105 $style_depends = array_unique( array_merge( $style_depends, $instance->get_style_depends() ) );
106 $scripts_depends = array_unique( array_merge( $scripts_depends, $instance->get_script_depends() ) );
107
108 // Capture dynamic CSS
109 if ( isset( $item['attrs']['ref'] ) || isset( $item['attrs']['block_id'] ) ) {
110 $block_id_or_ref = ! empty( $item['attrs']['block_id'] ) ? $item['attrs']['block_id'] : 'core_pattern_ref_' . $item['attrs']['ref'];
111 $aBlocks[ $block_id_or_ref ] = [
112 'block_name' => $item['blockName'],
113 'dynamic_style' => $instance->build_css( $attributes ),
114 ];
115 }
116 }//end if
117 }//end if
118
119 // Check for inner blocks and recursively process them
120 if ( is_array( $item['innerBlocks'] ) && count( $item['innerBlocks'] ) ) {
121 self::recursive_block_parser( $item['innerBlocks'], $aBlocks, $style_depends, $scripts_depends );
122 }
123 }//end if
124 }//end foreach
125 }//end if
126 }
127
128 public static function minify_css( $css_string ) {
129 // Remove comments (/* ... */) using regex
130 $css_string = preg_replace( '/\/\*[\s\S]*?\*\//', '', $css_string );
131
132 // Remove double spaces, tabs, and newlines
133 $css_string = preg_replace( '/\s+/', ' ', $css_string );
134
135 // Remove spaces around colons, semicolons, curly braces, and commas
136 $css_string = preg_replace( '/\s?([:,;{}])\s?/', '$1', $css_string );
137
138 return $css_string;
139 }
140
141 public static function copy_build_image_folder_to_uploads( $destination_folder ) {
142 global $wp_filesystem;
143 if ( empty( $wp_filesystem ) ) {
144 require_once ABSPATH . '/wp-admin/includes/file.php';
145 WP_Filesystem();
146 }
147
148 $source_folders = [
149 ABLOCKS_ASSETS_PATH . 'build/images/', // First folder
150 ABLOCKS_ASSETS_PATH . 'library/leaflet/', // Add more folders as needed
151 // Add more source folders here
152 ];
153
154 if ( ! file_exists( $destination_folder ) ) {
155 wp_mkdir_p( $destination_folder );
156 }
157
158 // Loop through each source folder and copy its contents
159 foreach ( $source_folders as $source_folder ) {
160 if ( file_exists( $source_folder ) ) {
161 self::recursive_copy( $source_folder, $destination_folder );
162 }
163 }
164 }
165
166 public static function recursive_copy( $source, $destination ) {
167 global $wp_filesystem;
168 if ( empty( $wp_filesystem ) ) {
169 require_once ABSPATH . '/wp-admin/includes/file.php';
170 WP_Filesystem();
171 }
172
173 $valid_extensions = [ 'jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp' ];
174
175 // Ensure WP_Filesystem is initialized
176 if ( ! is_object( $GLOBALS['wp_filesystem'] ) ) {
177 request_filesystem_credentials( '', '', true );
178 }
179
180 // Initialize the WP_Filesystem object
181 $wp_file_system = $GLOBALS['wp_filesystem'];
182
183 // Open the source directory
184 $dir = opendir( $source );
185
186 // Create the destination directory using WP_Filesystem
187 if ( ! $wp_file_system->is_dir( $destination ) ) {
188 $wp_file_system->mkdir( $destination );
189 }
190
191 // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
192 while ( false !== ( $file = readdir( $dir ) ) ) {
193 if ( ( $file !== '.' ) && ( $file !== '..' ) ) {
194 $source_file_path = $source . '/' . $file;
195 $destination_file_path = $destination . '/' . $file;
196
197 // If it's a directory, recursively copy its contents
198 if ( is_dir( $source_file_path ) ) {
199 self::recursive_copy( $source_file_path, $destination_file_path );
200 } else {
201 // Get file extension
202 $file_extension = pathinfo( $file, PATHINFO_EXTENSION );
203
204 // Check if the file has a valid image extension
205 if ( in_array( strtolower( $file_extension ), $valid_extensions, true ) ) {
206 // Copy only image files
207 $wp_file_system->copy( $source_file_path, $destination_file_path );
208 }
209 }
210 }
211 }
212
213 // Close the directory handle
214 closedir( $dir );
215 }
216
217 }
218