bp_blogs_record_existing_blogs

Populate the BP blogs table with existing blogs.

Description

bp_blogs_record_existing_blogs( (array) $args = array() ); 

Warning: By default, this will remove all existing records from the BP blogs and blogmeta tables before re-populating the tables.

Parameters (1)

0. $args — Optional. (array) => array()
Array of arguments.

Options

  • offset (int) => 0

    The offset to use.

  • limit (int) => 0

    The number of blogs to record at one time.

  • blog_ids (array) => null

    Blog IDs to record. If empty, all blogs will be recorded.

array(

    /**
     * The offset to use.
     *
     * @type int
     */
    'offset' => 0,

    /**
     * The number of blogs to record at one time.
     *
     * @type int
     */
    'limit' => 0,

    /**
     * Blog IDs to record. If empty, all blogs will be recorded.
     *
     * @type array
     * @default null
     */
    'blog_ids' => null
);        


Usage

  1. if ( !function_exists( 'bp_blogs_record_existing_blogs' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-blogs/bp-blogs-functions.php'; 
  3.  
  4. // Array of arguments. 
  5. $args = array( 
  6. 'offset' => 0, 
  7. 'limit' => 0, 
  8. 'blog_ids' => null 
  9. ); 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = bp_blogs_record_existing_blogs($args); 
  13.  

Defined (1)

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

/bp-blogs/bp-blogs-functions.php  
  1. function bp_blogs_record_existing_blogs( $args = array() ) { 
  2. global $wpdb; 
  3.  
  4. // Query for all sites in network. 
  5. $r = bp_parse_args( $args, array( 
  6. 'offset' => false === bp_get_option( '_bp_record_blogs_offset' ) ? 0 : bp_get_option( '_bp_record_blogs_offset' ),  
  7. 'limit' => 50,  
  8. 'blog_ids' => array(),  
  9. 'site_id' => $wpdb->siteid 
  10. ), 'record_existing_blogs' ); 
  11.  
  12. // Truncate all BP blogs tables if starting fresh 
  13. if ( empty( $r['offset'] ) && empty( $r['blog_ids'] ) ) { 
  14. $bp =buddypress); 
  15.  
  16. // Truncate user blogs table 
  17. $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name}" ); 
  18. if ( is_wp_error( $truncate ) ) { 
  19. return false; 
  20.  
  21. // Truncate user blogmeta table 
  22. $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name_blogmeta}" ); 
  23. if ( is_wp_error( $truncate ) ) { 
  24. return false; 
  25.  
  26. // Multisite 
  27. if ( is_multisite() ) { 
  28. $sql = array(); 
  29. $sql['select'] = $wpdb->prepare( "SELECT blog_id, last_updated FROM {$wpdb->base_prefix}blogs WHERE mature = 0 AND spam = 0 AND deleted = 0 AND site_id = %d", $r['site_id'] ); 
  30.  
  31. // Omit root blog if large network 
  32. if ( wp_is_large_network( 'users' ) ) { 
  33. $sql['omit_root_blog'] = $wpdb->prepare( "AND blog_id != %d", bp_get_root_blog_id() ); 
  34.  
  35. // Filter by selected blog IDs 
  36. if ( ! empty( $r['blog_ids'] ) ) { 
  37. $in = implode( ', ', wp_parse_id_list( $r['blog_ids'] ) ); 
  38. $sql['in'] = "AND blog_id IN ({$in})"; 
  39.  
  40. $sql['orderby'] = 'ORDER BY blog_id ASC'; 
  41.  
  42. $sql['limit'] = $wpdb->prepare( "LIMIT %d", $r['limit'] ); 
  43.  
  44. if ( ! empty( $r['offset'] ) ) { 
  45. $sql['offset'] = $wpdb->prepare( "OFFSET %d", $r['offset'] ); 
  46.  
  47. $blogs = $wpdb->get_results( implode( ' ', $sql ) ); 
  48.  
  49. // Record a single site. 
  50. } else { 
  51. // Just record blog for the current user only. 
  52. $record = bp_blogs_record_blog( $wpdb->blogid, get_current_user_id(), true ); 
  53.  
  54. if ( false === $record ) { 
  55. return false; 
  56. } else { 
  57. return true; 
  58.  
  59. // Bail if there are no blogs 
  60. if ( empty( $blogs ) ) { 
  61. // Make sure we remove our offset marker 
  62. if ( is_multisite() ) { 
  63. bp_delete_option( '_bp_record_blogs_offset' ); 
  64.  
  65. return false; 
  66.  
  67. // Loop through users of blogs and record the relationship. 
  68. foreach ( (array) $blogs as $blog ) { 
  69.  
  70. // Ensure that the cache is clear after the table TRUNCATE above. 
  71. wp_cache_delete( $blog->blog_id, 'blog_meta' ); 
  72.  
  73. // Get all users. 
  74. $users = get_users( array( 
  75. 'blog_id' => $blog->blog_id,  
  76. 'fields' => 'ID' 
  77. ) ); 
  78.  
  79. // Continue on if no users exist for this site (how did this happen?). 
  80. if ( empty( $users ) ) { 
  81. continue; 
  82.  
  83. // Loop through users and record their relationship to this blog. 
  84. foreach ( (array) $users as $user_id ) { 
  85. bp_blogs_add_user_to_blog( $user_id, false, $blog->blog_id ); 
  86.  
  87. // Clear cache 
  88. bp_blogs_clear_blog_object_cache( $blog->blog_id, $user_id ); 
  89.  
  90. // Update blog last activity timestamp 
  91. if ( ! empty( $blog->last_updated ) && false !== strtotime( $blog->last_updated ) ) { 
  92. bp_blogs_update_blogmeta( $blog->blog_id, 'last_activity', $blog->last_updated ); 
  93.  
  94. // See if we need to do this again 
  95. if ( is_multisite() && empty( $r['blog_ids'] ) ) { 
  96. $sql['offset'] = $wpdb->prepare( " OFFSET %d", $r['limit'] + $r['offset'] ); 
  97.  
  98. // Check if there are more blogs to record 
  99. $blog_ids = $wpdb->get_results( implode( ' ', $sql ) ); 
  100.  
  101. // We have more blogs; record offset and re-run function 
  102. if ( ! empty( $blog_ids ) ) { 
  103. bp_update_option( '_bp_record_blogs_offset', $r['limit'] + $r['offset'] ); 
  104. bp_blogs_record_existing_blogs( array( 
  105. 'offset' => $r['limit'] + $r['offset'],  
  106. 'limit' => $r['limit'],  
  107. 'blog_ids' => $r['blog_ids'],  
  108. 'site_id' => $r['site_id'] 
  109. ) ); 
  110.  
  111. // Bail since we have more blogs to record. 
  112. return; 
  113.  
  114. // No more blogs; delete offset marker 
  115. } else { 
  116. bp_delete_option( '_bp_record_blogs_offset' ); 
  117.  
  118. /** 
  119. * Fires after the BP blogs tables have been populated with existing blogs. 
  120. * @since 2.4.0 
  121. */ 
  122.  
  123. // No errors. 
  124. return true;