| 1 |
<?php namespace EmailLog\Addon; |
| 2 |
|
| 3 |
use EmailLog\Core\Loadie; |
| 4 |
|
| 5 |
/** |
| 6 |
* Enforce Addon Dependency by deactivating add-ons that don't satisfy dependency. |
| 7 |
* |
| 8 |
* @since 2.0 |
| 9 |
*/ |
| 10 |
class DependencyEnforcer implements Loadie { |
| 11 |
|
| 12 |
/** |
| 13 |
* Addon Dependency Map. |
| 14 |
* TODO: This map should be dynamically pulled from website based on base plugin version. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
private $addon_dependency_map = array( |
| 19 |
'email-log-forward-email/email-log-forward-email.php' => '2.0', |
| 20 |
'email-log-more-fields/email-log-more-fields.php' => '2.0', |
| 21 |
'email-log-resend-email/email-log-resend-email.php' => '2.0', |
| 22 |
); |
| 23 |
|
| 24 |
/** |
| 25 |
* Setup action and hooks. |
| 26 |
*/ |
| 27 |
public function load() { |
| 28 |
// TODO: Ideally, this should not be called on all admin pages. |
| 29 |
add_action( 'admin_notices', array( $this, 'render_compatibility_notice' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Render compatibility notice, if needed. |
| 34 |
* TODO: Include link to the add-on store in the admin notice. |
| 35 |
*/ |
| 36 |
public function render_compatibility_notice() { |
| 37 |
$deactivated_addons = $this->deactivate_outdated_active_addons(); |
| 38 |
|
| 39 |
if ( empty( $deactivated_addons ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
?> |
| 44 |
<div class="error"> |
| 45 |
<p> |
| 46 |
<?php _e( 'The following add-ons are not compatible with the installed version of Email Log and have been deactivated.', 'email-log' ); ?> |
| 47 |
<ul> |
| 48 |
<?php |
| 49 |
array_walk( $deactivated_addons, function( $addon ) { |
| 50 |
echo '<li>' . esc_html( $addon ) . '</li>'; |
| 51 |
} ); |
| 52 |
?> |
| 53 |
</ul> |
| 54 |
<?php _e( 'Please get the latest version of these add-ons from add-on store.', 'email-log' ); ?> |
| 55 |
</p> |
| 56 |
</div> |
| 57 |
<?php |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Deactivate outdated active add-ons. |
| 62 |
* |
| 63 |
* @access private |
| 64 |
* @return array List of add-ons (name and version) that got deactivated. |
| 65 |
*/ |
| 66 |
private function deactivate_outdated_active_addons() { |
| 67 |
$deactivated_active_addons = array(); |
| 68 |
|
| 69 |
$outdated_active_addons = $this->get_outdated_active_addons(); |
| 70 |
foreach ( $outdated_active_addons as $addon_file_name => $outdated_active_addon ) { |
| 71 |
deactivate_plugins( plugin_basename( $addon_file_name ) ); |
| 72 |
$deactivated_active_addons[] = $outdated_active_addon['Name'] . ' ' . $outdated_active_addon['Version']; |
| 73 |
} |
| 74 |
|
| 75 |
return $deactivated_active_addons; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get the list of add-ons that are outdated and are active. |
| 80 |
* |
| 81 |
* @access private |
| 82 |
* @return array List of outdated and active add-ons. |
| 83 |
*/ |
| 84 |
private function get_outdated_active_addons() { |
| 85 |
$outdated_active_addons = array(); |
| 86 |
$plugins = get_plugins(); |
| 87 |
|
| 88 |
foreach ( $this->addon_dependency_map as $addon => $required_version ) { |
| 89 |
if ( is_plugin_active( $addon ) ) { |
| 90 |
$active_addon = $plugins[ $addon ]; |
| 91 |
|
| 92 |
if ( version_compare( $active_addon['Version'], $required_version, '<' ) ) { |
| 93 |
$outdated_active_addons[ $addon ] = $active_addon; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return $outdated_active_addons; |
| 99 |
} |
| 100 |
} |
| 101 |
|