/**
  * Initializes the static values for this class.
  */
 public static function init()
 {
     $options = DG_Thumber::getOptions();
     if ($options['active']['thumber-co']) {
         parent::init();
         self::$webhook = admin_url('admin-post.php?action=' . self::ThumberAction);
         self::$client = DG_ThumberClient::getInstance();
     }
 }
/**
 * Validate settings for the tab.
 */
function dg_validate_settings($values)
{
    global $dg_options;
    $ret = $dg_options;
    $has_changed = $has_error = false;
    $old_uid = $dg_options['thumber-co']['uid'];
    $old_secret = $dg_options['thumber-co']['secret'];
    $old_subscription = $dg_options['thumber-co']['subscription'];
    // handle setting the UID
    if (isset($values['uid']) && 0 !== strcmp($values['uid'], $ret['thumber-co']['uid'])) {
        static $guid_regex = '/^[^\\W_]{8}-[^\\W_]{4}-[^\\W_]{4}-[^\\W_]{4}-[^\\W_]{12}$/';
        if ('' === $values['uid'] || preg_match($guid_regex, $values['uid'])) {
            $ret['thumber-co']['uid'] = '' !== $values['uid'] ? $values['uid'] : null;
            $has_changed = true;
        } else {
            add_settings_error(DG_OPTION_NAME, 'thumber-co-uid', __('Invalid user ID given: ', 'document-gallery') . $values['uid']);
            $has_error = true;
        }
    }
    // handle setting the user secret
    if (isset($values['secret']) && 0 !== strcmp($values['secret'], $ret['thumber-co']['secret'])) {
        static $secret_regex = '/^[-A-Z\\d]+$/i';
        if ('' === $values['secret'] || preg_match($secret_regex, $values['secret'])) {
            $ret['thumber-co']['secret'] = '' !== $values['secret'] ? $values['secret'] : null;
            $has_changed = true;
        } else {
            add_settings_error(DG_OPTION_NAME, 'thumber-co-secret', __('Invalid user secret given: ', 'document-gallery') . $values['secret']);
            $has_error = true;
        }
    }
    // test whether we can actually auth w/ given credentials
    if ($has_changed && !$has_error) {
        if (isset($ret['thumber-co']['uid']) && isset($ret['thumber-co']['secret'])) {
            include_once DG_PATH . 'inc/thumbers/thumber-co/class-thumber-co-thumber.php';
            // NOTE: We're tricking getSubscription to re-query subscription w/ new credentials
            $dg_options['thumber-co']['subscription'] = null;
            $client = DG_ThumberClient::getInstance();
            $client->setUid($ret['thumber-co']['uid']);
            $client->setUserSecret($ret['thumber-co']['secret']);
            $ret['thumber-co']['subscription'] = $client->getSubscription(false);
            if (!isset($ret['thumber-co']['subscription'])) {
                $ret['thumber-co']['uid'] = $old_uid;
                $ret['thumber-co']['secret'] = $old_secret;
                $ret['thumber-co']['subscription'] = $old_subscription;
                add_settings_error(DG_OPTION_NAME, 'thumber-co-authenticate', __('Failed to authenticate with given user ID and secret.', 'document-gallery'));
            } else {
                // auto-enable if we've got newly-working credentials
                $ret['thumber']['active']['thumber-co'] = true;
            }
        } else {
            // auto-disable if we've got newly-broken credentials
            $ret['thumber']['active']['thumber-co'] = false;
        }
    }
    return $ret;
}