Migrate) and the note that points to it. * * For every table plugin found on the site: what was found, where it is used, what carries over and what does * not, a recommendation, and the actions (copy, keep old shortcodes working). Source data is never changed. * * @package NitidaTables */ namespace NitidaTables; defined( 'ABSPATH' ) || exit; require_once __DIR__ . '/class-kses.php'; /** * Admin UI for Migrate. */ final class MigrateAdmin { const SLUG = 'nitida-tables-migrate'; const DISMISSED = 'nitida_tables_migrate_notice_dismissed'; public static function init() { add_action( 'admin_menu', array( __CLASS__, 'register_menu' ), 15 ); add_action( 'admin_post_nitida_tables_migrate', array( __CLASS__, 'handle' ) ); add_action( 'admin_post_nitida_tables_dismiss_migrate_notice', array( __CLASS__, 'handle_dismiss' ) ); add_action( 'nitida_tables_migrate_takeover', array( __CLASS__, 'purge_page_caches' ) ); } public static function url( array $args = array() ) { return add_query_arg( array_merge( array( 'page' => self::SLUG ), $args ), admin_url( 'admin.php' ) ); } public static function register_menu() { add_submenu_page( Admin::SLUG, __( 'Migrate to Nitida Tables', 'nitida-tables' ), __( 'From other plugins', 'nitida-tables' ), 'manage_options', self::SLUG, array( __CLASS__, 'render' ), 3 ); } /** Pages render the old shortcodes differently now: clear the page caches we know about. */ public static function purge_page_caches() { if ( function_exists( 'rocket_clean_domain' ) ) { rocket_clean_domain(); } do_action( 'litespeed_purge_all' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- LiteSpeed Cache hook. if ( function_exists( 'w3tc_flush_all' ) ) { w3tc_flush_all(); } if ( function_exists( 'wp_cache_clear_cache' ) ) { wp_cache_clear_cache(); } } /* --------------------------------------------------------------------- * Note on the All tables screen * ------------------------------------------------------------------ */ public static function notice() { if ( ! current_user_can( 'manage_options' ) || get_user_meta( get_current_user_id(), self::DISMISSED, true ) ) { return; } $p = Migrate::pending(); if ( $p['tables'] < 1 ) { return; } $scan = Migrate::scan(); $parts = array(); foreach ( $p['sources'] as $key => $n ) { $parts[] = $scan[ $key ]['label'] . ': ' . (int) $n; } $dismiss = wp_nonce_url( admin_url( 'admin-post.php?action=nitida_tables_dismiss_migrate_notice' ), 'nitida_tables_dismiss_migrate_notice' ); echo '
'; /* translators: %d: number of tables */ echo esc_html( sprintf( _n( 'Nitida Tables found %d table made with another plugin', 'Nitida Tables found %d tables made with other plugins', $p['tables'], 'nitida-tables' ), $p['tables'] ) ); echo ' (' . esc_html( implode( ', ', $parts ) ) . ').
' . esc_html__( 'The migration assistant shows what carries over, where each table is used and copies them without changing your posts.', 'nitida-tables' ) . '
'; echo '
' . esc_html__( 'Open the migration assistant', 'nitida-tables' ) . ' '; echo '' . esc_html__( 'Dismiss', 'nitida-tables' ) . '
'; } public static function handle_dismiss() { check_admin_referer( 'nitida_tables_dismiss_migrate_notice' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to do that.', 'nitida-tables' ), '', array( 'response' => 403 ) ); } update_user_meta( get_current_user_id(), self::DISMISSED, 1 ); wp_safe_redirect( Admin::url() ); exit; } /* --------------------------------------------------------------------- * Actions * ------------------------------------------------------------------ */ public static function handle() { check_admin_referer( 'nitida_tables_migrate' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to do that.', 'nitida-tables' ), '', array( 'response' => 403 ) ); } // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified above. $key = isset( $_POST['source'] ) ? sanitize_key( wp_unslash( $_POST['source'] ) ) : ''; $do = isset( $_POST['do'] ) ? sanitize_key( wp_unslash( $_POST['do'] ) ) : ''; $ids = isset( $_POST['ids'] ) && is_array( $_POST['ids'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['ids'] ) ) : array(); $ovw = ! empty( $_POST['overwrite'] ); // phpcs:enable $src = Migrate::source( $key ); $msg = array( 'type' => 'ok', 'lines' => array() ); if ( ! $src ) { $msg = array( 'type' => 'err', 'lines' => array( __( 'Unknown source.', 'nitida-tables' ) ) ); } elseif ( 'takeover_on' === $do || 'takeover_off' === $do ) { Migrate::set_takeover( $key, 'takeover_on' === $do ); $msg['lines'][] = 'takeover_on' === $do /* translators: %s: plugin name */ ? sprintf( __( 'Old %s shortcodes and blocks now show the Nitida copies. Page caches were cleared.', 'nitida-tables' ), $src->label() ) /* translators: %s: plugin name */ : sprintf( __( 'Old %s shortcodes and blocks are shown by that plugin again.', 'nitida-tables' ), $src->label() ); } elseif ( 'copy' === $do || 'copy_all' === $do ) { $r = Migrate::import( $key, 'copy_all' === $do ? null : $ids, $ovw ); $n = count( $r['imported'] ); /* translators: %d: number of tables */ $msg['lines'][] = sprintf( _n( '%d table copied.', '%d tables copied.', $n, 'nitida-tables' ), $n ); if ( $r['skipped'] ) { /* translators: %d: number of tables */ $msg['lines'][] = sprintf( _n( '%d table was copied before and was left as it is.', '%d tables were copied before and were left as they are.', count( $r['skipped'] ), 'nitida-tables' ), count( $r['skipped'] ) ); } foreach ( $r['errors'] as $sid => $e ) { $msg['type'] = 'err'; /* translators: 1: table id in the other plugin, 2: error */ $msg['lines'][] = sprintf( __( 'Table %1$s: %2$s', 'nitida-tables' ), $sid, $e ); } if ( $n && ! Migrate::takeover_on( $key ) && $src->shortcodes() ) { $msg['lines'][] = __( 'Next: turn on "Keep old shortcodes working" so your posts show the copies.', 'nitida-tables' ); } } set_transient( 'nitida_tables_migrate_msg_' . get_current_user_id(), $msg, 120 ); wp_safe_redirect( self::url() . '#tt-src-' . $key ); exit; } /* --------------------------------------------------------------------- * Screen * ------------------------------------------------------------------ */ private static function form_open( $key, $do, $class = '' ) { $h = '
'; $h .= ''; return $h . wp_nonce_field( 'nitida_tables_migrate', '_wpnonce', true, false ); } /** * The state and recommendation for one source. * * "waiting" counts the tables a post uses that are not copied yet; those decide whether the source is done. * Tables no post uses ("unused") never block it. * * @return array{level:string,text:string,waiting:int,blocked:int,unused:int,ids:string[],unused_ids:string[]} */ public static function recommend( array $s, $can_takeover, $plugin_file ) { $total = 0; $todo = 0; $blocked = 0; $todo_used = 0; $blocked_used = 0; $left_blocked = 0; $ids = array(); $unused_ids = array(); foreach ( $s['tables'] as $t ) { $total++; $copied = isset( $s['mapped'][ (string) $t['id'] ] ); $used = ! empty( $s['usage'][ (string) $t['id'] ] ); if ( $copied ) { continue; } if ( empty( $t['importable'] ) ) { $blocked++; if ( $used ) { $blocked_used++; } else { $left_blocked++; } continue; } $todo++; if ( $used ) { $todo_used++; $ids[] = (string) $t['id']; } else { $unused_ids[] = (string) $t['id']; } } $extra = array( 'waiting' => $todo_used, 'blocked' => $blocked_used, 'unused' => $todo + $left_blocked, 'ids' => $ids, 'unused_ids' => $unused_ids ); $out = function ( $level, $text ) use ( $extra ) { return array_merge( array( 'level' => $level, 'text' => $text ), $extra ); }; if ( $todo_used && $s['takeover'] ) { return $out( 'todo', sprintf( /* translators: %d: number of tables */ _n( '%d table that your posts use is not copied yet. Copy it, or see the message shown after the last try. Old shortcodes already show the copied tables.', '%d tables that your posts use are not copied yet. Copy them, or see the message shown after the last try. Old shortcodes already show the copied tables.', $todo_used, 'nitida-tables' ), $todo_used ) ); } if ( $todo_used ) { return $out( 'todo', sprintf( /* translators: 1: number of tables, 2: plugin name */ _n( 'Recommended: copy the %1$d remaining table, then turn on "Keep old shortcodes working". Your %2$s data stays as it is, so you can go back at any time.', 'Recommended: copy the %1$d remaining tables, then turn on "Keep old shortcodes working". Your %2$s data stays as it is, so you can go back at any time.', $todo_used, 'nitida-tables' ), $todo_used, $s['label'] ) ); } if ( $blocked_used ) { return $out( 'warn', sprintf( /* translators: %d: number of tables */ _n( '%d table that your posts use cannot be copied; see the reason next to it. Keep the old plugin active for now.', '%d tables that your posts use cannot be copied; see the reason next to each one. Keep the old plugin active for now.', $blocked_used, 'nitida-tables' ), $blocked_used ) ); } if ( $can_takeover && ! $s['takeover'] && $total - $blocked > 0 ) { return $out( 'todo', __( 'All tables that your posts use are copied. Turn on "Keep old shortcodes working" so your posts show the Nitida copies, then check the pages listed below.', 'nitida-tables' ) ); } if ( $blocked && $blocked === $total ) { return $out( 'warn', __( 'These tables cannot be copied yet; see the reason next to each one.', 'nitida-tables' ) ); } $unused = $todo + $left_blocked; $note = $unused ? ' ' . sprintf( /* translators: %d: number of tables */ _n( '%d table is not used in any post and was not copied; this does not block anything.', '%d tables are not used in any post and were not copied; this does not block anything.', $unused, 'nitida-tables' ), $unused ) : ''; if ( $s['active'] && '' !== $plugin_file ) { return $out( 'done', sprintf( /* translators: %s: plugin name */ __( 'Done. Check a few pages, then you can deactivate %s. Keep it installed for a while in case you want to go back.', 'nitida-tables' ), $s['label'] ) . $note ); } return $out( 'done', __( 'Done. Every table that your posts use is in Nitida Tables.', 'nitida-tables' ) . $note ); } /** Copied / total for one source, and the post count. @return array{copied:int,total:int,posts:int} */ private static function counts( array $s ) { $ids = array_map( function ( $t ) { return (string) $t['id']; }, $s['tables'] ); $copied = count( array_intersect_key( $s['mapped'], array_flip( $ids ) ) ); $posts = count( array_unique( call_user_func_array( 'array_merge', array_merge( array( array() ), array_values( $s['usage'] ) ) ) ) ); return array( 'copied' => $copied, 'total' => count( $ids ), 'posts' => $posts ); } private static function bar( $part, $all ) { $pct = $all > 0 ? (int) round( 100 * $part / $all ) : 0; return ''; } public static function render() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'nitida-tables' ), '', array( 'response' => 403 ) ); } $scan = Migrate::scan( isset( $_GET['refresh'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended echo '
'; echo wp_kses( '

' . esc_html__( 'Migrate to Nitida Tables', 'nitida-tables' ) . Admin::title_badges() . '

' . esc_html__( 'Scan again', 'nitida-tables' ) . '

', Kses::allowed() ); $key = 'nitida_tables_migrate_msg_' . get_current_user_id(); $m = get_transient( $key ); if ( is_array( $m ) && ! empty( $m['lines'] ) ) { delete_transient( $key ); echo '
' . implode( '
', array_map( 'esc_html', $m['lines'] ) ) . '
'; } echo '

' . esc_html__( 'Nitida Tables looks for tables made with other table plugins on this site. Copy them here: the originals are not changed. With "Keep old shortcodes working" on, posts that use the old shortcodes or blocks show the Nitida copies, so no post needs editing, and you can switch back at any time.', 'nitida-tables' ) . '

'; do_action( 'nitida_tables_migrate_top', $scan ); // Prompts under the intro (escaped by the callback). $core = class_exists( CoreNotice::class ) ? CoreNotice::stats() : array( 'tables' => 0, 'posts' => 0 ); if ( ! $scan && ! $core['tables'] ) { echo '

' . esc_html__( 'Nothing to migrate', 'nitida-tables' ) . '

' . esc_html__( 'No tables from TablePress, Ninja Tables, wpDataTables or Data Tables Generator were found on this site.', 'nitida-tables' ) . '

'; return; } // Overview tiles. echo '
'; foreach ( $scan as $k => $s ) { $src = Migrate::source( $k ); $c = self::counts( $s ); $rec = self::recommend( $s, $src && ( $src->shortcodes() || $src->blocks() ), $src ? $src->plugin() : '' ); $done = 'done' === $rec['level']; if ( $rec['waiting'] ) { /* translators: %d: number of tables that are not copied yet */ $badge = '' . esc_html( sprintf( __( '%d waiting', 'nitida-tables' ), $rec['waiting'] ) ) . ''; } elseif ( 'warn' === $rec['level'] ) { $badge = '' . esc_html__( 'Needs attention', 'nitida-tables' ) . ''; } else { $badge = '' . esc_html__( 'To do', 'nitida-tables' ) . ''; } echo '' . esc_html( $s['label'] ) . ''; echo wp_kses( $done ? '' : $badge, Kses::allowed() ); /* translators: 1: tables copied, 2: all tables */ echo '' . esc_html( sprintf( __( '%1$d / %2$d', 'nitida-tables' ), $c['copied'], $c['total'] ) ) . '' . esc_html__( 'tables copied', 'nitida-tables' ) . ''; echo wp_kses( self::bar( $c['copied'], $c['total'] ), Kses::allowed() ); /* translators: %d: posts and pages */ echo '' . esc_html( sprintf( _n( 'used in %d post or page', 'used in %d posts and pages', $c['posts'], 'nitida-tables' ), $c['posts'] ) ) . ' · ' . esc_html( $s['active'] ? __( 'Plugin active', 'nitida-tables' ) : __( 'Plugin inactive', 'nitida-tables' ) ) . ''; if ( $done && $rec['unused'] ) { /* translators: %d: number of tables */ echo '' . esc_html( sprintf( _n( '%d unused table not copied', '%d unused tables not copied', $rec['unused'], 'nitida-tables' ), $rec['unused'] ) ) . ''; } echo ''; } if ( $core['tables'] ) { $pro = defined( 'NITIDA_TABLES_PRO_VERSION' ); echo '' . esc_html__( 'WordPress tables', 'nitida-tables' ) . ''; if ( $pro ) { echo ''; } echo '' . (int) $core['tables'] . '' . esc_html( _n( 'wide table block', 'wide table blocks', (int) $core['tables'], 'nitida-tables' ) ) . ''; if ( $pro ) { echo wp_kses( self::bar( 1, 1 ), Kses::allowed() ); } /* translators: %d: posts and pages */ echo '' . esc_html( sprintf( _n( 'in %d post or page', 'in %d posts and pages', $core['posts'], 'nitida-tables' ), $core['posts'] ) ) . ''; } echo '
'; foreach ( $scan as $k => $s ) { self::render_source( $k, $s ); } if ( $core['tables'] ) { self::render_core( $core ); } echo ''; // Select all rows of one source (printed in the footer, after the lists). wp_enqueue_script( 'nitida-tables-migrate', NITIDA_TABLES_URL . 'assets/admin/migrate.js', array(), NITIDA_TABLES_VERSION, array( 'in_footer' => true ) ); } private static function render_source( $k, array $s ) { $src = Migrate::source( $k ); if ( ! $src ) { return; } $can_takeover = (bool) ( $src->shortcodes() || $src->blocks() ); $rec = self::recommend( $s, $can_takeover, $src->plugin() ); $c = self::counts( $s ); echo '
'; echo '

' . esc_html( $s['label'] ) . '

' . esc_html( $s['active'] ? __( 'Plugin active', 'nitida-tables' ) : __( 'Plugin inactive', 'nitida-tables' ) ) . '
'; /* translators: 1: tables copied, 2: all tables */ echo wp_kses( '
' . esc_html( sprintf( __( '%1$d of %2$d copied', 'nitida-tables' ), $c['copied'], $c['total'] ) ) . '' . self::bar( $c['copied'], $c['total'] ) . '
', Kses::allowed() ); echo '
'; echo '
' . esc_html( $rec['text'] ) . '
'; if ( '' !== $src->advice() ) { echo '

' . esc_html( $src->advice() ) . '

'; } $copy_done = ! $rec['waiting'] && ! in_array( $rec['level'], array( 'todo', 'warn' ), true ); $pages = array(); foreach ( $s['usage'] as $plist ) { foreach ( (array) $plist as $pid ) { $pages[ (int) $pid ] = true; } } $pages = array_slice( array_keys( $pages ), 0, 3 ); echo '
    '; // Step 1: copy. echo '
  1. ' . esc_html__( 'Copy the tables', 'nitida-tables' ) . '
    '; if ( $rec['waiting'] ) { echo wp_kses( self::form_open( $k, 'copy', 'tt-step-actions' ), Kses::allowed() ); foreach ( $rec['ids'] as $sid ) { echo ''; } /* translators: %d: number of tables */ echo ''; } elseif ( ! empty( $rec['unused_ids'] ) ) { echo '

    ' . esc_html__( 'Every table that your posts use is copied.', 'nitida-tables' ) . '

    '; echo wp_kses( self::form_open( $k, 'copy', 'tt-step-actions' ), Kses::allowed() ); foreach ( $rec['unused_ids'] as $sid ) { echo ''; } /* translators: %d: number of tables */ echo ''; } else { echo '

    ' . esc_html__( 'Every table that can be copied is copied.', 'nitida-tables' ) . '

    '; } if ( $can_takeover ) { $on = (bool) $s['takeover']; $codes = array_map( function ( $t ) { return '[' . $t . ' id=…]'; }, $src->shortcodes() ); echo wp_kses( self::form_open( $k, $on ? 'takeover_off' : 'takeover_on', 'tt-migrate-take' ), Kses::allowed() ); echo '
    ' . esc_html__( 'Keep old shortcodes working', 'nitida-tables' ) . ''; /* translators: %s: shortcodes, for example [ninja_tables id=…] */ echo esc_html( sprintf( __( '%s and the plugin\'s blocks show the Nitida copy of each copied table. Tables not copied yet stay as they are.', 'nitida-tables' ), implode( ', ', $codes ) ) ) . '
    '; echo ''; } echo '
  2. '; // Step 2: check pages. echo '
  3. ' . esc_html__( 'Check your pages', 'nitida-tables' ) . '

    ' . esc_html__( 'Open a few pages that use these tables and see that they look right.', 'nitida-tables' ) . '

    '; if ( $pages ) { echo ''; } echo '
  4. '; // Step 3: deactivate. echo '
  5. ' . esc_html__( 'Deactivate the old plugin', 'nitida-tables' ) . '

    '; if ( $s['active'] ) { /* translators: %s: plugin name */ echo esc_html( sprintf( __( 'When the pages look right, deactivate %s on the Plugins screen, but do not delete it yet.', 'nitida-tables' ), $s['label'] ) ); } else { echo esc_html__( 'The old plugin is already inactive.', 'nitida-tables' ); } echo '

