我需要帮助使这种逻辑更有效率

时间:2022-07-29 21:26:23

EDIT - I have completely re-written the logic below but I'm sure it can still be improved.

编辑 - 我完全重写了下面的逻辑,但我确信它仍然可以改进。

EDIT - The current working code for the alternate theme selector is at GitHub. I'd still like to improve on it but it's a lot cleaner than what I started with. At this point, I'll take the issue thread there since user testing will reveal the next steps.

编辑 - 备用主题选择器的当前工作代码位于GitHub。我仍然希望改进它,但它比我开始时更清洁。此时,我将把问题线程放在那里,因为用户测试将揭示后续步骤。

New Logic:

<?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');
    }
}

I've been staring at this for too long... it's completely inefficient. I'm literally running the entire switch statement once, then checking if active is flagged, then I'm running it again. That's so very wrong. I desperately need a bit of perspective on the right way to structure the logic.

我一直盯着这个......这完全没有效率。我实际上运行整个switch语句一次,然后检查active是否被标记,然后我再次运行它。那是非常错的。我迫切需要一些关于构造逻辑的正确方法的观点。

Background Information: It's switching the theme based on a user agent as configured in a WP Plugin admin. The theme has a dev mode for configuring the plugin and an active mode for the user agents.

背景信息:它是根据WP插件管理员中配置的用户代理切换主题。主题具有用于配置插件的开发模式和用于用户代理的活动模式。

$ats_plugin grabs the Redux array

$ ats_plugin抓取Redux数组

$the_theme grabs the selected theme from that array

$ the_theme从该数组中获取所选主题

$active grabs the switch for whether or not a theme is being served to that user agent.

$ active抓取切换是否正在向该用户代理提供主题。

The whole mess is being filtered back into WordPress on the end.

整个混乱被最终过滤回WordPress。

Here's my mess of code. It works but it's horribly inefficient.

这是我乱七八糟的代码。它有效,但效率非常低。

Original Madhouse Logic:

<?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');
}

I'm putting my super humble student goggles on right now.

我现在正把我超级谦逊的学生护目镜放在上面。

Screenshots of what I'm doing

我需要帮助使这种逻辑更有效率我需要帮助使这种逻辑更有效率

2 个解决方案

#1


0  

OK, first of all, do not run swapTheme() outside of any filter. Second, just check if you already matched something and than simple return.

好的,首先,不要在任何过滤器之外运行swapTheme()。其次,只需检查您是否已经匹配某些东西而不是简单的回报。

function swapTheme() {
    if ( ! empty( $the_theme ) ) {
        return;
    }
    // ...
}

#2


0  

While it's not exactly the answer I was hoping for, further iteration and a bit of banter with the guys at Redux Framework got things into a working state. The Alternate Theme Switcher code is now at Github if you'd like to see how I resolved the myriad issues that came up. I'd still like to further refactor things going forward but the question of "how to fix broken?" no longer needs to be active here.

虽然这不是我希望的答案,但是Redux Framework的人员进一步迭代和一些戏弄让事情变成了一个工作状态。 Alternate Theme Switcher代码现在在Github,如果你想看看我如何解决出现的无数问题。我还是想进一步重构前进的事情,但问题是“如何解决问题?”不再需要在这里活跃。

Thanks for the input that was provided.

感谢您提供的输入。

#1


0  

OK, first of all, do not run swapTheme() outside of any filter. Second, just check if you already matched something and than simple return.

好的,首先,不要在任何过滤器之外运行swapTheme()。其次,只需检查您是否已经匹配某些东西而不是简单的回报。

function swapTheme() {
    if ( ! empty( $the_theme ) ) {
        return;
    }
    // ...
}

#2


0  

While it's not exactly the answer I was hoping for, further iteration and a bit of banter with the guys at Redux Framework got things into a working state. The Alternate Theme Switcher code is now at Github if you'd like to see how I resolved the myriad issues that came up. I'd still like to further refactor things going forward but the question of "how to fix broken?" no longer needs to be active here.

虽然这不是我希望的答案,但是Redux Framework的人员进一步迭代和一些戏弄让事情变成了一个工作状态。 Alternate Theme Switcher代码现在在Github,如果你想看看我如何解决出现的无数问题。我还是想进一步重构前进的事情,但问题是“如何解决问题?”不再需要在这里活跃。

Thanks for the input that was provided.

感谢您提供的输入。