View source
<?php
define('TINIFY_PHP_VERSION', '1.5.2');
define('TINIFY_PHP_DOWNLOAD_URI', 'https://github.com/tinify/tinify-php/archive/' . TINIFY_PHP_VERSION . '.zip');
function tinypng_drush_command() {
$items = array();
$items['tinypng-library'] = array(
'callback' => 'drush_tinypng_tinify_download',
'description' => dt('Download and install the Tinify PHP library.'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'arguments' => array(
'path' => dt('Optional. A path where to install the Tinify PHP library. If omitted Drush will use the default location.'),
),
);
return $items;
}
function tinypng_drush_help($section) {
switch ($section) {
case 'drush:tinypng-library':
return dt('Download and install the Tinify PHP library from https://github.com/tinify/tinify-php, default location is sites/all/libraries.');
}
}
function drush_tinypng_post_pm_enable() {
$modules = func_get_args();
if (in_array('tinypng', $modules)) {
drush_tinypng_tinify_download();
}
}
function drush_tinypng_tinify_download() {
$args = func_get_args();
if (!empty($args[0])) {
$path = $args[0];
}
else {
$path = 'sites/all/libraries';
}
if (!is_dir($path)) {
drush_op('mkdir', $path);
drush_log(dt('Directory @path was created', array(
'@path' => $path,
)), 'notice');
}
$olddir = getcwd();
chdir($path);
if ($filepath = drush_download_file(TINIFY_PHP_DOWNLOAD_URI, TINIFY_PHP_VERSION . '.zip')) {
$filename = basename($filepath);
$dirname = basename($filepath, '.zip');
if (is_dir($dirname) || is_dir(TINIFY_PHP_DIRNAME)) {
drush_delete_dir($dirname, TRUE);
drush_delete_dir(TINIFY_PHP_DIRNAME, TRUE);
drush_log(dt('A existing Tinify PHP library was deleted from @path', array(
'@path' => $path,
)), 'notice');
}
drush_tarball_extract($filename, $dirname);
drush_move_dir(TINIFY_PHP_VERSION . '/' . TINIFY_PHP_DIRNAME . '-' . TINIFY_PHP_VERSION, TINIFY_PHP_DIRNAME, TRUE);
drush_delete_dir(TINIFY_PHP_VERSION);
@chmod($filename, 0777);
unlink($filename);
}
if (is_dir(TINIFY_PHP_DIRNAME)) {
drush_log(dt('Tinify PHP library has been installed in @path', array(
'@path' => $path,
)), 'success');
}
else {
drush_log(dt('Drush was unable to install the Tinify PHP library to @path', array(
'@path' => $path,
)), 'error');
}
chdir($olddir);
}