Skip to content

Instantly share code, notes, and snippets.

@Pross Pross/functions.php
Created Dec 19, 2019

Embed
What would you like to do?
10m challenge.. add to mu-plugins to show latest commits and branches in admin bar
<?php
add_action( 'admin_bar_menu', function() {
global $wp_admin_bar;
$menu_id = 'bb-git';
$wp_admin_bar->add_menu( array(
'id' => $menu_id,
'title' => __( 'BB - Git Helper' ),
) );
$paths = array(
'bb-plugin' => FL_BUILDER_DIR . '.git',
);
if ( defined( 'FL_THEME_DIR' ) ) {
$paths['bb-theme'] = FL_THEME_DIR . '/.git';
}
if ( defined( 'FL_THEME_BUILDER_DIR' ) ) {
$paths['bb-theme-builder'] = FL_THEME_BUILDER_DIR . '.git';
}
foreach ( $paths as $name => $path ) {
if ( is_dir( $path ) ) {
$gitbasepath = $path;
$gitstr = file_get_contents( $gitbasepath . '/head' );
$gitbranchname = rtrim( preg_replace( '/(.*?\/){2}/', '', $gitstr ) );
$gitpathbranch = $gitbasepath . '/refs/heads/' . $gitbranchname;
$githash = file_get_contents( $gitpathbranch );
$gitdate = gmdate( DATE_ATOM, filemtime( $gitpathbranch ) );
$logs = file( $gitbasepath . '/logs/head' );
$log = array_pop( $logs );
preg_match( '/commit:\s(.*)/', $log, $message );
if ( 'bb-plugin' !== $name ) {
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'title' => '----------------------------------',
'id' => 'spacer' . $name,
) );
}
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'title' => $name,
'id' => 'name' . $name,
) );
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'title' => 'date: ' . gmdate( 'F j, Y, g:i a', strtotime( $gitdate ) ),
'id' => 'date' . $name,
) );
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'title' => 'branch: ' . $gitbranchname,
'id' => 'branch' . $name,
) );
if ( isset( $message[1] ) ) {
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'title' => $message[1],
'id' => 'commit' . $name,
) );
}
}
}
}, 2000 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.