WC_Install::create_placeholder_image()private staticWC 3.5.0

Create a placeholder image in the media library.

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

Хуков нет.

Возвращает

null. Ничего (null).

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

$result = WC_Install::create_placeholder_image();

Список изменений

С версии 3.5.0 Введена.

Код WC_Install::create_placeholder_image() WC 8.7.0

private static function create_placeholder_image() {
	$placeholder_image = get_option( 'woocommerce_placeholder_image', 0 );

	// Validate current setting if set. If set, return.
	if ( ! empty( $placeholder_image ) ) {
		if ( ! is_numeric( $placeholder_image ) ) {
			return;
		} elseif ( $placeholder_image && wp_attachment_is_image( $placeholder_image ) ) {
			return;
		}
	}

	$upload_dir = wp_upload_dir();
	$source     = WC()->plugin_path() . '/assets/images/placeholder-attachment.png';
	$filename   = $upload_dir['basedir'] . '/woocommerce-placeholder.png';

	if ( ! file_exists( $filename ) ) {
		copy( $source, $filename ); // @codingStandardsIgnoreLine.
	}

	if ( ! file_exists( $filename ) ) {
		update_option( 'woocommerce_placeholder_image', 0 );
		return;
	}

	$filetype   = wp_check_filetype( basename( $filename ), null );
	$attachment = array(
		'guid'           => $upload_dir['url'] . '/' . basename( $filename ),
		'post_mime_type' => $filetype['type'],
		'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
		'post_content'   => '',
		'post_status'    => 'inherit',
	);

	$attach_id = wp_insert_attachment( $attachment, $filename );
	if ( is_wp_error( $attach_id ) ) {
		update_option( 'woocommerce_placeholder_image', 0 );
		return;
	}

	update_option( 'woocommerce_placeholder_image', $attach_id );

	// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
	require_once ABSPATH . 'wp-admin/includes/image.php';

	// Generate the metadata for the attachment, and update the database record.
	$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
	wp_update_attachment_metadata( $attach_id, $attach_data );
}