Fixed automatic background updates not working for plugins that use this library.

Background (i.e. unattended) plugin updates require an additional $update->plugin field to be set to plugin file name relative to the /wp-content/plugins directory. Fixed by adding a $filename property to the PluginInfo and PluginUpdate classes and updating toWpFormat() to return it as "plugin". Apparently WordPress update API introduced the field a couple of minor versions back, but I didn't notice because it's used *only* by the background updater and ignored otherwise. 

I named the internal variable "filename" instead of "plugin" because "plugin" is very vague. It doesn't really tell you if the variable contains a plugin slug, plugin name, the full plugin filename, a partial filename, basename or something else. "filename" is a bit less ambiguous, though not perfect.

Bumped version to 1.6.
This commit is contained in:
Yahnis Elsts 2014-10-24 15:53:16 +03:00
parent dbe109557b
commit 954aa4a192
1 changed files with 31 additions and 23 deletions

View File

@ -8,17 +8,17 @@
* http://www.gnu.org/licenses/gpl.html
*/
if ( !class_exists('PluginUpdateChecker_1_5') ):
if ( !class_exists('PluginUpdateChecker_1_6') ):
/**
* A custom plugin update checker.
*
* @author Janis Elsts
* @copyright 2014
* @version 1.5
* @version 1.6
* @access public
*/
class PluginUpdateChecker_1_5 {
class PluginUpdateChecker_1_6 {
public $metadataUrl = ''; //The URL of the plugin's metadata file.
public $pluginAbsolutePath = ''; //Full path of the main plugin file.
public $pluginFile = ''; //Plugin filename relative to the plugins directory. Many WP APIs use this to identify plugins.
@ -216,7 +216,8 @@ class PluginUpdateChecker_1_5 {
//Try to parse the response
$pluginInfo = null;
if ( !is_wp_error($result) && isset($result['response']['code']) && ($result['response']['code'] == 200) && !empty($result['body']) ){
$pluginInfo = PluginInfo_1_3::fromJson($result['body'], $this->debugMode);
$pluginInfo = PluginInfo_1_6::fromJson($result['body'], $this->debugMode);
$pluginInfo->filename = $this->pluginFile;
} else if ( $this->debugMode ) {
$message = sprintf("The URL %s does not point to a valid plugin metadata file. ", $url);
if ( is_wp_error($result) ) {
@ -247,7 +248,7 @@ class PluginUpdateChecker_1_5 {
if ( $pluginInfo == null ){
return null;
}
return PluginUpdate_1_3::fromPluginInfo($pluginInfo);
return PluginUpdate_1_6::fromPluginInfo($pluginInfo);
}
/**
@ -398,7 +399,7 @@ class PluginUpdateChecker_1_5 {
}
if ( !empty($state) && isset($state->update) && is_object($state->update) ){
$state->update = PluginUpdate_1_3::fromObject($state->update);
$state->update = PluginUpdate_1_6::fromObject($state->update);
}
return $state;
}
@ -518,6 +519,7 @@ class PluginUpdateChecker_1_5 {
//Check if the update is actually newer than the currently installed version.
$installedVersion = $this->getInstalledVersion();
if ( ($installedVersion !== null) && version_compare($update->version, $installedVersion, '>') ){
$update->filename = $this->pluginFile;
return $update;
}
}
@ -718,17 +720,17 @@ class PluginUpdateChecker_1_5 {
endif;
if ( !class_exists('PluginInfo_1_3') ):
if ( !class_exists('PluginInfo_1_6') ):
/**
* A container class for holding and transforming various plugin metadata.
*
* @author Janis Elsts
* @copyright 2012
* @version 1.3
* @copyright 2014
* @version 1.6
* @access public
*/
class PluginInfo_1_3 {
class PluginInfo_1_6 {
//Most fields map directly to the contents of the plugin's info.json file.
//See the relevant docs for a description of their meaning.
public $name;
@ -751,6 +753,8 @@ class PluginInfo_1_3 {
public $last_updated;
public $id = 0; //The native WP.org API returns numeric plugin IDs, but they're not used for anything.
public $filename; //Plugin filename relative to the plugins directory.
/**
* Create a new instance of PluginInfo from JSON-encoded plugin info
@ -838,24 +842,26 @@ class PluginInfo_1_3 {
endif;
if ( !class_exists('PluginUpdate_1_3') ):
if ( !class_exists('PluginUpdate_1_6') ):
/**
* A simple container class for holding information about an available update.
*
* @author Janis Elsts
* @copyright 2012
* @version 1.2
* @copyright 2014
* @version 1.6
* @access public
*/
class PluginUpdate_1_3 {
class PluginUpdate_1_6 {
public $id = 0;
public $slug;
public $version;
public $homepage;
public $download_url;
public $upgrade_notice;
private static $fields = array('id', 'slug', 'version', 'homepage', 'download_url', 'upgrade_notice');
public $filename; //Plugin filename relative to the plugins directory.
private static $fields = array('id', 'slug', 'version', 'homepage', 'download_url', 'upgrade_notice', 'filename');
/**
* Create a new instance of PluginUpdate from its JSON-encoded representation.
@ -868,7 +874,7 @@ class PluginUpdate_1_3 {
//Since update-related information is simply a subset of the full plugin info,
//we can parse the update JSON as if it was a plugin info string, then copy over
//the parts that we care about.
$pluginInfo = PluginInfo_1_3::fromJson($json, $triggerErrors);
$pluginInfo = PluginInfo_1_6::fromJson($json, $triggerErrors);
if ( $pluginInfo != null ) {
return self::fromPluginInfo($pluginInfo);
} else {
@ -934,12 +940,14 @@ class PluginUpdate_1_3 {
*/
public function toWpFormat(){
$update = new StdClass;
$update->id = $this->id;
$update->slug = $this->slug;
$update->new_version = $this->version;
$update->url = $this->homepage;
$update->package = $this->download_url;
$update->plugin = $this->filename;
if ( !empty($this->upgrade_notice) ){
$update->upgrade_notice = $this->upgrade_notice;
}
@ -1040,22 +1048,22 @@ class PucFactory {
endif;
//Register classes defined in this file with the factory.
PucFactory::addVersion('PluginUpdateChecker', 'PluginUpdateChecker_1_5', '1.5');
PucFactory::addVersion('PluginUpdate', 'PluginUpdate_1_3', '1.3');
PucFactory::addVersion('PluginInfo', 'PluginInfo_1_3', '1.3');
PucFactory::addVersion('PluginUpdateChecker', 'PluginUpdateChecker_1_6', '1.6');
PucFactory::addVersion('PluginUpdate', 'PluginUpdate_1_6', '1.6');
PucFactory::addVersion('PluginInfo', 'PluginInfo_1_6', '1.6');
/**
* Create non-versioned variants of the update checker classes. This allows for backwards
* compatibility with versions that did not use a factory, and it simplifies doc-comments.
*/
if ( !class_exists('PluginUpdateChecker') ) {
class PluginUpdateChecker extends PluginUpdateChecker_1_5 { }
class PluginUpdateChecker extends PluginUpdateChecker_1_6 { }
}
if ( !class_exists('PluginUpdate') ) {
class PluginUpdate extends PluginUpdate_1_3 {}
class PluginUpdate extends PluginUpdate_1_6 {}
}
if ( !class_exists('PluginInfo') ) {
class PluginInfo extends PluginInfo_1_3 {}
class PluginInfo extends PluginInfo_1_6 {}
}