Created
May 13, 2012 13:24
-
-
Save markoheijnen/2688460 to your computer and use it in GitHub Desktop.
Cool widget class for WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function rockstars_register_default_widget( $sidebar, $class_name, $settings ) { | |
| global $rockstars_widgets; | |
| if( !is_admin() ) { | |
| return $rockstars_widgets->register_default_widget( $sidebar, $class_name, $settings ); | |
| } | |
| return false; | |
| } | |
| function rockstars_register_horizontal_sidebar( $sidebar_id, $columns ) { | |
| global $rockstars_widgets; | |
| if( !is_admin() ) { | |
| return $rockstars_widgets->register_horizontal_sidebar( $sidebar_id, $columns ); | |
| } | |
| return false; | |
| } | |
| class Rockstars_Widgets { | |
| private $custom_settings = array(); | |
| public $horizontal_end_row = '<div class="clearfix"></div>'; | |
| private $horizontal_sidebars = array(); | |
| private $horizontal_counter = array(); | |
| function __construct() { | |
| add_filter( 'dynamic_sidebar_params', array( &$this, '_sidebar_params' ) ); | |
| } | |
| public function register_default_widget( $sidebar, $class_name, $settings ) { | |
| global $_wp_sidebars_widgets, $wp_widget_factory; | |
| if( empty( $_wp_sidebars_widgets[ $sidebar ] ) ) { | |
| if( isset( $wp_widget_factory->widgets[ $class_name ] ) ) { | |
| $class = $wp_widget_factory->widgets[ $class_name ]; | |
| if( !isset( $this->custom_settings[ $class->option_name ] ) ) { | |
| $this->custom_settings[ $class->option_name ] = array(); | |
| add_filter( 'option_' . $class->option_name, array( &$this, '_register_default_widgets_settings' ) ); | |
| } | |
| $id = 900 + count( $this->custom_settings[ $class->option_name ] ); | |
| $this->custom_settings[ $class->option_name ][ $id ] = $settings; | |
| $_wp_sidebars_widgets[ $sidebar ][] = $class->id_base . '-' . $id; | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| function _register_default_widgets_settings( $value ) { | |
| $filter = substr( current_filter(), 7); | |
| if( isset( $this->custom_settings[ $filter ] ) ) { | |
| $value = $value + $this->custom_settings[ $filter ]; | |
| } | |
| return $value; | |
| } | |
| public function register_horizontal_sidebar( $sidebar_id, $columns ) { | |
| global $_wp_sidebars_widgets; | |
| if( ! empty( $_wp_sidebars_widgets[ $sidebar_id ] ) ) { | |
| $this->horizontal_sidebars[ $sidebar_id ] = intval( $columns ); | |
| return true; | |
| } | |
| return false; | |
| } | |
| function _sidebar_params( $params ) { | |
| $sidebar_id = $params[0]['id']; | |
| if( ! isset( $this->horizontal_sidebars[ $sidebar_id ] ) ) { | |
| return $params; | |
| } | |
| if( ! isset( $this->horizontal_counter[ $sidebar_id ] ) ) { | |
| $this->horizontal_counter[ $sidebar_id ] = 0; | |
| } | |
| $this->horizontal_counter[ $sidebar_id ]++; | |
| $additional_classes = trim( apply_filters( 'rockstar_sidebar_classes', '', $this->horizontal_sidebars[ $sidebar_id ], $sidebar_id ) ); | |
| if( $this->horizontal_counter[ $sidebar_id ] % $this->horizontal_sidebars[ $sidebar_id ] == 0 ) { | |
| $additional_classes .= ' last'; | |
| $params[0]['after_widget'] = $params[0]['after_widget'] . $this->horizontal_end_row; | |
| } | |
| $params[0]['before_widget'] = preg_replace( '/class="/', "class=\"' . $additional_classes . ' ", $params[0]['before_widget'], 1 ); | |
| return $params; | |
| } | |
| } | |
| $GLOBALS['rockstars_widgets'] = new Rockstars_Widgets(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment