public
Last active

  • Download Gist
functions.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?php
/**
* /iPhone webapp & Favicon
*/
if (!function_exists('xc1_favicon')) {
function xc1_favicon() {
if (file_exists ( CHILD_THEME_IMAGES_PATH . 'xc1-iphone.png' ))
$apple_icon = CHILD_THEME_IMAGES_URI . 'xc1-iphone.png';
else
$apple_icon = XC1_THEME_IMAGES_URI . 'xc1-iphone.png';
if (file_exists ( CHILD_THEME_IMAGES_PATH . 'xc1-iphone-startup.png' ))
$apple_startup = CHILD_THEME_IMAGES_URI . 'xc1-iphone-startup.png';
else
$apple_startup = XC1_THEME_IMAGES_URI . 'xc1-iphone-startup.png';
if (file_exists ( CHILD_THEME_IMAGES_PATH . 'xc1-favicon.png' ))
$favicon_icon = CHILD_THEME_IMAGES_URI . 'xc1-favicon.png';
else
$favicon_icon = XC1_THEME_IMAGES_URI . 'xc1-favicon.png';
if (get_option ( 'show_avatars' )) {
echo "<link rel=\"apple-touch-icon-precomposed\" href=\"$apple_icon\" />\n";
echo "<link rel=\"apple-touch-startup-image\" href=\"$apple_startup\" />\n";
echo "<link rel=\"shortcut icon\" type=\"image/png\" href=\"$favicon_icon\" />\n";
}
}
}
 
/**
* Check to see if this page will paginate
*
* @return boolean
*/
function will_paginate()
{
global $wp_query;
if ( !is_singular() ) {
$max_num_pages = $wp_query->max_num_pages;
if ( $max_num_pages > 1 ) {
return true;
}
}
return false;
}
rewrites.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
<?php
class XC1_Mobile_Rewrites extends XC1_Mobile {
public function __construct($mobile) {
$this->mobile = $mobile;
}
/*
* From .html on pages (http://wordpress.org/extend/plugins/html-on-pages/) by IntroSites
*/
public function html_page_permalink() {
global $wp_rewrite;
if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')) {
$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
}
}
public function no_page_slash($string, $type){
global $wp_rewrite;
if ($wp_rewrite->using_permalinks() && $wp_rewrite->use_trailing_slashes == true && $type == 'page') {
return untrailingslashit($string);
} else {
return $string;
}
}
public function active() {
global $wp_rewrite;
if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')) {
$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
}
$wp_rewrite->flush_rules();
}
public function deactive() {
global $wp_rewrite;
$wp_rewrite->page_structure = str_replace(".html","",$wp_rewrite->page_structure);
$wp_rewrite->flush_rules();
}
 
// Remember to flush_rules() when adding rules
public function flushRules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
 
}
xc1-mobile.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
<?php
/*
Plugin Name: XC1 Mobile
Plugin URI:
Description: XC1 made mobile compatible
Version: 0.9
Author: Anders Hassis
Author URI: http://hassis.com
*/
 
if (!defined('WP_SITEURL')) {
define('WP_SITEURL', get_option('siteurl'));
}
 
if (!defined('WP_HOME')) {
define('WP_HOME', get_option('home'));
}
 
require_once('includes/functions.php');
require_once('includes/rewrites.php');
 
