Plugin Directory

Changeset 991519


Ignore:
Timestamp:
09/17/2014 12:06:35 AM (11 years ago)
Author:
federico_jacobi
Message:

Complete rewrite and removal of deprecated functions

Location:
theme-to-browser-t2b-control
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • theme-to-browser-t2b-control/trunk/readme.txt

    r772574 r991519  
    33Donate link: http://www.federicojacobi.com/
    44Tags: themes, browser, design, control, browser control, mobile, blackberry, ps3
    5 Requires at least: 2.0.2
    6 Tested up to: 3.2.1
    7 Stable tag: 0.4
     5Requires at least: 3.4
     6Tested up to: 4.0
     7Stable tag: 0.5
    88
    99Displays different themes based on the browser used.
     
    2929Internet Explorer, FireFox, Opera, iPad/iPhone/iPod, Safari, BlackBerry8310, BlackBerry
    3030
    31 What about browser versions (ie5, ie6, operamini) ?
    32 Not yet, but it is high in the todo list. For now IE is all the same.
    33 
    3431Can I add other browsers?
    3532If you know PHP yes ... just got to the code and modify the first few lines, otherwise, drop me a line at web[at]federicojacobi.com and I'll take care of it for you.
     33
     34What about browser versions (ie5, ie6, operamini) ?
     35Not yet, but you can add your own. See question 2.
    3636
    3737What if the browser is not detected?
     
    5151
    5252== Changelog ==
     53= 0.5 =
     54Complete modernization and rewrite. Cleared a bunch of notices and deprecated functions.
     55Added debug mode so y'all can test your custom regex against the current browser, and verify in the frontend if the proper theme is getting loaded.
     56
     57= 0.4 =
     58A couple of bug fixes.
     59
    5360= 0.3 =
    5461Added "Default theme" behavior. A little bit of beautification work. Playstation3 added.
  • theme-to-browser-t2b-control/trunk/t2b.php

    r772574 r991519  
    44Plugin URI: http://www.federicojacobi.com/wp/work/t2b-control/
    55Description: Display a different theme depending on the users browser. It is perfect for a quick way out of CSS annoyances and also for customization on mobile devices. Supports: Internet Explorer, FireFox, Opera, iPhone/iPod, iPad, Safari, BlackBerries, Playstation 3
    6 Version: 0.4
     6Version: 0.5
    77Author: Federico Jacobi
    88Author URI: http://www.federicojacobi.com
    99*/
    1010
    11 $supported_browsers = array (
    12     '|MSIE ([0-9]?.[0-9]{1,2})|' => array('ie','Internet Explorer'),
    13     '|Firefox/([0-9\.]+)|' => array('firefox','FireFox'),
    14     '/Opera|OPR\/(.[0-9]){1,5}/' => array('opera','Opera'),
    15 
    16     '|Chrome/[0-9]{1,3}(.[0-9]{1,3}){1,5}|' => array('chrome','Chrome'),
    17 
    18     '/\(iPhone|iPod/' => array('iphone','iPhone, iPod'),
    19     '/\(iPad/' => array('ipad','iPad'),
    20     '|Safari|' => array('safari','Safari'),
    21    
    22     '/BlackBerry8310/' => array('blackberry8310','BlackBerry 8310'),
    23     '/BlackBerry/' => array('blackberry','All BlackBerry'),
    24     '/PLAYSTATION 3|PS3/' => array('ps3','Playstation 3')
    25 );
    26 $lzt_version = '0.4';
    27 $useragent = $_SERVER['HTTP_USER_AGENT'];
    28 $browser_version = 0;
    29 $lzt_browser= 'default';
    30 
    31 foreach ($supported_browsers as $browser_regex => $browser_name) {
    32     if (preg_match($browser_regex,$useragent,$matched)) {
    33         $browser_version=$matched[1];
    34         $lzt_browser = $browser_name[0];
    35         break;
    36     }
     11class Theme2Browser {
     12
     13    var $supported_browsers = array (
     14        '|MSIE ([0-9]?.[0-9]{1,2})|' => array('ie','Internet Explorer'),
     15        '|Firefox/([0-9\.]+)|' => array('firefox','FireFox'),
     16        '/Opera|OPR\/(.[0-9]){1,5}/' => array('opera','Opera'),
     17
     18        '|Chrome/[0-9]{1,3}(.[0-9]{1,3}){1,5}|' => array('chrome','Chrome'),
     19
     20        '/\(iPhone|iPod/' => array('iphone','iPhone, iPod'),
     21        '/\(iPad/' => array('ipad','iPad'),
     22        '|Safari|' => array('safari','Safari'),
     23       
     24        '/BlackBerry8310/' => array('blackberry8310','BlackBerry 8310'),
     25        '/BlackBerry/' => array('blackberry','All BlackBerry'),
     26        '/PLAYSTATION 3|PS3/' => array('ps3','Playstation 3')
     27    );
     28   
     29    var $user_agent = '';
     30    var $debug = 0;
     31    var $theme = '';
     32    var $theme_parent = '';
     33
     34    function __construct() {
     35        add_action( 'plugins_loaded', array( $this, 'init' ) );
     36    }
     37   
     38    function init() {
     39        $options = get_option( 't2b_options' );
     40        $this->debug = intval( $options[ 'debug' ] );
     41       
     42        register_activation_hook( __FILE__, array( $this, 'onActivation' ) );
     43        register_deactivation_hook( __FILE__, array( $this, 'onDeactivation' ) );
     44        add_action( 'admin_menu', array( $this, 'addMenu' ) );
     45        add_action( 'admin_init', array( $this, 'initSettings' ) );
     46        $this->T2B();
     47    }
     48   
     49    function T2B() {
     50        $this->user_agent = $_SERVER['HTTP_USER_AGENT'];
     51        if ( ! is_admin() ) {
     52            $option = get_option( 't2b_options' );
     53           
     54            foreach ( $this->supported_browsers as $browser_regex => $browser_name ) {
     55                if ( preg_match( $browser_regex, $this->user_agent, $matched ) ) {
     56                    $this->theme = $option[ $browser_name[0] ];
     57                    break;
     58                }
     59            }
     60           
     61            if ( $this->theme && $this->theme != "t2b_default" ) {
     62               
     63                    $theme = wp_get_theme( $this->theme );
     64               
     65                    if ( $theme->exists() ) {
     66                        $this->theme_parent = $this->theme;
     67                        add_filter( 'template', array( $this, 'setTheme' ) );
     68                        add_filter( 'stylesheet', array( $this, 'setCSS' ) );
     69                    }
     70            }
     71            add_action( 'wp_footer', array( $this, 'debugInfo' ) );
     72        }
     73    }
     74   
     75    function addMenu() {
     76        add_theme_page(
     77            'T2B Control',
     78            "T2B Control",
     79            'manage_options',
     80            't2b_control',
     81            array( $this, 'doOptionsPage' )
     82        );
     83    }
     84   
     85    function initSettings() {
     86        register_setting( 'browsers', 't2b_options' );
     87        register_setting( 'debug', 't2b_options' );
     88       
     89        add_settings_section( 'browsers', 'Available browsers', array( $this, 'browser_section_text') , 't2b_control' );
     90        add_settings_section( 'debug', 'Debug Mode', null , 't2b_control' );
     91       
     92        foreach ( $this->supported_browsers as $browser_regex => $browser_name ) {
     93            add_settings_field( 't2b_' . $browser_name[0], $browser_name[1], array( $this, 'render_browser_option' ), 't2b_control', 'browsers', array( 'browser_info' => $browser_name, 'regex' => $browser_regex ) );
     94        }
     95       
     96        add_settings_field( 't2b_debug', 'Debug Mode', array( $this, 'render_debug_option' ), 't2b_control', 'debug' );
     97    }
     98   
     99    function render_browser_option( $args ) {
     100        $options = get_option( 't2b_options' );
     101       
     102        $browser_slug = $args[ 'browser_info' ][ 0 ];
     103        $browser_name = $args[ 'browser_info' ][ 1 ];
     104       
     105        $selected_theme = $options[ $browser_slug ];
     106
     107        $themes = wp_get_themes();
     108    ?>
     109        <select name="<?php echo "t2b_options[{$browser_slug}]" ?>" id="<?php echo "t2b_options[{$browser_slug}]" ?>" >
     110            <option value="0" <?php $selected_theme ? '' : selected( true ) ?> />Default theme</option>
     111            <?php
     112            foreach( $themes as $slug => $theme ) {
     113                ?>
     114                <option value="<?php echo esc_attr( $slug ) ?>" <?php selected( $slug, $selected_theme ) ?> />
     115                    <?php echo esc_html( $theme->name ); ?>
     116                </option>
     117                <?php
     118            }
     119        ?>
     120        </select>
     121        <?php
     122        if ( $options[ 'debug' ] ) {
     123            echo " <code>". esc_html( $args[ 'regex' ] ) . "</code>";
     124            if ( preg_match( $args[ 'regex' ], $this->user_agent ) ) {
     125                echo " <strong><- Matches your current browser</strong>";
     126            }
     127        }
     128    }
     129   
     130    function render_debug_option() {
     131        $options = get_option( 't2b_options' );
     132        $debug = $options[ 'debug' ];
     133        ?>
     134        <select name="t2b_options[debug]" id="t2b_options[debug]">
     135            <option value="0" <?php selected( $debug, 0 ) ?> >OFF</option>
     136            <option value="1" <?php selected( $debug, 1 ) ?> >ON</option>
     137        </select>
     138        <?php
     139    }
     140   
     141    function browser_section_text() {
     142    ?>
     143        <p>Select the desired theme to show for the respective browser. </p>
     144    <?php
     145    }
     146
     147    function doOptionsPage() {
     148        ?>
     149        <div class="wrap">
     150            <?php                   
     151                $plugin_data = get_plugin_data( __FILE__ );
     152            ?>
     153            <h2>Theme to Browser Control a.k.a. T2B (v.<?php echo esc_html( $plugin_data[ 'Version' ] ) ?>)</h2>
     154           
     155            <form action="options.php" method="post">
     156               
     157                <?php settings_fields( 'browsers' ); ?>
     158                <?php do_settings_sections('t2b_control' ); ?>
     159               
     160                <?php submit_button(); ?>
     161               
     162            </form>
     163            <hr />
     164            <h2>Extras</h2>
     165            <p>Your browser's user-agent string: <code><?php echo $this->user_agent ?></code></p>
     166            <p>More user agent strings: <a href="http://www.useragentstring.com/pages/useragentstring.php" target="_blank">http://www.useragentstring.com</a></p>
     167           
     168            <h3>How to add your browser/variation/version</h3>
     169            <p>First you need some really basic knowledge of regular expressions and php.</p>
     170            <p>Because you are loading a completely different theme using filters is not an option, so you have to dive
     171            into the plugin's code to do it, however, it is easy enough.
     172            </p>
     173            <p>
     174                Find the <code>$supported_browsers</code> array and add the proper regular expression to evaluate against the user agent string.
     175                For example, if you want to add Wii to your browser list add the following:
     176            </p>
     177<pre>
     178var $supported_browsers = array (
     179        '|MSIE ([0-9]?.[0-9]{1,2})|' => array('ie','Internet Explorer'),
     180        '|Firefox/([0-9\.]+)|' => array('firefox','FireFox'),
     181        '/Opera|OPR\/(.[0-9]){1,5}/' => array('opera','Opera'),
     182
     183        '|Chrome/[0-9]{1,3}(.[0-9]{1,3}){1,5}|' => array('chrome','Chrome'),
     184
     185        '/\(iPhone|iPod/' => array('iphone','iPhone, iPod'),
     186        '/\(iPad/' => array('ipad','iPad'),
     187        '|Safari|' => array('safari','Safari'),
     188       
     189        '/BlackBerry8310/' => array('blackberry8310','BlackBerry 8310'),
     190        '/BlackBerry/' => array('blackberry','All BlackBerry'),
     191        '/PLAYSTATION 3|PS3/' => array('ps3','Playstation 3'),
     192        '/Nintendo Wii/' => array('wii','Nintendo Wii')         // <------ THIS LINE ADDED
     193    );
     194</pre>
     195
     196            <p>Notice, if you want to add, let's say a version of IE you need to added BEFORE the current line, as they are evaluated
     197            in order. The first line in this example will catch ALL IE matches, version variations must come before that !
     198            If your line is too broad it'll catch too much, if it is to specific you might miss some cases.
     199            </p>
     200           
     201            <p>Use the debug option to get some theme information in your wp_footer. That way you'll know you are
     202            loading the proper theme depending on your setup.</p>
     203            <p>Supported browsers:
     204                <?php
     205                $browsers = array();
     206                foreach ( $this->supported_browsers as $browser_regex => $browser_info ) {
     207                    $browsers[] = $browser_info[1];
     208                }
     209                echo esc_html( implode( ', ', $browsers ) );
     210                ?>
     211            </p>
     212        </div>     
     213        <?
     214    }
     215
     216    function debugInfo() {
     217        if ( ! $this->debug )
     218            return;
     219    ?>
     220        <div>
     221            ThemeParent: <?php echo $this->theme_parent ?><br>
     222            ThemeCss: <?php echo $this->theme ?><br>
     223            User Agent: <?php echo $this->user_agent ?><br>
     224        </div>
     225    <?php
     226    }
     227
     228    function setTheme() {
     229        return $this->theme_parent;
     230    }
     231   
     232    function setCSS() {
     233        return $this->theme;
     234    }
     235   
     236    function onActivation() {
     237    }
     238   
     239    function onDeactivation() {
     240    }
     241   
    37242}
    38 
    39 $lzt_theme = get_option('lzt_'.$lzt_browser);
    40 if (! is_admin() ) {
    41     if ($lzt_theme != "t2b_default") {
    42         // Check for a valid theme directory, and set the template and stylesheet directory
    43         if($lzt_theme && file_exists(get_theme_root() . "/$lzt_theme")) {
    44        
    45             $theme_data = get_theme_data(get_theme_root()."/$lzt_theme/style.css");
    46             if ($theme_data['Template'] == '') {
    47                 $theme_parent = $lzt_theme;
    48             } else {
    49                 $theme_parent = $theme_data['Template'];
    50             }
    51             add_filter('template','lzt_set_theme');
    52             add_filter('stylesheet','lzt_set_css');
    53         }
    54     }
    55     //add_action('wp_footer','echoresult'); // Triggers debug at the footer
    56 }
    57 // Debug function ... just to make sure the right stuff is loaded
    58 function echoresult() {
    59     global $theme_parent, $lzt_theme;
    60     global $theme_data;
    61     global $useragent;
    62     echo "<div>";
    63     get_template();
    64     echo '|';
    65     echo "ThemeParent:".$theme_parent."<br>";
    66     echo "ThemeCss:".$lzt_theme."<br>";
    67     echo "Useragent:". $useragent."<br>";
    68     print_r($theme_data);
    69     echo "</div>";
    70 }
    71 
    72 
    73 function lzt_set_theme() {
    74     global $theme_parent;
    75     return $theme_parent;
    76 }
    77 function lzt_set_css() {
    78     global $lzt_theme;
    79     return $lzt_theme;
    80 }
    81 
    82 function admin_lazy_theme_options() {
    83     global $supported_browsers, $lzt_version;
    84     ?>
    85     <div class="wrap">
    86     <div id="icon-options-general" class="icon32"></div> <h2>Theme to Browser Control a.k.a. T2B (v.<?php echo $lzt_version ?>)</h2>
    87     <?php
    88     if ($_REQUEST['submit']) {
    89         update_lzt_options();
    90     }
    91     ?>
    92     <p>Select the desired theme to show for the respective browser. </p>
    93     <p>Supported browsers: <?php
    94    
    95     foreach ($supported_browsers as $browser_regex => $browser_name) {
    96         echo $browser_name[1].', ';
    97     }
    98     ?></p>
    99     <?php
    100     print_lzt_form();
    101     ?>
    102     </div>
    103     <?
    104 }
    105 
    106 function update_lzt_options() {
    107     global $supported_browsers;
    108     foreach ($supported_browsers as $browser_regex => $browser_name) {
    109         if ($_REQUEST[$browser_name[0]])
    110             update_option('lzt_'.$browser_name[0],$_REQUEST[$browser_name[0]]);
    111     }
    112     echo '<div id="message" class="updated fade">
    113     <p>Options have been saved. Please test!! if the plugin is not detecting your browser, please let me know at [email protected]</p>
    114     </div>';
    115 }
    116 
    117 function print_lzt_form() {
    118     global $supported_browsers;
    119     $first = false;
    120     ?>
    121         <form action="" method="post">
    122                 <?php
    123                     foreach ($supported_browsers as $browser_regex => $browser_name) {
    124                 ?>
    125             <div style="width:200px; float:left; display: inline-block; margin: 5px; background-color:#F1F1F1; border:1px solid #E3E3E3; padding:5px;">
    126                 <h3><?php echo $browser_name[1] ?></h4>
    127                 <div class="inside"><p><ul>
    128                 <?php
    129                     $themes = get_themes();
    130                     if (get_option('lzt_'.$browser_name[0]) == '')
    131                         $first = true;
    132                     ?>
    133                     <li><input type="radio" name="<?php echo $browser_name[0]?>" value="t2b_default" <?php
    134                     // IF este es el seleccionado THEN echo SELECTED
    135                     if ((get_option('lzt_'.$browser_name[0]) == "t2b_default") || ($first)) {
    136                         echo 'checked="checked"';
    137                         $first = false;
    138                     }
    139                     ?> /> Default theme</li>
    140                     <?php
    141                     foreach($themes as $theme) {
    142                 ?>
    143                 <li><input type="radio" name="<?php echo $browser_name[0]?>" value="<?php echo $theme['Stylesheet']; ?>" <?php
    144                     // IF este es el seleccionado THEN echo SELECTED
    145                     if ((get_option('lzt_'.$browser_name[0]) == $theme['Stylesheet']) || ($first)) {
    146                         echo 'checked="checked"';
    147                         $first = false;
    148                     }
    149                     ?> /> <?php echo htmlentities($theme['Title']); ?></li>
    150                 <?php
    151                     }
    152                
    153                 ?></ul></p>
    154                 </div>
    155             </div>
    156                 <?php
    157                     }
    158                 ?>
    159             <div style="clear:both;"></div>
    160             <input type="submit" name="submit" value="<?php _e("Save Settings"); ?>" class='button-primary' />
    161         </form>
    162    
    163     <?php
    164 }
    165 
    166 function lzt_modify_menu() {
    167     add_theme_page(
    168         'T2B Control',
    169         "T2B Control",
    170         'manage_options',
    171         __FILE__,
    172         'admin_lazy_theme_options'
    173     );
    174 }
    175 
    176 function set_lazy_theme_options() {
    177     global $supported_browsers;
    178     foreach ($supported_browsers as $browser_regex => $browser_name) {
    179         add_option('lzt_'.$browser_name[0],'');
    180     }
    181 }
    182 
    183 function unset_lazy_theme_options() {
    184     global $supported_browsers;
    185     foreach ($supported_browsers as $browser_regex => $browser_name) {
    186         delete_option('lzt_'.$browser_name[0]);
    187     }
    188 }
    189 
    190 
    191 add_action('admin_menu','lzt_modify_menu');
    192 
    193 register_activation_hook(__FILE__,'set_lazy_theme_options');
    194 register_deactivation_hook(__FILE__,'unset_lazy_theme_options');
    195 ?>
     243new Theme2Browser();
Note: See TracChangeset for help on using the changeset viewer.