<?php
/*
Plugin Name: GYS Themed Categories
Plugin URI: http://rumleydesign.com/plugins/gys-themed-categories-2/
Description: This plugin allows you to assign themes to each of your Wordpress categories. To assign themes to your categories, just go to Posts->Categories and you'll see a drop-down menu of available themes at the bottom of the form.
Author: Luke Rumley / Mike Lopez
Version: 2.6
Author URI: http://rumleydesign.com/
*/
function rd_url_to_postid($url) {
global $wp_rewrite;
$url = apply_filters('url_to_postid', $url);
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
if (preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values)) {
$id = absint($values[2]);
if ($id)
return $id;
}
// Check to see if we are using rewrite rules
$rewrite = $wp_rewrite -> wp_rewrite_rules();
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
if (empty($rewrite))
return 0;
// Get rid of the #anchor
$url_split = explode('#', $url);
$url = $url_split[0];
// Get rid of URL ?query=string
$url_split = explode('?', $url);
$url = $url_split[0];
// Add 'www.' if it is absent and should be there
if (false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.'))
$url = str_replace('://', '://www.', $url);
// Strip 'www.' if it is present and shouldn't be
if (false === strpos(home_url(), '://www.'))
$url = str_replace('://www.', '://', $url);
// Strip 'index.php/' if we're not using path info permalinks
if (!$wp_rewrite -> using_index_permalinks())
$url = str_replace('index.php/', '', $url);
if (false !== strpos($url, home_url())) {
// Chop off http://domain.com
$url = str_replace(home_url(), '', $url);
} else {
// Chop off /path/to/blog
$home_path = parse_url(home_url());
$home_path = isset($home_path['path']) ? $home_path['path'] : '';
$url = str_replace($home_path, '', $url);
}
// Trim leading and lagging slashes
$url = trim($url, '/');
$request = $url;
// Look for matches.
$request_match = $request;
foreach ((array)$rewrite as $match => $query) {
// If the requesting file is the anchor of the match, prepend it
// to the path info.
if (!empty($url) && ($url != $request) && (strpos($match, $url) === 0))
$request_match = $url . '/' . $request;
if (preg_match("!^$match!", $request_match, $matches)) {
// Got a match.
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
// Filter out non-public query vars
global $wp;
parse_str($query, $query_vars);
$query = array();
foreach ((array) $query_vars as $key => $value) {
if (in_array($key, $wp -> public_query_vars))
$query[$key] = $value;
}
// Taken from class-wp.php
foreach ($GLOBALS['wp_post_types'] as $post_type => $t)
if ($t -> query_var)
$post_type_query_vars[$t -> query_var] = $post_type;
foreach ($wp->public_query_vars as $wpvar) {
if (isset($wp -> extra_query_vars[$wpvar]))
$query[$wpvar] = $wp -> extra_query_vars[$wpvar];
elseif (isset($_POST[$wpvar]))
$query[$wpvar] = $_POST[$wpvar];
elseif (isset($_GET[$wpvar]))
$query[$wpvar] = $_GET[$wpvar];
elseif (isset($query_vars[$wpvar]))
$query[$wpvar] = $query_vars[$wpvar];
if (!empty($query[$wpvar])) {
if (!is_array($query[$wpvar])) {
$query[$wpvar] = (string)$query[$wpvar];
} else {
foreach ($query[$wpvar] as $vkey => $v) {
if (!is_object($v)) {
$query[$wpvar][$vkey] = (string)$v;
}
}
}
if (isset($post_type_query_vars[$wpvar])) {
$query['post_type'] = $post_type_query_vars[$wpvar];
$query['name'] = $query[$wpvar];
}
}
}
// Do the query
$query = new WP_Query($query);
if (!empty($query -> posts) && $query -> is_singular)
return $query -> post -> ID;
else
return 0;
}
}
return 0;
}
if (!class_exists('GYSThemedCategories')) {
class GYSThemedCategories {
function GYSThemedCategories() {
$this -> BlogCharset = get_option('blog_charset');
$this -> OptionName = strtoupper(get_class($this));
$this -> Options = get_option($this -> OptionName);
$this -> Theme = '';
}
function GetOption() {
$options = func_get_args();
$option = $this -> Options;
//echo "Arguments: ";
//print_r($options);
//echo "<br />";
//echo "Option: ";
//print_r($option);
//echo "<br />";
foreach ($options AS $o) {
// echo "o: " . $o . "<br />";
if (isset($option[$o])) {
$option = $option[$o];
}
}
// echo "Option: " . $option . "<br />";
return $option;
}
function SetOptions() {
$options = func_get_args();
for ($i = 0; $i < count($options); $i += 2) {
// echo "Option i: " . $options[$i] . "<br />Option i+1: " . $options[$i+1] . "<br />";
$this -> Options[$options[$i]] = $options[$i + 1];
}
update_option($this -> OptionName, $this -> Options);
}
// CATEGORY FORM PROCESSING
function EditCategoryForm() {
$themes = wp_get_themes();
if (isset($_GET['tag_ID'])) {
$template = $this -> GetOption('CategoryThemes', $_GET['tag_ID']);
} else {
$template = '';
}
//echo "CurrentTemplate: " . $template . "<br />";
//print_r($themes);
//echo "Stylesheet: " . $theme['Stylesheet'] . "<br />";
$options = '<option value="">---</option>';
foreach ($themes AS $theme) {
//echo "Temp: ". $theme['Template'] . " | Stylesheet: ". $theme['Stylesheet'] . "<br />";
$selected = $theme['Stylesheet'] == $template ? ' selected="selected" ' : '';
$options .= '<option value="' . $theme['Stylesheet'] . '"' . $selected . '>' . __($theme['Name']) . ' ' . $theme['Version'] . '</option>';
}
$form = <<<STRING
<div id="GYSThemedCategories">
<h3>GYS Themed Categories</h3>
<table class="form-table">
<tbody>
<tr class="form-field">
<th valign="top" scope="row">Category Theme</th>
<td><select id='GYSThemedCategories' name='GYSThemedCategories'>{$options}</select></td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript">
//<![CDATA[
function GYSThemedCategories(){
try{
var x=document.getElementById('GYSThemedCategories');
var p=x.parentNode;
var t=p.getElementsByTagName('p')[0];
p.insertBefore(x,t);
}catch(e){}
}
GYSThemedCategories();
//]]>
</script>
STRING;
echo $form;
}
function SaveCategory($id) {
if (isset($_POST['GYSThemedCategories'])) {
$catthemes = $this -> GetOption('CategoryThemes');
if ($_POST['GYSThemedCategories']) {
$catthemes[$id] = $_POST['GYSThemedCategories'];
} else {
unset($catthemes[$id]);
}
$this -> SetOptions('CategoryThemes', $catthemes);
}
}
// TEMPLATE PROCESSING
function Template($template) {
$pid = $cid = 0;
$perms = get_option('permalink_structure');
if ($perms) {
// get current URL if permalinks are set
$s = empty($_SERVER['HTTPS']) ? '' : $_SERVER['HTTPS'] == 'on' ? 's' : '';
$protocol = 'http' . $s;
$port = $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT'];
$url = $protocol . '://' . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
list($url) = explode('?', $url);
// get Post ID from URL
$pid = url_to_postid($url);
// get Category ID from URL
list($url) = explode('/page/', $url);
// <- added for paging compatibility
//echo '<h1 style="color: red; text-transform: uppercase;">is not page</h1>';
$cid = get_category_by_path($url, false);
if (is_object($cid) AND isset($cid -> cat_ID)) { $cid = $cid -> cat_ID;
}
//echo '<strong>Debugging info: is-page ==>' . is_page($pid) . ' | pid==>' . $pid . ' | cid==>' . $cid . " | post type==>". get_post_type($pid) . '</strong>';
} else {
// no permalinks so we simply check GET vars
$pid = $_GET['p'] + 0;
$cid = $_GET['cat'] + 0;
}
create_initial_taxonomies();
if (isset($cid)) {
// we're in a category page...
$cat = $cid;
} elseif ($pid != 0 && get_post_type($pid) != "page") {
// we're in a post page... so let's get the first category of this post
list($cat) = wp_get_post_categories($pid);
} else {
// we are on a page, home page, or custom post type...don't apply new theme
unset($cat);
}
if (isset($cat)) {
// we have our category ID now so let's get the theme for it...
$theme = $this -> GetOption('CategoryThemes', $cat);
// change template if a theme is specified for this category
if ($theme)
$template = $theme;
} else {
// grab current theme's stylesheet
$template = get_option('stylesheet');
}
// set Theme to current template, then grab full stylesheet location
$this -> Theme = $template;
if (strtolower(substr(ABSPATH, 1, 1)) == ":") {
// we're apparently on a Windows box, so ABSPATH and WP_CONTENT_DIR are messed up as of WP 3.2
$tempstyleloc = substr(ABSPATH, 0, strlen(ABSPATH) - 1) . "\\wp-content\\themes\\" . $this -> Theme . "\\style.css";
} else {
// ah, linux/unix
$tempstyleloc = WP_CONTENT_DIR . "/themes/" . $this -> Theme . "/style.css";
}
// grab template from stylesheet location
$themedata = wp_get_theme($tempstyleloc);
if ($themedata["Template"]) {
return $themedata["Template"];
} else {
return $template;
}
}
function Stylesheet() {
return $this -> Theme;
}
}
}
function init_gys_themed_categories() {
if (class_exists('GYSThemedCategories') && !isset($GYSThemedCategories)) {
$GYSThemedCategories = new GYSThemedCategories(__FILE__);
if (is_admin()) {
add_action('category_edit_form_fields', array(&$GYSThemedCategories, 'EditCategoryForm'));
add_action('category_add_form_fields', array(&$GYSThemedCategories, 'EditCategoryForm'));
add_action('create_category', array(&$GYSThemedCategories, 'SaveCategory'));
add_action('edit_category', array(&$GYSThemedCategories, 'SaveCategory'));
}
if (!is_admin()) {
add_filter('template', array(&$GYSThemedCategories, 'Template'));
add_filter('stylesheet', array(&$GYSThemedCategories, 'Stylesheet'));
}
}
}
add_action('plugins_loaded', 'init_gys_themed_categories');
?>