'; // Table list: folded once the work for this source is done. $open = 'done' !== $rec['level']; echo '
'; /* translators: %d: number of tables */ echo esc_html( sprintf( _n( 'Show %d table', 'Show all %d tables', $c['total'], 'nitida-tables' ), $c['total'] ) ) . ''; echo wp_kses( self::form_open( $k, 'copy' ), Kses::allowed() ); echo '
'; echo ''; foreach ( $s['tables'] as $t ) { $sid = (string) $t['id']; $tid = isset( $s['mapped'][ $sid ] ) ? (int) $s['mapped'][ $sid ] : 0; $posts = isset( $s['usage'][ $sid ] ) ? array_values( array_unique( $s['usage'][ $sid ] ) ) : array(); $check = ! empty( $t['importable'] ) ? '' : ''; echo wp_kses( '', Kses::allowed() ); echo ''; /* translators: 1: rows, 2: columns */ echo ''; echo ''; } echo '
' . esc_html__( 'Table', 'nitida-tables' ) . '' . esc_html__( 'Size', 'nitida-tables' ) . '' . esc_html__( 'Type', 'nitida-tables' ) . '' . esc_html__( 'Used in', 'nitida-tables' ) . '' . esc_html__( 'Status', 'nitida-tables' ) . '
' . $check . '' . esc_html( '' !== $t['name'] ? $t['name'] : __( '(untitled)', 'nitida-tables' ) ) . '
#' . esc_html( $sid ) . ( '' !== $t['modified'] ? ' · ' . esc_html( mysql2date( get_option( 'date_format' ), $t['modified'] ) ) : '' ) . '
'; if ( ! empty( $t['notes'] ) ) { echo '
    '; foreach ( (array) $t['notes'] as $n ) { echo '
  • ' . esc_html( $n ) . '
  • '; } echo '
