Skip to content

Commit 7a8bb4c

Browse files
committed
Merge branch 'main' into issue/14
2 parents 8826a32 + 7807caf commit 7a8bb4c

File tree

4 files changed

+109
-12
lines changed

4 files changed

+109
-12
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
@@ -145,7 +145,7 @@ public function ajax_activate() {
145145
}
146146

147147
update_option( $this->get_key_option_name(), $license_key );
148-
update_option( $this->get_status_option_name(), $license_data );
148+
$this->save( $license_data );
149149

150150
wp_send_json_success(
151151
array(
@@ -307,6 +307,17 @@ public function get_license_status_message() {
307307
}
308308
}
309309

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

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(

src/Requests/API.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,51 @@ public function make_request( $api_params = array() ) {
8989
* @return array
9090
*/
9191
private function get_body( array $api_params ) {
92-
return wp_parse_args(
93-
$api_params,
94-
array(
95-
'url' => rawurlencode( home_url() ),
96-
)
92+
93+
/**
94+
* Filters the API parameters. The hook is specific to the API URL.
95+
* For example, if the API URL is https://example.com, the hook will be `edd_sl_sdk_api_params_example`.
96+
*
97+
* @since <next-version>
98+
* @param array $api_params The parameters for the specific request.
99+
* @param string $api_url The API URL.
100+
* @return array
101+
*/
102+
return apply_filters(
103+
$this->get_api_filter_hook(),
104+
wp_parse_args(
105+
$api_params,
106+
array(
107+
'url' => rawurlencode( home_url() ),
108+
'environment' => wp_get_environment_type(),
109+
)
110+
),
111+
$this->api_url
97112
);
98113
}
99114

115+
/**
116+
* Gets the API hook.
117+
*
118+
* @since <next-version>
119+
* @return string
120+
*/
121+
private function get_api_filter_hook() {
122+
$url = wp_parse_url( $this->api_url );
123+
if ( empty( $url['host'] ) ) {
124+
return 'edd_sl_sdk_api_params';
125+
}
126+
127+
$base = explode( '.', $url['host'] );
128+
129+
// If the base is a subdomain, use the main domain.
130+
if ( count( $base ) > 2 ) {
131+
$base = array_slice( $base, 1 );
132+
}
133+
134+
return 'edd_sl_sdk_api_params_' . reset( $base );
135+
}
136+
100137
/**
101138
* Determines if a request has recently failed.
102139
*

0 commit comments

Comments
 (0)