Created
June 9, 2023 12:36
-
-
Save blorange2/b317440ae234d2909125699954761675 to your computer and use it in GitHub Desktop.
This is a script that allows you to generate scss files based on names of layouts in a flexible content field in ACF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* When ACF Fields are loaded, if they are flexible content store the name of each layout as a value in the post meta. | |
* This will allow us to retrieve the original name later. | |
* | |
* @param [type] $field | |
* | |
* @return void | |
*/ | |
function my_acf_load_field($field) | |
{ | |
if ($field['type'] === 'flexible_content') { | |
foreach ($field['layouts'] as $key => $layout) { | |
// Store the original layout name as post meta | |
update_post_meta(get_the_ID(), 'acf_layout_original_name_' . $key, $layout['name']); | |
} | |
} | |
return $field; | |
} | |
function create_css_files($field) | |
{ | |
// If we're not in the page builder, return early | |
if ($field['name'] !== 'page_builder_elements') { | |
return $field; | |
} | |
$base_stylesheet_path = get_stylesheet_directory() . '/resources/styles/app.scss'; | |
$base_scss_component_path = get_stylesheet_directory() . '/resources/styles/scss/'; | |
// Create the base scss component directory if it doesn't exist | |
if (!file_exists($base_scss_component_path)) { | |
mkdir($base_scss_component_path, 0755, true); | |
} | |
$layout_names = process_layouts($field, $base_scss_component_path); | |
update_main_stylesheet($base_stylesheet_path, $layout_names); | |
return $field; | |
} | |
function process_layouts($field, $scss_component_path) | |
{ | |
$layout_names = []; | |
foreach ($field['layouts'] as $key => $layout) { | |
$previous_name = get_post_meta(get_the_ID(), 'acf_layout_original_name_' . $layout['key'], true); | |
$current_name = $layout['name']; | |
if ($current_name !== $previous_name) { | |
if ($previous_name !== '') { | |
$previous_clean_name = sanitize_name($previous_name); | |
$previous_file_path = $scss_component_path . '_' . $previous_clean_name . '.scss'; | |
$new_clean_name = sanitize_name($current_name); | |
$new_file_path = $scss_component_path . '_' . $new_clean_name . '.scss'; | |
if (file_exists($previous_file_path)) { | |
rename($previous_file_path, $new_file_path); | |
} else { | |
file_put_contents($new_file_path, ''); | |
} | |
$layout_names[] = $new_clean_name; | |
} | |
} else { | |
$layout_names[] = sanitize_name($current_name); | |
} | |
} | |
return $layout_names; | |
} | |
function update_main_stylesheet($base_stylesheet_path, $layout_names) | |
{ | |
$contents = file_get_contents($base_stylesheet_path); | |
// Remove existing import statements | |
$contents = preg_replace("/@import\s+'scss\/[^\s;]+?';\n?/", '', $contents); | |
$importStatements = ''; | |
foreach ($layout_names as $import_name) { | |
$importStatements .= "@import 'scss/" . $import_name . "';\n"; | |
} | |
$contents .= "\n" . $importStatements; | |
file_put_contents($base_stylesheet_path, $contents); | |
} | |
/** | |
* Simple helper to clean name given. | |
* | |
* @param [type] $name | |
* | |
* @return void | |
*/ | |
function sanitize_name($name) | |
{ | |
$clean_name = preg_replace('/[^A-Za-z0-9_\-]/', '', $name); | |
$clean_name = str_replace('_', '-', $clean_name); | |
return $clean_name; | |
} | |
add_filter('acf/load_field', 'my_acf_load_field'); | |
add_filter('acf/update_field', 'create_css_files', 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment