OverridePluginAPI   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 29
c 2
b 0
f 1
dl 0
loc 78
ccs 0
cts 41
cp 0
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setup_updaters_for_inactive_addons() 0 25 3
A load() 0 4 1
A add_version_to_plugin_api_response() 0 20 6
1
<?php namespace EmailLog\Core\Request;
2
3
use EmailLog\Addon\API\EDDUpdater;
4
use EmailLog\Core\Loadie;
5
6
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
7
8
/**
9
 * Override WordPress Plugin API.
10
 * This is already done by EDD_SL_Plugin_Updater for Active add-on
11
 * and this class does it for all in active or yet to be installed add-ons.
12
 *
13
 * @since 2.0.0
14
 */
15
class OverridePluginAPI implements Loadie {
16
17
	/**
18
	 * Setup actions.
19
	 *
20
	 * @inheritdoc
21
	 */
22
	public function load() {
23
		add_action( 'admin_init', array( $this, 'setup_updaters_for_inactive_addons' ) );
24
25
		add_filter( 'plugins_api_result', array( $this, 'add_version_to_plugin_api_response' ), 100, 3 );
26
	}
27
28
	/**
29
	 * Setup updaters for all in-active addons.
30
	 */
31
	public function setup_updaters_for_inactive_addons() {
32
		$email_log = email_log();
33
		$licenser  = $email_log->get_licenser();
34
35
		if ( is_null( $licenser ) ) {
36
			return;
37
		}
38
39
		$inactive_addons = $licenser->get_addon_list()->get_inactive_addons();
40
41
		foreach ( $inactive_addons as $inactive_addon ) {
42
			$license_key = $licenser->get_addon_license_key( $inactive_addon->name );
43
44
			$updater = new EDDUpdater(
45
				$email_log->get_store_url(),
46
				$inactive_addon->file,
47
				array(
48
					'version'   => $inactive_addon->get_version(),
49
					'license'   => $license_key,
50
					'item_name' => $inactive_addon->name,
51
					'author'    => $inactive_addon->author,
52
				)
53
			);
54
55
			$licenser->add_updater( $updater );
56
		}
57
	}
58
59
	/**
60
	 * Add version attribute to plugin API response.
61
	 *
62
	 * The API response generated by EDD doesn't have the version attribute and it results in some warnings.
63
	 * This method fixes it by manually adding the version attribute to the API response.
64
	 *
65
	 * @since 2.1.0
66
	 *
67
	 * @param object $response API Response.
68
	 * @param string $action   Action name.
69
	 * @param array  $args     Arguments for the function.
70
	 *
71
	 * @return object Modified API response.
72
	 */
73
	public function add_version_to_plugin_api_response( $response, $action, $args ) {
74
		if ( 'plugin_information' !== $action ) {
75
			return $response;
76
		}
77
78
		if ( ! isset( $args->slug ) || ( substr( $args->slug, 0, 10 ) != 'email-log-' ) ) {
79
			return $response;
80
		}
81
82
		if ( isset( $response->version ) ) {
83
			return $response;
84
		}
85
86
		if ( ! isset( $response->new_version ) ) {
87
			return $response;
88
		}
89
90
		$response->version = $response->new_version;
91
92
		return $response;
93
	}
94
}
95