Created
March 15, 2017 16:47
-
-
Save borisschapira/ee13f15ceb37b100da620405cd409a58 to your computer and use it in GitHub Desktop.
Displaying current branch in Wordpress Admin Area
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
/** | |
* Get the git branch | |
* @return mixed Either the branch name or the HEAD content or a boolean false | |
*/ | |
function get_current_git_commit() { | |
if ( $hash = file_get_contents( get_home_path().'.git/HEAD' ) ) { | |
preg_match('/ref: refs\/heads\/(.*)/', $hash, $matches, PREG_OFFSET_CAPTURE); | |
if(count($matches)>=2) { | |
return $matches[1][0]; | |
} else { | |
return $hash; | |
} | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Generic function to show a message to the user using WP's | |
* standard CSS classes to make use of the already-defined | |
* message colour scheme. | |
* | |
* @param $message The message you want to tell the user. | |
* @param $errormsg If true, the message is an error, so use | |
* the red message style. If false, the message is a status | |
* message, so use the yellow information message style. | |
*/ | |
function showMessage($message, $errormsg = false) | |
{ | |
if ($errormsg) { | |
echo '<div id="message" class="error">'; | |
} | |
else { | |
echo '<div id="message" class="updated fade">'; | |
} | |
echo "<p><strong>$message</strong></p></div>"; | |
} | |
/** | |
* Just show our message (with possible checking if we only want | |
* to show message to certain users. | |
*/ | |
function showAdminMessages() | |
{ | |
// Only show to admins, on non-PROD environment | |
if (current_user_can('manage_options')) { | |
require( get_home_path().'wp-config.php' ); | |
if(WP_ENV && WP_ENV != 'PROD'){ | |
$hash = get_current_git_commit(); | |
showMessage( sprintf( 'Vous êtes actuellement en : %s', WP_ENV ) ); | |
if ($hash != 'master') { | |
showMessage( sprintf( 'Attention, vous êtes sur la branche : %s', $hash ), true ); | |
} | |
} | |
} | |
} | |
/** | |
* Call showAdminMessages() when showing other admin | |
* messages. | |
*/ | |
add_action('admin_notices', 'showAdminMessages'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment