| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Extensions; |
| 4 |
|
| 5 |
/** |
| 6 |
* Plugin manager |
| 7 |
*/ |
| 8 |
class Plugin_Manager { |
| 9 |
|
| 10 |
/** |
| 11 |
* Check if a plugin is installed. |
| 12 |
* |
| 13 |
* @param string $slug The slug of the plugin. |
| 14 |
* |
| 15 |
* @return bool True if installed, false otherwise. |
| 16 |
*/ |
| 17 |
public static function is_installed( string $slug ): bool { |
| 18 |
$plugins = get_plugins(); |
| 19 |
|
| 20 |
if ( is_array( $plugins ) ) { |
| 21 |
foreach ( $plugins as $plugin ) { |
| 22 |
if ( $plugin['TextDomain'] === $slug ) { |
| 23 |
return true; |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Check if a plugin is active. |
| 33 |
* |
| 34 |
* @param string $slug The slug of the plugin. |
| 35 |
* |
| 36 |
* @return bool True if activated, false otherwise. |
| 37 |
*/ |
| 38 |
public static function is_activated( string $slug ): bool { |
| 39 |
if ( ! self::is_installed( $slug ) ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
return is_plugin_active( self::get_plugin_path( $slug ) ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Install a plugin from the WordPress repository by slug. |
| 48 |
* |
| 49 |
* @param string $slug The slug of the plugin. |
| 50 |
* |
| 51 |
* @return bool True if installation succeeds, false otherwise. |
| 52 |
*/ |
| 53 |
public static function install_plugin( string $slug ): bool { |
| 54 |
if ( self::is_installed( $slug ) ) { |
| 55 |
return true; // Already installed |
| 56 |
} |
| 57 |
|
| 58 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 59 |
include_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 60 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 61 |
|
| 62 |
$skin = new \Automatic_Upgrader_Skin(); |
| 63 |
$upgrader = new \Plugin_Upgrader( $skin ); |
| 64 |
$download_link = isset( self::extensions()[ $slug ] ) ? self::extensions()[ $slug ]['download_link'] : null; |
| 65 |
$result = $upgrader->install( $download_link ); |
| 66 |
|
| 67 |
return (bool) $result; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Activate a plugin by slug. |
| 72 |
* |
| 73 |
* @param string $slug The slug of the plugin. |
| 74 |
* |
| 75 |
* @return bool True if activated, false otherwise. |
| 76 |
*/ |
| 77 |
public static function activate_plugin( string $slug ): bool { |
| 78 |
if ( ! self::is_installed( $slug ) ) { |
| 79 |
return false; // Plugin isn't installed |
| 80 |
} |
| 81 |
|
| 82 |
$plugin_path = self::get_plugin_path( $slug ); |
| 83 |
$result = activate_plugin( $plugin_path ); |
| 84 |
|
| 85 |
if ( is_wp_error( $result ) ) { |
| 86 |
return $result; |
| 87 |
} |
| 88 |
|
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Deactivate a plugin by slug. |
| 94 |
* |
| 95 |
* @param string $slug The slug of the plugin. |
| 96 |
* |
| 97 |
* @return bool True if deactivated, false otherwise. |
| 98 |
*/ |
| 99 |
public static function deactivate_plugin( string $slug ) { |
| 100 |
if ( ! self::is_activated( $slug ) ) { |
| 101 |
return false; // Plugin isn't activated |
| 102 |
} |
| 103 |
|
| 104 |
$plugin_path = self::get_plugin_path( $slug ); |
| 105 |
|
| 106 |
deactivate_plugins( $plugin_path ); |
| 107 |
|
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the plugin path by slug. |
| 113 |
* |
| 114 |
* @param string $slug The slug of the plugin. |
| 115 |
* |
| 116 |
* @return string The path to the plugin file. |
| 117 |
*/ |
| 118 |
private static function get_plugin_path( string $slug ) { |
| 119 |
$plugins = get_plugins(); |
| 120 |
|
| 121 |
if ( is_array( $plugins ) ) { |
| 122 |
foreach ( $plugins as $plugin_path => $plugin ) { |
| 123 |
if ( $plugin['TextDomain'] === $slug ) { |
| 124 |
return $plugin_path; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Check if the provided file is the main plugin file. |
| 134 |
* |
| 135 |
* @param string $file Path to the file. |
| 136 |
* |
| 137 |
* @return bool True if it is the main plugin file, false otherwise. |
| 138 |
*/ |
| 139 |
private static function is_main_plugin_file( string $file ): bool { |
| 140 |
|
| 141 |
$plugin_data = get_file_data( $file, array( 'Name' => 'Plugin Name' ) ); |
| 142 |
|
| 143 |
return ! empty( $plugin_data['Name'] ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Get the plugin name by slug. |
| 148 |
* |
| 149 |
* @param string $slug Plugin slug (e.g., 'woocommerce). |
| 150 |
* |
| 151 |
* @return string|null Returns the plugin name if found, or null if not found. |
| 152 |
*/ |
| 153 |
public static function get_plugin_name_by_slug( string $slug ) { |
| 154 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 155 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 156 |
} |
| 157 |
|
| 158 |
$all_plugins = get_plugins(); |
| 159 |
|
| 160 |
foreach ( $all_plugins as $plugin_file => $plugin_data ) { |
| 161 |
if ( strpos( $plugin_file, $slug ) !== false ) { |
| 162 |
return $plugin_data['Name']; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return null; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* List of all extensions |
| 171 |
* |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
public static function extensions(): array { |
| 175 |
return array( |
| 176 |
'booktics-stripe-addon' => array( |
| 177 |
'name' => 'booktics-stripe-addon', |
| 178 |
'slug' => 'booktics-stripe-addon', |
| 179 |
'type' => 'addon', |
| 180 |
'upgrade' => true, |
| 181 |
'upgrade_link' => 'https://themewinter.com/purchase-history', |
| 182 |
'status' => 'off', |
| 183 |
'is_pro' => false, |
| 184 |
'title' => __( 'Booktics Stripe Addon', 'booktics' ), |
| 185 |
'description' => __( 'It allows you to use stripe payment.', 'booktics' ), |
| 186 |
'icon' => Extension_Icon::get( 'booktics-stripe-addon' ), |
| 187 |
'notice' => '', |
| 188 |
'demo_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-stripe-addon', |
| 189 |
'settings_link' => '', |
| 190 |
'doc_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-stripe-addon', |
| 191 |
'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-stripe-addon-1.0.0.zip', |
| 192 |
), |
| 193 |
'booktics-google-calendar' => array( |
| 194 |
'name' => 'booktics-google-calendar', |
| 195 |
'slug' => 'booktics-google-calendar', |
| 196 |
'type' => 'addon', |
| 197 |
'upgrade' => true, |
| 198 |
'upgrade_link' => 'https://themewinter.com/purchase-history', |
| 199 |
'status' => 'off', |
| 200 |
'is_pro' => false, |
| 201 |
'title' => __( 'Booktics Google Calendar', 'booktics' ), |
| 202 |
'description' => __( 'It allows you to use google calendar.', 'booktics' ), |
| 203 |
'icon' => Extension_Icon::get( 'booktics-google-calendar' ), |
| 204 |
'notice' => '', |
| 205 |
'demo_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-google-calendar', |
| 206 |
'settings_link' => '', |
| 207 |
'doc_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-google-calendar', |
| 208 |
'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-google-calendar-1.0.0.zip', |
| 209 |
), |
| 210 |
'booktics-outlook-calendar' => array( |
| 211 |
'name' => 'booktics-outlook-calendar', |
| 212 |
'slug' => 'booktics-outlook-calendar', |
| 213 |
'type' => 'addon', |
| 214 |
'upgrade' => true, |
| 215 |
'upgrade_link' => 'https://themewinter.com/purchase-history', |
| 216 |
'status' => 'off', |
| 217 |
'is_pro' => false, |
| 218 |
'title' => __( 'Booktics Outlook Calendar', 'booktics' ), |
| 219 |
'description' => __( 'It allows you to use outlook calendar.', 'booktics' ), |
| 220 |
'icon' => Extension_Icon::get( 'booktics-outlook-calendar' ), |
| 221 |
'notice' => '', |
| 222 |
'demo_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-outlook-calendar-addon', |
| 223 |
'settings_link' => '', |
| 224 |
'doc_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-outlook-calendar-addon', |
| 225 |
'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-outlook-calendar-addon-1.0.0.zip', |
| 226 |
), |
| 227 |
'booktics-fluentcrm-addon' => array( |
| 228 |
'name' => 'booktics-fluentcrm-addon', |
| 229 |
'slug' => 'booktics-fluentcrm-addon', |
| 230 |
'type' => 'addon', |
| 231 |
'upgrade' => true, |
| 232 |
'upgrade_link' => 'https://themewinter.com/purchase-history', |
| 233 |
'status' => 'off', |
| 234 |
'is_pro' => false, |
| 235 |
'title' => __( 'Booktics FluentCRM Addon', 'booktics' ), |
| 236 |
'description' => __( 'It allows you to use FluentCRM.', 'booktics' ), |
| 237 |
'icon' => Extension_Icon::get( 'booktics-fluentcrm-addon' ), |
| 238 |
'notice' => '', |
| 239 |
'demo_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-fluentcrm-addon', |
| 240 |
'settings_link' => '', |
| 241 |
'doc_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-fluentcrm-addon', |
| 242 |
'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-fluentcrm-addon-1.0.0.zip', |
| 243 |
), |
| 244 |
'booktics-paypal-addon' => array( |
| 245 |
'name' => 'booktics-paypal-addon', |
| 246 |
'slug' => 'booktics-paypal-addon', |
| 247 |
'type' => 'addon', |
| 248 |
'upgrade' => true, |
| 249 |
'upgrade_link' => 'https://themewinter.com/purchase-history', |
| 250 |
'status' => 'off', |
| 251 |
'is_pro' => false, |
| 252 |
'title' => __( 'Booktics PayPal Addon', 'booktics' ), |
| 253 |
'description' => __( 'It allows you to use PayPal.', 'booktics' ), |
| 254 |
'icon' => Extension_Icon::get( 'booktics-paypal-addon' ), |
| 255 |
'notice' => '', |
| 256 |
'demo_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-paypal-addon', |
| 257 |
'settings_link' => '', |
| 258 |
'doc_link' => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-paypal-addon', |
| 259 |
'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-paypal-addon-1.0.0.zip', |
| 260 |
), |
| 261 |
); |
| 262 |
} |
| 263 |
} |
| 264 |
|