| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\ImportExport\Runners\Import; |
| 4 |
|
| 5 |
use Elementor\Core\Utils\Collection; |
| 6 |
use Elementor\Core\Utils\Plugins_Manager; |
| 7 |
use Elementor\Core\Utils\Str; |
| 8 |
|
| 9 |
class Plugins extends Import_Runner_Base { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var Plugins_Manager |
| 13 |
*/ |
| 14 |
private $plugins_manager; |
| 15 |
|
| 16 |
public function __construct( $plugins_manager = null ) { |
| 17 |
if ( $plugins_manager ) { |
| 18 |
$this->plugins_manager = $plugins_manager; |
| 19 |
} else { |
| 20 |
$this->plugins_manager = new Plugins_Manager(); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
public static function get_name() : string { |
| 25 |
return 'plugins'; |
| 26 |
} |
| 27 |
|
| 28 |
public function should_import( array $data ) { |
| 29 |
return ( |
| 30 |
isset( $data['include'] ) && |
| 31 |
in_array( 'plugins', $data['include'], true ) && |
| 32 |
! empty( $data['manifest']['plugins'] ) && |
| 33 |
! empty( $data['selected_plugins'] ) |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
public function import( array $data, array $imported_data ) { |
| 38 |
$plugins = $data['selected_plugins']; |
| 39 |
|
| 40 |
$plugins_collection = ( new Collection( $plugins ) ) |
| 41 |
->map( function ( $item ) { |
| 42 |
if ( ! Str::ends_with( $item['plugin'], '.php' ) ) { |
| 43 |
$item['plugin'] .= '.php'; |
| 44 |
} |
| 45 |
return $item; |
| 46 |
} ); |
| 47 |
|
| 48 |
$slugs = $plugins_collection |
| 49 |
->map( function ( $item ) { |
| 50 |
return $item['plugin']; |
| 51 |
} ) |
| 52 |
->all(); |
| 53 |
|
| 54 |
$installed = $this->plugins_manager->install( $slugs ); |
| 55 |
$activated = $this->plugins_manager->activate( $installed['succeeded'] ); |
| 56 |
|
| 57 |
$ordered_activated_plugins = $plugins_collection |
| 58 |
->filter( function ( $item ) use ( $activated ) { |
| 59 |
return in_array( $item['plugin'], $activated['succeeded'], true ); |
| 60 |
} ) |
| 61 |
->map( function ( $item ) { |
| 62 |
return $item['name']; |
| 63 |
} ) |
| 64 |
->all(); |
| 65 |
|
| 66 |
$result['plugins'] = $ordered_activated_plugins; |
| 67 |
|
| 68 |
return $result; |
| 69 |
} |
| 70 |
} |
| 71 |
|