Skip to content

Commit 9fbab15

Browse files
committed
Use GitHub API v4 (GraphQL) to fetch star count
Fixes #221
1 parent 880b0ee commit 9fbab15

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/templates/pages/default.txp

+21-3
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,31 @@
6868
<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>
6969
<txp:etc_cache id="github-stars" time="-3600">
7070
<txp:php>
71+
global $variable;
72+
73+
$query = <<<'GRAPHQL'
74+
query {
75+
repository(owner:textpattern, name:textpattern) {
76+
stargazers {
77+
totalCount
78+
}
79+
}
80+
}
81+
GRAPHQL;
82+
83+
$json = json_encode(['query' => $query]);
7184
$curl = curl_init();
7285

7386
curl_setopt_array($curl, [
7487
CURLOPT_RETURNTRANSFER => true,
75-
CURLOPT_URL => 'https://api.github.com/repos/textpattern/textpattern',
76-
CURLOPT_USERAGENT => 'Textpattern CMS',
88+
CURLOPT_URL => 'https://api.github.com/graphql',
7789
CURLOPT_FAILONERROR => true,
90+
CURLOPT_POSTFIELDS => $json,
91+
CURLOPT_HTTPHEADER => array(
92+
'User-Agent: Textpattern CMS',
93+
'Content-Type: application/json;charset=utf-8',
94+
'Authorization: bearer '.$variable['github-api-key']
95+
),
7896
]);
7997

8098
$response = curl_exec($curl);
@@ -89,7 +107,7 @@ if (!($json = json_decode($response))) {
89107
return;
90108
}
91109

92-
echo '<a class="count-bubble" rel="external" href="https://github.com/textpattern/textpattern/stargazers" title="Stargazers on GitHub">'.intval($json->stargazers_count).'</a>';
110+
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>';
93111
</txp:php>
94112
</txp:etc_cache>
95113
</p>

0 commit comments

Comments
 (0)