public static function get_instance()
 {
     if (!isset(self::$instance)) {
         $c = __CLASS__;
         self::$instance = new $c();
     }
     return self::$instance;
 }
 private function s3($S3_bucket = null)
 {
     if (isset($this->s3)) {
         if (isset($S3_bucket) && $this->s3->current_bucket() !== $S3_bucket) {
             $this->s3->set_current_bucket($S3_bucket);
         }
         return $this->s3;
     }
     if ($this->options) {
         $s3 = new S3_helper(isset($this->options['access_key']) ? $this->options['access_key'] : null, isset($this->options['secret_key']) ? $this->options['secret_key'] : null, isset($this->options['region']) ? $this->options['region'] : null);
         if ($s3 && isset($S3_bucket)) {
             $s3->set_current_bucket($S3_bucket);
         }
         $this->s3 = $s3;
         return $s3;
     }
     return false;
 }
    public function options_page()
    {
        $nonce_action = 'update_options';
        $nonce_name = '_wpnonce_update_options';
        $option_keys = $this->option_keys();
        $option_keys = apply_filters('nephila_clavata_option_keys', $option_keys);
        self::$options = $this->get_option();
        $title = __('Nephila clavata', NephilaClavata::TEXT_DOMAIN);
        $iv = new InputValidator('POST');
        $iv->set_rules($nonce_name, 'required');
        // Update options
        if (!is_wp_error($iv->input($nonce_name)) && check_admin_referer($nonce_action, $nonce_name)) {
            // Get posted options
            $fields = array_keys($option_keys);
            foreach ($fields as $field) {
                switch ($field) {
                    case 'access_key':
                    case 'secret_key':
                        $iv->set_rules($field, array('trim', 'esc_html', 'required'));
                        break;
                    default:
                        $iv->set_rules($field, array('trim', 'esc_html'));
                        break;
                }
            }
            $options = $iv->input($fields);
            $err_message = '';
            foreach ($option_keys as $key => $field) {
                if (is_wp_error($options[$key])) {
                    $error_data = $options[$key];
                    $err = '';
                    foreach ($error_data->errors as $errors) {
                        foreach ($errors as $error) {
                            $err .= (!empty($err) ? '<br />' : '') . __('Error! : ', NephilaClavata::TEXT_DOMAIN);
                            $err .= sprintf(__(str_replace($key, '%s', $error), NephilaClavata::TEXT_DOMAIN), $field);
                        }
                    }
                    $err_message .= (!empty($err_message) ? '<br />' : '') . $err;
                }
                if (!isset($options[$key]) || is_wp_error($options[$key])) {
                    $options[$key] = '';
                }
            }
            if (empty($options['s3_url']) && !empty($options['bucket'])) {
                $options['s3_url'] = sprintf('http://%1$s.s3-website-%2$s.amazonaws.com', strtolower($options['bucket']), strtolower(str_replace('_', '-', $options['region'])));
            }
            if (!empty($options['s3_url']) && !preg_match('#^https?://#i', $options['s3_url'])) {
                $options['s3_url'] = 'http://' . preg_replace('#^//?#', '', $options['s3_url']);
            }
            $options['s3_url'] = untrailingslashit($options['s3_url']);
            if (NephilaClavata::DEBUG_MODE && function_exists('dbgx_trace_var')) {
                dbgx_trace_var($options);
            }
            // Update options
            if (self::$options !== $options) {
                update_option(self::OPTION_KEY, $options);
                if (self::$options['s3_url'] !== $options['s3_url']) {
                    global $wpdb;
                    $sql = $wpdb->prepare("delete from {$wpdb->postmeta} where meta_key in (%s, %s)", NephilaClavata::META_KEY, NephilaClavata::META_KEY . '-replace');
                    $wpdb->query($sql);
                }
                printf('<div id="message" class="updated fade"><p><strong>%s</strong></p></div>' . "\n", empty($err_message) ? __('Done!', NephilaClavata::TEXT_DOMAIN) : $err_message);
                self::$options = $options;
            }
            unset($options);
        }
        // Get S3 Object
        $s3 = S3_helper::get_instance();
        $s3->init(isset(self::$options['access_key']) ? self::$options['access_key'] : null, isset(self::$options['secret_key']) ? self::$options['secret_key'] : null, isset(self::$options['region']) ? self::$options['region'] : null);
        $regions = $this->regions;
        $buckets = false;
        if ($s3) {
            $regions = $s3->get_regions();
            $buckets = $s3->list_buckets();
        }
        if (!$buckets) {
            unset($option_keys['bucket']);
            unset($option_keys['s3_url']);
        }
        $storage_classes = $this->storage_classes;
        ?>
		<div class="wrap">
		<?php 
        screen_icon();
        ?>
		<h2><?php 
        echo esc_html($title);
        ?>
</h2>
		<form method="post" action="<?php 
        echo $this->admin_action;
        ?>
">
		<?php 
        echo wp_nonce_field($nonce_action, $nonce_name, true, false) . "\n";
        ?>
		<table class="wp-list-table fixed"><tbody>
		<?php 
        foreach ($option_keys as $field => $label) {
            $this->input_field($field, $label, array('regions' => $regions, 'buckets' => $buckets, 'storage_classes' => $storage_classes));
        }
        ?>
		</tbody></table>
		<?php 
        submit_button();
        ?>
		</form>
		</div>
<?php 
    }
    public function options_page()
    {
        $option_keys = $this->option_keys();
        $this->options = $this->get_option();
        $title = __('StaticPress S3 Option', self::TEXT_DOMAIN);
        // Get S3 Object
        $s3 = new S3_helper(isset($this->options['access_key']) ? $this->options['access_key'] : null, isset($this->options['secret_key']) ? $this->options['secret_key'] : null, isset($this->options['region']) ? $this->options['region'] : null);
        $regions = $this->regions;
        $buckets = false;
        if ($s3) {
            $regions = $s3->get_regions();
            $buckets = $s3->list_buckets();
        }
        if (!$buckets) {
            unset($option_keys['bucket']);
            unset($option_keys['s3_url']);
        }
        ?>
		<div class="wrap">
		<h2><?php 
        echo esc_html($title);
        ?>
</h2>
		<form method="post" action="<?php 
        echo $this->admin_action;
        ?>
">
		<?php 
        echo wp_nonce_field(self::NONCE_ACTION, self::NONCE_NAME, true, false) . "\n";
        ?>
		<table class="wp-list-table fixed"><tbody>
		<?php 
        foreach ($option_keys as $field => $label) {
            $this->input_field($field, $label, array('regions' => $regions, 'buckets' => $buckets));
        }
        ?>
		</tbody></table>
		<?php 
        submit_button();
        ?>
		</form>
		</div>
<?php 
    }
 private function s3($S3_bucket = null)
 {
     if (isset(self::$s3)) {
         if (isset($S3_bucket) && self::$s3->current_bucket() !== $S3_bucket) {
             self::$s3->set_current_bucket($S3_bucket);
         }
         return self::$s3;
     }
     if (self::$options) {
         $s3 = S3_helper::get_instance();
         $s3->init(isset(self::$options['access_key']) ? self::$options['access_key'] : null, isset(self::$options['secret_key']) ? self::$options['secret_key'] : null, isset(self::$options['region']) ? self::$options['region'] : null);
         if ($s3 && isset($S3_bucket)) {
             $s3->set_current_bucket($S3_bucket);
         }
         self::$s3 = $s3;
         return $s3;
     }
     return false;
 }