Completed
Pull Request — master (#142)
by Sudar
02:02
created

BD_License_Handler::display_license_form()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * License Handler for Bulk Delete Addons.
4
 *
5
 * @since      5.0
6
 *
7
 * @author     Sudar
8
 *
9
 * @package    BulkDelete/License
10
 */
11
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
12
13
class BD_License_Handler {
14
	/**
15
	 * Name of the addon.
16
	 *
17
	 * @since 5.0
18
	 */
19
	private $addon_name;
20
21
	/**
22
	 * Code of the addon.
23
	 *
24
	 * @since 5.0
25
	 */
26
	private $addon_code;
27
28
	/**
29
	 * Version of the plugin.
30
	 *
31
	 * @since 5.0
32
	 */
33
	private $version;
34
35
	/**
36
	 * plugin file name.
37
	 *
38
	 * @since 5.0
39
	 */
40
	private $plugin_file;
41
42
	/**
43
	 * Plugin base name.
44
	 *
45
	 * @since 5.5
46
	 */
47
	private $plugin_basename;
48
49
	/**
50
	 * Author of the plugin.
51
	 *
52
	 * @since 5.0
53
	 */
54
	private $author;
55
56
	/**
57
	 * Instance of the updater class.
58
	 *
59
	 * @since 5.5
60
	 */
61
	private $updater;
62
63
	/**
64
	 * Notice Message.
65
	 *
66
	 * @since 5.5
67
	 */
68
	private $notice_msg = '';
69
70
	/**
71
	 * Constructor.
72
	 *
73
	 * @since 5.0
74
	 *
75
	 * @param string $addon_name  Name of the addon
76
	 * @param string $addon_code  Code of the addon
77
	 * @param string $version     Version of the addon
78
	 * @param string $plugin_file Addon file name
79
	 * @param string $author      (optional) Author of the addon
80
	 */
81
	public function __construct( $addon_name, $addon_code, $version, $plugin_file, $author = 'Sudar Muthu' ) {
82
		$this->addon_name      = $addon_name;
83
		$this->addon_code      = $addon_code;
84
		$this->version         = $version;
85
		$this->plugin_file     = $plugin_file;
86
		$this->plugin_basename = plugin_basename( $plugin_file );
87
		$this->author          = $author;
88
89
		$this->hooks();
90
	}
91
92
	/**
93
	 * setup hooks.
94
	 *
95
	 * @access private
96
	 *
97
	 * @since 5.0
98
	 */
99
	private function hooks() {
100
		add_action( 'admin_init', array( $this, 'check_license' ), 0 );
101
		add_action( 'admin_notices', array( $this, 'show_admin_notices' ) );
102
		add_action( 'after_plugin_row_' . $this->plugin_basename, array( $this, 'plugin_row' ), 11, 3 );
103
104
		add_action( 'bd_license_form' , array( $this, 'display_license_form' ) );
105
		add_action( 'bd_license_field', array( $this, 'add_license_field' ) );
106
		add_filter( 'bd_license_input', array( $this, 'parse_license_input' ), 1 );
107
	}
108
109
	/**
110
	 * Check whether the license is valid for the addon.
111
	 *
112
	 * If the license is not valid then add a notice about it.
113
	 * If it is valid then hook the plugin updater.
114
	 *
115
	 * @since 5.5
116
	 */
117
	public function check_license() {
118
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
119
			return;
120
		}
121
122
		$campaign_args = array(
123
			'utm_source'   => 'wpadmin',
124
			'utm_campaign' => 'BulkDelete',
125
			'utm_medium'   => 'header-notice',
126
			'utm_content'  => strtolower( $this->addon_code ),
127
		);
128
		$addon_url = bd_get_addon_url( $this->addon_name, $campaign_args );
129
130
		$license_code = BD_License::get_license_code( $this->addon_code );
131
132
		if ( false == $license_code ) {
133
			$this->notice_msg = sprintf( __( '"%1$s" addon is installed but not activated. To activate the addon, please <a href="%2$s">enter your license key</a>. If you don\'t have a license key, then you can <a href="%3$s" target="_blank">purchase one</a>.', 'bulk-delete' ), $this->addon_name, esc_url( get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::ADDON_PAGE_SLUG ), esc_url( $addon_url ) );
134
		} else {
135
			if ( BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) {
136
				if ( false != $license_code ) {
137
					$this->hook_updater( $license_code );
0 ignored issues
show
Bug introduced by
It seems like $license_code defined by \BD_License::get_license_code($this->addon_code) on line 130 can also be of type boolean; however, BD_License_Handler::hook_updater() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
138
				}
139
			} else {
140
				$this->notice_msg = sprintf( __( 'The license for "%1$s" addon is either invalid or has expired. Please <a href="%2$s" target="_blank">renew the license</a> or <a href="%3$s">enter a new license key</a> to receive updates and support.', 'bulk-delete' ), $this->addon_name, esc_url( $addon_url ), esc_url( get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::ADDON_PAGE_SLUG ) );
141
			}
142
		}
143
	}
144
145
	/**
146
	 * Start the updater.
147
	 *
148
	 * @since 5.0
149
	 * @access private
150
	 *
151
	 * @param string $license_code License Code
152
	 */
153
	private function hook_updater( $license_code ) {
154
		if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
155
			require_once Bulk_Delete::$PLUGIN_DIR . '/include/libraries/EDD_SL_Plugin_Updater.php';
156
		}
157
158
		$this->updater = new EDD_SL_Plugin_Updater( BD_EDD_API_Wrapper::STORE_URL, $this->plugin_file, array(
159
				'version'    => $this->version,
160
				'license'    => $license_code,
161
				'item_name'  => $this->addon_name,
162
				'addon_code' => $this->addon_code,
163
				'author'     => $this->author,
164
				'url'        => home_url(),
165
			)
166
		);
167
	}
168
169
	/**
170
	 * Display notification at the top of all admin pages.
171
	 *
172
	 * @since 5.5
173
	 */
174
	public function show_admin_notices() {
175
		if ( '' != $this->notice_msg ) {
176
			printf( '<div class="error"><p><strong>%s</strong></p></div>', $this->notice_msg );
177
		}
178
	}
179
180
	/**
181
	 * Display license information about addon in plugin list table.
182
	 *
183
	 * @since 5.5
184
	 *
185
	 * @param string $plugin_file Path to the plugin file, relative to the plugins directory.
186
	 * @param array  $plugin_data An array of plugin data.
187
	 * @param string $status      Status of the plugin.
188
	 */
189
	public function plugin_row( $plugin_file, $plugin_data, $status ) {
0 ignored issues
show
Unused Code introduced by
The parameter $plugin_data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
190
		if ( $plugin_file != $this->plugin_basename ) {
191
			return;
192
		}
193
194
		$campaign_args = array(
195
			'utm_source'   => 'wpadmin',
196
			'utm_campaign' => 'BulkDelete',
197
			'utm_medium'   => 'plugin-page',
198
			'utm_content'  => strtolower( $this->addon_code ),
199
		);
200
		$addon_url = bd_get_addon_url( $this->addon_name, $campaign_args );
201
202
		$license_code = BD_License::get_license_code( $this->addon_code );
203
		if ( false == $license_code ) {
204
			$plugin_row_msg = sprintf( __( 'Addon is not activated. To activate the addon, please <a href="%1$s">enter your license key</a>. If you don\'t have a license key, then you can <a href="%2$s" target="_blank">purchase one</a>.', 'bulk-delete' ), esc_url( get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::ADDON_PAGE_SLUG ), esc_url( $addon_url ) );
205
?>
206
			<tr class="plugin-update-tr">
207
				<td colspan="3" class="plugin-update">
208
					<div class="update-message"><span class="bd-licence-activate-notice"><?php echo $plugin_row_msg; ?></span></div>
209
				</td>
210
			</tr>
211
<?php
212
		} else {
213
			if ( ! BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) {
214
				$plugin_row_msg = sprintf( __( 'The license for this addon is either invalid or has expired. Please <a href="%1$s" target="_blank">renew the license</a> or <a href="%2$s">enter a new license key</a> to receive updates and support.', 'bulk-delete' ), esc_url( $addon_url ), esc_url( get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::ADDON_PAGE_SLUG ) );
215
?>
216
				<tr class="plugin-update-tr">
217
					<td colspan="3" class="plugin-update">
218
						<div class="update-message"><span class="bd-licence-activate-notice"><?php echo $plugin_row_msg; ?></span></div>
219
					</td>
220
				</tr>
221
<?php
222
			}
223
		}
224
	}
225
226
	/**
227
	 * Decide whether to display the license form or not.
228
	 *
229
	 * @since 5.0
230
	 */
231
	public function display_license_form() {
232
		if ( ! BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) {
233
			$bd                                = BULK_DELETE();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $bd. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
234
			$bd->display_activate_license_form = true;
235
		}
236
	}
237
238
	/**
239
	 * Add the license field to license form.
240
	 *
241
	 * @since 5.0
242
	 */
243
	public function add_license_field() {
244
		if ( ! BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) {
245
			add_settings_field(
246
				$this->addon_code, // ID
247
				'"' . $this->addon_name . '" ' . __( 'Addon License Key', 'bulk-delete' ), // Title
248
				array( $this, 'print_license_key_field' ), // Callback
249
				Bulk_Delete::ADDON_PAGE_SLUG, // Page
250
				Bulk_Delete::SETTING_SECTION_ID // Section
251
			);
252
		}
253
	}
254
255
	/**
256
	 * Print the license field.
257
	 *
258
	 * @since 5.0
259
	 */
260
	public function print_license_key_field() {
261
		if ( ! BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) {
262
			printf(
263
				'<input type="text" id="%s" name="%s[%s]" placeholder="%s" size="40">',
264
				$this->addon_code,
265
				Bulk_Delete::SETTING_OPTION_NAME,
266
				$this->addon_code,
267
				__( 'Enter license key', 'bulk-delete' )
268
			);
269
		}
270
	}
271
272
	/**
273
	 * Parse the license key and activate it if needed.
274
	 * If the key is invalid, then don't save it in the setting option.
275
	 *
276
	 * @since 5.0
277
	 *
278
	 * @param array $input
279
	 *
280
	 * @return array
281
	 */
282
	public function parse_license_input( $input ) {
283
		if ( is_array( $input ) && key_exists( $this->addon_code, $input ) ) {
284
			$license_code = trim( $input[ $this->addon_code ] );
285
286
			if ( ! empty( $license_code ) ) {
287
				if ( ! BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) {
288
					$activated = BD_License::activate_license( $this->addon_name, $this->addon_code, $license_code );
289
					if ( ! $activated ) {
290
						unset( $input[ $this->addon_code ] );
291
					}
292
				}
293
			} else {
294
				unset( $input[ $this->addon_code ] );
295
			}
296
		} else {
297
			if ( BD_License::has_valid_license( $this->addon_name, $this->addon_code ) ) {
298
				$license_code               = BD_License::get_license_code( $this->addon_code );
299
				$input[ $this->addon_code ] = $license_code;
300
			}
301
		}
302
303
		return $input;
304
	}
305
}
306
?>
307