Created
November 15, 2022 18:44
-
-
Save matteo-greco/e5f990fdafaa8c11930ee1dec1ad3c45 to your computer and use it in GitHub Desktop.
Override ACSS settings
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 | |
$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 ) ) ); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.