PluginProbe
Email Log / 2.1.0
Email Log v2.1.0
2.63 2.4.8 2.4.9 2.6 2.61 2.62 trunk 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.8.1 0.9 0.9.1 0.9.2 1.1 1.5 1.5.1 1.5.2 1.5.3 1.5.4 All 61 releases
email-log / include / Addon / AddonList.php

AddonList.php in Email Log 2.1.0, at include/Addon/AddonList.php

209 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace EmailLog\Addon;
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 AddonList {
11
12 const CACHE_EXPIRY_IN_HRS = 12;
13 const CACHE_KEY = 'el_addon_data';
14
15 /**
16 * Add-on list.
17 *
18 * @var Addon[]
19 */
20 protected $addons;
21
22 /**
23 * Store URL.
24 *
25 * @var string
26 */
27 protected $store_url;
28
29 /**
30 * Create a list of add-ons.
31 *
32 * @param Addon[]|null $addons List of Add-ons. If not passed, they will be automatically loaded.
33 * @param string|null $store_url Store url.
34 */
35 public function __construct( $addons = null, $store_url = null ) {
36 if ( null === $store_url ) {
37 $email_log = email_log();
38 $store_url = $email_log->get_store_url();
39 }
40 $this->store_url = $store_url;
41
42 if ( null === $addons ) {
43 $addons = $this->get_addons();
44 }
45
46 $this->addons = $addons;
47 }
48
49 /**
50 * Get an add-on by name.
51 *
52 * @param string $name Add-on name.
53 *
54 * @return \EmailLog\Addon\Addon|false Add-on if found, False otherwise.
55 */
56 public function get_addon_by_name( $name ) {
57 if ( array_key_exists( $name, $this->addons ) ) {
58 return $this->addons[ $name ];
59 }
60
61 return false;
62 }
63
64 /**
65 * Get all add-ons that are not active (either not installed or not activated).
66 *
67 * @return \EmailLog\Addon\Addon[] List of inactive add-ons.
68 */
69 public function get_inactive_addons() {
70 $inactive_addons = array();
71
72 foreach ( $this->addons as $addon ) {
73 if ( ! $addon->is_active() ) {
74 $inactive_addons[] = $addon;
75 }
76 }
77
78 return $inactive_addons;
79 }
80
81 /**
82 * Setup page to render the list of add-ons.
83 */
84 public function render() {
85 ?>
86
87 <div class="el-container">
88 <?php $this->render_addons(); ?>
89 <div class="clear"></div>
90 </div> <!-- .el-container -->
91 <?php
92 }
93
94 /**
95 * Retrieve the list of add-ons by calling the store API.
96 * If the store API is down, then read the data from the local JSON file.
97 *
98 * @return Addon[] List of add-ons, empty array if API call fails.
99 */
100 protected function get_addons() {
101 $json = get_transient( self::CACHE_KEY );
102 if ( false === $json ) {
103 $response = wp_remote_get( $this->get_api_url() );
104
105 if ( is_wp_error( $response ) || ! is_array( $response ) ) {
106 $json_string = $this->get_addon_data_from_local_file();
107 } else {
108 $json_string = wp_remote_retrieve_body( $response );
109 }
110
111 $json = json_decode( $json_string, true );
112
113 if ( ! is_array( $json ) ) {
114 return array();
115 }
116
117 set_transient( self::CACHE_KEY, $json, self::CACHE_EXPIRY_IN_HRS * HOUR_IN_SECONDS );
118 }
119
120 return $this->parse_response( $json );
121 }
122
123 /**
124 * Parse the response and get the list of add-on.
125 *
126 * @param array $data JSON Data array.
127 *
128 * @return array List of Add-ons.
129 */
130 protected function parse_response( $data ) {
131 if ( ! array_key_exists( 'products', $data ) ) {
132 return array();
133 }
134
135 return $this->build_addon_list( $data['products'] );
136 }
137
138 /**
139 * Build a list of Addon objects from products data array.
140 *
141 * @param array $products Products data array.
142 *
143 * @return Addon[] List of Addons.
144 */
145 protected function build_addon_list( $products ) {
146 $addons = array();
147
148 foreach ( $products as $product ) {
149 $addon = new Addon( $product );
150 $addons[ $addon->name ] = $addon;
151 }
152
153 return $addons;
154 }
155
156 /**
157 * Render the add-on list or display an error if the list can't be retrieved.
158 */
159 protected function render_addons() {
160 if ( empty( $this->addons ) ) {
161 $this->render_empty_list();
162 }
163
164 foreach ( $this->addons as $addon ) {
165 $addon->render();
166 }
167 }
168
169 /**
170 * Display a notice if the list of add-on can't be retrieved.
171 */
172 protected function render_empty_list() {
173 ?>
174 <span class="el-addon-empty">
175 <?php
176 printf(
177 __( '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
178 'https://wpemaillog.com/store/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=addon-grid-failed'
179 );
180 ?>
181 </span>
182 <?php
183 }
184
185 /**
186 * Get API URL.
187 *
188 * @return string API URL.
189 */
190 protected function get_api_url() {
191 return $this->store_url . '/edd-api/products/?category=addon';
192 }
193
194 /**
195 * Read the add-on data from the local data file.
196 *
197 * @since 2.1
198 *
199 * @return false|string JSON file content, False on failure.
200 */
201 private function get_addon_data_from_local_file() {
202 $email_log = email_log();
203
204 $local_json_file_path = $email_log->get_plugin_path() . 'data/products.json';
205
206 return file_get_contents( $local_json_file_path );
207 }
208 }
209