Skip to content

Instantly share code, notes, and snippets.

@brasofilo
Last active November 5, 2023 23:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save brasofilo/6898766 to your computer and use it in GitHub Desktop.
Save brasofilo/6898766 to your computer and use it in GitHub Desktop.
GitHub Plug-in for WordPress (Take 2) * Basic information about your repositories and its Forks and Watchers. * Based on http://www.developerdrive.com/2013/06/github-plug-in-for-wordpress/
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: GitHub Plug-in for WordPress (Take 2)
* Description: Basic information about, your repositories, and its Forks and Watchers.
* Plugin URI: https://gist.github.com/brasofilo/6898766
* Version: 2013.10.09
* Author: Rodolfo Buaiz
* Author URI: http://brasofilo.com
* License: GPL
* Text Domain: plugin_unique_name
* Domain Path: /languages
*/
/**
* BASED ON http://www.developerdrive.com/2013/06/github-plug-in-for-wordpress/
*
* Main changes:
* - Encapsulated into pro Plugin Class
* - Moved check_oauth into own function, loaded at plugin init
* - No need to load core files
* - Added cache with transients
* - Menu moved to Dashboard
* - Admin URL set as class property
* - Moved all Styles enqueue inside the Shortcode. Yes, it's possible.
* - Changed get_template_directory_uri for plugins_url
* - Updated link to API registration
* - Changed page name from 'github.php' to 'my-github-connect'
*/
/**
* Based on Plugin Class Demo
* https://gist.github.com/toscho/3804204
*/
add_action(
'plugins_loaded',
array ( B5F_GitHub_For_WordPress::get_instance(), 'plugin_setup' )
);
class B5F_GitHub_For_WordPress
{
/**
* Plugin instance.
* @see get_instance()
* @type object
*/
protected static $instance = NULL;
/**
* URL to this plugin's directory.
* @type string
*/
public $plugin_url = '';
/**
* Path to this plugin's directory.
* @type string
*/
public $plugin_path = '';
/**
* Hardcoded in the redirects.
* @type string
*/
public $plugin_admin_url = 'index.php?page=my-github-connect';
/**
* Constructor. Intentionally left empty and public.
* Please, read Plugin Class Demo documentation.
*/
public function __construct() { }
/**
* Access this plugin’s working instance
*
* @wp-hook plugins_loaded
* @since 2012.09.13
* @return object of this class
*/
public static function get_instance()
{
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}
/**
* Used for regular plugin work.
*
* @wp-hook plugins_loaded
* @since 2012.09.10
* @return void
*/
public function plugin_setup()
{
$this->plugin_url = plugins_url( '/', __FILE__ );
$this->plugin_path = plugin_dir_path( __FILE__ );
$this->load_language( 'plugin_unique_name' );
$this->check_oauth();
add_shortcode( 'github', array( $this, 'render_shortcode' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
/**
* Add plugin menu to the Dashboard
*/
public function admin_menu()
{
add_dashboard_page(
'GitHub Options',
'GitHub',
'manage_options',
'my-github-connect',
array( $this, 'admin_page' )
);
}
/**
* Plugin Admin page
*/
public function admin_page()
{
# Form enviado
if( $_SERVER["REQUEST_METHOD"] == "POST" )
{
# Seguridad
if (
isset( $_POST['b5f_gpfw_nonce'] )
&& wp_verify_nonce( $_POST['b5f_gpfw_nonce'], plugin_basename( __FILE__ ) )
)
{
# Actualizar opciones
if( isset( $_POST['api_key'] ) )
update_option( 'GITHUB_API_KEY', $_POST['api_key'] );
if( isset( $_POST['api_secret_key'] ) )
update_option( 'GITHUB_API_SECRET_KEY', $_POST['api_secret_key'] );
}
}
?>
<div class="github-admin-options">
<h1>GitHub Options</h1>
<form name="options" method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<?php wp_nonce_field( plugin_basename( __FILE__ ), 'b5f_gpfw_nonce' ); ?>
<label for="api_key">API Key<span class="required">(*)</span>: </label>
<input type="text" name="api_key" value="<?php echo get_option( 'GITHUB_API_KEY', '' ); ?>" size="70">
<br />
<label for="api_secret_key">Consumer Secret<span class="required">(*)</span>: </label>
<input type="text" name="api_secret_key" value="<?php echo get_option( 'GITHUB_API_SECRET_KEY', '' ); ?>" size="70">
<br />
<label for="bearer_token">Authentication Token: </label>
<input type="text" disabled value="<?php echo get_option( 'GITHUB_AUTHENTICATION_TOKEN', '' ); ?>" size="70">
<br />
<input class="button-primary" type="submit" name="save" />
<?php
$state = base64_encode( time() );
$redirect = admin_url( $this->plugin_admin_url );
$api_key = get_option( 'GITHUB_API_KEY' );
$api_secret = get_option( 'GITHUB_API_SECRET_KEY' );
$token = get_option( 'GITHUB_AUTHENTICATION_TOKEN' );
if( $api_key && $api_secret && !$token )
{
$api_url = "https://github.com/login/oauth/authorize?client_id=$api_key&scope=&state=$state&redirect_uri=$redirect";
?>
<a class="button-primary" type="button" href="<?php echo $api_url; ?>">Authenticate</a>
<?php
}
?>
<br/>
<small>You can sign up for a API key <a href="https://github.com/settings/applications/new" target="_blank">here</a></small>
</form>
<br />
<?php echo do_shortcode( '[github]' ); ?>
</div>
<?php
}
/**
* Render the shortcode
*
* Used in admin page as well
*
* @param array $atts
* @return string
*/
public function render_shortcode( $atts )
{
$token = get_option( 'GITHUB_AUTHENTICATION_TOKEN' );
if( !$token )
return;
wp_enqueue_style( 'github-style', plugins_url( '/style.css', __FILE__ ) );
add_filter( 'https_ssl_verify', '__return_false' );
$api_url = "https://api.github.com/user?access_token=$token";
$json = get_transient( 'github_user' );
if( !$json )
{
$response = wp_remote_get( $api_url );
$json = json_decode( $response['body'] );
set_transient( 'github_user', $json, 60*60 );
}
$return = '';
$return .= '<div class="github">';
$return .= '<h2><a href="' . $json->{'html_url'} . '" target="_blank">' . $json->{'name'} . '</a></h2>';
$return .= '<div class="bio">';
$return .= '<span>' . $json->{'bio'} . '</span>';
$return .= '</div>';
$return .= '<div class="counts">';
$return .= '<div>' . $json->{'public_repos'} . '<br/><span>repos</span></div>';
$return .= '<div>' . $json->{'followers'} . '<br/><span>followers</span></div>';
$return .= '<div>' . $json->{'following'} . '<br/><span>following</span></div>';
$return .= '</div>';
$api_url = "https://api.github.com/user/repos?access_token=$token";
$json = get_transient( 'github_repos' );
if( !$json )
{
$response = wp_remote_get( $api_url );
$json = json_decode( $response['body'] );
set_transient( 'github_repos', $json, 60*60 );
}
$return .= '<ol class="repos">';
foreach( $json as $i => $repos )
{
$return .= '<li>';
$return .= '<h3><a href="' . $repos->{'html_url'} . '" target="_blank" title="' . $repos->{'description'} . '">' . str_replace( '_', ' ', $repos->{'name'} ) . '</a></h3>';
$return .= '<div>';
$return .= '<div>';
$return .= $repos->{'forks_count'};
$return .= '<br/><span>forks</span>';
$return .= '</div>';
$return .= '<div>';
$return .= $repos->{'watchers_count'};
$return .= '<br/><span>watchers</span>';
$return .= '</div>';
$return .= '</div>';
$return .= '</li>';
}
$return .= '</ol></div>';
return $return;
}
/**
* Loads translation file.
*
* Accessible to other classes to load different language files (admin and
* front-end for example).
*
* @wp-hook init
* @param string $domain
* @since 2012.09.11
* @return void
*/
public function load_language( $domain )
{
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain(
$domain, WP_LANG_DIR . '/featured-link-image/' . $domain . '-' . $locale . '.mo'
);
load_plugin_textdomain( $domain, FALSE, $this->plugin_path . '/languages' );
}
/**
* Checks for oAuth returning token
*
* @return void
*/
private function check_oauth()
{
if( !isset( $_GET['page'] ) || 'my-github-connect' != $_GET['page'] )
return;
if( $_SERVER["REQUEST_METHOD"] == "GET" && isset( $_GET['code'] ) )
{
$redirect = admin_url( $this->plugin_admin_url );
$api_key = get_option( 'GITHUB_API_KEY' );
$api_secret = get_option( 'GITHUB_API_SECRET_KEY' );
$args = array(
'method' => 'POST',
'httpversion' => '1.1',
'blocking' => true,
'headers' => array(
'Accept' => 'application/json'
),
'body' => array(
'code' => $_GET['code'],
'redirect_uri' => $redirect,
'client_id' => $api_key,
'client_secret' => $api_secret
)
);
add_filter( 'https_ssl_verify', '__return_false' );
$response = wp_remote_post( 'https://github.com/login/oauth/access_token', $args );
$keys = json_decode( $response['body'] );
if( $keys )
update_option( 'GITHUB_AUTHENTICATION_TOKEN', $keys->access_token );
wp_redirect( admin_url( $this->plugin_admin_url ) );
exit;
}
}
}
div.github {
/*width: 270px;*/
}
div.github h2{
text-align: center;
}
div.github div.counts {
text-transform: uppercase;
text-align: center;
}
div.github div.counts div {
display: inline-block;
padding: 5px;
}
div.github ol.repos {
list-style: none;
margin: 0px;
padding: 0px;
}
div.github ol.repos li{
width: 120px;
height: 120px;
text-align: center;
font-size: 12px;
border: 1px dashed #000000;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
background-color: #e1e1e1;
position: relative;
display: inline-table;
margin: 5px;
background-clip: padding-box;
padding: 0 20px
}
div.github ol.repos li h3{
padding: 0px;
margin: 5px 0px;
}
div.github ol.repos li a, div.github ol.repos li a:hover, div.github ol.repos li a:visited {
text-align: center;
text-decoration: none;
}
div.github ol.repos li > div{
border-top: 1px dashed #000000;
position: absolute;
bottom: 5px;
width: 100%;
}
div.github ol.repos li div div{
display: inline-block;
font-size: 25px;
width: 50%;
margin-top: 8px;
}
div.github ol.repos li div div span{
font-size: 8px;
text-transform: uppercase;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment