register_post_type(); ( new Taxonomy() )->register_taxonomy(); self::maybe_upgrade(); flush_rewrite_rules(); } /** * Idempotent schema/data upgrade. Safe to run on every admin load: it * short-circuits once the stored DB version matches the plugin version, so * existing installs pick up new tables/columns without a reactivation. * * @return void */ public static function maybe_upgrade() { $installed = get_option( self::DB_VERSION_OPTION, '0' ); if ( version_compare( $installed, ACCORDION_FAQ_VERSION, '>=' ) ) { return; } // Analytics storage (events + search terms). dbDelta is idempotent. \AccordionFaq\Analytics\Database::create_table(); self::maybe_migrate(); update_option( self::DB_VERSION_OPTION, ACCORDION_FAQ_VERSION ); } /** * Deactivation callback. * * @return void */ public static function deactivate() { flush_rewrite_rules(); } /** * One-time, idempotent data migration for existing installs. * * @return void */ private static function maybe_migrate() { // Backfill a sensible order value for existing FAQs that have none. self::backfill_order_meta(); } /** * Give existing FAQs an incremental order based on their current date order. * * @return void */ private static function backfill_order_meta() { $query = new \WP_Query( array( 'post_type' => PostType::POST_TYPE, 'post_status' => 'any', 'posts_per_page' => 500, 'orderby' => 'date', 'order' => 'ASC', 'fields' => 'ids', 'no_found_rows' => true, 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- one-time migration. array( 'key' => Meta::META_ORDER, 'compare' => 'NOT EXISTS', ), ), ) ); $order = 1; foreach ( $query->posts as $post_id ) { update_post_meta( $post_id, Meta::META_ORDER, $order ); $order++; } wp_reset_postdata(); } }