|
<?php |
|
|
|
/** |
|
* Converts bytes into human readable file size. |
|
* |
|
* @param string $bytes |
|
* @return string human readable file size (2,87 МB) |
|
* Based on Mogilev Arseny’s code. Thank you! |
|
* @link https://secure.php.net/manual/en/function.filesize.php#112996 |
|
*/ |
|
function FileSizeConvert($bytes, $abbr = false) |
|
{ |
|
// Replace 'twentynineteen' by your own text domain |
|
global $text_domain; if (!$text_domain && !isset($text_domain)) $text_domain = 'twentynineteen'; |
|
|
|
$bytes = floatval($bytes); |
|
$arBytes = array( |
|
0 => array( |
|
"unit" => __('TB', $text_domain), |
|
"value" => pow(1024, 4), |
|
"singular" => __('terabyte', $text_domain), |
|
"plural" => __('terabytes', $text_domain) |
|
), |
|
1 => array( |
|
"unit" => __('GB', $text_domain), |
|
"value" => pow(1024, 3), |
|
"singular" => __('gigabyte', $text_domain), |
|
"plural" => __('gigabytes', $text_domain) |
|
), |
|
2 => array( |
|
"unit" => __('MB', $text_domain), |
|
"value" => pow(1024, 2), |
|
"singular" => __('megabyte', $text_domain), |
|
"plural" => __('megabytes', $text_domain) |
|
), |
|
3 => array( |
|
"unit" => __('KB', $text_domain), |
|
"value" => 1024, |
|
"singular" => __('kilobyte', $text_domain), |
|
"plural" => __('kilobytes', $text_domain) |
|
), |
|
4 => array( |
|
"unit" => __('B', $text_domain), |
|
"value" => 1, |
|
"singular" => __('byte', $text_domain), |
|
"plural" => __('bytes', $text_domain) |
|
), |
|
); |
|
|
|
foreach($arBytes as $arItem) |
|
{ |
|
if($bytes >= $arItem["value"]) |
|
{ |
|
$result = $bytes / $arItem["value"]; |
|
$rounded_result = round($result, 2); |
|
|
|
if ($rounded_result > 2) : |
|
if ($abbr) : |
|
$result = str_replace('.', ',' , strval($rounded_result)).' <abbr title="'. $arItem['plural'] .'">'. $arItem['unit'] .'</abbr>'; |
|
else : |
|
$result = str_replace('.', ',' , strval($rounded_result)).' '. $arItem['plural']; |
|
endif; |
|
else : |
|
if ($abbr) : |
|
$result = str_replace('.', ',' , strval($rounded_result)).' <abbr title="'. $arItem['singular'] .'">'. $arItem['unit'] .'</abbr>'; |
|
else : |
|
$result = str_replace('.', ',' , strval($rounded_result)).' '. $arItem['singular']; |
|
endif; |
|
endif; |
|
|
|
break; |
|
} |
|
} |
|
return $result; |
|
} |
|
|
|
/** |
|
* Display accessible download link for attachments, featuring file size and MIME type. |
|
* Example: [a11y_file id="1800"] where `1800` is the attachment ID. |
|
* @param title (bool) Default to 0. Use title="1" to keep a short link text, and display the size and MIME type in the link title attribute. Example: [a11y_file id="1800" title="1"] |
|
* @param link (bool) Default to 1. If for some obscure reason you don’t want to insert a link, use link="0" in the shortcode. Example: [a11y_file id="1800" link="0"] |
|
*/ |
|
function a42_a11y_files( $atts, $content = null ) { |
|
|
|
extract( shortcode_atts( array( |
|
'id' => '', |
|
'link' => 1, |
|
'title' => 0 |
|
), $atts ) ); |
|
|
|
if ($link === 'false') $link = false; |
|
$link = (bool) $link; |
|
|
|
if ($title === 'false') $title = false; |
|
$title = (bool) $title; |
|
|
|
// Replace 'twentynineteen' by your own text domain |
|
global $text_domain; if (!$text_domain && !isset($text_domain)) $text_domain = 'twentynineteen'; |
|
|
|
// Check if the ID matches any attachment |
|
$is_attachment = get_post_type($id) == 'attachment' ? true : false; |
|
|
|
// ID is mandatory |
|
if ($id && $is_attachment) : |
|
|
|
$a11y_file = ''; |
|
|
|
// Get file |
|
$file = get_attached_file($id); |
|
|
|
// Set separators |
|
$before = __('(', $text_domain); |
|
$sep = __(', ', $text_domain); |
|
$after = __(')', $text_domain); |
|
|
|
// Get file URL |
|
if ($link) : |
|
$file_url = wp_get_attachment_url($id); |
|
endif; |
|
|
|
// Get file title |
|
$file_title = sanitize_text_field(get_the_title($id)); |
|
|
|
// Get file size |
|
$file_size = filesize($file); |
|
$clean_file_size = FileSizeConvert($file_size); |
|
$clean_file_size_with_abbr = FileSizeConvert($file_size, true); |
|
|
|
// Get file MIME type |
|
$file_mime = get_post_mime_type($id); |
|
$clean_file_mime_array = explode('/', $file_mime); |
|
$clean_file_mime = $clean_file_mime_array[1]; |
|
|
|
// Display result |
|
if ($clean_file_mime_array && !empty($clean_file_mime_array)) : |
|
|
|
// Open link |
|
if ($link && $file_url) : |
|
$a11y_file .= '<a href="'.esc_url($file_url).'"'; |
|
|
|
if ($title) : |
|
$a11y_file .= ' title="' . $file_title . ' ' . $before . $clean_file_size . $sep . $clean_file_mime . $after .'"'; |
|
endif; |
|
|
|
$a11y_file .= '>'; |
|
|
|
endif; |
|
|
|
// RTL |
|
//if (is_rtl()) : |
|
|
|
// @todo |
|
|
|
// LTR |
|
//else : |
|
|
|
$a11y_file .= $file_title; |
|
|
|
if (!$title) : |
|
$a11y_file .= ' ' . $before . $clean_file_size_with_abbr . $sep . $clean_file_mime . $after; |
|
endif; |
|
|
|
//endif; |
|
|
|
// Close link |
|
if ($link && $file_url) : |
|
$a11y_file .= '</a>'; |
|
endif; |
|
|
|
endif; |
|
|
|
endif; |
|
|
|
return $a11y_file; |
|
|
|
} |
|
add_shortcode('a11y_file', 'a42_a11y_files'); |