WP_REST_Attachments_Controller::get_attachment_filesize │ protected │ WP 7.0.0

Gets the attachment's file size in bytes.

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

Хуков нет.

Возвращает

int|null. Attachment file size in bytes, or null if not available.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_attachment_filesize( $attachment_id ): ?int;
$attachment_id(int) (обязательный)
Attachment ID.

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

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

Код WP_REST_Attachments_Controller::get_attachment_filesize() WP 7.1.2

protected function get_attachment_filesize( int $attachment_id ): ?int {
	$meta = wp_get_attachment_metadata( $attachment_id );

	if ( isset( $meta['filesize'] ) && is_numeric( $meta['filesize'] ) && $meta['filesize'] > 0 ) {
		return (int) $meta['filesize'];
	}

	$original_path = wp_get_original_image_path( $attachment_id );
	$attached_file = $original_path ? $original_path : get_attached_file( $attachment_id );

	if ( is_string( $attached_file ) && is_readable( $attached_file ) ) {
		return wp_filesize( $attached_file );
	}

	return null;
}