Make WordPress Core

Ticket #12615: 12615.patch

File 12615.patch, 3.1 KB (added by hakre, 13 years ago)

Add "first" and "last" css-class in frontend use. Backend works as expected.

  • wp-includes/widgets.php

    ### Eclipse Workspace Patch 1.0
    #P wordpress-trunk
     
    844844        if ( is_int($index) ) {
    845845                $index = "sidebar-$index";
    846846        } else {
    847                 $index = sanitize_title($index);
    848                 foreach ( (array) $wp_registered_sidebars as $key => $value ) {
    849                         if ( sanitize_title($value['name']) == $index ) {
    850                                 $index = $key;
    851                                 break;
    852                         }
    853                 }
     847                $names = array_map( create_function( '$b', 'return $b["name"];' ), $wp_registered_sidebars );
     848                $names = array_flip( $names );
     849                if ( isset( $names[$index] ) )
     850                        $index = $names[$index];
     851                $index = sanitize_title( $index );
    854852        }
    855853
    856854        $sidebars_widgets = wp_get_sidebars_widgets();
    857855
    858         if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
     856        if ( empty( $wp_registered_sidebars[$index] ) || !is_array($sidebars_widgets[$index]) || empty( $sidebars_widgets[$index] ) )
    859857                return false;
    860858
    861859        $sidebar = $wp_registered_sidebars[$index];
     860       
     861        // only take those id that are in sidebar and exist in registered widgets
     862        $sidebars_widgets_ids = array_intersect( $sidebars_widgets[$index], array_keys( $wp_registered_widgets ) );
    862863
     864        // look for additional css classnames - first, last ...
     865        $find = array();
     866        foreach ( $sidebars_widgets_ids as $id ) {
     867                // normalize classname(s) into array if they are not yet
     868                if ( ! is_array( $wp_registered_widgets[$id]['classname'] ) )
     869                        $wp_registered_widgets[$id]['classname'] = (array) $wp_registered_widgets[$id]['classname'];
     870                       
     871                $callback = $wp_registered_widgets[$id]['callback'];
     872                if ( is_callable( $callback ) ) {
     873                        if ( empty( $find['first'] ) ) $find['first'] = $id; 
     874                        $find['last'] = $id;
     875                }
     876        }
     877       
     878        // ... and add classnames as needed
     879        foreach ( $find as $class => $id ) {
     880                $wp_registered_widgets[$id]['classname'][] = $class;                   
     881        }
     882       
    863883        $did_one = false;
    864         foreach ( (array) $sidebars_widgets[$index] as $id ) {
     884        foreach ( $sidebars_widgets_ids as $id ) {
    865885
    866                 if ( !isset($wp_registered_widgets[$id]) ) continue;
    867 
    868886                $params = array_merge(
    869887                        array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
    870888                        (array) $wp_registered_widgets[$id]['params']
    871889                );
    872890
    873                 // Substitute HTML id and class attributes into before_widget
    874                 $classname_ = '';
    875                 foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
    876                         if ( is_string($cn) )
    877                                 $classname_ .= '_' . $cn;
    878                         elseif ( is_object($cn) )
    879                                 $classname_ .= '_' . get_class($cn);
     891                // Substitute HTML id and class attributes into before_widget           
     892                $classes = array();
     893                foreach ( $wp_registered_widgets[$id]['classname'] as $cn ) {
     894                        if ( is_object($cn) ) $cn = get_class($cn);
     895                        if ( is_string($cn) ) $classes[] = $cn;
    880896                }
    881                 $classname_ = ltrim($classname_, '_');
     897                $classname_ = implode(' ', $classes);
    882898                $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
    883899
    884900                $params = apply_filters( 'dynamic_sidebar_params', $params );