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