Last active
September 18, 2016 03:26
-
-
Save trepmal/f0c13fca436196226947 to your computer and use it in GitHub Desktop.
WordPress plugin. Hot swap admin color scheme for probable local dev/stage sites
This file contains hidden or 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 | |
/** | |
* Plugin Name: Swap color scheme on local dev/stage | |
*/ | |
/** | |
* Hotswap an alternate color scheme | |
* | |
* @param string $result Original value | |
* @param string $option Option name | |
* @param object $user WP_User object | |
* @return string Color scheme handle | |
*/ | |
function dsa_get_user_option_admin_color( $result, $option, $user ) { | |
if ( dsa_is_dev_or_stage() ) { | |
return dsa_get_swapped_color( $result ); | |
} | |
return $result; | |
} | |
add_filter('get_user_option_admin_color', 'dsa_get_user_option_admin_color', 10, 3 ); | |
/** | |
* Calculate swapped color | |
* | |
* @param string $saved Current saved value | |
* @return string New swapped value | |
*/ | |
function dsa_get_swapped_color( $saved ) { | |
$alternate = 'ocean'; | |
if ( $saved == $alternate ) { | |
$alternate = 'sunrise'; | |
} | |
// one of: fresh, light, blue, coffee, ectoplasm, midnight, ocean, sunrise | |
return apply_filters( 'dsa_alternate_color', $alternate, $user ); | |
} | |
/** | |
* Unhook later so that actual saved value is properly shown to user when editing profile | |
*/ | |
add_action('in_admin_header', function() { | |
remove_filter('get_user_option_admin_color', 'dsa_get_user_option_admin_color', 10, 3 ); | |
}); | |
/** | |
* Check if home_url() appears to be local dev or stage | |
* | |
* @return bool | |
*/ | |
function dsa_is_dev_or_stage() { | |
if ( // url | |
// starts with... | |
preg_match( '/^(stage|staging)\./', home_url() ) || | |
// ends with... | |
preg_match( '/\.(dev|local|test|wp|localhost|10uplabs\.com)$/', home_url() ) | |
) { | |
// one of: fresh, light, blue, coffee, ectoplasm, midnight, ocean, sunrise | |
$return = true; | |
} else { | |
$return = false; | |
} | |
return apply_filters( 'is_dev_or_stage', $return ); | |
} | |
/** | |
* Notice under admin option to indicate change | |
* | |
* @return void | |
*/ | |
function dsa_admin_color_scheme_picker( $user_id ) { | |
if ( dsa_is_dev_or_stage() ) { | |
$alternate = dsa_get_swapped_color( get_user_option( 'admin_color', $user_id ) ); | |
echo '<p>'; | |
printf( __( '<strong>Notice:</strong> This site appears to be dev or stage, an alternate color scheme (%s) is being swapped in', 'dev-stage-alert' ), $alternate ); | |
echo '</p>'; | |
} | |
} | |
add_action( 'admin_color_scheme_picker', 'dsa_admin_color_scheme_picker', 20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment