Skip to content

Instantly share code, notes, and snippets.

@matteo-greco
Created November 15, 2022 18:44
Show Gist options
  • Save matteo-greco/e5f990fdafaa8c11930ee1dec1ad3c45 to your computer and use it in GitHub Desktop.
Save matteo-greco/e5f990fdafaa8c11930ee1dec1ad3c45 to your computer and use it in GitHub Desktop.
Override ACSS settings
<?php
$metabox_group_id = 50;
add_action( "rwmb_{$metabox_group_id}_after_save_post", function( $object_id ) {
// STEP: get the variables we want to override from the settings page.
$primary = rwmb_meta( 'test_primary_color', ['object_type' => 'setting'], 'test_client_dashboard');
error_log( sprintf( '%s: primary %s', 'my action handler', $primary ) );
$new_vars = array(
'color-primary' => $primary
);
// STEP: fix the saturation when overriding colors.
$color_modifiers = array( 'ultra-light', 'light', 'medium', 'dark', 'ultra-dark', 'hover', 'comp' );
foreach( $new_vars as $key => $value ) {
if ( preg_match( '/color-(\w+)/', $key, $matches ) ) {
$color_name = $matches[1];
$color = new \Automatic_CSS\Helpers\Color( $value );
$saturation = $color->s;
foreach( $color_modifiers as $color_modifier ) {
$new_vars[ $color_name . '-' . $color_modifier . '-s' ] = $saturation;
}
}
}
error_log( sprintf( "%s: overriding these var:\n%s", 'my action handler', print_r( $new_vars, true ) ) );
// STEP: try updating the database.
try {
$database = ( new \Automatic_CSS\Model\Database_Settings() )::get_instance();
$old_vars = $database->get_vars(); // grab the vars currently saved in the database.
$save_vars = array_merge( $old_vars, $new_vars ); // let the new vars override the existing ones.
$database->save_vars( $save_vars ); // save and trigger CSS regeneration.
} catch ( \Exception $e ) {
error_log( sprintf( "%s: Exception caught:\n%s", __FUNCTION__, print_r( $e, true ) ) );
}
} );
@pv21design
Copy link

pv21design commented Nov 15, 2022

This is awesome @matteo-greco !!! I'm no developer myself, but by searching the web for the same hook on ACF instead of MetaBox, I managed to do it. Here is the following working code.

<?php 

function save_acss_colors() {
         // STEP: Get the Current ACF Option Page
	$screen = get_current_screen();
	// STEP: If is your Options page, do this
	if (strpos($screen->id, "personalizar") == true) {
        // STEP: get the variables we want to override from the ACF settings page.
    $cor_principal = get_field( 'cor_principal', 'option' );
	$cor_secundaria = get_field( 'cor_secundaria', 'option' );
	$cor_de_destaque = get_field( 'cor_de_destaque', 'option' );
	$cor_de_base = get_field( 'cor_de_base', 'option' );
    $new_vars = array(
        'color-primary' => $cor_principal,
        'color-secondary' => $cor_secundaria,
        'color-accent' => $cor_de_destaque,
        'color-base' => $cor_de_destaque
    );
    // STEP: fix the saturation when overriding colors.
    $color_modifiers = array( 'ultra-light', 'light', 'medium', 'dark', 'ultra-dark', 'hover', 'comp' );
    foreach( $new_vars as $key => $value ) {
        if ( preg_match( '/color-(\w+)/', $key, $matches ) ) {
            $color_name = $matches[1];
            $color = new \Automatic_CSS\Helpers\Color( $value );
		    $saturation = $color->s;
		    foreach( $color_modifiers as $color_modifier ) {
		        $new_vars[ $color_name . '-' . $color_modifier . '-s' ] = $saturation;
		    }
        }
    }
    error_log( sprintf( "%s: overriding these var:\n%s", 'my action handler', print_r( $new_vars, true ) ) );
    // STEP: try updating the database.
    try {
		$database = ( new \Automatic_CSS\Model\Database_Settings() )::get_instance();
	    $old_vars = $database->get_vars(); // grab the vars currently saved in the database.
	    $save_vars = array_merge( $old_vars, $new_vars ); // let the new vars override the existing ones.
        $database->save_vars( $save_vars ); // save and trigger CSS regeneration.
	} catch ( \Exception $e ) {
		error_log( sprintf( "%s: Exception caught:\n%s", __FUNCTION__, print_r( $e, true ) ) );
	}
	}
}
// STEP: Save the ACF Options Page
add_action('acf/save_post', 'save_acss_colors', 20);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment