| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
Plugin Name: Github Embed |
| 5 |
Plugin URI: http://www.leewillis.co.uk/wordpress-plugins |
| 6 |
Description: Paste the URL to a Github project into your posts or pages, and have the project information pulled in and displayed automatically |
| 7 |
Version: 1.0 |
| 8 |
Author: Lee Willis |
| 9 |
Author URI: http://www.leewillis.co.uk/ |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Copyright (c) 2013 Lee Willis. All rights reserved. |
| 14 |
* |
| 15 |
* Released under the GPL license |
| 16 |
* http://www.opensource.org/licenses/gpl-license.php |
| 17 |
* |
| 18 |
* This is an add-on for WordPress |
| 19 |
* http://wordpress.org/ |
| 20 |
* |
| 21 |
* ********************************************************************** |
| 22 |
* This program is free software; you can redistribute it and/or modify |
| 23 |
* it under the terms of the GNU General Public License as published by |
| 24 |
* the Free Software Foundation; either version 2 of the License, or |
| 25 |
* (at your option) any later version. |
| 26 |
* |
| 27 |
* This program is distributed in the hope that it will be useful, |
| 28 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 30 |
* GNU General Public License for more details. |
| 31 |
* ********************************************************************** |
| 32 |
*/ |
| 33 |
|
| 34 |
class github_embed { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor. Registers hooks and filters |
| 40 |
*/ |
| 41 |
public function __construct() { |
| 42 |
|
| 43 |
add_action ( 'init', array ( $this, 'register_oembed_handler' ) ); |
| 44 |
add_action ( 'init', array ( $this, 'maybe_handle_oembed' ) ); |
| 45 |
|
| 46 |
// @TODO i18n |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Register the oEmbed provider, and point it at a local endpoint since github |
| 54 |
* doesn't directly support oEmbed yet. Our local endpoint will use the github |
| 55 |
* API to fulfil the request. |
| 56 |
* @param array $providers The current list of providers |
| 57 |
* @return array The list, with our new provider added |
| 58 |
*/ |
| 59 |
public function register_oembed_handler() { |
| 60 |
|
| 61 |
$oembed_url = home_url (); |
| 62 |
$key = $this->get_key(); |
| 63 |
$oembed_url = add_query_arg ( array ( 'github_oembed' => $key ), $oembed_url); |
| 64 |
wp_oembed_add_provider ( '#https?://github.com/.*#i', $oembed_url, true ); |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Generate a unique key that can be used on our requests to stop others |
| 72 |
* hijacking our internal oEmbed API |
| 73 |
* @return string The site key |
| 74 |
*/ |
| 75 |
private function get_key() { |
| 76 |
|
| 77 |
$key = get_option ( 'github_oembed_key' ); |
| 78 |
|
| 79 |
if ( ! $key ) { |
| 80 |
$key = md5 ( time() . rand ( 0,65535 ) ); |
| 81 |
add_option ( 'github_oembed_key', $key, '', 'yes' ); |
| 82 |
} |
| 83 |
|
| 84 |
return $key; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
/** |
| 91 |
* Check whether this is an oembed request, handle if it is |
| 92 |
* Ignore it if not. |
| 93 |
* Insert rant here about WP's lack of a front-end AJAX handler. |
| 94 |
*/ |
| 95 |
public function maybe_handle_oembed() { |
| 96 |
|
| 97 |
if ( isset ( $_GET['github_oembed'] ) ) { |
| 98 |
return $this->handle_oembed(); |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* Handle an oembed request |
| 107 |
*/ |
| 108 |
public function handle_oembed() { |
| 109 |
|
| 110 |
// Check this request is valid |
| 111 |
if ( $_GET['github_oembed'] != $this->get_key() ) { |
| 112 |
header ( 'HTTP/1.0 403 Forbidden' ); |
| 113 |
die ( 'Sad Octocat is sad.' ); |
| 114 |
} |
| 115 |
|
| 116 |
// Check we have the required information |
| 117 |
$url = isset ( $_REQUEST['url'] ) ? $_REQUEST['url'] : null; |
| 118 |
$format = isset ( $_REQUEST['format'] ) ? $_REQUEST['format'] : null; |
| 119 |
|
| 120 |
if ( ! empty ( $format ) && $format != 'json' ) { |
| 121 |
header ( 'HTTP/1.0 501 Not implemented' ); |
| 122 |
die ( 'This octocat only does json' ); |
| 123 |
} |
| 124 |
|
| 125 |
if ( ! $url ) { |
| 126 |
header ( 'HTTP/1.0 404 Not Found' ); |
| 127 |
die ( 'Octocat is lost, and afraid' ); |
| 128 |
} |
| 129 |
|
| 130 |
if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { |
| 131 |
|
| 132 |
$this->oembed_github_repo ( $matches[1], $matches[2] ); |
| 133 |
|
| 134 |
} elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) { |
| 135 |
|
| 136 |
$this->oembed_github_author ( $matches[1] ); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
/** |
| 145 |
* Retrieve the information from github for a repo, and |
| 146 |
* output it as an oembed response |
| 147 |
*/ |
| 148 |
private function oembed_github_repo ( $owner, $repository ) { |
| 149 |
|
| 150 |
$repository = trim ( $repository, '/' ); |
| 151 |
|
| 152 |
$results = wp_remote_get( "https://api.github.com/repos/$owner/$repository", $args = array ( |
| 153 |
'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); |
| 154 |
|
| 155 |
if ( is_wp_error( $results ) || |
| 156 |
! isset ( $results['response']['code'] ) || |
| 157 |
$results['response']['code'] != '200' ) { |
| 158 |
header ( 'HTTP/1.0 404 Not Found' ); |
| 159 |
die ( 'Octocat is lost, and afraid' ); |
| 160 |
} |
| 161 |
|
| 162 |
$repo = json_decode ( $results['body'] ); |
| 163 |
|
| 164 |
$results = wp_remote_get( "https://api.github.com/repos/$owner/$repository/commits", $args = array ( |
| 165 |
'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); |
| 166 |
|
| 167 |
if ( is_wp_error( $results ) || |
| 168 |
! isset ( $results['response']['code'] ) || |
| 169 |
$results['response']['code'] != '200' ) { |
| 170 |
header ( 'HTTP/1.0 404 Not Found' ); |
| 171 |
die ( 'Octocat is lost, and afraid' ); |
| 172 |
} |
| 173 |
|
| 174 |
$commits = json_decode ( $results['body'] ); |
| 175 |
|
| 176 |
$response = new stdClass(); |
| 177 |
$response->type = 'rich'; |
| 178 |
$response->width = '10'; |
| 179 |
$response->height = '10'; |
| 180 |
$response->version = '1.0'; |
| 181 |
$response->title = $repo->description; |
| 182 |
|
| 183 |
// @TODO This should all be templated |
| 184 |
$response->html = '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; |
| 185 |
$response->html .= esc_html($repo->html_url)."<br/>"; |
| 186 |
$response->html .= esc_html($repo->forks_count)." forks.<br/>"; |
| 187 |
$response->html .= esc_html($repo->open_issues_count)." open issues.<br/>"; |
| 188 |
|
| 189 |
if ( count ( $commits ) ) { |
| 190 |
|
| 191 |
$cnt = 0; |
| 192 |
$response->html .= 'Recent commits:'; |
| 193 |
$response->html .= '<ul class="github_commits">'; |
| 194 |
|
| 195 |
foreach ( $commits as $commit ) { |
| 196 |
|
| 197 |
if ($cnt > 4) |
| 198 |
break; |
| 199 |
|
| 200 |
$response->html .= '<li class="github_commit">'; |
| 201 |
$response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, "; |
| 202 |
$response->html .= esc_html($commit->commit->committer->name); |
| 203 |
$response->html .= '</li>'; |
| 204 |
|
| 205 |
$cnt++; |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
$response->html .= '</ul>'; |
| 210 |
|
| 211 |
} |
| 212 |
$response->html .= '</p>'; |
| 213 |
|
| 214 |
header ( 'Content-Type: application/json' ); |
| 215 |
echo json_encode ( $response ); |
| 216 |
die(); |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Retrieve the information from github for an author, and output |
| 224 |
* it as an oembed response |
| 225 |
*/ |
| 226 |
private function oembed_github_author ( $owner ) { |
| 227 |
|
| 228 |
$owner = trim ( $owner, '/' ); |
| 229 |
|
| 230 |
$results = wp_remote_get( "https://api.github.com/users/$owner", $args = array ( |
| 231 |
'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); |
| 232 |
|
| 233 |
if ( is_wp_error( $results ) || |
| 234 |
! isset ( $results['response']['code'] ) || |
| 235 |
$results['response']['code'] != '200' ) { |
| 236 |
header ( 'HTTP/1.0 404 Not Found' ); |
| 237 |
die ( 'Octocat is lost, and afraid' ); |
| 238 |
} |
| 239 |
|
| 240 |
$owner_info = json_decode ( $results['body'] ); |
| 241 |
|
| 242 |
$response = new stdClass(); |
| 243 |
$response->type = 'rich'; |
| 244 |
$response->width = '10'; |
| 245 |
$response->height = '10'; |
| 246 |
$response->version = '1.0'; |
| 247 |
$response->title = $owner_info->name; |
| 248 |
|
| 249 |
// @TODO This should all be templated |
| 250 |
$response->html = '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>"; |
| 251 |
$response->html .= esc_html($owner_info->public_repos).' repositories, '; |
| 252 |
$response->html .= esc_html($owner_info->followers).' followers.</p>'; |
| 253 |
|
| 254 |
header ( 'Content-Type: application/json' ); |
| 255 |
echo json_encode ( $response ); |
| 256 |
die(); |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
$github_embed = new github_embed(); |