Commit 4a92a56c authored by Michael Pretty's avatar Michael Pretty
Browse files

Merge pull request #1 from voceconnect/refactor_settings_api

refactor to use the voce-settings-api plugin
parents 4bb11b1e 9e085b9a
[submodule "voce-settings-api"]
path = voce-settings-api
url = git@github.com:voceconnect/voce-settings-api.git
CDN Rewrite
===========
Contributors: voceplatforms
Tags: cdn, rewrite
Requires at least: 3.3
Tested up to: 3.5
Stable tag: 0.1.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
## Description
This plugin allows you to rewrite the root url of assets, css, and JavaScript files. This allows you to load these resources from an external URL improving the page load time by taking advantage of parallel browser requests.
## Installation
### As standard plugin:
> See [Installing Plugins](http://codex.wordpress.org/Managing_Plugins#Installing_Plugins).
\ No newline at end of file
......@@ -8,14 +8,12 @@ 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 {
const OPTION_GENERAL = 'cdn_general';
private $submenu_general;
private $cdn_root_url;
private $file_extensions;
......@@ -44,11 +42,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'));
$this->add_options_page();
if ('' == $this->file_extensions || '' == $this->cdn_root_url) {
add_action('admin_notices', array($this, 'settings_warning'));
return;
......@@ -89,36 +87,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');
}
/**
* callback to display submenu_external
*
* @return void
*/
function submenu_general() {
?>
<div class="wrap">
<h2>CDN Rewrite Settings</h2>
<form method="post" action="options.php">
<?php settings_fields(self::OPTION_GENERAL); ?>
<?php do_settings_sections($this->submenu_general); ?>
<p class="submit">
<input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
</p>
</form>
</div>
<?php
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' );
}
/**
......
=== CDN Rewrite ===
Contributors: voceplatforms
Tags: cdn, rewrite
Requires at least: 3.3
Tested up to: 3.5
Stable tag: 0.1.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Rewrite the root url of assets, css, and JavaScript files.
== Description ==
This plugin allows you to rewrite the root url of assets, css, and JavaScript files. This allows you to load these resources from an external URL improving the page load time by taking advantage of parallel browser requests.
== Installation ==
1. Upload `cdn-rewrite` to the `/wp-content/plugins/` directory
1. Activate the plugin through the 'Plugins' menu in WordPress
1. Once the plugin has been activated, navigate to the CDN Rewrite settings page and set the appropriate settings.
\ No newline at end of file
Subproject commit 4ef6990f9cbe89ac10be1d17678fbadb3c045bd1
<?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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment