From 355b8a97da4147679d51f2d10cb64cca7e56d083 Mon Sep 17 00:00:00 2001 From: John Ciacia <john.ciacia@gmail.com> Date: Tue, 30 Jul 2013 13:45:11 -0500 Subject: [PATCH] refactor to use the voce-settings-api plugin --- .gitmodules | 3 + cdn-rewrite.php | 24 +++-- voce-settings-api | 1 + voce-settings.php | 218 ---------------------------------------------- 4 files changed, 15 insertions(+), 231 deletions(-) create mode 100644 .gitmodules create mode 160000 voce-settings-api delete mode 100644 voce-settings.php diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..ae37baf --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "voce-settings-api"] + path = voce-settings-api + url = git@github.com:voceconnect/voce-settings-api.git diff --git a/cdn-rewrite.php b/cdn-rewrite.php index 5ed3af7..3247bc6 100644 --- a/cdn-rewrite.php +++ b/cdn-rewrite.php @@ -8,7 +8,7 @@ Author: Chris Scott, Michael Pretty Author URI: http://voceconnect.com/ */ -require_once('voce-settings.php'); +require_once('voce-settings-api/voce-settings-api.php'); class CDN_Rewrite { @@ -44,11 +44,11 @@ class CDN_Rewrite { } public function initialize() { - if (!class_exists('Voce_Settings')) { + if (!class_exists('Voce_Settings_Api')) { return; } - add_action('admin_menu', array($this, 'add_options_page')); + self::add_options_page(); if ('' == $this->file_extensions || '' == $this->cdn_root_url) { add_action('admin_notices', array($this, 'settings_warning')); return; @@ -89,16 +89,14 @@ class CDN_Rewrite { * @return void */ public function add_options_page() { - $this->submenu_general = add_options_page('CDN Rewrite', 'CDN Rewrite', 'manage_options', self::OPTION_GENERAL, array($this, 'submenu_general')); - $settings = new Voce_Settings(self::OPTION_GENERAL, self::OPTION_GENERAL); - - $section = $settings->add_section('api', 'CDN Rewrite Settings', $this->submenu_general); - $section->add_field('root_url', 'CDN Root URL (required)', 'field_input', array('description' => 'The base URL of the CDN.')); - $section->add_field('file_extensions', 'File Extensions (required)', 'field_input'); - $section->add_field('css_root_url', 'CDN Root URL for CSS Files (optional)', 'field_input', array('description' => 'The base URL of the CDN for CSS Files.')); - $section->add_field('css_file_extensions', 'File Extensions for CSS Files (optional)', 'field_input'); - $section->add_field('js_root_url', 'CDN Root URL for JS Files (optional, defaults to Root URL)', 'field_input', array('description' => 'The base URL of the CDN for JS Files.')); - $section->add_field('js_file_extensions', 'File Extensions for JS Files (optional, defaults to Root URL)', 'field_input'); + Voce_Settings_API::GetInstance()->add_page('CDN Rewrite', 'CDN Rewrite', self::OPTION_GENERAL, 'manage_options', '', 'options-general.php' ) + ->add_group( 'CDN Rewrite Settings', 'cdn_general' ) + ->add_setting( 'CDN Root URL (required)', 'root_url', array( 'description' => 'The base URL of the CDN.' ) )->group + ->add_setting( 'File Extensions (required)', 'file_extensions' )->group + ->add_setting( 'CDN Root URL for CSS Files (optional)', 'css_root_url', array( 'description' => 'The base URL of the CDN for CSS Files.' ) )->group + ->add_setting( 'File Extensions for CSS Files (optional)', 'css_file_extensions' )->group + ->add_setting( 'CDN Root URL for JS Files (optional, defaults to Root URL)', 'js_root_url', array( 'description' => 'The base URL of the CDN for JS Files.' ) )->group + ->add_setting( 'File Extensions for JS Files (optional, defaults to Root URL)', 'js_file_extensions' ); } /** diff --git a/voce-settings-api b/voce-settings-api new file mode 160000 index 0000000..4ef6990 --- /dev/null +++ b/voce-settings-api @@ -0,0 +1 @@ +Subproject commit 4ef6990f9cbe89ac10be1d17678fbadb3c045bd1 diff --git a/voce-settings.php b/voce-settings.php deleted file mode 100644 index 6a94205..0000000 --- a/voce-settings.php +++ /dev/null @@ -1,218 +0,0 @@ -<?php -/* Copyright 2010 Chris Scott (cscott@voceconnect.com) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License, version 2, as - published by the Free Software Foundation. - - 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('Voce_Settings')) { - class Voce_Settings { - - private $page_name; - private $option_name; - private $page; - private $section; - - public function __construct($option_group, $option_name, $sanitize_callback = '') { - register_setting($option_group, $option_name, $sanitize_callback); - $this->option_name = $option_name; - } - - public function add_section($name, $title, $page, $callback = null) { - // use blank output to allow not setting a callback if no output is desired between heading and fields - $callback = (null === $callback) ? create_function('', 'echo \'\';') : $callback; - - add_settings_section($name, $title, $callback, $page); - $this->section = $name; - $this->page = $page; - return $this; - } - - public function add_field($id, $label, $type, $args = null, $page = null, $section = null) { - $section = (null === $section) ? $this->section : $section; - $page = (null === $page) ? $this->page : $page; - $this->add_settings_field( - $id, - $label, - $type, - $page, - $section, - $args - ); - } - - - /** - * helper to use the settings API to add a field - * - * @param string $id the HTML ID for the field - * @param string $label the label tag text - * @param string $display_callback the function to use for the field display - * @param string $extra_args extra args to pass to the callback - * @return void - */ - public function add_settings_field($id, $label, $display_callback, $page, $section, $extra_args = null) { - - $args = array( - 'id' => $id, - 'label_for' => $id - ); - - if (is_array($extra_args)) { - $args += $extra_args; - } - - add_settings_field( - $id, - $label, - array(&$this, $display_callback), - $page, - $section, - $args - ); - } - - - /** - * checkbox callback for settings API - * - * @param string $args - * @return string checkbox - */ - public function field_checkbox($args) { - $options = get_option($this->option_name); - - $defaults = array('prepend_field' => '', 'append_field' => '', 'class' => ''); - $args = wp_parse_args($args, $defaults); - extract($args); - - $description = ($description) ? sprintf('<p><span class="description">%s</span></p>', $description) : ''; - - $checked = checked($options[$id], '1', false); - - echo sprintf( - "%s<input id='%s' name='{$this->option_name}[%s]' type='checkbox' value='1' %s class='%s' />%s%s", - $prepend_field, - $id, - $id, - $checked, - $class, - $append_field, - $description - ); - } - - /** - * input type text callback for settings API - * - * @param string $args - * @return string input - */ - public function field_input($args) { - $options = get_option($this->option_name); - - $defaults = array('prepend_field' => '', 'append_field' => '', 'class' => 'regular-text', 'type' => 'text'); - $args = wp_parse_args($args, $defaults); - extract($args); - - $description = (isset($description) && $description) ? sprintf('<p><span class="description">%s</span></p>', $description) : ''; - - if (!isset($value)) { - if (isset($options[$id])) { - $value = $options[$id]; - } else { - $value = ''; - } - } - - echo sprintf( - "%s<input id='%s' name='{$this->option_name}[%s]' type='%s' class='%s' value='%s' />%s%s", - $prepend_field, - $id, - $id, - $type, - $class, - esc_attr($value), - $append_field, - $description - ); - } - - /** - * textarea callback for settings API - * - * @param string $args - * @return string textarea - */ - public function field_textarea($args) { - $options = get_option($this->option_name); - $id = $args['id']; - - $defaults = array('prepend_field' => '', 'append_field' => '', 'columns' => 40, 'rows' => 8, 'class' => 'large-text'); - $args = wp_parse_args($args, $defaults); - extract($args); - - $description = (isset($description) && $description) ? sprintf('<p><span class="description">%s</span></p>', $description) : ''; - - echo sprintf( - "%s<textarea id='%s' name='{$this->option_name}[%s]' columns='%s' rows='%s' class='%s' />%s</textarea>%s%s", - $prepend_field, - $id, - $id, - $columns, - $rows, - $class, - esc_attr($options[$id]), - $append_field, - $description - ); - } - - public function field_radio($args) { - $options = get_option($this->option_name); - $id = $args['id']; - - $defaults = array('type' => 'radio', 'class' => ''); - $args = wp_parse_args($args, $defaults); - extract($args); - - if (!$items) { - return; - } - - echo '<fieldset><p>'; - foreach ($items as $item) { - if (!empty($options[$id])) { - $checked = checked($options[$id], $item['value'], false); - } else { - $checked = false; - } - - echo sprintf( - "<label> <input id='%s' name='{$this->option_name}[%s]' type='%s', class='%s' value='%s' %s /> %s</label><br />", - $id, - $id, - $type, - $class, - esc_attr($item['value']), - $checked, - esc_html($item['text']) - ); - } - echo '</fieldset></p>'; - - $description = (isset($description) && $description) ? sprintf('<p><span class="description">%s</span></p>', $description) : ''; - echo $description; - } - } -} \ No newline at end of file -- GitLab