From c72532a8a75da935bfe59814b68d01bd3fd4c1b9 Mon Sep 17 00:00:00 2001 From: Zeb Walker Date: Mon, 26 Aug 2019 16:21:29 -0400 Subject: [PATCH 1/6] 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 ) ) ?> +

+
+

+
From 9f07f00804ae30860880e0ab28883f2e71f3fdfe Mon Sep 17 00:00:00 2001 From: Lee Willis Date: Mon, 2 Sep 2019 13:28:30 +0100 Subject: [PATCH 2/6] Rename theme template folder, rename template files --- github-embed.php | 21 +++++++++++-------- ...mbed_github_author.view.php => author.php} | 0 ...ed_github_repo.view.php => repository.php} | 0 ...s.view.php => repository_contributors.php} | 0 ...w.php => repository_milestone_summary.php} | 0 5 files changed, 12 insertions(+), 9 deletions(-) rename templates/{oembed_github_author.view.php => author.php} (100%) rename templates/{oembed_github_repo.view.php => repository.php} (100%) rename templates/{oembed_github_repo_contributors.view.php => repository_contributors.php} (100%) rename templates/{oembed_github_repo_milestone_summary.view.php => repository_milestone_summary.php} (100%) diff --git a/github-embed.php b/github-embed.php index 8283e97..27043e1 100644 --- a/github-embed.php +++ b/github-embed.php @@ -176,14 +176,17 @@ 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 + * 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. + * + * @return string */ private function process_template( $template, $data ) { ob_start(); - if ( ! locate_template($template, true) ) { - require_once $template; + if ( ! locate_template( 'wp-github-oembed/' . $template, true ) ) { + require_once 'templates/' . $template; } return ob_get_clean(); } @@ -207,7 +210,7 @@ class github_embed { $response->version = '1.0'; $response->title = $data['repo']->description; $response->html = $this->process_template( - 'templates/oembed_github_repo_contributors.view.php', $data); + 'repository_contributors.php', $data); header( 'Content-Type: application/json' ); echo json_encode( $response ); @@ -231,7 +234,7 @@ class github_embed { $response->version = '1.0'; $response->title = $data['repo']->description; $response->html = $this->process_template( - 'templates/oembed_github_repo_milestone_summary.view.php', $data); + 'repository_milestone_summary.php', $data); header( 'Content-Type: application/json' ); echo json_encode( $response ); @@ -259,7 +262,7 @@ class github_embed { $response->version = '1.0'; $response->title = $data['repo']->description; $response->html = $this->process_template( - 'templates/oembed_github_repo.view.php', $data); + 'repository.php', $data); header( 'Content-Type: application/json' ); @@ -285,7 +288,7 @@ class github_embed { $response->version = '1.0'; $response->title = $data['owner_info']->name; $response->html = $this->process_template( - 'templates/oembed_github_author.view.php', $data); + 'author.php', $data); header( 'Content-Type: application/json' ); echo json_encode( $response ); diff --git a/templates/oembed_github_author.view.php b/templates/author.php similarity index 100% rename from templates/oembed_github_author.view.php rename to templates/author.php diff --git a/templates/oembed_github_repo.view.php b/templates/repository.php similarity index 100% rename from templates/oembed_github_repo.view.php rename to templates/repository.php diff --git a/templates/oembed_github_repo_contributors.view.php b/templates/repository_contributors.php similarity index 100% rename from templates/oembed_github_repo_contributors.view.php rename to templates/repository_contributors.php diff --git a/templates/oembed_github_repo_milestone_summary.view.php b/templates/repository_milestone_summary.php similarity index 100% rename from templates/oembed_github_repo_milestone_summary.view.php rename to templates/repository_milestone_summary.php From 30bc2000e3be09c6c6f325522167884cfd9f2146 Mon Sep 17 00:00:00 2001 From: Lee Willis Date: Mon, 2 Sep 2019 13:47:02 +0100 Subject: [PATCH 3/6] Whitespace --- github-api.php | 2 - github-embed.php | 111 +++++++++++++++++++++++++---------------------- 2 files changed, 58 insertions(+), 55 deletions(-) diff --git a/github-api.php b/github-api.php index ba77898..d497187 100644 --- a/github-api.php +++ b/github-api.php @@ -1,7 +1,5 @@ get_results( $sql ); @@ -92,12 +92,14 @@ class github_embed { * 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 * API to fulfil the request. - * @param array $providers The current list of providers + * + * @param array $providers The current list of providers + * * @return array The list, with our new provider added */ public function register_oembed_handler() { $oembed_url = home_url(); - $key = $this->get_key(); + $key = $this->get_key(); $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url ); wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true ); } @@ -113,6 +115,7 @@ class github_embed { $key = md5( time() . rand( 0, 65535 ) ); add_option( 'github_oembed_key', $key, '', 'yes' ); } + return $key; } @@ -138,7 +141,7 @@ class github_embed { } // 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; if ( ! empty( $format ) && 'json' !== $format ) { @@ -188,29 +191,31 @@ class github_embed { if ( ! locate_template( 'wp-github-oembed/' . $template, true ) ) { require_once 'templates/' . $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 + * + * @param string $owner The owner of the repository + * @param string $repository The repository name */ private function oembed_github_repo_contributors( $owner, $repository ) { - $data = []; - $data['repo'] = $this->api->get_repo( $owner, $repository ); - $data['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' ); + $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 = new stdClass(); + $response->type = 'rich'; + $response->width = '10'; + $response->height = '10'; $response->version = '1.0'; - $response->title = $data['repo']->description; - $response->html = $this->process_template( - 'repository_contributors.php', $data); + $response->title = $data['repo']->description; + $response->html = $this->process_template( + 'repository_contributors.php', $data ); header( 'Content-Type: application/json' ); echo json_encode( $response ); @@ -222,19 +227,19 @@ class github_embed { * output it as an oembed response */ private function oembed_github_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 = []; + $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 = new stdClass(); + $response->type = 'rich'; + $response->width = '10'; + $response->height = '10'; $response->version = '1.0'; - $response->title = $data['repo']->description; - $response->html = $this->process_template( - 'repository_milestone_summary.php', $data); + $response->title = $data['repo']->description; + $response->html = $this->process_template( + 'repository_milestone_summary.php', $data ); header( 'Content-Type: application/json' ); echo json_encode( $response ); @@ -246,23 +251,23 @@ class github_embed { * Retrieve the information from github for a repo, and * output it as an oembed response */ - private function oembed_github_repo ( $owner, $repository ) { - $data = [ - $owner, - $repository, + private function oembed_github_repo( $owner, $repository ) { + $data = [ + 'owner_slug' => $owner, + 'repo_slug' => $repository, ]; - $data['repo'] = $this->api->get_repo( $owner, $repository ); - $data['commits'] = $this->api->get_repo_commits( $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 = new stdClass(); + $response->type = 'rich'; + $response->width = '10'; + $response->height = '10'; $response->version = '1.0'; - $response->title = $data['repo']->description; - $response->html = $this->process_template( - 'repository.php', $data); + $response->title = $data['repo']->description; + $response->html = $this->process_template( + 'repository.php', $data ); header( 'Content-Type: application/json' ); @@ -274,21 +279,21 @@ class github_embed { * Retrieve the information from github for an author, and output * it as an oembed response */ - private function oembed_github_author ( $owner ) { - $data = []; - $data["owner"] = $owner; + private function oembed_github_author( $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' ); + 'github-logo-octocat' ); - $response = new stdClass(); - $response->type = 'rich'; - $response->width = '10'; - $response->height = '10'; + $response = new stdClass(); + $response->type = 'rich'; + $response->width = '10'; + $response->height = '10'; $response->version = '1.0'; - $response->title = $data['owner_info']->name; - $response->html = $this->process_template( - 'author.php', $data); + $response->title = $data['owner_info']->name; + $response->html = $this->process_template( + 'author.php', $data ); header( 'Content-Type: application/json' ); echo json_encode( $response ); @@ -298,5 +303,5 @@ class github_embed { require_once( 'github-api.php' ); -$github_api = new github_api(); +$github_api = new github_api(); $github_embed = new github_embed( $github_api ); From 1dd8aefb9ca633b545518597e985660595559edb Mon Sep 17 00:00:00 2001 From: Lee Willis Date: Mon, 2 Sep 2019 13:47:13 +0100 Subject: [PATCH 4/6] Template tweaks --- templates/repository.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/templates/repository.php b/templates/repository.php index 81f6e44..fce6803 100644 --- a/templates/repository.php +++ b/templates/repository.php @@ -1,22 +1,22 @@ -
+

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

  • - commit->message ) ?>, commit->committer->name ); ?> + commit->message ) ?>, commit->committer->name ); ?>
  • Date: Mon, 2 Sep 2019 13:48:21 +0100 Subject: [PATCH 5/6] Template tweaks --- templates/author.php | 8 ++++---- templates/repository_contributors.php | 14 +++++++------- templates/repository_milestone_summary.php | 14 +++++++------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/templates/author.php b/templates/author.php index 1f7a78e..694b420 100644 --- a/templates/author.php +++ b/templates/author.php @@ -1,11 +1,11 @@ -
    +

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

    diff --git a/templates/repository_contributors.php b/templates/repository_contributors.php index ceb4805..9850e02 100644 --- a/templates/repository_contributors.php +++ b/templates/repository_contributors.php @@ -1,8 +1,8 @@ -
    +

    - + - description ) ?> + description ) ?>
    @@ -11,11 +11,11 @@

  • Picture of <?= esc_attr( $contributor->author->login ) ?> + src=" $data['gravatar_size'] ), $contributor->author->avatar_url ) ); ?>" + alt="Picture of author->login ) ?>"> diff --git a/templates/repository_milestone_summary.php b/templates/repository_milestone_summary.php index ae350c0..967363c 100644 --- a/templates/repository_milestone_summary.php +++ b/templates/repository_milestone_summary.php @@ -1,28 +1,28 @@ -
    +

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

    - description ) ) ?> + description ) ) ?>


    From 3f2b35a4b20e323f2eb9cd78b9ff3167162ff3bb Mon Sep 17 00:00:00 2001 From: Lee Willis Date: Mon, 2 Sep 2019 13:52:22 +0100 Subject: [PATCH 6/6] Whitespace --- github-api.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/github-api.php b/github-api.php index d497187..1a64a9d 100644 --- a/github-api.php +++ b/github-api.php @@ -55,10 +55,8 @@ class github_api { * provide the credentials. */ public function set_credentials() { - $this->client_id = apply_filters( 'github-embed-client-id', $this->client_id ); $this->client_secret = apply_filters( 'github-embed-client-secret', $this->client_secret ); - }