Browse Source

Whitespace cleanup

widget
Lee Willis 8 years ago
parent
commit
68c3aa9c78
  1. 241
      github-embed.php

241
github-embed.php

@ -37,81 +37,58 @@ Author URI: http://www.leewillis.co.uk/
* class to retrieve the information from the GitHub API. * class to retrieve the information from the GitHub API.
* @uses class github_api * @uses class github_api
*/ */
class github_embed {
class github_embed {
private $api; private $api;
/** /**
* Constructor. Registers hooks and filters * Constructor. Registers hooks and filters
* @param class $api An instance of the github_api classs * @param class $api An instance of the github_api classs
*/ */
public function __construct ( $api ) { public function __construct( $api ) {
$this->api = $api; $this->api = $api;
add_action( 'init', array( $this, 'register_oembed_handler' ) );
add_action( 'init', array ( $this, 'register_oembed_handler' ) );
add_action( 'init', array( $this, 'maybe_handle_oembed' ) ); add_action( 'init', array( $this, 'maybe_handle_oembed' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
add_action( 'admin_init', array( $this, 'schedule_expiry' ) ); add_action( 'admin_init', array( $this, 'schedule_expiry' ) );
add_action( 'github_embed_cron', array( $this, 'cron' ) ); add_action( 'github_embed_cron', array( $this, 'cron' ) );
// @TODO i18n // @TODO i18n
} }
/** /**
* Make sure we have a scheduled event set to clear down the oEmbed cache until * Make sure we have a scheduled event set to clear down the oEmbed cache until
* WordPress supports cache_age in oEmbed responses. * WordPress supports cache_age in oEmbed responses.
*/ */
function schedule_expiry() { function schedule_expiry() {
if ( ! wp_next_scheduled( 'github_embed_cron' ) ) {
if( ! wp_next_scheduled( 'github_embed_cron' ) ) { $frequency = apply_filters( 'github_embed_cache_frequency', 'daily' );
$frequency = apply_filters ( 'github_embed_cache_frequency', 'daily' );
wp_schedule_event( time(), $frequency, 'github_embed_cron' ); wp_schedule_event( time(), $frequency, 'github_embed_cron' );
} }
} }
/** /**
* Expire old oEmbeds. * Expire old oEmbeds.
* Note: This is a bit sledgehammer-to-crack-a-nut hence why I'm only running it * Note: This is a bit sledgehammer-to-crack-a-nut hence why I'm only running it
* daily. Ideally WP should honour cache_age in oEmbed responses properly * daily. Ideally WP should honour cache_age in oEmbed responses properly
*/ */
function cron() { function cron() {
global $wpdb, $table_prefix; global $wpdb, $table_prefix;
$sql = "DELETE $sql = "DELETE
FROM {$table_prefix}postmeta FROM {$table_prefix}postmeta
WHERE meta_key LIKE '_oembed_%'"; WHERE meta_key LIKE '_oembed_%'";
$results = $wpdb->get_results( $sql );
$results = $wpdb->get_results ( $sql );
} }
/** /**
* Enqueue the frontend CSS * Enqueue the frontend CSS
* @return void * @return void
*/ */
function enqueue_styles() { function enqueue_styles() {
wp_register_style( 'github-embed', plugins_url( basename( dirname( __FILE__ ) ) . '/css/github-embed.css' ) );
wp_register_style ( 'github-embed', plugins_url(basename(dirname(__FILE__)).'/css/github-embed.css' ) ); wp_enqueue_style( 'github-embed' );
wp_enqueue_style ( 'github-embed' );
} }
/** /**
* Register the oEmbed provider, and point it at a local endpoint since github * Register the oEmbed provider, and point it at a local endpoint since github
* doesn't directly support oEmbed yet. Our local endpoint will use the github * doesn't directly support oEmbed yet. Our local endpoint will use the github
@ -120,123 +97,93 @@ class github_embed {
* @return array The list, with our new provider added * @return array The list, with our new provider added
*/ */
public function register_oembed_handler() { public function register_oembed_handler() {
$oembed_url = home_url(); $oembed_url = home_url();
$key = $this->get_key(); $key = $this->get_key();
$oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url ); $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url );
wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true ); wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true );
} }
/** /**
* Generate a unique key that can be used on our requests to stop others * Generate a unique key that can be used on our requests to stop others
* hijacking our internal oEmbed API * hijacking our internal oEmbed API
* @return string The site key * @return string The site key
*/ */
private function get_key() { private function get_key() {
$key = get_option( 'github_oembed_key' );
$key = get_option ( 'github_oembed_key' );
if ( ! $key ) { if ( ! $key ) {
$key = md5 ( time() . rand ( 0,65535 ) ); $key = md5( time() . rand( 0, 65535 ) );
add_option ( 'github_oembed_key', $key, '', 'yes' ); add_option( 'github_oembed_key', $key, '', 'yes' );
} }
return $key; return $key;
} }
/** /**
* Check whether this is an oembed request, handle if it is * Check whether this is an oembed request, handle if it is
* Ignore it if not. * Ignore it if not.
* Insert rant here about WP's lack of a front-end AJAX handler. * Insert rant here about WP's lack of a front-end AJAX handler.
*/ */
public function maybe_handle_oembed() { public function maybe_handle_oembed() {
if ( isset( $_GET['github_oembed'] ) ) {
if ( isset ( $_GET['github_oembed'] ) ) {
return $this->handle_oembed(); return $this->handle_oembed();
} }
} }
/** /**
* Handle an oembed request * Handle an oembed request
*/ */
public function handle_oembed() { public function handle_oembed() {
// Check this request is valid // Check this request is valid
if ( $_GET['github_oembed'] != $this->get_key() ) { if ( $_GET['github_oembed'] !== $this->get_key() ) {
header ( 'HTTP/1.0 403 Forbidden' ); header( 'HTTP/1.0 403 Forbidden' );
die ( 'Sad Octocat is sad.' ); die( 'Sad Octocat is sad.' );
} }
// Check we have the required information // Check we have the required information
$url = isset ( $_REQUEST['url'] ) ? $_REQUEST['url'] : null; $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
$format = isset ( $_REQUEST['format'] ) ? $_REQUEST['format'] : null; $format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
if ( ! empty ( $format ) && $format != 'json' ) { if ( ! empty( $format ) && 'json' !== $format ) {
header ( 'HTTP/1.0 501 Not implemented' ); header( 'HTTP/1.0 501 Not implemented' );
die ( 'This octocat only does json' ); die( 'This octocat only does json' );
} }
if ( ! $url ) { if ( ! $url ) {
header ( 'HTTP/1.0 404 Not Found' ); header( 'HTTP/1.0 404 Not Found' );
die ( 'Octocat is lost, and afraid' ); die( 'Octocat is lost, and afraid' );
} }
// Issues / Milestones // Issues / Milestones
if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { if ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
$this->oembed_github_repo_contributors( $matches[1], $matches[2] );
$this->oembed_github_repo_contributors ( $matches[1], $matches[2] ); } elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
if ( preg_match( '#issues.?milestone=([0-9]*)#i', $url, $milestones ) ) {
} elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
if ( preg_match ( '#issues.?milestone=([0-9]*)#i', $url, $milestones ) ) {
$milestone = $milestones[1]; $milestone = $milestones[1];
} else { } else {
$milestone = null; $milestone = null;
} }
if ( $milestone ) { if ( $milestone ) {
$this->oembed_github_repo_milestone_summary ( $matches[1], $matches[2], $milestone ); $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $milestone );
} }
} elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/milestone/([0-9]*)$#i', $url, $matches ) ) {
// New style milestone URL, e.g. https://github.com/example/example/milestone/1 // New style milestone URL, e.g. https://github.com/example/example/milestone/1.
} elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/milestone/([0-9]*)$#i', $url, $matches ) ) {
$this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $matches[3] ); $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $matches[3] );
} elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
// Repository // Repository.
} elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { $this->oembed_github_repo( $matches[1], $matches[2] );
} elseif ( preg_match( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) {
$this->oembed_github_repo ( $matches[1], $matches[2] ); // User.
$this->oembed_github_author( $matches[1] );
// User
} elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) {
$this->oembed_github_author ( $matches[1] );
} }
} }
/** /**
* Retrieve a list of contributors for a project * Retrieve a list of contributors for a project
* @param string $owner The owner of the repository * @param string $owner The owner of the repository
* @param string $repository The repository name * @param string $repository The repository name
*/ */
private function oembed_github_repo_contributors ( $owner, $repository ) { private function oembed_github_repo_contributors( $owner, $repository ) {
$repo = $this->api->get_repo( $owner, $repository );
$repo = $this->api->get_repo ( $owner, $repository ); $contributors = $this->api->get_repo_contributors( $owner, $repository );
$contributors = $this->api->get_repo_contributors ( $owner, $repository );
$response = new stdClass(); $response = new stdClass();
$response->type = 'rich'; $response->type = 'rich';
@ -245,46 +192,37 @@ class github_embed {
$response->version = '1.0'; $response->version = '1.0';
$response->title = $repo->description; $response->title = $repo->description;
$gravatar_size = apply_filters ( 'github_oembed_gravatar_size', 64 ); $gravatar_size = apply_filters( 'github_oembed_gravatar_size', 64 );
// @TODO This should all be templated // @TODO This should all be templated
$response->html = '<div class="github-embed github-embed-repo-contributors">'; $response->html = '<div class="github-embed github-embed-repo-contributors">';
$response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; $response->html .= '<p><a href="' . esc_attr( $repo->html_url ) . '" target="_blank">';
$response->html .= '<strong>' . esc_html( $repo->description ) . '</strong></a><br/>';
$response->html .= '<span class="github-heading">Contributors: </span>'; $response->html .= '<span class="github-heading">Contributors: </span>';
$response->html .= '<ul class="github-repo-contributors">'; $response->html .= '<ul class="github-repo-contributors">';
foreach ( $contributors as $contributor ) { foreach ( $contributors as $contributor ) {
$response->html .= '<li class="github-repo-contributor">'; $response->html .= '<li class="github-repo-contributor">';
$response->html .= '<img class="github-repo-contributor-avatar" src="'; $response->html .= '<img class="github-repo-contributor-avatar" src="';
$response->html .= esc_url(add_query_arg(array('s'=>$gravatar_size), $contributor->author->avatar_url)); $response->html .= esc_url( add_query_arg( array( 's' => $gravatar_size ), $contributor->author->avatar_url ) );
$response->html .= '" alt="Picture of '.esc_attr($contributor->author->login).'">'; $response->html .= '" alt="Picture of ' . esc_attr( $contributor->author->login ) . '">';
$response->html .= '<span class="github-repo-contributor-login">'; $response->html .= '<span class="github-repo-contributor-login">';
$response->html .= '<a href="https://github.com/'.esc_attr($contributor->author->login).'">'.esc_attr($contributor->author->login).'</a></span>'; $response->html .= '<a href="https://github.com/' . esc_attr( $contributor->author->login ) . '">' . esc_attr( $contributor->author->login ) . '</a></span>';
} }
$response->html .= '</ul>'; $response->html .= '</ul>';
$response->html .= '<div style="clear: both;"></div>'; $response->html .= '<div style="clear: both;"></div>';
$response->html .= '</div>'; $response->html .= '</div>';
header( 'Content-Type: application/json' );
header ( 'Content-Type: application/json' ); echo json_encode( $response );
echo json_encode ( $response );
die(); die();
} }
/** /**
* Retrieve the summary information for a repo's milestone, and * Retrieve the summary information for a repo's milestone, and
* output it as an oembed response * output it as an oembed response
*/ */
private function oembed_github_repo_milestone_summary ( $owner, $repository, $milestone ) { private function oembed_github_repo_milestone_summary( $owner, $repository, $milestone ) {
$repo = $this->api->get_repo( $owner, $repository );
$repo = $this->api->get_repo ( $owner, $repository ); $summary = $this->api->get_repo_milestone_summary( $owner, $repository, $milestone );
$summary = $this->api->get_repo_milestone_summary ( $owner, $repository, $milestone );
$response = new stdClass(); $response = new stdClass();
$response->type = 'rich'; $response->type = 'rich';
@ -295,42 +233,39 @@ class github_embed {
// @TODO This should all be templated // @TODO This should all be templated
$response->html = '<div class="github-embed github-embed-milestone-summary">'; $response->html = '<div class="github-embed github-embed-milestone-summary">';
$response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; $response->html .= '<p><a href="' . esc_attr( $repo->html_url ) . '" target="_blank"><strong>' . esc_html( $repo->description ) . '</strong></a><br/>';
$response->html .= '<span class="github-heading">Milestone: </span>'; $response->html .= '<span class="github-heading">Milestone: </span>';
$response->html .= '<span class="github-milestone-title">'.esc_html($summary->title)."</span><br>"; $response->html .= '<span class="github-milestone-title">' . esc_html( $summary->title ) . '</span><br>';
$response->html .= '<span class="github-heading">Issues: </span>'; $response->html .= '<span class="github-heading">Issues: </span>';
$response->html .= '<span class="github-milestone-issues">'; $response->html .= '<span class="github-milestone-issues">';
$response->html .= esc_html ( number_format_i18n ( $summary->open_issues ) )." open, "; $response->html .= esc_html( number_format_i18n( $summary->open_issues ) ) . ' open, ';
$response->html .= esc_html ( number_format_i18n ( $summary->closed_issues ) )." closed.</span><br>"; $response->html .= esc_html( number_format_i18n( $summary->closed_issues ) ) . ' closed.</span><br>';
if ( ! empty ( $summary->due_on ) ) { if ( ! empty( $summary->due_on ) ) {
$response->html .= '<span class="github-heading">Due: </span>'; $response->html .= '<span class="github-heading">Due: </span>';
$due_date = date_format ( date_create ( $summary->due_on ), 'jS F Y' ); $due_date = date_format( date_create( $summary->due_on ), 'jS F Y' );
$response->html .= '<span class="github-milestone-due-date">'.esc_html($due_date).'</span><br>'; $response->html .= '<span class="github-milestone-due-date">' . esc_html( $due_date ) . '</span><br>';
} }
$response->html .= '<p class="github-milestone-description">'.nl2br(esc_html($summary->description))."</p><br>"; $response->html .= '<p class="github-milestone-description">' . nl2br( esc_html( $summary->description ) ) . '</p><br>';
$response->html .= '</div>'; $response->html .= '</div>';
error_log( basename( __FILE__ ) . ': (' . __LINE__ . ') : response is ' . print_r( $response, 1 ) ); error_log( basename( __FILE__ ) . ': (' . __LINE__ . ') : response is ' . print_r( $response, 1 ) );
header ( 'Content-Type: application/json' ); header( 'Content-Type: application/json' );
echo json_encode ( $response ); echo json_encode( $response );
die(); die();
} }
/** /**
* Retrieve the information from github for a repo, and * Retrieve the information from github for a repo, and
* output it as an oembed response * output it as an oembed response
*/ */
private function oembed_github_repo ( $owner, $repository ) { private function oembed_github_repo ( $owner, $repository ) {
$repo = $this->api->get_repo( $owner, $repository );
$repo = $this->api->get_repo ( $owner, $repository ); $commits =$this->api->get_repo_commits( $owner, $repository );
$commits =$this->api->get_repo_commits ( $owner, $repository );
$response = new stdClass(); $response = new stdClass();
$response->type = 'rich'; $response->type = 'rich';
@ -341,53 +276,42 @@ class github_embed {
// @TODO This should all be templated // @TODO This should all be templated
$response->html = '<div class="github-embed github-embed-repository">'; $response->html = '<div class="github-embed github-embed-repository">';
$response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; $response->html .= '<p><a href="' . esc_attr( $repo->html_url ) . '" target="_blank"><strong>' . esc_html( $repo->description ) . '</strong></a><br/>';
$response->html .= '<a href="'.esc_attr($repo->html_url).'" target="_blank">'.esc_html($repo->html_url)."</a><br/>"; $response->html .= '<a href="' . esc_attr( $repo->html_url ) . '" target="_blank">' . esc_html( $repo->html_url ) . '</a><br/>';
$response->html .= '<a href="'.esc_attr($repo->html_url . '/network') . '" target="_blank">'.esc_html ( number_format_i18n ( $repo->forks_count ) ) . "</a> forks.<br/>"; $response->html .= '<a href="' . esc_attr( $repo->html_url . '/network' ) . '" target="_blank">' . esc_html( number_format_i18n( $repo->forks_count ) ) . '</a> forks.<br/>';
$response->html .= '<a href="'.esc_attr($repo->html_url . '/stargazers') . '" target="_blank">'.esc_html ( number_format_i18n ( $repo->stargazers_count ) ) . "</a> stars.<br/>"; $response->html .= '<a href="' . esc_attr( $repo->html_url . '/stargazers' ) . '" target="_blank">' . esc_html( number_format_i18n( $repo->stargazers_count ) ) . '</a> stars.<br/>';
$response->html .= '<a href="'.esc_attr($repo->html_url . '/issues') . '" target="_blank">'.esc_html ( number_format_i18n ( $repo->open_issues_count ) ) . "</a> open issues.<br/>"; $response->html .= '<a href="' . esc_attr( $repo->html_url . '/issues' ) . '" target="_blank">' . esc_html( number_format_i18n( $repo->open_issues_count ) ) . '</a> open issues.<br/>';
if ( count ( $commits ) ) {
if ( count( $commits ) ) {
$cnt = 0; $cnt = 0;
$response->html .= 'Recent commits:'; $response->html .= 'Recent commits:';
$response->html .= '<ul class="github_commits">'; $response->html .= '<ul class="github_commits">';
foreach ( $commits as $commit ) { foreach ( $commits as $commit ) {
if ( $cnt > 4 ) {
if ($cnt > 4)
break; break;
}
$response->html .= '<li class="github_commit">'; $response->html .= '<li class="github_commit">';
$response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, "; $response->html .= '<a href="https://github.com/' . $owner . '/' . $repository . '/commit/' . esc_attr( $commit->sha ) . '" target="_blank">' . esc_html( $commit->commit->message ) . '</a>, ';
$response->html .= esc_html($commit->commit->committer->name); $response->html .= esc_html( $commit->commit->committer->name );
$response->html .= '</li>'; $response->html .= '</li>';
$cnt++; $cnt++;
} }
$response->html .= '</ul>'; $response->html .= '</ul>';
} }
$response->html .= '</p>'; $response->html .= '</p>';
$response->html .= '</div>'; $response->html .= '</div>';
header( 'Content-Type: application/json' );
header ( 'Content-Type: application/json' ); echo json_encode( $response );
echo json_encode ( $response );
die(); die();
} }
/** /**
* Retrieve the information from github for an author, and output * Retrieve the information from github for an author, and output
* it as an oembed response * it as an oembed response
*/ */
private function oembed_github_author ( $owner ) { private function oembed_github_author ( $owner ) {
$owner_info = $this->api->get_user ( $owner ); $owner_info = $this->api->get_user( $owner );
$response = new stdClass(); $response = new stdClass();
$response->type = 'rich'; $response->type = 'rich';
@ -398,20 +322,17 @@ class github_embed {
// @TODO This should all be templated // @TODO This should all be templated
$response->html = '<div class="github-embed github-embed-user">'; $response->html = '<div class="github-embed github-embed-user">';
$response->html .= '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>"; $response->html .= '<p><a href="https://github.com/' . esc_attr( $owner ) . '" target="_blank"><strong>' . esc_html( $owner ) . '</strong></a><br/>';
$response->html .= esc_html ( number_format_i18n ( $owner_info->public_repos ) ).' repositories, '; $response->html .= esc_html( number_format_i18n( $owner_info->public_repos ) ) . ' repositories, ';
$response->html .= esc_html ( number_format_i18n ( $owner_info->followers ) ).' followers.</p>'; $response->html .= esc_html( number_format_i18n( $owner_info->followers ) ) . ' followers.</p>';
$response->html .= '</div>'; $response->html .= '</div>';
header( 'Content-Type: application/json' );
header ( 'Content-Type: application/json' ); echo json_encode( $response );
echo json_encode ( $response );
die(); die();
} }
} }
require_once ( 'github-api.php' ); require_once( 'github-api.php' );
$github_api = new github_api(); $github_api = new github_api();
$github_embed = new github_embed ( $github_api ); $github_embed = new github_embed( $github_api );

Loading…
Cancel
Save