Advertisement
MoonWatch

Page Theme plugin fix

Apr 18th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 26.61 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Page Theme
  4. Plugin URI: http://wordpress.org/extend/plugins/page-theme/
  5. Description: Per-page, per-post theme selection. Works with both SEO and non-SEO permalinks.
  6. Version: 3.5
  7. Author: Chris Ravenscroft
  8. Author URI: http://nexus.zteo.com
  9. */
  10.  
  11. /* GPL License */
  12.  
  13. if (!function_exists('plugin_dir_url')) {
  14.     function plugin_dir_url($file) {
  15.         if (!function_exists('plugins_url')) {
  16.             return trailingslashit(get_option('siteurl') . '/wp-content/plugins/' . plugin_basename($file));
  17.         }
  18.         return trailingslashit(plugins_url(plugin_basename(dirname($file))));
  19.     }
  20. }
  21.  
  22. if(!empty($_GET['pcompaction'])) {
  23.     new PCompAction($_GET['pcompaction']);
  24.     exit;
  25. }
  26.  
  27. $ruri = $_SERVER['REQUEST_URI'];
  28. $_root = 'http';
  29. if ( isset( $_SERVER["HTTPS"] ) && $_SERVER["HTTPS"] == "on" ) {
  30.     $_root .= "s";
  31. }
  32. $_root .= "://";
  33. if ( $_SERVER["SERVER_PORT"] != "80" && ! ( isset( $_SERVER["HTTPS"] ) && $_SERVER["HTTPS"] == "on" && $_SERVER["SERVER_PORT"] == "443" ) ) {
  34.     $_root .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"];
  35. } else {
  36.     $_root .= $_SERVER["SERVER_NAME"];
  37. }
  38. $home = str_ireplace( $_root, '', home_url() );
  39. $ruri = str_ireplace( $home, '', $ruri );
  40.  
  41. if(strlen($ruri) >= 2) {
  42.     $pageId = false;
  43.     $first = explode( '?', $ruri );
  44.     $second = isset( $first[1] ) ? $first[1] : '';
  45.     $first = $first[0];
  46.     $p = stripos( $ruri, '?' );
  47.     if(!empty($second)) {
  48.         $fragments = explode('&', substr($ruri, ($p + 1)));
  49.         foreach($fragments as $fragment) {
  50.             $bits = explode('=', $fragment);
  51.             if($bits[0] == 'p' || $bits[0] == 'page') {
  52.                 $pageId = intval($bits[1]);
  53.                 break;
  54.             }
  55.         }
  56.     }
  57.     if(false !== $pageId) {
  58.         $pageTheme = new PageTheme($pageId);
  59.     }
  60.     else {
  61.         $c = strlen($first) - 2;
  62.         while($c > 0 && $first{$c} != '/') $c--;
  63.         $pageTheme = new PageTheme(strtolower(str_replace('/', '', substr($first, $c))));
  64.     }
  65. }
  66. else if($ruri == '/') {
  67.     $pageTheme = new PageTheme('');
  68. }
  69.  
  70.  
  71. class PageThemeOptions {
  72.  
  73.     protected static $instance = false;
  74.  
  75.     public static function getInstance() {
  76.         if(!self::$instance) {
  77.             self::$instance = new self();
  78.         }
  79.         return self::$instance;
  80.     }
  81.  
  82.     function getOptions() {
  83.         // Load page option settings
  84.         // Some versions of Wordpress deserialize automatically. WP3.0 doesn't.
  85.         try {
  86.             $options = unserialize(get_option("pageTheme_options"));
  87.         }
  88.         catch(Exception $e) {
  89.             $options = get_option("pageTheme_options");
  90.         }
  91.        
  92.         if (gettype($options)!="array"){
  93.             $options = array();
  94.         }
  95.  
  96.         return $options;
  97.     }
  98.  
  99.     function setOptions($options) {
  100.         update_option("pageTheme_options", serialize($options));
  101.     }
  102.  
  103.     function getDispOptions() {
  104.         try {
  105.             $options = unserialize(get_option("pageTheme_disp_options"));
  106.         }
  107.         catch(Exception $e) {
  108.             $options = get_option("pageTheme_disp_options");
  109.         }
  110.        
  111.         if (gettype($options)!="array"){
  112.             $options = array();
  113.         }
  114.  
  115.         return $options;
  116.     }
  117.  
  118.     function setDispOptions($options) {
  119.         update_option("pageTheme_disp_options", serialize($options));
  120.     }
  121. }
  122.  
  123. class PageThemeAdminManager {
  124.  
  125.     protected $uri;
  126.  
  127.     function __construct() {
  128.         $dispOptions = PageThemeOptions::getInstance()->getDispOptions();
  129.  
  130.         add_action('admin_head', array(&$this,'displayAdminHead'));
  131.         add_action('admin_menu', array(&$this, 'displayAdminMenu'));
  132.  
  133.         $dispOptions['disppages'] = isset( $dispOptions['disppages'] ) ? $dispOptions['disppages'] : false;
  134.  
  135.         if($dispOptions['disppages']) {
  136.             add_action('manage_pages_columns', array(&$this, 'managePagesColumns'));
  137.             add_action('manage_pages_custom_column', array(&$this, 'managePagesCustomColumn'), 10, 2);
  138.         }
  139.  
  140.         $dispOptions['dispposts'] = isset( $dispOptions['dispposts'] ) ? $dispOptions['dispposts'] : false;
  141.         if($dispOptions['dispposts']) {
  142.             add_action('manage_posts_columns', array(&$this, 'managePostsColumns'));
  143.             add_action('manage_posts_custom_column', array(&$this, 'managePostsCustomColumn'), 10, 2);
  144.         }
  145.    
  146.         // Specify uri for admin panels
  147.         $this->uri = '?page=' . $this->getRightMost(__FILE__, 'plugins/');
  148.     }
  149.  
  150.     // Common string function
  151.     private function getRightMost($sSrc, $sSrch) {
  152.         for ($i = strlen($sSrc); $i >= 0; $i = $i - 1) {
  153.             $f = strpos($sSrc, $sSrch, $i);
  154.             if ($f !== FALSE) {
  155.                return substr($sSrc,$f + strlen($sSrch), strlen($sSrc));
  156.             }
  157.         }
  158.         return $sSrc;
  159.     }
  160.  
  161.     function displayAdminHead() {
  162.         wp_enqueue_script('jquery');
  163.         $remoteLoader = get_option('siteurl').'/wp-admin/edit.php?post_type=page&pcompaction=pcomphtml';
  164.         $remoteSave = get_option('siteurl').'/wp-admin/edit.php?post_type=page&pcompaction=pcompsave';
  165.         $loadingHTML = '<img src="'.plugin_dir_url(__FILE__)."loading.gif\" />";
  166.         ?>
  167.         <link rel="stylesheet" href="<?php echo plugin_dir_url(__FILE__) ?>emposha/fcbklistselection.css" type="text/css" media="screen" />
  168.         <script type="text/javascript" src="<?php echo plugin_dir_url(__FILE__) ?>emposha/fcbklistselection.js"></script>
  169.         <script type="text/javascript" language="JavaScript">
  170.         var CCHandler = {
  171.             current_theme_screen_comp: null,
  172.             show_theme_screen: function(comp) {
  173.                 if(this.current_theme_screen_comp == comp) {
  174.                     this.current_theme_screen_comp = null;
  175.                     return this;
  176.                 }
  177.                 this.current_theme_screen_comp = comp;
  178.                 if(jQuery(comp).parent().parent().hasClass('alternate')) {
  179.                     var class_str = ' class="alternate"';
  180.                 }
  181.                 else {
  182.                     var class_str = '';
  183.                 }
  184.                 jQuery(comp).parent().parent().after(function(index) {
  185.                     return "<tr" + class_str + " id='pcompcurrent'><td colspan='"+columns_count+"'><div id='pcompcontainer'></div></td></tr>"
  186.                 });
  187.                 var post_id = this.current_theme_screen_comp.id.replace(/ptheme/, '');
  188.                 jQuery('#pcompcontainer').html('<?php echo $loadingHTML; ?>').load(
  189.                     '<?php echo $remoteLoader; ?>&post_id=' + post_id, function() {
  190.                     CCHandler.prepare_for_selection();
  191.                 });
  192.                 return this;
  193.             },
  194.             hide_theme_screen: function() {
  195.                 if(this.current_theme_screen_comp != null) {
  196.                     jQuery('#pcompcurrent').remove();
  197.                 }
  198.                 return this;
  199.             },
  200.             prepare_for_selection: function() {
  201.                 jQuery.fcbkListSelection("#fcbklist","300","250","2", function(selected) {
  202.                     var themeSelect = jQuery('#theme')[0];
  203.                     for(var i=0; i<themeSelect.options.length; i++) {
  204.                         if(themeSelect.options[i].value == selected) {
  205.                             themeSelect.options[i].selected = true;
  206.                             break;
  207.                         }
  208.                     }
  209.                 });
  210.                 updateGalleryUsingSelect = function() {
  211.                     var selected = jQuery('#theme option:selected')[0].value;
  212.                     jQuery('#fcbklist').each(function() {
  213.                         jQuery(this).children().each(function() {
  214.                             var myid = jQuery(this)[0].id;
  215.                             if(myid == selected) {
  216.                                 var obj = jQuery(jQuery(jQuery(this)[0]).children()[0]);
  217.                                 obj.click();
  218.                             }
  219.                         });
  220.                     });
  221.                 }
  222.                 jQuery('#theme').change(function() {
  223.                     updateGalleryUsingSelect();
  224.                 });
  225.                 updateGalleryUsingSelect();
  226.                 jQuery('#pthemesubmit').click(function() {
  227.                     var selected = jQuery('#theme option:selected')[0].value;
  228.                     var post_id = CCHandler.current_theme_screen_comp.id.replace(/ptheme/, '');
  229.                     jQuery.get(
  230.                         '<?php echo $remoteSave; ?>',
  231.                         {post_id:post_id, theme:selected},
  232.                         function(responseText) {
  233.                             var res = responseText.split("\n");
  234.                             if(res.length != 3) {
  235.                                 alert(responseText);
  236.                             }
  237.                             else {
  238.                                 jQuery(CCHandler.current_theme_screen_comp).html(
  239.                                     '<img class="pthemepreview" style="border:1px solid lightgray;" align="left" width="32" height="32" src="' + res[2] + "\" />&nbsp;" + res[1] + "\n");
  240.                                 CCHandler.hide_theme_screen().show_theme_screen(CCHandler.current_theme_screen_comp);
  241.                             }
  242.                         },
  243.                         'html');
  244.                     return false;
  245.                 });
  246.             }
  247.         };
  248.         jQuery(document).ready(function() {
  249.             jQuery.ajaxSetup({cache: false}); // TODO Really?
  250.             jQuery('.pthemebutton').click(function() {
  251.                 CCHandler.hide_theme_screen().show_theme_screen(this);
  252.             }).hover(
  253.                 function() {
  254.                     this.style.cursor = 'pointer';
  255.                     var thisCopy = this;
  256.                     hover_intent = setTimeout(
  257.                         function() {
  258.                             jQuery(thisCopy).find('.pthemepreview').width(300).height(250);
  259.                         }, 1000);
  260.                 },
  261.                 function() {
  262.                     try{clearTimeout(hover_intent);} catch(e){}
  263.                     jQuery(this).find('.pthemepreview').width(32).height(32);
  264.                 });
  265.         });
  266.  
  267.         </script>
  268.         <?php
  269.     }
  270.  
  271.     function displayAdminMenu(){
  272.         // Show up under "Appearance"
  273.         add_theme_page('Page Theme Options', 'Page Theme', 'administrator', __FILE__, array(&$this, 'createAdminPanel'));
  274.     }
  275.  
  276.     // Create the administration panel
  277.     function createAdminPanel(){
  278.         if ( isset( $_GET['action'] ) && $_GET['action'] == "saveSettings" ){
  279.             $dispOptions = array(
  280.                 'disppages' => (!empty($_POST['pthemedisppages']) ? $_POST['pthemedisppages'] : 0),
  281.                 'dispposts' => (!empty($_POST['pthemedispposts']) ? $_POST['pthemedispposts'] : 0),
  282.             );
  283.             PageThemeOptions::getInstance()->setDispOptions($dispOptions);
  284.         }
  285.         else {
  286.             $dispOptions = PageThemeOptions::getInstance()->getDispOptions();
  287.         }
  288.         $dispOptions['disppages'] = isset( $dispOptions['disppages'] ) ? $dispOptions['disppages'] : false;
  289.         $dispOptions['dispposts'] = isset( $dispOptions['dispposts'] ) ? $dispOptions['dispposts'] : false;
  290.  
  291.         echo '
  292.            <div class="wrap">
  293.                <div id="icon-themes" class="icon32"><br /></div>
  294.                <h2>Page-Theme Settings</h2>
  295.                <form name="themeSettings" id="themeSettings" action="'.$this->uri.'&action=saveSettings" method="post">
  296.                    <table class="form-table">
  297.                        <tr>
  298.                            <th scope="row"><strong>Admin Panel</strong></th>
  299.                            <td>
  300.                                <p><label>
  301.                                    <input type="checkbox" name="pthemedisppages" value="1" ' . ($dispOptions['disppages'] ? 'checked' : '') . '/>
  302.                                    Add to "Pages" admin screen
  303.                                    <br /><em>When this option is selected, the "Pages > Pages" admin screen will display an additional column, letting you edit each page\'s theme.</em>
  304.                                </label></p>
  305.                                <p><label>
  306.                                    <input type="checkbox" name="pthemedispposts" value="1" ' . ($dispOptions['dispposts'] ? 'checked' : '') . ' />
  307.                                    Add to "Posts" admin screen
  308.                                    <br /><em>When this option is selected, the "Posts > Posts" admin screen will display an additional column, letting you edit each post\'s theme.</em>
  309.                                </label></p>
  310.                            </td>
  311.                        </tr>
  312.                    </table>
  313.                    <p class="submit"><input type="submit" class="button-primary" name="submit" value="Save Settings" /></p>
  314.                </form>
  315.            </div>
  316.  
  317. <div id="dashboard-widgets-wrap">
  318.    <div id="dashboard-widgets" class="metabox-holder">
  319.        <div class="postbox-container">
  320.            <div id="normal-sortables" class="meta-box-sortables">
  321.                <div id="dashboard_right_now" class="postbox ">
  322.                    <div class="handlediv" title="Click to toggle"><br /></div>
  323.                    <h3 class="hndle"><span>About</span></h3>
  324.                    <div class="inside" style="padding:8px">
  325.                        <p>
  326.                            This plugin lets you select specific themes for selected pages and blog posts. Despite its name, it works with both pages and posts. It also supports both SEO and non-SEO permalinks.
  327.                        </p>
  328.                        <p>
  329.                            After enabling the extra columns for pages and/or posts using this screen, you will be able to select these themes on their respective screens by clicking on each page/post\'s current theme.
  330.                        </p>
  331.                        <p>
  332.                            <strong>Note</strong>: by default, all posts and pages are displayed using the "Default Theme."
  333.                        </p>
  334.                        <hr />
  335.                        <p>
  336.                            <em>This plugin was created by Chris F. Ravenscroft.</em> Need assistance? Visit my blog at <a href="http://nexus.zteo.com" target="_blank">http://nexus.zteo.com</a> or follow me on <a href="http://twitter.com/chrisfr" target="_blank">Twitter</a>.
  337.                        </p>
  338.                    </div>
  339.                </div>
  340.            </div>
  341.        </div>
  342.    </div>
  343. </div>
  344.  
  345.        ';
  346.     }
  347.  
  348.     function managePagesColumns($defaults) {
  349.         return CustomColumnManager::getInstance(CustomColumnManager::PAGE)->enumColumns($defaults);
  350.     }
  351.    
  352.     function managePostsColumns($defaults) {
  353.         return CustomColumnManager::getInstance(CustomColumnManager::POST)->enumColumns($defaults);
  354.     }
  355.  
  356.     function managePagesCustomColumn($column_name, $page_id) {
  357.         if($column_name != 'ptheme') {
  358.             return;
  359.         }
  360.         CustomColumnManager::getInstance(CustomColumnManager::PAGE)->handleRow($column_name, $page_id);
  361.     }
  362.  
  363.     function managePostsCustomColumn($column_name, $post_id) {
  364.         if($column_name != 'ptheme') {
  365.             return;
  366.         }
  367.         CustomColumnManager::getInstance(CustomColumnManager::POST)->handleRow($column_name, $post_id);
  368.     }
  369. }
  370.  
  371. // Four possibilities:
  372. // - a SEO page, ...../name
  373. // - a non-SEO page, ..../?page_id=n
  374. // - a SEO post, ..../name
  375. // - a non-SEO post, ..../?p=n
  376.  
  377. class PageThemeThemeSwitcher {
  378.  
  379.     private $stylesheet;
  380.     private $template;
  381.  
  382.     function __construct($page) {
  383.         // Get default settings
  384.         $this->stylesheet = get_option("stylesheet");
  385.         $this->template = get_option("template");
  386.  
  387.         $options = PageThemeOptions::getInstance()->getOptions();
  388.        
  389.         $pAliased = false;
  390.         if($page == '') {
  391.             if(get_option("show_on_front") == 'page') {
  392.                 $page = intval(get_option("page_on_front"));
  393.                 if($page > 0) {
  394.                     $pAliased = true;
  395.                 }
  396.             }
  397.             if(!$pAliased) {
  398.                 return;
  399.             }
  400.         }
  401.  
  402.         $page_id = false;
  403.         if($pAliased) {
  404.             $page_id = $page;
  405.         }
  406.         else if(strncmp($page, '?p=', 3) == 0) {
  407.             $page_id = substr($page, 3);
  408.         }
  409.         else if(strncmp($page, '?page_id=', 9) == 0) {
  410.             $page_id = substr($page, 9);
  411.         }
  412.  
  413.         if($page_id) {
  414.             foreach($options as $dt){
  415.                 if ($page_id==$dt['id']){
  416.                     // Update the settings for the matching page
  417.                     $this->stylesheet = $dt['theme'];
  418.                     $this->template = $dt['theme'];
  419.                 }  
  420.             }
  421.         }
  422.         else {
  423.             // Locate the matching index for the current page
  424.             foreach($options as $dt){
  425.                 if ($page==$dt['url']){
  426.                     // Update the settings for the matching page
  427.                     $this->stylesheet = $dt['theme'];
  428.                     $this->template = $dt['theme'];
  429.                 }  
  430.             }
  431.         }
  432.  
  433.         add_filter('pre_option_stylesheet', array(&$this, 'getStylesheet'));
  434.         add_filter('pre_option_template', array(&$this, 'getTemplate'));
  435.     }
  436.  
  437.     function getStylesheet(){
  438.         return $this->stylesheet;
  439.     }
  440.  
  441.     function getTemplate(){
  442.         return $this->template;
  443.     }
  444. }
  445.  
  446. class CustomPageColumnHandler {
  447.  
  448.     protected $options;
  449.  
  450.     function __construct() {
  451.         $this->options = PageThemeOptions::getInstance()->getOptions();
  452.     }
  453.  
  454.     function getOptions($id) {
  455.         // Locate the matching index for the current page
  456.         foreach($this->options as $dt){
  457.             if ($dt['id'] == $id){
  458.                 // Update the settings for the matching page
  459.                 return(
  460.                     array(
  461.                         'stylesheet' => $dt['theme'],
  462.                         'template' => $dt['theme']
  463.                     )
  464.                 );
  465.             }  
  466.         }
  467.         return false;
  468.     }
  469. }
  470.  
  471. class CustomPostColumnHandler {
  472.  
  473.     protected $options;
  474.  
  475.     function __construct() {
  476.         $this->options = PageThemeOptions::getInstance()->getOptions();
  477.     }
  478.  
  479.     function getOptions($id) {
  480.         // Locate the matching index for the current page
  481.         foreach($this->options as $dt){
  482.             if ($dt['id'] == $id){
  483.                 // Update the settings for the matching page
  484.                 return(
  485.                     array(
  486.                         'stylesheet' => $dt['theme'],
  487.                         'template' => $dt['theme']
  488.                     )
  489.                 );
  490.             }  
  491.         }
  492.         return false;
  493.     }
  494. }
  495.  
  496. class CustomColumnManager {
  497.  
  498.     const PAGE = 'page';
  499.     const POST = 'post';
  500.  
  501.     protected static $instances = false;
  502.     protected static $themes = false;
  503.     protected $customColumnHandler = false;
  504.  
  505.     public static function getInstance($handlerType) {
  506.         if(!self::$instances) {
  507.             self::$instances = array();
  508.             self::$themes = function_exists( 'wp_get_themes' ) ? wp_get_themes() : get_themes();
  509.         }
  510.         if(!self::$instances[$handlerType]) {
  511.             switch($handlerType) {
  512.                 case self::PAGE:
  513.                     self::$instances[$handlerType] = new self(new CustomPageColumnHandler());
  514.                     break;
  515.                 case self::POST:
  516.                     self::$instances[$handlerType] = new self(new CustomPostColumnHandler());
  517.                     break;
  518.                 default:
  519.                     throw new Exception('Unknown handler type in CustomColumnManager');
  520.             }
  521.         }
  522.         return self::$instances[$handlerType];
  523.     }
  524.  
  525.     function __construct($customColumnHandler) {
  526.         $this->customColumnHandler = $customColumnHandler;
  527.     }
  528.  
  529.     function enumColumns($defaults) {
  530.         $newDefaults = array();
  531.         foreach($defaults as $key => $value) {
  532.             if($key == 'author') {
  533.                 $newDefaults['ptheme'] = __('Theme');
  534.             }
  535.             $newDefaults[$key] = $value;
  536.         }
  537.         static $iKnowColumnsCount = false;
  538.         if(!$iKnowColumnsCount) {
  539.             $iKnowColumnsCount = true;
  540.             $columnsCount = count($newDefaults);
  541.             ?>
  542.             <script type="text/javascript" language="JavaScript">
  543.                 var columns_count = <?php echo $columnsCount; ?>;
  544.             </script>
  545.             <?php
  546.         }
  547.         return $newDefaults;
  548.     }
  549.  
  550.     function handleRow($columnName, $pageId) {
  551.         echo "<div id='ptheme".$pageId."' class='pthemebutton'>\n";
  552.         $foundTheme = false;
  553.         $options = $this->customColumnHandler->getOptions($pageId);
  554.         if($options) {
  555.             foreach(self::$themes as $theme){
  556.                 if($options['template'] == $theme['Template']) {
  557.                     $foundTheme = true;
  558.                     echo '<img class="pthemepreview" style="border:1px solid lightgray;" align="left" width="32" height="32" src="'.$theme["Theme Root URI"].'/'.$theme["Stylesheet"].'/'.$theme["Screenshot"]."\" />&nbsp;" . $theme['Name'] . "\n";
  559.                 }
  560.             }
  561.         }
  562.         if(!$foundTheme) {
  563.             echo '<img style="border:1px solid lightgray;" align="left" width="32" height="32" src="'.plugin_dir_url(__FILE__)."white.png\" />&nbsp;<em>Default Theme</em>\n";
  564.         }
  565.         echo "</div>\n";
  566.     }
  567. }
  568.  
  569. class PageTheme {  
  570.  
  571.     function __construct($page){
  572.         new PageThemeThemeSwitcher($page);
  573.         new PageThemeAdminManager();
  574.     }
  575. }      
  576.  
  577. class PCompAction {
  578.  
  579.     function __construct($action){
  580.         switch($action) {
  581.             case 'pcomphtml':
  582.                 $this->pcomphtml();
  583.                 break;
  584.             case 'pcompsave':
  585.                 $this->pcompsave();
  586.                 break;
  587.         }
  588.     }
  589.  
  590.     function getPageThemeOptions() {
  591.         return PageThemeOptions::getInstance()->getOptions();
  592.     }
  593.  
  594.     function getPostByID($id) {
  595.         global $wpdb;
  596.         $pagepost = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID = $id");
  597.         if($pagepost && count($pagepost) == 1) {
  598.             return $pagepost[0];
  599.         }
  600.         else {
  601.             return false;
  602.         }
  603.     }
  604.  
  605.     function getThemeInfoByTemplate($template) {
  606.         $themes = function_exists( 'wp_get_themes' ) ? wp_get_themes() : get_themes();
  607.         foreach($themes as $theme){
  608.             if($theme['Template'] == $template) {
  609.                 return $theme;
  610.             }
  611.         }
  612.         return false;
  613.     }
  614.  
  615.     function pcomphtml() {
  616.         if(empty($_GET['post_id'])) {
  617.             die("Missing parameters");
  618.         }
  619.         $post_id = intval($_GET['post_id']);
  620.         if(!$post_id) {
  621.             die("Invalid post id");
  622.         }
  623.         $currentTheme = false;
  624.         $options = $this->getPageThemeOptions();
  625.         foreach($options as $option) {
  626.             if($option['id'] == $post_id) {
  627.                 $currentTheme = $option['theme'];
  628.                 break;
  629.             }
  630.         }
  631.         echo '<div class="wrap">
  632.            <h2>Custom Theme</h2>
  633.                <table class="form-table">
  634.                    <tr class="form-field">
  635.                        <th scope="row" valign="top"><label for="theme">Theme</label></th>
  636.                        <td>
  637.                            <select name="theme" id="theme" class="postform" >
  638.            <option value="pthemedefault">[Default]</option>';
  639.                             $themes = function_exists( 'wp_get_themes' ) ? wp_get_themes() : get_themes();
  640.                             foreach($themes as $theme){
  641.                                 if($theme['Template'] == $currentTheme) {
  642.                                     echo '<option value="'.$theme["Template"].'" selected>'.$theme["Name"].'</option>';
  643.                                 }
  644.                                 else {
  645.                                     echo '<option value="'.$theme["Template"].'">'.$theme["Name"].'</option>';
  646.                                 }
  647.                             }
  648.          echo '             </select>
  649. <ul id="fcbklist">
  650. <li id="pthemedefault"><div style="font-weight:bold;">[Default]</div><img src="' . plugin_dir_url(__FILE__) . 'white.png" /></li>';
  651.         foreach($themes as $theme){
  652.             echo '<li id="'.$theme['Template'].'"><div style="font-weight:bold;">'.$theme["Name"].'</div><img src="'.$theme["Theme Root URI"].'/'.$theme["Stylesheet"].'/'.$theme["Screenshot"]."\" /></li>\n";
  653.         }
  654.         echo '                     </ul>
  655.                                    <br />
  656.                                    Specify the theme to use when the site is accessed by the given page.
  657.                                </td>
  658.                            </tr>
  659.                        </table>
  660.                    <p class="submit"><input type="submit" id="pthemesubmit" class="button" name="submit" value="Set Theme" /></p>
  661.                </div>
  662.        ';
  663.     }
  664.  
  665.     function pcompsave() {
  666.         if(empty($_GET['post_id']) || empty($_GET['theme'])) {
  667.             die("Missing parameters");
  668.         }
  669.         $post_id = intval($_GET['post_id']);
  670.         if(!$post_id) {
  671.             die("Invalid post id");
  672.         }
  673.         $theme_name = mysql_real_escape_string($_GET['theme']);
  674.         $post = array();
  675.         $post['id']  = $post_id;
  676.         $postInfo = $this->getPostByID($post_id);
  677.         if(!$postInfo) {
  678.             die("Error retrieving post name");
  679.         }
  680.         $post['url'] = strtolower($postInfo->post_name);
  681.         $post['theme'] = $theme_name;
  682.         $options = $this->getPageThemeOptions();
  683.         $newOptions = array();
  684.         foreach($options as $option) {
  685.             if($option['id'] == $post_id) {
  686.                 continue;
  687.             }
  688.             array_push($newOptions, $option);
  689.         }
  690.         if($theme_name != 'pthemedefault') {
  691.             array_push($newOptions, $post); // add new option for this post
  692.         }
  693.         PageThemeOptions::getInstance()->setOptions($newOptions);
  694.  
  695.         if($theme_name != 'pthemedefault') {
  696.             $theme = $this->getThemeInfoByTemplate($theme_name);
  697.             $response = "OK\n" . $theme['Name'] . "\n" . $theme["Theme Root URI"].'/'.$theme["Stylesheet"].'/'.$theme["Screenshot"];
  698.         }
  699.         else {
  700.             $response = "OK\n<em>Default Theme</em>\n".plugin_dir_url(__FILE__)."white.png";
  701.         }
  702.         echo $response;
  703.     }
  704. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement