Skip to content

Commit 7807caf

Browse files
authored
Merge pull request #17 from awesomemotive/issue/15
Add a weekly license check
2 parents 977efb8 + 969e9ce commit 7807caf

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

src/Handlers/Handler.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ public function __construct( string $api_url, array $args = array() ) {
5757
$this->args = wp_parse_args(
5858
$args,
5959
array(
60-
'file' => '',
61-
'item_id' => false,
62-
'version' => false,
63-
'api_url' => $api_url,
60+
'file' => '',
61+
'item_id' => false,
62+
'version' => false,
63+
'api_url' => $api_url,
64+
'weekly_check' => true,
6465
)
6566
);
6667
$this->args['slug'] = $this->get_slug();
@@ -122,6 +123,36 @@ public function ajax_get_license_overlay() {
122123
wp_send_json_success( ob_get_clean() );
123124
}
124125

126+
/**
127+
* Checks the license weekly.
128+
*
129+
* @since <next-version>
130+
* @return void
131+
*/
132+
public function weekly_license_check() {
133+
if ( ! defined( 'DOING_CRON' ) || ! DOING_CRON ) {
134+
return;
135+
}
136+
137+
if ( empty( $this->license->get_license_key() ) ) {
138+
return;
139+
}
140+
141+
$api_params = wp_parse_args(
142+
array(
143+
'edd_action' => 'check_license',
144+
),
145+
$this->get_default_api_request_args()
146+
);
147+
$api = new \EasyDigitalDownloads\Updater\Requests\API( $this->args['api_url'] );
148+
$license_data = $api->make_request( $api_params );
149+
if ( empty( $license_data->success ) ) {
150+
return;
151+
}
152+
153+
$this->license->save( $license_data );
154+
}
155+
125156
/**
126157
* Initializes the auto updater.
127158
*
@@ -152,6 +183,12 @@ private function add_general_listeners() {
152183
add_action( 'wp_ajax_edd_sl_sdk_activate_' . $slug, array( $this->license, 'ajax_activate' ) );
153184
add_action( 'wp_ajax_edd_sl_sdk_delete_' . $slug, array( $this->license, 'ajax_delete' ) );
154185
add_action( 'wp_ajax_edd_sl_sdk_update_tracking_' . $slug, array( $this->license, 'ajax_update_tracking' ) );
186+
if ( ! empty( $this->args['weekly_check'] ) ) {
187+
if ( ! wp_next_scheduled( 'edd_sl_sdk_weekly_license_check_' . $slug ) ) {
188+
wp_schedule_event( time(), 'weekly', 'edd_sl_sdk_weekly_license_check_' . $slug );
189+
}
190+
add_action( 'edd_sl_sdk_weekly_license_check_' . $slug, array( $this, 'weekly_license_check' ) );
191+
}
155192
}
156193

157194
/**

src/Licensing/License.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function ajax_activate() {
146146
}
147147

148148
update_option( $this->get_key_option_name(), $license_key );
149-
update_option( $this->get_status_option_name(), $license_data );
149+
$this->save( $license_data );
150150

151151
wp_send_json_success(
152152
array(
@@ -308,6 +308,17 @@ public function get_license_status_message() {
308308
}
309309
}
310310

311+
/**
312+
* Saves the license data.
313+
*
314+
* @since <next-version>
315+
* @param \stdClass $license_data The license data.
316+
* @return void
317+
*/
318+
public function save( $license_data ) {
319+
update_option( $this->get_status_option_name(), $license_data );
320+
}
321+
311322
/**
312323
* Get the button parameters based on the status.
313324
*
@@ -361,7 +372,7 @@ private function can_manage_license( $nonce_name = 'edd_sl_sdk_license_handler'
361372
* @since <next-version>
362373
* @return string
363374
*/
364-
private function get_status_option_name() {
375+
public function get_status_option_name() {
365376
return ! empty( $this->args['option_name'] ) ? "{$this->args['option_name']}_license" : "{$this->slug}_license";
366377
}
367378
}

src/Registry.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,24 @@ public function register( array $integration ) {
5959
* @return void
6060
*/
6161
private function add( array $integration ) {
62-
if ( ! isset( $integration['id'] ) ) {
62+
if ( empty( $integration['id'] ) ) {
6363
throw new \InvalidArgumentException(
6464
'The integration ID is required.'
6565
);
6666
}
6767

68+
if ( empty( $integration['url'] ) ) {
69+
throw new \InvalidArgumentException(
70+
'The integration URL is required.'
71+
);
72+
}
73+
74+
if ( empty( $integration['item_id'] ) ) {
75+
throw new \InvalidArgumentException(
76+
'The integration item ID is required.'
77+
);
78+
}
79+
6880
if ( $this->offsetExists( $integration['id'] ) ) {
6981
throw new \InvalidArgumentException(
7082
sprintf(

0 commit comments

Comments
 (0)