Swissgeek
Swissgeek

Reputation: 23

Replacement of array values

Newbie here. I'm trying to create a WordPress plugin, and I get the following code to display a dropdown for font selection. It works but looks just wrong and not efficient, what is the way to change the array's value for display without writing those if statements?

$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'; }
}
}

Upvotes: 2

Views: 104

Answers (2)

HamZa
HamZa

Reputation: 14921

You may try something like this:

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

Upvotes: 1

Blender
Blender

Reputation: 298176

I'd just make an associative array instead:

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

Although since most of those font names are just lowercase and without punctuation, you could also make a generic function and then just account for the special cases.

Either way, this code is too complicated. Why do you need to do this?

Upvotes: 3

Related Questions