Advertisement
pierovdfn

Plugin Resolution [Flatpress]

Nov 5th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.55 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Resolution
  4. Plugin URI: http://www.pierovdfn.it/
  5. Description: Adds a custom stylesheet for every resolution
  6. Author: Piero VDFN
  7. Version: 1.0
  8. Author URI: http://www.pierovdfn.it/
  9. */
  10.  
  11. /*
  12.  * HOW TO CONFIGURE THE PLUGIN
  13.  *
  14.  * To configure the stylesheets please go to line 69 and modify the array
  15.  * of the function "getStylesheets" with the resolution as key and the
  16.  * complete URL as value.
  17.  *
  18.  * You can get the URL of the theme by using the function
  19.  * theme_geturl('yourthemename') or
  20.  * theme_style_geturl('yourstyle', 'yourtheme').
  21.  */
  22.  
  23. class PluginResolution {
  24.  
  25.     /**
  26.      * The custom stylesheet to add to the header.
  27.      *
  28.      * @var string
  29.      * @access private
  30.      */
  31.     private $stylesheet = '';
  32.  
  33.     /**
  34.      * The name of the cookie that stores the resolution.
  35.      *
  36.      * @var string
  37.      * @access private
  38.      */
  39.     private $cookieName = 'pl_resolution';
  40.  
  41.     /**
  42.      * The duration of the cookie that stores the resolution in seconds.
  43.      *
  44.      * @var integer
  45.      * @access private
  46.      */
  47.     private $cookieDuration = 2678400; // 31 days
  48.  
  49.     /**
  50.      * The constructor of the class. Registers hooks.
  51.      */
  52.     public function __construct()
  53.     {
  54.         add_action('init', array($this, 'init'));
  55.         add_action('wp_head', array($this, 'head'));
  56.     }
  57.  
  58.     /**
  59.      * Return the array of stylesheets.
  60.      *
  61.      * This is done using a function instead of a member so the user can
  62.      * call other functions.
  63.      *
  64.      * The function must return an array with resolutions as keys and URLs
  65.      * as values. URLs must be complete URL.
  66.      *
  67.      * @return array: see the description
  68.      */
  69.     private function getStylesheets()
  70.     {
  71.         $styles = array(
  72.             //'yourresoltion' => theme_style_geturl('yourtheme', 'yourstyle') . 'file.css',
  73.             '1280x1024' => theme_style_geturl('yourstyle', 'yourtheme') . 'file.css',
  74.         );
  75.  
  76.         return $styles;
  77.     }
  78.  
  79.     /**
  80.      * Handles the init hook.
  81.      */
  82.     public function init()
  83.     {
  84.         $res = @$_COOKIE[$this->cookieName];
  85.         if(isset($_GET['resolution'])) {
  86.             $res = $_GET['resolution'];
  87.         }
  88.  
  89.         $styles = $this->getStylesheets();
  90.         if(!empty($res) && isset($styles[$res])) {
  91.             setcookie($this->cookieName, $res, time() + $this->cookieDuration, BLOG_ROOT);
  92.             $this->stylesheet = $styles[$res];
  93.         } elseif(!empty($_COOKIE[$this->cookieName])) {
  94.             setcookie($this->cookieName, '', time() - 1000, BLOG_ROOT);
  95.         }
  96.     }
  97.  
  98.     /**
  99.      * Handles the wp_head hook.
  100.      */
  101.     public function head()
  102.     {
  103.         if(!empty($this->stylesheet)) {
  104.             echo "\n" . '<link rel="stylesheet" href="' . $this->stylesheet . '" type="text/css" />' . "\n";
  105.         }
  106.     }
  107. }
  108.  
  109. new PluginResolution();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement