编辑 - The current working code for the alternate theme selector is at GitHub。我仍然希望改进它,但它比我开始时更清洁。此时,我将把问题线程放在那里,因为用户测试将揭示后续步骤。
<?php
// Fetch theme options array
global $ats_plugin;
global $is_IE;
// Initialize the main variables
$ats_active = 0;
$the_theme = null;
// We only want to run this fat chunk of logic on IE since it's IE with the issues
if ( $is_IE ) {
// Is a supported useragent active?
function checkActive($data) {
global $ats_active;
$user_agent = $_SERVER['HTTP_USER_AGENT'];
switch($user_agent) {
case strpos($user_agent, 'MSIE 5') :
if ($data['ie5_switch'] == 1 && !empty($data['ie5_theme'])) {
$ats_active = 'ie5';
};
break;
case strpos($user_agent, 'MSIE 6') :
if ($data['ie6_switch'] == 1 && !empty($data['ie6_theme'])) {
$ats_active = 'ie6';
};
break;
case strpos($user_agent, 'MSIE 7') :
if ($data['ie7_switch'] == 1 && !empty($data['ie7_theme'])) {
$ats_active = 'ie7';
};
break;
case strpos($user_agent, 'MSIE 8') :
if ($data['ie8_switch'] == 1 && !empty($data['ie8_theme'])) {
$ats_active = 'ie8';
};
break;
default :
$ats_active = 0;
}
// Dev Mode Overide
if($data['dev_mode'] == 1) {
$ats_active = 1;
}
return $ats_active;
}
// Run active agents check
checkActive($ats_plugin);
if(!empty($ats_active)) {
function change_theme() {
global $ats_plugin;
global $ats_active;
global $the_theme;
if(!empty($ats_plugin['ie5_theme'])) {$ie5 = $ats_plugin['ie5_theme'];} else {$ie5 = null;}
if(!empty($ats_plugin['ie6_theme'])) {$ie6 = $ats_plugin['ie6_theme'];} else {$ie6 = null;}
if(!empty($ats_plugin['ie7_theme'])) {$ie7 = $ats_plugin['ie7_theme'];} else {$ie7 = null;}
if(!empty($ats_plugin['ie8_theme'])) {$ie8 = $ats_plugin['ie8_theme'];} else {$ie8 = null;}
$theme_key = array(
'ie5' => $ie5,
'ie6' => $ie6,
'ie7' => $ie7,
'ie8' => $ie8,
);
// Only one value should return
foreach ($theme_key as $browser => $selection ) {
if ($ats_active == $browser) {
$the_theme = $selection;
}
}
// Add the dev mode override
if(!empty($ats_plugin['dev_theme'])) {
$the_theme = $ats_plugin['dev_theme'];
}
return $the_theme;
}
add_filter('template', 'change_theme');
add_filter('option_template', 'change_theme');
add_filter('option_stylesheet', 'change_theme');
}
}
// For non-IE browsers, we check if the user is an admin and enable developer mode
if ( !$is_IE && current_user_can('manage_options') ) {
if($ats_plugin['dev_mode'] == 1 && !empty($ats_plugin['dev_theme'])) {
$the_theme = $ats_plugin['dev_theme'];
}
function dev_theme() {
global $the_theme;
return $the_theme;
}
/* @todo if the theme is in developer mode, there should be a visual warning as a reminder */
if ($ats_plugin['dev_mode'] == 1) {
add_filter('template', 'dev_theme');
add_filter('option_template', 'dev_theme');
add_filter('option_stylesheet', 'dev_theme');
}
}
我一直盯着这个......这完全是效率低下的。我实际上运行整个switch语句一次,然后检查active是否被标记,然后我再次运行它。那是非常错误的。我迫切需要一些关于构建逻辑的正确方法的观点。
背景资料: 它正在根据WP插件管理员中配置的用户代理切换主题。该主题具有用于配置插件的开发模式和用户代理的活动模式。
$ ats_plugin抓取Redux数组
$ the_theme从该数组中获取所选主题
$ active抓住交换机是否正在向该用户代理提供主题。
最后,整个混乱将被过滤回WordPress。
这是我乱七八糟的代码。它有效,但效率非常低。
<?php
/**
* Define some early logic, which should probably be moved to a class later
*/
// Initialize the main variables
$active = 0;
$the_theme = null;
function swapTheme()
{
// Fetch the plugin options array
global $ats_plugin;
global $the_theme;
global $active;
// Fetch the user agent
$user_agent = $_SERVER['HTTP_USER_AGENT'];
// Assign variables based on user agent
switch ($user_agent) {
case strpos($user_agent, 'MSIE 5') :
$active = $ats_plugin['ie5_switch'];
$the_theme = $ats_plugin['ie5_theme'];
break;
case strpos($user_agent, 'MSIE 6') :
$active = $ats_plugin['ie6_switch'];
$the_theme = $ats_plugin['ie6_theme'];
break;
case strpos($user_agent, 'MSIE 7') :
$active = $ats_plugin['ie7_switch'];
$the_theme = $ats_plugin['ie7_theme'];
break;
case strpos($user_agent, 'MSIE 8') :
$active = $ats_plugin['ie8_switch'];
$the_theme = $ats_plugin['ie8_theme'];
break;
default :
// Check for Developer Mode
if ($ats_plugin['dev_mode'] == 1) {
$active = 1;
// Check for Developer Theme
if (!empty($ats_plugin['dev_theme']) && current_user_can('manage_options')) {
$the_theme = $ats_plugin['dev_theme'];
} else {
$the_theme = null;
}
}
break;
}
return $the_theme;
}
swapTheme();
if (!empty($the_theme) && $active == 1) {
add_filter('template', 'swapTheme');
add_filter('option_template', 'swapTheme');
add_filter('option_stylesheet', 'swapTheme');
}
我现在正把我超级谦逊的学生护目镜放在上面。
答案 0 :(得分:0)
好的,首先,不要在任何过滤器之外运行swapTheme()
。其次,只需检查您是否已经匹配了某些东西,而不是简单的回报。
function swapTheme() {
if ( ! empty( $the_theme ) ) {
return;
}
// ...
}
答案 1 :(得分:0)
虽然这并不是我所希望的答案,但进一步的迭代和与Redux Framework的人们的一些戏弄让事情进入了一个工作状态。 The Alternate Theme Switcher code is now at Github如果你想看看我如何解决出现的无数问题。我还想进一步重构前进的事情,但问题是“如何解决问题?”不再需要在这里活跃。
感谢您提供的输入。