'; } echo '
' . ( (int) $t['rows'] > 0 ? esc_html( sprintf( __( '%1$d × %2$d', 'nitida-tables' ), (int) $t['rows'], (int) $t['cols'] ) ) : '?' ) . '' . esc_html( $t['type'] ) . ''; if ( $posts ) { foreach ( array_slice( $posts, 0, 3 ) as $pid ) { $title = get_the_title( $pid ); echo '' . esc_html( '' !== $title ? $title : '#' . $pid ) . '
'; } if ( count( $posts ) > 3 ) { /* translators: %d: more posts */ echo '' . esc_html( sprintf( __( '+%d more', 'nitida-tables' ), count( $posts ) - 3 ) ) . ''; } } else { echo '' . esc_html__( 'Not found in posts', 'nitida-tables' ) . ''; } echo '
'; if ( $tid ) { /* translators: %d: Nitida table number */ echo '' . esc_html__( 'Copied', 'nitida-tables' ) . ''; } elseif ( empty( $t['importable'] ) ) { echo '' . esc_html__( 'Cannot copy', 'nitida-tables' ) . '
' . esc_html( $t['reason'] ) . '
'; } else { echo '' . esc_html__( 'Not copied', 'nitida-tables' ) . ''; } echo '
'; echo '
'; echo '
'; echo '
'; } private static function render_core( array $core ) { $pro = defined( 'NITIDA_TABLES_PRO_VERSION' ); echo '

' . esc_html__( 'WordPress tables', 'nitida-tables' ) . '

'; /* translators: 1: number of tables, 2: number of posts and pages */ echo '

' . esc_html( sprintf( _n( '%1$d table block with five or more columns, in %2$d post or page. It is part of the post, so it is not copied here.', '%1$d table blocks with five or more columns, in %2$d posts and pages. They are part of each post, so they are not copied here.', (int) $core['tables'], 'nitida-tables' ), $core['tables'], $core['posts'] ) ) . '

'; if ( $pro ) { echo '
' . esc_html__( 'Nitida Tables Pro shows them as cards on phones.', 'nitida-tables' ) . ' ' . esc_html__( 'Open WordPress tables', 'nitida-tables' ) . '
'; } else { echo '
' . esc_html__( 'On phones their columns get squeezed. Nitida Tables Pro shows them as cards on small screens and adds search and sorting, without changing your posts.', 'nitida-tables' ) . ' ' . esc_html__( 'Learn about Pro', 'nitida-tables' ) . '
'; } echo '
'; } } MigrateAdmin::init();