Completed
Pull Request — master (#56)
by Maria Daniel Deepak
07:14 queued 03:56
created

AddonController::render_addon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 23
ccs 0
cts 13
cp 0
crap 2
rs 9.0856
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();
0 ignored issues
show
Unused Code introduced by
$addons_result_array 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_result_array =  get_transient( "el_addons_result_array" ) ) ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal el_addons_result_array does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
47
			var_dump( 'Not inside if statement' );
0 ignored issues
show
Security Debugging Code introduced by
var_dump('Not inside if statement'); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
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();
0 ignored issues
show
Unused Code introduced by
The call to the method EmailLog\Core\UI\Compone...r::render_addon_error() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
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();
0 ignored issues
show
Unused Code introduced by
The call to the method EmailLog\Core\UI\Compone...r::render_addon_error() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
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
				$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