Cleanup
3 months ago
Helpers
3 months ago
WP
3 months ago
Content_Importer.php
3 months ago
Plugin_Importer.php
1 year ago
Theme_Mods_Importer.php
3 months ago
Widgets_Importer.php
2 months ago
Zelle_Importer.php
2 years ago
Plugin_Importer.php
439 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Importer. |
| 4 | * |
| 5 | * @package templates-patterns-collection |
| 6 | */ |
| 7 | |
| 8 | namespace TIOB\Importers; |
| 9 | |
| 10 | use Plugin_Upgrader; |
| 11 | use TIOB\Importers\Cleanup\Active_State; |
| 12 | use TIOB\Importers\Helpers\Quiet_Skin; |
| 13 | use TIOB\Importers\Helpers\Quiet_Skin_Legacy; |
| 14 | use TIOB\Logger; |
| 15 | use WP_REST_Request; |
| 16 | use WP_REST_Response; |
| 17 | |
| 18 | /** |
| 19 | * Class Plugin_Importer |
| 20 | */ |
| 21 | class Plugin_Importer { |
| 22 | |
| 23 | const OPTIMOLE_FRESH_INSTALL_FLAG = 'optml_fresh_install'; |
| 24 | const OPTIMOLE_SLUG = 'optimole-wp'; |
| 25 | |
| 26 | /** |
| 27 | * Log |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | public $log = ''; |
| 32 | |
| 33 | /** |
| 34 | * Logger Instance. |
| 35 | * |
| 36 | * @var Logger |
| 37 | */ |
| 38 | private $logger; |
| 39 | |
| 40 | /** |
| 41 | * Exceptions entry files mapping. |
| 42 | * |
| 43 | * slug => entry-file |
| 44 | * |
| 45 | * @var array |
| 46 | */ |
| 47 | private $exception_mapping = array( |
| 48 | 'advanced-css-editor' => 'css-editor.php', |
| 49 | 'contact-form-7' => 'wp-contact-form-7.php', |
| 50 | 'wpforms-lite' => 'wpforms.php', |
| 51 | 'beaver-builder-lite-version' => 'fl-builder.php', |
| 52 | 'wpzoom-addons-for-beaver-builder' => 'wpzoom-bb-addon-pack.php', |
| 53 | 'recipe-card-blocks-by-wpzoom' => 'wpzoom-recipe-card.php', |
| 54 | 'restrict-content' => 'restrictcontent.php', |
| 55 | 'pods' => 'init.php', |
| 56 | 'wp-full-stripe-free' => 'wp-full-stripe.php', |
| 57 | 'wp-cloudflare-page-cache' => 'wp-cloudflare-super-page-cache.php', |
| 58 | 'learning-management-system' => 'lms.php', |
| 59 | ); |
| 60 | |
| 61 | public function __construct() { |
| 62 | $this->logger = Logger::get_instance(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Install Plugins. |
| 67 | * |
| 68 | * @param WP_REST_Request $request contains the plugins that should be installed. |
| 69 | * |
| 70 | * @return WP_REST_Response |
| 71 | */ |
| 72 | public function install_plugins( WP_REST_Request $request ) { |
| 73 | require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
| 74 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 75 | require_once( ABSPATH . 'wp-admin/includes/misc.php' ); |
| 76 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 77 | require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
| 78 | |
| 79 | $plugins = $request->get_json_params(); |
| 80 | |
| 81 | foreach ( $plugins as $slug => $state ) { |
| 82 | if ( ! $state || empty( $state ) ) { |
| 83 | unset( $plugins[ $slug ] ); |
| 84 | } |
| 85 | |
| 86 | if ( $this->plugin_dir_exists( $slug ) && $this->plugin_is_active( $slug ) ) { |
| 87 | unset( $plugins[ $slug ] ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if ( empty( $plugins ) || ! is_array( $plugins ) ) { |
| 92 | return new WP_REST_Response( |
| 93 | array( |
| 94 | 'success' => true, |
| 95 | 'log' => $this->log, |
| 96 | ) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | do_action( 'themeisle_ob_before_plugins_install' ); |
| 101 | |
| 102 | $install = $this->run_plugins_install( $plugins ); |
| 103 | |
| 104 | if ( $install instanceof WP_REST_Response ) { |
| 105 | return $install; |
| 106 | } |
| 107 | |
| 108 | return new WP_REST_Response( |
| 109 | array( |
| 110 | 'success' => true, |
| 111 | 'log' => $this->log, |
| 112 | ) |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Install and activate plugins. |
| 118 | * |
| 119 | * @param array $plugins_array plugins formated slug => true. |
| 120 | * |
| 121 | * |
| 122 | * @return WP_REST_Response |
| 123 | */ |
| 124 | public function run_plugins_install( $plugins_array ) { |
| 125 | $plugin_cleanup = array(); |
| 126 | foreach ( $plugins_array as $plugin_slug => $path ) { |
| 127 | $this->logger->log( "Installing {$plugin_slug}.", 'progress' ); |
| 128 | $download_path = ''; |
| 129 | if ( $path !== true && is_string( $path ) && ! empty( $path ) ) { |
| 130 | $download_path = $path; |
| 131 | } |
| 132 | |
| 133 | $install = $this->install_single_plugin( $plugin_slug, $download_path ); |
| 134 | if ( ! $install ) { |
| 135 | $this->logger->log( 'Current user cannot install plugins.' ); |
| 136 | |
| 137 | return new WP_REST_Response( |
| 138 | array( |
| 139 | 'success' => false, |
| 140 | 'log' => $this->log, |
| 141 | 'data' => 'no_plugin_install_permission', |
| 142 | ) |
| 143 | ); |
| 144 | } |
| 145 | $plugin_cleanup[ $plugin_slug ]['installed'] = true; |
| 146 | $this->logger->log( "Activating {$plugin_slug}.", 'progress' ); |
| 147 | $activate = $this->activate_single_plugin( $plugin_slug ); |
| 148 | if ( ! $activate ) { |
| 149 | $this->logger->log( 'Current user cannot activate plugins.' ); |
| 150 | |
| 151 | return new WP_REST_Response( |
| 152 | array( |
| 153 | 'success' => false, |
| 154 | 'log' => $this->log, |
| 155 | 'data' => 'no_plugin_activation_permission', |
| 156 | ) |
| 157 | ); |
| 158 | } |
| 159 | $plugin_cleanup[ $plugin_slug ]['active'] = true; |
| 160 | } |
| 161 | do_action( 'themeisle_cl_add_property_state', Active_State::PLUGINS_NSP, $plugin_cleanup ); |
| 162 | |
| 163 | $this->remove_possible_redirects(); |
| 164 | $this->logger->log( 'Installed and activated plugins.', 'success' ); |
| 165 | |
| 166 | do_action( 'themeisle_ob_after_plugins_install' ); |
| 167 | |
| 168 | update_option( 'themeisle_ob_plugins_installed', 'yes' ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Remove admin redirects. |
| 173 | */ |
| 174 | private function remove_possible_redirects() { |
| 175 | delete_transient( '_wc_activation_redirect' ); |
| 176 | delete_transient( 'wpforms_activation_redirect' ); |
| 177 | update_option( 'themeisle_blocks_settings_redirect', false ); |
| 178 | update_option( 'masteriyo_first_time_activation_flag', true ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Install a single plugin |
| 183 | * |
| 184 | * @param string $plugin_slug plugin slug. |
| 185 | * |
| 186 | * @return bool |
| 187 | */ |
| 188 | private function install_single_plugin( $plugin_slug, $download_path = '' ) { |
| 189 | // Plugin is already there. |
| 190 | if ( $this->plugin_dir_exists( $plugin_slug ) ) { |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | // User doesn't have permissions. |
| 195 | if ( ! current_user_can( 'install_plugins' ) && ! class_exists( 'WP_CLI' ) ) { |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | do_action( 'themeisle_ob_before_single_plugin_install', $plugin_slug ); |
| 200 | |
| 201 | $api = plugins_api( |
| 202 | 'plugin_information', |
| 203 | array( |
| 204 | 'slug' => $plugin_slug, |
| 205 | 'fields' => array( |
| 206 | 'short_description' => false, |
| 207 | 'sections' => false, |
| 208 | 'requires' => false, |
| 209 | 'rating' => false, |
| 210 | 'ratings' => false, |
| 211 | 'downloaded' => false, |
| 212 | 'last_updated' => false, |
| 213 | 'added' => false, |
| 214 | 'tags' => false, |
| 215 | 'compatibility' => false, |
| 216 | 'homepage' => false, |
| 217 | 'donate_link' => false, |
| 218 | ), |
| 219 | ) |
| 220 | ); |
| 221 | |
| 222 | if ( version_compare( PHP_VERSION, '5.6' ) === - 1 ) { |
| 223 | $skin = new Quiet_Skin_Legacy( |
| 224 | array( |
| 225 | 'api' => $api, |
| 226 | ) |
| 227 | ); |
| 228 | } else { |
| 229 | $skin = new Quiet_Skin( |
| 230 | array( |
| 231 | 'api' => $api, |
| 232 | ) |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | $download_link = $download_path; |
| 237 | if ( empty( $download_path ) ) { |
| 238 | $download_link = $api->download_link; |
| 239 | } |
| 240 | |
| 241 | $upgrader = new Plugin_Upgrader( $skin ); |
| 242 | $install = $upgrader->install( $download_link ); |
| 243 | if ( $install !== true ) { |
| 244 | $this->log .= 'Error: Install process failed (' . ucwords( $plugin_slug ) . ').' . "\n"; |
| 245 | |
| 246 | return false; |
| 247 | } |
| 248 | $this->log .= 'Installed "' . ucwords( $plugin_slug ) . '"' . "\n "; |
| 249 | |
| 250 | do_action( 'themeisle_ob_after_single_plugin_install', $plugin_slug ); |
| 251 | |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Get full plugin path. |
| 257 | * |
| 258 | * @param string $slug The plugin slug. |
| 259 | * |
| 260 | * @return string |
| 261 | */ |
| 262 | private function get_plugin_path( $slug ) { |
| 263 | $plugin_dir = WP_PLUGIN_DIR . '/' . $slug; |
| 264 | |
| 265 | if ( array_key_exists( $slug, $this->exception_mapping ) ) { |
| 266 | return trailingslashit( $plugin_dir ) . $this->exception_mapping[ $slug ]; |
| 267 | } |
| 268 | |
| 269 | $plugin_path = $plugin_dir . '/' . $slug . '.php'; |
| 270 | |
| 271 | if ( ! file_exists( $plugin_path ) ) { |
| 272 | $plugin_path = $plugin_dir . '/' . 'index.php'; |
| 273 | } |
| 274 | |
| 275 | return $plugin_path; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Get plugin entry file. |
| 280 | * |
| 281 | * @param string $slug The plugin slug. |
| 282 | * |
| 283 | * @return string |
| 284 | */ |
| 285 | private function get_plugin_entry( $slug ) { |
| 286 | if ( $slug === 'advanced-css-editor' ) { |
| 287 | return $slug . '/css-editor.php'; |
| 288 | } |
| 289 | |
| 290 | if ( $slug === 'contact-form-7' ) { |
| 291 | return $slug . '/wp-contact-form-7.php'; |
| 292 | } |
| 293 | |
| 294 | if ( $slug === 'wpforms-lite' ) { |
| 295 | return $slug . '/wpforms.php'; |
| 296 | } |
| 297 | |
| 298 | $plugins_dir = WP_PLUGIN_DIR . '/'; |
| 299 | $entry = $slug . '/' . $slug . '.php'; |
| 300 | if ( ! file_exists( $plugins_dir . $entry ) ) { |
| 301 | $entry = $slug . '/index.php'; |
| 302 | } |
| 303 | |
| 304 | return $entry; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Activate a single plugin |
| 309 | * |
| 310 | * @param string $plugin_slug plugin slug. |
| 311 | * |
| 312 | * @return bool |
| 313 | */ |
| 314 | private function activate_single_plugin( $plugin_slug ) { |
| 315 | $plugin_dir = WP_PLUGIN_DIR . '/' . $plugin_slug; |
| 316 | |
| 317 | $plugin_path = $this->get_plugin_path( $plugin_slug ); |
| 318 | |
| 319 | // Plugin isn't there. |
| 320 | if ( ! file_exists( $plugin_path ) ) { |
| 321 | $this->log .= 'No plugin with the slug "' . $plugin_slug . '" under that directory.' . "\n"; |
| 322 | |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | do_action( 'themeisle_ob_before_single_plugin_activation', $plugin_slug ); |
| 327 | |
| 328 | // Plugin is already active. |
| 329 | if ( $this->plugin_is_active( $plugin_slug ) ) { |
| 330 | $this->log .= '"' . ucwords( $plugin_slug ) . '" already active.' . "\n"; |
| 331 | |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | // User doesn't have permissions. |
| 336 | if ( ! current_user_can( 'activate_plugins' ) && ! class_exists( 'WP_CLI' ) ) { |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | $this->maybe_provide_activation_help( $plugin_slug, $plugin_dir ); |
| 341 | |
| 342 | activate_plugin( $plugin_path ); |
| 343 | $this->log .= 'Activated ' . ucwords( $plugin_slug ) . '.' . "\n"; |
| 344 | |
| 345 | if ( $plugin_slug === self::OPTIMOLE_SLUG ) { |
| 346 | delete_transient( self::OPTIMOLE_FRESH_INSTALL_FLAG ); |
| 347 | } |
| 348 | |
| 349 | do_action( 'themeisle_ob_after_single_plugin_activation', $plugin_slug ); |
| 350 | |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Take care of plugins that need special conditions on activation. |
| 356 | * |
| 357 | * @param string $slug plugin slug. |
| 358 | * @param string $path plugin path. |
| 359 | */ |
| 360 | private function maybe_provide_activation_help( $slug, $path ) { |
| 361 | if ( $slug === 'woocommerce' ) { |
| 362 | require_once( $path . '/includes/admin/wc-admin-functions.php' ); |
| 363 | |
| 364 | // hook into this filter to remove pages on activation of woocommerce |
| 365 | add_filter( 'woocommerce_create_pages', array( $this, 'woocommerce_activation_pages' ) ); |
| 366 | } |
| 367 | |
| 368 | if ( $slug === 'learning-management-system' ) { |
| 369 | // hook into this filter to remove pages on activation of masteriyo |
| 370 | add_filter( 'masteriyo_create_pages', array( $this, 'masteriyo_activation_pages' ) ); |
| 371 | } |
| 372 | |
| 373 | if ( $slug === 'estatik' ) { |
| 374 | update_option( 'es_is_demo_executed', 1 ); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Filter pages from woocommerce activation. |
| 380 | * |
| 381 | * @param array $pages List of pages to be created on activation. |
| 382 | * |
| 383 | * @return array |
| 384 | */ |
| 385 | public function woocommerce_activation_pages( $pages ) { |
| 386 | $filtered_pages = array( 'shop', 'cart', 'checkout', 'myaccount' ); |
| 387 | foreach ( $filtered_pages as $filter ) { |
| 388 | if ( isset( $pages[ $filter ] ) ) { |
| 389 | unset( $pages[ $filter ] ); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | return $pages; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Filter pages from masteriyo activation. |
| 398 | * |
| 399 | * @param array $pages List of pages to be created on activation. |
| 400 | * |
| 401 | * @return array |
| 402 | */ |
| 403 | public function masteriyo_activation_pages( $pages ) { |
| 404 | $filtered_pages = array( 'courses', 'checkout', 'account', 'learn', 'instructor-registration', 'instructors-list' ); |
| 405 | foreach ( $filtered_pages as $filter ) { |
| 406 | if ( isset( $pages[ $filter ] ) ) { |
| 407 | unset( $pages[ $filter ] ); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | return $pages; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Check if plugin directory exists. |
| 416 | * |
| 417 | * @param string $slug plugin slug. |
| 418 | * |
| 419 | * @retun bool |
| 420 | */ |
| 421 | private function plugin_dir_exists( $slug ) { |
| 422 | return is_dir( WP_PLUGIN_DIR . '/' . $slug ); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Check if plugin is already active. |
| 427 | * |
| 428 | * @param string $slug plugin slug. |
| 429 | * |
| 430 | * @retun bool |
| 431 | */ |
| 432 | private function plugin_is_active( $slug ) { |
| 433 | $plugin_entry = $this->get_plugin_entry( $slug ); |
| 434 | include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 435 | |
| 436 | return is_plugin_active( $plugin_entry ); |
| 437 | } |
| 438 | } |
| 439 |