| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Compatibility; |
| 4 |
|
| 5 |
use ImportWP\Common\Filesystem\Filesystem; |
| 6 |
use ImportWP\Common\Properties\Properties; |
| 7 |
|
| 8 |
class CompatibilityManager |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var Properties |
| 12 |
*/ |
| 13 |
public $properties; |
| 14 |
/** |
| 15 |
* |
| 16 |
* @var Filesystem |
| 17 |
*/ |
| 18 |
public $filesystem; |
| 19 |
|
| 20 |
public function __construct($properties, $filesystem) |
| 21 |
{ |
| 22 |
$this->properties = $properties; |
| 23 |
$this->filesystem = $filesystem; |
| 24 |
|
| 25 |
// Checks the compatibility mode MU plugin version and updates if it's out of date. |
| 26 |
add_action('admin_init', array($this, 'muplugin_version_check'), 1); |
| 27 |
|
| 28 |
add_action('iwp/compat/register_muplugin_uninstall', [$this, 'remove_muplugin_on_deactivation']); |
| 29 |
} |
| 30 |
|
| 31 |
public function muplugin_version_check() |
| 32 |
{ |
| 33 |
if (isset($_GET['page']) && $_GET['page'] === 'importwp') { |
| 34 |
if (true === $this->is_muplugin_update_required()) { |
| 35 |
return $this->copy_muplugin(); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
public function is_muplugin_update_required() |
| 43 |
{ |
| 44 |
if (version_compare(get_option('iwp_muplugin_version', 0), $this->properties->mu_plugin_version, '<')) { |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
public function copy_muplugin() |
| 52 |
{ |
| 53 |
if (!wp_mkdir_p(dirname($this->properties->mu_plugin_dest)) || !$this->filesystem->copy($this->properties->mu_plugin_source, $this->properties->mu_plugin_dest)) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
update_option('iwp_muplugin_version', $this->properties->mu_plugin_version); |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
public function remove_muplugin_on_deactivation() |
| 63 |
{ |
| 64 |
|
| 65 |
if ($this->filesystem->file_exists($this->properties->mu_plugin_dest)) { |
| 66 |
@unlink($this->properties->mu_plugin_dest); |
| 67 |
delete_option('iwp_muplugin_version'); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|