Skip to content

Instantly share code, notes, and snippets.

@aendra-rininsland
Created August 13, 2014 00:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aendra-rininsland/17476da9249c92d25584 to your computer and use it in GitHub Desktop.
Save aendra-rininsland/17476da9249c92d25584 to your computer and use it in GitHub Desktop.
Install Bower dependencies on WordPress plugin activation
<?php
register_activation_hook(__FILE__, 'activate_my_plugin');
function activate_my_plugin() {
// Some basic securifying
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
// Is Node Bower installed on server? Use that if so.
if (`which bower`) {
exec('cd ' . dirname(__FILE__) . ' && bower install');
} else { // Otherwise download and use BowerPHP
if (!file_exists(dirname(__FILE__) . '/bowerphp')) {
// include WP_Http to download bowerphp.
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
$request = new WP_Http;
$result = $request->request('http://bowerphp.org/bowerphp.phar');
file_put_contents(dirname(__FILE__) . '/bowerphp', $result['body']);
exec('chmod ugo+x ' . dirname(__FILE__) . '/bowerphp');
}
//BowerPHP install
exec('cd ' . dirname(__FILE__) . ' && ' . dirname(__FILE__) . '/bowerphp install');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment