Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/15/2009 12:47:21 AM (16 years ago)
Author:
apeatling
Message:

Updating BuddyPress widgets to support new widget API.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1.0/bp-blogs/bp-blogs-widgets.php

    r1520 r1602  
    33/* Register widgets for blogs component */
    44function bp_blogs_register_widgets() {
    5     global $current_blog;
    6 
    7     /* Latest Posts Widget */
    8     wp_register_sidebar_widget( 'buddypress-blogs', __('Recent Blog Posts', 'buddypress'), 'bp_blogs_widget_recent_posts');
    9     wp_register_widget_control( 'buddypress-blogs', __('Recent Blog Posts', 'buddypress'), 'bp_blogs_widget_recent_posts_control' );
    10 
    11     if ( is_active_widget( 'bp_blogs_widget_recent_posts' ) ) {
    12         wp_enqueue_style( 'bp-blogs-widget-posts-css', BP_PLUGIN_URL . '/bp-blogs/css/widget-blogs.css' );     
    13     }
     5    add_action('widgets_init', create_function('', 'return register_widget("BP_Blogs_Recent_Posts_Widget");') );
    146}
    157add_action( 'plugins_loaded', 'bp_blogs_register_widgets' );
    168
    17 
    18 function bp_blogs_widget_recent_posts($args) {
    19     global $current_blog;
    20    
    21     extract($args);
    22     $options = get_blog_option( $current_blog->blog_id, 'bp_blogs_widget_recent_posts' );
    23    
    24     if ( empty( $options['max_posts'] ) || !$options['max_posts'] )
    25         $options['max_posts'] = 5;
    26 ?>
    27     <?php echo $before_widget; ?>
    28     <?php echo $before_title
    29         . $widget_name
    30         . $after_title; ?>
    31 
    32     <?php $posts = bp_blogs_get_latest_posts( null, $options['max_posts'] ) ?>
    33     <?php $counter = 0; ?>
    34        
    35     <?php if ( $posts ) : ?>
    36         <div class="item-options" id="recent-posts-options">
    37             <?php _e("Site Wide", 'buddypress') ?>
    38         </div>
    39         <ul id="recent-posts" class="item-list">
    40             <?php foreach ( $posts as $post ) : ?>
    41                 <li>
    42                     <div class="item-avatar">
    43                         <a href="<?php echo bp_post_get_permalink( $post, $post->blog_id ) ?>" title="<?php echo apply_filters( 'the_title', $post->post_title ) ?>"><?php echo bp_core_get_avatar( $post->post_author, 1 ) ?></a>
    44                     </div>
    45 
    46                     <div class="item">
    47                         <h4 class="item-title"><a href="<?php echo bp_post_get_permalink( $post, $post->blog_id ) ?>" title="<?php echo apply_filters( 'the_title', $post->post_title ) ?>"><?php echo apply_filters( 'the_title', $post->post_title ) ?></a></h4>
    48                         <?php if ( !$counter ) : ?>
    49                             <div class="item-content"><?php echo bp_create_excerpt($post->post_content) ?></div>
    50                         <?php endif; ?>
    51                         <div class="item-meta"><em><?php printf( __( 'by %s from the blog <a href="%s">%s</a>', 'buddypress' ), bp_core_get_userlink( $post->post_author ), get_blog_option( $post->blog_id, 'siteurl' ), get_blog_option( $post->blog_id, 'blogname' ) ) ?></em></div>
    52                     </div>
    53                 </li>
    54                 <?php $counter++; ?>   
    55             <?php endforeach; ?>
    56         </ul>
    57     <?php else: ?>
    58         <div class="widget-error">
    59             <?php _e('There are no recent blog posts, why not write one?', 'buddypress') ?>
    60         </div>
    61     <?php endif; ?>
    62 
    63     <?php echo $after_widget; ?>
    64 <?php
    65 }
    66 
    67 function bp_blogs_widget_recent_posts_control() {
    68     global $current_blog;
    69    
    70     $options = $newoptions = get_blog_option( $current_blog->blog_id, 'bp_blogs_widget_recent_posts');
    71 
    72     if ( $_POST['bp-blogs-widget-recent-posts-submit'] ) {
    73         $newoptions['max_posts'] = strip_tags( stripslashes( $_POST['bp-blogs-widget-recent-posts-max'] ) );
    74     }
    75    
    76     if ( $options != $newoptions ) {
    77         $options = $newoptions;
    78         update_blog_option( $current_blog->blog_id, 'bp_blogs_widget_recent_posts', $options );
     9class BP_Blogs_Recent_Posts_Widget extends WP_Widget {
     10    function bp_blogs_recent_posts_widget() {
     11        parent::WP_Widget( false, $name = 'Recent Site Wide Posts' );
     12        wp_enqueue_style( 'bp-blogs-widget-posts-css', BP_PLUGIN_URL . '/bp-blogs/css/widget-blogs.css' );     
    7913    }
    8014
     15    function widget($args, $instance) {
     16        global $bp;
     17       
     18        extract( $args );
     19       
     20        echo $before_widget;
     21        echo $before_title
     22           . $widget_name
     23           . $after_title; ?>
     24   
     25        <?php
     26        if ( empty( $instance['max_posts'] ) || !$instance['max_posts'] )
     27            $instance['max_posts'] = 10; ?>
     28       
     29        <?php $posts = bp_blogs_get_latest_posts( null, $instance['max_posts'] ) ?>
     30        <?php $counter = 0; ?>
     31       
     32        <?php if ( $posts ) : ?>
     33            <div class="item-options" id="recent-posts-options">
     34                <?php _e("Site Wide", 'buddypress') ?>
     35            </div>
     36            <ul id="recent-posts" class="item-list">
     37                <?php foreach ( $posts as $post ) : ?>
     38                    <li>
     39                        <div class="item-avatar">
     40                            <a href="<?php echo bp_post_get_permalink( $post, $post->blog_id ) ?>" title="<?php echo apply_filters( 'the_title', $post->post_title ) ?>"><?php echo bp_core_get_avatar( $post->post_author, 1 ) ?></a>
     41                        </div>
     42
     43                        <div class="item">
     44                            <h4 class="item-title"><a href="<?php echo bp_post_get_permalink( $post, $post->blog_id ) ?>" title="<?php echo apply_filters( 'the_title', $post->post_title ) ?>"><?php echo apply_filters( 'the_title', $post->post_title ) ?></a></h4>
     45                            <?php if ( !$counter ) : ?>
     46                                <div class="item-content"><?php echo bp_create_excerpt($post->post_content) ?></div>
     47                            <?php endif; ?>
     48                            <div class="item-meta"><em><?php printf( __( 'by %s from the blog <a href="%s">%s</a>', 'buddypress' ), bp_core_get_userlink( $post->post_author ), get_blog_option( $post->blog_id, 'siteurl' ), get_blog_option( $post->blog_id, 'blogname' ) ) ?></em></div>
     49                        </div>
     50                    </li>
     51                    <?php $counter++; ?>   
     52                <?php endforeach; ?>
     53            </ul>
     54        <?php else: ?>
     55            <div class="widget-error">
     56                <?php _e('There are no recent blog posts, why not write one?', 'buddypress') ?>
     57            </div>
     58        <?php endif; ?>
     59           
     60        <?php echo $after_widget; ?>
     61    <?php
     62    }
     63
     64    function update( $new_instance, $old_instance ) {
     65        $instance = $old_instance;
     66        $instance['max_posts'] = strip_tags( $new_instance['max_posts'] );
     67
     68        return $instance;
     69    }
     70
     71    function form( $instance ) {
     72        $instance = wp_parse_args( (array) $instance, array( 'max_posts' => 10 ) );
     73        $max_posts = strip_tags( $instance['max_posts'] );
     74        ?>
     75
     76        <p><label for="bp-blogs-widget-posts-max"><?php _e('Max posts to show:', 'buddypress'); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_posts' ); ?>" name="<?php echo $this->get_field_name( 'max_posts' ); ?>" type="text" value="<?php echo attribute_escape( $max_posts ); ?>" style="width: 30%" /></label></p>
     77    <?php
     78    }
     79}
    8180?>
    82         <p><label for="bp-blogs-widget-recent-posts-max"><?php _e('Max Number of Posts:', 'buddypress'); ?> <input class="widefat" id="bp-blogs-widget-recent-posts-max" name="bp-blogs-widget-recent-posts-max" type="text" value="<?php echo attribute_escape( $options['max_posts'] ); ?>" style="width: 30%" /></label></p>
    83         <input type="hidden" id="bp-blogs-widget-recent-posts-submit" name="bp-blogs-widget-recent-posts-submit" value="1" />
    84 <?php
    85 }
    86 
    87 ?>
Note: See TracChangeset for help on using the changeset viewer.