/**
 * Validate settings for the tab.
 */
function dg_validate_settings($values)
{
    global $dg_options;
    $ret = $dg_options;
    include_once DG_PATH . 'inc/class-gallery.php';
    $thumbs_cleared = false;
    // handle gallery shortcode defaults
    $errs = array();
    $ret['gallery'] = DG_Gallery::sanitizeDefaults(null, $values['gallery_defaults'], $errs);
    foreach ($errs as $k => $v) {
        add_settings_error(DG_OPTION_NAME, str_replace('_', '-', $k), $v);
    }
    // handle setting width
    if (isset($values['thumbnail_generation']['width'])) {
        $width = (int) $values['thumbnail_generation']['width'];
        if ($width > 0) {
            $ret['thumber']['width'] = $width;
        } else {
            add_settings_error(DG_OPTION_NAME, 'thumber-width', __('Invalid width given: ', 'document-gallery') . $values['thumbnail_generation']['width']);
        }
        unset($values['thumbnail_generation']['width']);
    }
    // handle setting height
    if (isset($values['thumbnail_generation']['height'])) {
        $height = (int) $values['thumbnail_generation']['height'];
        if ($height > 0) {
            $ret['thumber']['height'] = $height;
        } else {
            add_settings_error(DG_OPTION_NAME, 'thumber-height', __('Invalid height given: ', 'document-gallery') . $values['thumbnail_generation']['height']);
        }
        unset($values['thumbnail_generation']['width']);
    }
    // delete thumb cache to force regeneration if max dimensions changed
    if ($ret['thumber']['width'] !== $dg_options['thumber']['width'] || $ret['thumber']['height'] !== $dg_options['thumber']['height']) {
        DG_Thumb::purgeThumbs();
    }
    // handle setting the active thumbers
    foreach (array_keys($ret['thumber']['active']) as $k) {
        $ret['thumber']['active'][$k] = isset($values['thumbnail_generation'][$k]);
    }
    // if new thumbers available, clear failed thumbnails for retry
    if (!$thumbs_cleared) {
        DG_Thumb::purgeFailedThumbs();
    }
    // handle modified CSS
    if (trim($ret['css']['text']) !== trim($values['css'])) {
        $ret['css']['text'] = trim($values['css']);
    }
    return $ret;
}
/**
 * Validate settings for the tab.
 */
function dg_validate_settings($values)
{
    global $dg_options;
    $ret = $dg_options;
    $responseArr = array('result' => false);
    if (isset($values['entry'])) {
        $ID = intval($values['entry']);
    } else {
        $ID = -1;
    }
    // Thumbnail(s) cleanup;
    // cleanup value is a marker
    if (isset($values['cleanup']) && isset($values['ids'])) {
        $deleted = array_values(array_intersect(array_keys(DG_Thumb::getThumbs()), $values['ids']));
        DG_Thumb::purgeThumbs($deleted);
        $responseArr['result'] = true;
        $responseArr['deleted'] = $deleted;
    } elseif (isset($values['title']) && $ID != -1) {
        $attachment = array('ID' => $ID, 'post_title' => rawurldecode(addslashes($values['title'])));
        if (wp_update_post($attachment)) {
            $responseArr['result'] = true;
        }
    } elseif (isset($values['description']) && $ID != -1) {
        $attachment = array('ID' => $ID, 'post_content' => rawurldecode(addslashes($values['description'])));
        if (wp_update_post($attachment)) {
            $responseArr['result'] = true;
        }
    } elseif (isset($values['upload']) && isset($_FILES['file']) && array_key_exists($ID, DG_Thumb::getThumbs())) {
        $uploaded_filename = DG_Admin::validateUploadedFile();
        if ($uploaded_filename && ($thumb = DG_Thumber::setThumbnail($ID, $uploaded_filename))) {
            $responseArr['result'] = true;
            $responseArr['url'] = $thumb->getUrl();
        }
    }
    if (defined('DOING_AJAX') && DOING_AJAX) {
        @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
        echo wp_json_encode($responseArr);
        add_filter('wp_redirect', 'dg_exit', 1, 0);
    }
    return $ret;
}
 /**
  * Removes all metadata related to a thumbnail for the given attachment ID(s). This allows
  * the plugin to attempt to re-generate the thumbnail for this attachment next time it
  * is requested in a gallery or through some other means.
  *
  * @param int|int[] $ids Which thumbnails to delete.
  */
 public static function deleteThumbnails($ids)
 {
     DG_Thumb::purgeThumbs($ids);
 }
 /**
  * Runs when DG is uninstalled for an individual blog.
  */
 private static function _uninstall($blog)
 {
     DG_Thumb::purgeThumbs(null, $blog);
     DocumentGallery::deleteOptions($blog);
 }