Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
danielmilner committed Aug 14, 2020
1 parent c61b403 commit 9ec3928
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 71 deletions.
126 changes: 65 additions & 61 deletions core/EDD_License_Field.php → core/class-edd-license-field.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<?php
/**
* Carbon Fields Easy Digital Downloads License Field
*
* @package edd-license-field
*/

namespace Carbon_Field_EDD_License;

use Carbon_Fields\Field\Field;
use EDD_SL_Plugin_Updater;
use CF_EDD_SL_Plugin_Updater;

/**
* Carbon Fields Easy Digital Downloads License Field
*/
class EDD_License_Field extends Field {

/**
Expand All @@ -16,7 +24,7 @@ class EDD_License_Field extends Field {

/**
* Item ID
*
*
* @var int
*/
protected $item_id;
Expand Down Expand Up @@ -95,7 +103,7 @@ public static function admin_enqueue_scripts() {
'edd_license',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'edd-license' )
'nonce' => wp_create_nonce( 'edd-license' ),
)
);
}
Expand All @@ -107,7 +115,7 @@ public static function admin_enqueue_scripts() {
* @return array
*/
public function to_json( $load ) {
$status = get_option( "{$this->name}_status" );
$status = get_option( "{$this->name}_status" );
$field_data = parent::to_json( $load );
$field_data = array_merge(
$field_data,
Expand All @@ -122,57 +130,40 @@ public function to_json( $load ) {
'license_status' => $this->get_license_status( $status ),
'nonce' => wp_create_nonce( "{$this->name}_nonce" ),
'nonce_name' => "{$this->name}_nonce",
'date_format' => get_option('date_format'),
'date_format' => get_option( 'date_format' ),
'license' => $this->get_license_key(),
)
);
return $field_data;
}

public function init() {
add_action( "wp_ajax_{$this->name}_activate", array( $this, 'activate_license' ) );
add_action( "wp_ajax_{$this->name}_deactivate", array( $this, 'deactivate_license' ) );

// if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
// include realpath( __DIR__ ) . '../lib/EDD_SL_Plugin_Updater.php';
// }

// new EDD_SL_Plugin_Updater(
// $this->store_url,
// $this->plugin_file,
// array(
// 'license' => $this->get_license_key(),
// 'version' => $this->version, // Current version number.
// 'item_id' => $this->item_id, // ID of the product.
// 'author' => $this->author, // Author of the product.
// 'beta' => $this->beta, // Receive beta updates.
// )
// );
}

/**
* Admin init
* Init
*
* @return void
*/
public function admin_init() {

// if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
// include realpath( __DIR__ ) . '../lib/EDD_SL_Plugin_Updater.php';
// }

// new EDD_SL_Plugin_Updater(
// $this->store_url,
// $this->plugin_file,
// array(
// 'license' => $this->get_license_key(),
// 'version' => $this->version, // Current version number.
// 'item_id' => $this->item_id, // ID of the product.
// 'author' => $this->author, // Author of the product.
// 'beta' => $this->beta, // Receive beta updates.
// )
// );
public function init() {
add_action( "wp_ajax_{$this->name}_activate", array( $this, 'activate_license' ) );
add_action( "wp_ajax_{$this->name}_deactivate", array( $this, 'deactivate_license' ) );

/*
Comment
if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
include realpath( __DIR__ ) . '../lib/EDD_SL_Plugin_Updater.php';
}
new EDD_SL_Plugin_Updater(
$this->store_url,
$this->plugin_file,
array(
'license' => $this->get_license_key(),
'version' => $this->version, // Current version number.
'item_id' => $this->item_id, // ID of the product.
'author' => $this->author, // Author of the product.
'beta' => $this->beta, // Receive beta updates.
)
);
*/
}

/**
Expand Down Expand Up @@ -284,6 +275,7 @@ public function activate_license() {
switch ( $license_data->error ) {
case 'expired':
$message = sprintf(
// translators: the placeholder is for a date.
__( 'Your license key expired on %s.' ),
date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) )
);
Expand Down Expand Up @@ -333,7 +325,7 @@ public function activate_license() {

wp_send_json(
array(
'error' => $this->error_message,
'error' => $this->error_message,
'status' => $license_status,
)
);
Expand Down Expand Up @@ -398,7 +390,7 @@ public function deactivate_license() {

wp_send_json(
array(
'error' => $this->error_message,
'error' => $this->error_message,
'status' => $license_status,
)
);
Expand All @@ -411,13 +403,17 @@ private function get_license_key() {
switch ( $this->context ) {
case 'theme_options':
return \carbon_get_theme_option( $this->base_name );
break;
default:
return '';
break;
}
}

/**
* Get the license status in text format
*
* @param object $license_data License data.
* @return string
*/
private function get_license_status( $license_data ) {
$license_status = '';

Expand All @@ -434,14 +430,14 @@ private function get_license_status( $license_data ) {
isset( $license_data->license ) &&
'valid' === $license_data->license &&
isset( $license_data->expires )
){
) {
if ( 'lifetime' === $license_data->expires ) {
$license_status = __( 'Your license key never expires.' );
} else {
$license_status = sprintf(
"Your license key expires on %s.",
'Your license key expires on %s.',
date(
get_option('date_format'),
get_option( 'date_format' ),
strtotime( $license_data->expires )
)
);
Expand All @@ -457,13 +453,15 @@ private function get_license_status( $license_data ) {
* @return void
*/
private function save_item_info() {
$licenses = get_option( 'cf_edd_license_data', array() );
$licenses = \get_option( 'cf_edd_license_data' );

if ( ! is_array( $licenses ) ) {
$licenses = wp_json_decode( $licenses );
if ( ! $licenses ) {
$licenses = new stdClass();
} else {
$licenses = \json_decode( $licenses );
}

$licenses[ $this->name ] = array(
$licenses->{$this->name } = array(
'store_url' => $this->store_url,
'plugin_file' => $this->plugin_file,
'license' => $this->get_license_key(),
Expand All @@ -473,7 +471,7 @@ private function save_item_info() {
'beta' => $this->beta,
);

update_option( 'cf_edd_license_data', wp_json_encode( $licenses ) );
\update_option( 'cf_edd_license_data', \wp_json_encode( $licenses ) );
}

/**
Expand All @@ -482,14 +480,20 @@ private function save_item_info() {
* @return void
*/
private function delete_item_info() {
$licenses = get_option( 'cf_edd_license_data', array() );
$licenses = \get_option( 'cf_edd_license_data' );

if ( ! $licenses ) {
$licenses = new stdClass();
} else {
$licenses = \json_decode( $licenses );
}

if ( ! is_array( $licenses ) ) {
$licenses = wp_json_decode( $licenses );
if ( ! isset( $licenses->{ $this->name } ) ) {
return;
}

unset( $licenses[ $this->name ] );
unset( $licenses->{ $this->name } );

update_option( 'cf_edd_license_data', wp_json_encode( $licenses ) );
\update_option( 'cf_edd_license_data', \wp_json_encode( $licenses ) );
}
}
62 changes: 53 additions & 9 deletions field.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,64 @@
* @package Carbon_Fields_EDD_License
*/

namespace CF_EDD_License_Field;

use Carbon_Fields\Carbon_Fields;
use Carbon_Field_EDD_License\EDD_License_Field;
use CF_EDD_SL_Plugin_Updater;

define( 'Carbon_Field_EDD_License\\DIR', __DIR__ );

require_once __DIR__ . '/core/EDD_License_Field.php';
/**
* Register the field with Carbon Fields
*
* @return void
*/
function register_field() {
require_once __DIR__ . '/core/class-edd-license-field.php';

Carbon_Fields::extend(
EDD_License_Field::class,
function( $container ) {
return new EDD_License_Field(
$container['arguments']['type'],
$container['arguments']['name'],
$container['arguments']['label']
);
}
);
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\\register_field' );

/**
* Register the updater
*
* @return void
*/
function register_updater() {
$license_data = \get_option( 'cf_edd_license_data' );

if ( ! $license_data ) {
return;
}

$license_data = \json_decode( $license_data );

Carbon_Fields::extend(
EDD_License_Field::class,
function( $container ) {
return new EDD_License_Field(
$container['arguments']['type'],
$container['arguments']['name'],
$container['arguments']['label']
foreach ( $license_data as $field => $data ) {
if ( ! \class_exists( 'CF_EDD_SL_Plugin_Updater' ) ) {
include_once \realpath( __DIR__ ) . '/lib/CF_EDD_SL_Plugin_Updater.php';
}
$results = new CF_EDD_SL_Plugin_Updater(
$data->store_url,
$data->plugin_file,
array(
'license' => $data->license,
'version' => $data->version, // Current version number.
'item_id' => $data->item_id, // ID of the product.
'author' => $data->author, // Author of the product.
'beta' => $data->beta, // Receive beta updates.
)
);
}
);
}
add_action( 'admin_init', __NAMESPACE__ . '\\register_updater' );
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Easy Digital Downloads
* @version 1.7.1
*/
class EDD_SL_Plugin_Updater {
class CF_EDD_SL_Plugin_Updater {

private $api_url = '';
private $api_data = array();
Expand Down

0 comments on commit 9ec3928

Please sign in to comment.