Completed
Push — master ( 678818...49679a )
by Sudar
02:11
created

Addon::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
rs 9.4285
1
<?php namespace EmailLog\Addon;
2
3
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
4
5
/**
6
 * Encapsulate Add-on Data.
7
 *
8
 * @since 2.0.0
9
 */
10
class Addon {
11
12
	private $title;
13
	private $thumbnail;
14
	private $description;
15
	private $link;
16
	private $slug;
17
	private $file;
18
19
	/**
20
	 * Construct Addon object from data array.
21
	 *
22
	 * @param array $data Data array.
23
	 */
24
	public function __construct( $data ) {
25
		$this->title       = $data['info']['title'];
26
		$this->thumbnail   = $data['info']['thumbnail'];
27
		$this->description = $data['info']['excerpt'];
28
		$this->link        = $data['info']['permalink'];
29
		$this->slug        = 'email-log-' . $data['info']['slug'];
30
		$this->file        = sprintf( '%1$s/%1$s.php', $this->slug );
31
	}
32
33
	/**
34
	 * Render the add-on in Addon list page.
35
	 */
36
	public function render() {
37
		?>
38
		<div class="el-addon">
39
			<h3 class="el-addon-title"> <?php echo esc_html( $this->title ); ?> </h3>
40
41
			<a href="<?php echo esc_url( $this->link ); ?>" title="<?php echo esc_attr( $this->title ); ?>">
42
				<img src="<?php echo esc_url( $this->thumbnail ); ?>" class="attachment-showcase wp-post-image"
43
					 alt="<?php echo esc_attr( $this->title ); ?>" title="<?php echo esc_attr( $this->title ); ?>">
44
			</a>
45
46
			<p> <?php echo esc_html( $this->description ); ?> </p>
47
48
			<?php echo $this->get_actions(); ?>
49
		</div> <!-- .el-addon -->
50
51
		<?php
52
	}
53
54
	/**
55
	 * Have a magic getter instead of having individual getters.
56
	 *
57
	 * @param string $property Property.
58
	 *
59
	 * @return string Value for the property.
60
	 */
61
	public function __get( $property ) {
62
		if ( isset( $this->{$property} ) ) {
63
			return $this->{$property};
64
		}
65
66
		return false;
67
	}
68
69
	/**
70
	 * Get action links for add-ons.
71
	 *
72
	 * @return string Action links.
73
	 */
74
	protected function get_actions() {
75
		$email_log             = email_log();
76
		$bundle_license_active = $email_log->get_licenser()->is_bundle_license_active();
77
78
		if ( ! $bundle_license_active ) {
79
			return sprintf(
80
				'<a disabled class="button-secondary" title="%s" href="#">%s</a>',
81
				__( 'You need an active license to install the add-on', 'email-log' ),
82
				_x( 'Install', 'Download and activate addon', 'email-log' )
83
			);
84
		}
85
86
		if ( $this->is_installed() ) {
87
			$actions = '<a disabled class="button button-secondary">' . _x( 'Installed', 'Installed on website but not activated', 'email-log' );
88
89
			if ( is_plugin_active( $this->file ) ) {
90
				$actions .= ' &amp; ' . _x( 'Activated', 'Installed and activated on website', 'email-log' ) . '</a>';
91
			} else {
92
				$actions .= sprintf( '</a> <a class="button button-primary" href="%s">%s</a>', $this->get_activate_url(), _x( 'Activate', 'Enable addon so it may be used', 'email-log' ) );
93
			}
94
		} else {
95
			// TODO: Make sure WordPress core can handle add-on installation.
96
			$actions = sprintf( '<a class="button button-primary" href="%s">%s</a>', $this->get_install_url(), _x( 'Install', 'Download and activate addon', 'email-log' ) );
97
		}
98
99
		$actions .= sprintf( ' <a class="button button-secondary" href="%s">%s</a>', $this->get_download_url(), _x( 'Download', 'Download to your computer', 'email-log' ) );
100
101
		return $actions;
102
	}
103
104
	/**
105
	 * Is the add-on installed?
106
	 *
107
	 * @return bool True, if installed. False otherwise.
108
	 */
109
	protected function is_installed() {
110
		$installed_plugins = array_keys( get_plugins() );
111
112
		return in_array( $this->file, $installed_plugins, true );
113
	}
114
115
	/**
116
	 * Get teh activate url for the add-on.
117
	 *
118
	 * @return string Activate url with nonce.
119
	 */
120
	protected function get_activate_url() {
121
		return wp_nonce_url( network_admin_url( 'plugins.php?action=activate&amp;plugin=' . $this->file ), 'activate-plugin_' . $this->file );
122
	}
123
124
	/**
125
	 * Get the install url for the add-on.
126
	 *
127
	 * @return string Install url with nonce.
128
	 */
129
	protected function get_install_url() {
130
		return wp_nonce_url( network_admin_url( 'update.php?action=install-plugin&plugin=' . $this->slug ), 'install-plugin_' . $this->slug );
131
	}
132
133
	/**
134
	 * Get the download url for add-on.
135
	 * TODO: Link correct download url.
136
	 *
137
	 * @return string Download url for add-on.
138
	 */
139
	protected function get_download_url() {
140
		return '';
141
	}
142
}
143