[]]), ]; } public function sanitizeValue($value, array $field) { // The id of an attachment. Anything else (an id that is gone, text) becomes empty, without a PHP warning. $meta = get_post_meta((int) $value); if (! empty($meta['_wp_attachment_metadata']) || ! empty($meta['_wp_attached_file'])) { return $value; } return ''; } public function usesAssets(): bool { return true; } public function enqueue(array $field): void { wp_enqueue_media(); // Versioned by file date (like ColorPicker), not by the plugin version: a changed script under an // unchanged plugin version would otherwise keep being served from the browser cache. wp_enqueue_script( Plugin::PREFIX . 'file-js', Plugin::assetsUrl() . 'js/' . Plugin::PREFIX . 'file.js', ['wp-i18n'], Enqueue::version('js/' . Plugin::PREFIX . 'file.js'), 'all' ); Enqueue::scriptTranslations(Plugin::PREFIX . 'file-js'); wp_enqueue_style( Plugin::PREFIX . 'file-css', Plugin::assetsUrl() . 'css/' . Plugin::PREFIX . 'file.css', [], Enqueue::version('css/' . Plugin::PREFIX . 'file.css'), 'all' ); } /** * A link to the file with its title as text (`text="..."` for another text); only the URL with `link="0"`. */ public function displayValue($value, array $field, array $args): string { $id = Display::attachmentId($value); $url = $id ? (string) wp_get_attachment_url($id) : ''; if ($url === '') { return ''; } if (in_array($args['link'] ?? '', ['0', 'no', 'false'], true)) { return esc_html($url); } return Display::link($url, ($args['text'] ?? '') !== '' ? (string) $args['text'] : get_the_title($id)); } public function bindingValue($value, array $field, string $attribute): ?string { $id = Display::attachmentId($value); if ($id === 0) { return null; } switch ($attribute) { case 'url': return (string) wp_get_attachment_url($id); case 'title': return get_the_title($id); case 'id': return (string) $id; case 'content': case 'text': return $this->displayValue($value, $field, []); } return null; } public function render(array $field, $value): void { // Like the image field, the name is printed hidden either way (see aop-file.js): the script decides // whether to show it, so there is exactly one place ("is a file chosen?") that makes that call. $filename = $value ? basename((string) get_attached_file((int) $value)) : ''; printf('