add_cap( 'manage_pixelpair' ); } } ); } /** * Initialize at most 100 sites, then schedule the next batch. * * @param int $network_id Network ID. * @param int $offset Number of sites already processed. */ public static function initialize_network( int $network_id, int $offset = 0 ): void { /** Site IDs returned by the fields=ids query. * * @var list $ids */ $ids = get_sites( array( 'network_id' => $network_id, 'fields' => 'ids', 'number' => 100, 'offset' => $offset, 'orderby' => 'id', 'order' => 'ASC', ) ); foreach ( $ids as $blog_id ) { self::initialize_site( (int) $blog_id ); } if ( 100 === count( $ids ) ) { Sites::on_site( (int) get_main_site_id( $network_id ), static function () use ( $network_id, $offset ): void { $args = array( $network_id, $offset + 100 ); if ( ! wp_next_scheduled( 'pixelpair_initialize_network', $args ) ) { wp_schedule_single_event( time() + 10, 'pixelpair_initialize_network', $args ); } } ); } } /** * Initialize a new site only when this plugin is active for its network. * * @param WP_Site $site The fully initialized site. */ public static function on_created_site( WP_Site $site ): void { $active = get_network_option( (int) $site->network_id, 'active_sitewide_plugins', array() ); if ( is_array( $active ) && isset( $active[ plugin_basename( PIXELPAIR_FILE ) ] ) ) { self::initialize_site( (int) $site->blog_id ); } } }