@@ -26,7 +26,8 @@ public function run() {
2626 $ this ->migrate_golden_todo_task ();
2727
2828 // Clean up old category taxonomy data.
29- $ this ->cleanup_category_taxonomy ();
29+ // This needs to run on init hook so we can register the taxonomy.
30+ \add_action ( 'init ' , [ $ this , 'cleanup_category_taxonomy ' ], 0 );
3031
3132 // Migrate task priorities to new priority system.
3233 // This needs to run after tasks_manager is initialized (priority 99 on init hook).
@@ -69,77 +70,39 @@ private function migrate_golden_todo_task() {
6970 * Clean up old category taxonomy data.
7071 *
7172 * The prpl_recommendations_category taxonomy has been removed in v1.9.0.
72- * This method removes all related data from the database.
73+ * This method temporarily registers the taxonomy, deletes all its terms,
74+ * then unregisters it.
7375 *
7476 * @return void
7577 */
76- private function cleanup_category_taxonomy () {
77- global $ wpdb ;
78-
79- // Get term taxonomy IDs for the old category taxonomy.
80- $ term_taxonomy_ids = $ wpdb ->get_col ( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
81- $ wpdb ->prepare (
82- 'SELECT term_taxonomy_id FROM %i WHERE taxonomy = %s ' ,
83- $ wpdb ->term_taxonomy , // @phpstan-ignore-line property.nonObject
84- 'prpl_recommendations_category '
85- )
86- );
87-
88- if ( empty ( $ term_taxonomy_ids ) ) {
89- return ; // Nothing to clean up.
90- }
91-
92- // Remove term relationships.
93- $ placeholders = \implode ( ', ' , \array_fill ( 0 , \count ( $ term_taxonomy_ids ), '%d ' ) );
94- $ wpdb ->query ( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
95- $ wpdb ->prepare ( // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- $placeholders contains format specifiers.
96- "DELETE FROM %i WHERE term_taxonomy_id IN ( $ placeholders) " , // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
97- $ wpdb ->term_relationships , // @phpstan-ignore-line property.nonObject
98- ...$ term_taxonomy_ids
99- )
100- );
101-
102- // Get term IDs before deleting term taxonomy entries.
103- $ term_ids = $ wpdb ->get_col ( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
104- $ wpdb ->prepare (
105- 'SELECT term_id FROM %i WHERE taxonomy = %s ' ,
106- $ wpdb ->term_taxonomy , // @phpstan-ignore-line property.nonObject
107- 'prpl_recommendations_category '
108- )
78+ public function cleanup_category_taxonomy () {
79+ // Temporarily register the old taxonomy so we can use WordPress functions.
80+ \register_taxonomy (
81+ 'prpl_recommendations_category ' ,
82+ [ 'prpl_recommendations ' ],
83+ [
84+ 'public ' => false ,
85+ 'hierarchical ' => false ,
86+ ]
10987 );
11088
111- // Remove term taxonomy entries.
112- $ wpdb ->delete ( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
113- $ wpdb ->term_taxonomy , // @phpstan-ignore-line property.nonObject
114- [ 'taxonomy ' => 'prpl_recommendations_category ' ],
115- [ '%s ' ]
89+ // Get all terms in this taxonomy.
90+ $ terms = \get_terms (
91+ [
92+ 'taxonomy ' => 'prpl_recommendations_category ' ,
93+ 'hide_empty ' => false ,
94+ ]
11695 );
11796
118- // Remove orphaned terms that are no longer used by any taxonomy.
119- if ( ! empty ( $ term_ids ) ) {
120- $ term_placeholders = \implode ( ', ' , \array_fill ( 0 , \count ( $ term_ids ), '%d ' ) );
121- $ wpdb ->query ( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
122- $ wpdb ->prepare ( // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- $term_placeholders contains format specifiers.
123- "DELETE FROM %i WHERE term_id IN ( $ term_placeholders) AND term_id NOT IN (SELECT DISTINCT term_id FROM %i) " , // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
124- $ wpdb ->terms , // @phpstan-ignore-line property.nonObject
125- $ wpdb ->term_taxonomy , // @phpstan-ignore-line property.nonObject
126- ...$ term_ids
127- )
128- );
97+ // Delete each term. WordPress will handle all related cleanup automatically.
98+ if ( ! \is_wp_error ( $ terms ) && ! empty ( $ terms ) ) {
99+ foreach ( $ terms as $ term ) {
100+ \wp_delete_term ( $ term ->term_id , 'prpl_recommendations_category ' );
101+ }
129102 }
130103
131- // Clean up term meta for deleted terms.
132- if ( ! empty ( $ term_ids ) ) {
133- $ term_placeholders = \implode ( ', ' , \array_fill ( 0 , \count ( $ term_ids ), '%d ' ) );
134- $ wpdb ->query ( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
135- $ wpdb ->prepare ( // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- $term_placeholders contains format specifiers.
136- "DELETE FROM %i WHERE term_id IN ( $ term_placeholders) AND term_id NOT IN (SELECT DISTINCT term_id FROM %i) " , // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
137- $ wpdb ->termmeta , // @phpstan-ignore-line property.nonObject
138- $ wpdb ->terms , // @phpstan-ignore-line property.nonObject
139- ...$ term_ids
140- )
141- );
142- }
104+ // Unregister the taxonomy.
105+ \unregister_taxonomy ( 'prpl_recommendations_category ' );
143106
144107 // Clear WordPress caches.
145108 \wp_cache_flush ();
0 commit comments