code improvements in oenology_enqueue_social_icon_style() #70

Closed
gregory-hk opened this Issue Apr 28, 2012 · 4 comments

Comments

Projects
None yet
2 participants

hello Chip.

I'm investigating the enqueue-ing of the social network icon css when the option is turned off, and I came across this code. I've done some coding with arrays recently and suspect that you could improve the code here:

foreach ( $socialnetworks as $network ) { 
    $linkposx = '0';
    $linkposy = '0';
    $hoverposx = '0';
    $hoverposy = '0';
    foreach ( $socialiconbgposition as $bg ) {
        if ( $network['name'] == $bg['name'] ) {
            $linkposx = $bg[$linkcolor]['x'];
            $linkposy = $bg[$linkcolor]['y'];
            $hoverposx = $bg[$linkhovercolor]['x'];
            $hoverposy = $bg[$linkhovercolor]['y'];
        }
    }

you could replace the whole foreach ( $socialiconbgposition as $bg ) statement with this:
(assuming that the primary keys of the $socialiconbgposition array items are identical to their 'name' values.)

    $linkposx = $socialiconbgposition[$network['name']][$linkcolor]['x'];
    $linkposy = $socialiconbgposition[$network['name']][$linkcolor]['y'];
    $hoverposx = $socialiconbgposition[$network['name']][$linkhovercolor]['x'];
    $hoverposy = $socialiconbgposition[$network['name']][$linkhovercolor]['y'];

or simply the code a little by assigning $network['name'] to a temp variable:

    $net_name = $network['name'];
    $linkposx = $socialiconbgposition[$net_name][$linkcolor]['x'];
    $linkposy = $socialiconbgposition[$net_name][$linkcolor]['y'];
    $hoverposx = $socialiconbgposition[$net_name][$linkhovercolor]['x'];
    $hoverposy = $socialiconbgposition[$net_name][$linkhovercolor]['y'];
    $profile = $net_name . '_profile';

two more things :-)

I would move the if ( isset( $oenology_options[$profile] ) ) control statement to just after foreach ( $socialnetworks as $network ). there's no need to do all that work in the middle if you're not going to build the css rules for that network.

BUT... the if statement to see if a social setting has been specified is incomplete. if you add another check, it'll work as expected:

if ( isset( $oenology_options[$profile] ) && !empty($oenology_options[$profile]) ) {

and finally, since the new addition to the network check requires $profile, move the $profile assignment statement up too.

the revised code would look like this:

foreach ( $socialnetworks as $network ) { 
    $profile = $network['name'] . '_profile';
    if ( isset( $oenology_options[$profile] ) && !empty($oenology_options[$profile]) ) {
        $linkposx = $linkposy = $hoverposx = $hoverposy = '0'; // compacted! :-)
        $linkposx = $socialiconbgposition[$network['name']][$linkcolor]['x'];
        $linkposy = $socialiconbgposition[$network['name']][$linkcolor]['y'];
        $hoverposx = $socialiconbgposition[$network['name']][$linkhovercolor]['x'];
        $hoverposy = $socialiconbgposition[$network['name']][$linkhovercolor]['y'];
?>
a[class="sidebar-social-icon"][title ^="<?php echo $network['title']; ?>"] {
    background: url('<?php echo get_template_directory_uri(); ?>/images/socialiconsprite.png');
    background-position: <?php echo $linkposx . 'px ' . $linkposy . 'px'; ?>;
}
a[class="sidebar-social-icon"][title ^="<?php echo $network['title']; ?>"]:hover {
    background: url('<?php echo get_template_directory_uri(); ?>/images/socialiconsprite.png');
    background-position: <?php echo $hoverposx . 'px ' . $hoverposy . 'px'; ?>;
}
<?php 
    } 
}
?>

but one more thing :-)

move the `$oenology_options = oenology_get_options();' statement to the very top of the function and add one more statement to get this:

function oenology_enqueue_social_icon_style() { 
    $oenology_options = oenology_get_options();
    if( false == $oenology_options['display_social_icons'] )
        return '';

ouch! I need a break.

cheers,
Gregory

and a typo in the following comment:

// Enqueue Varietal Stylesheet at wp_print_styles()
add_action('wp_print_styles', 'oenology_enqueue_social_icon_style', 11 );

the comment is actually quite popular. it also incorrectly appears in 2 other place.

cheers.

sorry. one more change.

<style type="text/css">
<?php if( 'none' != $oenology_options['rss_feed'] ) { ?>
a[class="sidebar-social-icon"][title ^="RSS"] {

and then:

<?php 
}
foreach ( $socialnetworks as $network ) { 

cheers.

the whole function as it now stands:

function oenology_enqueue_social_icon_style() { 

    $oenology_options = oenology_get_options();
    if( false == $oenology_options['display_social_icons'] )
        return '';

    $socialiconbgposition = array(
        'aim' => array(
            'name' => 'aim',
            'black' => array(
                'x' => '0',
                'y' => '0'
            ),
            'gray' => array(
                'x' => '0',
                'y' => '-90'
            ),
            'silver' => array(
                'x' => '0',
                'y' => '-180'
            )
        ),
        'facebook' => array(
            'name' => 'facebook',
            'black' => array(
                'x' => '0',
                'y' => '-270'
            ),
            'gray' => array(
                'x' => '0',
                'y' => '-360'
            ),
            'silver' => array(
                'x' => '0',
                'y' => '-450'
            )
        ),
        'flickr' => array(
            'name' => 'flickr',
            'black' => array(
                'x' => '0',
                'y' => '-540'
            ),
            'gray' => array(
                'x' => '0',
                'y' => '-630'
            ),
            'silver' => array(
                'x' => '0',
                'y' => '-720'
            )
        ),
        'linkedin' => array(
            'name' => 'linkedin',
            'black' => array(
                'x' => '0',
                'y' => '-810'
            ),
            'gray' => array(
                'x' => '0',
                'y' => '-900'
            ),
            'silver' => array(
                'x' => '0',
                'y' => '-990'
            )
        ),
        'myspace' => array(
            'name' => 'myspace',
            'black' => array(
                'x' => '0',
                'y' => '-1080'
            ),
            'gray' => array(
                'x' => '0',
                'y' => '-1170'
            ),
            'silver' => array(
                'x' => '0',
                'y' => '-1260'
            )
        ),
        'rss' => array(
            'name' => 'rss',
            'black' => array(
                'x' => '0',
                'y' => '-1350'
            ),
            'gray' => array(
                'x' => '0',
                'y' => '-1440'
            ),
            'silver' => array(
                'x' => '0',
                'y' => '-1530'
            )
        ),
        'skype' => array(
            'name' => 'skype',
            'black' => array(
                'x' => '0',
                'y' => '-1620'
            ),
            'gray' => array(
                'x' => '0',
                'y' => '-1710'
            ),
            'silver' => array(
                'x' => '0',
                'y' => '-1800'
            )
        ),
        'twitter' => array(
            'name' => 'twitter',
            'black' => array(
                'x' => '0',
                'y' => '-1890'
            ),
            'gray' => array(
                'x' => '-90',
                'y' => '0'
            ),
            'silver' => array(
                'x' => '-90',
                'y' => '-90'
            )
        ),
        'yahoo' => array(
            'name' => 'yahoo',
            'black' => array(
                'x' => '-90',
                'y' => '-180'
            ),
            'gray' => array(
                'x' => '-90',
                'y' => '-270'
            ),
            'silver' => array(
                'x' => '-90',
                'y' => '-360'
            )
        ),
        'youtube' => array(
            'name' => 'youtube',
            'black' => array(
                'x' => '-90',
                'y' => '-450'
            ),
            'gray' => array(
                'x' => '-90',
                'y' => '-540'
            ),
            'silver' => array(
                'x' => '-90',
                'y' => '-630'
            )
        )
    );

    $socialnetworks = oenology_get_social_networks();
    $linkcolor = 'silver';
    $linkhovercolor = 'black';
    $colorscheme = oenology_get_color_scheme();
    if ( 'dark' == $colorscheme ) {
        $linkcolor = 'gray';
        $linkhovercolor = 'silver';
    }

?>

<style type="text/css">
<?php if( 'none' != $oenology_options['rss_feed'] ) { ?>
a[class="sidebar-social-icon"][title ^="RSS"] {
    background: url('<?php echo get_template_directory_uri(); ?>/images/socialiconsprite.png');
    background-position: <?php echo $socialiconbgposition['rss'][$linkcolor]['x'] . 'px ' . $socialiconbgposition['rss'][$linkcolor]['y'] . 'px'; ?>;
}
a[class="sidebar-social-icon"][title ^="RSS"]:hover {
    background: url('<?php echo get_template_directory_uri(); ?>/images/socialiconsprite.png');
    background-position: <?php echo $socialiconbgposition['rss'][$linkhovercolor]['x'] . 'px ' . $socialiconbgposition['rss'][$linkhovercolor]['y'] . 'px'; ?>;
}
<?php 
}
foreach ( $socialnetworks as $network ) { 

    $profile = $network['name'] . '_profile';
    if ( isset( $oenology_options[$profile] ) && !empty($oenology_options[$profile]) ) {
        $linkposx = $linkposy = $hoverposx = $hoverposy = '0';
        $linkposx = $socialiconbgposition[$network['name']][$linkcolor]['x'];
        $linkposy = $socialiconbgposition[$network['name']][$linkcolor]['y'];
        $hoverposx = $socialiconbgposition[$network['name']][$linkhovercolor]['x'];
        $hoverposy = $socialiconbgposition[$network['name']][$linkhovercolor]['y'];

?>
a[class="sidebar-social-icon"][title ^="<?php echo $network['title']; ?>"] {
    background: url('<?php echo get_template_directory_uri(); ?>/images/socialiconsprite.png');
    background-position: <?php echo $linkposx . 'px ' . $linkposy . 'px'; ?>;
}
a[class="sidebar-social-icon"][title ^="<?php echo $network['title']; ?>"]:hover {
    background: url('<?php echo get_template_directory_uri(); ?>/images/socialiconsprite.png');
    background-position: <?php echo $hoverposx . 'px ' . $hoverposy . 'px'; ?>;
}
<?php 
    } 
}
?>
</style>

<?php }

// Enqueue Social Icon Styles at wp_print_styles()
add_action('wp_print_styles', 'oenology_enqueue_social_icon_style', 11 );

chipbennett added a commit that referenced this issue May 6, 2012

Owner

chipbennett commented May 6, 2012

I think this one is good now. Thanks!

@chipbennett chipbennett closed this May 6, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment