Automattic\WooCommerce\Internal\CLI\Migrator\Platforms\Webflow

WebflowFetcher::load_collection_items_mapprivateWC 1.0

Page through a CMS collection and build an id => {name, slug} map.

Method of the class: WebflowFetcher{}

No Hooks.

Returns

Array.

Usage

// private - for code of main (parent) class only
$result = $this->load_collection_items_map( $collection_id ): array;
$collection_id(string) (required)
The Webflow CMS collection ID.

WebflowFetcher::load_collection_items_map() code WC 11.0.1

private function load_collection_items_map( string $collection_id ): array {
	$map       = array();
	$offset    = 0;
	$page_size = self::MAX_PAGE_SIZE;
	$max_pages = 100;
	// Safety net to avoid runaway loops on broken pagination.
	$page_index = 0;

	while ( $page_index < $max_pages ) {
		$response = $this->webflow_client->rest_request(
			"/collections/{$collection_id}/items",
			array(
				'limit'  => $page_size,
				'offset' => $offset,
			)
		);

		if ( is_wp_error( $response ) ) {
			// @phpstan-ignore-next-line class.notFound
			\WP_CLI::debug( 'Could not load Webflow category items: ' . $response->get_error_message() );
			break;
		}

		$items = ( is_object( $response ) && isset( $response->items ) && is_array( $response->items ) ) ? $response->items : array();
		if ( empty( $items ) ) {
			break;
		}

		foreach ( $items as $item ) {
			if ( ! is_object( $item ) || empty( $item->id ) ) {
				continue;
			}
			$field_data = isset( $item->fieldData ) && is_object( $item->fieldData ) ? $item->fieldData : null; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Webflow API uses camelCase.
			if ( null === $field_data ) {
				continue;
			}

			$name = isset( $field_data->name ) ? (string) $field_data->name : '';
			$slug = isset( $field_data->slug ) ? (string) $field_data->slug : sanitize_title( $name );

			if ( '' === $name ) {
				continue;
			}

			$map[ (string) $item->id ] = array(
				'name' => $name,
				'slug' => $slug,
			);
		}

		$total   = isset( $response->pagination->total ) ? (int) $response->pagination->total : ( $offset + count( $items ) );
		$offset += count( $items );
		if ( $offset >= $total ) {
			break;
		}

		++$page_index;
	}

	return $map;
}