-
-
Save connorjburton/8c3899ed26c286dcc04c 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']] = $data; | |
} | |
} | |
curl_close($ch); | |
} | |
return $transient; | |
} | |
public function modify_source($source, $remote_source, $hook_extra) { | |
$exploded = explode('/', $source); | |
$exploded[count($exploded) - 2] = get_config('paths')['parent-theme']; | |
$newSource = implode('/', $exploded); | |
if(!rename($source, $newSource)) { debug_log('Can\'t rename source'); return; } | |
$source = $newSource; | |
return $source; | |
} | |
public function before_install($options) { | |
$options['hook_extra']['double_check'] = get_config('paths')['parent-theme']; | |
return $options; | |
} | |
/** | |
* Wordpress seems to update the theme but not with the same name, | |
* instead it creates a folder pm-{hash}, so we copy that directory | |
* to the correct folder, then remove the old folder | |
* | |
* @param $response:int | |
* @param $hook_extra:array | |
* @param $result:array | |
* | |
* @return null | |
*/ | |
public function after_install($response, $hook_extra, $result) { | |
//double check we are actually updating the base theme | |
if($hook_extra['theme'] === $hook_extra['double_check']) { | |
$filesystem = new WP_Filesystem_Direct(array()); | |
$filesystem->delete($result['source'], true); | |
} | |
} | |
} | |
$handler = new ThemeUpdateHandler; | |
add_filter('upgrader_source_selection', array($handler, 'modify_source'), 5, 3); | |
add_filter('upgrader_package_options', array($handler, 'before_install'), 5, 3); | |
add_filter('upgrader_post_install', array($handler, 'after_install'), 5, 3); | |
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