bbp_admin_repair_freshness

Recaches the last post in every topic and forum.

Description

(array) bbp_admin_repair_freshness(); 

Returns (array)

An array of the status code and the message


Usage

  1. if ( !function_exists( 'bbp_admin_repair_freshness' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/admin/tools.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bbp_admin_repair_freshness(); 
  7.  

Defined (1)

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

/includes/admin/tools.php  
  1. function bbp_admin_repair_freshness() { 
  2. global $wpdb; 
  3.  
  4. $statement = __( 'Recomputing latest post in every topic and forum… %s', bbpress ); 
  5. $result = __( 'Failed!', bbpress ); 
  6.  
  7. // First, delete everything. 
  8. if ( is_wp_error( $wpdb->query( "DELETE FROM `$wpdb->postmeta` WHERE `meta_key` IN ( '_bbp_last_reply_id', '_bbp_last_topic_id', '_bbp_last_active_id', '_bbp_last_active_time' );" ) ) ) 
  9. return array( 1, sprintf( $statement, $result ) ); 
  10.  
  11. // Next, give all the topics with replies the ID their last reply. 
  12. if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) 
  13. ( SELECT `topic`.`ID`, '_bbp_last_reply_id', MAX( `reply`.`ID` ) 
  14. FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent` 
  15. WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply' 
  16. GROUP BY `topic`.`ID` );" ) ) ) 
  17. return array( 2, sprintf( $statement, $result ) ); 
  18.  
  19. // For any remaining topics, give a reply ID of 0. 
  20. if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) 
  21. ( SELECT `ID`, '_bbp_last_reply_id', 0 
  22. FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply` 
  23. ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_reply_id' 
  24. WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) ) 
  25. return array( 3, sprintf( $statement, $result ) ); 
  26.  
  27. // Now we give all the forums with topics the ID their last topic. 
  28. if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) 
  29. ( SELECT `forum`.`ID`, '_bbp_last_topic_id', `topic`.`ID` 
  30. FROM `$wpdb->posts` AS `forum` INNER JOIN `$wpdb->posts` AS `topic` ON `forum`.`ID` = `topic`.`post_parent` 
  31. WHERE `topic`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `forum`.`post_type` = 'forum' AND `topic`.`post_type` = 'topic' 
  32. GROUP BY `forum`.`ID` );" ) ) ) 
  33. return array( 4, sprintf( $statement, $result ) ); 
  34.  
  35. // For any remaining forums, give a topic ID of 0. 
  36. if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) 
  37. ( SELECT `ID`, '_bbp_last_topic_id', 0 
  38. FROM `$wpdb->posts` AS `forum` LEFT JOIN `$wpdb->postmeta` AS `topic` 
  39. ON `forum`.`ID` = `topic`.`post_id` AND `topic`.`meta_key` = '_bbp_last_topic_id' 
  40. WHERE `topic`.`meta_id` IS NULL AND `forum`.`post_type` = 'forum' );" ) ) ) 
  41. return array( 5, sprintf( $statement, $result ) ); 
  42.  
  43. // After that, we give all the topics with replies the ID their last reply (again, this time for a different reason). 
  44. if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) 
  45. ( SELECT `topic`.`ID`, '_bbp_last_active_id', MAX( `reply`.`ID` ) 
  46. FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent` 
  47. WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply' 
  48. GROUP BY `topic`.`ID` );" ) ) ) 
  49. return array( 6, sprintf( $statement, $result ) ); 
  50.  
  51. // For any remaining topics, give a reply ID of themself. 
  52. if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) 
  53. ( SELECT `ID`, '_bbp_last_active_id', `ID` 
  54. FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply` 
  55. ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_id' 
  56. WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) ) 
  57. return array( 7, sprintf( $statement, $result ) ); 
  58.  
  59. // Give topics with replies their last update time. 
  60. if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) 
  61. ( SELECT `topic`.`ID`, '_bbp_last_active_time', MAX( `reply`.`post_date` ) 
  62. FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent` 
  63. WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply' 
  64. GROUP BY `topic`.`ID` );" ) ) ) 
  65. return array( 8, sprintf( $statement, $result ) ); 
  66.  
  67. // Give topics without replies their last update time. 
  68. if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) 
  69. ( SELECT `ID`, '_bbp_last_active_time', `post_date` 
  70. FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply` 
  71. ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_time' 
  72. WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) ) 
  73. return array( 9, sprintf( $statement, $result ) ); 
  74.  
  75. // Forums need to know what their last active item is as well. Now it gets a bit more complex to do in the database. 
  76. $forums = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = 'forum' and `post_status` != 'auto-draft';" ); 
  77. if ( is_wp_error( $forums ) ) 
  78. return array( 10, sprintf( $statement, $result ) ); 
  79.  
  80. // Loop through forums 
  81. foreach ( $forums as $forum_id ) { 
  82. if ( !bbp_is_forum_category( $forum_id ) ) { 
  83. bbp_update_forum( array( 'forum_id' => $forum_id ) ); 
  84.  
  85. // Loop through categories when forums are done 
  86. foreach ( $forums as $forum_id ) { 
  87. if ( bbp_is_forum_category( $forum_id ) ) { 
  88. bbp_update_forum( array( 'forum_id' => $forum_id ) ); 
  89.  
  90. // Complete results 
  91. return array( 0, sprintf( $statement, __( 'Complete!', bbpress ) ) );