Skip to content

Commit d996de0

Browse files
committed
Fetch GitHub API via JavaScript
Works around our PHP version on server being older than supported TLS1.2.
1 parent bafecc5 commit d996de0

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/assets/js/main.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
// Load objects as variables.
1010

1111
var code = document.querySelectorAll('pre code'),
12-
navmenu = document.getElementById('site-navigation');
12+
navmenu = document.getElementById('site-navigation'),
13+
starbubble = document.getElementById('starbubble');
1314

1415
// Syntax highlighting, via 'Prism'.
1516
// Applies syntax highlighting to `code` HTML elements.
@@ -32,4 +33,47 @@
3233
});
3334
}
3435

36+
// GitHub star count.
37+
38+
if (starbubble) {
39+
var githubStars = function (callback) {
40+
var xmlhttp = new XMLHttpRequest(),
41+
url = ['https://api.github.com/repos/textpattern/textpattern'],
42+
useCallback = (typeof(callback) === 'function');
43+
44+
function countStars(response) {
45+
if (!(response instanceof Array)) {
46+
response = [response];
47+
}
48+
var stars = 0;
49+
for (var i in response) {
50+
if (response.hasOwnProperty(i)) {
51+
stars += parseInt(response[i].stargazers_count);
52+
}
53+
}
54+
return stars;
55+
}
56+
57+
if (useCallback) {
58+
xmlhttp.onreadystatechange = function() {
59+
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
60+
callback(countStars(JSON.parse(xmlhttp.responseText)));
61+
}
62+
};
63+
}
64+
65+
xmlhttp.open('GET', url, useCallback);
66+
xmlhttp.setRequestHeader('Accept', 'application/vnd.github.v3+json');
67+
xmlhttp.send();
68+
69+
if (!useCallback) {
70+
return countStars(JSON.parse(xmlhttp.responseText));
71+
}
72+
};
73+
74+
githubStars(function(stars) {
75+
starbubble.innerHTML = '<a id="starcount" class="count-bubble" rel="external" href="https://github.com/textpattern/textpattern/stargazers" title="Stargazers on GitHub">'+stars+'</a>';
76+
});
77+
}
78+
3579
})();

src/mockups/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ <h1 class="cta"><span>The small content management system that can handle big id
118118
</p>
119119
<p>
120120
<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>
121-
<a class="count-bubble" rel="external" href="https://github.com/textpattern/textpattern/stargazers" title="Stargazers on GitHub">300</a>
121+
<span id="starbubble"></span>
122122
</p>
123123

124124
</div><!-- /.container -->

src/templates/pages/default.txp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,7 @@
7575
</p>
7676
<p>
7777
<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>
78-
<txp:etc_cache id="github-stars" time="-3600">
79-
<txp:php>
80-
$curl1 = curl_init();
81-
82-
curl_setopt_array($curl1, array(
83-
CURLOPT_RETURNTRANSFER => 1,
84-
CURLOPT_URL => 'https://api.github.com/repos/textpattern/textpattern',
85-
CURLOPT_USERAGENT => 'Textpattern CMS',
86-
));
87-
88-
$response1 = curl_exec($curl1);
89-
90-
if (curl_error($curl1)) {
91-
// Do nothing.
92-
} else {
93-
$json = json_decode($response1);
94-
echo '<a class="count-bubble" rel="external" href="https://github.com/textpattern/textpattern/stargazers" title="Stargazers on GitHub">'.$json->stargazers_count.'</a>';
95-
}
96-
97-
curl_close($curl1);
98-
</txp:php>
99-
</txp:etc_cache>
78+
<span id="starbubble"></span>
10079
</p>
10180
</div>
10281
</div>

0 commit comments

Comments
 (0)