Created
December 27, 2013 23:33
-
-
Save bradyvercher/8154099 to your computer and use it in GitHub Desktop.
Methods for retrieving widget instances.
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 americanaura_widget_classes( $params ) { | |
if ( 'pinboard' != $params[0]['id'] ) { | |
return $params; | |
} | |
// If a track has a file add a '.js-playable' class to the widget container | |
if ( 'audiotheme-track' == $widget_id_base ) { | |
$instance = americanaura_get_widget_instance( $params[0]['widget_id'], $params[1]['number'] ); | |
if ( isset( $instance['post_id'] ) && get_audiotheme_track_file_url( $instance['post_id'] ) ) { | |
$params[0]['before_widget'] = str_replace( '">', ' js-playable">', $params[0]['before_widget'] ); | |
} | |
} | |
return $params; | |
} | |
add_filter( 'dynamic_sidebar_params', 'americanaura_widget_classes' ); | |
/** | |
* Retrieve a widget instance. | |
* | |
* A helper function for retrieving a specific widget instance. The widget ID | |
* and instance number must be known. | |
* | |
* @param string $widget_id Widget identifier. | |
* @param int $number Widget number. | |
* @return array | |
*/ | |
function americanaura_get_widget_instance( $widget_id, $number ) { | |
global $wp_registered_widgets; | |
$widget_instance = null; | |
if ( isset( $wp_registered_widgets[ $widget_id ] ) ) { | |
$widget = $wp_registered_widgets[ $widget_id ]; | |
$widget_instances = get_option( $widget['callback'][0]->option_name ); | |
$widget_instance = $widget_instances[ $number ]; | |
} | |
return $widget_instance; | |
} |
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 | |
/** | |
* Retrieve a widget instance. | |
* | |
* A helper function for retrieving a specific widget instance. The widget ID | |
* and instance number must be known. | |
* | |
* Inspect the $wp_registered_widgets global for widget ids and numbers. | |
* | |
* @since 1.0.0 | |
* @uses $wp_registered_widgets | |
* | |
* @param string $widget_id | |
* @param int $number | |
* @return array | |
*/ | |
function tortilla_get_widget_instance( $widget_id, $number ) { | |
global $wp_registered_widgets; | |
$widget_instance = null; | |
if ( isset( $wp_registered_widgets[ $widget_id ] ) ) { | |
$widget = $wp_registered_widgets[ $widget_id ]; | |
$widget_instances = get_option( $widget['callback'][0]->option_name ); | |
$widget_instance = $widget_instances[ $number ]; | |
} | |
return $widget_instance; | |
} | |
/** | |
* Retrieve a list of widget instances for a given widget area. | |
* | |
* @since 1.0.0 | |
* @see dynamic_sidebar() | |
* @uses tortilla_is_valid_sidebar() | |
* @uses tortilla_get_widget_instance() | |
* | |
* @param int|string $index Widget area id. | |
* @return array | |
*/ | |
function tortilla_get_widget_instances( $index = 1 ) { | |
global $wp_registered_sidebars, $wp_registered_widgets; | |
$index = tortilla_is_valid_sidebar( $index ); | |
if ( ! $index ) { | |
return false; | |
} | |
$widgets = array(); | |
$sidebar = $wp_registered_sidebars[ $index ]; | |
$sidebars_widgets = wp_get_sidebars_widgets(); | |
foreach( $sidebars_widgets[ $index ] as $id ) { | |
if ( ! isset( $wp_registered_widgets[ $id ] ) ) { | |
continue; | |
} | |
$widget = $wp_registered_widgets[ $id ]; | |
$class = get_class( $widget['callback'][0] ); | |
$instance = tortilla_get_widget_instance( $id, $widget['params'][0]['number'] ); | |
// Add some extra args to indicate the type of widget. | |
$instance['widget_id_base'] = $widget['callback'][0]->id_base; | |
$instance['widget_classname'] = $widget['classname']; | |
$widgets[] = $instance; | |
} | |
return ( empty( $widgets ) ) ? false : $widgets; | |
} | |
/** | |
* Determine if a given identifier is a valid sidebar and retrieve it's id. | |
* | |
* Converts sidebar names and integers to ids. | |
* | |
* @since 1.0.0 | |
* @see dynamic_sidebar() | |
* | |
* @param string|int $index A sidebar identifier (id|name|numeric index). | |
* @return string|bool The sidebar id if found or false if not. | |
*/ | |
function tortilla_is_valid_sidebar( $index ) { | |
global $wp_registered_sidebars; | |
if ( is_int( $index ) ) { | |
$index = 'sidebar-' . $index; | |
} else { | |
$index = sanitize_title( $index ); | |
foreach ( (array) $wp_registered_sidebars as $key => $value ) { | |
if ( sanitize_title( $value['name'] ) == $index ) { | |
$index = $key; | |
break; | |
} | |
} | |
} | |
$sidebars_widgets = wp_get_sidebars_widgets(); | |
if ( empty( $sidebars_widgets ) ) { | |
return false; | |
} | |
if ( empty( $wp_registered_sidebars[ $index ] ) || ! array_key_exists( $index, $sidebars_widgets ) || ! is_array( $sidebars_widgets[ $index ] ) || empty( $sidebars_widgets[ $index ] ) ) { | |
return false; | |
} | |
return $index; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment