\TGM_Plugin_Activation

Automatic plugin installation and activation library.

Creates a way to automatically install and activate plugins from within themes. The plugins can be either bundled, downloaded from the WordPress Plugin Repository or downloaded from another external source.

Summary

Methods
Properties
Constants
__get()
init()
add_plugin_action_link_filters()
filter_plugin_action_links_activate()
filter_plugin_action_links_deactivate()
filter_plugin_action_links_update()
admin_init()
thickbox()
admin_menu()
install_plugins_page()
inject_update_info()
maybe_adjust_source_dir()
notices()
dismiss()
register()
sanitize_key()
config()
actions()
flush_plugins_cache()
populate_file_path()
_get_plugin_data_from_name()
get_download_url()
get_info_link()
get_tgmpa_url()
get_tgmpa_status_url()
is_tgmpa_complete()
is_plugin_installed()
is_plugin_active()
can_plugin_update()
can_plugin_activate()
get_installed_version()
does_plugin_require_update()
does_plugin_have_update()
get_upgrade_notice()
get_plugins()
update_dismiss()
force_activation()
force_deactivation()
show_tgmpa_version()
get_instance()
$instance
$plugins
$id
$parent_slug
$capability
$default_path
$has_notices
$dismissable
$dismiss_msg
$is_automatic
$message
$strings
$wp_version
$page_hook
TGMPA_VERSION
WP_REPO_REGEX
IS_URL_REGEX
add_admin_menu()
do_plugin_install()
activate_single_plugin()
display_settings_errors()
get_plugin_source_type()
_get_plugin_basename_from_slug()
get_wp_repo_download_url()
get_plugins_api()
is_tgmpa_page()
$sort_order
$has_forced_activation
$has_forced_deactivation
$menu
N/A
No private methods found
No private properties found
N/A

Constants

TGMPA_VERSION

TGMPA_VERSION

TGMPA version number.

WP_REPO_REGEX

WP_REPO_REGEX

Regular expression to test if a URL is a WP plugin repo URL.

IS_URL_REGEX

IS_URL_REGEX

Arbitrary regular expression to test if a string starts with a URL.

Properties

$instance

$instance : \TGM_Plugin_Activation

Holds a copy of itself, so it can be referenced by the class name.

Type

\TGM_Plugin_Activation

$plugins

$plugins : array

Holds arrays of plugin details.

Type

array

$id

$id : string

Name of the unique ID to hash notices.

Type

string

$parent_slug

$parent_slug : string

Parent menu file slug.

Type

string

$capability

$capability : string

Capability needed to view the plugin installation menu item.

Type

string

$default_path

$default_path : string

Default absolute path to folder containing bundled plugin zip files.

Type

string — Absolute path prefix to zip file location for bundled plugins. Default is empty string.

$has_notices

$has_notices : boolean

Flag to show admin notices or not.

Type

boolean

$dismissable

$dismissable : boolean

Flag to determine if the user can dismiss the notice nag.

Type

boolean

$dismiss_msg

$dismiss_msg : string

Message to be output above nag notice if dismissable is false.

Type

string

$is_automatic

$is_automatic : boolean

Flag to set automatic activation of plugins. Off by default.

Type

boolean

$message

$message : string

Optional message to display before the plugins table.

Type

string — Message filtered by wp_kses_post(). Default is empty string.

$strings

$strings : array

Holds configurable array of strings.

Default values are added in the constructor.

Type

array

$wp_version

$wp_version : int

Holds the version of WordPress.

Type

int

$page_hook

$page_hook : string

Holds the hook name for the admin page.

Type

string

$sort_order

$sort_order : array

Holds arrays of plugin names to use to sort the plugins array.

Type

array

$has_forced_activation

$has_forced_activation : bool

Whether any plugins have the 'force_activation' setting set to true.

Type

bool

$has_forced_deactivation

$has_forced_deactivation : bool

Whether any plugins have the 'force_deactivation' setting set to true.

Type

bool

$menu

$menu : string

Name of the query-string argument for the admin page.

Type

string

Methods

__get()

__get(string $name) : mixed

Magic method to get the value of a protected property outside of this class context.

Parameters

string $name

Name of an inaccessible property.

Returns

mixed —

The property value.

init()

init()

Initialise the interactions between this class and WordPress.

Hooks in three new methods for the class: admin_menu, notices and styles.

add_plugin_action_link_filters()

add_plugin_action_link_filters()

Prevent activation of plugins which don't meet the minimum version requirement from the WP native plugins page.

filter_plugin_action_links_activate()

filter_plugin_action_links_activate(array $actions) : array

Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the minimum version requirements.

Parameters

array $actions

Action links.

Returns

array

filter_plugin_action_links_deactivate()

filter_plugin_action_links_deactivate(array $actions) : array

Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate.

Parameters

array $actions

Action links.

Returns

array

filter_plugin_action_links_update()

filter_plugin_action_links_update(array $actions) : array

Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the minimum version requirements.

Parameters

array $actions

Action links.

Returns

array

admin_init()

admin_init() : null

Handles calls to show plugin information via links in the notices.

We get the links in the admin notices to point to the TGMPA page, rather than the typical plugin-install.php file, so we can prepare everything beforehand.

WP does not make it easy to show the plugin information in the thickbox - here we have to require a file that includes a function that does the main work of displaying it, enqueue some styles, set up some globals and finally call that function before exiting.

Down right easy once you know how...

Returns early if not the TGMPA page.

Returns

null —

Returns early if not the TGMPA page.

thickbox()

thickbox()

Enqueue thickbox scripts/styles for plugin info.

Thickbox is not automatically included on all admin pages, so we must manually enqueue it for those pages.

Thickbox is only loaded if the user has not dismissed the admin notice or if there are any plugins left to install and activate.

admin_menu()

admin_menu() : null

Adds submenu page if there are plugin actions to take.

This method adds the submenu page letting users know that a required plugin needs to be installed.

This page disappears once the plugin has been installed and activated.

Returns

null —

Return early if user lacks capability to install a plugin.

install_plugins_page()

install_plugins_page() : null

Echoes plugin installation form.

This method is the callback for the admin_menu method function. This displays the admin page and form area where the user can select to install and activate the plugin. Aborts early if we're processing a plugin installation action.

Returns

null —

Aborts early if we're processing a plugin installation action.

inject_update_info()

inject_update_info(array $plugins)

Inject information into the 'update_plugins' site transient as WP checks that before running an update.

Parameters

array $plugins

The plugin information for the plugins which are to be updated.

maybe_adjust_source_dir()

maybe_adjust_source_dir(string $source, string $remote_source, \WP_Upgrader $upgrader) : string

Adjust the plugin directory name if necessary.

