Created
October 5, 2012 12:59
-
-
Save stas/3839694 to your computer and use it in GitHub Desktop.
Hook git repo version into WordPress <head>.
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 | |
/* | |
Plugin Name: Git Version | |
Plugin URI: http://wordpress.org/extend/plugins/git-version/ | |
Description: Add a git version meta to HEAD | |
Version: 0.1 | |
Author: sushkov | |
Author URI: http://wordpress.org/extend/plugins/git-version/ | |
*/ | |
?> | |
<?php | |
/** | |
* Hook current Git version data into <head> | |
*/ | |
class GitVersion { | |
/** | |
* Static constructor | |
*/ | |
function init() { | |
add_action( 'wp_head', array( __CLASS__, 'read_git_head' ) ); | |
} | |
/** | |
* Reads git HEAD if available | |
*/ | |
function read_git_head() { | |
$head = ABSPATH . '.git/HEAD'; | |
$ref = null; | |
if ( file_exists( $head ) ) { | |
$git_ref = ABSPATH . str_replace( 'ref: ', '.git/', file_get_contents( $head ) ); | |
$ref = file_get_contents( trim( $git_ref ) ); | |
} | |
if ( $ref = trim( $ref ) ) { | |
printf( '<meta name="version" content="%s" />' . "\n", substr( $ref, 0, 6 ) ); | |
} | |
} | |
} | |
GitVersion::init(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment