Skip to content

Instantly share code, notes, and snippets.

@jbrinley
Created January 8, 2014 17:59
Show Gist options
  • Save jbrinley/8321252 to your computer and use it in GitHub Desktop.
Save jbrinley/8321252 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: The Events Calendar - Downgrade Recurrence
Description: Downgrading from 3.4 to an earlier version of The Events Calendar? Activate this plugin to restore recurrence settings.
Version: 1.0
Author: Modern Tribe, Inc.
Author URI: http://tri.be?ref=tec-plugin
Text Domain: tribe-events-calendar
License: GPLv2 or later
*/
if ( !function_exists('tribe_events_recurrence_downgrade') ) {
function tribe_events_recurrence_downgrade() {
if ( class_exists('TribeEventsPro') && defined('TribeEventsPro::VERSION') ) {
if ( version_compare(TribeEventsPro::VERSION, '3.4') < 0 && version_compare(tribe_get_option('pro-schema-version', '3.3'), '3.3') > 0 && !class_exists('Tribe_Events_Recurrence_Instance') ) {
/** @var wpdb $wpdb */
global $wpdb;
$parents = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_parent FROM {$wpdb->posts} WHERE post_type=%s AND post_parent <> 0", TribeEvents::POSTTYPE));
foreach ( $parents as $parent_id ) {
$children = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type=%s AND post_parent=%d", TribeEvents::POSTTYPE, $parent_id));
if ( empty($children) ) { continue; }
foreach ( $children as $child_id ) {
if ( $start_date = get_post_meta($child_id, '_EventStartDate', TRUE) ) {
add_post_meta( $parent_id, '_EventStartDate', $start_date );
wp_delete_post( $child_id, TRUE );
}
}
}
tribe_update_option('pro-schema-version', '3.3');
}
}
}
add_action('admin_init', 'tribe_events_recurrence_downgrade');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment