Completed
Push — master ( 50f480...58e24f )
by Sudar
01:59
created

Licenser::is_bundle_license_active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php namespace EmailLog\License;
2
3
use EmailLog\Core\EmailLog;
4
use EmailLog\Core\Loadie;
5
6
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
7
8
/**
9
 * Handles the add-on licensing for Email Log.
10
 *
11
 * There can be one normal license for each add-on or one bundle license for all add-ons.
12
 * This class is final because we don't want other plugins to interfere with Email Log licensing.
13
 *
14
 * @since 2.0.0
15
 */
16
final class Licenser implements Loadie {
17
18
	private $bundle_license;
19
20
	/**
21
	 * Licenser constructor.
22
	 * If the bundle_license object is not passed a new object is created.
23
	 *
24
	 * @param \EmailLog\License\BundleLicense $bundle_license Optional. Bundle License.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $bundle_license not be BundleLicense|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
25
	 */
26
	public function __construct( $bundle_license = null ) {
27
		if ( ! $bundle_license instanceof BundleLicense ) {
28
			$bundle_license = new BundleLicense();
29
		}
30
31
		$this->bundle_license = $bundle_license;
32
	}
33
34
	/**
35
	 * Load all Licenser related hooks.
36
	 *
37
	 * @inheritdoc
38
	 */
39
	public function load() {
40
		$this->bundle_license->load();
41
42
		add_action( 'el_before_addon_list', array( $this, 'render_bundle_license_form' ) );
43
44
		add_action( 'el_bundle_license_activate', array( $this, 'activate_bundle_license' ) );
45
		add_action( 'el_bundle_license_deactivate', array( $this, 'deactivate_bundle_license' ) );
46
	}
47
48
	/**
49
	 * Render the Bundle License Form.
50
	 */
51
	public function render_bundle_license_form() {
52
		$action = 'el_bundle_license_activate';
53
		$action_text = __( 'Activate', 'email-log' );
54
		$button_class = 'button-primary';
55
56
		if ( $this->is_bundle_license_active() ) {
57
			$action = 'el_bundle_license_deactivate';
58
			$action_text = __( 'Deactivate', 'email-log' );
59
			$button_class = '';
60
		}
61
		?>
62
63
		<div class="bundle-license">
64
			<?php if ( ! $this->is_bundle_license_active() ) : ?>
65
				<p class="notice notice-warning">
66
					<?php
67
						printf(
68
							__( "Enter your license key to activate add-ons. If you don't have a license, then you can <a href='%s' target='_blank'>buy it</a>", 'email-log' ),
69
							'https://wpemaillog.com'
70
						);
71
					?>
72
				</p>
73
			<?php endif; ?>
74
75
			<form method="post">
76
				<input type="text" name="el-license" class="el-license" size="40"
77
				       title="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>"
78
				       placeholder="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>"
79
					   value="<?php echo esc_attr( $this->bundle_license->get_license_key() ); ?>">
80
81
				<input type="submit" class="button button-large <?php echo sanitize_html_class( $button_class ); ?>"
82
					   value="<?php echo esc_attr( $action_text ); ?>">
83
84
				<input type="hidden" name="el-action" value="<?php echo esc_attr( $action ); ?>">
85
86
				<?php wp_nonce_field( $action, $action . '_nonce' ); ?>
87
			</form>
88
		</div>
89
		<?php
90
	}
91
92
	/**
93
	 * Activate Bundle License.
94
	 *
95
	 * @param array $request Request Object.
96
	 */
97
	public function activate_bundle_license( $request ) {
98
		$license_key = sanitize_text_field( $request['el-license'] );
99
100
		$this->bundle_license->set_license_key( $license_key );
101
102
		try {
103
			$this->bundle_license->activate();
104
			$message = __( 'Your license has been activated. You can now install add-ons, will receive automatic updates and access to email support.', 'email-log' );
105
			$type = 'updated';
106
		} catch ( \Exception $e ) {
107
			$message = $e->getMessage();
108
			$type = 'error';
109
		}
110
111
		add_settings_error( 'bundle-license', 'bundle-license', $message, $type );
112
	}
113
114
	/**
115
	 * Deactivate Bundle License.
116
	 */
117
	public function deactivate_bundle_license() {
118
		try {
119
			$this->bundle_license->deactivate();
120
			$message = __( 'Your license has been deactivated. You will not receive automatic updates.', 'email-log' );
121
			$type = 'updated';
122
		} catch ( \Exception $e ) {
123
			$message = $e->getMessage();
124
			$type = 'error';
125
		}
126
127
		add_settings_error( 'bundle-license', 'bundle-license', $message, $type );
128
	}
129
130
	/**
131
	 * Is the bundle license active?
132
	 *
133
	 * @return bool True, if Bundle License is active, False otherwise.
134
	 */
135
	public function is_bundle_license_active() {
136
		return $this->bundle_license->is_active();
137
	}
138
}
139