Completed
Push — master ( 50f480...58e24f )
by Sudar
01:59
created

AddonListRenderer::parse_response()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 9
rs 9.6666
1
<?php namespace EmailLog\Core\UI\Component;
2
3
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
4
5
/**
6
 * Retrieve the list of add-ons and render them.
7
 *
8
 * @since 2.0.0
9
 */
10
class AddonListRenderer {
11
12
	const API_URL = 'https://wpemaillog.com/edd-api/products/?category=addon';
13
	const CACHE_EXPIRY_IN_HRS = 12;
14
	const CACHE_KEY = 'el_addon_list';
15
16
	/**
17
	 * Plugin File.
18
	 *
19
	 * @var string
20
	 */
21
	private $plugin_file;
22
23
	/**
24
	 * Create a new instance with the passed in plugin file.
25
	 *
26
	 * @param string $plugin_file Plugin File.
27
	 */
28
	public function __construct( $plugin_file ) {
29
		$this->plugin_file = $plugin_file;
30
	}
31
32
	/**
33
	 * Setup page to render the list of add-ons.
34
	 */
35
	public function render() {
36
		$email_log = email_log();
37
38
		wp_enqueue_style( 'el_addon_list', plugins_url( 'assets/css/admin/addon-list.css', $this->plugin_file ), array(), $email_log->get_version() );
39
		?>
40
41
		<div class="el-container">
42
			<?php $this->render_addons(); ?>
43
			<div class="clear"></div>
44
		</div> <!-- .el-container -->
45
		<?php
46
	}
47
48
	/**
49
	 * Retrieve the list of add-ons by calling the store API.
50
	 *
51
	 * @return array List of add-ons, empty array if API call fails.
52
	 */
53
	protected function get_addons() {
54
		if ( false === ( $addons = get_transient( self::CACHE_KEY ) ) ) {
55
			$response = wp_remote_get( self::API_URL );
56
57
			if ( is_wp_error( $response ) || ! is_array( $response ) ) {
58
				// TODO: Don't keep trying if the server is down.
59
				return array();
60
			}
61
62
			$addons = $this->parse_response( wp_remote_retrieve_body( $response ) );
63
64
			if ( ! empty( $addons ) ) {
65
				set_transient( self::CACHE_KEY, $addons, self::CACHE_EXPIRY_IN_HRS * HOUR_IN_SECONDS );
66
			}
67
		}
68
69
		return $addons;
70
	}
71
72
	/**
73
	 * Parse the response and get the list of add-on.
74
	 *
75
	 * @param string $response API Response.
76
	 *
77
	 * @return array List of Add-ons.
78
	 */
79
	protected function parse_response( $response ) {
80
		$json = json_decode( $response, true );
81
82
		if ( ! is_array( $json ) || ! array_key_exists( 'products', $json ) ) {
83
			return array();
84
		}
85
86
		return $json['products'];
87
	}
88
89
	/**
90
	 * Render the add-on list or display an error if the list can't be retrieved.
91
	 */
92
	protected function render_addons() {
93
		$email_log = email_log();
94
		$bundle_license_active = $email_log->get_licenser()->is_bundle_license_active();
95
96
		$addons = $this->get_addons();
97
98
		if ( empty( $addons ) ) {
99
			$this->render_empty_list();
100
		}
101
102
		foreach ( $addons as $addon ) {
103
			$this->render_addon( $addon, $bundle_license_active );
104
		}
105
	}
106
107
	/**
108
	 * Renders an individual addon.
109
	 *
110
	 * @param array $addon                 Details about an add-on.
111
	 * @param bool  $bundle_license_active Is the Bundle license active?
112
	 */
113
	protected function render_addon( $addon, $bundle_license_active ) {
114
		$addon_title       = $addon['info']['title'];
115
		$addon_thumbnail   = $addon['info']['thumbnail'];
116
		$addon_description = $addon['info']['excerpt'];
117
		$addon_link        = $addon['info']['permalink'];
118
		$addon_slug        = 'email-log-' . $addon['info']['slug'];
119
		$addon_file        = sprintf( '%1$s/%1$s.php', $addon_slug );
120
		?>
121
		<div class="el-addon">
122
			<h3 class="el-addon-title">
123
				<?php echo esc_html( $addon_title ); ?>
124
			</h3>
125
126
			<a href="<?php echo esc_url( $addon_link ); ?>" title="<?php echo esc_attr( $addon_title ); ?>">
127
				<img src="<?php echo esc_url( $addon_thumbnail ); ?>" class="attachment-showcase wp-post-image"
128
					 alt="<?php echo esc_attr( $addon_title ); ?>" title="<?php echo esc_attr( $addon_title ); ?>">
129
			</a>
130
131
			<p>
132
				<?php echo esc_html( $addon_description ); ?>
133
			</p>
134
135
			<?php
136
			if ( $bundle_license_active ) {
137
				$installed_plugins  = array_keys( get_plugins() );
138
139
				if ( in_array( $addon_file, $installed_plugins, true ) ) {
140
					$actions = '<a disabled class="button button-secondary">' . _x( 'Installed', 'Installed on website but not activated', 'email-log' );
141
					if ( is_plugin_active( $addon_file ) ) {
142
						$actions .= ' &amp; ' . _x( 'Activated', 'Installed and activated on website', 'email-log' ) . '</a>';
143
					} else {
144
						$activate_url = wp_nonce_url( network_admin_url( 'plugins.php?action=activate&amp;plugin=' . $addon_file ), 'activate-plugin_' . $addon_file );
145
						$actions .= sprintf( '</a> <a class="button button-primary" href="%s">%s</a>', $activate_url, _x( 'Activate', 'Enable addon so it may be used', 'email-log' ) );
146
					}
147
				} else {
148
					// TODO: Make sure WordPress core can handle add-on installation.
149
					$install_url = wp_nonce_url( network_admin_url( 'update.php?action=install-plugin&plugin=' . $addon_slug ), 'install-plugin_' . $addon_slug );
150
					$actions     = sprintf( '<a class="button button-primary" href="%s">%s</a>', $install_url, _x( 'Install', 'Download and activate addon', 'email-log' ) );
151
				}
152
153
				// TODO: Link correct download url.
154
				$download_url = '';
155
				$actions .= sprintf( ' <a class="button button-secondary" href="%s">%s</a>', $download_url, _x( 'Download', 'Download to your computer', 'email-log' ) );
156
			} else {
157
				$actions = sprintf(
158
					'<a disabled class="button-secondary" title="%s" href="#">%s</a>',
159
					__( 'You need an active license to install the add-on', 'email-log' ),
160
					_x( 'Install', 'Download and activate addon', 'email-log' )
161
				);
162
			}
163
164
			echo $actions;
165
			?>
166
		</div> <!-- .el-addon -->
167
		<?php
168
	}
169
170
	/**
171
	 * Display a notice if the list of add-on can't be retrieved.
172
	 */
173
	protected function render_empty_list() {
174
		?>
175
		<span class="el-addon-empty">
176
			<?php
177
				printf(
178
					__( 'We are not able to retrieve the add-on list now. Please visit the <a href="%s">add-on page</a> to view the add-ons.', 'email-log' ), // @codingStandardsIgnoreLine
179
					'https://wpemaillog.com/addons'
180
				);
181
			?>
182
		</span>
183
		<?php
184
	}
185
}
186