Skip to content

Commit 4110a4b

Browse files
committed
Update borg blocks module to pull latest version from API.
1 parent 1b031e6 commit 4110a4b

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

www/modules/custom/borg_blocks/borg_blocks.module

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ function borg_blocks_branding($settings) {
172172
* Block callback.
173173
*/
174174
function borg_blocks_tagline($settings) {
175-
$version = check_plain($settings['version']);
176-
177175
$button_class_name = 'borg__button';
178176
if (module_exists('tugboat')) {
179177
$demo_text = t('Demo Backdrop CMS');
@@ -203,10 +201,20 @@ function borg_blocks_tagline($settings) {
203201
'html' => TRUE,
204202
)
205203
);
206-
207204
}
208205

209-
$dl_url = 'https://github.com/backdrop/backdrop/releases/download/' . $version . '/backdrop.zip';
206+
// Initiate a curl session.
207+
$data = _borg_blocks_get_version();
208+
209+
$version = $data['items'][0]['version'];
210+
$dl_url = $data['items'][0]['download_link'];
211+
212+
// After update.
213+
//$version = $data['latest']['version'];
214+
//$dl_url = $data['items'][0]['download_link'];
215+
216+
217+
//$dl_url = 'https://github.com/backdrop/backdrop/releases/download/' . $version . '/backdrop.zip';
210218
$dl_text = t('Download Backdrop&nbsp;CMS') . ' <span class="version">v&nbsp;' . $version . '</span>';
211219

212220
$download_link = l($dl_text, $dl_url,
@@ -352,11 +360,18 @@ function borg_blocks_block_configure($delta = '', $settings = array()) {
352360
'version' => '1.13.0',
353361
);
354362

363+
$data = _borg_blocks_get_version();
364+
$version = $data['items'][0]['version'];
365+
// After update.
366+
//$version = $data['latest']['version'];
367+
355368
$form['version'] = array(
356369
'#type' => 'textfield',
357370
'#size' => 10,
358371
'#title' => t('Current Version Number'),
359-
'#default_value' => $settings['version'],
372+
'#default_value' => $version,
373+
'#disabled' => TRUE,
374+
'#description' => t('<strong>Clear all caches</strong> to update. This value is now determined automatically. '),
360375
);
361376

362377
}
@@ -395,3 +410,28 @@ function borg_blocks_preprocess_block(&$variables){
395410
}
396411
}
397412
}
413+
414+
/**
415+
* Helper function, get core version from JSON API.
416+
*/
417+
function _borg_blocks_get_version() {
418+
$cached = cache_get('backdrop_core_version_latest');
419+
$data = isset($cached->data) ? $cached->data : array();
420+
if (empty($data)) {
421+
$ch = curl_init();
422+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
423+
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
424+
curl_setopt($ch, CURLOPT_URL, 'https://backdropcms.org/core/latest.json');
425+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
426+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
427+
$json = curl_exec($ch);
428+
$res_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
429+
curl_close($ch);
430+
$data = json_decode($json, TRUE);
431+
432+
$expires = time() + 60*60; // Expire in no less than 1 hour.
433+
cache_set('backdrop_core_version_latest', $data, 'cache', $expires);
434+
}
435+
436+
return $data;
437+
}

0 commit comments

Comments
 (0)