| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Utils\traits; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
|
| 7 |
trait ThemePlugin { |
| 8 |
/** |
| 9 |
* Installed plugin list. |
| 10 |
*/ |
| 11 |
protected static ?array $installed_plugins = null; |
| 12 |
|
| 13 |
/** |
| 14 |
* @param $basename |
| 15 |
* |
| 16 |
* @return false|array{Name:string,PluginURI:string,Version:string,Description:string,Author:string,AuthorURI:string,TextDomain:string,DomainPath:string,Network:string,RequiresWP:string,RequiresPHP:string,UpdateURI:string,RequiresPlugins:string} |
| 17 |
*/ |
| 18 |
public static function is_plugin_installed( $basename ) { |
| 19 |
if ( null === static::$installed_plugins ) { |
| 20 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 21 |
include_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 22 |
} |
| 23 |
|
| 24 |
static::$installed_plugins = get_plugins(); |
| 25 |
} |
| 26 |
|
| 27 |
return static::$installed_plugins[ $basename ] ?? false; |
| 28 |
} |
| 29 |
|
| 30 |
public static function is_active_storeengine_pro(): bool { |
| 31 |
return self::is_plugin_active( 'storeengine-pro/storeengine-pro.php' ); |
| 32 |
} |
| 33 |
|
| 34 |
public static function is_plugin_active( $basename ): bool { |
| 35 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 36 |
include_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 37 |
} |
| 38 |
|
| 39 |
return is_plugin_active( $basename ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Attempts activation of plugin in a "sandbox" and redirects on success. |
| 44 |
* |
| 45 |
* |
| 46 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 47 |
* @param string $redirect Optional. URL to redirect to. |
| 48 |
* @param bool $network_wide Optional. Whether to enable the plugin for all sites in the network |
| 49 |
* or just the current site. Multisite only. Default false. |
| 50 |
* @param bool $silent Optional. Whether to prevent calling activation hooks. Default false. |
| 51 |
* |
| 52 |
* @return null|WP_Error Null on success, WP_Error on invalid file. |
| 53 |
* @since 1.6.4 |
| 54 |
* |
| 55 |
*/ |
| 56 |
public static function activate_plugin( string $plugin, string $redirect = '', bool $network_wide = false, bool $silent = false ): ?WP_Error { |
| 57 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 58 |
include_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 59 |
} |
| 60 |
|
| 61 |
return activate_plugin( $plugin, $redirect, $network_wide, $silent ); |
| 62 |
} |
| 63 |
|
| 64 |
public static function get_plugin_install_url( string $slug ): string { |
| 65 |
return wp_nonce_url( admin_url( 'update.php?action=install-plugin&plugin=' . $slug ), 'install-plugin_' . $slug ); |
| 66 |
} |
| 67 |
|
| 68 |
public static function get_plugin_activation_url( string $basename ): string { |
| 69 |
return wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=' . $basename ), 'activate-plugin_' . $basename ); |
| 70 |
} |
| 71 |
|
| 72 |
public static function get_plugin_deactivation_url( string $basename ): string { |
| 73 |
return wp_nonce_url( admin_url( 'plugins.php?action=deactivate&plugin=' . $basename ), 'deactivate-plugin_' . $basename ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// End of file theme-plugin.php. |
| 78 |
|