2

新手来了 我正在尝试创建一个 WordPress 插件,我得到以下代码来显示字体选择的下拉菜单。它可以工作,但看起来只是错误且效率不高,在不编写那些 if 语句的情况下更改数组的值以进行显示的方法是什么?

$plugin_options = get_option('plugin_options');
$plugin_fonts_array = array('Chunk', 'Crimson', 'Deja Vu Sans', 'Deja Vu Sans Condensed', 'Deja Vu Mono', 'Deja Vu Serif', 'Deja Vu Serif Condensed', 'IM Fell French Canon', 'Junction', 'Lato Light', 'Lato Regular', 'Lato Black', 'League Gothic Condensed', 'League Gothic Regular', 'League Script Number One', 'Linux Libertine', 'Linux Libertine Display', 'Linux Libertine Mono', 'OCRB', 'Open Baskerville', 'Open Sans Light', 'Open Sans', 'Open Sans Semi Bold', 'Open Sans Extra Bold', 'Prociono', 'Raleway Thin', 'Source Sans Pro Extra Light', 'Source Sans Pro Light', 'Source Sans Pro', 'Source Sans Pro Semi Bold', 'Source Sans Pro Black');
foreach ($plugin_fonts_array as $font) {
    if ($font == $plugin_options['dropdown1']) {
        $plugin_front_font = $plugin_options['dropdown1'];      
    if ($plugin_front_font == 'Chunk') { $plugin_front_font = 'chunk'; }
    if ($plugin_front_font == 'Crimson') { $plugin_front_font = 'crimson'; }
    if ($plugin_front_font == 'Deja Vu Sans') { $plugin_front_font = 'DejaVuSans'; }
    if ($plugin_front_font == 'Deja Vu Sans Condensed') { $plugin_front_font = 'DejaVuSansCondensed'; }
    if ($plugin_front_font == 'Deja Vu Mono') { $plugin_front_font = 'DejaVuSansMono'; }
    if ($plugin_front_font == 'Deja Vu Serif') { $plugin_front_font = 'DejaVuSerif'; }
    if ($plugin_front_font == 'Deja Vu Serif Condensed') { $plugin_front_font = 'DejaVuSerifCondensed'; }
    if ($plugin_front_font == 'IM Fell French Canon') { $plugin_front_font = 'imfellfrenchcanon'; }
    if ($plugin_front_font == 'Junction') { $plugin_front_font = 'junction'; }
    if ($plugin_front_font == 'Lato Light') { $plugin_front_font = 'latolight'; }
    if ($plugin_front_font == 'Lato Regular') { $plugin_front_font = 'latoregular'; }
    if ($plugin_front_font == 'Lato Black') { $plugin_front_font = 'latoblack'; }
    if ($plugin_front_font == 'League Gothic Condensed') { $plugin_front_font = 'leaguegothiccondensedregular'; }
    if ($plugin_front_font == 'League Gothic Regular') { $plugin_front_font = 'leaguegothicregular'; }
    if ($plugin_front_font == 'League Script Number One') { $plugin_front_font = 'leaguescriptnumberone'; }
    if ($plugin_front_font == 'Linux Libertine') { $plugin_front_font = 'linuxlibertine'; }
    if ($plugin_front_font == 'Linux Libertine Display') { $plugin_front_font = 'linuxlibertinedisplay'; }
    if ($plugin_front_font == 'Linux Libertine Mono') { $plugin_front_font = 'linuxlibertinemono'; }
    if ($plugin_front_font == 'OCRB') { $plugin_front_font = 'ocrb10'; }
    if ($plugin_front_font == 'Open Baskerville') { $plugin_front_font = 'openbaskerville'; }
    if ($plugin_front_font == 'Open Sans Light') { $plugin_front_font = 'opensanslight'; }
    if ($plugin_front_font == 'Open Sans') { $plugin_front_font = 'opensans'; }
    if ($plugin_front_font == 'Open Sans Semi Bold') { $plugin_front_font = 'opensanssemibold'; }
    if ($plugin_front_font == 'Open Sans Extra Bold') { $plugin_front_font = 'opensansextrabold'; }
    if ($plugin_front_font == 'Prociono') { $plugin_front_font = 'prociono'; }
    if ($plugin_front_font == 'Raleway Thin') { $plugin_front_font = 'ralewaythin'; }
    if ($plugin_front_font == 'Source Sans Pro Extra Light') { $plugin_front_font = 'sourcesansproextralight'; }
    if ($plugin_front_font == 'Source Sans Pro Light') { $plugin_front_font = 'sourcesansprolight'; }
    if ($plugin_front_font == 'Source Sans Pro') { $plugin_front_font = 'sourcesanspro'; }
    if ($plugin_front_font == 'Source Sans Pro Semi Bold') { $plugin_front_font = 'sourcesansprosemibold'; }
    if ($plugin_front_font == 'Source Sans Pro Black') { $plugin_front_font = 'sourcesansproblack'; }
}
}
4

