Automattic\WooCommerce\Admin

DataSourcePoller::read_data_source()protected staticWC 1.0

Read a single data source and return the read specs

Method of the class: DataSourcePoller{}

No Hooks.

Return

Array. The specs that have been read from the data source.

Usage

$result = DataSourcePoller::read_data_source( $url );
$url(string) (required)
The URL to read the specs from.

DataSourcePoller::read_data_source() code WC 8.6.1

protected static function read_data_source( $url ) {
	$logger_context = array( 'source' => $url );
	$logger         = self::get_logger();
	$response       = wp_remote_get(
		add_query_arg(
			'locale',
			get_user_locale(),
			$url
		),
		array(
			'user-agent' => 'WooCommerce/' . WC_VERSION . '; ' . home_url( '/' ),
		)
	);

	if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
		$logger->error(
			'Error getting data feed',
			$logger_context
		);
		// phpcs:ignore
		$logger->error( print_r( $response, true ), $logger_context );

		return [];
	}

	$body  = $response['body'];
	$specs = json_decode( $body );

	if ( $specs === null ) {
		$logger->error(
			'Empty response in data feed',
			$logger_context
		);

		return [];
	}

	if ( ! is_array( $specs ) ) {
		$logger->error(
			'Data feed is not an array',
			$logger_context
		);

		return [];
	}

	return $specs;
}