Advertisement
Imperative-Ideas

Conditional Logic for ATS Plugin

Feb 5th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.70 KB | None | 0 0
  1. <?php
  2. // Fetch theme options array
  3. global $ats_plugin;
  4. global $is_IE;
  5.  
  6. // Initialize the main variables
  7. $ats_active = null;
  8. $the_theme = null;
  9.  
  10. // We only want to run this fat chunk of logic on IE since it's IE with the issues
  11. if ( $is_IE ) {
  12.  
  13. // Is a supported useragent active?
  14. function checkActive($data) {
  15.     global $ats_active;
  16.     $user_agent = $_SERVER['HTTP_USER_AGENT'];
  17.  
  18.     switch($user_agent) {
  19.         case strpos($user_agent, 'MSIE 5') :
  20.             if ($data['ie5_switch'] == 1 && !empty($data['ie5_theme'])) {
  21.                 $ats_active = 'ie5';
  22.             };
  23.             break;
  24.         case strpos($user_agent, 'MSIE 6') :
  25.             if ($data['ie6_switch'] == 1 && !empty($data['ie6_theme'])) {
  26.                 $ats_active = 'ie6';
  27.             };
  28.             break;
  29.         case strpos($user_agent, 'MSIE 7') :
  30.             if ($data['ie7_switch'] == 1 && !empty($data['ie7_theme'])) {
  31.                 $ats_active = 'ie7';
  32.             };
  33.             break;
  34.         case strpos($user_agent, 'MSIE 8') :
  35.             if ($data['ie8_switch'] == 1 && !empty($data['ie8_theme'])) {
  36.                 $ats_active = 'ie8';
  37.             };
  38.             break;
  39.         case strpos($user_agent, 'MSIE 9') :
  40.             if ($data['ie9_switch'] == 1 && !empty($data['ie9_theme'])) {
  41.                 $ats_active = 'ie9';
  42.             };
  43.             break;
  44.         default :
  45.             $ats_active = null;
  46.     }
  47.  
  48.     // Dev Mode Overide
  49.     if(current_user_can('manage_options') && $data['dev_mode'] == 1) {
  50.         $ats_active = 1;
  51.     }
  52.  
  53.     return $ats_active;
  54. }
  55.  
  56. // Run active agents check
  57. checkActive($ats_plugin);
  58.  
  59. if(!empty($ats_active)) {
  60.  
  61.     function change_theme() {
  62.         global $ats_plugin;
  63.         global $ats_active;
  64.         global $the_theme;
  65.  
  66.         if(!empty($ats_plugin['ie5_theme'])) {$ie5 = $ats_plugin['ie5_theme'];} else {$ie5 = null;}
  67.         if(!empty($ats_plugin['ie6_theme'])) {$ie6 = $ats_plugin['ie6_theme'];} else {$ie6 = null;}
  68.         if(!empty($ats_plugin['ie7_theme'])) {$ie7 = $ats_plugin['ie7_theme'];} else {$ie7 = null;}
  69.         if(!empty($ats_plugin['ie8_theme'])) {$ie8 = $ats_plugin['ie8_theme'];} else {$ie8 = null;}
  70.         if(!empty($ats_plugin['ie9_theme'])) {$ie9 = $ats_plugin['ie9_theme'];} else {$ie9 = null;}
  71.  
  72.         $theme_key = array(
  73.             'ie5' => $ie5,
  74.             'ie6' => $ie6,
  75.             'ie7' => $ie7,
  76.             'ie8' => $ie8,
  77.             'ie9' => $ie9
  78.         );
  79.  
  80.         // Only one value should return
  81.         foreach ($theme_key as $browser => $selection ) {
  82.             if ($ats_active == $browser) {
  83.                 $the_theme = $selection;
  84.             }
  85.         }
  86.  
  87.         // Add the dev mode override
  88.         if( current_user_can('manage_options') && !empty($ats_plugin['dev_theme'])) {
  89.             $the_theme = $ats_plugin['dev_theme'];
  90.         }
  91.  
  92.         return $the_theme;
  93.     }
  94.     add_filter('template', 'change_theme');
  95.     add_filter('option_template', 'change_theme');
  96.     add_filter('option_stylesheet', 'change_theme');
  97. }
  98. }
  99.  
  100. // For non-IE browsers, we check if the user is an admin and enable developer mode
  101. if ( !$is_IE && current_user_can('manage_options') ) {
  102.     if($ats_plugin['dev_mode'] == 1 && !empty($ats_plugin['dev_theme'])) {
  103.         $the_theme = $ats_plugin['dev_theme'];
  104.     }
  105.  
  106.     function dev_theme() {
  107.         global $the_theme;
  108.         return $the_theme;
  109.     }
  110.  
  111.     /* @todo if the theme is in developer mode, there should be a visual warning as a reminder */
  112.  
  113.     if ($ats_plugin['dev_mode'] == 1) {
  114.         add_filter('template', 'dev_theme');
  115.         add_filter('option_template', 'dev_theme');
  116.         add_filter('option_stylesheet', 'dev_theme');
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement