| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Utils\Traits; |
| 4 |
|
| 5 |
use Plugin_Upgrader; |
| 6 |
use Theme_Upgrader; |
| 7 |
use WP_Ajax_Upgrader_Skin; |
| 8 |
use WP_Error; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
trait Repository { |
| 15 |
|
| 16 |
/** |
| 17 |
* Install/Activate Free Themes. |
| 18 |
* |
| 19 |
* @param string $theme_slug Slug that match on wp.org repo. |
| 20 |
* |
| 21 |
* @return true|WP_Error |
| 22 |
*/ |
| 23 |
public static function install_and_active_theme( string $theme_slug ) { |
| 24 |
// Include required WordPress files for theme management. |
| 25 |
require_once ABSPATH . 'wp-admin/includes/theme.php'; |
| 26 |
self::require_dependencies(); |
| 27 |
|
| 28 |
// Check if the theme is already installed. |
| 29 |
$installed_themes = wp_get_themes(); |
| 30 |
if ( isset( $installed_themes[ $theme_slug ] ) ) { |
| 31 |
switch_theme( $theme_slug ); |
| 32 |
return true; |
| 33 |
} |
| 34 |
|
| 35 |
// Fetch theme information from wp.org repo. |
| 36 |
$theme_info = themes_api('theme_information', array( |
| 37 |
'slug' => $theme_slug, |
| 38 |
'fields' => array( |
| 39 |
'sections' => false, |
| 40 |
'tested' => false, |
| 41 |
), |
| 42 |
)); |
| 43 |
|
| 44 |
if ( is_wp_error( $theme_info ) ) { |
| 45 |
return $theme_info; |
| 46 |
} |
| 47 |
|
| 48 |
// Set up the installer. |
| 49 |
$upgrader = new Theme_Upgrader( new WP_Ajax_Upgrader_Skin() ); |
| 50 |
$result = $upgrader->install( $theme_info->download_link ); // @phpstan-ignore-line |
| 51 |
|
| 52 |
if ( is_wp_error( $result ) ) { |
| 53 |
return $result; |
| 54 |
} |
| 55 |
|
| 56 |
// Activate the theme |
| 57 |
if ( wp_get_theme( $theme_slug )->exists() ) { |
| 58 |
switch_theme( $theme_slug ); |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
return new WP_Error( 400, 'Theme installed but could not be activated' ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Install/Activate Free Plugins. |
| 67 |
* |
| 68 |
* @param string $plugin_slug Slug that match on wp.org repo. |
| 69 |
* |
| 70 |
* @return true|WP_Error |
| 71 |
*/ |
| 72 |
public static function install_and_active_plugin( string $plugin_slug ) { |
| 73 |
// Include required WordPress files for plugin management. |
| 74 |
self::require_dependencies(); |
| 75 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 76 |
|
| 77 |
// Check if the plugin is already installed. |
| 78 |
$installed_plugins = get_plugins(); |
| 79 |
foreach ( $installed_plugins as $plugin_file => $plugin_data ) { |
| 80 |
if ( strpos( $plugin_file, $plugin_slug . '/' ) === 0 ) { |
| 81 |
$activation = activate_plugin( $plugin_file, '', false, true ); |
| 82 |
if ( is_wp_error( $activation ) ) { |
| 83 |
return $activation; |
| 84 |
} |
| 85 |
return true; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// Get plugin information from WordPress.org |
| 90 |
$plugin_info = plugins_api('plugin_information', array( |
| 91 |
'slug' => $plugin_slug, |
| 92 |
'fields' => array( |
| 93 |
'sections' => false, |
| 94 |
'tested' => false, |
| 95 |
), |
| 96 |
)); |
| 97 |
|
| 98 |
if ( is_wp_error( $plugin_info ) ) { |
| 99 |
return $plugin_info; |
| 100 |
} |
| 101 |
|
| 102 |
// Set up the installer |
| 103 |
$upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() ); |
| 104 |
$result = $upgrader->install( $plugin_info->download_link ); // @phpstan-ignore-line |
| 105 |
|
| 106 |
if ( is_wp_error( $result ) ) { |
| 107 |
return $result; |
| 108 |
} |
| 109 |
|
| 110 |
// Activate the plugin. |
| 111 |
$plugin_name = self::get_plugin_name( $plugin_slug ); |
| 112 |
if ( is_wp_error( $plugin_name ) ) { |
| 113 |
return $plugin_name; |
| 114 |
} |
| 115 |
|
| 116 |
if ( ! is_plugin_active( $plugin_name ) ) { |
| 117 |
$activation = activate_plugin( $plugin_name, '', false, true ); |
| 118 |
|
| 119 |
if ( is_wp_error( $activation ) ) { |
| 120 |
return $activation; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get plugin name from directory name. |
| 129 |
* |
| 130 |
* @param string $directory Directory/slug. |
| 131 |
* |
| 132 |
* @return WP_Error|string |
| 133 |
*/ |
| 134 |
public static function get_plugin_name( string $directory ) { |
| 135 |
$plugin_data = get_plugins( '/' . $directory ); |
| 136 |
$plugin_root_file = count( $plugin_data ) > 0 ? array_key_first( $plugin_data ) : false; |
| 137 |
if ( ! $plugin_root_file ) { |
| 138 |
return new WP_Error( 400, 'Invalid plugin folder!' ); |
| 139 |
} |
| 140 |
|
| 141 |
return $directory . '/' . $plugin_root_file; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Commonly used dependencies here. |
| 146 |
* |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
private static function require_dependencies() { |
| 150 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 151 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 152 |
} |
| 153 |
} |
| 154 |
|