Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created June 2, 2012 04:56
Show Gist options
  • Save trepmal/2856658 to your computer and use it in GitHub Desktop.
Save trepmal/2856658 to your computer and use it in GitHub Desktop.
WPORG Repo Plugins
<?php
/*
Plugin Name: WPORG Repo Plugins
Description: Widget to display plugins on the wordpress.org repository by author
Author: Kailey Lampert
Author URI: http://kaileylampert.com/
*/
add_action( 'widgets_init', 'register_wporg_repo_plugins' );
function register_wporg_repo_plugins() {
register_widget( 'WPORG_Repo_Plugins' );
}
class WPORG_Repo_Plugins extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'wporg-repo-plugins', 'description' => __( 'Your WordPress.org Repository Plugins' ) );
$control_ops = array( /*'width' => 300*/ );
parent::WP_Widget( 'wporg_repo_plugins', __( 'WPORG Repo Plugins' ), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
extract( $instance, EXTR_SKIP );
// do nothing if no username was provided
if ( empty( $wporgusername ) ) return;
echo $before_widget;
echo $instance['hide_title'] ? '' : $before_title . $instance['title'] . $after_title;
//create a unique id for this widget and username
$uid = $widget_id .'-'. $wporgusername;
//cache based on unique id
if ( false === ( $plugins = get_transient( $uid ) ) ) {
require_once( ABSPATH. 'wp-admin/includes/plugin-install.php' );
$api = plugins_api('query_plugins', array('author' => $wporgusername ) );
$plugins = json_decode( json_encode( $api->plugins ), true );
set_transient( $uid, $plugins, 60*60 ); //keep for an hour
//echo '<!-- just cached -->';
}
$keys = array_keys( $plugins[0] );
// keys:
// name, slug, version, author, author_profile, contributors,
// requires, tested, compatibility, rating, num_ratings, homepage,
// description, short_description
//extra keys for custom/alternate functionality
$keys[] = 'url';
$keys[] = 'contributors_unlinked';
$keys[] = 'author_unlinked';
$format = stripslashes( $format );
// store each plugin listing in an array, then we can use implode for easy separation
$plugin_array = array();
foreach( $plugins as $plg ) {
$html = $format;
foreach( $keys as $k ) {
switch ( $k ) {
case 'contributors' : //contains array. needs special handling
$x = array();
foreach( $plg[ $k ] as $name => $url ) {
$x[] = "<a href='$url'>$name</a>";
}
$content = implode( ', ', $x );
break;
case 'contributors_unlinked' : //contains array, alt format. needs special handling
$x = array();
foreach( $plg[ 'contributors' ] as $name => $url ) {
$x[] = $name;
}
$content = implode( ', ', $x );
break;
case 'compatibility' : //nested arrays. skipping for now
break;
case 'author_unlinked' : //alt format. needs special handling
$content = strip_tags( $plg[ 'author' ] );
break;
case 'url' : //custom key. needs special handling
$content = 'http://wordpress.org/extend/plugins/'.$plg['slug'];
break;
default : //doesn't need special handling!
$content = $plg[ $k ];
}
$html = str_replace("[$k]", $content, $html );
}
$plugin_array[] = $html;
}
echo stripslashes( $before_format );
echo implode( $separator, $plugin_array );
echo stripslashes( $after_format );
echo $after_widget;
} //end widget()
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = esc_attr( $new_instance['title'] );
$instance['hide_title'] = (bool) $new_instance['hide_title'] ? 1 : 0;
$instance['wporgusername'] = esc_attr( $new_instance['wporgusername'] );
$instance['before_format'] = wp_filter_post_kses( $new_instance['before_format'] );
$instance['format'] = wp_filter_post_kses( $new_instance['format'] );
$instance['separator'] = wp_filter_post_kses( $new_instance['separator'] );
$instance['after_format'] = wp_filter_post_kses( $new_instance['after_format'] );
return $instance;
} //end update()
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => 'My WordPress.org Plugins', 'hide_title' => 0, 'wporgusername' => '', 'before_format' => '<ul>', 'format' => '<li>[name]</li>', 'separator' => '', 'after_format' => '</ul>' ) );
extract( $instance );
?>
<p style="width:63%;float:left;">
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' );?>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</label>
</p>
<p style="width:33%;float:right;padding-top:20px;height:20px;">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hide_title'); ?>" name="<?php echo $this->get_field_name('hide_title'); ?>"<?php checked( $hide_title ); ?> />
<label for="<?php echo $this->get_field_id('hide_title'); ?>"><?php _e('Hide Title?' );?></label>
</p>
<p style="clear:both;">
<label for="<?php echo $this->get_field_id( 'wporgusername' ); ?>"><?php _e( 'WP.org Username:' );?>
<input class="widefat" id="<?php echo $this->get_field_id('wporgusername'); ?>" name="<?php echo $this->get_field_name('wporgusername'); ?>" type="text" value="<?php echo $wporgusername; ?>" />
</label>
</p>
<p style="clear:both;">
<label for="<?php echo $this->get_field_id( 'before_format' ); ?>"><?php _e( 'Before Format:' );?>
<input class="widefat" id="<?php echo $this->get_field_id('before_format'); ?>" name="<?php echo $this->get_field_name('before_format'); ?>" type="text" value="<?php echo htmlspecialchars( stripslashes( $before_format ) ); ?>" />
</label>
<small>Displayed before all the plugins.</small>
</p>
<p style="clear:both;">
<label for="<?php echo $this->get_field_id( 'format' ); ?>"><?php _e( 'Format:' );?></label>
<textarea class="widefat" id="<?php echo $this->get_field_id('format'); ?>" name="<?php echo $this->get_field_name('format'); ?>"><?php
echo htmlspecialchars( stripslashes( $format ) );
?></textarea>
<small>How to display each plugin.</small><br />
<?php
$keys = array( 'name', 'slug', 'version', 'author', 'author_profile', 'contributors', 'requires', 'tested', 'compatibility', 'rating', 'num_ratings', 'homepage', 'description', 'short_description' );
$keys[] = 'url';
$keys[] = 'contributors_unlinked';
$keys[] = 'author_unlinked';
unset( $keys[ array_search('compatibility', $keys ) ] ); //maybe later
sort($keys);
$keys = '['. implode('], [', $keys) .']';
echo "<small>Accepted shortcodes: $keys</small>";
?>
</p>
<p style="clear:both;">
<label for="<?php echo $this->get_field_id( 'separator' ); ?>"><?php _e( 'Separator:' );?>
<input class="widefat" id="<?php echo $this->get_field_id('separator'); ?>" name="<?php echo $this->get_field_name('separator'); ?>" type="text" value="<?php echo htmlspecialchars( stripslashes( $separator ) ); ?>" />
</label>
<small>Displayed between each plugin.</small>
</p>
<p style="clear:both;">
<label for="<?php echo $this->get_field_id( 'after_format' ); ?>"><?php _e( 'After Format:' );?>
<input class="widefat" id="<?php echo $this->get_field_id('after_format'); ?>" name="<?php echo $this->get_field_name('after_format'); ?>" type="text" value="<?php echo htmlspecialchars( stripslashes( $after_format ) ); ?>" />
</label>
<small>Displayed after all the plugins.</small>
</p>
<?php
} //end form()
}
@trepmal
Copy link
Author

trepmal commented Jun 2, 2012

@jarretc $widget_id is extracted from $args

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment