WooCommerce Code Reference

PluginsHelper
in package

Class PluginsHelper

Table of Contents

DISMISS_CONNECT_NOTICE  = 'woo_connect_notice_dismiss'
Meta key for dismissing connected notice
DISMISS_CONNECTED_ACCOUNT_NOTICE  = 'woo_connected_account_notice_dismiss'
Meta key for dismissing the connected account notice on My Subscriptions
DISMISS_DISCONNECT_NOTICE  = 'woo_disconnect_notice_dismiss'
Meta key for dismissing disconnected notice
DISMISS_EXPIRED_SUBS_NOTICE  = 'woo_subscription_expired_notice_dismiss'
Meta key for dismissing expired subscription notices.
DISMISS_EXPIRING_SUBS_NOTICE  = 'woo_subscription_expiring_notice_dismiss'
Meta key for dismissing expiring subscription notices
DISMISS_MISSING_SUBS_NOTICE  = 'woo_subscription_missing_notice_dismiss'
Meta key for dismissing missing subscription notices
WOO_ADD_PAYMENT_METHOD_URL  = 'https://woocommerce.com/my-account/add-payment-method/'
The URL for the WooCommerce.com add payment method page.
WOO_CART_PAGE_URL  = 'https://woocommerce.com/cart/'
The URL for the WooCommerce.com cart page.
WOO_SUBSCRIPTION_PAGE_URL  = 'https://woocommerce.com/my-account/my-subscriptions/'
The URL for the WooCommerce subscription page.
MAX_ERROR_REASON_LENGTH  = 300
Longest reason we pass on.
OPAQUE_ERROR_DATA_CODES  = array('unexpected_output')
Error codes whose WP_Error data is machine output rather than a readable detail.
$subscription_usage_notices_already_shown  : bool
Subscription notices in Woo screens are shown in clear priority order, first expired, and if those don't exist, expiring, and finally if none of those exist, then missing. This keeps track of whether we can show the next set of notices.
activate_plugins()  : WP_Error|array<string|int, mixed>
Activate the requested plugins.
get_action_data()  : array<string|int, mixed>
Gets the plugin data for the first action.
get_activation_status()  : array<string|int, mixed>
Activation status.
get_active_plugin_slugs()  : array<string|int, mixed>
Get an array of active plugin slugs.
get_error_reason()  : string
Build a merchant-readable reason from the first WP_Error among the given candidates.
get_expired_subscription_notice()  : array<string|int, mixed>
Get formatted notice information for expired subscription.
get_expiring_subscription_notice()  : array<string|int, mixed>
Get formatted notice information for expiring subscription.
get_installation_status()  : array<string|int, mixed>
Installation status.
get_installed_plugin_slugs()  : array<string|int, mixed>
Get an array of installed plugin slugs.
get_installed_plugins_paths()  : array<string|int, mixed>
Get an array of installed plugins with their file paths as a key value pair.
get_missing_payment_method_notice()  : array<string|int, mixed>
Get the notice data for missing payment method.
get_missing_subscription_notice()  : array<string|int, mixed>
Get formatted notice information for missing subscription.
get_plugin_data()  : array<string|int, mixed>|false
Get plugin data.
get_plugin_path_from_slug()  : string|false
Get the path to the plugin file relative to the plugins directory from the plugin slug.
get_requirements_error_reason()  : string
Compose a reason for an install that failed an unmet PHP or WordPress version requirement.
get_subscriptions_notice_data()  : array<string|int, mixed>
Construct the subscription notice data based on user subscriptions data.
get_wccom_connected_notice()  : string
Get the connected status notice message.
get_wccom_disconnected_notice()  : string
Get notice information when WCCOM connection is disconnected.
init()  : mixed
Initialize hooks.
install_and_activate_plugins_async_callback()  : bool
Callback registered by OnboardingPlugins::install_and_activate_async.
install_plugins()  : array<string|int, mixed>
Install an array of plugins.
is_plugin_active()  : bool
Checks if a plugin is active.
is_plugin_installed()  : bool
Checks if a plugin is installed.
maybe_enqueue_scripts_for_connect_notice()  : void
Enqueue scripts for connect notice in WooCommerce settings page.
maybe_enqueue_scripts_for_notices_in_plugins()  : void
Enqueue scripts for notices in plugin list page.
maybe_enqueue_scripts_for_subscription_notice()  : void
Enqueue scripts for woo subscription notice.
maybe_show_connect_notice_in_plugin_list()  : void
Show notices to connect to woocommerce.com for unconnected store in the plugin list.
maybe_show_expired_subscriptions_notice()  : void
Show notice about to expired subscription on WC settings page.
maybe_show_expiring_subscriptions_notice()  : void
Show notice about to expiring subscription on WC settings page.
schedule_activate_plugins()  : string
Schedule plugin activation.
schedule_install_plugins()  : string
Schedule plugin installation.
should_show_notice()  : bool
Determine whether a specific notice should be shown to the current user.
get_required_version()  : string
Read a version requirement from plugin header data.
has_active_usable_product_subscription()  : bool
Check whether the current store has an active usable subscription for a product.
normalize_reason()  : string
Make a reason safe to show, store and transmit.

Constants

DISMISS_CONNECT_NOTICE

Meta key for dismissing connected notice

public mixed DISMISS_CONNECT_NOTICE = 'woo_connect_notice_dismiss'

DISMISS_CONNECTED_ACCOUNT_NOTICE

Meta key for dismissing the connected account notice on My Subscriptions

public mixed DISMISS_CONNECTED_ACCOUNT_NOTICE = 'woo_connected_account_notice_dismiss'

DISMISS_DISCONNECT_NOTICE

Meta key for dismissing disconnected notice

public mixed DISMISS_DISCONNECT_NOTICE = 'woo_disconnect_notice_dismiss'

DISMISS_EXPIRED_SUBS_NOTICE

Meta key for dismissing expired subscription notices.

public mixed DISMISS_EXPIRED_SUBS_NOTICE = 'woo_subscription_expired_notice_dismiss'

DISMISS_EXPIRING_SUBS_NOTICE

Meta key for dismissing expiring subscription notices

public mixed DISMISS_EXPIRING_SUBS_NOTICE = 'woo_subscription_expiring_notice_dismiss'

DISMISS_MISSING_SUBS_NOTICE

Meta key for dismissing missing subscription notices

public mixed DISMISS_MISSING_SUBS_NOTICE = 'woo_subscription_missing_notice_dismiss'

WOO_ADD_PAYMENT_METHOD_URL

The URL for the WooCommerce.com add payment method page.

public mixed WOO_ADD_PAYMENT_METHOD_URL = 'https://woocommerce.com/my-account/add-payment-method/'

WOO_CART_PAGE_URL

The URL for the WooCommerce.com cart page.

public mixed WOO_CART_PAGE_URL = 'https://woocommerce.com/cart/'

WOO_SUBSCRIPTION_PAGE_URL

The URL for the WooCommerce subscription page.

public mixed WOO_SUBSCRIPTION_PAGE_URL = 'https://woocommerce.com/my-account/my-subscriptions/'

MAX_ERROR_REASON_LENGTH

Longest reason we pass on.

private mixed MAX_ERROR_REASON_LENGTH = 300

Reasons reach a notice, the persisted install log option and a Tracks property, so an upgrader payload of unknown size must never travel untrimmed.

OPAQUE_ERROR_DATA_CODES

Error codes whose WP_Error data is machine output rather than a readable detail.

private mixed OPAQUE_ERROR_DATA_CODES = array('unexpected_output')

activate_plugin() puts the plugin's whole captured output buffer in the error data, which is never something a merchant can act on.

Properties

$subscription_usage_notices_already_shown

Subscription notices in Woo screens are shown in clear priority order, first expired, and if those don't exist, expiring, and finally if none of those exist, then missing. This keeps track of whether we can show the next set of notices.

public static bool $subscription_usage_notices_already_shown = false

Methods

activate_plugins()

Activate the requested plugins.

public static activate_plugins(array<string|int, mixed> $plugins[, PluginsInstallLogger|null $logger = null ]) : WP_Error|array<string|int, mixed>
Parameters
$plugins : array<string|int, mixed>

Plugins.

$logger : PluginsInstallLogger|null = null

Logger.

Return values
WP_Error|array<string|int, mixed> — Plugin Status

get_action_data()

Gets the plugin data for the first action.

public static get_action_data(array<string|int, mixed> $actions) : array<string|int, mixed>
Parameters
$actions : array<string|int, mixed>

Array of AS actions.

Return values
array<string|int, mixed> — Array of action data.

get_activation_status()

Activation status.

public static get_activation_status([int $job_id = null ]) : array<string|int, mixed>
Parameters
$job_id : int = null

Job ID.

Return values
array<string|int, mixed> — Array of action data.

get_active_plugin_slugs()

Get an array of active plugin slugs.

public static get_active_plugin_slugs() : array<string|int, mixed>

The list will include both network active and site active plugins.

Return values
array<string|int, mixed> — The list of active plugin slugs.

get_error_reason()

Build a merchant-readable reason from the first WP_Error among the given candidates.

public static get_error_reason(mixed ...$candidates) : string

WordPress's upgrader reports the real failure (unmet PHP/WP version, filesystem, download) as a WP_Error whose message is a short sentence and whose data is often the detail sentence. Non-error candidates (false, null, true) are skipped.

The data is only appended when it reads as a detail: codes listed in OPAQUE_ERROR_DATA_CODES carry raw plugin output instead, and the result is capped at MAX_ERROR_REASON_LENGTH because callers persist and transmit it.

Parameters
$candidates : mixed

Values that may be WP_Error instances, in order of preference.

Tags
since
11.2.0
Return values
string — The reason, HTML-stripped, trimmed and length-capped, or an empty string when none is available.

get_expired_subscription_notice()

Get formatted notice information for expired subscription.

public static get_expired_subscription_notice([bool $allowed_link = true ]) : array<string|int, mixed>
Parameters
$allowed_link : bool = true

whether the notice description should include a link.

Return values
array<string|int, mixed> — notice information.

get_expiring_subscription_notice()

Get formatted notice information for expiring subscription.

public static get_expiring_subscription_notice([bool $allowed_link = true ]) : array<string|int, mixed>
Parameters
$allowed_link : bool = true

whether the notice description should include a link.

Return values
array<string|int, mixed> — notice information.

get_installation_status()

Installation status.

public static get_installation_status([int $job_id = null ]) : array<string|int, mixed>
Parameters
$job_id : int = null

Job ID.

Return values
array<string|int, mixed> — Job data.

get_installed_plugin_slugs()

Get an array of installed plugin slugs.

public static get_installed_plugin_slugs() : array<string|int, mixed>
Return values
array<string|int, mixed> —

get_installed_plugins_paths()

Get an array of installed plugins with their file paths as a key value pair.

public static get_installed_plugins_paths() : array<string|int, mixed>
Return values
array<string|int, mixed> —

get_missing_payment_method_notice()

Get the notice data for missing payment method.

public static get_missing_payment_method_notice([bool $allowed_link = true ][, int $total_expiring_subscriptions = 1 ]) : array<string|int, mixed>
Parameters
$allowed_link : bool = true

whether should show link on the notice or not.

$total_expiring_subscriptions : int = 1

total expiring subscriptions.

Return values
array<string|int, mixed> — the notices data.

get_missing_subscription_notice()

Get formatted notice information for missing subscription.

public static get_missing_subscription_notice() : array<string|int, mixed>
Return values
array<string|int, mixed> — notice information.

get_plugin_data()

Get plugin data.

public static get_plugin_data(string $plugin) : array<string|int, mixed>|false
Parameters
$plugin : string

Path to the plugin file relative to the plugins directory or the plugin directory name.

Return values
array<string|int, mixed>|false —

get_plugin_path_from_slug()

Get the path to the plugin file relative to the plugins directory from the plugin slug.

public static get_plugin_path_from_slug(string $slug) : string|false

E.g. 'woocommerce' returns 'woocommerce/woocommerce.php'

Parameters
$slug : string

Plugin slug to get path for.

Return values
string|false — The plugin path or false if the plugin is not installed.

get_requirements_error_reason()

Compose a reason for an install that failed an unmet PHP or WordPress version requirement.

public static get_requirements_error_reason(mixed $error, mixed $plugin_data) : string

WordPress reports these with copy written for the zip-upload flow ("the uploaded plugin requires"), so build our own sentence from the versions it actually checked: the package headers it read in Plugin_Upgrader::check_package(), which can differ from the wp.org listing metadata.

Parameters
$error : mixed

The WP_Error from the upgrader skin, or any non-error value.

$plugin_data : mixed

The headers of the package that failed, as Plugin_Upgrader::$new_plugin_data holds them.

Tags
since
11.2.0
Return values
string — The reason, or an empty string when the error is not a requirement failure.

get_subscriptions_notice_data()

Construct the subscription notice data based on user subscriptions data.

public static get_subscriptions_notice_data(array<string|int, mixed> $all_subs, array<string|int, mixed> $subs_to_show, int $total, array<string|int, mixed> $messages, string $type) : array<string|int, mixed>
Parameters
$all_subs : array<string|int, mixed>

all subscription data.

$subs_to_show : array<string|int, mixed>

filtered subscriptions as condition.

$total : int

total subscription count.

$messages : array<string|int, mixed>

message.

$type : string

type of notice, whether it is for expiring or expired subscription.

Return values
array<string|int, mixed> — notice data to return. Contains type, parsed_message and product_id (can be a single value or an array).

get_wccom_connected_notice()

Get the connected status notice message.

public static get_wccom_connected_notice(string $user_email) : string
Parameters
$user_email : string

the user email.

Return values
string — the connected notice message.

get_wccom_disconnected_notice()

Get notice information when WCCOM connection is disconnected.

