| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace MultiSafepay\WooCommerce\Utils; |
| 4 |
|
| 5 |
use MultiSafepay\WooCommerce\Exceptions\MissingDependencyException; |
| 6 |
|
| 7 |
/** |
| 8 |
* This class defines all code necessary to run during the plugin's activation. |
| 9 |
* |
| 10 |
* @see https://developer.wordpress.org/reference/functions/register_activation_hook/ |
| 11 |
*/ |
| 12 |
class Activator { |
| 13 |
|
| 14 |
/** |
| 15 |
* Fired during plugin activation according if is multisite or not. |
| 16 |
* |
| 17 |
* @param null|bool $network_wide |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public function activate( ?bool $network_wide ): void { |
| 21 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 22 |
die( esc_html__( 'It seems you don\'t have permission to activate plugins', 'multisafepay' ) ); |
| 23 |
} |
| 24 |
if ( ( ! is_multisite() ) || ( is_multisite() && ! $network_wide ) ) { |
| 25 |
$this->activate_plugin_single_site(); |
| 26 |
} |
| 27 |
if ( $network_wide ) { |
| 28 |
$this->activate_plugin_all_sites(); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Check if dependencies are not active and return fatal error |
| 34 |
* for a single site. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
private function activate_plugin_single_site(): void { |
| 39 |
try { |
| 40 |
$dependency_checker = new DependencyChecker(); |
| 41 |
$dependency_checker->check(); |
| 42 |
} catch ( MissingDependencyException $missing_dependency_exception ) { |
| 43 |
$dependencies = implode( ', ', $missing_dependency_exception->get_missing_plugin_names() ); |
| 44 |
$message = sprintf( __( 'Missing dependencies: %s. Please install these extensions to use the MultiSafepay WooCommerce plugin', 'multisafepay' ), $dependencies ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
| 45 |
die( esc_html( $message ) ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Check if dependencies are not active and return fatal error |
| 51 |
* for a network. |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
private function activate_plugin_all_sites(): void { |
| 56 |
$blog_ids = $this->get_blogs_ids(); |
| 57 |
foreach ( $blog_ids as $blog_id ) { |
| 58 |
switch_to_blog( $blog_id ); |
| 59 |
$this->activate_plugin_single_site(); |
| 60 |
restore_current_blog(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Return all sites ids |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
private function get_blogs_ids(): array { |
| 70 |
$args = array( |
| 71 |
'fields' => 'ids', |
| 72 |
); |
| 73 |
$blogs_ids = get_sites( $args ); |
| 74 |
return $blogs_ids; |
| 75 |
} |
| 76 |
} |
| 77 |
|