| |
| <?php
|
|
|
|
|
|
|
| function eddrll_add_metabox() {
|
| $post_types = apply_filters( 'edd_download_metabox_post_types', array( 'download' ) );
|
| foreach ( $post_types as $post_type ) {
|
| add_meta_box(
|
| 'edd_retroactive_lifetime_licenses',
|
| __( 'Retroactive Lifetime Licenses', 'edd-retroactive-lifetime-licenses' ),
|
| 'eddrll_render_metabox',
|
| $post_type,
|
| 'advanced',
|
| 'low'
|
| );
|
| }
|
| }
|
| add_action('add_meta_boxes', 'eddrll_add_metabox');
|
|
|
|
|
|
|
| function eddrll_render_metabox() {
|
| global $post;
|
| ?>
|
| <button href="#" id="eddrll" data-id="<?php echo absint($post->ID); ?>" class="button">
|
| <?php esc_html_e('Grant lifetime license to all existing licenses for this download', 'edd-retroactive-lifetime-licenses'); ?>
|
| </button>
|
| <?php delete_option('eddrll_status_' . $post->ID); ?>
|
| <script type="text/javascript">
|
| jQuery(document).ready(function ($) {
|
| eddrll = {
|
| nonce: '<?php echo esc_js(wp_create_nonce('eddrll')); ?>',
|
| };
|
| $('#eddrll').click(function (e) {
|
| e.preventDefault();
|
| e.stopPropagation();
|
| var id = $(this).data('id');
|
| if (!id) {
|
| return;
|
| }
|
| $(this).attr('disabled', 'disabled');
|
| var modified_licenses = 0;
|
| $(this).after('<div id="eddrll-progress">Processing...</div>');
|
| eddrll_post(id, modified_licenses);
|
| });
|
| var eddrll_post = function (id, modified_licenses) {
|
| var counter = counter || 0;
|
| counter++;
|
| $.post(
|
| ajaxurl,
|
| {
|
| action: 'eddrll',
|
| nonce: eddrll.nonce,
|
| download_id: id,
|
| },
|
| function (r) {
|
| if (typeof r === 'undefined' || !r.success) {
|
| $('#eddrll-progress').html('Error');
|
| console.log(r);
|
| }
|
| modified_licenses += r.data.modified_licenses.length;
|
| $('#eddrll-progress').text(modified_licenses.toString() + ' licenses modified');
|
| if (r.data.status === true) {
|
| $('#eddrll-progress').text($('#eddrll-progress').text() + ' (complete)');
|
| return;
|
| }
|
| eddrll_post(id, modified_licenses);
|
| }
|
| );
|
| };
|
| });
|
| </script>
|
| <?php
|
| }
|
|
|
|
|
|
|
| function eddrll_process_license_extension() {
|
| if (!check_ajax_referer('eddrll', 'nonce') || !current_user_can('manage_options')) {
|
| wp_send_json_error(
|
| array(
|
| 'error' => 'unauthenticated_request',
|
| )
|
| );
|
| }
|
| $id = empty($_POST['download_id']) ? 0 : absint($_POST['download_id']);
|
| if (empty($id) || get_post_type($id) !== 'download') {
|
| wp_send_json_error(
|
| array(
|
| 'error' => 'no download found',
|
| )
|
| );
|
| }
|
| $status = get_option('eddrll_status_' . $id, 1);
|
| if ($status === 'complete') {
|
| wp_send_json_success(
|
| array(
|
| 'status' => true,
|
| 'modified_licenses' => array(),
|
| )
|
| );
|
| }
|
| $batch_size = 500;
|
| $status = (int)$status;
|
| global $wpdb;
|
| $query = "SELECT ID FROM {$wpdb->prefix}edd_licenses WHERE download_id = %d LIMIT %d OFFSET %d";
|
| $licenses = $wpdb->get_results($wpdb->prepare($query, $id, $batch_size, ($status - 1) * $batch_size), ARRAY_A);
|
| if (empty($licenses)) {
|
| update_option('eddrll_status_' . $id, 'complete');
|
| wp_send_json_success(
|
| array(
|
| 'status' => true,
|
| 'modified_licenses' => array(),
|
| )
|
| );
|
| }
|
| $modified_licenses = array();
|
| foreach ($licenses as $license) {
|
| delete_post_meta($license['ID'], '_edd_sl_expiration');
|
| update_post_meta($license['ID'], '_edd_sl_is_lifetime', 1);
|
| $modified_licenses[] = $license['ID'];
|
| }
|
| $status++;
|
| update_option('eddrll_status_' . $id, $status);
|
| wp_send_json_success(
|
| array(
|
| 'status' => $status,
|
| 'modified_licenses' => $modified_licenses,
|
| )
|
| );
|
| }
|
| add_action('wp_ajax_eddrll', 'eddrll_process_license_extension');
|
| |
| |
Comments