Completed
Push — feature/addons-page ( 59ba10...83b77b )
by Maria Daniel Deepak
03:58
created

AddonController::render_addons()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 16
ccs 0
cts 12
cp 0
crap 12
rs 9.4285
1
<?php namespace EmailLog\Core\UI\Component;
2
3
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
4
5
/**
6
 * Create Addon list UI.
7
 *
8
 * Retrieve and render the Addons list.
9
 *
10
 * @since   2.0
11
 * @package EmailLog\Core\UI
12
 */
13
class AddonController {
14
15
	/**
16
	 * @var string Plugin basename.
17
	 */
18
	protected $plugin_dir_url;
19
20
	/**
21
	 * @var string Error message.
22
	 */
23
	protected $error_message;
24
25
	/**
26
	 * @var const Cache expiration in hours.
27
	 */
28
	const CACHE_EXPIRY_IN_HRS = 12;
29
30
	/**
31
	 * Initialize the plugin.
32
	 */
33
	public function __construct( $file ) {
34
		$this->plugin_dir_url = plugin_dir_url( $file );
35
	}
36
37
	/**
38
	 * Retrieves and outputs the Addon list HTML.
39
	 *
40
	 * return array If API call fails, the array is empty, else contains the addons info.
41
	 */
42
	protected function get_addons() {
43
		$addons = array();
0 ignored issues
show
Unused Code introduced by
$addons is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
45
		// Get Addons result array from Cache if available.
46
		if ( false === ( $addons = get_transient( 'el_addons_adm' ) ) ) {
47
48
			// The products endpoint does not need a key or token to render published products.
49
			// @todo: Change the API Url to get the actual addons.
50
			$response = wp_remote_get( 'http://local.wordpress.dev/edd-api/products/' );
51
52
			if ( ! is_wp_error( $response ) && is_array( $response ) ) {
53
54
				$body = wp_remote_retrieve_body( $response );
55
				// Convert the JSON response to array
56
				$addons = json_decode( $body, true );
57
58
				/*
59
				 * Cache Addons result for performance.
60
				 * Transient data other than string type are automatically serialized and deserialized.
61
				 * @link http://wordpress.stackexchange.com/a/123031/83739
62
				 */
63
				set_transient( 'el_addons_adm', $addons, self::CACHE_EXPIRY_IN_HRS * HOUR_IN_SECONDS );
64
			} else {
65
				// Incase of error, default to empty array.
66
				$addons = array();
67
			}
68
		}
69
70
		// Default the array to empty when required array key don't exist.
71
		if ( ! array_key_exists( 'products', $addons ) ) {
72
			$addons = array();
73
		}
74
75
		return $addons;
76
	}
77
78
	/**
79
	 * Displays addons.
80
	 *
81
	 * Invokes `render_addon()` to display individual addons.
82
	 *
83
	 * return void
84
	 */
85
	public function render_addons() {
86
		$addons_result = $this->get_addons();
87
88
		// Checks for any errors in the API call.
89
		if ( empty( $addons_result ) ) {
90
			// @todo: Include hyperlink if necessary.
91
			$this->error_message =  __( 'We are not able to retrieve the add-on list now. Visit add-on page link to view the add-ons.', 'email-log' );
92
			$this->render_addon_error();
93
		} else {
94
			// The array key is set by the EDD plugin
95
			$addons = $addons_result['products'];
96
			foreach ( $addons as $addon ) {
97
				$this->render_addon( $addon );
98
			}
99
		}
100
	}
101
102
	/**
103
	 * Renders HTML of individual addon.
104
	 *
105
	 * return void
106
	 */
107
	public function render_addon( $addon ) {
108
		$addon_title       = $addon['info']['title'];
109
		$addon_thumbnail   = $addon['info']['thumbnail'];
110
		$addon_description = $addon['info']['excerpt'];
111
		$addon_buy_button  = __( 'Gear up!', 'email-log');
112
		?>
113
		<div class="el-addon">
114
			<h3 class="el-addon-title">
115
				<?php echo $addon_title; ?>
116
			</h3>
117
118
			<a href="#" title="<?php echo $addon_title; ?>">
119
120
				<img src="<?php echo $addon_thumbnail; ?>" class="attachment-showcase wp-post-image" alt="<?php echo $addon_title; ?>" title="<?php echo $addon_title; ?>" />
121
			</a>
122
123
			<p>
124
				<?php echo $addon_description; ?>
125
			</p>
126
127
			<a href="#" class="button-secondary"><?php echo $addon_buy_button; ?></a>
128
		</div> <!-- .el-addon -->
129
		<?php
130
	}
131
132
	/**
133
	 * Render error in Addon page if any.
134
	 *
135
	 * return void
136
	 */
137
	public function render_addon_error() {
138
		?>
139
		<span class="el-addon-error">
140
			<?php
141
				// Error message set in render_addons() method.
142
				echo $this->error_message;
143
			?>
144
		</span>
145
		<?php
146
	}
147
148
	/**
149
	 * Renders the HTML for the Addons page.
150
	 */
151
	public function render_page() {
152
		// Use Plugin version as CSS version to bust cache.
153
		$stylesheet_version = \EmailLog\Core\EmailLog::VERSION;
154
155
		// Enqueue the required styles
156
		wp_enqueue_style( 'el_addon_adm_pg', $this->plugin_dir_url . 'assets/css/admin/addon-list.css', array(), $stylesheet_version, 'all' );
157
	?>
158
		<p>
159
			<?php _e( 'These extensions <em><strong>add functionality</strong></em> to your existing Email logs.', 'email-log' ); ?>
160
		</p>
161
		<div class="el-container">
162
			<?php $this->render_addons(); ?>
163
			<div class="clear"></div>
164
		</div> <!-- .el-container -->
165
	<?php
166
	}
167
}
168