class XC1_Mobile {
private $themeChildPath;
private $themeChildURI;
private $themeMobileName;
private $themeMobilePath;
private $themeMobileURI;
private $desiredView;
private $pluginURI;
private $pluginPath;
public $Rewrites;
public function __construct() {
$this->Rewrites = new XC1_Mobile_Rewrites($this);
// Check if user switches layout
if (isset($_GET['xc1-layout'])) {
switch ($_GET['xc1-layout']) {
case 'mobile':
$this->setLayout('mobile');
break;
default:
$this->setLayout('default');
break;
}
}
add_filter('init', array(&$this->Rewrites, 'flushRules') );
if (isset($_COOKIE['xc1-layout'])) {
if ($_COOKIE['xc1-layout'] == 'mobile') {
$this->desiredView = 'mobile';
} else {
$this->desiredView = 'default';
}
} else {
if ( self::isMobile() ) {
$this->desiredView = 'mobile';
} else {
$this->desiredView = 'default';
}
}
if ($this->desiredView == 'mobile') {
$this->init();
}
}
private function init() {
// Make sure we have a trailing .html on pages
add_action('init', array(&$this->Rewrites, 'html_page_permalink'), -1);
register_activation_hook(__FILE__, array(&$this->Rewrites, 'active'), '');
register_deactivation_hook(__FILE__, array(&$this->Rewrites, 'deactive'), '');
add_filter('user_trailingslashit', array(&$this->Rewrites, 'no_page_slash'), '',66,2);
// Define the URI to plugin
$this->pluginURI = WP_PLUGIN_URL . "/" . basename(dirname(__FILE__)) . "/";
$this->pluginPath = WP_PLUGIN_DIR . "/" . basename(dirname(__FILE__)) . "/";
// Find our child/parent theme
$this->themeChildPath = get_stylesheet_directory();
$this->themeChildURI = get_stylesheet_directory_uri();
 
// Hook into it and add our trailing -mobile
$this->themeMobileName = get_stylesheet() . '-mobile';
$this->themeMobilePath = $this->themeChildPath . '-mobile';
$this->themeChildURI = $this->themeChildURI . '-mobile';
// Make sure the mobile theme exists
if (!is_dir($this->themeMobilePath))
return;
 
// Constants (XC1-style)
$this->constants();
 
// Buckle up!
if ( !is_admin() ) {
add_filter('stylesheet', array(&$this, 'get_stylesheet') );
}
}
public function setLayout($layout = 'mobile') {
if ($layout == 'mobile') {
setcookie('xc1-layout', '', time() - 3600);
setcookie('xc1-layout', 'mobile', time()+3600*24*365); /* expire in 365 days */
} else {
setcookie('xc1-layout', '', time() - 3600);
setcookie('xc1-layout', 'default', time()+3600*24*365); /* expire in 365 days */
}
header("Location: " . WP_HOME);
die();
}
private function constants() {
// Dont like this trailing slash...
define('MOBILE_THEME_PATH', $this->themeMobilePath . '/' );
define('MOBILE_THEME_URI', $this->themeChildURI . '/' );
define('MOBILE_THEME_STATIC_PATH', $this->themeMobilePath . '/static/' );
define('MOBILE_THEME_STATIC_URI', $this->themeChildURI . '/static/' );
define('MOBILE_THEME_CSS_PATH', MOBILE_THEME_STATIC_PATH . 'css/' );
define('MOBILE_THEME_CSS_URI', MOBILE_THEME_STATIC_URI . 'css/' );
 
define('MOBILE_THEME_JS_PATH', MOBILE_THEME_STATIC_PATH . 'js/' );
define('MOBILE_THEME_JS_URI', MOBILE_THEME_STATIC_URI . 'js/' );
 
define('MOBILE_THEME_IMAGES_PATH', MOBILE_THEME_STATIC_PATH . 'images/' );
define('MOBILE_THEME_IMAGES_URI', MOBILE_THEME_STATIC_URI . 'images/' );
}
public function get_stylesheet( $stylesheet ) {
if ($this->desiredView == 'mobile') {
return $this->themeMobileName;
} else {
return $stylesheet;
}
}
public static function isMobile() {
$devices = array(
'acer','alcatel','audiovox','avantgo','blackberry',
'blazer','cdm','digital paths','elaine','epoc',
'ericsson','handspring','iemobile','kyocera','lg',
'midp','mmp','mobile','motorola','nec',
'nokia','o2','openwave','opera mini','operamini',
'opwv','palm','panasonic','pda','phone',
'playstation portable','pocket','psp','qci','sagem',
'sanyo','samsung','sec','sendo','sharp',
'smartphone','sonyericsson','symbian','telit','tsm',
'up-browser','up.browser','up.link','vodafone','wap',
'windows ce','xiino','xda','tosh','upg1',
'upsi','webc','winw','w3c ','acs-',
'alav','amoi','benq','bird','brew',
'cell','cldc','cmd-','dang','doco',
'hipt','inno','ipaq','jigs','kddi',
'keji','leno','lg-c','lg-d','lg-g',
'lge-','maui','maxo','midp','mits',
'mmef','newt','pant','phil',
'prox','qwap','sph-','t-mo','tim-',
'vk-v'
);
if( isset( $_SERVER['HTTP_USER_AGENT'])) {
$agent = strtolower( $_SERVER['HTTP_USER_AGENT']);
foreach( $devices as $browser) {
if( strpos( $agent, $browser)) {
return true;
}
}
return false;
}
}
}
 
$mobile = new XC1_Mobile();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.