_wp_batch_split_terms

Splits a batch of shared taxonomy terms.

Description

_wp_batch_split_terms(); 

Usage

  1. if ( !function_exists( '_wp_batch_split_terms' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = _wp_batch_split_terms(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/taxonomy.php  
  1. function _wp_batch_split_terms() { 
  2. global $wpdb; 
  3.  
  4. $lock_name = 'term_split.lock'; 
  5.  
  6. // Try to lock. 
  7. $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) ); 
  8.  
  9. if ( ! $lock_result ) { 
  10. $lock_result = get_option( $lock_name ); 
  11.  
  12. // Bail if we were unable to create a lock, or if the existing lock is still valid. 
  13. if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) { 
  14. wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' ); 
  15. return; 
  16.  
  17. // Update the lock, as by this point we've definitely got a lock, just need to fire the actions. 
  18. update_option( $lock_name, time() ); 
  19.  
  20. // Get a list of shared terms (those with more than one associated row in term_taxonomy). 
  21. $shared_terms = $wpdb->get_results( 
  22. "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt 
  23. LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id 
  24. GROUP BY t.term_id 
  25. HAVING term_tt_count > 1 
  26. LIMIT 10" 
  27. ); 
  28.  
  29. // No more terms, we're done here. 
  30. if ( ! $shared_terms ) { 
  31. update_option( 'finished_splitting_shared_terms', true ); 
  32. delete_option( $lock_name ); 
  33. return; 
  34.  
  35. // Shared terms found? We'll need to run this script again. 
  36. wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' ); 
  37.  
  38. // Rekey shared term array for faster lookups. 
  39. $_shared_terms = array(); 
  40. foreach ( $shared_terms as $shared_term ) { 
  41. $term_id = intval( $shared_term->term_id ); 
  42. $_shared_terms[ $term_id ] = $shared_term; 
  43. $shared_terms = $_shared_terms; 
  44.  
  45. // Get term taxonomy data for all shared terms. 
  46. $shared_term_ids = implode( ', ', array_keys( $shared_terms ) ); 
  47. $shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" ); 
  48.  
  49. // Split term data recording is slow, so we do it just once, outside the loop. 
  50. $split_term_data = get_option( '_split_terms', array() ); 
  51. $skipped_first_term = $taxonomies = array(); 
  52. foreach ( $shared_tts as $shared_tt ) { 
  53. $term_id = intval( $shared_tt->term_id ); 
  54.  
  55. // Don't split the first tt belonging to a given term_id. 
  56. if ( ! isset( $skipped_first_term[ $term_id ] ) ) { 
  57. $skipped_first_term[ $term_id ] = 1; 
  58. continue; 
  59.  
  60. if ( ! isset( $split_term_data[ $term_id ] ) ) { 
  61. $split_term_data[ $term_id ] = array(); 
  62.  
  63. // Keep track of taxonomies whose hierarchies need flushing. 
  64. if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) { 
  65. $taxonomies[ $shared_tt->taxonomy ] = 1; 
  66.  
  67. // Split the term. 
  68. $split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false ); 
  69.  
  70. // Rebuild the cached hierarchy for each affected taxonomy. 
  71. foreach ( array_keys( $taxonomies ) as $tax ) { 
  72. delete_option( "{$tax}_children" ); 
  73.  
  74. update_option( '_split_terms', $split_term_data ); 
  75.  
  76. delete_option( $lock_name );