forked from awesomemotive/EDD-License-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEDD_License_Handler.php
More file actions
219 lines (179 loc) · 6.37 KB
/
EDD_License_Handler.php
File metadata and controls
219 lines (179 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* License handler for EDD
*
* This class should simplify the process of adding license information
* to new EDD extensions.
*
* @author Daniel J Griffiths
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
if ( !class_exists( 'EDD_License' ) ) {
class EDD_License {
private $file;
private $license;
private $item_name;
private $item_shortname;
private $version;
private $author;
/**
* Class constructor
*
* @global array $edd_options
* @param string $_file
* @param string $_item_name
* @param string $_version
* @param string $_author
*/
function __construct( $_file, $_item_name, $_version, $_author ) {
global $edd_options;
$this->file = $_file;
$this->item_name = $_item_name;
$this->item_shortname = 'edd_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->item_name ) ) );
$this->version = $_version;
$this->license = isset( $edd_options[$this->item_shortname . '_license_key'] ) ? trim( $edd_options[$this->item_shortname . '_license_key'] ) : '';
$this->author = $_author;
// Setup hooks
$this->includes();
$this->hook();
$this->auto_updater();
}
/**
* Include the updater class
*
* @access private
* @return void
*/
private function includes() {
if ( !class_exists( 'EDD_SL_Plugin_Updater' ) )
require_once 'EDD_SL_Plugin_Updater.php';
}
/**
* Setup hooks
*
* @access private
* @return void
*/
private function hook() {
// Register settings
add_filter( 'edd_settings_licenses', array( $this, 'settings' ), 1 );
// Activate license key on settings save
add_action( 'admin_init', array( $this, 'activate_license' ) );
// Deactivate license key
add_action( 'admin_init', array( $this, 'deactivate_license' ) );
}
/**
* Auto updater
*
* @access private
* @global array $edd_options
* @return void
*/
private function auto_updater() {
// Setup the updater
$edd_updater = new EDD_SL_Plugin_Updater( 'https://easydigitaldownloads.com', $this->file, array(
'version' => $this->version,
'license' => $this->license,
'item_name' => $this->item_name,
'author' => $this->author
)
);
}
/**
* Add license field to settings
*
* @access public
* @param array $settings
* @return array
*/
public function settings( $settings ) {
$edd_license_settings = array(
array(
'id' => $this->item_shortname . '_license_key',
'name' => sprintf( __( '%1$s License Key', 'edd' ), $this->item_name ),
'desc' => '',
'type' => 'license_key',
'options' => array( 'is_valid_license_option' => $this->item_shortname . '_license_active' ),
'size' => 'regular'
)
);
return array_merge( $settings, $edd_license_settings );
}
/**
* Activate the license key
*
* @access public
* @return void
*/
public function activate_license() {
if ( !isset( $_POST['edd_settings_licenses'] ) ) return;
if ( !isset( $_POST['edd_settings_licenses'][$this->item_shortname . '_license_key'] ) ) return;
if ( get_option( $this->item_shortname . '_license_active' ) == 'valid' ) return;
$license = sanitize_text_field( $_POST['edd_settings_licenses'][$this->item_shortname . '_license_key'] ) ;
// Data to send to the API
$api_params = array(
'edd_action' => 'activate_license',
'license' => $license,
'item_name' => urlencode( $this->item_name )
);
// Call the API
$response = wp_remote_get( add_query_arg( $api_params, 'https://easydigitaldownloads.com' ), array( 'timeout' => 15, 'body' => $api_params, 'sslverify' => false ) );
// Make sure there are no errors
if ( is_wp_error( $response ) ) return;
// Decode license data
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
update_option( $this->item_shortname . '_license_active', $license_data->license );
}
/**
* Deactivate the license key
*
* @access public
* @return void
*/
public function deactivate_license() {
if ( !isset( $_POST['edd_settings_licenses'] ) ) return;
if ( !isset( $_POST['edd_settings_licenses'][$this->item_shortname . '_license_key'] ) ) return;
// Run on deactivate button press
if ( isset( $_POST[$this->item_shortname . '_license_key_deactivate'] ) ) {
// Run a quick security check
if ( !check_admin_referer( $this->item_shortname . '_license_key_nonce', $this->item_shortname . '_license_key_nonce' ) ) return;
// Data to send to the API
$api_params = array(
'edd_action' => 'deactivate_license',
'license' => $this->license,
'item_name' => urlencode( $this->item_name )
);
// Call the API
$response = wp_remote_get( add_query_arg( $api_params, 'https://easydigitaldownloads.com' ), array( 'timeout' => 15, 'sslverify' => false ) );
// Make sure there are no errors
if ( is_wp_error( $response ) ) return;
// Decode the license data
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
if ( $license_data->license == 'deactivated' )
delete_option( $this->item_shortname . '_license_active' );
}
}
}
}
/**
* Register the new license field type
*
* This has been included in core, but is maintained for backwards compatibility
*
* @return void
*/
if ( !function_exists( 'edd_license_key_callback' ) ) {
function edd_license_key_callback( $args ) {
global $edd_options;
if ( isset( $edd_options[ $args['id'] ] ) ) { $value = $edd_options[ $args['id'] ]; } else { $value = isset( $args['std'] ) ? $args['std'] : ''; }
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
$html = '<input type="text" class="' . $size . '-text" id="edd_settings_' . $args['section'] . '[' . $args['id'] . ']" name="edd_settings_' . $args['section'] . '[' . $args['id'] . ']" value="' . esc_attr( $value ) . '"/>';
if ( 'valid' == get_option( $args['options']['is_valid_license_option'] ) ) {
$html .= wp_nonce_field( $args['id'] . '_nonce', $args['id'] . '_nonce', false );
$html .= '<input type="submit" class="button-secondary" name="' . $args['id'] . '_deactivate" value="' . __( 'Deactivate License', 'edd-recurring' ) . '"/>';
}
$html .= '<label for="edd_settings_' . $args['section'] . '[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
echo $html;
}
}