diff --git a/mc_after_settings.html b/mc_after_settings.html index 5e96a0cb0..62e801934 100644 --- a/mc_after_settings.html +++ b/mc_after_settings.html @@ -134,7 +134,7 @@
Parameters:
Source:
diff --git a/mc_custom_sidebar_panels.html b/mc_custom_sidebar_panels.html index b799543fb..4e6ec0877 100644 --- a/mc_custom_sidebar_panels.html +++ b/mc_custom_sidebar_panels.html @@ -134,7 +134,7 @@
Parameters:
Source:
diff --git a/mc_format_tribe_event_for_import.html b/mc_format_tribe_event_for_import.html index c069c69f3..488f5507b 100644 --- a/mc_format_tribe_event_for_import.html +++ b/mc_format_tribe_event_for_import.html @@ -157,7 +157,7 @@
Parameters:
Source:
diff --git a/mc_imported_event.html b/mc_imported_event.html index f28014758..a1927bb2a 100644 --- a/mc_imported_event.html +++ b/mc_imported_event.html @@ -134,7 +134,7 @@
Parameters:
Source:
diff --git a/mcs_submission_permissions.html b/mcs_submission_permissions.html index 556ea39dd..344f0c11e 100644 --- a/mcs_submission_permissions.html +++ b/mcs_submission_permissions.html @@ -134,7 +134,7 @@
Parameters:
Source:
diff --git a/my-calendar-import.php.html b/my-calendar-import.php.html index 8d24a33bd..31f147eca 100644 --- a/my-calendar-import.php.html +++ b/my-calendar-import.php.html @@ -55,47 +55,100 @@ } /** - * Import Tribe Events. + * Display current progress in importing. * - * @return int Number of events imported. + * @return string */ -function mc_import_source_tribe_events() { - $count = wp_count_posts( 'tribe_events' ); - $total = 0; - foreach ( $count as $c ) { - $total = $total + (int) $c; +function mc_display_progress() { + $message = ''; + if ( as_has_scheduled_action( 'mc_import_tribe' ) ) { + $count = mc_count_tribe_remaining(); + $message = sprintf( __( 'Import from The Events Calendar is in progress. There are currently %d events remaining.', 'my-calendar' ), $count ); } - if ( $total < 50 ) { - $num_posts = -1; - } else { - $num_posts = 25; - } - // Get all events not already imported. + + return ( $message ) ? '<div class="notice notice-info"><p>' . $message . '</p></div>' : ''; +} + +/** + * Count remaining events from Tribe Events Calendar. + * + * @return int + */ +function mc_count_tribe_remaining() { $args = array( 'post_type' => 'tribe_events', - 'numberposts' => $num_posts, + 'numberposts' => -1, 'fields' => 'ids', 'post_status' => 'any', 'meta_query' => array( - 'queries' => array( + array( 'key' => '_mc_imported', 'compare' => 'NOT EXISTS', ), ), ); $events = get_posts( $args ); + if ( 0 === count( $events ) ) { + as_unschedule_all_actions( 'mc_import_tribe' ); + } + + return count( $events ); +} + +/** + * Import Tribe Events. + * + * @return string Message about imported events. + */ +function mc_import_source_tribe_events() { + global $wpdb; + $count = wp_count_posts( 'tribe_events' ); + $message = ''; + $total = 0; + foreach ( $count as $c ) { + $total = $total + (int) $c; + } + if ( $total < 50 ) { + $num_posts = -1; + } else { + $num_posts = 25; + } + // Get selection of events not already imported. + $query = "SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.ID FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '_mc_imported' ) WHERE 1=1 AND ( $wpdb->postmeta.post_id IS NULL ) AND $wpdb->posts.post_type = 'tribe_events' AND (($wpdb->posts.post_status <> 'trash' AND $wpdb->posts.post_status <> 'auto-draft')) GROUP BY wp_posts.ID ORDER BY $wpdb->posts.post_date DESC LIMIT 0, $num_posts"; + $events = $wpdb->get_results( $query ); + print_r( $events ); + $ids = array(); - foreach ( $events as $post_id ) { - $id = mc_import_source_tribe_event( $post_id ); - if ( $id ) { - $ids[] = $id; + $count = count( $events ); + if ( 0 === $count ) { + update_option( 'mc_import_tribe_completed', 'true' ); + } else { + foreach ( $events as $post ) { + $id = mc_import_source_tribe_event( $post->ID ); + if ( $id ) { + $ids[] = $id; + } + } + + $completed = count( $ids ); + if ( false === as_has_scheduled_action( 'mc_import_tribe' ) ) { + as_schedule_recurring_action( strtotime( '+1 minutes' ), 60, 'mc_import_tribe', array(), 'my-calendar' ); } + // translators: 1) Number of events imported, 2) total number of events found. + $message = '<div class="notice notice-info"><p>' . sprintf( __( '%1$d events imported. %2$d remaining. Remaining events are being imported in the background. You can feel free to leave this page.', 'my-calendar' ), $completed, $total ) . '</p></div>'; } - $completed = count( $ids ); - // translators: 1) Number of events imported, 2) total number of events found. - echo '<div class="notice notice-success"><p>' . sprintf( __( '%1$d events imported. %2$d remaining. Remaining events are being imported in the background. You can feel free to leave this page.', 'my-calendar' ), $completed, $total ) . '</p></div>'; - return $completed; + return $message; +} +add_action( 'mc_import_tribe', 'mc_import_source_tribe_events' ); + +/** + * + */ +function mc_check_tribe_imports() { + if ( 'true' === get_option( 'mc_import_tribe_completed' ) ) { + as_unschedule_all_actions( 'mc_import_tribe' ); + } } /** @@ -106,10 +159,10 @@ * @return bool|int False of new post ID. */ function mc_import_source_tribe_event( $post_id ) { - // If already imported, return the new event ID. + // If already imported, return false. $imported = get_post_meta( $post_id, '_mc_imported', true ); if ( $imported ) { - return $imported; + return false; } $tribe_event = get_post( $post_id ); /** @@ -132,6 +185,7 @@ if ( $check[0] ) { $response = my_calendar_save( 'add', $check ); $event_id = $response['event_id']; + update_post_meta( $post_id, '_mc_imported', $event_id ); } return $event_id; @@ -146,18 +200,21 @@ */ function mc_format_tribe_event_for_import( $event ) { $terms = get_the_terms( $event, 'tribe_events_cat' ); - foreach ( $terms as $term ) { - $cat_id = mc_category_by_name( $term->name ); - if ( ! $cat_id ) { - $cat = array( - 'category_name' => $term->name, - ); - $cat_id = mc_create_category( $cat ); + if ( is_array( $terms ) ) { + foreach ( $terms as $term ) { + $cat_id = mc_category_by_name( $term->name ); + if ( ! $cat_id ) { + $cat = array( + 'category_name' => $term->name, + ); + $cat_id = mc_create_category( $cat ); + } + // if category does not exist, create. + $category_ids[] = $cat_id; } - // if category does not exist, create. - $category_ids[] = $cat_id; + } else { + $category_ids[] = 1; } - $my_calendar_event = array( // Event data. 'event_title' => $event->post_title, diff --git a/my-calendar-settings.php.html b/my-calendar-settings.php.html index b0812df4f..62c52541c 100644 --- a/my-calendar-settings.php.html +++ b/my-calendar-settings.php.html @@ -705,56 +705,7 @@ ?> </div> <div class="settings postbox-container jcd-wide"> - <div class="metabox-holder"> - <?php - if ( isset( $_POST['import'] ) && 'true' === $_POST['import'] ) { - $nonce = $_REQUEST['_wpnonce']; - if ( ! wp_verify_nonce( $nonce, 'my-calendar-nonce' ) ) { - wp_die( 'My Calendar: Security check failed' ); - } - $source = ( in_array( $_POST['source'], array( 'calendar', 'tribe' ), true ) ) ? $_POST['source'] : false; - if ( $source ) { - my_calendar_import( $source ); - } - } - if ( function_exists( 'check_calendar' ) && 'true' !== get_option( 'ko_calendar_imported' ) ) { - ?> - <div id="mc-importer" class='notice notice-info'> - <p> - <?php _e( 'You have the Calendar plugin by Kieran O\'Shea installed. You can import those events and categories into My Calendar.', 'my-calendar' ); ?> - </p> - - <form method="post" action="<?php echo esc_url( admin_url( 'admin.php?page=my-calendar-config' ) ); ?>"> - <div> - <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'my-calendar-nonce' ); ?>"/> - <input type="hidden" name="import" value="true" /> - <input type="hidden" name="source" value="calendar" /> - <input type="submit" value="<?php _e( 'Import from Calendar', 'my-calendar' ); ?>" name="import-calendar" class="button-primary"/> - </div> - </form> - </div> - <?php - } - delete_option( 'mc_tribe_imported' ); - if ( function_exists( 'tribe_get_event' ) && 'true' !== get_option( 'mc_tribe_imported' ) ) { - ?> - <div id="mc-importer" class='notice notice-info'> - <p> - <?php _e( 'You have The Events Calendar installed. You can import those events, venues, and categories into My Calendar.', 'my-calendar' ); ?> - </p> - - <form method="post" action="<?php echo esc_url( admin_url( 'admin.php?page=my-calendar-config' ) ); ?>"> - <div> - <input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'my-calendar-nonce' ); ?>"/> - <input type="hidden" name="import" value="true" /> - <input type="hidden" name="source" value="tribe" /> - <input type="submit" value="<?php _e( 'Import Events', 'my-calendar' ); ?>" name="import-calendar" class="button-primary"/> - </div> - </form> - </div> - <?php - } - ?> +<div class="metabox-holder"> <div class="ui-sortable meta-box-sortables"> <div class="wptab postbox" aria-labelledby="tab_manage" role="tabpanel" id="my-calendar-manage"> <h2><?php esc_html_e( 'My Calendar Management', 'my-calendar' ); ?></h2> diff --git a/my-calendar.php.html b/my-calendar.php.html index cc218f984..ca77626b1 100644 --- a/my-calendar.php.html +++ b/my-calendar.php.html @@ -162,6 +162,7 @@ } } +include( dirname( __FILE__ ) . '/action-scheduler/action-scheduler.php' ); include( dirname( __FILE__ ) . '/includes/date-utilities.php' ); include( dirname( __FILE__ ) . '/includes/general-utilities.php' ); include( dirname( __FILE__ ) . '/includes/event-utilities.php' ); @@ -177,6 +178,7 @@ include( dirname( __FILE__ ) . '/my-calendar-core.php' ); include( dirname( __FILE__ ) . '/my-calendar-install.php' ); include( dirname( __FILE__ ) . '/my-calendar-settings.php' ); +include( dirname( __FILE__ ) . '/my-calendar-migrate.php' ); include( dirname( __FILE__ ) . '/my-calendar-categories.php' ); include( dirname( __FILE__ ) . '/my-calendar-locations.php' ); include( dirname( __FILE__ ) . '/my-calendar-location-manager.php' ); @@ -413,6 +415,21 @@ } } +/** + * Test whether a system is present that My Calendar supports migration from. + * + * @return bool + */ +function mc_has_migration_path() { + if ( function_exists( 'check_calendar' ) && 'true' !== get_option( 'ko_calendar_imported' ) ) { + return true; + } + if ( function_exists( 'tribe_get_event' ) && 'true' !== get_option( 'mc_tribe_imported' ) ) { + return true; + } + return false; +} + /** * Add My Calendar menu items to main admin menu */ @@ -465,6 +482,9 @@ } add_submenu_page( 'my-calendar', __( 'Design', 'my-calendar' ), __( 'Design', 'my-calendar' ), $permission, 'my-calendar-design', 'my_calendar_design' ); add_submenu_page( 'my-calendar', __( 'Settings', 'my-calendar' ), __( 'Settings', 'my-calendar' ), 'mc_edit_settings', 'my-calendar-config', 'my_calendar_settings' ); + if ( mc_has_migration_path() ) { + add_submenu_page( 'my-calendar', __( 'Migration', 'my-calendar' ), __( 'Migration', 'my-calendar' ), 'mc_edit_settings', 'my-calendar-migrate', 'my_calendar_migration' ); + } add_submenu_page( 'my-calendar', __( 'My Calendar Shortcode Generator', 'my-calendar' ), __( 'Shortcodes', 'my-calendar' ), 'mc_view_help', 'my-calendar-shortcodes', 'my_calendar_shortcodes' ); add_submenu_page( 'my-calendar', __( 'My Calendar Help', 'my-calendar' ), __( 'Help', 'my-calendar' ), 'mc_view_help', 'my-calendar-help', 'my_calendar_help' ); // Null submenu parent prevents this from appearing in the admin menu.