-
-
Save connorjburton/3f9db2abf888f2192c73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ThemeUpdateHandler { | |
/** | |
* Checks if the current version of the base theme is old, triggers | |
* and update message if it is. | |
* | |
* If a new version is available, we save the new version number to the database, | |
* so we can pull it back later and run version specific upgrades | |
* | |
* @param $transient:object | |
*/ | |
public function check($transient) { | |
if(!empty($transient->checked[get_config('paths')['parent-theme']])) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, get_config('update')['json']); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 3); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$result = curl_exec($ch); | |
if(curl_exec($ch) === false) { | |
debug_log('CURL failed on theme update json file, error code:' . curl_errno($ch)); | |
} elseif(!empty($result)) { | |
$data = json_decode($result, true); | |
if(version_compare($transient->checked[get_config('paths')['parent-theme']], $data['new_version'], '<')) { | |
$versions = json_encode(array('new' => $data['new_version'], 'old' => $transient->checked[get_config('paths')['parent-theme']])); | |
if(!get_option('pm_upgrade_versions')) { | |
add_option('pm_upgrade_versions', $versions); | |
} else { | |
update_option('pm_upgrade_versions', $versions); | |
} | |
$transient->response[get_config('paths')['parent-theme']] = (array) $data; | |
} | |
} | |
curl_close($ch); | |
} | |
return $transient; | |
} | |
} | |
$handler = new ThemeUpdateHandler; | |
add_filter('pre_set_site_transient_update_themes', array($handler, 'check')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment