예제 #1
0
function edd_sl_install()
{
    $current_version = get_option('edd_sl_version');
    if (!$current_version) {
        require_once EDD_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';
        // When new upgrade routines are added, mark them as complete on fresh install
        $upgrade_routines = array('sl_add_bundle_licenses');
        foreach ($upgrade_routines as $upgrade) {
            edd_set_upgrade_complete($upgrade);
        }
    }
    add_option('edd_sl_version', EDD_SL_VERSION, '', false);
}
/**
 * Remove sale logs from refunded orders
 *
 * @since  2.4.3
 * @return void
 */
function edd_remove_refunded_sale_logs()
{
    global $wpdb, $edd_logs;
    if (!current_user_can('manage_shop_settings')) {
        wp_die(__('You do not have permission to do shop upgrades', 'edd'), __('Error', 'edd'), array('response' => 403));
    }
    ignore_user_abort(true);
    if (!edd_is_func_disabled('set_time_limit') && !ini_get('safe_mode')) {
        @set_time_limit(0);
    }
    $step = isset($_GET['step']) ? absint($_GET['step']) : 1;
    $total = isset($_GET['total']) ? absint($_GET['total']) : edd_count_payments()->refunded;
    $refunds = edd_get_payments(array('status' => 'refunded', 'number' => 20, 'page' => $step));
    if (!empty($refunds)) {
        // Refunded Payments found so process them
        foreach ($refunds as $refund) {
            if ('refunded' !== $refund->post_status) {
                continue;
                // Just to be safe
            }
            // Remove related sale log entries
            $edd_logs->delete_logs(null, 'sale', array(array('key' => '_edd_log_payment_id', 'value' => $refund->ID)));
        }
        $step++;
        $redirect = add_query_arg(array('page' => 'edd-upgrades', 'edd-upgrade' => 'remove_refunded_sale_logs', 'step' => $step, 'total' => $total), admin_url('index.php'));
        wp_redirect($redirect);
        exit;
    } else {
        // No more refunded payments found, finish up
        update_option('edd_version', preg_replace('/[^0-9.].*/', '', EDD_VERSION));
        edd_set_upgrade_complete('remove_refunded_sale_logs');
        delete_option('edd_doing_upgrade');
        wp_redirect(admin_url());
        exit;
    }
}
/**
 * Upgrade the Usermeta API Key storage to swap keys/values for performance
 *
 * @since  2.4
 * @return void
 */
function edd_upgrade_user_api_keys()
{
    global $wpdb;
    if (!current_user_can('manage_shop_settings')) {
        wp_die(__('You do not have permission to do shop upgrades', 'edd'), __('Error', 'edd'), array('response' => 403));
    }
    ignore_user_abort(true);
    if (!edd_is_func_disabled('set_time_limit') && !ini_get('safe_mode')) {
        @set_time_limit(0);
    }
    $step = isset($_GET['step']) ? absint($_GET['step']) : 1;
    $number = 10;
    $offset = $step == 1 ? 0 : ($step - 1) * $number;
    if ($step < 2) {
        // Check if we have any users with API Keys before moving on
        $sql = "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'edd_user_public_key' LIMIT 1";
        $has_key = $wpdb->get_col($sql);
        if (empty($has_key)) {
            // We had no key, just complete
            update_option('edd_version', preg_replace('/[^0-9.].*/', '', EDD_VERSION));
            edd_set_upgrade_complete('upgrade_user_api_keys');
            delete_option('edd_doing_upgrade');
            wp_redirect(admin_url());
            exit;
        }
    }
    $total = isset($_GET['total']) ? absint($_GET['total']) : false;
    if (empty($total) || $total <= 1) {
        $total = $wpdb->get_var("SELECT count(user_id) FROM {$wpdb->usermeta} WHERE meta_key = 'edd_user_public_key'");
    }
    $keys_sql = $wpdb->prepare("SELECT user_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'edd_user_public_key' OR meta_key = 'edd_user_secret_key' ORDER BY user_id ASC LIMIT %d,%d;", $offset, $number);
    $found_keys = $wpdb->get_results($keys_sql);
    if (!empty($found_keys)) {
        foreach ($found_keys as $key) {
            $user_id = $key->user_id;
            $meta_key = $key->meta_key;
            $meta_value = $key->meta_value;
            // Generate a new entry
            update_user_meta($user_id, $meta_value, $meta_key);
            // Delete the old one
            delete_user_meta($user_id, $meta_key);
        }
        // More Payments found so upgrade them
        $step++;
        $redirect = add_query_arg(array('page' => 'edd-upgrades', 'edd-upgrade' => 'upgrade_user_api_keys', 'step' => $step, 'number' => $number, 'total' => $total), admin_url('index.php'));
        wp_redirect($redirect);
        exit;
    } else {
        // No more customers found, finish up
        update_option('edd_version', preg_replace('/[^0-9.].*/', '', EDD_VERSION));
        edd_set_upgrade_complete('upgrade_user_api_keys');
        delete_option('edd_doing_upgrade');
        wp_redirect(admin_url());
        exit;
    }
}
예제 #4
0
/**
 * Install
 *
 * Runs on plugin install by setting up the post types, custom taxonomies,
 * flushing rewrite rules to initiate the new 'downloads' slug and also
 * creates the plugin and populates the settings fields for those plugin
 * pages. After successful install, the user is redirected to the EDD Welcome
 * screen.
 *
 * @since 1.0
 * @global $wpdb
 * @global $edd_options
 * @global $wp_version
 * @return void
 */
