<?php
/*
Plugin Name: Wordpress PDA & iPhone
Plugin URI: http://imthi.com/wp-pda/
Description: This plugin helps the users to view your blog in a pda and iPhone browser.
Author: Imthiaz Rafiq
Version: 1.2.8
Author URI: http://imthi.com/
*/
class PDAPlugin{
var $pda;
var $iphone;
function PDAPlugin(){
$this->pda = false;
$this->iphone = false;
$defaultUserAgents = "Elaine/3.0, iPhone, iPod, Palm, EudoraWeb, Blazer, AvantGo, Windows CE, Cellphone, Small, MMEF20, Danger, hiptop, Proxinet, ProxiNet, Newt, PalmOS, NetFront, SHARP-TQ-GX10, SonyEricsson, SymbianOS, UP.Browser, UP.Link, TS21i-10, MOT-V, portalmmm, DoCoMo, Opera Mini, Palm, Handspring, Nokia, Kyocera, Samsung, Motorola, Mot, Smartphone, Blackberry, WAP, SonyEricsson, PlayStation Portable, LG, MMP,OPWV, Symbian, EPOC";
if(get_option('pda_browser_agents')==false){
add_option('pda_browser_agents',$defaultUserAgents);
}
add_action('plugins_loaded',array(&$this,'detectPDA'));
add_action('admin_menu', array(&$this,'pdaAdminMenu'));
add_filter('theme_root',array(&$this,'theme_root'));
add_filter('theme_root_uri',array(&$this,'get_theme_root_uri'));
add_filter('stylesheet',array(&$this,'get_stylesheet'));
add_filter('post_link',array(&$this,'updateToIphoneLink'),10,2);
add_filter('page_link',array(&$this,'updateToIphoneLink'),10,2);
add_filter('category_link',array(&$this,'updateToIphoneLink'),10,2);
add_filter('year_link',array(&$this,'updateToIphoneLink'),10,2);
add_filter('month_link',array(&$this,'updateToIphoneLink'),10,2);
add_filter('day_link',array(&$this,'updateToIphoneLink'),10,2);
add_filter('template',array(&$this,'get_template'));
add_filter('option_template',array(&$this,'get_template_directory'));
add_filter('option_stylesheet',array(&$this,'get_template_directory'));
}
function pdaAdminMenu(){
if ( function_exists('add_submenu_page') ) {
add_submenu_page('plugins.php', __('PDA'), __('PDA'),'manage_options' , 'pda-options' ,array(&$this,'pdaPluginOption'));
}
}
function updateToIphoneLink($link,$value){
if($this->iphone){
if(strpos($link,'iphone')===false){
if(strpos($link,'?')===false){
$link .= '?iphone=true';
}else{
$link .= '&iphone=true';
}
}
}
return $link;
}
function pdaPluginOption(){
if ( isset($_POST['submit']) ) {
if ( function_exists('current_user_can') && !current_user_can('manage_options') ){
die(__('Cheatin’ uh?'));
}
update_option('pda_browser_agents', $_POST['pda_browser_agents']);
}
include_once('pda-option.php');
}
function getBrowserAgentsToDetect(){
$browserAgents = get_option('pda_browser_agents');
if(!empty($browserAgents)){
$browserAgents = explode(',',$browserAgents);
if(!empty($browserAgents)){
foreach ($browserAgents as $key => $value){
$browserAgents[$key] = trim($value);
}
return $browserAgents;
}
}
return array();
}
function detectPDA($query){
$browserAgent = $_SERVER['HTTP_USER_AGENT'];
$userAgents = $this->getBrowserAgentsToDetect();
foreach ( $userAgents as $userAgent ) {
if(eregi($userAgent,$browserAgent)){
if(eregi("iphone",$browserAgent) || eregi("ipod",$browserAgent) ){
$this->iphone = true;
}else{
$this->pda = true;
}
}
}
}
function get_template_directory($value){
if($this->pda){
return 'pda-theme';
}elseif($this->iphone){
return 'iphone-theme';
}else{
return $value;
}
}
function theme_root($path){
$theme_root = dirname(__FILE__);
if($this->pda || $this->iphone){
return $theme_root;
}else{
return $path;
}
}
function get_theme_root_uri($url){
if($this->pda || $this->iphone){
return get_settings('siteurl') . "/wp-content/plugins/wp-pda";
}else{
return $url;
}
}
function get_stylesheet($stylesheet) {
if($this->pda){
return 'pda-theme';
}elseif($this->iphone){
return 'iphone-theme';
}else{
return $stylesheet;
}
}
function get_template($template) {
if($this->pda){
return 'pda-theme';
}elseif($this->iphone){
return 'iphone-theme';
}else{
return $template;
}
}
function iPhoneGetMore(){
global $paged, $wp_query;
$max_page = $wp_query->max_num_pages;
if ( !$paged ){
$paged = 1;
}
$nextpage = intval($paged) + 1;
if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
$nextLink = get_next_posts_page_link($max_page);
if(strpos($nextLink,'loadmore')===false){
if(strpos($nextLink,'?')===false){
$nextLink .= '?loadmore=true';
}else{
$nextLink .= '&loadmore=true';
}
}
return $nextLink;
}else{
return false;
}
}
}
$wp_pda = new PDAPlugin();