Skip to content

Commit 9beac7a

Browse files
committed
Improve: Added addons improvements
1 parent 7c894cd commit 9beac7a

File tree

7 files changed

+265
-130
lines changed

7 files changed

+265
-130
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Freemius Plugin Licensing
22

3-
This is a lite version of the main Freemius SDK, specifically developed for use in Duck Dev WordPress plugins. This library focuses exclusively on managing plugin license activation, deactivation, and updates. It does not provide any user interface, so your plugin will need to create its own UI and use this library to handle the logic.
3+
This is a lite version of the main Freemius SDK, specifically developed for use in Duck Dev WordPress plugins. This
4+
library focuses exclusively on managing plugin license activation, deactivation, and updates. It does not provide any
5+
user interface, so your plugin will need to create its own UI and use this library to handle the logic.
46

57
## Requirements
68

79
* PHP version 7.4 or higher.
810
* WordPress 5.0+
911

10-
1112
## Installation
1213

1314
This library should be installed and included in your WordPress plugin using Composer.
@@ -18,27 +19,29 @@ composer require duckdev/freemius-licensing-sdk
1819

1920
## Usage
2021

21-
2222
### Initialization
2323

24-
Initialize the Freemius SDK by calling the static `DuckDev\Freemius\Freemius::get_instance()` method with your plugin's details.
24+
Initialize the Freemius SDK by calling the static `DuckDev\Freemius\Freemius::get_instance()` method with your plugin's
25+
details.
2526

2627
```php
2728
// Assuming Composer's autoload.php has been included.
2829
$freemius = DuckDev\Freemius\Freemius::get_instance(
29-
12345, // Your Freemius product ID.
30-
'test-addon', // Your plugin's unique Freemius slug.
31-
__FILE__ // The path to your plugin's main file.
30+
12345, // Your Freemius product ID.
31+
array(
32+
'slug' => 'loggedin', // Your plugin's unique Freemius slug.
33+
'main_file' => LOGGEDIN_FILE, // The path to your plugin's main file.
34+
'public_key' => 'pk_XXXXXXXXXXXXXXXXX', // Your plugin's public key.
35+
)
3236
);
33-
3437
```
3538

3639
### License Activation
3740

3841
To activate a license, call the `activate()` method on the `license()` object with the user's license key.
3942

4043
```php
41-
$freemius->license()->activate( 'YOUR-LICENSE-KEY' );
44+
$freemius->license()->activate( 'XXXX-XXXX-XXXX' );
4245
```
4346

4447
### License Deactivation
@@ -51,4 +54,5 @@ $freemius->license()->deactivate();
5154

5255
### Updates
5356

54-
The library will automatically handle plugin updates as long as a valid license is active. No additional code is required to check for and apply updates.
57+
The library will automatically handle plugin updates as long as a valid license is active. No additional code is
58+
required to check for and apply updates.

src/data/class-plugin.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,30 @@ class Plugin {
4545
*/
4646
private string $main_file;
4747

48+
/**
49+
* Plugin public key.
50+
*
51+
* @since 1.0.0
52+
*
53+
* @var string
54+
*/
55+
private string $public_key = '';
56+
4857
/**
4958
* Plugin class constructor.
5059
*
5160
* @since 1.0.0
5261
*
53-
* @param int $id Plugin ID.
54-
* @param string $slug Plugin slug.
55-
* @param string $main_file Plugin main file.
62+
* @param int $id Plugin ID.
63+
* @param array $args Arguments.
5664
*
5765
* @return void
5866
*/
59-
public function __construct( int $id, string $slug, string $main_file ) {
60-
$this->id = $id;
61-
$this->slug = $slug;
62-
$this->main_file = $main_file;
67+
public function __construct( int $id, array $args ) {
68+
$this->id = $id;
69+
$this->slug = $args['slug'] ?? '';
70+
$this->main_file = $args['main_file'] ?? '';
71+
$this->public_key = $args['public_key'] ?? '';
6372
}
6473

6574
/**
@@ -94,4 +103,15 @@ public function get_slug(): string {
94103
public function get_main_file(): string {
95104
return $this->main_file;
96105
}
106+
107+
/**
108+
* Gets the plugin public key.
109+
*
110+
* @since 1.0.0
111+
*
112+
* @return string
113+
*/
114+
public function get_public_key(): string {
115+
return $this->public_key;
116+
}
97117
}

src/freemius.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
defined( 'WPINC' ) || die;
1515

1616
use DuckDev\Freemius\Data\Plugin;
17+
use DuckDev\Freemius\Services\Addon;
1718
use DuckDev\Freemius\Services\License;
1819
use DuckDev\Freemius\Services\Update;
1920

@@ -40,45 +41,62 @@ class Freemius {
4041
*/
4142
private Update $update;
4243

44+
/**
45+
* Addons manager service instance.
46+
*
47+
* @since 1.0.0
48+
*
49+
* @var Addon
50+
*/
51+
private Addon $addon;
52+
4353
/**
4454
* Class constructor.
4555
*
4656
* @since 1.0.0
4757
*
48-
* @param int $id Plugin ID.
49-
* @param string $slug Plugin slug.
50-
* @param string $file Plugin file.
51-
* @param array $args Arguments.
58+
* @param int $id Plugin ID.
59+
* @param array $args Arguments.
5260
*/
53-
protected function __construct( int $id, string $slug, string $file, array $args = array() ) {
54-
$plugin = new Plugin( $id, $slug, $file );
61+
protected function __construct( int $id, array $args ) {
62+
$plugin = new Plugin( $id, $args );
5563
$this->license = new License( $plugin );
5664
$this->update = new Update( $plugin );
65+
$this->addon = new Addon( $plugin );
5766
}
5867

5968
/**
6069
* Get the singleton instance of the class.
6170
*
6271
* @since 1.0.0
6372
*
64-
* @param int $id Plugin ID.
65-
* @param string $slug Plugin slug.
66-
* @param string $file Plugin file.
67-
* @param array $args Arguments.
73+
* @param int $id Plugin ID.
74+
* @param array $args Arguments.
6875
*
6976
* @return Freemius
7077
*/
71-
public static function get_instance( int $id, string $slug, string $file, array $args = array() ): Freemius {
78+
public static function get_instance( int $id, array $args ): Freemius {
7279
static $instances = array();
7380

7481
// Create new instance only if doesn't exist.
7582
if ( ! isset( $instances[ $id ] ) || ! $instances[ $id ] instanceof Freemius ) {
76-
$instances[ $id ] = new self( $id, $slug, $file, $args );
83+
$instances[ $id ] = new self( $id, $args );
7784
}
7885

7986
return $instances[ $id ];
8087
}
8188

89+
/**
90+
* Get the addons manager service instance.
91+
*
92+
* @since 1.0.0
93+
*
94+
* @return Addon
95+
*/
96+
public function addon(): Addon {
97+
return $this->addon;
98+
}
99+
82100
/**
83101
* Get the license manager service instance.
84102
*

src/services/class-addon.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* This class handles addons.
4+
*
5+
* @license http://www.gnu.org/licenses/ GNU General Public License
6+
* @author Joel James <[email protected]>
7+
* @since 1.0.0
8+
* @package Freemius
9+
* @subpackage Services
10+
*/
11+
12+
namespace DuckDev\Freemius\Services;
13+
14+
// If this file is called directly, abort.
15+
defined( 'WPINC' ) || die;
16+
17+
use DuckDev\Freemius\Api\Api;
18+
use WP_Error;
19+
20+
/**
21+
* Class Addon
22+
*/
23+
class Addon extends Service {
24+
25+
/**
26+
* Get the list of addons.
27+
*
28+
* @since 1.0.0
29+
*
30+
* @return array
31+
*/
32+
public function get_addons(): array {
33+
// Get from cache first.
34+
$addons = $this->get_transient( 'addons' );
35+
if ( ! empty( $addons ) ) {
36+
return $addons;
37+
}
38+
39+
// Get from the API.
40+
$addons = $this->get_remote_addons();
41+
42+
if ( ! is_wp_error( $addons ) ) {
43+
// Format the data.
44+
$addons = array_map( array( $this, 'format_addon_data' ), $addons );
45+
// Save to cache.
46+
$this->set_transient( 'addons', $addons );
47+
48+
return $addons;
49+
}
50+
51+
return array();
52+
}
53+
54+
/**
55+
* Get the list of addons from the API.
56+
*
57+
* @since 1.0.0
58+
*
59+
* @return array|string|WP_Error
60+
*/
61+
protected function get_remote_addons() {
62+
// Get authenticated API instance using public key.
63+
$api = Api::get_auth_instance(
64+
$this->plugin->get_id(),
65+
$this->plugin->get_public_key(),
66+
$this->plugin->get_public_key(), // Use public key again for secret key to use public key encryption.
67+
'plugin'
68+
);
69+
70+
// Addon list from API.
71+
$response = $api->get(
72+
'addons.json',
73+
array(
74+
'enriched' => true, // Get addon info.
75+
'show_pending' => false, // Get only released addons.
76+
)
77+
);
78+
79+
if ( is_wp_error( $response ) ) {
80+
return $response;
81+
}
82+
83+
return $response['plugins'] ?? array();
84+
}
85+
86+
/**
87+
* Get the list of addons from the API.
88+
*
89+
* @since 1.0.0
90+
*
91+
* @return array|string|WP_Error
92+
*/
93+
protected function format_addon_data( $addon ): array {
94+
// Add checkout link.
95+
$addon['link'] = "https://checkout.freemius.com/plugin/{$addon['id']}";
96+
// Premium if pricing is visible.
97+
$addon['is_premium'] = $addon['is_pricing_visible'] ?? false;
98+
99+
return $addon;
100+
}
101+
}

src/services/class-license.php

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,6 @@ public function get_license_key(): string {
4141
return '';
4242
}
4343

44-
/**
45-
* Get currently active plan name.
46-
*
47-
* @since 1.0.0
48-
*
49-
* @return string
50-
*/
51-
public function get_plan_name(): string {
52-
if ( $this->is_activated() ) {
53-
$activation = $this->get_activation_data();
54-
if ( ! empty( $activation['install_data']['license_plan_name'] ) ) {
55-
return $activation['install_data']['license_plan_name'];
56-
}
57-
}
58-
59-
return '';
60-
}
61-
6244
/**
6345
* Activates the license key for the site.
6446
*
@@ -163,49 +145,6 @@ public function deactivate() {
163145
return new WP_Error( 'unknown_error', __( 'Unknown error.', 'duckdev-freemius' ) );
164146
}
165147

166-
/**
167-
* Sync install with remote.
168-
*
169-
* @since 1.0.0
170-
*
171-
* @return array|WP_Error
172-
*/
173-
public function sync_install() {
174-
if ( $this->is_activated() ) {
175-
$activation = $this->get_activation_data();
176-
if ( empty( $activation['activation_params']['license_key'] ) ) {
177-
return new WP_Error( 'empty_license', __( 'Invalid or empty license key.', 'duckdev-freemius' ) );
178-
}
179-
180-
// Deactivate the license.
181-
$deactivate = $this->deactivate();
182-
if ( is_wp_error( $deactivate ) ) {
183-
return $deactivate;
184-
}
185-
186-
// Attempt to re-activate the license.
187-
return $this->activate( $activation['activation_params']['license_key'] );
188-
}
189-
190-
return new WP_Error( 'not_active', __( 'License not active.', 'duckdev-freemius' ) );
191-
}
192-
193-
/**
194-
* Check if the current license is for a specific plan.
195-
*
196-
* @since 1.0.0
197-
*
198-
* @param string $plan Plan name.
199-
* @param bool $matching Should match.
200-
*
201-
* @return bool
202-
*/
203-
public function is_plan( string $plan, bool $matching = true ): bool {
204-
$is_match = $this->get_plan_name() === $plan;
205-
206-
return $matching ? $is_match : ! $is_match;
207-
}
208-
209148
/**
210149
* Check if current license can be deactivated.
211150
*
@@ -215,7 +154,7 @@ public function is_plan( string $plan, bool $matching = true ): bool {
215154
*
216155
* @return bool
217156
*/
218-
private function can_deactivate( array $activation ): bool {
157+
protected function can_deactivate( array $activation ): bool {
219158
// We need activation data.
220159
if ( empty( $activation ) ) {
221160
return false;
@@ -241,7 +180,7 @@ private function can_deactivate( array $activation ): bool {
241180
*
242181
* @return string
243182
*/
244-
private function get_current_site_uid(): string {
183+
protected function get_current_site_uid(): string {
245184
$blog_id = get_current_blog_id();
246185
$site_url = get_site_url( $blog_id );
247186
$site_url_parts = parse_url( $site_url );

0 commit comments

Comments
 (0)