Skip to content

Instantly share code, notes, and snippets.

@cklosowski
Created December 19, 2016 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cklosowski/d87b9f144de1d38429b299a5a286d908 to your computer and use it in GitHub Desktop.
Save cklosowski/d87b9f144de1d38429b299a5a286d908 to your computer and use it in GitHub Desktop.
WIP Example of using the EDD upgrade routines
<?php
/**
* Triggers all upgrade functions
*
* @since 2.2
* @return void
*/
function edd_pl_show_upgrade_notice() {
if( ! function_exists( 'EDD' ) ) {
return;
}
// If there is a stored version, get it first.
$edd_pl_version = get_option( 'edd_pl_version' );
// First be sure that the right version of EDD is isntalled that supports this.
if( function_exists( 'edd_has_upgrade_completed' ) && function_exists( 'edd_maybe_resume_upgrade' ) ) {
// See if there is an upgrade that didn't finish.
$resume_upgrade = edd_maybe_resume_upgrade();
// If no upgrades are needing to be resumed, we can show our notice.
if ( empty( $resume_upgrade ) ) {
// Do any necessary version checks, and also see if the person has already completed this upgrade
if ( version_compare( $edd_pl_version, '3.4.8', '<' ) || ! edd_has_upgrade_completed( 'pl_update_slug' ) ) { // Make this slug unique to what data is being upgraded
printf(
'<div class="updated"><p>' . __( 'The Purchast Limit post meta needs to be upgraded, click <a href="%s">here</a> to start the upgrade.', 'text-domain' ) . '</p></div>',
esc_url( add_query_arg( array( 'edd_action' => 'pl_update_slug' ), admin_url() ) )
);
}
}
}
}
add_action( 'admin_notices', 'edd_pl_show_upgrade_notice' );
/**
* Runs an upgrade routine to remove old _edd_sl_site_count meta in favor of the helper method in the main class:
* edd_software_licensing->get_site_count( $license_id )
*
* @since 3.4.8
* @return void
*/
function edd_pl_upgrade_function() {
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);
}
// Get's the step from the Query string, or defaults to 1
$step = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
// Set this to the number of items you want to process per step. Lower for more intense DB queries
$number = 25;
// Gets the total from the query string if it's passed, so we don't have to recalc this every time.
$total = isset( $_GET['total'] ) ? absint( $_GET['total'] ) : false;
// If this is the first step, let's see if we need to process anything.
if ( $step < 2 ) {
// Check if we have any downloads before moving on
$sql = "SELECT ID FROM $wpdb->posts WHERE post_type = 'download' LIMIT 1";
$has_downloads = $wpdb->get_col( $sql );
if( empty( $has_downloads ) ) {
// We had no downloads, just complete
// If you store a version in the DB, update it
update_option( 'edd_sl_version', preg_replace( '/[^0-9.].*/', '', EDD_SL_VERSION ) );
// Set our upgrade as complete
edd_set_upgrade_complete( 'pl_update_slug' );
// Remove the 'doing upgrade' place holder
delete_option( 'edd_doing_upgrade' );
// GTFO
wp_redirect( admin_url() ); exit;
}
// Check if we have any of the old meta too, if nothing has it, no sense in running
$sql = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_edd_purchase_limit' LIMIT 1";
$has_old_meta = $wpdb->get_col( $sql );
if ( empty( $has_old_meta ) ) {
// We had no site count meta, just complete
// If you store a version in the DB, update it
update_option( 'edd_sl_version', preg_replace( '/[^0-9.].*/', '', EDD_SL_VERSION ) );
// Set our upgrade as complete
edd_set_upgrade_complete( 'pl_update_slug' );
// Remove the 'doing upgrade' place holder
delete_option( 'edd_doing_upgrade' );
// GTFO
wp_redirect( admin_url() ); exit;
}
}
if ( false === $total ) {
$sql = "SELECT COUNT( post_id ) FROM $wpdb->postmeta WHERE meta_key = '_edd_purchase_limit' ";
$total = $wpdb->get_var( $sql );
}
$sql = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_edd_purchase_limit' LIMIT %d;", $number );
$download_ids = $wpdb->get_col( $sql );
if( ! empty( $download_ids ) ) {
foreach ( $download_ids as $download_id ) {
// Do wicked stuff with the $download_id, and make your new meta and stuff.
}
// More Payments found so upgrade them
$step++;
$redirect = add_query_arg( array(
'page' => 'edd-upgrades',
'edd-upgrade' => 'pl_update_slug',
'step' => $step,
'number' => $number,
'total' => $total,
), 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( 'pl_update_slug' );
delete_option( 'edd_doing_upgrade' );
wp_redirect( admin_url() ); exit;
}
}
add_action( 'edd_pl_update_slug', 'edd_pl_upgrade_function' );
/** This goes in your activation stuff */
function edd_pl_install() {
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(
'pl_update_slug',
);
foreach ( $upgrade_routines as $upgrade ) {
edd_set_upgrade_complete( $upgrade );
}
}
register_activation_hook( __FILE__, 'edd_pl_install' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment