WC_Product_Importer::get_attachment_id_from_url()publicWC 1.0

Get attachment ID.

Метод класса: WC_Product_Importer{}

Хуков нет.

Возвращает

int.

Использование

$WC_Product_Importer = new WC_Product_Importer();
$WC_Product_Importer->get_attachment_id_from_url( $url, $product_id );
$url(строка) (обязательный)
Attachment URL.
$product_id(int) (обязательный)
Product ID.

Код WC_Product_Importer::get_attachment_id_from_url() WC 8.7.0

public function get_attachment_id_from_url( $url, $product_id ) {
	if ( empty( $url ) ) {
		return 0;
	}

	$id         = 0;
	$upload_dir = wp_upload_dir( null, false );
	$base_url   = $upload_dir['baseurl'] . '/';

	// Check first if attachment is inside the WordPress uploads directory, or we're given a filename only.
	if ( false !== strpos( $url, $base_url ) || false === strpos( $url, '://' ) ) {
		// Search for yyyy/mm/slug.extension or slug.extension - remove the base URL.
		$file = str_replace( $base_url, '', $url );
		$args = array(
			'post_type'   => 'attachment',
			'post_status' => 'any',
			'fields'      => 'ids',
			'meta_query'  => array( // @codingStandardsIgnoreLine.
				'relation' => 'OR',
				array(
					'key'     => '_wp_attached_file',
					'value'   => '^' . $file,
					'compare' => 'REGEXP',
				),
				array(
					'key'     => '_wp_attached_file',
					'value'   => '/' . $file,
					'compare' => 'LIKE',
				),
				array(
					'key'     => '_wc_attachment_source',
					'value'   => '/' . $file,
					'compare' => 'LIKE',
				),
			),
		);
	} else {
		// This is an external URL, so compare to source.
		$args = array(
			'post_type'   => 'attachment',
			'post_status' => 'any',
			'fields'      => 'ids',
			'meta_query'  => array( // @codingStandardsIgnoreLine.
				array(
					'value' => $url,
					'key'   => '_wc_attachment_source',
				),
			),
		);
	}

	$ids = get_posts( $args ); // @codingStandardsIgnoreLine.

	if ( $ids ) {
		$id = current( $ids );
	}

	// Upload if attachment does not exists.
	if ( ! $id && stristr( $url, '://' ) ) {
		$upload = wc_rest_upload_image_from_url( $url );

		if ( is_wp_error( $upload ) ) {
			throw new Exception( $upload->get_error_message(), 400 );
		}

		$id = wc_rest_set_uploaded_image_as_attachment( $upload, $product_id );

		if ( ! wp_attachment_is_image( $id ) ) {
			/* translators: %s: image URL */
			throw new Exception( sprintf( __( 'Not able to attach "%s".', 'woocommerce' ), $url ), 400 );
		}

		// Save attachment source for future reference.
		update_post_meta( $id, '_wc_attachment_source', $url );
	}

	if ( ! $id ) {
		/* translators: %s: image URL */
		throw new Exception( sprintf( __( 'Unable to use image "%s".', 'woocommerce' ), $url ), 400 );
	}

	return $id;
}