Skip to content

Instantly share code, notes, and snippets.

@parse
Created August 23, 2010 11:34
Show Gist options
  • Save parse/545298 to your computer and use it in GitHub Desktop.
Save parse/545298 to your computer and use it in GitHub Desktop.
<?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;
}
<?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();
}
}
<?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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment