Making WordPress.org


Ignore:
Timestamp:
02/20/2024 01:16:24 AM (9 months ago)
Author:
dd32
Message:

Plugin Directory: Cleanup empty terms regularly.

By having less empty terms we get benefits around query performance and less noise to filter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-meta-sync.php

    r13220 r13223  
    2828        $this->sync_ratings();
    2929        $this->update_tested_up_to();
     30        $this->cleanup_empty_terms();
    3031
    3132        Manager::clear_memory_heavy_variables();
     
    196197        }
    197198    }
     199
     200    /**
     201     * Remove old plugin tags that are no longer in use.
     202     */
     203    public function cleanup_empty_terms() {
     204        $taxonomies = [
     205            'plugin_tags',
     206            'plugin_contributors',
     207            'plugin_committers',
     208            'plugin_support_reps'
     209        ];
     210        foreach ( $taxonomies as $taxonomy ) {
     211            $terms = get_terms( array(
     212                'taxonomy'   => $taxonomy,
     213                'hide_empty' => false,
     214                'count'      => true,
     215            ) );
     216
     217            $terms = array_filter( $terms, function( $term ) {
     218                return $term->count === 0;
     219            } );
     220
     221            foreach ( $terms as $term ) {
     222                wp_delete_term( $term->term_id, $term->taxonomy );
     223            }
     224        }
     225    }
    198226}
Note: See TracChangeset for help on using the changeset viewer.