Created
October 27, 2012 19:46
-
-
Save markoheijnen/3965864 to your computer and use it in GitHub Desktop.
Get the plugins of an author on WordPress.org
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 | |
| class Connection_Wordpress { | |
| private $base_url = 'http://api.wordpress.org/'; | |
| function __construct() { | |
| add_action( 'widgets_init', array( $this, '_register_widgets' ) ); | |
| } | |
| function _register_widgets() { | |
| register_widget( 'Connection_Wordpress_Widget' ); | |
| } | |
| public function get_author_plugins( $name, $cache = true ) { | |
| if( ! $name ) | |
| return false; | |
| if ( false === $cache || false === ( $data = get_transient( 'connection_wordpress_author' . $name ) ) ) { | |
| $url = $this->base_url . 'plugins/info/1.0/'; | |
| $payload = array( | |
| 'action' => 'query_themes', | |
| 'request' => serialize( | |
| (object) array( | |
| 'author' => $name, | |
| 'per_page' => 30 | |
| ) | |
| ) | |
| ); | |
| $request = wp_remote_post( $url, array( 'body' => $payload ) ); | |
| $data = maybe_unserializew( wp_remote_retrieve_body( $request ) ); | |
| if( $cache && $data ) | |
| set_transient( 'connection_wordpress_author' . $name, $data ); | |
| } | |
| return $data; | |
| } | |
| public function get_author_themes( $name, $cache = true ) { | |
| if( ! $name ) | |
| return false; | |
| if ( false === $cache || false === ( $data = get_transient( 'connection_wordpress_author' . $name ) ) ) { | |
| $url = $this->base_url . 'themes/info/1.0/'; | |
| $payload = array( | |
| 'action' => 'query_plugins', | |
| 'request' => serialize( | |
| (object) array( | |
| 'author' => $name, | |
| 'per_page' => 30 | |
| ) | |
| ) | |
| ); | |
| $request = wp_remote_post( $url, array( 'body' => $payload ) ); | |
| $data = unserialize( wp_remote_retrieve_body( $request ) ); | |
| if( $cache && $data ) | |
| set_transient( 'connection_wordpress_author' . $name, $data ); | |
| } | |
| return $data; | |
| } | |
| } | |
| new Connection_Wordpress; | |
| class Connection_Wordpress_Widget extends WP_Widget { | |
| function __construct() { | |
| parent::__construct( false, 'Connection - WordPress plugins' ); | |
| } | |
| function widget( $args, $instance ) { | |
| extract( $args ); | |
| $title = apply_filters( 'widget_title', $instance['title'] ); | |
| echo $before_widget; | |
| if ( ! empty( $title ) ) | |
| echo $before_title . $title . $after_title; | |
| $plugins = Connection_Wordpress::get_author_plugins( $instance[ 'author' ] ); | |
| echo '<ul>'; | |
| foreach( $plugins->plugins as $plugin ) { | |
| echo '<li><a href="http://wordpress.org/extend/plugins/' . $plugin->slug . '">' . $plugin->name . '</a></li>'; | |
| } | |
| echo '</ul>'; | |
| echo $after_widget; | |
| } | |
| function update( $new_instance, $old_instance ) { | |
| $instance = array(); | |
| $instance['title'] = strip_tags( $new_instance['title'] ); | |
| $instance['author'] = strip_tags( $new_instance['author'] ); | |
| return $instance; | |
| } | |
| function form( $instance ) { | |
| if ( isset( $instance[ 'title' ] ) ) { | |
| $title = $instance[ 'title' ]; | |
| $author = $instance[ 'author' ]; | |
| } | |
| else { | |
| $title = __( 'New title', 'connections' ); | |
| $author = ''; | |
| } | |
| ?> | |
| <p> | |
| <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> | |
| <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> | |
| </p> | |
| <p> | |
| <label for="<?php echo $this->get_field_id( 'author' ); ?>"><?php _e( 'Author:' ); ?></label> | |
| <input class="widefat" id="<?php echo $this->get_field_id( 'author' ); ?>" name="<?php echo $this->get_field_name( 'author' ); ?>" type="text" value="<?php echo esc_attr( $author ); ?>" /> | |
| </p> | |
| <?php | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment