Completed
Push — master ( 777841...f8a625 )
by Sudar
12:40
created

AddonListRenderer::get_addons()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 0
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
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 = 'http://wpemaillog.dev/edd-api/products/';
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
		<p>
41
			<?php _e( 'These extensions <em><strong>add functionality</strong></em> to your existing Email logs.', 'email-log' ); ?>
42
		</p>
43
44
		<div class="el-container">
45
			<?php $this->render_addons(); ?>
46
			<div class="clear"></div>
47
		</div> <!-- .el-container -->
48
		<?php
49
	}
50
51
	/**
52
	 * Retrieve the list of add-ons by calling the store API.
53
	 *
54
	 * @return array List of add-ons, empty array if API call fails.
55
	 */
56
	protected function get_addons() {
57
		if ( false === ( $addons = get_transient( self::CACHE_KEY ) ) ) {
58
			$response = wp_remote_get( self::API_URL );
59
60
			if ( is_wp_error( $response ) || ! is_array( $response ) ) {
61
				// TODO: Don't keep trying if the server is down.
62
				return array();
63
			}
64
65
			$addons = $this->filter_addons( wp_remote_retrieve_body( $response ) );
66
67
			if ( ! empty( $addons ) ) {
68
				set_transient( self::CACHE_KEY, $addons, self::CACHE_EXPIRY_IN_HRS * HOUR_IN_SECONDS );
69
			}
70
		}
71
72
		return $addons;
73
	}
74
75
	/**
76
	 * Parse the response and get only the list of add-on and ignore bundles.
77
	 *
78
	 * @param string $response API Response.
79
	 *
80
	 * @return array List of Add-ons.
81
	 */
82
	protected function filter_addons( $response ) {
83
		$json = json_decode( $response, true );
84
85
		if ( ! array_key_exists( 'products', $json ) ) {
86
			return array();
87
		}
88
89
		$addons = array();
90
91
		foreach ( $json['products'] as $addon ) {
92
			if ( 'addon' === $addon['info']['category'][0]['slug'] ) {
93
				$addons[] = $addon;
94
			}
95
		}
96
97
		return $addons;
98
	}
99
100
	/**
101
	 * Render the add-on list or display an error if the list can't be retrieved.
102
	 */
103
	protected function render_addons() {
104
		$addons = $this->get_addons();
105
106
		if ( empty( $addons ) ) {
107
			$this->render_empty_list();
108
		}
109
110
		foreach ( $addons as $addon ) {
111
			$this->render_addon( $addon );
112
		}
113
	}
114
115
	/**
116
	 * Renders an individual addon.
117
	 *
118
	 * @param array $addon Details about an add-on.
119
	 */
120
	protected function render_addon( $addon ) {
121
		$addon_title       = $addon['info']['title'];
122
		$addon_thumbnail   = $addon['info']['thumbnail'];
123
		$addon_description = $addon['info']['excerpt'];
124
		?>
125
		<div class="el-addon">
126
			<h3 class="el-addon-title">
127
				<?php echo esc_html( $addon_title ); ?>
128
			</h3>
129
130
			<a href="#" title="<?php echo esc_attr( $addon_title ); ?>">
131
				<img src="<?php echo esc_url( $addon_thumbnail ); ?>" class="attachment-showcase wp-post-image"
132
					 alt="<?php echo esc_attr( $addon_title ); ?>" title="<?php echo esc_attr( $addon_title ); ?>">
133
			</a>
134
135
			<p>
136
				<?php echo esc_html( $addon_description ); ?>
137
			</p>
138
139
			<a href="#" class="button-secondary"><?php _e( 'Gear up!', 'email-log' ); ?></a>
140
		</div> <!-- .el-addon -->
141
		<?php
142
	}
143
144
	/**
145
	 * Display a notice if the list of add-on can't be retrieved.
146
	 */
147
	protected function render_empty_list() {
148
		?>
149
		<span class="el-addon-empty">
150
			<?php
151
				printf(
152
					__( '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
153
					'https://wpemaillog.com/addons'
154
				);
155
			?>
156
		</span>
157
		<?php
158
	}
159
}
160