Skip to content

Commit

Permalink
Use GitHub API v4 (GraphQL) to fetch star count
Browse files Browse the repository at this point in the history
Fixes #221
  • Loading branch information
philwareham committed May 2, 2019
1 parent 880b0ee commit 9fbab15
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/templates/pages/default.txp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,31 @@
<a class="button" rel="external" href="https://github.com/textpattern/textpattern/" title="Stars on GitHub"><span class="ui-icon ui-extra-icon-github">GitHub</span> <strong>Stars</strong></a>
<txp:etc_cache id="github-stars" time="-3600">
<txp:php>
global $variable;

$query = <<<'GRAPHQL'
query {
repository(owner:textpattern, name:textpattern) {
stargazers {
totalCount
}
}
}
GRAPHQL;

$json = json_encode(['query' => $query]);
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://api.github.com/repos/textpattern/textpattern',
CURLOPT_USERAGENT => 'Textpattern CMS',
CURLOPT_URL => 'https://api.github.com/graphql',
CURLOPT_FAILONERROR => true,
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
'User-Agent: Textpattern CMS',
'Content-Type: application/json;charset=utf-8',
'Authorization: bearer '.$variable['github-api-key']
),
]);

$response = curl_exec($curl);
Expand All @@ -89,7 +107,7 @@ if (!($json = json_decode($response))) {
return;
}

echo '<a class="count-bubble" rel="external" href="https://github.com/textpattern/textpattern/stargazers" title="Stargazers on GitHub">'.intval($json->stargazers_count).'</a>';
echo '<a class="count-bubble" rel="external" href="https://github.com/textpattern/textpattern/stargazers" title="Stargazers on GitHub">'.intval($json->data->repository->stargazers->totalCount).'</a>';
</txp:php>
</txp:etc_cache>
</p>
Expand Down

0 comments on commit 9fbab15

Please sign in to comment.