function edd_install()
{
    global $wpdb, $edd_options, $wp_version;
    if (!function_exists('edd_create_protection_files')) {
        require_once EDD_PLUGIN_DIR . 'includes/admin/upload-functions.php';
    }
    // Setup the Downloads Custom Post Type
    edd_setup_edd_post_types();
    // Setup the Download Taxonomies
    edd_setup_download_taxonomies();
    // Clear the permalinks
    flush_rewrite_rules(false);
    // Add Upgraded From Option
    $current_version = get_option('edd_version');
    if ($current_version) {
        update_option('edd_version_upgraded_from', $current_version);
    }
    // Setup some default options
    $options = array();
    // Checks if the purchase page option exists
    if (!edd_get_option('purchase_page', false)) {
        // Checkout Page
        $checkout = wp_insert_post(array('post_title' => __('Checkout', 'easy-digital-downloads'), 'post_content' => '[download_checkout]', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'page', 'comment_status' => 'closed'));
        // Purchase Confirmation (Success) Page
        $success = wp_insert_post(array('post_title' => __('Purchase Confirmation', 'easy-digital-downloads'), 'post_content' => __('Thank you for your purchase! [edd_receipt]', 'easy-digital-downloads'), 'post_status' => 'publish', 'post_author' => 1, 'post_parent' => $checkout, 'post_type' => 'page', 'comment_status' => 'closed'));
        // Failed Purchase Page
        $failed = wp_insert_post(array('post_title' => __('Transaction Failed', 'easy-digital-downloads'), 'post_content' => __('Your transaction failed, please try again or contact site support.', 'easy-digital-downloads'), 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'page', 'post_parent' => $checkout, 'comment_status' => 'closed'));
        // Purchase History (History) Page
        $history = wp_insert_post(array('post_title' => __('Purchase History', 'easy-digital-downloads'), 'post_content' => '[purchase_history]', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'page', 'post_parent' => $checkout, 'comment_status' => 'closed'));
        // Store our page IDs
        $options['purchase_page'] = $checkout;
        $options['success_page'] = $success;
        $options['failure_page'] = $failed;
        $options['purchase_history_page'] = $history;
    }
    // Populate some default values
    foreach (edd_get_registered_settings() as $tab => $settings) {
        foreach ($settings as $option) {
            if ('checkbox' == $option['type'] && !empty($option['std'])) {
                $options[$option['id']] = '1';
            }
        }
    }
    update_option('edd_settings', array_merge($edd_options, $options));
    update_option('edd_version', EDD_VERSION);
    // Create wp-content/uploads/edd/ folder and the .htaccess file
    edd_create_protection_files(true);
    // Create EDD shop roles
    $roles = new EDD_Roles();
    $roles->add_roles();
    $roles->add_caps();
    $api = new EDD_API();
    update_option('edd_default_api_version', 'v' . $api->get_version());
    // Create the customers database
    @EDD()->customers->create_table();
    // Check for PHP Session support, and enable if available
    EDD()->session->use_php_sessions();
    // Add a temporary option to note that EDD pages have been created
    set_transient('_edd_installed', $options, 30);
    // Bail if activating from network, or bulk
    if (is_network_admin() || isset($_GET['activate-multi'])) {
        return;
    }
    if (!$current_version) {
        require_once EDD_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';
        // When new upgrade routines are added, mark them as complete on fresh install
        $upgrade_routines = array('upgrade_payment_taxes', 'upgrade_customer_payments_association', 'upgrade_user_api_keys', 'remove_refunded_sale_logs');
        foreach ($upgrade_routines as $upgrade) {
            edd_set_upgrade_complete($upgrade);
        }
    }
    // Add the transient to redirect
    set_transient('_edd_activation_redirect', true, 30);
}
예제 #5
0
/**
 * Run the upgrade to generate licenses for past bundle purchases
 *
 * @since  3.2
 * @return void
 */
function edd_sl_v32_add_bundle_licenses()
{
    global $wpdb;
    if (!current_user_can('manage_shop_settings')) {
        wp_die(__('You do not have permission to do shop upgrades', 'edd'), __('Error', 'edd'), array('response' => 403));
    }
    ignore_user_abort(true);
    if (!edd_is_func_disabled('set_time_limit') && !ini_get('safe_mode')) {
        @set_time_limit(0);
    }
    $step = isset($_GET['step']) ? absint($_GET['step']) : 1;
    $number = 25;
    $offset = $step == 1 ? 0 : ($step - 1) * $number;
    // Get the upgrade type...if none provided, dismiss is used and no further action is taken
    $custom = isset($_GET['custom']) ? absint($_GET['custom']) : 0;
    if ($step < 2) {
        // User chose to not run any upgrades
        if (0 === $custom) {
            update_option('edd_sl_version', preg_replace('/[^0-9.].*/', '', EDD_SL_VERSION));
            edd_set_upgrade_complete('sl_add_bundle_licenses');
            delete_option('edd_doing_upgrade');
            wp_redirect(admin_url());
            exit;
        }
        // Check if we have any payments before moving on
        $sql = "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'edd_payment' LIMIT 1";
        $has_payments = $wpdb->get_col($sql);
        if (empty($has_payments)) {
            // We had no payments, just complete
            update_option('edd_sl_version', preg_replace('/[^0-9.].*/', '', EDD_SL_VERSION));
            edd_set_upgrade_complete('sl_add_bundle_licenses');
            delete_option('edd_doing_upgrade');
            wp_redirect(admin_url());
            exit;
        }
        // Check if we have any bundle products as well
        $sql = "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_edd_bundled_products' LIMIT 1";
        $has_bundles = $wpdb->get_col($sql);
        if (empty($has_bundles)) {
            // We had no bundles, just complete
            update_option('edd_sl_version', preg_replace('/[^0-9.].*/', '', EDD_SL_VERSION));
            edd_set_upgrade_complete('sl_add_bundle_licenses');
            delete_option('edd_doing_upgrade');
            wp_redirect(admin_url());
            exit;
        }
    }
    $sql = "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_edd_bundled_products'";
    $bundle_download_ids = $wpdb->get_col($sql);
    foreach ($bundle_download_ids as $key => $bundle_id) {
        $licenses_enabled = get_post_meta($bundle_id, '_edd_sl_enabled', false);
        if (empty($licenses_enabled)) {
            unset($bundle_download_ids[$key]);
        }
    }
    // If we ended up with no bundles that have licensing enabled, just exit as well
    if (empty($bundle_download_ids)) {
        update_option('edd_sl_version', preg_replace('/[^0-9.].*/', '', EDD_SL_VERSION));
        edd_set_upgrade_complete('sl_add_bundle_licenses');
        delete_option('edd_doing_upgrade');
        wp_redirect(admin_url());
        exit;
    }
    $total = isset($_GET['total']) ? absint($_GET['total']) : false;
    if (empty($total) || $total <= 1) {
        $args = array('number' => -1, 'download' => $bundle_download_ids);
        $all_payments = new EDD_Payments_Query($args);
        $all_payments = $all_payments->get_payments();
        $total = count($all_payments);
    }
    $payment_args = array('order' => 'ASC', 'orderby' => 'ID', 'number' => $number, 'page' => $step, 'download' => $bundle_download_ids);
    $payments = new EDD_Payments_Query($payment_args);
    $payments = $payments->get_payments();
    $bundle_contents = array();
    if (!empty($payments)) {
        foreach ($payments as $payment) {
            $cart_details = edd_get_payment_meta_downloads($payment->ID);
            foreach ($cart_details as $cart_index => $cart_item) {
                if (!in_array($cart_item['id'], $bundle_download_ids)) {
                    // Item is not a bundled product, move along
                    continue;
                }
                // See if there is a bundle license already
                $bundle_license = edd_software_licensing()->get_license_by_purchase($payment->ID, $cart_item['id']);
                // It not create the bundle license
                if (empty($bundle_license)) {
                    $bundle_license = edd_software_licensing()->generate_license($cart_item['id'], $payment->ID, 'default', $cart_item, $cart_index);
                    $bundle_license = edd_software_licensing()->get_license_by_purchase($payment->ID, $cart_item['id'], $cart_index);
                }
                // If we haven't retrieved the bundle contents yet, get them
                if (!isset($bundle_contents[$cart_item['id']])) {
                    $bundle_contents[$cart_item['id']] = edd_get_bundled_products($cart_item['id']);
                }
                // Get the products in the bundle
                $bundle_products = $bundle_contents[$cart_item['id']];
                // For every bundle in the download, get the license for this payment, and update it's post_parent
                foreach ($bundle_products as $license_download_id) {
                    $license = edd_software_licensing()->get_license_by_purchase($payment->ID, $license_download_id);
                    if (empty($license)) {
                        continue;
                    }
                    $update_args = array('ID' => $license->ID, 'post_parent' => $bundle_license->ID);
                    wp_update_post($update_args);
                }
            }
        }
        // More Payments found so upgrade them
        $step++;
        $redirect = add_query_arg(array('page' => 'edd-upgrades', 'edd-upgrade' => 'sl_add_bundle_licenses', 'step' => $step, 'number' => $number, 'total' => $total, 'custom' => $custom), admin_url('index.php'));
        wp_safe_redirect($redirect);
        exit;
    } else {
        // No more data to update. Downloads have been altered or dismissed
        update_option('edd_sl_version', preg_replace('/[^0-9.].*/', '', EDD_SL_VERSION));
        edd_set_upgrade_complete('sl_add_bundle_licenses');
        delete_option('edd_doing_upgrade');
        wp_redirect(admin_url());
        exit;
    }
}