Created
January 18, 2014 19:09
-
-
Save paulgibbs/8494792 to your computer and use it in GitHub Desktop.
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 | |
// Based on Achievements' DPA_Available_Achievements_Widget widget. Built in about 45 minutes, so YMMV. | |
add_action( 'dpa_ready', 'md_widget_wrapper', 200 ); | |
function md_widget_wrapper() { | |
class MD_Unlocked_Achievements_Widget_Grid extends DPA_Available_Achievements_Widget { | |
/** | |
* Constructor | |
*/ | |
public function __construct() { | |
$widget_ops = array( | |
'classname' => 'widget_dpa_available_achievements', // MD - left intact so existing CSS works | |
'description' => __( 'Displays a photo grid of unlocked achievements for the "current" user.', 'dpa' ), | |
); | |
WP_Widget::__construct( false, __( '(MD) Unlocked Photo Grid', 'dpa' ), $widget_ops ); | |
} | |
/** | |
* Register the widget | |
*/ | |
static public function register_widget() { | |
register_widget( 'MD_Unlocked_Achievements_Widget_Grid' ); | |
} | |
/** | |
* Displays the output | |
* | |
* @param array $args | |
* @param array $instance | |
*/ | |
public function widget( $args, $instance ) { | |
$settings = $this->parse_settings( $instance ); | |
// Use these filters | |
$settings['limit'] = absint( apply_filters( 'dpa_available_achievements_limit', $settings['limit'], $instance, $this->id_base ) ); | |
$settings['title'] = apply_filters( 'dpa_available_achievements_title', $settings['title'], $instance, $this->id_base ); | |
// WordPress filters widget_title through esc_html. | |
$settings['title'] = apply_filters( 'widget_title', $settings['title'], $instance, $this->id_base ); | |
// MD - bail out if not author page | |
if ( ! is_author() ) | |
return; | |
// MD - get unlocked posts for this user | |
$progress_posts = dpa_get_progress( array( | |
'author' => dpa_get_displayed_user_id(), | |
'fields' => 'id=>parent', | |
'no_found_rows' => true, | |
'nopaging' => true, | |
'numberposts' => $settings['limit'], | |
'post_status' => dpa_get_unlocked_status_id(), | |
) ); | |
// MD | |
if ( empty( $progress_posts ) ) | |
return; | |
// MD | |
$achievement_ids = array_unique( array_keys( $progress_posts ) ); | |
// MD - get the posts | |
$achievements = get_posts( array( | |
'ignore_sticky_posts' => true, | |
'no_found_rows' => true, | |
'post__in' => $progress_posts, | |
'post_status' => 'publish', | |
'post_type' => dpa_get_achievement_post_type(), | |
'suppress_filters' => false, | |
) ); | |
// Bail if no posts | |
if ( empty( $achievements ) ) | |
return; | |
echo $args['before_widget']; | |
if ( ! empty( $settings['title'] ) ) | |
echo $args['before_title'] . $settings['title'] . $args['after_title']; | |
echo '<ul>'; | |
foreach ( $achievements as $post ) { | |
if ( has_post_thumbnail( $post->ID ) ) : | |
?> | |
<li> | |
<a href="<?php dpa_achievement_permalink( $post->ID ); ?>"><?php echo get_the_post_thumbnail( $post->ID, 'dpa-thumb', array( 'alt' => dpa_get_achievement_title( $post->ID ) ) ); ?></a> | |
</li> | |
<?php | |
endif; | |
} | |
echo '</ul>'; | |
echo $args['after_widget']; | |
} | |
} | |
MD_Unlocked_Achievements_Widget_Grid::register_widget(); | |
} // function md_widget_wrapper() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment