Skip to main content

Reference

This page covers the public API, which is in three types: the Updater builder, the PackageUpdater that the register() method returns, and the Settings object that the settings() method of the PackageUpdater returns. The Settings object is the updater-provided settings section. The page also covers the names that the updater derives from the consumer-provided prefix. The three types are in the ScottOffen\WpGithubUpdater namespace.

The following diagram shows how the three types connect. The plugin() and theme() methods return the Updater, and each arrow is labeled with the method that returns the next type.

Summary​

The following tables list the members of each type that this page describes. Each member name includes the type that the member belongs to.

Updater​

MemberDescription
Updater::plugin()Starts the setup for a plugin.
Updater::theme()Starts the setup for a theme.
Updater::uninstall()Removes the saved token and the stored state of the last check for a prefix.
Updater::fromGitHub()Sets the repository that the releases are in.
Updater::prefix()Sets the prefix that the names of the token constant and the options are derived from.
Updater::capability()Sets the capability that the settings section requires.
Updater::assetPattern()Sets the pattern for the file name of the release asset.
Updater::register()Registers the update hooks and returns the updater.

PackageUpdater​

MemberDescription
PackageUpdater::settings()Returns the updater-provided settings section.
PackageUpdater::capability()Returns the capability that the settings section requires.

Settings​

MemberDescription
Settings::render()Prints the settings section on a page.
Settings::addPage()Adds a separate page for the settings section to the admin menu.

Entry Points​

These methods are static methods of the Updater class.

Updater::plugin()​

public static function plugin(string $file): Updater

Starts the setup for a plugin and returns the builder so that additional calls can be chained.

ParameterTypeDescription
$filestringThe main file of the plugin, which is __FILE__ in that file.

Updater::theme()​

public static function theme(string $file): Updater

Starts the setup for a theme and returns the builder so that additional calls can be chained.

ParameterTypeDescription
$filestringThe functions.php file of the theme, which is __FILE__ in that file.

Updater::uninstall()​

public static function uninstall(string $prefix): void

Removes the saved token and the stored state of the last check for a prefix. Nothing else is removed. A token constant in wp-config.php is not removed, and the saved token is removed even when a constant is defined. A plugin calls this method from its uninstall.php file. See Removing the Token on Uninstall.

ParameterTypeDescription
$prefixstringThe prefix that the plugin or theme was set up with.
ExceptionThrown when
InvalidArgumentExceptionThe prefix is not lowercase letters, digits, and underscores that start with a letter. Nothing is removed.

Builder Methods​

These methods belong to the Updater builder that the plugin() and theme() methods return. Each builder method returns the builder so that additional calls can be chained.

Updater::fromGitHub()​

public function fromGitHub(string $repository): Updater

Sets the repository that the releases are in. The repository is required, and register() throws an exception when it was not set.

ParameterTypeDescription
$repositorystringThe repository, written owner/repository. Replace owner with the user or organization that owns the repository, and repository with the name of the repository.

Updater::prefix()​

public function prefix(string $prefix): Updater

Sets the prefix that the names of the token constant and the options are derived from. The prefix is required, and register() throws an exception when it was not set. See Names From the Prefix.

ParameterTypeDescription
$prefixstringA prefix of lowercase letters, digits, and underscores, starting with a letter. Each plugin and theme on a site must have its own prefix.

Updater::capability()​

public function capability(string $capability): Updater

Sets the capability that the settings section requires. The default is manage_options. See Capability.

ParameterTypeDescription
$capabilitystringThe capability, using letters, digits, underscores, and hyphens.
ExceptionThrown when
InvalidArgumentExceptionThe capability is empty or has any other character.

Updater::assetPattern()​

public function assetPattern(string $pattern): Updater

Sets the pattern for the file name of the release asset. The default is <slug>-*.zip, where the slug is the name of the plugin or theme folder. See Custom Asset Names.

ParameterTypeDescription
$patternstringA file name pattern, such as exampleplugin-*.zip. In the pattern, * stands for any run of characters, ? stands for one character, and a set of characters in square brackets, such as [0-9], stands for one character from the set.
ExceptionThrown when
InvalidArgumentExceptionThe pattern is empty or has a slash.

Updater::register()​

public function register(): PackageUpdater

Registers the update hooks and returns the PackageUpdater. A second call on the same builder returns the updater from the first call.

ExceptionThrown when
LogicExceptionfromGitHub() or prefix() was not called.
InvalidArgumentExceptionThe repository is not written owner/repository, the prefix is not valid, the file of a plugin is not a PHP file, the plugin is not in a folder inside the plugins folder, the file of a theme is not functions.php, or the name of the folder is not a valid slug.
RuntimeExceptionThe setup is for a plugin and the WP_PLUGIN_DIR constant is not defined as a string.

PackageUpdater Methods​

These methods belong to the PackageUpdater that the register() method returns.

PackageUpdater::settings()​

public function settings(): Settings

Returns the updater-provided settings section. See Updater-Provided Settings Section.

PackageUpdater::capability()​

public function capability(): string

Returns the capability that the settings section requires, which is manage_options unless the Updater::capability() builder method set another one. Use it with current_user_can() to check the same capability on your own page.

Settings Methods​

These methods belong to the Settings object that the PackageUpdater::settings() method returns.

Settings::render()​

public function render(string $heading = ''): void

Prints the settings section at the place where it is called. It prints nothing for someone who does not have the capability, and it must be called outside any <form> element. See Show the Section on an Existing Page.

ParameterTypeDescription
$headingstringA heading that appears above the section. The default is no heading.

Settings::addPage()​

public function addPage(string $title, string $parent): void

Adds a separate page for the settings section to the admin menu. The updater adds the page when WordPress builds the admin menu, so call the method when the plugin or theme loads. See Add a Separate Page.

ParameterTypeDescription
$titlestringThe title of the page, which is also the label of its menu entry.
$parentstringThe slug of the admin menu to add the page under. The slug options-general.php adds the page under Settings, and themes.php adds it under Appearance.
ExceptionThrown when
InvalidArgumentExceptionThe title or the parent is empty.

Names From the Prefix​

The following table describes the names that the updater derives from the prefix. In the table, <prefix> is the prefix in lowercase and <PREFIX> is the prefix in uppercase. The last column shows the names for the prefix exampleplugin.

NameDescriptionFor exampleplugin
<PREFIX>_GITHUB_TOKENThe constant where the token can be defined. See Configuring the Token.EXAMPLEPLUGIN_GITHUB_TOKEN
<prefix>_updater_github_tokenThe option where the saved token is stored.exampleplugin_updater_github_token
<prefix>_updater_release_stateThe option where the state of the last check is stored.exampleplugin_updater_release_state
<prefix>_updater_settingsThe slug of the page that addPage() adds.exampleplugin_updater_settings