1. <?php
  2. /*
  3.  Plugin Name: GYS Themed Categories
  4.  Plugin URI: http://rumleydesign.com/plugins/gys-themed-categories-2/
  5.  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.
  6.  Author: Luke Rumley / Mike Lopez
  7.  Version: 2.6
  8.  Author URI: http://rumleydesign.com/
  9.  */
  10.  
  11. function rd_url_to_postid($url) {
  12.     global $wp_rewrite;
  13.  
  14.     $url = apply_filters('url_to_postid', $url);
  15.  
  16.     // First, check to see if there is a 'p=N' or 'page_id=N' to match against
  17.     if (preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values)) {
  18.         $id = absint($values[2]);
  19.         if ($id)
  20.             return $id;
  21.     }
  22.  
  23.     // Check to see if we are using rewrite rules
  24.     $rewrite = $wp_rewrite -> wp_rewrite_rules();
  25.  
  26.     // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
  27.     if (empty($rewrite))
  28.         return 0;
  29.  
  30.     // Get rid of the #anchor
  31.     $url_split = explode('#', $url);
  32.     $url = $url_split[0];
  33.  
  34.     // Get rid of URL ?query=string
  35.     $url_split = explode('?', $url);
  36.     $url = $url_split[0];
  37.  
  38.     // Add 'www.' if it is absent and should be there
  39.     if (false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.'))
  40.         $url = str_replace('://', '://www.', $url);
  41.  
  42.     // Strip 'www.' if it is present and shouldn't be
  43.     if (false === strpos(home_url(), '://www.'))
  44.         $url = str_replace('://www.', '://', $url);
  45.  
  46.     // Strip 'index.php/' if we're not using path info permalinks
  47.     if (!$wp_rewrite -> using_index_permalinks())
  48.         $url = str_replace('index.php/', '', $url);
  49.  
  50.     if (false !== strpos($url, home_url())) {
  51.         // Chop off http://domain.com
  52.         $url = str_replace(home_url(), '', $url);
  53.     } else {
  54.         // Chop off /path/to/blog
  55.         $home_path = parse_url(home_url());
  56.         $home_path = isset($home_path['path']) ? $home_path['path'] : '';
  57.         $url = str_replace($home_path, '', $url);
  58.     }
  59.  
  60.     // Trim leading and lagging slashes
  61.     $url = trim($url, '/');
  62.  
  63.     $request = $url;
  64.     // Look for matches.
  65.     $request_match = $request;
  66.     foreach ((array)$rewrite as $match => $query) {
  67.         // If the requesting file is the anchor of the match, prepend it
  68.         // to the path info.
  69.         if (!empty($url) && ($url != $request) && (strpos($match, $url) === 0))
  70.             $request_match = $url . '/' . $request;
  71.  
  72.         if (preg_match("!^$match!", $request_match, $matches)) {
  73.             // Got a match.
  74.             // Trim the query of everything up to the '?'.
  75.             $query = preg_replace("!^.+\?!", '', $query);
  76.  
  77.             // Substitute the substring matches into the query.
  78.             $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
  79.  
  80.             // Filter out non-public query vars
  81.             global $wp;
  82.             parse_str($query, $query_vars);
  83.             $query = array();
  84.             foreach ((array) $query_vars as $key => $value) {
  85.                 if (in_array($key, $wp -> public_query_vars))
  86.                     $query[$key] = $value;
  87.             }
  88.  
  89.             // Taken from class-wp.php
  90.             foreach ($GLOBALS['wp_post_types'] as $post_type => $t)
  91.                 if ($t -> query_var)
  92.                     $post_type_query_vars[$t -> query_var] = $post_type;
  93.  
  94.             foreach ($wp->public_query_vars as $wpvar) {
  95.                 if (isset($wp -> extra_query_vars[$wpvar]))
  96.                     $query[$wpvar] = $wp -> extra_query_vars[$wpvar];
  97.                 elseif (isset($_POST[$wpvar]))
  98.                     $query[$wpvar] = $_POST[$wpvar];
  99.                 elseif (isset($_GET[$wpvar]))
  100.                     $query[$wpvar] = $_GET[$wpvar];
  101.                 elseif (isset($query_vars[$wpvar]))
  102.                     $query[$wpvar] = $query_vars[$wpvar];
  103.  
  104.                 if (!empty($query[$wpvar])) {
  105.                     if (!is_array($query[$wpvar])) {
  106.                         $query[$wpvar] = (string)$query[$wpvar];
  107.                     } else {
  108.                         foreach ($query[$wpvar] as $vkey => $v) {
  109.                             if (!is_object($v)) {
  110.                                 $query[$wpvar][$vkey] = (string)$v;
  111.                             }
  112.                         }
  113.                     }
  114.  
  115.                     if (isset($post_type_query_vars[$wpvar])) {
  116.                         $query['post_type'] = $post_type_query_vars[$wpvar];
  117.                         $query['name'] = $query[$wpvar];
  118.                     }
  119.                 }
  120.             }
  121.  
  122.             // Do the query
  123.             $query = new WP_Query($query);
  124.             if (!empty($query -> posts) && $query -> is_singular)
  125.                 return $query -> post -> ID;
  126.             else
  127.                 return 0;
  128.         }
  129.     }
  130.     return 0;
  131. }
  132.  
  133. if (!class_exists('GYSThemedCategories')) {
  134.     class GYSThemedCategories {
  135.         function GYSThemedCategories() {
  136.             $this -> BlogCharset = get_option('blog_charset');
  137.             $this -> OptionName = strtoupper(get_class($this));
  138.             $this -> Options = get_option($this -> OptionName);
  139.             $this -> Theme = '';
  140.         }
  141.  
  142.         function GetOption() {
  143.             $options = func_get_args();
  144.             $option = $this -> Options;
  145.             //echo "Arguments: ";
  146.             //print_r($options);
  147.             //echo "<br />";
  148.             //echo "Option: ";
  149.             //print_r($option);
  150.             //echo "<br />";
  151.             foreach ($options AS $o) {
  152.                 // echo "o: " . $o . "<br />";
  153.                 if (isset($option[$o])) {
  154.                     $option = $option[$o];
  155.                 }
  156.             }
  157.             // echo "Option: " . $option . "<br />";
  158.             return $option;
  159.         }
  160.  
  161.         function SetOptions() {
  162.             $options = func_get_args();
  163.             for ($i = 0; $i < count($options); $i += 2) {
  164.                 // echo "Option i: " . $options[$i] . "<br />Option i+1: " . $options[$i+1] . "<br />";
  165.                 $this -> Options[$options[$i]] = $options[$i + 1];
  166.             }
  167.             update_option($this -> OptionName, $this -> Options);
  168.         }
  169.  
  170.         // CATEGORY FORM PROCESSING
  171.         function EditCategoryForm() {
  172.             $themes = wp_get_themes();
  173.             if (isset($_GET['tag_ID'])) {
  174.                 $template = $this -> GetOption('CategoryThemes', $_GET['tag_ID']);
  175.             } else {
  176.                 $template = '';
  177.             }
  178.             //echo "CurrentTemplate: " . $template . "<br />";
  179.             //print_r($themes);
  180.             //echo "Stylesheet: " . $theme['Stylesheet'] . "<br />";
  181.             $options = '<option value="">---</option>';
  182.             foreach ($themes AS $theme) {
  183.                 //echo "Temp: ". $theme['Template'] . " | Stylesheet: ". $theme['Stylesheet'] . "<br />";
  184.                 $selected = $theme['Stylesheet'] == $template ? ' selected="selected" ' : '';
  185.                 $options .= '<option value="' . $theme['Stylesheet'] . '"' . $selected . '>' . __($theme['Name']) . ' ' . $theme['Version'] . '</option>';
  186.             }
  187.             $form = <<<STRING
  188.                 <div id="GYSThemedCategories">
  189.                     <h3>GYS Themed Categories</h3>
  190.                     <table class="form-table">
  191.                     <tbody>
  192.                     <tr class="form-field">
  193.                         <th valign="top" scope="row">Category Theme</th>
  194.                         <td><select id='GYSThemedCategories' name='GYSThemedCategories'>{$options}</select></td>
  195.                     </tr>
  196.                     </tbody>
  197.                     </table>
  198.                 </div>
  199.                 <script type="text/javascript">
  200.                     //<![CDATA[
  201.                     function GYSThemedCategories(){
  202.                         try{
  203.                             var x=document.getElementById('GYSThemedCategories');
  204.                             var p=x.parentNode;
  205.                             var t=p.getElementsByTagName('p')[0];
  206.                             p.insertBefore(x,t);
  207.                         }catch(e){}
  208.                     }
  209.                     GYSThemedCategories();
  210.                     //]]>
  211.                 </script>
  212. STRING;
  213.             echo $form;
  214.         }
  215.  
  216.         function SaveCategory($id) {
  217.             if (isset($_POST['GYSThemedCategories'])) {
  218.                 $catthemes = $this -> GetOption('CategoryThemes');
  219.                 if ($_POST['GYSThemedCategories']) {
  220.                     $catthemes[$id] = $_POST['GYSThemedCategories'];
  221.                 } else {
  222.                     unset($catthemes[$id]);
  223.                 }
  224.                 $this -> SetOptions('CategoryThemes', $catthemes);
  225.             }
  226.         }
  227.  
  228.         // TEMPLATE PROCESSING
  229.         function Template($template) {
  230.             $pid = $cid = 0;
  231.             $perms = get_option('permalink_structure');
  232.             if ($perms) {
  233.                 // get current URL if permalinks are set
  234.                 $s = empty($_SERVER['HTTPS']) ? '' : $_SERVER['HTTPS'] == 'on' ? 's' : '';
  235.                 $protocol = 'http' . $s;
  236.                 $port = $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT'];
  237.                 $url = $protocol . '://' . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
  238.                 list($url) = explode('?', $url);
  239.                 // get Post ID from URL
  240.                 $pid = url_to_postid($url);
  241.                 // get Category ID from URL
  242.                 list($url) = explode('/page/', $url);
  243.                 // <- added for paging compatibility
  244.                 //echo '<h1 style="color: red; text-transform: uppercase;">is not page</h1>';
  245.                 $cid = get_category_by_path($url, false);
  246.                 if (is_object($cid) AND isset($cid -> cat_ID)) { $cid = $cid -> cat_ID;
  247.                 }
  248.                 //echo '<strong>Debugging info: is-page ==>' . is_page($pid) . ' | pid==>' . $pid . ' | cid==>' . $cid . " | post type==>". get_post_type($pid) . '</strong>';
  249.             } else {
  250.                 // no permalinks so we simply check GET vars
  251.                 $pid = $_GET['p'] + 0;
  252.                 $cid = $_GET['cat'] + 0;
  253.             }
  254.  
  255.             create_initial_taxonomies();
  256.             if (isset($cid)) {
  257.                 // we're in a category page...
  258.                 $cat = $cid;
  259.             } elseif ($pid != 0 && get_post_type($pid) != "page") {
  260.                 // we're in a post page... so let's get the first category of this post
  261.                 list($cat) = wp_get_post_categories($pid);
  262.             } else {
  263.                 // we are on a page, home page, or custom post type...don't apply new theme
  264.                 unset($cat);
  265.             }
  266.  
  267.             if (isset($cat)) {
  268.                 // we have our category ID now so let's get the theme for it...
  269.                 $theme = $this -> GetOption('CategoryThemes', $cat);
  270.                 // change template if a theme is specified for this category
  271.                 if ($theme)
  272.                     $template = $theme;
  273.             } else {
  274.                 // grab current theme's stylesheet
  275.                 $template = get_option('stylesheet');
  276.             }
  277.  
  278.             // set Theme to current template, then grab full stylesheet location
  279.             $this -> Theme = $template;
  280.             if (strtolower(substr(ABSPATH, 1, 1)) == ":") {
  281.                 // we're apparently on a Windows box, so ABSPATH and WP_CONTENT_DIR are messed up as of WP 3.2
  282.                 $tempstyleloc = substr(ABSPATH, 0, strlen(ABSPATH) - 1) . "\\wp-content\\themes\\" . $this -> Theme . "\\style.css";
  283.             } else {
  284.                 // ah, linux/unix
  285.                 $tempstyleloc = WP_CONTENT_DIR . "/themes/" . $this -> Theme . "/style.css";
  286.             }
  287.             // grab template from stylesheet location
  288.             $themedata = wp_get_theme($tempstyleloc);
  289.             if ($themedata["Template"]) {
  290.                 return $themedata["Template"];
  291.             } else {
  292.                 return $template;
  293.             }
  294.         }
  295.  
  296.         function Stylesheet() {
  297.             return $this -> Theme;
  298.         }
  299.  
  300.     }
  301.  
  302. }
  303.  
  304. function init_gys_themed_categories() {
  305.     if (class_exists('GYSThemedCategories') && !isset($GYSThemedCategories)) {
  306.         $GYSThemedCategories = new GYSThemedCategories(__FILE__);
  307.         if (is_admin()) {
  308.             add_action('category_edit_form_fields', array(&$GYSThemedCategories, 'EditCategoryForm'));
  309.             add_action('category_add_form_fields', array(&$GYSThemedCategories, 'EditCategoryForm'));
  310.             add_action('create_category', array(&$GYSThemedCategories, 'SaveCategory'));
  311.             add_action('edit_category', array(&$GYSThemedCategories, 'SaveCategory'));
  312.         }
  313.         if (!is_admin()) {
  314.             add_filter('template', array(&$GYSThemedCategories, 'Template'));
  315.             add_filter('stylesheet', array(&$GYSThemedCategories, 'Stylesheet'));
  316.         }
  317.     }
  318. }
  319.  
  320. add_action('plugins_loaded', 'init_gys_themed_categories');
  321. ?>