From c72532a8a75da935bfe59814b68d01bd3fd4c1b9 Mon Sep 17 00:00:00 2001 From: Zeb Walker Date: Mon, 26 Aug 2019 16:21:29 -0400 Subject: [PATCH] Implement templating --- github-embed.php | 141 +++++++----------- templates/oembed_github_author.view.php | 11 ++ templates/oembed_github_repo.view.php | 27 ++++ .../oembed_github_repo_contributors.view.php | 25 ++++ ...bed_github_repo_milestone_summary.view.php | 29 ++++ 5 files changed, 142 insertions(+), 91 deletions(-) create mode 100644 templates/oembed_github_author.view.php create mode 100644 templates/oembed_github_repo.view.php create mode 100644 templates/oembed_github_repo_contributors.view.php create mode 100644 templates/oembed_github_repo_milestone_summary.view.php 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->html .= '

'; - $response->html .= '' . esc_html( $repo->description ) . '
'; - $response->html .= 'Contributors: '; - $response->html .= '

'; - $response->html .= '
'; - $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->html .= '

' . esc_html( $repo->description ) . '
'; - - $response->html .= 'Milestone: '; - $response->html .= '' . esc_html( $summary->title ) . '
'; - - $response->html .= 'Issues: '; - $response->html .= ''; - $response->html .= esc_html( number_format_i18n( $summary->open_issues ) ) . ' open, '; - $response->html .= esc_html( number_format_i18n( $summary->closed_issues ) ) . ' closed.
'; - - if ( ! empty( $summary->due_on ) ) { - $response->html .= 'Due: '; - $due_date = date_format( date_create( $summary->due_on ), 'jS F Y' ); - $response->html .= '' . esc_html( $due_date ) . '
'; - } - - $response->html .= '

' . nl2br( esc_html( $summary->description ) ) . '


'; - $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->html .= '

' . esc_html( $repo->description ) . '
'; - $response->html .= '' . esc_html( $repo->html_url ) . '
'; - $response->html .= '' . esc_html( number_format_i18n( $repo->forks_count ) ) . ' forks.
'; - $response->html .= '' . esc_html( number_format_i18n( $repo->stargazers_count ) ) . ' stars.
'; - $response->html .= '' . esc_html( number_format_i18n( $repo->open_issues_count ) ) . ' open issues.
'; - - if ( count( $commits ) ) { - $cnt = 0; - $response->html .= 'Recent commits:'; - $response->html .= '

'; - } - $response->html .= '

'; - $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->html .= '

' . esc_html( $owner ) . '
'; - $response->html .= esc_html( number_format_i18n( $owner_info->public_repos ) ) . ' repositories, '; - $response->html .= esc_html( number_format_i18n( $owner_info->followers ) ) . ' followers.

'; - $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 @@ +
+

+ + + + + +
+ public_repos ) ) ?> repositories, followers ) ) ?> followers. +

+
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 @@ +
+

+ + + description ) ?> + + +
+ html_url ) ?>
+ forks_count ) ) ?> forks.
+ stargazers_count ) ) ?> stars.
+ open_issues_count ) ) ?> open issues.
+ Recent commits: +

+

+
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 @@ +
+

+ + + description ) ?> + + +
+ Contributors: +

+
+

+
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 @@ +
+

+ + + description ) ?> + + +
+ Milestone: + title ) ?> +
+ Issues: + + open_issues ) ) ?> open, closed_issues ) ) ?> closed. + +
+ due_on ) ) : ?> + Due: + + due_on ), 'jS F Y' ) ) ?> + +
+ +

+ description ) ) ?> +

+
+

+