DependencyEnforcer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 91
ccs 0
cts 39
cp 0
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_outdated_active_addons() 0 15 4
A load() 0 3 1
A render_compatibility_notice() 0 20 2
A deactivate_outdated_active_addons() 0 10 2
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
	 *
65
	 * @return array List of add-ons (name and version) that got deactivated.
66
	 */
67
	private function deactivate_outdated_active_addons() {
68
		$deactivated_active_addons = array();
69
70
		$outdated_active_addons = $this->get_outdated_active_addons();
71
		foreach ( $outdated_active_addons as $addon_file_name => $outdated_active_addon ) {
72
			deactivate_plugins( plugin_basename( $addon_file_name ) );
73
			$deactivated_active_addons[] = $outdated_active_addon['Name'] . ' ' . $outdated_active_addon['Version'];
74
		}
75
76
		return $deactivated_active_addons;
77
	}
78
79
	/**
80
	 * Get the list of add-ons that are outdated and are active.
81
	 *
82
	 * @access private
83
	 *
84
	 * @return array List of outdated and active add-ons.
85
	 */
86
	private function get_outdated_active_addons() {
87
		$outdated_active_addons = array();
88
		$plugins                = get_plugins();
89
90
		foreach ( $this->addon_dependency_map as $addon => $required_version ) {
91
			if ( is_plugin_active( $addon ) ) {
92
				$active_addon = $plugins[ $addon ];
93
94
				if ( version_compare( $active_addon['Version'], $required_version, '<' ) ) {
95
					$outdated_active_addons[ $addon ] = $active_addon;
96
				}
97
			}
98
		}
99
100
		return $outdated_active_addons;
101
	}
102
}
103