Completed
Push — feature/addons-page ( 330995...77b505 )
by Maria Daniel Deepak
02:36
created

AddonController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 83
ccs 0
cts 36
cp 0
rs 10
wmc 7
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B display_addons() 0 43 5
A render_page() 0 16 1
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
	 * Initialize the plugin.
22
	 */
23
	public function __construct( $file ) {
24
		$this->plugin_dir_url = plugin_dir_url( $file );
25
	}
26
27
	/**
28
	 * Retrieves and outputs the Addon list HTML.
29
	 *
30
	 * return voids
31
	 */
32
	public function display_addons() {
33
		// The products endpoint does not need a key or token to render published products.
34
		// @todo: Change the API Url to get the actual addons.
35
		$response = wp_remote_get( 'http://local.wordpress.dev/edd-api/products/' );
36
37
		if ( is_wp_error( $response ) ) {
38
			// @todo: Modify the error message to be displayed if required.
39
			echo 'Error Found ( '. $response->get_error_message() .' )';
40
		} elseif ( is_array( $response ) ) {
41
			$body = wp_remote_retrieve_body( $response );
42
			// Convert the JSON response to array
43
			$result_set = json_decode( $body, true );
44
			// The array key is set by the EDD plugin
45
			if ( array_key_exists( 'products', $result_set ) ) {
46
47
				$products = $result_set['products'];
48
				foreach ( $products as $product ) : ?>
49
50
					<div class="el-addon">
51
						<h3 class="el-addon-title">
52
							<?php _e( $product['info']['title'], 'email-log' ); ?>
53
						</h3>
54
55
						<a href="#" title="Starter Package">
56
							<!-- @todo: Replace the thumbnail url from the $products array. -->
57
							<img src="https://8333-presscdn-0-98-pagely.netdna-ssl.com/wp-content/themes/edd-v2/images/starter-package.png" class="attachment-showcase wp-post-image" alt="Alternate Title" title="Some Good Title" />
58
						</a>
59
60
						<p>
61
							<?php _e( $product['info']['excerpt'], 'email-log'); ?>
62
						</p>
63
64
						<a href="#" class="button-secondary"><?php _e( 'Gear up!', 'email-log'); ?></a>
65
					</div> <!-- .el-addon -->
66
67
				<?php endforeach;
68
69
			} else {
70
				// @todo: Modify the error message if required.
71
				_e( 'Something went wrong with the API result.', 'email-log' );
72
			}
73
		}
74
	}
75
76
	/**
77
	 * Renders the HTML for the Addons page.
78
	 */
79
	public function render_page() {
80
		// Use Plugin version as CSS version to bust cache.
81
		$stylesheet_version = \EmailLog\Core\EmailLog::VERSION;
82
83
		// Enqueue the required styles
84
		wp_enqueue_style( 'el_addon_adm_pg', $this->plugin_dir_url . 'assets/css/admin/addon-list.css', array(), $stylesheet_version, 'all' )
85
	?>
86
		<p>
87
			<?php _e( 'These extensions <em><strong>add functionality</strong></em> to your existing Email logs.', 'email-log' ); ?>
88
		</p>
89
		<div class="el-container">
90
			<?php $this->display_addons(); ?>
91
			<div class="clear"></div>
92
		</div> <!-- .el-container -->
93
	<?php
94
	}
95
}
96