Skip to content

Commit

Permalink
Merge pull request #5 from Freemius/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
vovafeldman authored Nov 11, 2016
2 parents c5aca75 + 28d1bc6 commit 79b69f3
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 31 deletions.
20 changes: 20 additions & 0 deletions includes/class-fs-entity-mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,26 @@ function get_by_remote( $type, $remote_id ) {
function get_by_remote_entity( FS_Entity $entity ) {
return $this->get_by_remote( $entity->get_type(), $entity->id );
}

/**
* Clear all namespace mapping.
*
* @author Vova Feldman (@svovaf)
* @since 1.0.2
*
* @return false|int
*/
function clear_mapping()
{
global $wpdb;

return $wpdb->delete(
$this->_table_name,
array(
'namespace' => $this->_namespace,
)
);
}
}

/**
Expand Down
6 changes: 5 additions & 1 deletion includes/entities/class-fs-entity-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ class FS_Entity_Map extends FS_Entity {
/**
* @var string
*/
public $type;
public $namespace;
/**
* @var string
*/
public $entity_type;
/**
* @var string
*/
Expand Down
12 changes: 12 additions & 0 deletions includes/migration/class-fs-migration-endpoint-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ protected function init( $namespace ) {
$this->_entity_mapper = FS_Entity_Mapper::instance( $namespace );

add_action( 'wp_ajax_fs_sync_module', array( &$this, '_sync_module_to_freemius_callback' ) );
add_action( 'wp_ajax_fs_clear_mapping', array( &$this, '_clear_mapping_callback' ) );
}

#endregion
Expand Down Expand Up @@ -817,6 +818,17 @@ public function _sync_module_to_freemius_callback() {
}
}

/**
* Clear all mapping.
*
* @author Vova Feldman
* @since 1.0.2
*/
public function _clear_mapping_callback(){
$this->_entity_mapper->clear_mapping();
$this->shoot_json_success();
}

