if (\class_exists($classname) && \defined("{$classname}::ITEM_ID")) {
if ($id = $classname::ITEM_ID ?? null) {
$data['ITEM_ID'] = \absint($id);
$barn2_installed[] = $data;
}
}
}
}
}
}
return $barn2_installed;
}
/**
* Check if a plugin is installed on the WordPress site (whether active or not).
*
* @param string $plugin_file The plugin file relative to the plugins directory. e.g. my-plugin/my-plugin.php
* @return bool
*/
public static function is_plugin_installed($plugin_file)
{
if (!\function_exists('get_plugins')) {
require_once \ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugins = \get_plugins();
if (\strpos($plugin_file, '/') !== \false) {
return isset($plugins[$plugin_file]);
}
foreach ($plugins as $plugin_path => $plugin_data) {
if (\basename($plugin_path) === $plugin_file) {
return \true;
}
}
return \false;
}
/**
* Sanitize anything.
*
* @param mixed $var the thing to sanitize.
* @return mixed
*/
public static function clean($var)
{
if (\is_array($var)) {
return \array_map([__CLASS__, 'clean'], $var);
} else {
return \is_scalar($var) ? \sanitize_text_field($var) : $var;
}
}
/**
* Declare compatibility with the High-Performance Order Storage
* feature in WooCommerce.
*
* @param string $plugin_entry_file The main plugin file.
* @param bool $compatible Whether the plugin is compatible with HPOS.
* @return void
* @deprecated 1.6.0 HPOS compatibility is handled automatically by Simple_Plugin so this function is not needed.
*/
public static function declare_hpos_compatibility($plugin_entry_file, $compatible = \true)
{
\_deprecated_function(__METHOD__, '1.6.0', 'Handled automatically by Simple_Plugin');
\add_action('before_woocommerce_init', function () use($plugin_entry_file, $compatible) {
if (\class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', $plugin_entry_file, $compatible);
}
});
}
/**
* Get the link to a plugin on wordpress.org.
*
* @param string $plugin_name
* @param string $plugin_slug
* @return string
*/
public static function get_plugin_link($plugin_name, $plugin_slug)
{
return \sprintf('%2$s', 'https://wordpress.org/plugins/' . $plugin_slug, $plugin_name);
}
/**
* Get the link to install, activate or upgrade a plugin.
*
* @param string $plugin_name The name of the plugin.
* @param string $plugin_slug The slug of the plugin.
* @param string $plugin_basename The basename of the plugin (e.g. 'woocommerce/woocommerce.php').
* @param string $action The action to perform on the plugin (install, activate or upgrade).
* @return string
*/
public static function get_plugin_install_activate_upgrade_link($plugin_name, $plugin_slug, $plugin_basename, $action = null)
{
if (\is_wp_error(\validate_plugin($plugin_basename)) || $action === 'install') {
$action = 'install-plugin';
$command = 'Install';
$page = 'update.php';
$file = $plugin_slug;
$nonce_key = "{$action}_{$file}";
} elseif (\is_plugin_inactive($plugin_basename) || $action === 'activate') {
$action = 'activate';
$command = 'Activate';
$page = 'plugins.php';
$file = $plugin_basename;
$nonce_key = "{$action}-plugin_{$file}";
} elseif ($action === 'upgrade') {
$action = 'upgrade-plugin';
$command = 'Upgrade';
$page = 'update.php';
$file = $plugin_basename;
$nonce_key = "{$action}_{$file}";
}
// there is no `else` clause here because there shouldn't be other cases
// if `$action` is still `null`, then the function will return an empty string
if (\is_null($action)) {
return '';
}
$plugin_install_activate_link = \wp_nonce_url(\add_query_arg(
['action' => $action, 'plugin' => $file],
// on multisites, the installation of a plugin must happen on the network admin, hence the use of `self_admin_url()`
$action === 'install' ? \self_admin_url($page) : \admin_url($page)
), $nonce_key);
return \sprintf(' %2$s', $plugin_install_activate_link, "{$command} {$plugin_name}");
}
/**
* Install the bonus plugin.
*
* @param array $bonus_plugins A list of bonus plugins to install.
* Each plugin is an object with the following properties:
* - id: The ID of the EDD download post for the plugin.
* - name: The name of the plugin.
* - url: The URL of the plugin ZIP file.
*
* @return array The results of the installation (either true or a WP_Error).
*/
public static function install_bonus_plugins($bonus_plugins)
{
include_once \ABSPATH . 'wp-admin/includes/file.php';
include_once \ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
include_once \ABSPATH . 'wp-admin/includes/plugin-install.php';
include_once \ABSPATH . 'wp-admin/includes/plugin.php';
$skin = new \WP_Ajax_Upgrader_Skin();
$upgrader = new \Plugin_Upgrader($skin);
$results = [];
foreach ($bonus_plugins as $plugin) {
$name = $plugin->name;
$result = $upgrader->run(['package' => $plugin->url, 'destination' => \WP_PLUGIN_DIR]);
if (\is_wp_error($result)) {
$results[$name] = new WP_Error('bonus_download_install_failed', $result->get_error_message(), $result->get_error_data());
continue;
} else {
if (\is_wp_error($skin->result)) {
$results[$name] = new WP_Error('bonus_download_install_failed', $skin->result->get_error_message(), $skin->result->get_error_data());
continue;
} else {
if ($skin->get_errors()->get_error_code()) {
$results[$name] = new WP_Error('bonus_download_install_failed', $skin->get_error_messages(), $skin->get_errors()->get_error_data());
continue;
} else {
if (\is_null($result)) {
WP_Filesystem();
global $wp_filesystem;
$error_message = __('Unable to connect to the filesystem. Please confirm your credentials.', 'woocommerce-product-table');
if ($wp_filesystem instanceof WP_Filesystem_Base && \is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) {
$error_message = \esc_html($wp_filesystem->errors->get_error_message());
}
$results[$name] = new WP_Error('bonus_download_install_failed', $error_message);
continue;
}
}
}
}
if (isset($result['destination_name'])) {
$plugin = "{$result['destination_name']}/{$result['destination_name']}.php";
} else {
$plugin = '';
}
if ($plugin && \current_user_can('activate_plugin', $plugin)) {
$cache_plugins = \wp_cache_get('plugins', 'plugins');
if (!empty($cache_plugins)) {
$new_plugin = \get_plugin_data(\WP_PLUGIN_DIR . '/' . $plugin, \false, \false);
$cache_plugins[''][$plugin] = $new_plugin;
\wp_cache_set('plugins', $cache_plugins, 'plugins');
}
$result = \activate_plugin($plugin);
if (\is_wp_error($result)) {
$results[$name] = new WP_Error('bonus_download_activation_failed', $result->get_error_message(), $result->get_error_data());
continue;
}
} else {
$results[$name] = new WP_Error('bonus_download_no_activation_permission', esc_html__('You don\'t have permission to activate the plugin.', 'woocommerce-product-table'));
continue;
}
$results[$name] = \true;
}
return $results;
}
}