acf_field_file::format_value_for_jsonldpublicACF 6.8.0

Formats the field value for JSON-LD output.

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

Хуков нет.

Возвращает

mixed.

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

$acf_field_file = new acf_field_file();
$acf_field_file->format_value_for_jsonld( $value, $post_id, $field );
$value(разное) (обязательный)
The value of the field.
$post_id(int|строка) (обязательный)
The ID of the post.
$field(массив) (обязательный)
The field array.

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

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

Код acf_field_file::format_value_for_jsonld() ACF 6.8.8

public function format_value_for_jsonld( $value, $post_id, $field ) {
	if ( empty( $value ) ) {
		return null;
	}

	// Get output format with fallback.
	$output_format = $field['schema_output_format'] ?? '';
	if ( empty( $output_format ) ) {
		$property      = $field['schema_property'] ?? '';
		$output_format = \ACF\AI\GEO\Schema::get_default_output_format( $this->name, $property );
	}

	// Get attachment ID.
	$attachment_id = is_array( $value ) ? ( $value['ID'] ?? 0 ) : (int) $value;
	if ( ! $attachment_id ) {
		return null;
	}

	$url = wp_get_attachment_url( $attachment_id );
	if ( ! $url ) {
		return null;
	}

	// URL format - just return the URL string.
	if ( 'URL' === $output_format ) {
		return $url;
	}

	// MediaObject or DataDownload format.
	$file_object = array(
		'@type'      => $output_format ?: 'MediaObject',
		'contentUrl' => $url,
	);

	// Add name from attachment title.
	$attachment = get_post( $attachment_id );
	if ( $attachment && $attachment->post_title ) {
		$file_object['name'] = $attachment->post_title;
	}

	// Add description if available.
	if ( $attachment && $attachment->post_content ) {
		$file_object['description'] = $attachment->post_content;
	}

	// Add file metadata.
	$file_path = get_attached_file( $attachment_id );
	if ( $file_path && file_exists( $file_path ) ) {
		$file_object['contentSize'] = size_format( filesize( $file_path ) );
	}

	// Add MIME type.
	$mime_type = get_post_mime_type( $attachment_id );
	if ( $mime_type ) {
		$file_object['encodingFormat'] = $mime_type;
	}

	return $file_object;
}