Skip to content

Instantly share code, notes, and snippets.

@jamesmorrison
Created January 3, 2018 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmorrison/5b6449291af387594e575940441092fc to your computer and use it in GitHub Desktop.
Save jamesmorrison/5b6449291af387594e575940441092fc to your computer and use it in GitHub Desktop.
WordPress plugin to display your Git branch in the admin bar
<?php
/**
* Plugin Name: Display Git Branch
* Version: 1.0.0
* Description: Shows which Git branch you're working on. Highlights restricted branches in red.
* Author: James Morrison
* Author URI: https://www.jamesmorrison.me/
**/
// Namespace
namespace Display_Git_Branch;
// Security Check
defined( 'ABSPATH' ) || die( 'Direct file access is forbidden.' );
// Define restricted branches
function restricted_branches() {
return [
'master',
'develop',
];
}
// Get git branch
function branch() {
// Sane default, assume this isn't using Git
$_branch = 'Not detected';
// Look in theme, then site root
$paths = [
get_stylesheet_directory(), // Theme
ABSPATH, // Site
];
// Run through the paths and check each, break on success
// PHPCS suggests wp_remote_get instead of file_get_contents - this does not work as our path is relative
foreach ( $paths as $location ) {
if ( file_exists( $location . '.git/HEAD' ) ) {
$_branch = implode( '/', array_slice( explode( '/', file_get_contents( $location . '.git/HEAD' ) ), 2 ) );
break;
}
}
// This includes a line break... Strip it out and return the branch name.
return str_replace( "\n", '', $_branch );
}
// Display Git Branch in Admin Bar
add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {
$_branch = branch();
$wp_admin_bar->add_node( [
'id' => 'show-git-branch',
'title' => "GIT Branch: $_branch",
] );
}, 99999 );
// Echo CSS inc. warning highlight
add_action( 'admin_head', __NAMESPACE__ . '\\display', 100, 0 );
add_action( 'wp_head', __NAMESPACE__ . '\\display', 100, 0 );
function display() {
$output = '<style type="text/css">#wp-admin-bar-show-git-branch .ab-item:before { content: "\f237"; top: 2px; }</style>';
$_branch = branch();
$_resticted_branches = restricted_branches();
if ( in_array( $_branch, $_resticted_branches, true ) ) {
$output .= '<style type="text/css">#wp-admin-bar-show-git-branch .ab-item { background: #c00; }</style>';
}
// PHPCS Ok - the outputted text contains no variables and cannot be manipulated
echo $output;
}
// Display warning if on restricted branches
add_action( 'wp_footer', function() {
$_branch = branch();
$_resticted_branches = restricted_branches();
if ( in_array( $_branch, $_resticted_branches, true ) ) {
echo '<div style="position: absolute; top: 150px; right: 50px; width: 500px; padding: 50px; background: #c00; font-size: 50px; color: #fff;">
You are on the <strong>' . esc_attr( strtoupper( $_branch ) ) . '</strong> branch.
</div>';
}
}, 100, 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment