tp_{name}. */ const TABLES = array( 'translation_groups', 'segments', 'strings', 'memory', 'glossary', 'jobs', 'logs' ); /** * Expose the plugin's tables as $wpdb properties so queries can use * {$wpdb->tp_strings} like core tables. Re-run after switch_to_blog(). */ public static function register_tables(): void { global $wpdb; foreach ( self::TABLES as $name ) { $wpdb->{'tp_' . $name} = $wpdb->prefix . 'tp_' . $name; } } /** * @param bool $network_wide Network activation on multisite. */ public static function activate( $network_wide = false ): void { if ( is_multisite() && $network_wide ) { foreach ( get_sites( array( 'fields' => 'ids', 'number' => 0 ) ) as $site_id ) { switch_to_blog( $site_id ); self::activate_site(); restore_current_blog(); } return; } self::activate_site(); } public static function activate_site(): void { self::register_tables(); self::create_tables(); \TranslatePlus\Workflow\Roles::install(); update_option( self::DB_OPTION, TRANSLATEPLUS_DB_VERSION ); if ( false === get_option( Settings::OPTION ) ) { add_option( Settings::OPTION, Settings::defaults() ); } set_transient( 'translateplus_activation_redirect', 1, 60 ); flush_rewrite_rules(); } public static function deactivate(): void { if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( '', array(), 'translateplus' ); } wp_clear_scheduled_hook( 'translateplus_prune_logs' ); flush_rewrite_rules(); } /** * New site on a network where the plugin is network-active. * * @param \WP_Site $site Site. */ public static function new_site( $site ): void { if ( ! function_exists( 'is_plugin_active_for_network' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if ( is_plugin_active_for_network( plugin_basename( TRANSLATEPLUS_FILE ) ) ) { switch_to_blog( (int) $site->blog_id ); self::activate_site(); restore_current_blog(); } } public static function maybe_upgrade(): void { if ( get_option( self::DB_OPTION ) !== TRANSLATEPLUS_DB_VERSION ) { self::create_tables(); \TranslatePlus\Workflow\Roles::install(); update_option( self::DB_OPTION, TRANSLATEPLUS_DB_VERSION ); } } /** * Create or update all custom tables via dbDelta. */ public static function create_tables(): void { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $c = $wpdb->get_charset_collate(); $p = $wpdb->prefix . 'tp_'; $sql = array( "CREATE TABLE {$p}translation_groups ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, group_id BIGINT UNSIGNED NOT NULL, object_type VARCHAR(20) NOT NULL, object_id BIGINT UNSIGNED NOT NULL, lang VARCHAR(15) NOT NULL, source_hash CHAR(32) NOT NULL DEFAULT '', status VARCHAR(20) NOT NULL DEFAULT 'auto', updated_at DATETIME NOT NULL, PRIMARY KEY (id), UNIQUE KEY object (object_type, object_id), KEY group_lang (group_id, lang) ) $c;", "CREATE TABLE {$p}segments ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, object_type VARCHAR(20) NOT NULL, object_id BIGINT UNSIGNED NOT NULL, lang VARCHAR(15) NOT NULL, segment_key VARCHAR(191) NOT NULL, source_hash CHAR(32) NOT NULL, source LONGTEXT NOT NULL, translation LONGTEXT NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'auto', origin VARCHAR(10) NOT NULL DEFAULT 'auto', updated_by BIGINT UNSIGNED NOT NULL DEFAULT 0, updated_at DATETIME NOT NULL, PRIMARY KEY (id), UNIQUE KEY seg (object_type, object_id, lang, segment_key), KEY status (status) ) $c;", "CREATE TABLE {$p}strings ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, hash CHAR(32) NOT NULL, domain VARCHAR(100) NOT NULL DEFAULT '', context VARCHAR(191) NOT NULL DEFAULT '', lang VARCHAR(15) NOT NULL, source LONGTEXT NOT NULL, translation LONGTEXT NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'untranslated', updated_at DATETIME NOT NULL, PRIMARY KEY (id), UNIQUE KEY hash_lang (hash, lang), KEY domain (domain) ) $c;", "CREATE TABLE {$p}memory ( hash CHAR(32) NOT NULL, source_lang VARCHAR(15) NOT NULL, target_lang VARCHAR(15) NOT NULL, source LONGTEXT NOT NULL, translation LONGTEXT NOT NULL, hits INT UNSIGNED NOT NULL DEFAULT 0, created_at DATETIME NOT NULL, PRIMARY KEY (hash) ) $c;", "CREATE TABLE {$p}glossary ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, term VARCHAR(191) NOT NULL, lang VARCHAR(15) NOT NULL DEFAULT '*', translation VARCHAR(191) NOT NULL DEFAULT '', case_sensitive TINYINT(1) NOT NULL DEFAULT 1, PRIMARY KEY (id), UNIQUE KEY term_lang (term, lang) ) $c;", "CREATE TABLE {$p}jobs ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, type VARCHAR(30) NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'queued', payload LONGTEXT NOT NULL, total INT UNSIGNED NOT NULL DEFAULT 0, done INT UNSIGNED NOT NULL DEFAULT 0, failed INT UNSIGNED NOT NULL DEFAULT 0, credits_estimated BIGINT UNSIGNED NOT NULL DEFAULT 0, error TEXT NULL, created_by BIGINT UNSIGNED NOT NULL DEFAULT 0, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY (id), KEY status (status) ) $c;", "CREATE TABLE {$p}logs ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, level VARCHAR(10) NOT NULL, message TEXT NOT NULL, context LONGTEXT NULL, created_at DATETIME NOT NULL, PRIMARY KEY (id), KEY created_at (created_at) ) $c;", ); foreach ( $sql as $statement ) { dbDelta( $statement ); } } }