Update_Plugin
extends Updater
in package
Plugin updater using GitHub repository as the update host.
This class is used to check for updates and display latest released plugin information. The class uses core WordPress plugin update functionality.
The three main class methods are:
- get_update() to return the latest plugin update information.
- get_info() to return the latest plugin release information for the view details popup.
- get_release_info() to retrieve the latest release information from GitHub. This is cached in a transient.
Tags
Table of Contents
Constants
- API_HOST = 'api.github.com'
- The GitHub API host.
- CONTENT_URL = 'https://api.github.com/repos/%1$s/%2$s/contents/%3$s?ref=%4$s'
- Github API content URL template.
- PLUGINS_API_REQS = array('author' => '', 'banners' => array(), 'download_link' => '', 'homepage' => '', 'last_updated' => '', 'name' => '', 'requires_php' => '', 'requires' => '', 'sections' => array(), 'slug' => '', 'tested' => '', 'version' => '')
- The required content for the plugins_api filter.
- RELEASE_HEADERS = array('author' => 'Author', 'authorURI' => 'Author URI', 'description' => 'Description', 'domainPath' => 'Domain Path', 'name' => 'Plugin Name', 'network' => 'Network', 'pluginURI' => 'Plugin URI', 'requires_php' => 'Requires PHP', 'requires' => 'Requires at least', 'requiresPlugins' => 'Requires Plugins', 'tested' => 'Tested up to', 'textDomain' => 'Text Domain', 'updateURI' => 'Update URI', 'version' => 'Version')
- The release plugin file headers to parse.
- REPOSITORY_NAME = ''
- Github repository name.
- REPOSITORY_OWNER = 'krpfeiffer'
- Github content owner.
- TRANSIENT_EXPIRATION_DEBUG = 2 * MINUTE_IN_SECONDS
- Transient expiration for debugging.
- TRANSIENT_EXPIRATION_PRODUCTION = 6 * HOUR_IN_SECONDS
- Transient expiration for production.
- TRANSIENT_SUFFIX = '_release_info'
- Github release info transient key suffix.
- UPDATE_PLUGINS_HOSTNAME_CONTENT = array('autoupdate' => false, 'banners' => array(), 'icons' => array(), 'id' => '', 'package' => '', 'requires_php' => '', 'requires' => '', 'slug' => '', 'tested' => '', 'translations' => array(), 'url' => '', 'version' => '')
- The required content for the update_plugins_hostname filter.
- UPGRADE_ACTIONS = array('update-selected', 'update-plugin')
- The actions to intercept for upgrader source manipulation.
Properties
- $github_token : string
- The GitHub access token.
- $http_handler : HTTP_Handler
- The instance of the HTTP_Handler class.
- $installed_info : array<string|int, mixed>
- The array of installed plugin or theme meta data.
- $instances : array<string|int, object>
- The array of instances of this class and sub-classes.
- $release_transient_expiration : int
- The GitHub response transient expiration.
- $release_transient_key : string
- The GitHub release info transient key.
Methods
- get_info() : false|object
- Filter the plugin information for view details popup.
- get_instance() : object
- Creates and/or returns the instance of this class.
- get_update() : array<string|int, mixed>
- Return the latest plugin release status.
- set_github_credentials() : array<string|int, mixed>
- (Maybe) set GitHub authorisation credentials.
- set_upgrader_source() : string
- Rename the unzipped update folder to be the same as the existing plugin folder.
- __construct() : void
- Initialise the class and set its properties.
- get_file_headers() : array<string|int, mixed>
- Get file headers from GitHub.
- get_image_data() : array<string|int, mixed>
- Get the update details banner and icon image file data.
- get_package_data() : array<string|int, mixed>
- Get the latest release package information from GitHub.
- get_release_info() : array<string|int, mixed>
- Get the latest release information.
- get_release_notes() : string
- Get the release notes in HTML format from GitHub.
- move_update_folder() : string
- Rename (move) the unzipped update folder to have the same name as the existing theme or plugin folder.
- rebuild_transient() : array<string|int, mixed>
- Rebuild transient with information from latest GitHub release.
- set_installed_info() : void
- Set the installed plugin or theme meta data.
Constants
API_HOST
The GitHub API host.
protected
string
API_HOST
= 'api.github.com'
Tags
CONTENT_URL
Github API content URL template.
protected
string
CONTENT_URL
= 'https://api.github.com/repos/%1$s/%2$s/contents/%3$s?ref=%4$s'
The placeholders are: %1$s - repository owner. %2$s - repository name. %3$s - file path. This is the file we want to retrieve from the repository. For example, 'assets/icon.svg', RELEASE.md. %4$s - tag name. This is retrieved from the latest release.
Tags
PLUGINS_API_REQS
The required content for the plugins_api filter.
protected
array<string|int, mixed>
PLUGINS_API_REQS
= array('author' => '', 'banners' => array(), 'download_link' => '', 'homepage' => '', 'last_updated' => '', 'name' => '', 'requires_php' => '', 'requires' => '', 'sections' => array(), 'slug' => '', 'tested' => '', 'version' => '')
- The fields are defined in install_plugin_information() in wp-admin/includes/plugin-install.php.
@type string $author The name of the author of the plugin. @type array $banners Array of plugin banners. @type string $download_link The download link for the plugin update package. @type string $homepage The URL of the plugin homepage. @type string $last_updated The date the plugin was last updated. @type string $name The name of the plugin. @type string $requires_php The version of PHP the plugin requires. @type string $requires The version of WordPress the plugin requires. @type array $sections Array of plugin sections. @type string $slug Directory name of the plugin. @type string $tested The version of WordPress the plugin is tested against. @type string $version The version of the plugin.
Tags
RELEASE_HEADERS
The release plugin file headers to parse.
protected
array<string|int, mixed>
RELEASE_HEADERS
= array('author' => 'Author', 'authorURI' => 'Author URI', 'description' => 'Description', 'domainPath' => 'Domain Path', 'name' => 'Plugin Name', 'network' => 'Network', 'pluginURI' => 'Plugin URI', 'requires_php' => 'Requires PHP', 'requires' => 'Requires at least', 'requiresPlugins' => 'Requires Plugins', 'tested' => 'Tested up to', 'textDomain' => 'Text Domain', 'updateURI' => 'Update URI', 'version' => 'Version')
- List of headers, in the format: array( 'header_key' => 'Header Name' ).
Tags
REPOSITORY_NAME
Github repository name.
protected
string
REPOSITORY_NAME
= ''
This MUST be overridden in sub-classes.
Tags
REPOSITORY_OWNER
Github content owner.
protected
string
REPOSITORY_OWNER
= 'krpfeiffer'
Tags
TRANSIENT_EXPIRATION_DEBUG
Transient expiration for debugging.
protected
int
TRANSIENT_EXPIRATION_DEBUG
= 2 * MINUTE_IN_SECONDS
Tags
TRANSIENT_EXPIRATION_PRODUCTION
Transient expiration for production.
protected
int
TRANSIENT_EXPIRATION_PRODUCTION
= 6 * HOUR_IN_SECONDS
Tags
TRANSIENT_SUFFIX
Github release info transient key suffix.
protected
string
TRANSIENT_SUFFIX
= '_release_info'
Tags
UPDATE_PLUGINS_HOSTNAME_CONTENT
The required content for the update_plugins_hostname filter.
protected
array<string|int, mixed>
UPDATE_PLUGINS_HOSTNAME_CONTENT
= array('autoupdate' => false, 'banners' => array(), 'icons' => array(), 'id' => '', 'package' => '', 'requires_php' => '', 'requires' => '', 'slug' => '', 'tested' => '', 'translations' => array(), 'url' => '', 'version' => '')
@type bool $autoupdate Optional. Whether the plugin should automatically update. Defaults to false.
@type array $banners Optional. Array of plugin banners.
@type array $icons Optional. Array of plugin icons.
@type string $id Optional. ID of the plugin for update purposes, should be a URI specified in the Update URI header field.
@type string $package The URL to the update ZIP for the plugin.
@type string $requires Optional. The version of WordPress the plugin requires.
@type string $requires_php Optional. The version of PHP the plugin requires.
@type string $slug Directory name of the plugin.
@type string $tested Optional. The version of WordPress the plugin is tested against.
@type array $translations Optional. List of translation updates for the plugin.
@type string $url The URL for details of the plugin.
@type string $version The version of the plugin.
Tags
UPGRADE_ACTIONS
The actions to intercept for upgrader source manipulation.
protected
array<string|int, string>
UPGRADE_ACTIONS
= array('update-selected', 'update-plugin')
Tags
Properties
$github_token
The GitHub access token.
protected
string
$github_token
= ''
- The token is retrieved from the plugin settings.
Tags
$http_handler
The instance of the HTTP_Handler class.
protected
HTTP_Handler
$http_handler
.
Tags
$installed_info
The array of installed plugin or theme meta data.
protected
array<string|int, mixed>
$installed_info
= array()
Tags
$instances
The array of instances of this class and sub-classes.
protected
static array<string|int, object>
$instances
= array()
Tags
$release_transient_expiration
The GitHub response transient expiration.
protected
int
$release_transient_expiration
= 0
- Short expiration in development. 6-hour expiration in production.
- The value is set in class constructor.
Tags
$release_transient_key
The GitHub release info transient key.
protected
string
$release_transient_key
= ''
Tags
Methods
get_info()
Filter the plugin information for view details popup.
public
get_info(array<string|int, mixed>|false|object $result, string $action, object $args) : false|object
Parameters
- $result : array<string|int, mixed>|false|object
-
The result object or array. Default false.
- $action : string
-
The type of information being requested from the plugin installation API.
- $args : object
-
Plugin API arguments.
Tags
Return values
false|object —The result. Must be an object or false.
get_instance()
Creates and/or returns the instance of this class.
public
static get_instance() : object
Tags
Return values
object —The instance of this class.
get_update()
Return the latest plugin release status.
public
get_update(array<string|int, mixed>|false $update, array<string|int, mixed> $plugin_data, string $plugin_basename, array<string|int, string> $locales) : array<string|int, mixed>
Parameters
- $update : array<string|int, mixed>|false
-
The plugin update data with the latest details. Default false.
- $plugin_data : array<string|int, mixed>
-
Plugin headers.
- $plugin_basename : string
-
Plugin basename.
- $locales : array<string|int, string>
-
Installed locales to look up translations for.
Tags
Return values
array<string|int, mixed> —The (maybe) updated plugin information.
set_github_credentials()
(Maybe) set GitHub authorisation credentials.
public
set_github_credentials(array<string|int, mixed> $args, string $url) : array<string|int, mixed>
- Intercept HHTP requests and add authorisation token if request is to the GitHub api by the GitHub repository owner.
Parameters
- $args : array<string|int, mixed>
-
HTTP request arguments.
- $url : string
-
The request URL.
Tags
Return values
array<string|int, mixed> —The (maybe) updated args.
set_upgrader_source()
Rename the unzipped update folder to be the same as the existing plugin folder.
public
set_upgrader_source(string $source, string $remote_source, WP_Upgrader $upgrader, array<string|int, mixed> $hook_extra) : string
- The GitHub release archive zip file structure usually means that WordPress assigns a random name to the unzipped folder.
- This method renames the unzipped folder to allow the core update process to work as expected.
Parameters
- $source : string
-
The location of the unzipped update files.
- $remote_source : string
-
The temporary location of the uploaded zip folder.
- $upgrader : WP_Upgrader
-
The WP_Upgrader instance.
- $hook_extra : array<string|int, mixed>
-
Extra arguments passed to hooked filters.
Tags
Return values
string —The (maybe) updated source location.
__construct()
Initialise the class and set its properties.
protected
__construct() : void
Tags
get_file_headers()
Get file headers from GitHub.
protected
get_file_headers(string $url) : array<string|int, mixed>
Parameters
- $url : string
-
The URL to use to extract header information.
Tags
Return values
array<string|int, mixed> —The file header information.
get_image_data()
Get the update details banner and icon image file data.
protected
get_image_data() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>get_package_data()
Get the latest release package information from GitHub.
protected
get_package_data(string $update_url) : array<string|int, mixed>
Parameters
- $update_url : string
-
The URL at which the latest release should be found.
Tags
Return values
array<string|int, mixed> —The array of data about the release returned from GitHub.
get_release_info()
Get the latest release information.
protected
get_release_info() : array<string|int, mixed>
- A transient is used to cache response so we don't hammer the GitHub API.
- Some values are duplicated, as different WordPress functions expect different keys.
- Not all fields will be populated, as some are not relevant to plugins or themes.
Tags
Return values
array<string|int, mixed> —The latest plugin or theme release information. @type string $api_url The API URL for the release. @type array $assets The assets for the release. @type string $assets_url The URL for the release assets. @type string $author The author of the release plugin or theme. @type string $authorURI The author URL. @type bool $autoupdate Whether the plugin should automatically update. @type array $banners The array of banner images for the view details page. @type string $body The text release notes of the release. @type string $created_at The date the release was created. @type string $description The description of the plugin or theme. @type string $domainPath The path to the language files. @type string $download_link The download link for the release. Same as zipball_url. @type bool $draft Whether the release is a draft. @type string $homepage The homepage of the plugin. Same as pluginURI. @type string $html_url The HTML URL for the release. @type array $icons The array of plugin or theme icons for the updates page. @type int $id The ID of the release. @type bool $immutable Whether the release is immutable. @type string $last_updated The date the release was last updated. @type string $name The plugin or theme name @type string $network The network the plugin or theme is associated with. @type string $node_id The node ID of the release. @type string $package The package URL for the release. Sames as zipball_url. @type string $pluginURI The plugin URI. @type bool $prerelease Whether the release is a pre-release. @type string $published_at The date the release was published. @type string $release_name The name of the release. @type string $requires The version of WordPress required by the plugin or theme. @type string $requires_php The version of PHP required by the plugin or theme. @type string $requiresPlugins The plugins required by the plugin. @type array $sections The sections for the plugin details page. @type string $description The description section. Same as description. @type string $changelog The changelog section. This is the HTML formatted release notes. @type string $slug The slug of the plugin or theme. This is the plugin or theme folder name. @type string $tag_name The tag name of the latest release. @type string $tarball_url The tarball URL for the release. @type string $target_commitish The target commitish for the release. @type string $tested The version of WordPress the plugin or theme is tested against. @type string $textDomain The text domain of the plugin or theme. @type string $theme The directory name of the theme. @type string $updateURI The update URI for the plugin or theme. @type string $upload_url The upload URL for the release assets. @type string $url The URL for the plugin. Same as pluginURI. @type string $version The version of the plugin or theme update. Follows SemVer standard. @type string $zipball_url The zipball URL for the release.
get_release_notes()
Get the release notes in HTML format from GitHub.
protected
get_release_notes(string $tag_name) : string
Parameters
- $tag_name : string
-
The tag name of the release to target.
Tags
Return values
string —The release notes in HTML format.
move_update_folder()
Rename (move) the unzipped update folder to have the same name as the existing theme or plugin folder.
protected
move_update_folder(string $source, string $slug) : string
Parameters
- $source : string
-
The location of the unzipped files, within a temporary folder.
- $slug : string
-
The name of the theme or plugin folder.
Tags
Return values
string —The updated source location.
rebuild_transient()
Rebuild transient with information from latest GitHub release.
protected
rebuild_transient() : array<string|int, mixed>
- Get the latest release metadata from GitHub.
- Extended metadata is retrieved from the headers of the main plugin file included in the release.
- The changelog is retrieved from the release notes (RELEASE.md) in the repository.
Tags
Return values
array<string|int, mixed> —The release information array.
set_installed_info()
Set the installed plugin or theme meta data.
protected
set_installed_info() : void
- This method MUST be overridden in sub-classes.