The final destination directory of a plugin is based on the subdirectory name found in the (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this subdirectory name is not the same as the expected slug and the plugin will not be recognized as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to the expected plugin slug.

Parameters

string $source

Path to upgrade/zip-file-name.tmp/subdirectory/.

string $remote_source

Path to upgrade/zip-file-name.tmp.

\WP_Upgrader $upgrader

Instance of the upgrader which installs the plugin.

Returns

string —

$source

notices()

notices() : null

Echoes required plugin notice.

Outputs a message telling users that a specific plugin is required for their theme. If appropriate, it includes a link to the form page where users can install and activate the plugin.

Returns early if we're on the Install page.

Returns

null —

Returns early if we're on the Install page.

dismiss()

dismiss()

Add dismissable admin notices.

Appends a link to the admin nag messages. If clicked, the admin notice disappears and no longer is visible to users.

register()

register(array|null $plugin) : null

Add individual plugin to our collection of plugins.

If the required keys are not set or the plugin has already been registered, the plugin is not added.

Parameters

array|null $plugin

Array of plugin arguments or null if invalid argument.

Returns

null —

Return early if incorrect argument.

sanitize_key()

sanitize_key(string $key) : string

Sanitizes a string key.

Near duplicate of WP Core sanitize_key(). The difference is that uppercase characters are allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase characters in the plugin directory path/slug. Silly them.

Parameters

string $key

String key.

Returns

string —

Sanitized key

config()

config(array $config)

Amend default configuration settings.

Parameters

array $config

Array of config options to pass as class properties.

actions()

actions(array $install_actions) : array

Amend action link after plugin installation.

Parameters

array $install_actions

Existing array of actions.

Returns

array —

Amended array of actions.

flush_plugins_cache()

flush_plugins_cache(bool $clear_update_cache)

Flushes the plugins cache on theme switch to prevent stale entries from remaining in the plugin table.

Parameters

bool $clear_update_cache

Optional. Whether to clear the Plugin updates cache.

                            Parameter added in v2.5.0.

populate_file_path()

populate_file_path(string $plugin_slug)

Set file_path key for each installed plugin.

Parameters

string $plugin_slug

Optional. If set, only (re-)populates the file path for that specific plugin.

                       Parameter added in v2.5.0.

_get_plugin_data_from_name()

_get_plugin_data_from_name(string $name, string $data) : string|boolean

Retrieve plugin data, given the plugin name.

Loops through the registered plugins looking for $name. If it finds it, it returns the $data from that plugin. Otherwise, returns false.

Parameters

string $name

Name of the plugin, as it was registered.

string $data

Optional. Array key of plugin data to return. Default is slug.

Returns

string|boolean —

Plugin slug if found, false otherwise.

get_download_url()

get_download_url(string $slug) : string

Retrieve the download URL for a package.

Parameters

string $slug

Plugin slug.

Returns

string —

Plugin download URL or path to local file or empty string if undetermined.

get_info_link()

get_info_link(string $slug) : string

Retrieve a link to a plugin information page.

Parameters

string $slug

Plugin slug.

Returns

string —

Fully formed html link to a plugin information page if available

           or the plugin name if not.

get_tgmpa_status_url()

get_tgmpa_status_url(string $status) : string

Retrieve the URL to the TGMPA Install page for a specific plugin status (view).

I.e. depending on the config settings passed something along the lines of: http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install

Parameters

string $status

Plugin status - either 'install', 'update' or 'activate'.

Returns

string —

Properly encoded URL (not escaped).

is_tgmpa_complete()

is_tgmpa_complete() : bool

Determine whether there are open actions for plugins registered with TGMPA.

Returns

bool —

True if complete, i.e. no outstanding actions. False otherwise.

is_plugin_installed()

is_plugin_installed(string $slug) : bool

Check if a plugin is installed. Does not take must-use plugins into account.

Parameters

string $slug

Plugin slug.

Returns

bool —

True if installed, false otherwise.

is_plugin_active()

is_plugin_active(string $slug) : bool

Check if a plugin is active.

Parameters

string $slug

Plugin slug.

Returns

bool —

True if active, false otherwise.

can_plugin_update()

can_plugin_update(string $slug) : bool

Check if a plugin can be updated, i.e. if we have information on the minimum WP version required available, check whether the current install meets them.

Parameters

string $slug

Plugin slug.

Returns

bool —

True if OK to update, false otherwise.

can_plugin_activate()

can_plugin_activate(string $slug) : bool

Check if a plugin can be activated, i.e. is not currently active and meets the minimum plugin version requirements set in TGMPA (if any).

Parameters

string $slug

Plugin slug.

Returns

bool —

True if OK to activate, false otherwise.

get_installed_version()

get_installed_version(string $slug) : string

Retrieve the version number of an installed plugin.

Parameters

string $slug

Plugin slug.

Returns

string —

Version number as string or an empty string if the plugin is not installed

           or version unknown (plugins which don't comply with the plugin header standard).

does_plugin_require_update()

does_plugin_require_update(string $slug) : bool

Check whether a plugin complies with the minimum version requirements.

Parameters

string $slug

Plugin slug.

Returns

bool —

True when a plugin needs to be updated, otherwise false.

does_plugin_have_update()

does_plugin_have_update(string $slug) : false|string

Check whether there is an update available for a plugin.

Parameters

string $slug

Plugin slug.

Returns

false|string —

Version number string of the available update or false if no update available.

get_upgrade_notice()

get_upgrade_notice(string $slug) : string

Retrieve potential upgrade notice for a plugin.

Parameters

string $slug

Plugin slug.

Returns

string —

The upgrade notice or an empty string if no message was available or provided.

get_plugins()

get_plugins(string $plugin_folder) : array

Wrapper around the core WP get_plugins function, making sure it's actually available.

Parameters

string $plugin_folder

Optional. Relative path to single plugin folder.

Returns

array —

Array of installed plugins with plugin information.

update_dismiss()

update_dismiss()

Delete dismissable nag option when theme is switched.

This ensures that the user(s) is/are again reminded via nag of required and/or recommended plugins if they re-activate the theme.

force_activation()

force_activation()

Forces plugin activation if the parameter 'force_activation' is set to true.

This allows theme authors to specify certain plugins that must be active at all times while using the current theme.

Please take special care when using this parameter as it has the potential to be harmful if not used correctly. Setting this parameter to true will not allow the specified plugin to be deactivated unless the user switches themes.

force_deactivation()

force_deactivation()

Forces plugin deactivation if the parameter 'force_deactivation' is set to true.

This allows theme authors to specify certain plugins that must be deactivated upon switching from the current theme to another.

Please take special care when using this parameter as it has the potential to be harmful if not used correctly.

show_tgmpa_version()

show_tgmpa_version()

Echo the current TGMPA version number to the page.

get_instance()

get_instance() : object

Returns the singleton instance of the class.

Returns

object —

The TGM_Plugin_Activation object.

add_admin_menu()

add_admin_menu(array $args)

Add the menu item.

Parameters

array $args

Menu item configuration.

do_plugin_install()

do_plugin_install() : boolean

Installs, updates or activates a plugin depending on the action link clicked by the user.

Checks the $_GET variable to see which actions have been passed and responds with the appropriate method.

Uses WP_Filesystem to process and handle the plugin installation method.

Returns

boolean —

True on success, false on failure.

activate_single_plugin()

activate_single_plugin(string $file_path, string $slug, bool $automatic) : bool

Activate a single plugin and send feedback about the result to the screen.

Parameters

string $file_path

Path within wp-plugins/ to main plugin file.

string $slug

Plugin slug.

bool $automatic

Whether this is an automatic activation after an install. Defaults to false.

                     This determines the styling of the output messages.

Returns

bool —

False if an error was encountered, true otherwise.

display_settings_errors()

display_settings_errors()

Display settings errors and remove those which have been displayed to avoid duplicate messages showing

get_plugin_source_type()

get_plugin_source_type(string $source) : string

Determine what type of source the plugin comes from.

Parameters

string $source

The source of the plugin as provided, either empty (= WP repo), a file path

                  (= bundled) or an external URL.

Returns

string —

'repo', 'external', or 'bundled'

_get_plugin_basename_from_slug()

_get_plugin_basename_from_slug(string $slug) : string

Helper function to extract the file path of the plugin file from the plugin slug, if the plugin is installed.

Parameters

string $slug

Plugin slug (typically folder name) as provided by the developer.

Returns

string —

Either file path for plugin if installed, or just the plugin slug.

get_wp_repo_download_url()

get_wp_repo_download_url(string $slug) : string

Retrieve the download URL for a WP repo package.

Parameters

string $slug

Plugin slug.

Returns

string —

Plugin download URL.

get_plugins_api()

get_plugins_api(string $slug) : object

Try to grab information from WordPress API.

Parameters

string $slug

Plugin slug.

Returns

object —

Plugins_api response object on success, WP_Error on failure.

is_tgmpa_page()

is_tgmpa_page() : boolean

Determine if we're on the TGMPA Install page.

Returns

boolean —

True when on the TGMPA page, false otherwise.