/**
* Automatically sync modules that have matching slugs
* on local environment and on Freemius.
Expand Down
78 changes: 61 additions & 17 deletions includes/migration/edd/class-fs-edd-download-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ function __construct(
$period = 'never';
}

$license_limit = get_post_meta( $download->ID, '_edd_sl_limit', true );

$this->_edd_prices = array(
// Set the EDD price ID as ZERO when the download doesn't have variable prices.
0 => array(
'recurring' => $recurring ? 'yes' : 'no',
'period' => $period,
'license_limit' => 0,
'license_limit' => is_numeric( $license_limit ) ? $license_limit : 0,
'amount' => $download->get_price(),
)
);
Expand Down Expand Up @@ -99,22 +101,64 @@ function __construct(
private function get_billing_cycle( $edd_price ) {
$billing_cycle = 'lifetime';

if ( ! empty( $edd_price['recurring'] ) &&
'yes' === $edd_price['recurring'] &&
! empty( $edd_price['period'] ) &&
'never' !== $edd_price['period']
) {
switch ( $edd_price['period'] ) {
case 'year':
$billing_cycle = 'annual';
break;
case 'month':
$billing_cycle = 'monthly';
break;
default:
// @todo Throw an error when billing cycle is not supported.
$billing_cycle = 'monthly';
break;
if ( class_exists( 'EDD_Recurring' ) ) {
if ( ! empty( $edd_price['recurring'] ) &&
'yes' === $edd_price['recurring'] &&
! empty( $edd_price['period'] ) &&
'never' !== $edd_price['period']
) {
switch ( $edd_price['period'] ) {
case 'year':
$billing_cycle = 'annual';
break;
case 'month':
$billing_cycle = 'monthly';
break;
default:
// @todo Throw an error when billing cycle is not supported.
$billing_cycle = 'monthly';
break;
}
}
} else {
if ( ! empty( $edd_price['is_lifetime'] ) &&
$edd_price['is_lifetime']
) {
$billing_cycle = 'lifetime';
} else {
// Alias.
$download_id = $this->_edd_download->ID;

$is_limited = get_post_meta( $download_id, 'edd_sl_download_lifetime', true );
$is_limited = empty( $is_limited );

if ( $is_limited ) {
$exp_unit = get_post_meta( $download_id, '_edd_sl_exp_unit', true );
$exp_length = get_post_meta( $download_id, '_edd_sl_exp_length', true );

if ( 4 == $exp_length && 'weeks' === $exp_unit ) {
$billing_cycle = 'monthly';
} else if ( 12 == $exp_length && 'months' === $exp_unit ) {
$billing_cycle = 'annual';
} else {
if ( 1 != $exp_length ) {
// @todo Throw an error when billing cycle
}

switch ( $exp_unit ) {
case 'years':
$billing_cycle = 'annual';
break;
case 'months':
$billing_cycle = 'monthly';
break;
default:
// @todo Throw an error when billing cycle is not supported.
$billing_cycle = 'monthly';
break;
}
}
}
}
}

Expand Down
34 changes: 25 additions & 9 deletions samples/module-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ function do_my_edd2fs_license_migration(
* @return bool Is successfully spawned the migration request.
*/
function spawn_my_edd2fs_license_migration( $edd_download_id ) {
global $wp;

#region Make sure only one request handles the migration (prevent race condition)

// Generate unique md5.
Expand All @@ -148,7 +146,7 @@ function spawn_my_edd2fs_license_migration( $edd_download_id ) {
* we only want that one request will succeed writing this
* option to the storage.
*/
if ( fs_add_transient( 'fsm_edd_' . $edd_download_id, $migration_uid ) ) {
if ( fs_add_transient( 'fsm_edd_' . $edd_download_id, $migration_uid, MINUTE_IN_SECONDS ) ) {
$loaded_migration_uid = fs_get_transient( 'fsm_edd_' . $edd_download_id );
}

Expand All @@ -158,7 +156,12 @@ function spawn_my_edd2fs_license_migration( $edd_download_id ) {

#endregion

$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
$host = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI'];
$port = $_SERVER['SERVER_PORT'];
$port = ( ( ! WP_FS__IS_HTTPS && $port == '80' ) || ( WP_FS__IS_HTTPS && $port == '443' ) ) ? '' : ':' . $port;
$current_url = ( WP_FS__IS_HTTPS ? 'https' : 'http' ) . "://{$host}{$port}{$uri}";

$migration_url = add_query_arg(
'fsm_edd_' . $edd_download_id,
$migration_uid,
Expand Down Expand Up @@ -231,17 +234,18 @@ function my_non_blocking_edd2fs_license_migration(
return 'freemius_installed_before';
}

$migration_uid = fs_get_transient( 'fsm_edd_' . $edd_download_id );
$key = 'fsm_edd_' . $edd_download_id;

$in_migration = ( false !== $migration_uid );
$migration_uid = fs_get_transient( $key );
$in_migration = ! empty( $_REQUEST[ $key ] );

if ( ! $is_blocking && ! $in_migration ) {
// Initiate license migration in a non-blocking request.
return spawn_my_edd2fs_license_migration( $edd_download_id );
} else {
if ( $is_blocking ||
( ! empty( $_REQUEST[ 'fsm_edd_' . $edd_download_id ] ) &&
$migration_uid === $_REQUEST[ 'fsm_edd_' . $edd_download_id ] &&
( ! empty( $_REQUEST[ $key] ) &&
$migration_uid === $_REQUEST[ $key ] &&
'POST' === $_SERVER['REQUEST_METHOD'] )
) {
$success = do_my_edd2fs_license_migration(
Expand Down Expand Up @@ -409,7 +413,9 @@ function fs_add_transient( $transient, $value, $expiration = 0 ) {
$transient_option = '_fs_transient_' . $transient;
$transient_timeout = '_fs_transient_timeout_' . $transient;

if ( false === get_option( $transient_option ) ) {
$current_value = fs_get_transient($transient);

if ( false === $current_value ) {
$autoload = 'yes';
if ( $expiration ) {
$autoload = 'no';
Expand All @@ -418,6 +424,16 @@ function fs_add_transient( $transient, $value, $expiration = 0 ) {

return add_option( $transient_option, $value, '', $autoload );
}
else
{
// If expiration is requested, but the transient has no timeout option,
// delete, then re-create the timeout.
if ( $expiration ) {
if ( false === get_option( $transient_timeout ) ) {
add_option( $transient_timeout, time() + $expiration, '', 'no' );
}
}
}

return false;
}
Expand Down
34 changes: 30 additions & 4 deletions templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
}
}

$local_modules = array_merge($synced_local_modules, $not_synced_local_modules);
$local_modules = array_merge( $synced_local_modules, $not_synced_local_modules );
?>

<?php foreach ( $local_modules as $local_module ) : ?>
Expand All @@ -83,19 +83,23 @@ class="<?php echo $is_synced ? 'fs--synced' : '' ?>">
echo ( false !== $remote_plan_id ) ? $remote_plan_id : '';
?></td>
<td>
<button class="button"><?php _efs( 'Resync' ) ?></button>
<button class="button"><?php _efs( 'Resync', 'freemius' ) ?></button>
</td>
<?php else : ?>
<td class="fs--module-id"></td>
<td class="fs--paid-plan-id"></td>
<td style="text-align: right">
<button class="button button-primary"><?php _efs( 'Sync to Freemius' ) ?></button>
<button class="button button-primary"><?php _efs( 'Sync to Freemius', 'freemius' ) ?></button>
</td>
<?php endif ?>
</tr>
<?php endforeach ?>
</tbody>
</table>

<br>

<button id="clear_mapping" class="button"><?php _efs( 'Clear Mapping Data' ) ?></button>
<?php endif ?>
<?php if ( ! $is_connected ) : ?>
<p><?php printf(
Expand Down Expand Up @@ -208,7 +212,6 @@ class="<?php echo $is_synced ? 'fs--synced' : '' ?>">
}, function (result) {
if (result.success) {
$container.addClass('fs--synced');
$container.removeClass('fs--syncing');
$moduleID.html(result.data.module_id);
$paidPlanID.html(result.data.plan_id);

Expand All @@ -220,10 +223,33 @@ class="<?php echo $is_synced ? 'fs--synced' : '' ?>">
// Recover button's label.
$this.html('<?php _efs( 'Re-sync' ) ?>');
$this.prop('disabled', false);
$container.removeClass('fs--syncing');
});

return false;
});

$('#clear_mapping').click(function () {
if (confirm("<?php _e('Are you sure you\'d like to clear all mapping data?', 'freemius') ?>")) {
$.post(ajaxurl, {
action: 'fs_clear_mapping'
}, function (result) {
if (result.success) {
$('.fs--synced').each(function () {
var $this = $(this);
$this.removeClass('fs--syncing fs--synced');
$this.find('.fs--module-id').html('');
$this.find('.fs--paid-plan-id').html('');
$this.find('.button').removeClass('button-primary').html('<?php __fs( 'Sync to Freemius', 'freemius' ) ?>')
});

alert('<?php _e('All mapping data was successfully deleted.', 'freemius') ?>');
} else {
alert('<?php _e('Oops... Something went wrong, please try again in few min.', 'freemius' ) ?>');
}
});
}
});
})
(jQuery);
</script>

0 comments on commit 79b69f3

Please sign in to comment.