| 1 |
<?php namespace EmailLog\Addon; |
| 2 |
|
| 3 |
use EmailLog\Addon\License\AddonLicense; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 6 |
|
| 7 |
/** |
| 8 |
* Encapsulate Add-on Data. |
| 9 |
* |
| 10 |
* @since 2.0.0 |
| 11 |
* |
| 12 |
* @property-read string $name |
| 13 |
* @property-read string $version |
| 14 |
* @property-read string $thumbnail |
| 15 |
* @property-read string $description |
| 16 |
* @property-read string $link |
| 17 |
* @property-read string $slug |
| 18 |
* @property-read string $file |
| 19 |
* @property-read string $author |
| 20 |
*/ |
| 21 |
class Addon { |
| 22 |
|
| 23 |
private $name; |
| 24 |
private $version; |
| 25 |
private $thumbnail; |
| 26 |
private $description; |
| 27 |
private $link; |
| 28 |
private $slug; |
| 29 |
private $file; |
| 30 |
private $author; |
| 31 |
|
| 32 |
protected $email_log; |
| 33 |
protected $license; |
| 34 |
|
| 35 |
/** |
| 36 |
* Construct Addon object from data array. |
| 37 |
* |
| 38 |
* @param array $data Data array. |
| 39 |
* @param \EmailLog\Addon\License\AddonLicense|null $license Add-on License. |
| 40 |
* @param \EmailLog\Core\EmailLog|null $email_log Email Log instance. |
| 41 |
*/ |
| 42 |
public function __construct( $data, $license = null, $email_log = null ) { |
| 43 |
$this->parse_data( $data ); |
| 44 |
|
| 45 |
if ( null === $license ) { |
| 46 |
$license = new AddonLicense(); |
| 47 |
$license->set_addon_name( $this->name ); |
| 48 |
$license->load(); |
| 49 |
} |
| 50 |
$this->license = $license; |
| 51 |
|
| 52 |
if ( null === $email_log ) { |
| 53 |
$email_log = email_log(); |
| 54 |
} |
| 55 |
$this->email_log = $email_log; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Render the add-on in Addon list page. |
| 60 |
*/ |
| 61 |
public function render() { |
| 62 |
?> |
| 63 |
<div class="el-addon"> |
| 64 |
<h3 class="el-addon-title"> <?php echo esc_html( $this->name ); ?> </h3> |
| 65 |
|
| 66 |
<a href="<?php echo esc_url( $this->link ); ?>?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=addon-grid&utm_content=<?php echo $this->name; ?>" |
| 67 |
title="<?php echo esc_attr( $this->name ); ?>"> |
| 68 |
<img src="<?php echo esc_url( $this->thumbnail ); ?>" class="attachment-showcase wp-post-image" |
| 69 |
alt="<?php echo esc_attr( $this->name ); ?>" title="<?php echo esc_attr( $this->name ); ?>"> |
| 70 |
</a> |
| 71 |
|
| 72 |
<p> <?php echo esc_html( $this->description ); ?> </p> |
| 73 |
|
| 74 |
<?php $this->print_actions(); ?> |
| 75 |
</div> <!-- .el-addon --> |
| 76 |
|
| 77 |
<?php |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Have a magic getter instead of having individual getters. |
| 82 |
* |
| 83 |
* @param string $property Property. |
| 84 |
* |
| 85 |
* @return string Value for the property. |
| 86 |
*/ |
| 87 |
public function __get( $property ) { |
| 88 |
if ( isset( $this->{$property} ) ) { |
| 89 |
return $this->{$property}; |
| 90 |
} |
| 91 |
|
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get Add-on License object. |
| 97 |
* |
| 98 |
* @return \EmailLog\Addon\License\AddonLicense License object. |
| 99 |
*/ |
| 100 |
public function get_license() { |
| 101 |
return $this->license; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get action links for add-ons. |
| 106 |
*/ |
| 107 |
protected function print_actions() { |
| 108 |
if ( $this->has_valid_bundle_license() ) { |
| 109 |
$this->print_valid_actions(); |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
if ( ! $this->has_valid_addon_license() ) { |
| 114 |
$this->print_invalid_actions(); |
| 115 |
} else { |
| 116 |
$this->print_valid_actions(); |
| 117 |
} |
| 118 |
|
| 119 |
$this->render_individual_license(); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Print actions that are available when the license is valid. |
| 124 |
*/ |
| 125 |
protected function print_valid_actions() { |
| 126 |
if ( $this->is_installed() ) { |
| 127 |
$actions = '<a disabled class="button button-secondary">' . _x( 'Installed', 'Installed on website but not activated', 'email-log' ); |
| 128 |
|
| 129 |
if ( $this->is_active() ) { |
| 130 |
$actions .= ' & ' . _x( 'Activated', 'Installed and activated on website', 'email-log' ) . '</a>'; |
| 131 |
} else { |
| 132 |
$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' ) ); |
| 133 |
} |
| 134 |
} else { |
| 135 |
$actions = sprintf( '<a class="button button-primary" href="%s">%s</a>', $this->get_install_url(), _x( 'Install', 'Download and activate addon', 'email-log' ) ); |
| 136 |
} |
| 137 |
|
| 138 |
$actions .= sprintf( ' <a class="button button-secondary" target="_blank" href="%s">%s</a>', $this->get_download_url(), _x( 'Download', 'Download to your computer', 'email-log' ) ); |
| 139 |
|
| 140 |
echo $actions; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Print actions that are available when the license is not valid. |
| 145 |
*/ |
| 146 |
protected function print_invalid_actions() { |
| 147 |
$label = _x( 'Activate License to Install', 'Download and activate addon', 'email-log' ); |
| 148 |
|
| 149 |
if ( $this->is_installed() ) { |
| 150 |
$label = _x( 'Activate License to Use', 'Download and activate addon', 'email-log' ); |
| 151 |
} |
| 152 |
|
| 153 |
printf( |
| 154 |
'<a disabled class="button-secondary disabled" title="%s" href="#">%s</a>', |
| 155 |
__( 'You need an active license to install the add-on', 'email-log' ), |
| 156 |
$label |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Render Individual license form. |
| 162 |
*/ |
| 163 |
protected function render_individual_license() { |
| 164 |
$action = 'el_license_activate'; |
| 165 |
$action_text = __( 'Activate', 'email-log' ); |
| 166 |
$button_class = 'button-primary'; |
| 167 |
$dashicon = 'down'; |
| 168 |
$license_wrap = 'hidden'; |
| 169 |
$expires = ''; |
| 170 |
|
| 171 |
if ( $this->has_valid_addon_license() ) { |
| 172 |
$action = 'el_license_deactivate'; |
| 173 |
$action_text = __( 'Deactivate', 'email-log' ); |
| 174 |
$button_class = ''; |
| 175 |
$dashicon = 'up'; |
| 176 |
$license_wrap = ''; |
| 177 |
|
| 178 |
$expiry_date = date( 'F d, Y', strtotime( $this->get_license()->get_expiry_date() ) ); |
| 179 |
$expires = sprintf( __( 'Your license expires on %s', 'email-log' ), $expiry_date ); |
| 180 |
} |
| 181 |
?> |
| 182 |
|
| 183 |
<span class="el-expander dashicons dashicons-arrow-<?php echo sanitize_html_class( $dashicon ); ?>" |
| 184 |
title="<?php _e( 'Individual add-on license', 'email-log' ); ?>"></span> |
| 185 |
|
| 186 |
<div class="individual-license <?php echo sanitize_html_class( $license_wrap ); ?>"> |
| 187 |
<form method="post"> |
| 188 |
<input type="text" name="el-license" class="el-license" size="36" |
| 189 |
title="<?php _e( 'Email Log License Key', 'email-log' ); ?>" |
| 190 |
placeholder="<?php echo esc_attr( sprintf( __( '%s Add-on License Key', 'email-log' ), $this->name ) ); ?>" |
| 191 |
value="<?php echo esc_attr( $this->get_addon_license_key() ); ?>"> |
| 192 |
|
| 193 |
<input type="submit" class="button button-large <?php echo sanitize_html_class( $button_class ); ?>" |
| 194 |
value="<?php echo esc_attr( $action_text ); ?>"> |
| 195 |
|
| 196 |
<p class="expires"><?php echo esc_html( $expires ); ?></p> |
| 197 |
|
| 198 |
<input type="hidden" name="el-addon" value="<?php echo esc_attr( $this->name ); ?>"> |
| 199 |
<input type="hidden" name="el-action" value="<?php echo esc_attr( $action ); ?>"> |
| 200 |
|
| 201 |
<?php wp_nonce_field( $action, $action . '_nonce' ); ?> |
| 202 |
</form> |
| 203 |
</div> |
| 204 |
<?php |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Is the add-on installed? |
| 210 |
* |
| 211 |
* @return bool True, if installed. False otherwise. |
| 212 |
*/ |
| 213 |
public function is_installed() { |
| 214 |
$installed_plugins = array_keys( get_plugins() ); |
| 215 |
|
| 216 |
return in_array( $this->file, $installed_plugins, true ); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Get the version of the add-on. |
| 221 |
* If the add-on is installed then it returns the installed version, |
| 222 |
* otherwise returns the latest add-on version from server. |
| 223 |
* |
| 224 |
* @return string Add-on version. |
| 225 |
*/ |
| 226 |
public function get_version() { |
| 227 |
if ( ! $this->is_installed() ) { |
| 228 |
return $this->version; |
| 229 |
} |
| 230 |
|
| 231 |
$plugins_data = get_plugins(); |
| 232 |
|
| 233 |
return $plugins_data[ $this->file ]['Version']; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Is the add-on active? |
| 238 |
* |
| 239 |
* @return bool True if the add-on is active, False otherwise. |
| 240 |
*/ |
| 241 |
public function is_active() { |
| 242 |
return is_plugin_active( $this->file ); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Get the activate url for the add-on. |
| 247 |
* |
| 248 |
* @return string Activate url with nonce. |
| 249 |
*/ |
| 250 |
protected function get_activate_url() { |
| 251 |
return wp_nonce_url( network_admin_url( 'plugins.php?action=activate&plugin=' . $this->file ), 'activate-plugin_' . $this->file ); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Get the install url for the add-on. |
| 256 |
* |
| 257 |
* @return string Install url with nonce. |
| 258 |
*/ |
| 259 |
protected function get_install_url() { |
| 260 |
return wp_nonce_url( network_admin_url( 'update.php?action=install-plugin&plugin=' . $this->slug ), 'install-plugin_' . $this->slug ); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Get the download url for add-on. |
| 265 |
* |
| 266 |
* @return string Download url for add-on. |
| 267 |
*/ |
| 268 |
public function get_download_url() { |
| 269 |
$licenser = $this->email_log->get_licenser(); |
| 270 |
|
| 271 |
if ( is_null( $licenser ) ) { |
| 272 |
return ''; |
| 273 |
} |
| 274 |
|
| 275 |
return $licenser->get_addon_download_url( $this->slug ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Is there a valid bundle license? |
| 280 |
* |
| 281 |
* @return bool True if valid, False otherwise. |
| 282 |
*/ |
| 283 |
protected function has_valid_bundle_license() { |
| 284 |
$licenser = $this->email_log->get_licenser(); |
| 285 |
|
| 286 |
if ( is_null( $licenser ) ) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
return $licenser->is_bundle_license_valid(); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Is the license of this add-on valid? |
| 295 |
* |
| 296 |
* @return bool True if valid, False otherwise. |
| 297 |
*/ |
| 298 |
protected function has_valid_addon_license() { |
| 299 |
return $this->get_license()->is_valid(); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Get license key if the add-on has a valid license. |
| 304 |
* |
| 305 |
* @return string|null License key if found, null otherwise. |
| 306 |
*/ |
| 307 |
public function get_addon_license_key() { |
| 308 |
return $this->get_license()->get_license_key(); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Parse and store add-on data from data array. |
| 313 |
* |
| 314 |
* @param array $data Data array. |
| 315 |
*/ |
| 316 |
protected function parse_data( $data ) { |
| 317 |
$this->name = $data['info']['title']; |
| 318 |
$this->version = $data['info']['version']; |
| 319 |
$this->thumbnail = $data['info']['thumbnail']; |
| 320 |
$this->description = $data['info']['excerpt']; |
| 321 |
$this->link = $data['info']['permalink']; |
| 322 |
$this->slug = 'email-log-' . $data['info']['slug']; |
| 323 |
$this->file = sprintf( '%1$s/%1$s.php', $this->slug ); |
| 324 |
$this->author = 'Sudar Muthu'; |
| 325 |
} |
| 326 |
} |
| 327 |
|