Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Resolution
- Plugin URI: http://www.pierovdfn.it/
- Description: Adds a custom stylesheet for every resolution
- Author: Piero VDFN
- Version: 1.0
- Author URI: http://www.pierovdfn.it/
- */
- /*
- * HOW TO CONFIGURE THE PLUGIN
- *
- * To configure the stylesheets please go to line 69 and modify the array
- * of the function "getStylesheets" with the resolution as key and the
- * complete URL as value.
- *
- * You can get the URL of the theme by using the function
- * theme_geturl('yourthemename') or
- * theme_style_geturl('yourstyle', 'yourtheme').
- */
- class PluginResolution {
- /**
- * The custom stylesheet to add to the header.
- *
- * @var string
- * @access private
- */
- private $stylesheet = '';
- /**
- * The name of the cookie that stores the resolution.
- *
- * @var string
- * @access private
- */
- private $cookieName = 'pl_resolution';
- /**
- * The duration of the cookie that stores the resolution in seconds.
- *
- * @var integer
- * @access private
- */
- private $cookieDuration = 2678400; // 31 days
- /**
- * The constructor of the class. Registers hooks.
- */
- public function __construct()
- {
- add_action('init', array($this, 'init'));
- add_action('wp_head', array($this, 'head'));
- }
- /**
- * Return the array of stylesheets.
- *
- * This is done using a function instead of a member so the user can
- * call other functions.
- *
- * The function must return an array with resolutions as keys and URLs
- * as values. URLs must be complete URL.
- *
- * @return array: see the description
- */
- private function getStylesheets()
- {
- $styles = array(
- //'yourresoltion' => theme_style_geturl('yourtheme', 'yourstyle') . 'file.css',
- '1280x1024' => theme_style_geturl('yourstyle', 'yourtheme') . 'file.css',
- );
- return $styles;
- }
- /**
- * Handles the init hook.
- */
- public function init()
- {
- $res = @$_COOKIE[$this->cookieName];
- if(isset($_GET['resolution'])) {
- $res = $_GET['resolution'];
- }
- $styles = $this->getStylesheets();
- if(!empty($res) && isset($styles[$res])) {
- setcookie($this->cookieName, $res, time() + $this->cookieDuration, BLOG_ROOT);
- $this->stylesheet = $styles[$res];
- } elseif(!empty($_COOKIE[$this->cookieName])) {
- setcookie($this->cookieName, '', time() - 1000, BLOG_ROOT);
- }
- }
- /**
- * Handles the wp_head hook.
- */
- public function head()
- {
- if(!empty($this->stylesheet)) {
- echo "\n" . '<link rel="stylesheet" href="' . $this->stylesheet . '" type="text/css" />' . "\n";
- }
- }
- }
- new PluginResolution();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement