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 
    }
Beispiel #2
0
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
if (!class_exists('S3_helper')) {
    require dirname(__FILE__) . '/includes/class-S3_helper.php';
}
if (!class_exists('NephilaClavata_Admin')) {
    require dirname(__FILE__) . '/includes/class-NephilaClavata_Admin.php';
}
if (!class_exists('NephilaClavata')) {
    require dirname(__FILE__) . '/includes/class-NephilaClavata.php';
}
load_plugin_textdomain(NephilaClavata::TEXT_DOMAIN, false, dirname(plugin_basename(__FILE__)) . '/languages/');
// Go Go Go!
$nephila_clavata = NephilaClavata::get_instance();
$nephila_clavata->init(NephilaClavata_Admin::get_option());
$nephila_clavata->add_hook();
if (is_admin()) {
    $nephila_clavata_admin = NephilaClavata_Admin::get_instance();
    $nephila_clavata_admin->init();
    $nephila_clavata_admin->add_hook();
}