2013-03-25 97 views
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'; } 
} 
} 

回答

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"; 
} 

虽然因为大多数的字体名都只是小写和不加标点,你也可以做一个通用的功能,然后就占特殊情况。

无论哪种方式,这段代码太复杂了。你为什么需要这样做?

+0

+1使用'array_key_exists()',而不是一个循环! – HamZa 2013-03-25 02:22:19

+0

它已经好多了(和作品),谢谢。 将博客导出为pdf时,将字体设置为封面,标题和正文文本。 – Swissgeek 2013-03-25 03:33:34

+0

@Swissgeek不要忘记检查它作为答案:) – HamZa 2013-03-25 08:56:30

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 
     } 
    } 
}