Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple product select logic #2

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,27 @@ define("CODECANYON_API_KEY", "UNr.....");
define("CODECANYON_SLUG_PLUGIN", "my-plugin");
```

## Adding multiple products

Add suffixes to the constants

```
define("FS__PLUGIN_ID_PRODUCT_1", "1234");
define("FS__PLUGIN_PK_APIKEY_PRODUCT_1", "pk_abcde...");
define("FS__PLUGIN_SK_APIKEY_PRODUCT_1", "sk_abcde...");
define("FS__PLUGIN_PLAN_ID_PRODUCT_1", "5678");
define("FS__PLUGIN_PRICING_ID_PRODUCT_1", "90123");
define("CODECANYON_SLUG_PLUGIN_PRODUCT_1", "my-plugin");
```

Modify `get_constants_by_product` function with your product slugs and add more products if needed

Edit the `render_shortcode` function with your product slugs and names

## Adding the license migration form

Create a new page with and with the following shortcode: `[ctf_form]`

## Contribution

This plugin was originally contributed by the awesome team of [NextPress](https://nextpress.co) & [WP Ultimo](https://wpultimo.com/), Arindo Duque and Marcelo Assis, who've built it for migrating WP Ultimo from Gumroad to Freemius.
This plugin was originally contributed by the awesome team of [NextPress](https://nextpress.co) & [WP Ultimo](https://wpultimo.com/), Arindo Duque and Marcelo Assis, who've built it for migrating WP Ultimo from Gumroad to Freemius.
60 changes: 46 additions & 14 deletions ctf-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Codecanyon_To_Freemius {
*/
public $version = '0.0.1';

public $debug = false;

/**
* WP_Error messages
*
Expand Down Expand Up @@ -205,6 +207,11 @@ public function render_shortcode($atts) {
'id' => '1',
'form_email_label' => __('Enter your email address', 'np-ctf'),
'form_license_label' => __('Enter your license key from CodeCanyon', 'np-ctf'),
'form_product_label' => __('Select a product', 'np-ctf'),
'form_products' => array(
'dinery' => __('Dinery', 'np-ctf'),
'margin' => __('Margin', 'np-ctf')
),
'form_button_label' => __('Convert License', 'np-ctf'),
'form_success_message' => __('Confirmation email was successfully sent with your new access credentials and license key!', 'np-ctf'),
), $atts, 'ctf_form');
Expand Down Expand Up @@ -291,7 +298,7 @@ public function validation($data) {
*/
public function is_debug_mode() {

return isset($_GET['debug']) && $_GET['debug'] === 'SECRET!!!!';
return $this->debug;

} // end is_debug_mode;

Expand Down Expand Up @@ -322,28 +329,45 @@ public function is_license_key_already_used($license_key) {
* Get ctf constants defined in wp-config.php
*
* @since 0.0.1
*
* @param string $product Product.
*
* @return array
*/
public function get_constants_by_id() {
public function get_constants_by_product($product) {

return array(
$defaults = array(
'freemius_dev_pk_apikey' => defined("FS__DEV_PK_APIKEY") ? constant("FS__DEV_PK_APIKEY") : '',
'freemius_dev_sk_apikey' => defined("FS__DEV_SK_APIKEY") ? constant("FS__DEV_SK_APIKEY") : '',
'freemius_dev_id' => defined("FS__DEV_ID") ? constant("FS__DEV_ID") : '',

'freemius_plugin_pk_apikey' => defined("FS__PLUGIN_PK_APIKEY") ? constant("FS__PLUGIN_PK_APIKEY") : '',
'freemius_plugin_sk_apikey' => defined("FS__PLUGIN_SK_APIKEY") ? constant("FS__PLUGIN_SK_APIKEY") : '',
'freemius_plugin_id' => defined("FS__PLUGIN_ID") ? constant("FS__PLUGIN_ID") : '',
'freemius_plugin_plan_id' => defined("FS__PLUGIN_PLAN_ID") ? constant("FS__PLUGIN_PLAN_ID") : '',
'freemius_plugin_pricing_id' => defined("FS__PLUGIN_PRICING_ID") ? constant("FS__PLUGIN_PRICING_ID") : '',
'freemius_plugin_expires_at' => defined("FS__PLUGIN_EXPIRES_AT") ? constant("FS__PLUGIN_EXPIRES_AT") : '',

'codecanyon_api_key' => defined("CODECANYON_API_KEY") ? constant("CODECANYON_API_KEY") : '',
'codecanyon_slug_plugin' => defined("CODECANYON_SLUG_PLUGIN") ? constant("CODECANYON_SLUG_PLUGIN") : '',
);

} // end get_constants_by_id;
switch ($product) {
case 'dinery':
return array_merge( $defaults, array(
'freemius_plugin_id' => defined("FS__PLUGIN_ID_PRODUCT_1") ? constant("FS__PLUGIN_ID_PRODUCT_1") : '',
'freemius_plugin_pk_apikey' => defined("FS__PLUGIN_PK_APIKEY_PRODUCT_1") ? constant("FS__PLUGIN_PK_APIKEY_PRODUCT_1") : '',
'freemius_plugin_sk_apikey' => defined("FS__PLUGIN_SK_APIKEY_PRODUCT_1") ? constant("FS__PLUGIN_SK_APIKEY_PRODUCT_1") : '',
'freemius_plugin_plan_id' => defined("FS__PLUGIN_PLAN_ID_PRODUCT_1") ? constant("FS__PLUGIN_PLAN_ID_PRODUCT_1") : '',
'freemius_plugin_pricing_id' => defined("FS__PLUGIN_PRICING_ID_PRODUCT_1") ? constant("FS__PLUGIN_PRICING_ID_PRODUCT_1") : '',
'codecanyon_slug_plugin' => defined("CODECANYON_SLUG_PLUGIN_PRODUCT_1") ? constant("CODECANYON_SLUG_PLUGIN_PRODUCT_1") : '',
) );
case 'margin':
return array_merge( $defaults, array(
'freemius_plugin_id' => defined("FS__PLUGIN_ID_PRODUCT_2") ? constant("FS__PLUGIN_ID_PRODUCT_2") : '',
'freemius_plugin_pk_apikey' => defined("FS__PLUGIN_PK_APIKEY_PRODUCT_2") ? constant("FS__PLUGIN_PK_APIKEY_PRODUCT_2") : '',
'freemius_plugin_sk_apikey' => defined("FS__PLUGIN_SK_APIKEY_PRODUCT_2") ? constant("FS__PLUGIN_SK_APIKEY_PRODUCT_2") : '',
'freemius_plugin_plan_id' => defined("FS__PLUGIN_PLAN_ID_PRODUCT_2") ? constant("FS__PLUGIN_PLAN_ID_PRODUCT_2") : '',
'freemius_plugin_pricing_id' => defined("FS__PLUGIN_PRICING_ID_PRODUCT_2") ? constant("FS__PLUGIN_PRICING_ID_PRODUCT_2") : '',
'codecanyon_slug_plugin' => defined("CODECANYON_SLUG_PLUGIN_PRODUCT_2") ? constant("CODECANYON_SLUG_PLUGIN_PRODUCT_2") : '',
) );
default:
return $defaults; // or throw an error
}

} // end get_constants_by_product;

/**
* Handle of form submit
Expand All @@ -365,9 +389,10 @@ public function handle_form_submission() {
$data = array(
'license_key' => $_POST['license_key'],
'email' => $_POST['email'],
'product' => $_POST['product'],
);

$data = array_merge($data, $this->get_constants_by_id());
$data = array_merge($data, $this->get_constants_by_product($data['product']));

if (!$this->validation($data)) {
return;
Expand All @@ -379,7 +404,6 @@ public function handle_form_submission() {

if ($this->is_debug_mode()) {


} // end if;

if (!$license || $license->success == false) {
Expand All @@ -390,6 +414,14 @@ public function handle_form_submission() {

} // end if;


// Check if the purchase code matches the selected product
if ( strtolower($license->product) !== $data['product'] ) {
$this->messages->add('invalid-product', __('The product does not match your purchase code.', 'np-ctf'));
CTF_Logger::add('ctf_log_error', implode('<br>', $this->messages->get_error_messages()));
return;
}

$existing_user = $ctf_api->verify_freemius_exists_user($data['email']);

if (!$existing_user) {
Expand Down
1 change: 1 addition & 0 deletions inc/ctf-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public function verify_envato_purchase_code($code) {
return (object) array(
'success' => isset($output->buyer),
'golden_ticket' => isset($output->buyer),
'product' => isset($output->item->wordpress_theme_metadata->theme_name) ? $output->item->wordpress_theme_metadata->theme_name : '',
'purchase' => (object) array(
'refunded' => false,
)
Expand Down
12 changes: 12 additions & 0 deletions views/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
</label>
</p>

<p>
<label for="np-ctf-panel-product">
<?php echo $atts['form_product_label']; ?>:
<select id="np-ctf-panel-product" class="form-control form-control-lg" name="product" required>
<option value=""><?php echo $atts['form_product_label']; ?></option>
<?php foreach ( $atts['form_products'] as $slug => $product ) : ?>
<option value="<?php echo $slug; ?>"><?php echo $product; ?></option>
<?php endforeach; ?>
</select>
</label>
</p>

<p>
<button class="button button-primary btn btn-primary" type="submit">
<?php echo $atts['form_button_label']; ?>
Expand Down