Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Last active June 27, 2020 05:02
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 joshuadavidnelson/a9d38f454247e9eb91229c855cbba684 to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/a9d38f454247e9eb91229c855cbba684 to your computer and use it in GitHub Desktop.
Create a commit for every plugin update.
<?php
/**
* Plugin Name: Commit on Update
* Plugin Author: Joshua David Nelson
**/
defined( 'ABSPATH' ) or die( 'Nope!' );
class Commit_On_Update {
/**
* The version prior to update.
*
* @since 1.0.0
*
* @var string|bool
*/
var $prev_version = false;
/**
* The version after the update.
*
* @since 1.0.0
*
* @var string|bool
*/
var $new_version = false;
/**
* The name of the thing being updated.
*
* @since 1.0.0
*
* @var string|bool
*/
var $name = false;
/**
* The final commit message.
*
* @since 1.0.0
*
* @var string|bool
*/
var $commit_message = false;
/**
* The commit author.
*
* @since 1.0.0
*
* @var string
*/
var $author = 'Joshua Nelson <josh@joshuadnelson.com>';
/**
* Get the party started.
*
* @since 1.0.0
*/
function init() {
// Grab the plugin information before it updates
// upgrader_process_complete
add_filter( 'upgrader_source_selection', array( $this, 'get_previous_version_before_upgrade' ), 10, 4 );
// After we've updated, make the commit
add_filter( 'upgrader_post_install', array( $this, 'commit_after_update' ), 10, 3 );
}
/**
* Store some information on the plugin/theme before being updated.
*
* @since 1.0.0
*
* @param string $source
* @param string $remote_source
* @param object $object
* @param array $hook_extra
*
* @return string $source
*/
function get_previous_version_before_upgrade( $source, $remote_source, $object, $hook_extra ) {
if( ! isset( $hook_extra['plugin'] ) )
return $source;
// Get the plugin version before the update and log it
$this->prev_version = isset( $object->skin->plugin_info['Version'] ) ? $object->skin->plugin_info['Version'] : $this->get_plugin_version_from_hook_extra( $hook_extra );
// Get the plugin name
$this->name = isset( $object->skin->plugin_info['Name'] ) ? $object->skin->plugin_info['Name'] : false;
return $source;
}
/**
* Fallback method of extracting plugin version.
*
* @since 1.0.0
*
* @param array $hook_extra
*
* @return string
*/
function get_plugin_version_from_hook_extra( $hook_extra ) {
$version = 'Unknown';
if( ! isset( $hook_extra['plugin'] ) ) {
return $version;
}
$plugin_data = $this->get_plugin_data( WP_PLUGIN_DIR . '/' . $hook_extra['plugin'] );
$plugin_version = isset( $plugin_data['Version'] ) ? $plugin_data['Version'] : 'Unknown';
return $plugin_version;
}
/**
* A wrapper function for the WordPress function.
*
* @since 1.0.0
*
* @param string $path
*
* @return array
*/
function get_plugin_data( $path ) {
$data = get_plugin_data( $path, false, false );
return $data;
}
/**
* After a successful update, commit the changes.
*
* @since 1.0.0
*
* @param bool $boolean
* @param array $hook_extra
* @param string $result
*
* @return bool
*/
function commit_after_update( $boolean, $hook_extra, $result ) {
if( ! isset( $hook_extra['plugin'] ) || ! isset( $result['destination_name'] ) )
return $boolean;
// Get new version, post update
$plugin_data = $this->get_plugin_data( WP_PLUGIN_DIR . '/' . $hook_extra['plugin'] );
if( ! isset( $plugin_data['Name'] ) || ! isset( $plugin_data['Version'] ) )
return $boolean;
// New Version
$this->new_version = $plugin_data['Version'];
// Plugin Name fallback
if( ! $this->name )
$this->name = $plugin_data['Name'];
// Add the plugin folder to the git stage
$folder = '"' . WP_PLUGIN_DIR . '/' . $result['destination_name'] . '"';
// Commit Message
$this->commit_message = 'Update Plugin: ' . $this->name . ' from ' . $this->prev_version . ' to ' . $this->new_version;
// Soft reset to avoid commit anything else
$this->execute_command( 'git reset --soft' );
// Add the folder to stage all the changes
$this->execute_command( 'git add --all ' . $folder );
// Commit the changes - for some reason this isn't working?
$this->execute_command( 'git commit -m "' . $this->commit_message . '" --author="' . $this->author . '" -- ' . $folder );
return $boolean;
}
/**
* Execute the shell command.
*
* @since 1.0.0
*
* @param string $command
*/
function execute_command( string $command ) {
system( $command );
}
}
global $_commit_on_update;
$_commit_on_update = new Commit_On_Update;
$_commit_on_update->init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment