web/wp-content/plugins/xilitheme-select/xilithemeselect.php
changeset 170 8e3a5a6fc63e
child 194 32102edaa81b
equal deleted inserted replaced
169:2d410dc8c1f3 170:8e3a5a6fc63e
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: xili-theme-select
       
     4 Plugin URI: http://dev.xiligroup.com/xilitheme-select/
       
     5 Description: A plugin for WordPress that automatically redirects your blog's theme for optimized viewing on Apple's <a href="http://www.apple.com/iphone/">iPhone</a> and <a href="http://www.apple.com/ipodtouch/">iPod touch</a>.
       
     6 Author: MS xiligroup dev 
       
     7 Version: 1.0.1
       
     8 Author URI: http://dev.xiligroup.com
       
     9 
       
    10 # inspired initially from iwphone from Robot Content - 2007 - (http://www.contentrobot.com)
       
    11 # 2008-10 : optimized rewritting with class and optional dashboard settings
       
    12 # the "iphone / itouch" theme folder must have the same name with an extension ('_4touch') declared in instantiation
       
    13 # examples of instantiations :
       
    14 # $wp_ismobile = new xilithemeselector(true); //true if dashboard part 
       
    15 # $wp_ismobile = new xilithemeselector(false); // false w/o dashboard (default extension = '_4touch')
       
    16 # $wp_ismobile = new xilithemeselector(false,'_yourext'); //- false w/o dashboard and your extension
       
    17 #
       
    18 # USE var $wp_ismobile->iphone if necessary in your functions... (see code below at line > 105)
       
    19 
       
    20 # This plugin is free software; you can redistribute it and/or
       
    21 # modify it under the terms of the GNU Lesser General Public
       
    22 # License as published by the Free Software Foundation; either
       
    23 # version 2.1 of the License, or (at your option) any later version.
       
    24 #
       
    25 # This plugin is distributed in the hope that it will be useful,
       
    26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
       
    28 # Lesser General Public License for more details.
       
    29 #
       
    30 # You should have received a copy of the GNU Lesser General Public
       
    31 # License along with this plugin; if not, write to the Free Software
       
    32 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
       
    33 
       
    34 */
       
    35 
       
    36 define('XILITHEME_VER','1.0.1');
       
    37 
       
    38 class xilithemeselector{
       
    39 	
       
    40 	var $themextend = ""; /*no extension = no touch theme*/
       
    41 	var $iphone = false;
       
    42 	var $currentthemefolder; /*the normal theme used by default*/
       
    43 	var $newfolder;
       
    44 	var $listofdevices = array("iPhone","iPod"); /* if present selector is activated - can be upgraded !*/
       
    45 	var $xitype; /*type of the selected device*/
       
    46 	var $xiversion; /*release of the selected device*/
       
    47 	/**/
       
    48 	var $testnoappledevices = true; /* if true call a function to check no apple devices*/
       
    49 	var $othermobile = false;
       
    50 	var $listnonappledevices = array(array("Opera","_4opera"),array("opera mini","_4touch"),array("Windows CE","_4touch"),array("Blackberry",""),array("Android","_4touch"));
       
    51 	var $othermobiletype = ""; /* filled when $othermobile == true */
       
    52 	/**/
       
    53 	var $cookienable = 0; /*disable by default see after instantiation*/
       
    54 	var $mode = 'mobilebrowser'; /*as usual*/
       
    55 	
       
    56 	function xilithemeselector($dashboard = true, $extend = "_4touch"){
       
    57 		if ($extend != "" && $dashboard == false) $this->themextend = $extend;
       
    58 		
       
    59 		add_action('plugins_loaded',array(&$this,'detectiPhone'));
       
    60 		add_filter('stylesheet',array(&$this,'get_stylesheet'));
       
    61 		add_filter('template',array(&$this,'get_template'));
       
    62 		//add_filter('template_directory', array(&$this,'change_template_path')); // add for pages (template)
       
    63 		
       
    64 		
       
    65 		/*admin part - if not : use extension only */
       
    66 		if ($dashboard == true) :
       
    67 			
       
    68 			add_action('admin_menu', array(&$this, 'add_admin_pages'));
       
    69 			add_action('init', array(&$this, 'init_textdomain'));
       
    70 			add_filter('plugin_action_links', array(&$this, 'xilisel_filter_plugin_actions'), 10, 2); /*0.9.3*/
       
    71 			
       
    72 			$options =  get_option('xilithemeselect_options');
       
    73 			if (!is_array($options))
       
    74 			{
       
    75 				$options['xilithemeextension'] = "_4touch";
       
    76 				$options['isxilithemefullname'] = 'extension';
       
    77 				$options['xilithemefullname'] = "default_4touch";
       
    78 			}
       
    79 			if (!isset($options['xilithemeextension']))
       
    80 				$options['xilithemeextension'] = "_4touch";
       
    81 			if ('' == $options['xilithemefullname'])
       
    82 				$options['xilithemefullname'] = "default_4touch";	
       
    83 			foreach ($options as $option_name => $option_value)
       
    84 	        	$this-> {$option_name} = $option_value;
       
    85 			$this->themextend = $this->xilithemeextension;
       
    86 			$this->isfullname = false;
       
    87 			if ($this->isxilithemefullname == 'full') 
       
    88 										$this->isfullname = true;
       
    89 				
       
    90 		endif;	
       
    91 		
       
    92 	}
       
    93 	
       
    94 	function init_textdomain () {
       
    95 		load_plugin_textdomain('xilithemeselect', PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)), dirname(plugin_basename(__FILE__)));
       
    96 	}
       
    97 	
       
    98 	/**
       
    99  * Add action link(s) to plugins page
       
   100  * 
       
   101  * @since 0.9.3
       
   102  * @author MS
       
   103  * @copyright Dion Hulse, http://dd32.id.au/wordpress-plugins/?configure-link and scripts@schloebe.de
       
   104  */
       
   105 	function xilisel_filter_plugin_actions($links, $file){
       
   106 		static $this_plugin;
       
   107 
       
   108 		if( !$this_plugin ) $this_plugin = plugin_basename(__FILE__);
       
   109 
       
   110 		if( $file == $this_plugin ){
       
   111 			$settings_link = '<a href="options-general.php?page=xilithemeselect">' . __('Settings') . '</a>';
       
   112 			$links = array_merge( array($settings_link), $links); // before other links
       
   113 		}
       
   114 	return $links;
       
   115 }
       
   116 
       
   117 
       
   118 	
       
   119 	function detectiPhone($query){
       
   120 		/* prepare target theme folders according options and test them */
       
   121 		$this->currentthemefolder = str_replace(get_theme_root()."/","",get_template_directory());
       
   122 		if($this->isfullname) :
       
   123 			/*test if theme with fullname exist*/
       
   124 			$curpath = get_theme_root()."/".$this->xilithemefullname;
       
   125 			if (file_exists($curpath)) :
       
   126 				$this->newfolder = $this->xilithemefullname;
       
   127 			else :
       
   128 				//echo "CAUTION: THE ".$this->xilithemefullname." THEME FOLDER IS NOT PRESENT";
       
   129 				$this->newfolder = $this->currentthemefolder; /*display the theme for current browsers*/
       
   130 			endif;
       
   131 		else :
       
   132 			/*test if theme with extension exist*/
       
   133 			$curpath = get_theme_root()."/".$this->currentthemefolder.$this->themextend;
       
   134 			if (file_exists($curpath)) :
       
   135 				$this->newfolder = $this->currentthemefolder.$this->themextend;
       
   136 			else :
       
   137 				//echo "CAUTION: THE ".$this->currentthemefolder." THEME FOLDER WITH EXTENSION ".$this->themextend." IS NOT PRESENT";
       
   138 				$this->newfolder = $this->currentthemefolder; /*display the theme for current browsers*/
       
   139 			endif;	
       
   140 		endif;
       
   141 		/* analyse type of browsers */
       
   142 		$container = $_SERVER['HTTP_USER_AGENT'];
       
   143 		//print_r($container); //this prints out the user agent array. uncomment to see it shown on page.
       
   144 		$userdevices = $this->listofdevices;
       
   145 		
       
   146 		foreach ( $userdevices as $userdevice ) {
       
   147 			if (eregi($userdevice,$container)){
       
   148 				$this->iphone = true;
       
   149 				$this->xitype = $userdevice;
       
   150 				/* version for future uses - like 3D css 2.1 in theme*/
       
   151 				$this->xiversion = preg_replace('#((.+)(Version/)(.+) M(.+))#i','$4' ,$container);
       
   152 				//print_r(preg_grep("#i#",array('a'=>'opo','b'=>'uiop')));
       
   153 				//echo " ---";
       
   154 				break;
       
   155 			}
       
   156 		}
       
   157 		
       
   158 		if ($this->testnoappledevices == true && $this->iphone == false) {
       
   159 		   $this->checknoappledevices();
       
   160 		   
       
   161 		}
       
   162 	}
       
   163 	
       
   164 	function checknoappledevices() {
       
   165 		
       
   166 		$container = $_SERVER['HTTP_USER_AGENT'];
       
   167 		$userdevices = $this->listnonappledevices;
       
   168 		
       
   169 		foreach ( $userdevices as $userdevice ) {
       
   170 			
       
   171 			if (ereg($userdevice[0],$container)){
       
   172 				$this->othermobile = true;
       
   173 				$this->othermobiletype = $userdevice[0];
       
   174 				$this->otherthemextend = $userdevice[1];
       
   175 				$curpath = get_theme_root()."/".$this->currentthemefolder.$this->otherthemextend;
       
   176 				if (file_exists($curpath)) {
       
   177 					$this->newfolder = $this->currentthemefolder.$this->otherthemextend;
       
   178 				} else {
       
   179 				echo "<br />CAUTION: THE ".$this->currentthemefolder." THEME FOLDER WITH EXTENSION ".$this->otherthemextend." IS NOT PRESENT";
       
   180 					$this->newfolder = $this->currentthemefolder; /*display the theme for current browsers*/
       
   181 				}
       
   182 				break;
       
   183 			}
       
   184 		}
       
   185 	}
       
   186 		
       
   187 	/* theme's selection according mobile */
       
   188 	function get_stylesheet($stylesheet) {
       
   189 		if($this->iphone && $this->mode=='mobilebrowser'){
       
   190 			return $this->newfolder;
       
   191 		} elseif ($this->othermobile){	
       
   192 			return $this->newfolder;
       
   193 		} else {
       
   194 			return $stylesheet;
       
   195 		}
       
   196 	}
       
   197 	
       
   198 	function get_template($template) {
       
   199 		if($this->cookienable == 1) $this->set_xilitheme_cookie();
       
   200 		
       
   201 		if($this->iphone && $this->mode=='mobilebrowser'){
       
   202 			return $this->newfolder;
       
   203 		} elseif ($this->othermobile){	
       
   204 			return $this->newfolder;
       
   205 		} else {
       
   206 			return $template;
       
   207 		}
       
   208 	}
       
   209 	
       
   210 	function change_template_path($template_dir) {
       
   211 		if($this->iphone && $this->mode=='mobilebrowser'){
       
   212 			//because use TEMPLATEPATH absolute so find page.php or the template page...
       
   213 			// major add on inspired iwphone
       
   214 			$template_dir = get_theme_root() . "/".$this->newfolder;
       
   215 		} elseif ($this->othermobile) {
       
   216 			$template_dir = get_theme_root() . "/".$this->newfolder;	
       
   217 		}
       
   218 		//echo "----".$template_dir;
       
   219 		return $template_dir;	
       
   220 	}
       
   221 	
       
   222 	/* cookie part for theme selection if iphone of ipod */
       
   223 	function set_xilitheme_cookie() {
       
   224 	$expire = time() + 30000000;
       
   225 		if (!empty($_GET["xilitheme"])):
       
   226 			setcookie("xilitheme" . COOKIEHASH,
       
   227 							stripslashes($_GET["xilitheme"]),
       
   228 							$expire,
       
   229 							COOKIEPATH
       
   230 							);
       
   231 			$this->mode = $_GET["xilitheme"];
       
   232 			
       
   233 			$refreshinglink = $this->build_thelink();
       
   234 			
       
   235 			if (function_exists('wp_redirect'))
       
   236 					wp_redirect($refreshinglink);
       
   237 				else
       
   238 					header("Location: ". $refreshinglink);
       
   239 			
       
   240 			exit;/**/
       
   241 		endif;
       
   242 		if ($this->get_xilitheme_cookie()==''):
       
   243 			$this->mode = "mobilebrowser";
       
   244 		else:
       
   245 			$this->mode = $this->get_xilitheme_cookie();
       
   246 		endif;
       
   247 	}
       
   248 	
       
   249 	function get_xilitheme_cookie() {
       
   250 		if (!empty($_COOKIE["xilitheme" . COOKIEHASH])) :
       
   251 			return $_COOKIE["xilitheme" . COOKIEHASH];
       
   252 		else:
       
   253 			return '';
       
   254 		endif;
       
   255 	}
       
   256 	
       
   257 	function build_thelink($type4refresh=true,$modelink="browser") {
       
   258 		$querystring = "";
       
   259 			/*restore the _GET*/
       
   260 			$permalink = get_option('permalink_structure');
       
   261 			if ( '' != $permalink) :
       
   262 			   $q = "?";
       
   263 			   $querystring = str_replace(preg_replace('|/[^/]+?$|', '/', $_SERVER['PHP_SELF']),'',$_SERVER['REQUEST_URI']);
       
   264 			   
       
   265 			   if ($type4refresh): 
       
   266 			   			//ignore this ?xilitheme $_GET value
       
   267 			   			$querystring = preg_replace('#\?xilitheme(.+)browser#isU', '',$querystring );
       
   268 			   endif;			
       
   269 			else:
       
   270 				$q = "&";
       
   271 				$i=0;
       
   272 				foreach ($_GET as $key => $value) {
       
   273             		if ($key != "xilitheme") {  // ignore this particular $_GET value
       
   274                 		if ($i == 0) $querystring = "?";
       
   275                 		if ($i > 0)  $querystring .= "&";
       
   276                 		$querystring .= $key."=".$value;
       
   277                 		$i++;
       
   278             		}
       
   279 				}
       
   280 			endif;
       
   281 			if ($type4refresh):
       
   282 				 return get_option('siteurl').'/'.$querystring;
       
   283 			else:
       
   284 				if ($querystring == "")
       
   285 					return get_option('siteurl').'/?xilitheme='.$modelink;
       
   286 				else
       
   287 					return get_option('siteurl').'/'.$querystring.$q.'xilitheme='.$modelink;	
       
   288 			endif;		
       
   289 	}
       
   290 	function display_themetype($displayhtml = array('before'=>'<span class="xilithemelink">','after'=>'</span>','bro'=>'browser','mobbro'=>'iTouch')){
       
   291 	/* return the link display in current theme or in mobile theme */
       
   292 		if ($this->iphone == true && $this->cookienable == 1) :
       
   293 			
       
   294 			if (($this->get_xilitheme_cookie() == 'browser')) {	
       
   295 						$output = stripslashes($displayhtml['before']).'<a href="'
       
   296 						.$this->build_thelink(false,"mobilebrowser").'">'
       
   297 						.stripslashes($displayhtml['mobbro']).'</a>'.stripslashes($displayhtml['after']);
       
   298 			} else {	
       
   299 					$output = stripslashes($displayhtml['before']).'<a href="'
       
   300 						.$this->build_thelink(false,"browser").'">'
       
   301 						.stripslashes($displayhtml['bro']).'</a>'.stripslashes($displayhtml['after']);
       
   302 			}
       
   303 	    	return $output;
       
   304 	    endif;
       
   305 	    
       
   306 	}
       
   307 	
       
   308 
       
   309 	
       
   310 	
       
   311 	
       
   312 	/* admin part */
       
   313 
       
   314 	function add_admin_pages()
       
   315 		{
       
   316 		add_options_page('Xilitheme select', 'Xilitheme select', 8, 'xilithemeselect', array(&$this, 'option_page'));
       
   317 		}
       
   318 	function option_page()
       
   319 		{
       
   320 			if ( isset($_POST['submitted']) ) {
       
   321 		$options = array();
       
   322 		$options['xilithemeextension'] = $_POST['xilithemeextension'];
       
   323 		$options['isxilithemefullname'] = $_POST['isxilithemefullname'];
       
   324 		$options['xilithemefullname'] = $_POST['xilithemefullname'];
       
   325 		
       
   326 		update_option('xilithemeselect_options', $options);
       
   327 		foreach ($options as $option_name => $option_value)
       
   328 	        $this-> {$option_name} = $option_value;	  
       
   329 		echo '<div id="message" class="updated fade"><p>'.__("Plugin<strong> xilithemeselect</strong> settings saved.","xilithemeselect").'</p></div>';
       
   330 		}
       
   331 		
       
   332 	?>
       
   333 		<div class='wrap'>
       
   334 		<h2><?php _e("Xilitheme select settings","xilithemeselect"); ?></h2>
       
   335 		<p><cite><a href='http://www.xiliphone.mobi' target='_blank'>Xilitheme select</a></cite> <?php _e("provides an automatic selection of the theme for iphone's user when visiting your site.<br />Don't forget to upload a specific theme for ipod touch / iphone with a right folder's name in the wp-content/themes folder.<br /> If an error occur, the current theme is displayed in ipod touch / iphone browser.","xilithemeselect"); ?></p>
       
   336 		<p><?php _e("the current theme is","xilithemeselect"); echo ": <em>".$this->currentthemefolder ?>. </em><strong><br />
       
   337 		<?php _e("itouch theme's folder","xilithemeselect"); echo ": <em>".$this->currentthemefolder.$this->xilithemeextension."</em> "; 
       
   338 		$curpath = get_theme_root()."/".$this->currentthemefolder.$this->xilithemeextension;
       
   339 			if (file_exists($curpath)) :
       
   340 				_e("is available","xilithemeselect");
       
   341 			else :
       
   342 				_e("is NOT available","xilithemeselect");
       
   343 			endif;			
       
   344 		?>, </strong>
       
   345 		<br /><strong>
       
   346 		<?php _e("itouch theme's folder","xilithemeselect"); echo ": <em>".$this->xilithemefullname."</em> "; 
       
   347 		$curpath = get_theme_root()."/".$this->xilithemefullname;
       
   348 			if (file_exists($curpath)) :
       
   349 				_e("is available","xilithemeselect");
       
   350 			else :
       
   351 				_e("is NOT available","xilithemeselect");
       
   352 			endif;			
       
   353 		?>  </strong>
       
   354 		<br /><?php _e("in the current wp-content/themes folder.","xilithemeselect");?></p>
       
   355 		<form name="xiliphone" action="<?php echo $action_url; ?>" method="post">
       
   356 			<input type="hidden" name="submitted" value="1" />
       
   357 				
       
   358 			<fieldset class="options">
       
   359 				<ul>
       
   360 					<li>
       
   361 					<label for="xilithemeextension">
       
   362 						<?php _e("itouch theme's folder extension:","xilithemeselect"); ?>
       
   363 						<input type="text" id="xilithemeextension" name="xilithemeextension"
       
   364 							size="7" maxlength="8"
       
   365 							value="<?php echo $this->xilithemeextension; ?>" />
       
   366 					</label>
       
   367 					</li>
       
   368 					<li>
       
   369 					<label for="isxilithemefullname">
       
   370 						<?php _e("Option: extension or full name:","xilithemeselect"); ?>
       
   371 						<select name="isxilithemefullname">
       
   372                     <option value="full"<?php if ($this->isxilithemefullname == 'full') { ?> selected="selected"<?php } ?> ><?php _e('full name','xilithemeselect'); ?></option>
       
   373                     <option value="extension"<?php if ($this->isxilithemefullname == 'extension') { ?> selected="selected"<?php } ?> ><?php _e('extension','xilithemeselect'); ?></option>
       
   374                     </select>
       
   375 					</label>
       
   376 					</li>
       
   377 					<li>
       
   378 					<label for="xilithemefullname">
       
   379 						<?php _e("itouch theme's folder full name:","xilithemeselect"); ?>
       
   380 						<input type="text" id="xilithemefullname" name="xilithemefullname"
       
   381 							size="20" maxlength="30"
       
   382 							value="<?php echo $this->xilithemefullname; ?>" />
       
   383 					</label>
       
   384 					</li>
       
   385 					
       
   386 				</ul>
       
   387 			</fieldset>
       
   388 			<p class="submit"><input type="submit" name="Submit" value="<?php _e('Save Changes'); ?> &raquo;" /></p>
       
   389 		</form>
       
   390 		<h4><a href="http://dev.xiligroup.com/xilitheme-select/" title="Plugin page and docs" target="_blank" style="text-decoration:none" ><img style="vertical-align:middle" src="<?php echo WP_PLUGIN_URL.'/'.dirname(plugin_basename(__FILE__)).'/xilitheme-logo-32.gif'; ?>" alt="xilitheme-select logo"/>  xilitheme-select</a> - © <a href="http://dev.xiligroup.com" target="_blank" title="<?php _e('Author'); ?>" >xiligroup.com</a>™ - msc 2007-9 - v. <?php echo XILITHEME_VER; ?></h4>
       
   391 	</div>
       
   392 	<?php
       
   393 	}
       
   394 
       
   395 }
       
   396 
       
   397 global $wp_ismobile;
       
   398 $wp_ismobile = new xilithemeselector(true); //true if dashboard part - false w/o dashboard - see doc on top
       
   399 $wp_ismobile->cookienable = 1; /*0 to disable*/
       
   400 
       
   401 /**** tag insertable in the theme's footer or elsewhere ****/
       
   402 /* example 1 (in browser): if (function_exists('the_xilithemelink')) the_xilithemelink('mobbro=<img src="'.get_bloginfo("template_directory").'/images/mobilelink.gif" alt=""/>');
       
   403 */
       
   404 /* example 2 (in mobilebrowser): if (function_exists('the_xilithemelink')) the_xilithemelink('bro=naviguer en mode normal');
       
   405 */
       
   406 function the_xilithemelink($args = ''){
       
   407 	
       
   408 	if ( is_array($args) )
       
   409 		$r = &$args;
       
   410 	else
       
   411 		parse_str($args, $r);
       
   412 		
       
   413 	$defaults = array('before'=>'<span class="xilithemelink">','after'=>'</span>','bro'=>'browser','mobbro'=>'iTouch');
       
   414 	$r = array_merge($defaults, $r);
       
   415 	
       
   416 	global $wp_ismobile; 
       
   417 	echo $wp_ismobile->display_themetype($r);
       
   418 }	
       
   419 ?>