get_option(); $this->plugin_basename = Argiope_Amoena::plugin_basename(); } public function add_hook(){ add_action('admin_menu', array($this, 'admin_menu')); add_filter('plugin_action_links', array($this, 'plugin_setting_links'), 10, 2 ); } static public function option_keys(){ return array( 'access_key' => __('AWS Access Key', Argiope_Amoena::TEXT_DOMAIN), 'secret_key' => __('AWS Secret Key', Argiope_Amoena::TEXT_DOMAIN), 'region' => __('AWS Region', Argiope_Amoena::TEXT_DOMAIN), 'bucket' => __('S3 Bucket', Argiope_Amoena::TEXT_DOMAIN), 's3_url' => __('S3 URL', Argiope_Amoena::TEXT_DOMAIN), 'storage_class' => __('Storage Class', Argiope_Amoena::TEXT_DOMAIN), ); } static public function get_option(){ $options = get_option(self::OPTION_KEY); foreach (array_keys(self::option_keys()) as $key) { if (!isset($options[$key]) || is_wp_error($options[$key])) $options[$key] = ''; } return $options; } //************************************************************************************** // Add Admin Menu //************************************************************************************** public function admin_menu() { global $wp_version; $title = __('Argiope amoena', Argiope_Amoena::TEXT_DOMAIN); $this->admin_hook = add_options_page($title, $title, 'manage_options', self::OPTION_PAGE, array($this, 'options_page')); $this->admin_action = admin_url( apply_filters( 'argiope_amoena_admin_url', '/options-general.php') ) . '?page=' . self::OPTION_PAGE; } public function options_page(){ $nonce_action = 'update_options'; $nonce_name = '_wpnonce_update_options'; $option_keys = $this->option_keys(); $option_keys = apply_filters( 'argiope_amoena_option_keys', $option_keys ); self::$options = $this->get_option(); $title = __('Argiope amoena', Argiope_Amoena::TEXT_DOMAIN); $av = new Argiope_Validator('POST'); $av->set_rules($nonce_name, 'required'); // Update options if (!is_wp_error($av->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': $av->set_rules($field, array('trim','esc_html','required')); break; default: $av->set_rules($field, array('trim','esc_html')); break; } } $options = $av->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) ? '
' : '') . __('Error! : ', Argiope_Amoena::TEXT_DOMAIN); $err .= sprintf( __(str_replace($key, '%s', $error), Argiope_Amoena::TEXT_DOMAIN), $field ); } } $err_message .= (!empty($err_message) ? '
' : '') . $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 (Argiope_Amoena::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)", Argiope_Amoena::META_KEY, Argiope_Amoena::META_KEY.'-replace' ); $wpdb->query($sql); } printf( '

%s

'."\n", empty($err_message) ? __('Done!', Argiope_Amoena::TEXT_DOMAIN) : $err_message ); self::$options = $options; } unset($options); } // Get S3 Object $s3 = Argiope_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; ?>

$label) { $this->input_field($field, $label, array('regions' => $regions, 'buckets' => $buckets, 'storage_classes' => $storage_classes)); } ?>
'."\n", $field, $label); $input_field = sprintf(''."\n", $field, esc_attr(self::$options[$field])); switch ($field) { case 'region': if ($regions && count($regions) > 0) { $input_field = sprintf(''; } break; case 'bucket': if ($buckets && count($buckets) > 0) { $input_field = sprintf(''; } break; /* case 'storage_class': if ($storage_classes && count($storage_classes) > 0) { $input_field = sprintf(''; } break; */ } echo "\n{$label}{$input_field}\n"; } //************************************************************************************** // Add setting link //************************************************************************************** public function plugin_setting_links($links, $file) { if ($file === $this->plugin_basename) { $settings_link = '' . __('Settings') . ''; array_unshift($links, $settings_link); // before other links } return $links; } }