diff --git a/github-embed.php b/github-embed.php index 01dca76..8283e97 100644 --- a/github-embed.php +++ b/github-embed.php @@ -175,42 +175,40 @@ class github_embed { } } + /** + * Capture then return output of template, provided theme or fallback to plugin default + * @param string $template The template name to process + * @param string $data Array, object, or variable that the template needs + */ + private function process_template( $template, $data ) { + ob_start(); + if ( ! locate_template($template, true) ) { + require_once $template; + } + return ob_get_clean(); + } + /** * Retrieve a list of contributors for a project * @param string $owner The owner of the repository * @param string $repository The repository name */ private function oembed_github_repo_contributors( $owner, $repository ) { - $repo = $this->api->get_repo( $owner, $repository ); - $contributors = $this->api->get_repo_contributors( $owner, $repository ); + $data = []; + $data['repo'] = $this->api->get_repo( $owner, $repository ); + $data['contributors'] = $this->api->get_repo_contributors( $owner, $repository ); + $data['gravatar_size'] = apply_filters( 'github_oembed_gravatar_size', 64 ); + $data['logo_class'] = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-octocat' ); $response = new stdClass(); $response->type = 'rich'; $response->width = '10'; $response->height = '10'; $response->version = '1.0'; - $response->title = $repo->description; - - $gravatar_size = apply_filters( 'github_oembed_gravatar_size', 64 ); - - // @TODO This should all be templated - $logo_class = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-octocat' ); - $response->html = '
'; + $response->title = $data['repo']->description; + $response->html = $this->process_template( + 'templates/oembed_github_repo_contributors.view.php', $data); + header( 'Content-Type: application/json' ); echo json_encode( $response ); die(); @@ -221,37 +219,19 @@ class github_embed { * output it as an oembed response */ private function oembed_github_repo_milestone_summary( $owner, $repository, $milestone ) { - $repo = $this->api->get_repo( $owner, $repository ); - $summary = $this->api->get_repo_milestone_summary( $owner, $repository, $milestone ); + $data = []; + $data['repo'] = $this->api->get_repo( $owner, $repository ); + $data['summary'] = $this->api->get_repo_milestone_summary( $owner, $repository, $milestone ); + $data['logo_class'] = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-octocat' ); $response = new stdClass(); $response->type = 'rich'; $response->width = '10'; $response->height = '10'; $response->version = '1.0'; - $response->title = $repo->description; - - // @TODO This should all be templated - $logo_class = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-octocat' ); - $response->html = ' '; + $response->title = $data['repo']->description; + $response->html = $this->process_template( + 'templates/oembed_github_repo_milestone_summary.view.php', $data); header( 'Content-Type: application/json' ); echo json_encode( $response ); @@ -264,43 +244,24 @@ class github_embed { * output it as an oembed response */ private function oembed_github_repo ( $owner, $repository ) { - $repo = $this->api->get_repo( $owner, $repository ); - $commits =$this->api->get_repo_commits( $owner, $repository ); + $data = [ + $owner, + $repository, + ]; + $data['repo'] = $this->api->get_repo( $owner, $repository ); + $data['commits'] = $this->api->get_repo_commits( $owner, $repository ); + $data['logo_class'] = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-mark' ); $response = new stdClass(); $response->type = 'rich'; $response->width = '10'; $response->height = '10'; $response->version = '1.0'; - $response->title = $repo->description; - - // @TODO This should all be templated - $logo_class = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-mark' ); - $response->html = ' '; + $response->title = $data['repo']->description; + $response->html = $this->process_template( + 'templates/oembed_github_repo.view.php', $data); + + header( 'Content-Type: application/json' ); echo json_encode( $response ); die(); @@ -311,23 +272,21 @@ class github_embed { * it as an oembed response */ private function oembed_github_author ( $owner ) { - - $owner_info = $this->api->get_user( $owner ); + $data = []; + $data["owner"] = $owner; + $data["owner_info"] = $this->api->get_user( $owner ); + $data["logo_class"] = apply_filters( 'wp_github_oembed_logo_class', + 'github-logo-octocat' ); $response = new stdClass(); $response->type = 'rich'; $response->width = '10'; $response->height = '10'; $response->version = '1.0'; - $response->title = $owner_info->name; - - // @TODO This should all be templated - $logo_class = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-octocat' ); - $response->html = ' '; + $response->title = $data['owner_info']->name; + $response->html = $this->process_template( + 'templates/oembed_github_author.view.php', $data); + header( 'Content-Type: application/json' ); echo json_encode( $response ); die(); diff --git a/templates/oembed_github_author.view.php b/templates/oembed_github_author.view.php new file mode 100644 index 0000000..1f7a78e --- /dev/null +++ b/templates/oembed_github_author.view.php @@ -0,0 +1,11 @@ + diff --git a/templates/oembed_github_repo.view.php b/templates/oembed_github_repo.view.php new file mode 100644 index 0000000..81f6e44 --- /dev/null +++ b/templates/oembed_github_repo.view.php @@ -0,0 +1,27 @@ + diff --git a/templates/oembed_github_repo_contributors.view.php b/templates/oembed_github_repo_contributors.view.php new file mode 100644 index 0000000..ceb4805 --- /dev/null +++ b/templates/oembed_github_repo_contributors.view.php @@ -0,0 +1,25 @@ + diff --git a/templates/oembed_github_repo_milestone_summary.view.php b/templates/oembed_github_repo_milestone_summary.view.php new file mode 100644 index 0000000..ae350c0 --- /dev/null +++ b/templates/oembed_github_repo_milestone_summary.view.php @@ -0,0 +1,29 @@ +