bb_recount_tag_delete_empty

The BuddyPress bb recount tag delete empty function.

Description

bb_recount_tag_delete_empty(); 

Usage

  1. if ( !function_exists( 'bb_recount_tag_delete_empty' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-admin/includes/functions.bb-recount.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bb_recount_tag_delete_empty(); 
  7.  

Defined (1)

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

/bp-forums/bbpress/bb-admin/includes/functions.bb-recount.php  
  1. function bb_recount_tag_delete_empty() 
  2. global $bbdb; 
  3.  
  4. $statement = __( 'Deleting topic tags with no topics… %s' ); 
  5. $result = __( 'Failed!' ); 
  6.  
  7. static $run_once; 
  8. if ( isset( $run_once ) ) { 
  9. if ($run_once > 0) { 
  10. $exit = sprintf( __( 'failure (returned code %s)' ), $run_once ); 
  11. } else { 
  12. $exit = __( 'success' ); 
  13. $result = sprintf( __( 'Already run with %s.' ), $exit ); 
  14. return array( $run_once, sprintf( $statement, $result ) ); 
  15.  
  16. // Get all topic ids 
  17. $sql_topics = "SELECT `topic_id` FROM $bbdb->topics ORDER BY `topic_id`;"; 
  18. $topics = $bbdb->get_results( $sql_topics ); 
  19. if ( is_wp_error( $topics ) ) { 
  20. $result = __('No topics found.'); 
  21. $run_once = 1; 
  22. return array( 1, sprintf( $statement, $result ) ); 
  23. $topic_ids = array(); 
  24. foreach ($topics as $topic) { 
  25. $topic_ids[] = $topic->topic_id; 
  26.  
  27. // Get all topic tag term relationships without a valid topic id 
  28. $in_topic_ids = implode(', ', $topic_ids); 
  29. $sql_bad_term_relationships = "SELECT 
  30. `$bbdb->term_taxonomy`.`term_taxonomy_id`,  
  31. `$bbdb->term_taxonomy`.`term_id`,  
  32. `$bbdb->term_relationships`.`object_id` 
  33. FROM `$bbdb->term_relationships` 
  34. JOIN `$bbdb->term_taxonomy` 
  35. ON `$bbdb->term_taxonomy`.`term_taxonomy_id` = `$bbdb->term_relationships`.`term_taxonomy_id` 
  36. WHERE 
  37. `$bbdb->term_taxonomy`.`taxonomy` = 'bb_topic_tag' AND 
  38. `$bbdb->term_relationships`.`object_id` NOT IN ($in_topic_ids) 
  39. ORDER BY 
  40. `$bbdb->term_relationships`.`object_id`,  
  41. `$bbdb->term_taxonomy`.`term_id`,  
  42. `$bbdb->term_taxonomy`.`term_taxonomy_id`;"; 
  43.  
  44. $bad_term_relationships = $bbdb->get_results( $sql_bad_term_relationships ); 
  45. if ( is_wp_error( $bad_term_relationships ) || !is_array( $bad_term_relationships ) ) { 
  46. $run_once = 2; 
  47. return array( 2, sprintf( $statement, $result ) ); 
  48.  
  49. // Delete those bad term relationships 
  50. if ( !empty( $bad_term_relationships ) ) { 
  51. $values = array(); 
  52. foreach ( $bad_term_relationships as $bad_term_relationship ) { 
  53. $values[] = '(`object_id` = ' . $bad_term_relationship->object_id . ' AND `term_taxonomy_id` = ' . $bad_term_relationship->term_taxonomy_id . ')'; 
  54. if ( !empty( $values ) ) { 
  55. $values = join(' OR ', $values); 
  56. $sql_bad_term_relationships_delete = "DELETE 
  57. FROM `$bbdb->term_relationships` 
  58. WHERE $values;"; 
  59. if ( is_wp_error( $bbdb->query( $sql_bad_term_relationships_delete ) ) ) { 
  60. $run_once = 3; 
  61. return array( 3, sprintf( $statement, $result ) ); 
  62.  
  63. // Now get all term taxonomy ids with term relationships 
  64. $sql_term_relationships = "SELECT `term_taxonomy_id` FROM $bbdb->term_relationships ORDER BY `term_taxonomy_id`;"; 
  65. $term_taxonomy_ids = $bbdb->get_col($sql_term_relationships); 
  66. if ( is_wp_error( $term_taxonomy_ids ) ) { 
  67. $run_once = 4; 
  68. return array( 4, sprintf( $statement, $result ) ); 
  69. $term_taxonomy_ids = array_unique( $term_taxonomy_ids ); 
  70.  
  71. // Delete topic tags that don't have any term relationships 
  72. if ( !empty( $term_taxonomy_ids ) ) { 
  73. $in_term_taxonomy_ids = implode(', ', $term_taxonomy_ids); 
  74. $sql_delete_term_relationships = "DELETE 
  75. FROM $bbdb->term_taxonomy 
  76. WHERE 
  77. `taxonomy` = 'bb_topic_tag' AND 
  78. `term_taxonomy_id` NOT IN ($in_term_taxonomy_ids);"; 
  79. if ( is_wp_error( $bbdb->query( $sql_delete_term_relationships ) ) ) { 
  80. $run_once = 5; 
  81. return array( 5, sprintf( $statement, $result ) ); 
  82.  
  83. // Get all valid term ids 
  84. $sql_terms = "SELECT `term_id` FROM $bbdb->term_taxonomy ORDER BY `term_id`;"; 
  85. $term_ids = $bbdb->get_col($sql_terms); 
  86. if ( is_wp_error( $term_ids ) ) { 
  87. $run_once = 6; 
  88. return array( 6, sprintf( $statement, $result ) ); 
  89. $term_ids = array_unique( $term_ids ); 
  90.  
  91. // Delete terms that don't have any associated term taxonomies 
  92. if ( !empty( $term_ids ) ) { 
  93. $in_term_ids = implode(', ', $term_ids); 
  94. $sql_delete_terms = "DELETE 
  95. FROM $bbdb->terms 
  96. WHERE 
  97. `term_id` NOT IN ($in_term_ids);"; 
  98. if ( is_wp_error( $bbdb->query( $sql_delete_terms ) ) ) { 
  99. $run_once = 7; 
  100. return array( 7, sprintf( $statement, $result ) ); 
  101.  
  102. $result = __( 'Complete!' ); 
  103. $run_once = 0; 
  104. return array( 0, sprintf( $statement, $result ) );