One-off data migration (method) per schema version that introduced it. */ private const DATA_MIGRATIONS = [ 8 => 'retireNonRelayConnections', ]; /** * Runs the data migrations an existing install has not had yet. * * @param int $fromVersion Schema version installed before this update (> 0). */ private function runDataMigrations( int $fromVersion ): void { foreach ( self::DATA_MIGRATIONS as $version => $method ) { if ( $fromVersion < $version ) { $this->{$method}(); } } } /** * Runs pending migrations and records the new version. */ public function run(): void { $installed = (int) get_option( 'feednetic_db_version', 0 ); if ( $installed >= self::DB_VERSION ) { return; } $this->createTables(); if ( $installed > 0 ) { $this->runDataMigrations( $installed ); } update_option( 'feednetic_db_version', self::DB_VERSION, false ); } /** * v8: accounts are connected only through the Feednetic relay. * * - Pending OAuth state transients are deleted (they could hold an * encrypted Meta app secret from the removed connection method). * - Sources not connected through the relay keep their posts but lose * their token and become "expired" with a reconnect message, so the * admin reconnects them through the relay. */ private function retireNonRelayConnections(): void { global $wpdb; // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one-off migration on plugin-owned rows // delete_transient() also clears the object cache. With an external // object cache these transients are not in the options table; they expire // within 10 minutes and nothing reads their app-secret field any more. $prefix = '_transient_feednetic_oauth_state_'; $names = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( $prefix ) . '%' ) ); foreach ( (array) $names as $name ) { delete_transient( substr( (string) $name, strlen( '_transient_' ) ) ); } $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}feednetic_sources SET status = 'expired', token_encrypted = NULL, token_expires_at = NULL, last_error = %s, updated_at = %s WHERE auth_type <> 'relay'", __( 'Reconnect needed: this account was connected with a method Feednetic no longer supports. Reconnect it to keep the feed updating.', 'feednetic' ), gmdate( 'Y-m-d H:i:s' ) ) ); // phpcs:enable } /** * Creates all plugin tables using dbDelta(). */ private function createTables(): void { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $p = $wpdb->prefix; $c = $wpdb->get_charset_collate(); foreach ( $this->schemaSqls( $p, $c ) as $sql ) { dbDelta( $sql ); } } /** * @param string $p Table prefix. * @param string $c Charset collate string. * @return string[] */ private function schemaSqls( string $p, string $c ): array { return [ $this->sourcesTable( $p, $c ), $this->feedsTable( $p, $c ), $this->feedSourcesTable( $p, $c ), $this->postsTable( $p, $c ), $this->mediaTable( $p, $c ), $this->sourceStatsTable( $p, $c ), $this->feedPostOverridesTable( $p, $c ), ]; } /** @param string $p Prefix. @param string $c Collate. */ private function sourcesTable( string $p, string $c ): string { return 'CREATE TABLE ' . $p . "feednetic_sources (\n" . " id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n" . " network VARCHAR(32) NOT NULL,\n" . " external_id VARCHAR(191) NOT NULL,\n" . " name VARCHAR(191) NOT NULL DEFAULT '',\n" . " username VARCHAR(191) NOT NULL DEFAULT '',\n" . " avatar_url TEXT NULL,\n" . " auth_type VARCHAR(16) NOT NULL DEFAULT 'relay',\n" . " token_encrypted LONGTEXT NULL,\n" . " token_expires_at DATETIME NULL,\n" . " data_access_expires_at DATETIME NULL,\n" . " status VARCHAR(16) NOT NULL DEFAULT 'active',\n" . " last_error TEXT NULL,\n" . " last_fetched_at DATETIME NULL,\n" . " followers_count INT NULL,\n" . " media_count INT NULL,\n" . " biography TEXT NULL,\n" . " profile_url TEXT NULL,\n" . " avatar_path TEXT NULL,\n" . " profile_fetched_at DATETIME NULL,\n" . " meta LONGTEXT NULL,\n" . " created_at DATETIME NOT NULL,\n" . " updated_at DATETIME NOT NULL,\n" . " PRIMARY KEY (id),\n" . " UNIQUE KEY network_external (network, external_id)\n" . ') ENGINE=InnoDB ' . $c . ';'; } /** @param string $p Prefix. @param string $c Collate. */ private function sourceStatsTable( string $p, string $c ): string { return 'CREATE TABLE ' . $p . "feednetic_source_stats (\n" . " source_id BIGINT UNSIGNED NOT NULL,\n" . " stat_date DATE NOT NULL,\n" . " followers_count INT NULL,\n" . " media_count INT NULL,\n" . " PRIMARY KEY (source_id, stat_date)\n" . ') ENGINE=InnoDB ' . $c . ';'; } /** @param string $p Prefix. @param string $c Collate. */ private function feedsTable( string $p, string $c ): string { return 'CREATE TABLE ' . $p . "feednetic_feeds (\n" . " id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n" . " name VARCHAR(191) NOT NULL DEFAULT '',\n" . " status VARCHAR(16) NOT NULL DEFAULT 'publish',\n" . " settings LONGTEXT NOT NULL,\n" . " is_headless TINYINT(1) NOT NULL DEFAULT 0,\n" . " created_at DATETIME NOT NULL,\n" . " updated_at DATETIME NOT NULL,\n" . " PRIMARY KEY (id)\n" . ') ENGINE=InnoDB ' . $c . ';'; } /** @param string $p Prefix. @param string $c Collate. */ private function feedSourcesTable( string $p, string $c ): string { return 'CREATE TABLE ' . $p . "feednetic_feed_sources (\n" . " feed_id BIGINT UNSIGNED NOT NULL,\n" . " source_id BIGINT UNSIGNED NOT NULL,\n" . " stream VARCHAR(64) NOT NULL DEFAULT 'timeline',\n" . " position INT NOT NULL DEFAULT 0,\n" . " PRIMARY KEY (feed_id, source_id, stream)\n" . ') ENGINE=InnoDB ' . $c . ';'; } /** @param string $p Prefix. @param string $c Collate. */ private function postsTable( string $p, string $c ): string { return 'CREATE TABLE ' . $p . "feednetic_posts (\n" . " id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n" . " source_id BIGINT UNSIGNED NOT NULL,\n" . " stream VARCHAR(64) NOT NULL DEFAULT 'timeline',\n" . " network VARCHAR(32) NOT NULL,\n" . " external_id VARCHAR(191) NOT NULL,\n" . " type VARCHAR(16) NOT NULL,\n" . " text LONGTEXT NULL,\n" . " permalink TEXT NOT NULL,\n" . " author_name VARCHAR(191) NULL,\n" . " author_handle VARCHAR(191) NULL,\n" . " author_avatar TEXT NULL,\n" . " published_at DATETIME NOT NULL,\n" . " metrics LONGTEXT NULL,\n" . " extra LONGTEXT NULL,\n" . " is_hidden TINYINT(1) NOT NULL DEFAULT 0,\n" . " is_pinned TINYINT(1) NOT NULL DEFAULT 0,\n" . " is_page_update TINYINT(1) NOT NULL DEFAULT 0,\n" . " linked_object_type VARCHAR(32) NULL,\n" . " linked_object_id VARCHAR(191) NULL,\n" . " shop_link_url TEXT NULL,\n" . " shop_link_label VARCHAR(191) NULL,\n" . " fetched_at DATETIME NOT NULL,\n" . " updated_at DATETIME NOT NULL,\n" . " PRIMARY KEY (id),\n" . " UNIQUE KEY source_stream_external (source_id, stream, external_id),\n" . " KEY source_timeline (source_id, stream, is_hidden, is_page_update, published_at)\n" . ') ENGINE=InnoDB ' . $c . ';'; } /** @param string $p Prefix. @param string $c Collate. */ private function feedPostOverridesTable( string $p, string $c ): string { return 'CREATE TABLE ' . $p . "feednetic_feed_post_overrides (\n" . " feed_id BIGINT UNSIGNED NOT NULL,\n" . " post_id BIGINT UNSIGNED NOT NULL,\n" . " is_hidden TINYINT(1) NOT NULL DEFAULT 0,\n" . " is_pinned TINYINT(1) NOT NULL DEFAULT 0,\n" . " PRIMARY KEY (feed_id, post_id)\n" . ') ENGINE=InnoDB ' . $c . ';'; } /** @param string $p Prefix. @param string $c Collate. */ private function mediaTable( string $p, string $c ): string { return 'CREATE TABLE ' . $p . "feednetic_media (\n" . " id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n" . " post_id BIGINT UNSIGNED NOT NULL,\n" . " position INT NOT NULL DEFAULT 0,\n" . " kind VARCHAR(16) NOT NULL,\n" . " original_url TEXT NOT NULL,\n" . " width INT NULL,\n" . " height INT NULL,\n" . " variants LONGTEXT NULL,\n" . " status VARCHAR(16) NOT NULL DEFAULT 'pending',\n" . " attempts TINYINT NOT NULL DEFAULT 0,\n" . " created_at DATETIME NOT NULL,\n" . " updated_at DATETIME NOT NULL,\n" . " PRIMARY KEY (id),\n" . " KEY post_position (post_id, position)\n" . ') ENGINE=InnoDB ' . $c . ';'; } }