Updater
in package
Plugin and theme updater using GitHub repository as update source.
This class is the base class for plugin and theme updaters.
- Provide methods to check for updates and display latest released plugin or theme information.
- Handle the authorisation and interaaction with the GitHub API to retrieve the latest release information.
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.
- RELEASE_HEADERS = array()
- The release main 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.
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_instance() : object
- Creates and/or returns the instance of this class.
- set_github_credentials() : array<string|int, mixed>
- (Maybe) set GitHub authorisation credentials.
- __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.
- 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
RELEASE_HEADERS
The release main file headers to parse.
protected
array<string|int, mixed>
RELEASE_HEADERS
= array()
- List of headers, in the format: array( 'header_key' => 'Header Name' ).
- This MUST be overridden in sub-classes.
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
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_instance()
Creates and/or returns the instance of this class.
public
static get_instance() : object
Tags
Return values
object —The instance of this class.
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.
__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.
set_installed_info()
Set the installed plugin or theme meta data.
protected
set_installed_info() : void
- This method MUST be overridden in sub-classes.