| @@ -1,19 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | /* |
| 3 | 4 | Plugin Name: Github Embed |
| 4 | -Plugin URI: https://wordpress.org/plugins/github-embed/ | |
| 5 | +Plugin URI: http://www.leewillis.co.uk/wordpress-plugins | |
| 5 | 6 | Description: Paste the URL to a Github project into your posts or pages, and have the project information pulled in and displayed automatically |
| 6 | -Version: 2.2.0 | |
| 7 | -Author: Ademti Software Ltd. | |
| 8 | -Author URI: https://www.ademti-software.co.uk/ | |
| 7 | +Version: 1.4 | |
| 8 | +Author: Lee Willis | |
| 9 | +Author URI: http://www.leewillis.co.uk/ | |
| 9 | 10 | */ |
| 10 | 11 | |
| 11 | 12 | /** |
| 12 | - * Copyright (c) 2013-2025 Ademti Software. All rights reserved. | |
| 13 | + * Copyright (c) 2013 Lee Willis. All rights reserved. | |
| 13 | 14 | * |
| 14 | - * Released under the GPL license v2 | |
| 15 | - * https://www.gnu.org/licenses/gpl-2.0.en.html | |
| 15 | + * Released under the GPL license | |
| 16 | + * http://www.opensource.org/licenses/gpl-license.php | |
| 16 | 17 | * |
| 17 | 18 | * This is an add-on for WordPress |
| 18 | 19 | * http://wordpress.org/ |
| 19 | 20 | * |
| @@ -37,74 +38,99 @@ | ||
| 37 | 38 | * @uses class github_api |
| 38 | 39 | */ |
| 39 | 40 | class github_embed { |
| 40 | 41 | |
| 42 | + | |
| 43 | + | |
| 41 | 44 | private $api; |
| 42 | 45 | |
| 46 | + | |
| 47 | + | |
| 43 | 48 | /** |
| 44 | 49 | * Constructor. Registers hooks and filters |
| 45 | - * | |
| 46 | 50 | * @param class $api An instance of the github_api classs |
| 47 | 51 | */ |
| 48 | - public function __construct( $api ) { | |
| 52 | + public function __construct ( $api ) { | |
| 53 | + | |
| 49 | 54 | $this->api = $api; |
| 50 | - add_action( 'init', array( $this, 'register_oembed_handler' ) ); | |
| 51 | - add_action( 'init', array( $this, 'maybe_handle_oembed' ) ); | |
| 52 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); | |
| 53 | - add_action( 'admin_init', array( $this, 'schedule_expiry' ) ); | |
| 54 | - add_action( 'github_embed_cron', array( $this, 'cron' ) ); | |
| 55 | + | |
| 56 | + add_action ( 'init', array ( $this, 'register_oembed_handler' ) ); | |
| 57 | + add_action ( 'init', array ( $this, 'maybe_handle_oembed' ) ); | |
| 58 | + add_action ( 'wp_enqueue_scripts', array ( $this, 'enqueue_styles' ) ); | |
| 59 | + add_action ( 'admin_init', array ( $this, 'schedule_expiry' ) ); | |
| 60 | + add_action ( 'github_embed_cron', array ( $this, 'cron' ) ); | |
| 61 | + | |
| 55 | 62 | // @TODO i18n |
| 63 | + | |
| 56 | 64 | } |
| 57 | 65 | |
| 58 | - /** | |
| 59 | - * Make sure we have a scheduled event set to clear down the oEmbed cache until | |
| 60 | - * WordPress supports cache_age in oEmbed responses. | |
| 61 | - */ | |
| 62 | - public function schedule_expiry() { | |
| 63 | - if ( ! wp_next_scheduled( 'github_embed_cron' ) ) { | |
| 64 | - $frequency = apply_filters( 'github_embed_cache_frequency', 'daily' ); | |
| 65 | - wp_schedule_event( time(), $frequency, 'github_embed_cron' ); | |
| 66 | - } | |
| 67 | - } | |
| 68 | 66 | |
| 69 | - /** | |
| 70 | - * Expire old oEmbeds. | |
| 71 | - * Note: This is a bit sledgehammer-to-crack-a-nut hence why I'm only running it | |
| 72 | - * daily. Ideally WP should honour cache_age in oEmbed responses properly | |
| 73 | - */ | |
| 74 | - public function cron() { | |
| 75 | - global $wpdb, $table_prefix; | |
| 76 | - $sql = "DELETE | |
| 77 | - FROM {$table_prefix}postmeta | |
| 78 | - WHERE meta_key LIKE '_oembed_%'"; | |
| 79 | - $results = $wpdb->get_results( $sql ); | |
| 80 | - } | |
| 81 | 67 | |
| 68 | + /** | |
| 69 | + * Make sure we have a scheduled event set to clear down the oEmbed cache until | |
| 70 | + * WordPress supports cache_age in oEmbed responses. | |
| 71 | + */ | |
| 72 | + function schedule_expiry() { | |
| 73 | + | |
| 74 | + if( ! wp_next_scheduled( 'github_embed_cron' ) ) { | |
| 75 | + $frequency = apply_filters ( 'github_embed_cache_frequency', 'daily' ); | |
| 76 | + wp_schedule_event( time(), $frequency, 'github_embed_cron' ); | |
| 77 | + } | |
| 78 | + | |
| 79 | + } | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Expire old oEmbeds. | |
| 85 | + * Note: This is a bit sledgehammer-to-crack-a-nut hence why I'm only running it | |
| 86 | + * daily. Ideally WP should honour cache_age in oEmbed responses properly | |
| 87 | + */ | |
| 88 | + function cron() { | |
| 89 | + | |
| 90 | + global $wpdb, $table_prefix; | |
| 91 | + | |
| 92 | + $sql = "DELETE | |
| 93 | + FROM {$table_prefix}postmeta | |
| 94 | + WHERE meta_key LIKE '_oembed_%'"; | |
| 95 | + | |
| 96 | + $results = $wpdb->get_results ( $sql ); | |
| 97 | + | |
| 98 | + } | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 82 | 102 | /** |
| 83 | 103 | * Enqueue the frontend CSS |
| 84 | 104 | * @return void |
| 85 | 105 | */ |
| 86 | - public function enqueue_styles() { | |
| 87 | - wp_register_style( 'github-embed', plugins_url( basename( dirname( __FILE__ ) ) . '/css/github-embed.css' ) ); | |
| 88 | - wp_enqueue_style( 'github-embed' ); | |
| 106 | + function enqueue_styles() { | |
| 107 | + | |
| 108 | + wp_register_style ( 'github-embed', plugins_url(basename(dirname(__FILE__)).'/css/github-embed.css' ) ); | |
| 109 | + wp_enqueue_style ( 'github-embed' ); | |
| 110 | + | |
| 89 | 111 | } |
| 90 | 112 | |
| 113 | + | |
| 114 | + | |
| 91 | 115 | /** |
| 92 | 116 | * Register the oEmbed provider, and point it at a local endpoint since github |
| 93 | 117 | * doesn't directly support oEmbed yet. Our local endpoint will use the github |
| 94 | 118 | * API to fulfil the request. |
| 95 | - * | |
| 96 | - * @param array $providers The current list of providers | |
| 97 | - * | |
| 119 | + * @param array $providers The current list of providers | |
| 98 | 120 | * @return array The list, with our new provider added |
| 99 | 121 | */ |
| 100 | 122 | public function register_oembed_handler() { |
| 101 | - $oembed_url = home_url(); | |
| 102 | - $key = $this->get_key(); | |
| 103 | - $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url ); | |
| 104 | - wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true ); | |
| 123 | + | |
| 124 | + $oembed_url = home_url (); | |
| 125 | + $key = $this->get_key(); | |
| 126 | + $oembed_url = add_query_arg ( array ( 'github_oembed' => $key ), $oembed_url); | |
| 127 | + wp_oembed_add_provider ( '#https?://github.com/.*#i', $oembed_url, true ); | |
| 128 | + | |
| 105 | 129 | } |
| 106 | 130 | |
| 131 | + | |
| 132 | + | |
| 107 | 133 | /** |
| 108 | 134 | * Generate a unique key that can be used on our requests to stop others |
| 109 | 135 | * hijacking our internal oEmbed API |
| 110 | 136 | * @return string The site key |
| @@ -109,17 +135,22 @@ | ||
| 109 | 135 | * hijacking our internal oEmbed API |
| 110 | 136 | * @return string The site key |
| 111 | 137 | */ |
| 112 | 138 | private function get_key() { |
| 113 | - $key = get_option( 'github_oembed_key' ); | |
| 139 | + | |
| 140 | + $key = get_option ( 'github_oembed_key' ); | |
| 141 | + | |
| 114 | 142 | if ( ! $key ) { |
| 115 | - $key = md5( time() . rand( 0, 65535 ) ); | |
| 116 | - add_option( 'github_oembed_key', $key, '', 'yes' ); | |
| 143 | + $key = md5 ( time() . rand ( 0,65535 ) ); | |
| 144 | + add_option ( 'github_oembed_key', $key, '', 'yes' ); | |
| 117 | 145 | } |
| 118 | 146 | |
| 119 | 147 | return $key; |
| 148 | + | |
| 120 | 149 | } |
| 121 | 150 | |
| 151 | + | |
| 152 | + | |
| 122 | 153 | /** |
| 123 | 154 | * Check whether this is an oembed request, handle if it is |
| 124 | 155 | * Ignore it if not. |
| 125 | 156 | * Insert rant here about WP's lack of a front-end AJAX handler. |
| @@ -124,186 +155,256 @@ | ||
| 124 | 155 | * Ignore it if not. |
| 125 | 156 | * Insert rant here about WP's lack of a front-end AJAX handler. |
| 126 | 157 | */ |
| 127 | 158 | public function maybe_handle_oembed() { |
| 128 | - if ( isset( $_GET['github_oembed'] ) ) { | |
| 159 | + | |
| 160 | + if ( isset ( $_GET['github_oembed'] ) ) { | |
| 129 | 161 | return $this->handle_oembed(); |
| 130 | 162 | } |
| 163 | + | |
| 131 | 164 | } |
| 132 | 165 | |
| 166 | + | |
| 167 | + | |
| 133 | 168 | /** |
| 134 | 169 | * Handle an oembed request |
| 135 | 170 | */ |
| 136 | 171 | public function handle_oembed() { |
| 172 | + | |
| 137 | 173 | // Check this request is valid |
| 138 | - if ( $_GET['github_oembed'] !== $this->get_key() ) { | |
| 139 | - header( 'HTTP/1.0 403 Forbidden' ); | |
| 140 | - die( 'Sad Octocat is sad.' ); | |
| 174 | + if ( $_GET['github_oembed'] != $this->get_key() ) { | |
| 175 | + header ( 'HTTP/1.0 403 Forbidden' ); | |
| 176 | + die ( 'Sad Octocat is sad.' ); | |
| 141 | 177 | } |
| 142 | 178 | |
| 143 | 179 | // Check we have the required information |
| 144 | - $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null; | |
| 145 | - $format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null; | |
| 180 | + $url = isset ( $_REQUEST['url'] ) ? $_REQUEST['url'] : null; | |
| 181 | + $format = isset ( $_REQUEST['format'] ) ? $_REQUEST['format'] : null; | |
| 146 | 182 | |
| 147 | - if ( ! empty( $format ) && 'json' !== $format ) { | |
| 148 | - header( 'HTTP/1.0 501 Not implemented' ); | |
| 149 | - die( 'This octocat only does json' ); | |
| 183 | + if ( ! empty ( $format ) && $format != 'json' ) { | |
| 184 | + header ( 'HTTP/1.0 501 Not implemented' ); | |
| 185 | + die ( 'This octocat only does json' ); | |
| 150 | 186 | } |
| 151 | 187 | |
| 152 | 188 | if ( ! $url ) { |
| 153 | - header( 'HTTP/1.0 404 Not Found' ); | |
| 154 | - die( 'Octocat is lost, and afraid' ); | |
| 189 | + header ( 'HTTP/1.0 404 Not Found' ); | |
| 190 | + die ( 'Octocat is lost, and afraid' ); | |
| 155 | 191 | } |
| 156 | 192 | |
| 157 | 193 | // Issues / Milestones |
| 158 | - if ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty( $matches[2] ) ) { | |
| 159 | - $this->oembed_github_repo_contributors( $matches[1], $matches[2] ); | |
| 160 | - } elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty( $matches[2] ) ) { | |
| 161 | - if ( preg_match( '#issues.?milestone=([0-9]*)#i', $url, $milestones ) ) { | |
| 194 | + if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { | |
| 195 | + | |
| 196 | + $this->oembed_github_repo_contributors ( $matches[1], $matches[2] ); | |
| 197 | + | |
| 198 | + } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { | |
| 199 | + | |
| 200 | + if ( preg_match ( '#issues.?milestone=([0-9]*)#i', $url, $milestones ) ) { | |
| 162 | 201 | $milestone = $milestones[1]; |
| 163 | 202 | } else { |
| 164 | 203 | $milestone = null; |
| 165 | 204 | } |
| 205 | + | |
| 166 | 206 | if ( $milestone ) { |
| 167 | - $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $milestone ); | |
| 207 | + $this->oembed_github_repo_milestone_summary ( $matches[1], $matches[2], $milestone ); | |
| 168 | 208 | } |
| 169 | - } elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/milestone/([0-9]*)$#i', $url, $matches ) ) { | |
| 170 | - // New style milestone URL, e.g. https://github.com/example/example/milestone/1. | |
| 171 | - $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $matches[3] ); | |
| 172 | - } elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty( $matches[2] ) ) { | |
| 173 | - // Repository. | |
| 174 | - $this->oembed_github_repo( $matches[1], $matches[2] ); | |
| 175 | - } elseif ( preg_match( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) { | |
| 176 | - // User. | |
| 177 | - $this->oembed_github_author( $matches[1] ); | |
| 209 | + | |
| 210 | + // Repository | |
| 211 | + } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { | |
| 212 | + | |
| 213 | + $this->oembed_github_repo ( $matches[1], $matches[2] ); | |
| 214 | + | |
| 215 | + // User | |
| 216 | + } elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) { | |
| 217 | + | |
| 218 | + $this->oembed_github_author ( $matches[1] ); | |
| 219 | + | |
| 178 | 220 | } |
| 221 | + | |
| 179 | 222 | } |
| 180 | 223 | |
| 181 | - /** | |
| 182 | - * Capture then return output of template, provided theme or fallback to plugin default. | |
| 183 | - * | |
| 184 | - * @param string $template The template name to process. | |
| 185 | - * @param string $data Array, object, or variable that the template needs. | |
| 186 | - * | |
| 187 | - * @return string | |
| 188 | - */ | |
| 189 | - private function process_template( $template, $data ) { | |
| 190 | - ob_start(); | |
| 191 | - if ( ! locate_template( 'wp-github-oembed/' . $template, true, false, ['data' => $data] ) ) { | |
| 192 | - require_once 'templates/' . $template; | |
| 193 | - } | |
| 194 | 224 | |
| 195 | - return ob_get_clean(); | |
| 196 | - } | |
| 197 | 225 | |
| 198 | 226 | /** |
| 199 | 227 | * Retrieve a list of contributors for a project |
| 200 | - * | |
| 201 | - * @param string $owner The owner of the repository | |
| 202 | - * @param string $repository The repository name | |
| 228 | + * @param string $owner The owner of the repository | |
| 229 | + * @param string $repository The repository name | |
| 203 | 230 | */ |
| 204 | - private function oembed_github_repo_contributors( $owner, $repository ) { | |
| 205 | - $data = []; | |
| 206 | - $data['repo'] = $this->api->get_repo( $owner, $repository ); | |
| 207 | - $data['contributors'] = $this->api->get_repo_contributors( $owner, $repository ); | |
| 208 | - $data['gravatar_size'] = apply_filters( 'github_oembed_gravatar_size', 64 ); | |
| 209 | - $data['logo_class'] = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-octocat' ); | |
| 210 | - $data['details_expanded'] = apply_filters( 'wp_github_oembed_contributor_details_expanded', true ); | |
| 231 | + private function oembed_github_repo_contributors ( $owner, $repository ) { | |
| 211 | 232 | |
| 212 | - $response = new stdClass(); | |
| 213 | - $response->type = 'rich'; | |
| 214 | - $response->width = '10'; | |
| 215 | - $response->height = '10'; | |
| 233 | + $repo = $this->api->get_repo ( $owner, $repository ); | |
| 234 | + $contributors = $this->api->get_repo_contributors ( $owner, $repository ); | |
| 235 | + | |
| 236 | + $response = new stdClass(); | |
| 237 | + $response->type = 'rich'; | |
| 238 | + $response->width = '10'; | |
| 239 | + $response->height = '10'; | |
| 216 | 240 | $response->version = '1.0'; |
| 217 | - $response->title = $data['repo']->description; | |
| 218 | - $response->html = $this->process_template( | |
| 219 | - 'repository_contributors.php', $data ); | |
| 241 | + $response->title = $repo->description; | |
| 220 | 242 | |
| 221 | - header( 'Content-Type: application/json' ); | |
| 222 | - echo json_encode( $response ); | |
| 243 | + $gravatar_size = apply_filters ( 'github_oembed_gravatar_size', 64 ); | |
| 244 | + | |
| 245 | + // @TODO This should all be templated | |
| 246 | + $response->html = '<div class="github-embed github-embed-repo-contributors">'; | |
| 247 | + $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; | |
| 248 | + | |
| 249 | + $response->html .= '<span class="github-heading">Contributors: </span>'; | |
| 250 | + | |
| 251 | + $response->html .= '<ul class="github-repo-contributors">'; | |
| 252 | + | |
| 253 | + foreach ( $contributors as $contributor ) { | |
| 254 | + | |
| 255 | + $response->html .= '<li class="github-repo-contributor">'; | |
| 256 | + $response->html .= '<img class="github-repo-contributor-avatar" src="'; | |
| 257 | + $response->html .= esc_url(add_query_arg(array('s'=>$gravatar_size), $contributor->author->avatar_url)); | |
| 258 | + $response->html .= '" alt="Picture of '.esc_attr($contributor->author->login).'">'; | |
| 259 | + $response->html .= '<span class="github-repo-contributor-login">'; | |
| 260 | + $response->html .= '<a href="https://github.com/'.esc_attr($contributor->author->login).'">'.esc_attr($contributor->author->login).'</a></span>'; | |
| 261 | + } | |
| 262 | + | |
| 263 | + $response->html .= '</ul>'; | |
| 264 | + $response->html .= '<div style="clear: both;"></div>'; | |
| 265 | + $response->html .= '</div>'; | |
| 266 | + | |
| 267 | + header ( 'Content-Type: application/json' ); | |
| 268 | + echo json_encode ( $response ); | |
| 223 | 269 | die(); |
| 270 | + | |
| 224 | 271 | } |
| 225 | 272 | |
| 273 | + | |
| 274 | + | |
| 226 | 275 | /** |
| 227 | 276 | * Retrieve the summary information for a repo's milestone, and |
| 228 | 277 | * output it as an oembed response |
| 229 | 278 | */ |
| 230 | - private function oembed_github_repo_milestone_summary( $owner, $repository, $milestone ) { | |
| 231 | - $data = []; | |
| 232 | - $data['repo'] = $this->api->get_repo( $owner, $repository ); | |
| 233 | - $data['summary'] = $this->api->get_repo_milestone_summary( $owner, $repository, $milestone ); | |
| 234 | - $data['logo_class'] = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-octocat' ); | |
| 279 | + private function oembed_github_repo_milestone_summary ( $owner, $repository, $milestone ) { | |
| 235 | 280 | |
| 236 | - $response = new stdClass(); | |
| 237 | - $response->type = 'rich'; | |
| 238 | - $response->width = '10'; | |
| 239 | - $response->height = '10'; | |
| 281 | + $repo = $this->api->get_repo ( $owner, $repository ); | |
| 282 | + $summary = $this->api->get_repo_milestone_summary ( $owner, $repository, $milestone ); | |
| 283 | + | |
| 284 | + $response = new stdClass(); | |
| 285 | + $response->type = 'rich'; | |
| 286 | + $response->width = '10'; | |
| 287 | + $response->height = '10'; | |
| 240 | 288 | $response->version = '1.0'; |
| 241 | - $response->title = $data['repo']->description; | |
| 242 | - $response->html = $this->process_template( | |
| 243 | - 'repository_milestone_summary.php', $data ); | |
| 289 | + $response->title = $repo->description; | |
| 244 | 290 | |
| 245 | - header( 'Content-Type: application/json' ); | |
| 246 | - echo json_encode( $response ); | |
| 291 | + // @TODO This should all be templated | |
| 292 | + $response->html = '<div class="github-embed github-embed-milestone-summary">'; | |
| 293 | + $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; | |
| 294 | + | |
| 295 | + $response->html .= '<span class="github-heading">Milestone: </span>'; | |
| 296 | + $response->html .= '<span class="github-milestone-title">'.esc_html($summary->title)."</span><br>"; | |
| 297 | + | |
| 298 | + $response->html .= '<span class="github-heading">Issues: </span>'; | |
| 299 | + $response->html .= '<span class="github-milestone-issues">'; | |
| 300 | + $response->html .= esc_html ( number_format_i18n ( $summary->open_issues ) )." open, "; | |
| 301 | + $response->html .= esc_html ( number_format_i18n ( $summary->closed_issues ) )." closed.</span><br>"; | |
| 302 | + | |
| 303 | + if ( ! empty ( $summary->due_on ) ) { | |
| 304 | + $response->html .= '<span class="github-heading">Due: </span>'; | |
| 305 | + $due_date = date_format ( date_create ( $summary->due_on ), 'jS F Y' ); | |
| 306 | + $response->html .= '<span class="github-milestone-due-date">'.esc_html($due_date).'</span><br>'; | |
| 307 | + } | |
| 308 | + | |
| 309 | + $response->html .= '<p class="github-milestone-description">'.nl2br(esc_html($summary->description))."</p><br>"; | |
| 310 | + $response->html .= '</div>'; | |
| 311 | + | |
| 312 | + header ( 'Content-Type: application/json' ); | |
| 313 | + echo json_encode ( $response ); | |
| 247 | 314 | die(); |
| 248 | 315 | |
| 249 | 316 | } |
| 250 | 317 | |
| 318 | + | |
| 319 | + | |
| 251 | 320 | /** |
| 252 | 321 | * Retrieve the information from github for a repo, and |
| 253 | 322 | * output it as an oembed response |
| 254 | 323 | */ |
| 255 | - private function oembed_github_repo( $owner, $repository ) { | |
| 256 | - $data = [ | |
| 257 | - 'owner_slug' => $owner, | |
| 258 | - 'repo_slug' => $repository, | |
| 259 | - ]; | |
| 260 | - $data['repo'] = $this->api->get_repo( $owner, $repository ); | |
| 261 | - $data['commits'] = $this->api->get_repo_commits( $owner, $repository ); | |
| 262 | - $data['logo_class'] = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-mark' ); | |
| 263 | - $data['details_expanded'] = apply_filters( 'wp_github_oembed_repository_commit_details_expanded', true ); | |
| 324 | + private function oembed_github_repo ( $owner, $repository ) { | |
| 264 | 325 | |
| 265 | - $response = new stdClass(); | |
| 266 | - $response->type = 'rich'; | |
| 267 | - $response->width = '10'; | |
| 268 | - $response->height = '10'; | |
| 326 | + $repo = $this->api->get_repo ( $owner, $repository ); | |
| 327 | + $commits =$this->api->get_repo_commits ( $owner, $repository ); | |
| 328 | + | |
| 329 | + $response = new stdClass(); | |
| 330 | + $response->type = 'rich'; | |
| 331 | + $response->width = '10'; | |
| 332 | + $response->height = '10'; | |
| 269 | 333 | $response->version = '1.0'; |
| 270 | - $response->title = $data['repo']->description; | |
| 271 | - $response->html = $this->process_template( | |
| 272 | - 'repository.php', $data ); | |
| 334 | + $response->title = $repo->description; | |
| 273 | 335 | |
| 336 | + // @TODO This should all be templated | |
| 337 | + $response->html = '<div class="github-embed github-embed-repository">'; | |
| 338 | + $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; | |
| 339 | + $response->html .= '<a href="'.esc_attr($repo->html_url).'" target="_blank">'.esc_html($repo->html_url)."</a><br/>"; | |
| 340 | + $response->html .= esc_html ( number_format_i18n ( $repo->forks_count ) )." forks.<br/>"; | |
| 341 | + $response->html .= esc_html ( number_format_i18n ( $repo->open_issues_count ) )." open issues.<br/>"; | |
| 274 | 342 | |
| 275 | - header( 'Content-Type: application/json' ); | |
| 276 | - echo json_encode( $response ); | |
| 343 | + if ( count ( $commits ) ) { | |
| 344 | + | |
| 345 | + $cnt = 0; | |
| 346 | + $response->html .= 'Recent commits:'; | |
| 347 | + $response->html .= '<ul class="github_commits">'; | |
| 348 | + | |
| 349 | + foreach ( $commits as $commit ) { | |
| 350 | + | |
| 351 | + if ($cnt > 4) | |
| 352 | + break; | |
| 353 | + | |
| 354 | + $response->html .= '<li class="github_commit">'; | |
| 355 | + $response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, "; | |
| 356 | + $response->html .= esc_html($commit->commit->committer->name); | |
| 357 | + $response->html .= '</li>'; | |
| 358 | + | |
| 359 | + $cnt++; | |
| 360 | + | |
| 361 | + } | |
| 362 | + | |
| 363 | + $response->html .= '</ul>'; | |
| 364 | + | |
| 365 | + } | |
| 366 | + $response->html .= '</p>'; | |
| 367 | + $response->html .= '</div>'; | |
| 368 | + | |
| 369 | + header ( 'Content-Type: application/json' ); | |
| 370 | + echo json_encode ( $response ); | |
| 277 | 371 | die(); |
| 372 | + | |
| 278 | 373 | } |
| 279 | 374 | |
| 375 | + | |
| 376 | + | |
| 280 | 377 | /** |
| 281 | 378 | * Retrieve the information from github for an author, and output |
| 282 | 379 | * it as an oembed response |
| 283 | 380 | */ |
| 284 | - private function oembed_github_author( $owner ) { | |
| 285 | - $data = []; | |
| 286 | - $data["owner"] = $owner; | |
| 287 | - $data["owner_info"] = $this->api->get_user( $owner ); | |
| 288 | - $data["logo_class"] = apply_filters( 'wp_github_oembed_logo_class', | |
| 289 | - 'github-logo-octocat' ); | |
| 381 | + private function oembed_github_author ( $owner ) { | |
| 290 | 382 | |
| 291 | - $response = new stdClass(); | |
| 292 | - $response->type = 'rich'; | |
| 293 | - $response->width = '10'; | |
| 294 | - $response->height = '10'; | |
| 383 | + $owner_info = $this->api->get_user ( $owner ); | |
| 384 | + | |
| 385 | + $response = new stdClass(); | |
| 386 | + $response->type = 'rich'; | |
| 387 | + $response->width = '10'; | |
| 388 | + $response->height = '10'; | |
| 295 | 389 | $response->version = '1.0'; |
| 296 | - $response->title = $data['owner_info']->name; | |
| 297 | - $response->html = $this->process_template( | |
| 298 | - 'author.php', $data ); | |
| 390 | + $response->title = $owner_info->name; | |
| 299 | 391 | |
| 300 | - header( 'Content-Type: application/json' ); | |
| 301 | - echo json_encode( $response ); | |
| 392 | + // @TODO This should all be templated | |
| 393 | + $response->html = '<div class="github-embed github-embed-user">'; | |
| 394 | + $response->html .= '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>"; | |
| 395 | + $response->html .= esc_html ( number_format_i18n ( $owner_info->public_repos ) ).' repositories, '; | |
| 396 | + $response->html .= esc_html ( number_format_i18n ( $owner_info->followers ) ).' followers.</p>'; | |
| 397 | + $response->html .= '</div>'; | |
| 398 | + | |
| 399 | + header ( 'Content-Type: application/json' ); | |
| 400 | + echo json_encode ( $response ); | |
| 302 | 401 | die(); |
| 402 | + | |
| 303 | 403 | } |
| 404 | + | |
| 304 | 405 | } |
| 305 | 406 | |
| 306 | -require_once( 'github-api.php' ); | |
| 407 | +require_once ( 'github-api.php' ); | |
| 307 | 408 | |
| 308 | -$github_api = new github_api(); | |
| 309 | -$github_embed = new github_embed( $github_api ); | |
| 409 | +$github_api = new github_api(); | |
| 410 | +$github_embed = new github_embed ( $github_api ); | |