| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| add_action( 'admin_notices', function () {
|
|
|
| global $pagenow;
|
|
|
| if (
|
| ! current_user_can( 'manage_options' ) ||
|
| $pagenow !== 'index.php' ||
|
| get_option( 'sugar_calendar_tec_migration' ) === false
|
| ) {
|
| return;
|
| }
|
|
|
| $url = wp_nonce_url( admin_url( 'admin-post.php?action=sc_reset_tec_migration' ), 'sc_reset_tec_migration' );
|
|
|
| printf(
|
| '<div class="notice notice-warning"><p>%s</p><p><a class="button" href="%s" onclick="return confirm(\'%s\');">%s</a></p></div>',
|
| 'This deletes the Sugar Calendar events, calendars, tags and venues that match items in The Events Calendar, then lets you run the migration again. Back up your database first.',
|
| esc_url( $url ),
|
| esc_js( 'Permanently delete the migrated items and reset the migration?' ),
|
| 'Reset TEC migration'
|
| );
|
| } );
|
|
|
| add_action( 'admin_post_sc_reset_tec_migration', function () {
|
|
|
| if ( ! current_user_can( 'manage_options' ) ) {
|
| wp_die( 'Not allowed.' );
|
| }
|
|
|
| check_admin_referer( 'sc_reset_tec_migration' );
|
|
|
| global $wpdb;
|
|
|
|
|
| $tec_titles = function ( $post_type ) use ( $wpdb ) {
|
| return $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_title FROM {$wpdb->posts} WHERE post_type = %s AND post_status NOT IN ( 'trash', 'auto-draft' )", $post_type ) );
|
| };
|
| $tec_terms = function ( $taxonomy, $object_type ) use ( $wpdb ) {
|
| return $wpdb->get_col( $wpdb->prepare(
|
| "SELECT DISTINCT t.name FROM {$wpdb->terms} t
|
| INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_id = t.term_id
|
| INNER JOIN {$wpdb->term_relationships} tr ON tr.term_taxonomy_id = tt.term_taxonomy_id
|
| INNER JOIN {$wpdb->posts} p ON p.ID = tr.object_id
|
| WHERE tt.taxonomy = %s AND p.post_type = %s",
|
| $taxonomy,
|
| $object_type
|
| ) );
|
| };
|
|
|
|
|
| $delete_posts = function ( $post_types, $titles ) use ( $wpdb ) {
|
| if ( empty( $titles ) ) {
|
| return 0;
|
| }
|
| $ids = $wpdb->get_col(
|
| "SELECT ID FROM {$wpdb->posts} WHERE post_type IN ( '" . implode( "','", array_map( 'esc_sql', $post_types ) ) . "' )
|
| AND post_title IN ( '" . implode( "','", array_map( 'esc_sql', $titles ) ) . "' )"
|
| );
|
| foreach ( $ids as $id ) {
|
| wp_delete_post( (int) $id, true );
|
| }
|
| return count( $ids );
|
| };
|
|
|
|
|
| $delete_terms = function ( $taxonomy, $names ) {
|
| $count = 0;
|
| foreach ( $names as $name ) {
|
| $terms = get_terms( [ 'taxonomy' => $taxonomy, 'name' => $name, 'hide_empty' => false ] );
|
| foreach ( is_array( $terms ) ? $terms : [] as $term ) {
|
| $count += (int) ( wp_delete_term( $term->term_id, $taxonomy ) === true );
|
| }
|
| }
|
| return $count;
|
| };
|
|
|
| $deleted = [
|
| 'events' => $delete_posts( [ 'sc_event', 'sc_recurring_event' ], $tec_titles( 'tribe_events' ) ),
|
| 'venues' => $delete_posts( [ 'sugarcalendar_venue' ], $tec_titles( 'tribe_venue' ) ),
|
| 'calendars' => $delete_terms( 'sc_event_category', $tec_terms( 'tribe_events_cat', 'tribe_events' ) ),
|
| 'tags' => $delete_terms( 'sc_event_tags', $tec_terms( 'post_tag', 'tribe_events' ) ),
|
| ];
|
|
|
|
|
| foreach ( [ 'events', 'categories', 'tickets', 'attendees', 'orders', 'venues', 'tags' ] as $table ) {
|
| $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}sc_migrate_tec_{$table}" );
|
| }
|
|
|
| delete_option( 'sugar_calendar_tec_migration' );
|
| delete_option( 'sugar_calendar_tec_migration_process' );
|
|
|
| wp_die(
|
| sprintf(
|
| 'Reset done. Deleted %d events, %d venues, %d calendars and %d tags. <a href="%s">Run the migration again</a>, then remove this snippet.',
|
| $deleted['events'],
|
| $deleted['venues'],
|
| $deleted['calendars'],
|
| $deleted['tags'],
|
| esc_url( admin_url( 'admin.php?page=sc-tools§ion=migrate&importer=the-events-calendar' ) )
|
| ),
|
| 'TEC migration reset',
|
| [ 'response' => 200 ]
|
| );
|
| } );
|
| |
| |
Comments