Automattic\WooCommerce\Internal\CLI\Migrator\Lib

ImportSession::create_frontloading_stubspublicWC 1.0

Creates placeholder attachments for the assets to be downloaded in the frontloading stage.

Method of the class: ImportSession{}

No Hooks.

Returns

null. Nothing (null).

Usage

$ImportSession = new ImportSession();
$ImportSession->create_frontloading_stubs( $urls );
$urls(required)
.

ImportSession::create_frontloading_stubs() code WC 10.7.0

public function create_frontloading_stubs( $urls ) {
	global $wpdb;

	foreach ( $urls as $url => $_ ) {
		/**
		 * Check if placeholder with this URL already exists
		 * There's a race condition here – another insert may happen
		 * between the check and the insert.
		 *
		 * @TODO: Explore solutions. A custom table with a UNIQUE constraint
		 * may or may not be an option, depending on the performance impact
		 * on 100GB+ VIP databases.
		 */
		$exists = $wpdb->get_var(
			$wpdb->prepare(
				"SELECT ID FROM $wpdb->posts
			WHERE post_type = 'frontloading_stub'
			AND post_parent = %d
			AND guid = %s
			LIMIT 1",
				$this->post_id,
				$url
			)
		);

		if ( $exists ) {
			continue;
		}

		$post_data        = array(
			'post_type'   => 'frontloading_stub',
			'post_parent' => $this->post_id,
			'post_title'  => basename( $url ),
			'post_status' => self::FRONTLOAD_STATUS_AWAITING_DOWNLOAD,
			'guid'        => $url,
			'meta_input'  => array(
				'original_url' => $url,
				'current_url'  => $url,
				'attempts'     => 0,
				'last_error'   => null,
				'target_path'  => '',
			),
		);
		$insertion_result = wp_insert_post( $post_data );
		if ( is_wp_error( $insertion_result ) ) {
			throw new \Exception( 'Failed to insert frontloading placeholder' );
		}
	}
}