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 $addon_error_message; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var const Cache expiration in hours. |
27
|
|
|
*/ |
28
|
|
|
const CACHE_EXPIRE_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_result_array = array(); |
|
|
|
|
44
|
|
|
|
45
|
|
|
// Get Addons result array from Cache if available. |
46
|
|
|
if ( false === ( $addons_result_array = get_transient( 'el_addons_result_array' ) ) ) { |
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 ) ) { |
53
|
|
|
/* |
54
|
|
|
* @todo: The same error message is used twice in the class. |
55
|
|
|
* Should we change this to a different one? |
56
|
|
|
*/ |
57
|
|
|
$this->addon_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' ); |
58
|
|
|
} elseif ( is_array( $response ) ) { |
59
|
|
|
$body = wp_remote_retrieve_body( $response ); |
60
|
|
|
// Convert the JSON response to array |
61
|
|
|
$addons_result_array = json_decode( $body, true ); |
62
|
|
|
|
63
|
|
|
/* |
64
|
|
|
* Cache Addons result for speed. |
65
|
|
|
* Transient data other than string type are automatically serialized and deserialized. |
66
|
|
|
* @link http://wordpress.stackexchange.com/a/123031/83739 |
67
|
|
|
*/ |
68
|
|
|
set_transient( 'el_addons_result_array', $addons_result_array, self::CACHE_EXPIRE_IN_HRS * HOUR_IN_SECONDS ); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return $addons_result_array; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Checks Addons result and handles errors. |
76
|
|
|
* |
77
|
|
|
* Invokes `render_addon()` if no errors. |
78
|
|
|
* |
79
|
|
|
* return void |
80
|
|
|
*/ |
81
|
|
|
public function render_addons() { |
82
|
|
|
$addons_result_array = $this->get_addons(); |
83
|
|
|
// Checks for any errors in the API call. |
84
|
|
|
if ( $this->addon_error_message != '' && empty( $addons_result_array ) ) { |
85
|
|
|
$this->render_addon_error(); |
86
|
|
|
} elseif ( array_key_exists( 'products', $addons_result_array ) ) { |
87
|
|
|
// The array key is set by the EDD plugin |
88
|
|
|
$addons = $addons_result_array['products']; |
89
|
|
|
foreach ( $addons as $addon ) { |
90
|
|
|
$this->render_addon( $addon ); |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
// @todo: Include the addon page link |
94
|
|
|
$this->addon_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' ); |
95
|
|
|
$this->render_addon_error(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Renders HTML of individual addon. |
101
|
|
|
* |
102
|
|
|
* return void |
103
|
|
|
*/ |
104
|
|
|
public function render_addon( $addon ) { |
105
|
|
|
$addon_title = __( $addon['info']['title'], 'email-log' ); |
106
|
|
|
$addon_description = __( $addon['info']['excerpt'], 'email-log' ); |
107
|
|
|
$addon_buy_button = __( 'Gear up!', 'email-log'); |
108
|
|
|
?> |
109
|
|
|
<div class="el-addon"> |
110
|
|
|
<h3 class="el-addon-title"> |
111
|
|
|
<?php echo $addon_title; ?> |
112
|
|
|
</h3> |
113
|
|
|
|
114
|
|
|
<a href="#" title="<?php echo $addon_title; ?>"> |
115
|
|
|
<!-- @todo: Replace the thumbnail url from the $products array. --> |
116
|
|
|
<img src="<?php echo $addon['info']['thumbnail']; ?>" class="attachment-showcase wp-post-image" alt="<?php echo $addon_title; ?>" title="<?php echo $addon_title; ?>" /> |
117
|
|
|
</a> |
118
|
|
|
|
119
|
|
|
<p> |
120
|
|
|
<?php echo $addon_description; ?> |
121
|
|
|
</p> |
122
|
|
|
|
123
|
|
|
<a href="#" class="button-secondary"><?php echo $addon_buy_button; ?></a> |
124
|
|
|
</div> <!-- .el-addon --> |
125
|
|
|
<?php |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Render error in Addon page if any. |
130
|
|
|
* |
131
|
|
|
* return void |
132
|
|
|
*/ |
133
|
|
|
public function render_addon_error() { |
134
|
|
|
?> |
135
|
|
|
<span class="el-addon-error"> |
136
|
|
|
<?php |
137
|
|
|
// Error message set in render_addons() method. |
138
|
|
|
echo $this->addon_error_message; |
139
|
|
|
?> |
140
|
|
|
</span> |
141
|
|
|
<?php |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Renders the HTML for the Addons page. |
146
|
|
|
*/ |
147
|
|
|
public function render_page() { |
148
|
|
|
// Use Plugin version as CSS version to bust cache. |
149
|
|
|
$stylesheet_version = \EmailLog\Core\EmailLog::VERSION; |
150
|
|
|
|
151
|
|
|
// Enqueue the required styles |
152
|
|
|
wp_enqueue_style( 'el_addon_adm_pg', $this->plugin_dir_url . 'assets/css/admin/addon-list.css', array(), $stylesheet_version, 'all' ); |
153
|
|
|
?> |
154
|
|
|
<p> |
155
|
|
|
<?php _e( 'These extensions <em><strong>add functionality</strong></em> to your existing Email logs.', 'email-log' ); ?> |
156
|
|
|
</p> |
157
|
|
|
<div class="el-container"> |
158
|
|
|
<?php $this->render_addons(); ?> |
159
|
|
|
<div class="clear"></div> |
160
|
|
|
</div> <!-- .el-container --> |
161
|
|
|
<?php |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.