2 回答 2

3

我只是制作一个关联数组:

$plugin_options = get_option('plugin_options');

$font_map = array(
    'Chunk' => 'chunk',
    'Crimson' => 'crimson',
    'Deja Vu Sans' => 'DejaVuSans',
    'Deja Vu Sans Condensed' => 'DejaVuSansCondensed',
    'Deja Vu Mono' => 'DejaVuSansMono',
    'Deja Vu Serif' => 'DejaVuSerif',
    'Deja Vu Serif Condensed' => 'DejaVuSerifCondensed',
    'IM Fell French Canon' => 'imfellfrenchcanon',
    'Junction' => 'junction',
    'Lato Light' => 'latolight',
    'Lato Regular' => 'latoregular',
    'Lato Black' => 'latoblack',
    'League Gothic Condensed' => 'leaguegothiccondensedregular',
    'League Gothic Regular' => 'leaguegothicregular',
    'League Script Number One' => 'leaguescriptnumberone',
    'Linux Libertine' => 'linuxlibertine',
    'Linux Libertine Display' => 'linuxlibertinedisplay',
    'Linux Libertine Mono' => 'linuxlibertinemono',
    'OCRB' => 'ocrb10',
    'Open Baskerville' => 'openbaskerville',
    'Open Sans Light' => 'opensanslight',
    'Open Sans' => 'opensans',
    'Open Sans Semi Bold' => 'opensanssemibold',
    'Open Sans Extra Bold' => 'opensansextrabold',
    'Prociono' => 'prociono',
    'Raleway Thin' => 'ralewaythin',
    'Source Sans Pro Extra Light' => 'sourcesansproextralight',
    'Source Sans Pro Light' => 'sourcesansprolight',
    'Source Sans Pro' => 'sourcesanspro',
    'Source Sans Pro Semi Bold' => 'sourcesansprosemibold',
    'Source Sans Pro Black' => 'sourcesansproblack'
);

$dropdown_font = $plugin_options['dropdown1'];

if (array_key_exists($dropdown_font, $font_map)) {
    $plugin_front_font = $font_map[$dropdown_font];
} else {
    echo "Invalid font";
}

尽管由于大多数字体名称都是小写且没有标点符号,但您也可以创建一个通用函数,然后只考虑特殊情况。

不管怎样,这段代码太复杂了。为什么需要这样做?

于 2013-03-25T02:02:02.913 回答
1

你可以尝试这样的事情:

$plugin_options = get_option('plugin_options');
$plugin_fonts_array = array('Chunk', 'Crimson', 'Deja Vu Sans', 'Deja Vu Sans Condensed', 'Deja Vu Mono', 'Deja Vu Serif', 'Deja Vu Serif Condensed', 'IM Fell French Canon', 'Junction', 'Lato Light', 'Lato Regular', 'Lato Black', 'League Gothic Condensed', 'League Gothic Regular', 'League Script Number One', 'Linux Libertine', 'Linux Libertine Display', 'Linux Libertine Mono', 'OCRB', 'Open Baskerville', 'Open Sans Light', 'Open Sans', 'Open Sans Semi Bold', 'Open Sans Extra Bold', 'Prociono', 'Raleway Thin', 'Source Sans Pro Extra Light', 'Source Sans Pro Light', 'Source Sans Pro', 'Source Sans Pro Semi Bold', 'Source Sans Pro Black');
$plugin_fonts_flipped_array = array_flip($plugin_fonts_array);
foreach ($plugin_fonts_array as $font) {
    if ($font == $plugin_options['dropdown1']) {
    $plugin_front_font = $plugin_options['dropdown1'];
        if(isset($plugin_fonts_flipped_array[$plugin_front_font])){
            $plugin_front_font = strtolower(str_replace(' ', '', $plugin_front_font)); // remove the whitespace and convert to lower space
        }
    }
}
于 2013-03-25T02:04:55.867 回答