1 <?php
2 3 4 5 6 7 8 9 10
11
12 if ( ! defined( 'ABSPATH' ) ) exit;
13
14 if ( ! class_exists( 'EDD_License' ) ) :
15
16 17 18 19 20
21 class EDD_License {
22 private $file;
23 private $license;
24 private $item_name;
25 private $item_shortname;
26 private $version;
27 private $author;
28
29 30 31 32 33 34 35 36 37
38 function __construct( $_file, $_item_name, $_version, $_author, $_optname = null ) {
39 global $edd_options;
40
41 $this->file = $_file;
42 $this->item_name = $_item_name;
43 $this->item_shortname = 'edd_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->item_name ) ) );
44 $this->version = $_version;
45 $this->license = isset( $edd_options[ $this->item_shortname . '_license_key' ] ) ? trim( $edd_options[ $this->item_shortname . '_license_key' ] ) : '';
46 $this->author = $_author;
47
48 49 50 51 52 53
54 if ( ! empty( $_optname ) && isset( $edd_options[ $_optname ] ) && empty( $this->license ) ) {
55 $this->license = trim( $edd_options[ $_optname ] );
56 }
57
58
59 $this->includes();
60 $this->hooks();
61 $this->auto_updater();
62 }
63
64 65 66 67 68 69
70 private function includes() {
71 if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) require_once 'class-edd-sl-plugin-updater.php';
72 }
73
74 75 76 77 78 79
80 private function hooks() {
81
82 add_filter( 'edd_settings_licenses', array( $this, 'settings' ), 1 );
83
84
85 add_action( 'admin_init', array( $this, 'activate_license' ) );
86
87
88 add_action( 'admin_init', array( $this, 'deactivate_license' ) );
89 }
90
91 92 93 94 95 96 97
98 private function auto_updater() {
99
100 $edd_updater = new EDD_SL_Plugin_Updater(
101 'https://easydigitaldownloads.com',
102 $this->file,
103 array(
104 'version' => $this->version,
105 'license' => $this->license,
106 'item_name' => $this->item_name,
107 'author' => $this->author
108 )
109 );
110 }
111
112
113 114 115 116 117 118 119
120 public function settings( $settings ) {
121 $edd_license_settings = array(
122 array(
123 'id' => $this->item_shortname . '_license_key',
124 'name' => sprintf( __( '%1$s License Key', 'edd' ), $this->item_name ),
125 'desc' => '',
126 'type' => 'license_key',
127 'options' => array( 'is_valid_license_option' => $this->item_shortname . '_license_active' ),
128 'size' => 'regular'
129 )
130 );
131
132 return array_merge( $settings, $edd_license_settings );
133 }
134
135
136 137 138 139 140 141
142 public function activate_license() {
143 if ( ! isset( $_POST['edd_settings_licenses'] ) )
144 return;
145
146 if ( ! isset( $_POST['edd_settings_licenses'][ $this->item_shortname . '_license_key' ] ) )
147 return;
148
149 if ( 'valid' == get_option( $this->item_shortname . '_license_active' ) )
150 return;
151
152 $license = sanitize_text_field( $_POST['edd_settings_licenses'][ $this->item_shortname . '_license_key' ] );
153
154
155 $api_params = array(
156 'edd_action' => 'activate_license',
157 'license' => $license,
158 'item_name' => urlencode( $this->item_name )
159 );
160
161
162 $response = wp_remote_get(
163 add_query_arg( $api_params, 'https://easydigitaldownloads.com' ),
164 array(
165 'timeout' => 15,
166 'body' => $api_params,
167 'sslverify' => false
168 )
169 );
170
171
172 if ( is_wp_error( $response ) )
173 return;
174
175
176 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
177
178 update_option( $this->item_shortname . '_license_active', $license_data->license );
179 }
180
181
182 183 184 185 186 187
188 public function deactivate_license() {
189 if ( ! isset( $_POST['edd_settings_licenses'] ) )
190 return;
191
192 if ( ! isset( $_POST['edd_settings_licenses'][ $this->item_shortname . '_license_key' ] ) )
193 return;
194
195
196 if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) {
197
198 if ( check_admin_referer( $this->item_shortname . '_license_key_nonce', $this->item_shortname . '_license_key_nonce' ) )
199 return;
200
201
202 $api_params = array(
203 'edd_action' => 'deactivate_license',
204 'license' => $this->license,
205 'item_name' => urlencode( $this->item_name )
206 );
207
208
209 $response = wp_remote_get(
210 add_query_arg( $api_params, 'https://easydigitaldownloads.com' ),
211 array(
212 'timeout' => 15,
213 'sslverify' => false
214 )
215 );
216
217
218 if ( is_wp_error( $response ) )
219 return;
220
221
222 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
223
224 if ( $license_data->license == 'deactivated' )
225 delete_option( $this->item_shortname . '_license_active' );
226 }
227 }
228 }
229
230 endif;
231
232
233 234 235 236 237 238 239
240 if ( ! function_exists( 'edd_license_key_callback' ) ) {
241 function edd_license_key_callback( $args ) {
242 global $edd_options;
243
244 if ( isset( $edd_options[ $args['id'] ] ) )
245 $value = $edd_options[ $args['id'] ];
246 else
247 $value = isset( $args['std'] ) ? $args['std'] : '';
248
249 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
250 $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 ) . '"/>';
251
252 if ( 'valid' == get_option( $args['options']['is_valid_license_option'] ) ) {
253 $html .= wp_nonce_field( $args['id'] . '_nonce', $args['id'] . '_nonce', false );
254 $html .= '<input type="submit" class="button-secondary" name="' . $args['id'] . '_deactivate" value="' . __( 'Deactivate License', 'edd-recurring' ) . '"/>';
255 }
256
257 $html .= '<label for="edd_settings_' . $args['section'] . '[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
258
259 echo $html;
260 }
261 }
262