public static get_wccom_disconnected_notice() : string
Return values
string — disconnect notice.

install_and_activate_plugins_async_callback()

Callback registered by OnboardingPlugins::install_and_activate_async.

public static install_and_activate_plugins_async_callback(array<string|int, mixed> $plugins, string $job_id[, string|null $source = null ]) : bool

It is used to call install_plugins and activate_plugins with a custom logger.

Parameters
$plugins : array<string|int, mixed>

A list of plugins to install.

$job_id : string

An unique job I.D.

$source : string|null = null

The source of the request.

Return values
bool —

install_plugins()

Install an array of plugins.

public static install_plugins(array<string|int, mixed> $plugins[, PluginsInstallLogger|null $logger = null ][, string|null $source = null ]) : array<string|int, mixed>
Parameters
$plugins : array<string|int, mixed>

Plugins to install.

$logger : PluginsInstallLogger|null = null

an optional logger.

$source : string|null = null

place where the request is coming from.

Return values
array<string|int, mixed> —

is_plugin_active()

Checks if a plugin is active.

public static is_plugin_active(string $plugin) : bool
Parameters
$plugin : string

Path to the plugin file relative to the plugins directory or the plugin directory name.

Return values
bool —

is_plugin_installed()

Checks if a plugin is installed.

public static is_plugin_installed(string $plugin) : bool
Parameters
$plugin : string

Path to the plugin file relative to the plugins directory or the plugin directory name.

Return values
bool —

maybe_enqueue_scripts_for_connect_notice()

Enqueue scripts for connect notice in WooCommerce settings page.

public static maybe_enqueue_scripts_for_connect_notice() : void
Return values
void —

maybe_enqueue_scripts_for_notices_in_plugins()

Enqueue scripts for notices in plugin list page.

public static maybe_enqueue_scripts_for_notices_in_plugins() : void
Return values
void —

maybe_enqueue_scripts_for_subscription_notice()

Enqueue scripts for woo subscription notice.

public static maybe_enqueue_scripts_for_subscription_notice() : void
Return values
void —

maybe_show_connect_notice_in_plugin_list()

Show notices to connect to woocommerce.com for unconnected store in the plugin list.

public static maybe_show_connect_notice_in_plugin_list() : void
Return values
void —

maybe_show_expired_subscriptions_notice()

Show notice about to expired subscription on WC settings page.

public static maybe_show_expired_subscriptions_notice() : void
Return values
void —

maybe_show_expiring_subscriptions_notice()

Show notice about to expiring subscription on WC settings page.

public static maybe_show_expiring_subscriptions_notice() : void
Return values
void —

schedule_activate_plugins()

Schedule plugin activation.

public static schedule_activate_plugins(array<string|int, mixed> $plugins) : string
Parameters
$plugins : array<string|int, mixed>

Plugins to activate.

Return values
string — Job ID.

schedule_install_plugins()

Schedule plugin installation.

public static schedule_install_plugins(array<string|int, mixed> $plugins) : string
Parameters
$plugins : array<string|int, mixed>

Plugins to install.

Return values
string — Job ID.

should_show_notice()

Determine whether a specific notice should be shown to the current user.

public static should_show_notice(string $dismiss_notice_meta[, bool $show_after_one_month = true ]) : bool
Parameters
$dismiss_notice_meta : string

User meta that includes the timestamp when a store notice was dismissed.

$show_after_one_month : bool = true

Show the notices dismissed earlier than one month.

Return values
bool — True if the notice should be shown, false otherwise.

get_required_version()

Read a version requirement from plugin header data.

private static get_required_version(mixed $value) : string

The headers come from get_plugin_data() on the unpacked package, whose values pass through filters, so the value is not guaranteed to be a string.

Parameters
$value : mixed

The raw RequiresPHP or RequiresWP value.

Tags
since
11.2.0
Return values
string — The version, or an empty string when the value is not one.

has_active_usable_product_subscription()

Check whether the current store has an active usable subscription for a product.

private static has_active_usable_product_subscription(int $product_id, array<string|int, mixed> $subscriptions) : bool
Parameters
$product_id : int

Product id.

$subscriptions : array<string|int, mixed>

Subscription list data.

Return values
bool —

normalize_reason()

Make a reason safe to show, store and transmit.

private static normalize_reason(string $text) : string

Reasons come from WordPress and from values that reach us through filters, so they can carry markup or be arbitrarily long. WordPress also writes some messages as multiple paragraphs, so block boundaries become spaces first: stripping the tags would otherwise run the last word of one paragraph into the first word of the next.

Parameters
$text : string

The raw reason.

Tags
since
11.2.0
Return values
string — The reason, HTML-stripped, whitespace-collapsed and length-capped.