You are here

function imagecache_drush_preset_flush in ImageCache 6.2

Drush callback to perform actual imagecache preset flush.

1 string reference to 'imagecache_drush_preset_flush'
imagecache_drush_command in ./imagecache.drush.inc
Implementation of hook_drush_command().

File

./imagecache.drush.inc, line 45

Code

function imagecache_drush_preset_flush() {
  $args = func_get_args();

  // Rebuild imagecache presets.
  foreach (imagecache_presets(TRUE) as $preset) {
    $preset_names[] = $preset['presetname'];
  }
  if (empty($args)) {
    $choice = drush_choice($preset_names, 'Enter a number to choose which preset to flush.');
    if ($choice !== FALSE) {
      $args[] = $preset_names[$choice];
    }
  }
  else {

    // Implement 'all'
    if (count($args) == 1 && $args[0] == 'all') {
      $args = $preset_names;
    }
  }

  // Remove any invalid preset names and report them as errors.
  $not_found = array_diff($args, $preset_names);
  $args = array_intersect($args, $preset_names);
  if ($not_found) {
    drush_log(dt('Preset(s) not found: @presets', array(
      '@presets' => implode($not_found, ' '),
    )), 'error');
  }
  if (empty($args)) {
    return FALSE;
  }
  $path = drush_get_context('DRUSH_DRUPAL_ROOT') . '/' . file_directory_path() . '/imagecache/';
  foreach ($args as $arg) {

    // Load preset.
    if ($preset = imagecache_preset_by_name($arg)) {

      // This mimics the logic inside of the function
      // imagecache_preset_flush(), but without the access check.
      $presetdir = $path . $preset['presetname'];
      if (is_dir($presetdir)) {
        _imagecache_recursive_delete($presetdir);
        drush_log(dt('Flushed "@preset" preset.', array(
          '@preset' => $arg,
        )), 'ok');
      }
      else {
        drush_log(dt('Cache for preset "@preset" was already empty.', array(
          '@preset' => $arg,
        )), 'ok');
      }
    }
  }
  return TRUE;
}