new name. */ const OPTIONS = array( 'twr_settings' => 'tablewright_settings', 'twr_presets' => 'tablewright_presets', 'twr_templates' => 'tablewright_templates', 'twr_install_id' => 'tablewright_install_id', 'twr_install_since' => 'tablewright_install_since', 'twr_stats_asked' => 'tablewright_stats_asked', ); public static function init() { // Before the post type is registered (init, priority 10), so the editor never sees a half-moved site. add_action( 'init', array( __CLASS__, 'maybe_upgrade' ), 1 ); } public static function maybe_upgrade() { if ( version_compare( (string) get_option( self::OPTION, '0' ), self::VERSION, '>=' ) ) { return; } self::move_options(); if ( ! self::move_tables() ) { return; // More tables than one request should move; the rest move on the next page load. } wp_clear_scheduled_hook( 'twr_stats_daily' ); delete_site_transient( 'twr_update_tablewright' ); delete_site_transient( 'twr_update_tablewright-pro' ); self::move_fonts(); update_option( self::OPTION, self::VERSION ); } private static function move_options() { foreach ( self::OPTIONS as $old => $new ) { $value = get_option( $old, null ); if ( null === $value ) { continue; } if ( null === get_option( $new, null ) ) { add_option( $new, $value, '', 'tablewright_settings' === $new ? 'yes' : 'no' ); } delete_option( $old ); } } /** Moves up to 200 tables a call to the new post type and meta key. Returns true when none are left. */ private static function move_tables() { $ids = get_posts( array( 'post_type' => 'twr_table', 'post_status' => 'any', 'numberposts' => 200, 'fields' => 'ids', ) ); foreach ( $ids as $id ) { $raw = get_post_meta( $id, '_twr_data', true ); if ( '' !== $raw && '' === get_post_meta( $id, '_tablewright_data', true ) ) { update_post_meta( $id, '_tablewright_data', wp_slash( $raw ) ); } delete_post_meta( $id, '_twr_data' ); set_post_type( $id, 'tablewright_table' ); } return count( $ids ) < 200; } /** uploads/tablewright-fonts → uploads/tablewright/fonts (the stylesheets point at files/ relatively, so they keep working). */ private static function move_fonts() { $up = wp_upload_dir( null, false ); $old = trailingslashit( $up['basedir'] ) . 'tablewright-fonts'; $new = Tablewright_Fonts::local_dir(); if ( ! is_dir( $old ) || is_link( $old ) || is_dir( $new['path'] ) || ! wp_mkdir_p( $new['root'] ) ) { return; } // One atomic folder move inside uploads (WP_Filesystem::move() may copy file by file); on failure the fonts download again. if ( ! @rename( $old, $new['path'] ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename, WordPress.PHP.NoSilencedErrors.Discouraged Tablewright_Fonts::purge_dir( $old ); return; } Tablewright_Fonts::ensure_dirs( $new ); } }