Last active
February 18, 2019 14:46
-
-
Save mintplugins/8fc88954dbff7732abe2b86f5681daef to your computer and use it in GitHub Desktop.
This is a simple plugin that can be used to set all child license keys expiration dates to their parent's. This is not for use in production, and is for example purposes only
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Easy Digital Downloads - Fix child licenses | |
Description: Make the child licenses of all licensed bundles match the parent license expiration | |
Plugin URI: https://easydigitaldownlaods.com | |
Author: Phil Johnston | |
Author URI: https://easydigitaldownloads.com | |
Version: 1.0 | |
License: GPL2 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
function set_all_child_key_expirations_to_match_bundle_parent() { | |
global $wpdb; | |
if ( ! isset( $_GET['set_all_child_key_expirations_to_match_bundle_parent'] ) ) { | |
return false; | |
} | |
?> | |
<h1>Setting all child license keys to match their parent license key expiration.</h1> | |
<h2>Do not close this window until the process has completed</h2> | |
<p> | |
<div id="current_status">Starting...</div> | |
</p> | |
<?php | |
$table_name = $wpdb->prefix . 'edd_licenses'; | |
// Query all licenses that have a parent set to get a total count | |
$query = $wpdb->prepare( "SELECT DISTINCT( l1.id ) FROM {$table_name} l1 WHERE 1=1 AND l1.parent !=0 AND l1.status != 'private' ORDER BY l1.id ASC LIMIT %d,%d;", | |
0, | |
0 | |
); | |
$total = count( $query ); | |
// Output the javascript that will fetch to the server to process each chunk of purchases ?> | |
<script type="text/javascript"> | |
function set_all_child_key_expirations_to_match_bundle_parent( step ) { | |
var postData = JSON.stringify({ | |
step: step, | |
total: <?php echo $total; ?> | |
}); | |
fetch( '<?php echo get_bloginfo( 'url' ); ?>/?set_all_child_key_expirations_to_match_bundle_parent_endpoint', { | |
method: "POST", | |
mode: "same-origin", | |
credentials: "same-origin", | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
body: postData | |
} ).then( | |
function( response ) { | |
if ( response.status !== 200 ) { | |
console.log('Looks like there was a problem. Status Code: ' + response.status); | |
return; | |
} | |
// Examine the text in the response | |
response.json().then( | |
function( data ) { | |
if ( data.success ) { | |
// If the steps are complete | |
if ( data.complete ) { | |
document.getElementById("current_status").innerHTML="100% Complete"; | |
} | |
// If the steps are not complete, run the next step. It gets incremented on the server. | |
if ( ! data.complete ) { | |
document.getElementById("current_status").innerHTML="Step " + data.step + " of " + data.total; | |
set_all_child_key_expirations_to_match_bundle_parent( data.step ); | |
} | |
} else { | |
document.getElementById("current_status").innerHTML=data.details; | |
console.log('Looks like there was a problem. Response from server was: ' + data.details ); | |
} | |
} | |
); | |
} | |
).catch( | |
function( err ) { | |
console.log('Fetch Error :-S', err); | |
} | |
); | |
} | |
set_all_child_key_expirations_to_match_bundle_parent( 0 ); | |
</script> | |
<?php | |
die(); | |
} | |
add_action( 'init', 'set_all_child_key_expirations_to_match_bundle_parent' ); | |
function set_all_child_key_expirations_to_match_bundle_parent_endpoint() { | |
global $wpdb; | |
if ( ! isset( $_GET['set_all_child_key_expirations_to_match_bundle_parent_endpoint'] ) ) { | |
return false; | |
} | |
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : ''; | |
if ( $contentType !== "application/json" ) { | |
echo json_encode( array( | |
'success' => false, | |
'details' => 'Request was incorrect.' | |
) ); | |
die(); | |
} | |
//Receive the RAW post data. | |
$content = trim(file_get_contents("php://input")); | |
$decoded = json_decode($content, true); | |
//If json_decode failed, the JSON is invalid. | |
if( ! is_array( $decoded ) ) { | |
echo json_encode( array( | |
'success' => false, | |
'details' => 'Request was incorrect.' | |
) ); | |
die(); | |
} | |
if ( ! isset( $decoded['step'] ) ){ | |
echo json_encode( array( | |
'success' => false, | |
'details' => 'No Step Found' | |
) ); | |
die(); | |
} | |
// Increment the step | |
$step = absint( $decoded['step'] ) + 1; | |
$number_of_posts_per_step = 10; | |
$offset = $step * $number_of_posts_per_step - $number_of_posts_per_step; | |
$total = absint( absint( $decoded['total'] ) / $number_of_posts_per_step ); | |
$table_name = $wpdb->prefix . 'edd_licenses'; | |
// Query 10 licenses that have a parent set | |
$query = $wpdb->prepare( "SELECT DISTINCT( l1.id ) FROM {$table_name} l1 WHERE 1=1 AND l1.parent !=0 AND l1.status != 'private' ORDER BY l1.id ASC LIMIT %d,%d;", | |
$offset, | |
$number_of_posts_per_step | |
); | |
$child_license_ids = $wpdb->get_col( $query, 0 ); | |
if ( empty( $child_license_ids ) ) { | |
echo json_encode( array( | |
'success' => true, | |
'complete' => true, | |
) ); | |
die(); | |
} | |
// loop through each child license | |
foreach( $child_license_ids as $child_license_id ) { | |
$child_license = edd_software_licensing()->get_license( $child_license_id ); | |
// Get the parent license of this child license | |
$parent_license = edd_software_licensing()->get_license( $child_license->parent ); | |
$child_license->expiration = $parent_license->expiration; | |
} | |
echo json_encode( array( | |
'success' => true, | |
'complete' => false, | |
'step' => $step, | |
'total' => $total | |
) ); | |
die(); | |
} | |
add_action( 'init', 'set_all_child_key_expirations_to_match_bundle_parent_endpoint' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment