Skip to content

Commit 30f76f4

Browse files
authored
Merge pull request #2 from duckdev/dev
Merge new changes
2 parents 1872eaa + 1a71573 commit 30f76f4

17 files changed

+1110
-101
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ $freemius = DuckDev\Freemius\Freemius::get_instance(
3030
12345, // Your Freemius product ID.
3131
array(
3232
'slug' => 'loggedin', // Your plugin's unique Freemius slug.
33-
'main_file' => LOGGEDIN_FILE, // The path to your plugin's main file.
33+
'main_file' => LOGGEDIN_FILE, // The path to your plugin's main file.
3434
'public_key' => 'pk_XXXXXXXXXXXXXXXXX', // Your plugin's public key.
3535
)
3636
);

src/api/class-api.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* @license http://www.gnu.org/licenses/ GNU General Public License
77
* @author Joel James <[email protected]>
88
* @since 1.0.0
9+
* @package Freemius
10+
* @subpackage API
911
*/
1012

1113
namespace DuckDev\Freemius\Api;
@@ -187,7 +189,7 @@ public function delete( string $endpoint, array $data = array() ) {
187189
* @return mixed|WP_Error
188190
*/
189191
public function prepare_response( $response ) {
190-
// If WP error, return.
192+
// If WP error, return as it is.
191193
if ( is_wp_error( $response ) ) {
192194
return $response;
193195
}
@@ -225,7 +227,7 @@ protected function prepare_request( string $method, string $endpoint, array $dat
225227

226228
$headers = array();
227229

228-
// Sign the request for auth.
230+
// Sign the request for auth if both pub and secret keys are set.
229231
if ( $this->public_key && $this->secret_key ) {
230232
$headers = $this->get_signed_headers(
231233
$endpoint,
@@ -237,6 +239,7 @@ protected function prepare_request( string $method, string $endpoint, array $dat
237239
);
238240
}
239241

242+
// Perform the request.
240243
return $this->perform_http_request( $method, $url, $data, $headers );
241244
}
242245

@@ -254,6 +257,7 @@ protected function prepare_request( string $method, string $endpoint, array $dat
254257
protected function prepare_url( string $method, string $endpoint, array $data = array() ): string {
255258
$url = $this->base_url . $endpoint;
256259

260+
// For GET request add query params.
257261
if ( $method === 'GET' && ! empty( $data ) ) {
258262
$url = add_query_arg( $data, $url );
259263
}
@@ -297,11 +301,13 @@ protected function prepare_endpoint( string $endpoint ): string {
297301
protected function perform_http_request( string $method, string $url, array $data = array(), array $headers = array() ) {
298302
$method = strtoupper( $method );
299303
$body = null;
304+
// Make sure the content type is JSON.
300305
if ( in_array( $method, array( 'POST', 'PUT', 'DELETE' ) ) ) {
301306
$headers['Content-type'] = 'application/json';
302307
$body = json_encode( $data );
303308
}
304309

310+
// Request args.
305311
$args = array(
306312
'method' => $method,
307313
'connect_timeout' => 10,
@@ -381,6 +387,7 @@ private function get_signed_headers(
381387
$content_type = '';
382388
$date = date( 'r', time() );
383389

390+
// Make sure the content type is JSON.
384391
if ( in_array( $method, array( 'POST', 'PUT' ) ) ) {
385392
$content_type = 'application/json';
386393
}

src/data/class-plugin.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* @license http://www.gnu.org/licenses/ GNU General Public License
77
* @author Joel James <[email protected]>
88
* @since 1.0.0
9+
* @package Freemius
10+
* @subpackage Data
911
*/
1012

1113
namespace DuckDev\Freemius\Data;
@@ -54,6 +56,33 @@ class Plugin {
5456
*/
5557
private string $public_key = '';
5658

59+
/**
60+
* Is a premium plugin.
61+
*
62+
* @since 1.0.0
63+
*
64+
* @var bool
65+
*/
66+
private bool $is_premium;
67+
68+
/**
69+
* Has addons.
70+
*
71+
* @since 1.0.0
72+
*
73+
* @var bool
74+
*/
75+
private bool $has_addons;
76+
77+
/**
78+
* Plugin data.
79+
*
80+
* @since 1.0.0
81+
*
82+
* @var array
83+
*/
84+
private array $data = array();
85+
5786
/**
5887
* Plugin class constructor.
5988
*
@@ -67,6 +96,8 @@ class Plugin {
6796
public function __construct( int $id, array $args ) {
6897
$this->id = $id;
6998
$this->slug = $args['slug'] ?? '';
99+
$this->is_premium = $args['is_premium'] ?? false;
100+
$this->has_addons = $args['has_addons'] ?? false;
70101
$this->main_file = $args['main_file'] ?? '';
71102
$this->public_key = $args['public_key'] ?? '';
72103
}
@@ -114,4 +145,45 @@ public function get_main_file(): string {
114145
public function get_public_key(): string {
115146
return $this->public_key;
116147
}
148+
149+
/**
150+
* Check if current plugin is premium.
151+
*
152+
* @since 1.0.0
153+
*
154+
* @return bool
155+
*/
156+
public function is_premium(): bool {
157+
return $this->is_premium;
158+
}
159+
160+
/**
161+
* Check if current plugin has addons.
162+
*
163+
* @since 1.0.0
164+
*
165+
* @return bool
166+
*/
167+
public function has_addons(): bool {
168+
return $this->has_addons;
169+
}
170+
171+
/**
172+
* Get current plugin data.
173+
*
174+
* @since 1.0.0
175+
*
176+
* @return array
177+
*/
178+
public function get_data(): array {
179+
if ( empty( $this->data ) ) {
180+
if ( ! function_exists( '\get_plugin_data' ) ) {
181+
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
182+
}
183+
184+
$this->data = \get_plugin_data( $this->main_file );
185+
}
186+
187+
return $this->data;
188+
}
117189
}

src/freemius.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
/**
33
* The Freemius SDK for Duck Dev plugins.
44
*
5+
* This library does not perform any permission checks or nonce
6+
* verifications. Plugins should do it before processing any forms.
7+
*
8+
* References:
9+
* - https://github.com/Freemius/wp-sdk-lite
10+
* - https://github.com/gambitph/freemius-lite-activation
11+
*
512
* @link https://duckdev.com/
613
* @license http://www.gnu.org/licenses/ GNU General Public License
714
* @author Joel James <[email protected]>
@@ -59,7 +66,9 @@ class Freemius {
5966
* @param array $args Arguments.
6067
*/
6168
protected function __construct( int $id, array $args ) {
62-
$plugin = new Plugin( $id, $args );
69+
// Create a plugin data instance.
70+
$plugin = new Plugin( $id, $args );
71+
// Create services.
6372
$this->license = new License( $plugin );
6473
$this->update = new Update( $plugin );
6574
$this->addon = new Addon( $plugin );

src/services/class-addon.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This class handles addons.
3+
* This class handles addons data.
44
*
55
* @license http://www.gnu.org/licenses/ GNU General Public License
66
* @author Joel James <[email protected]>
@@ -30,9 +30,15 @@ class Addon extends Service {
3030
* @return array
3131
*/
3232
public function get_addons(): array {
33+
// Only if current plugin has addons.
34+
if ( $this->plugin->has_addons() ) {
35+
return array();
36+
}
37+
3338
// Get from cache first.
3439
$addons = $this->get_transient( 'addons' );
35-
if ( ! empty( $addons ) ) {
40+
// If found is cache, return it.
41+
if ( false !== $addons ) {
3642
return $addons;
3743
}
3844

@@ -56,9 +62,14 @@ public function get_addons(): array {
5662
*
5763
* @since 1.0.0
5864
*
59-
* @return array|string|WP_Error
65+
* @return array|WP_Error
6066
*/
6167
protected function get_remote_addons() {
68+
// Avoid multiple requests.
69+
if ( $this->is_duplicate_request( 'addons_check' ) ) {
70+
return new WP_Error( 'too_many_requests', __( 'Too many requests. Slow down.', 'duckdev-freemius' ) );
71+
}
72+
6273
// Get authenticated API instance using public key.
6374
$api = Api::get_auth_instance(
6475
$this->plugin->get_id(),
@@ -67,7 +78,7 @@ protected function get_remote_addons() {
6778
'plugin'
6879
);
6980

70-
// Addon list from API.
81+
// Addon list from the API.
7182
$response = $api->get(
7283
'addons.json',
7384
array(
@@ -76,6 +87,9 @@ protected function get_remote_addons() {
7687
)
7788
);
7889

90+
// To prevent multiple requests for 5 mins.
91+
$this->set_request_time( 'addons_check' );
92+
7993
if ( is_wp_error( $response ) ) {
8094
return $response;
8195
}
@@ -84,7 +98,7 @@ protected function get_remote_addons() {
8498
}
8599

86100
/**
87-
* Get the list of addons from the API.
101+
* Format the addon list to add additional data.
88102
*
89103
* @since 1.0.0
90104
*
@@ -96,6 +110,14 @@ protected function format_addon_data( $addon ): array {
96110
// Premium if pricing is visible.
97111
$addon['is_premium'] = $addon['is_pricing_visible'] ?? false;
98112

99-
return $addon;
113+
/**
114+
* Filter to modify addon data.
115+
*
116+
* @since 1.0.0
117+
*
118+
* @param array $addon Addon data.
119+
* @param Addon $this Current class instance.
120+
*/
121+
return apply_filters( 'duckdev_freemius_format_addon_data', $addon, $this );
100122
}
101123
}

0 commit comments

Comments
 (0)