• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

timber / timber / 5690593717

pending completion
5690593717

Pull #1617

github

web-flow
Merge f587ceffa into b563d274e
Pull Request #1617: 2.x

4433 of 4433 new or added lines in 57 files covered. (100.0%)

3931 of 4433 relevant lines covered (88.68%)

58.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.36
/src/Attachment.php
1
<?php
2

3
namespace Timber;
4

5
use Timber\Factory\PostFactory;
6

7
/**
8
 * Class Attachment
9
 *
10
 * Objects of this class represent WordPress attachments. This is the basis that `Timber\Image`
11
 * objects build upon.
12
 *
13
 * @api
14
 * @since 2.0.0
15
 */
16
class Attachment extends Post
17
{
18
    /**
19
     * Representation.
20
     *
21
     * @var string What does this class represent in WordPress terms?
22
     */
23
    public static $representation = 'attachment';
24

25
    /**
26
     * File.
27
     *
28
     * @api
29
     * @var mixed
30
     */
31
    public $file;
32

33
    /**
34
     * File location.
35
     *
36
     * @api
37
     * @var string The absolute path to the attachmend file in the filesystem
38
     *             (Example: `/var/www/htdocs/wp-content/uploads/2015/08/my-pic.jpg`)
39
     */
40
    public $file_loc;
41

42
    /**
43
     * Formatted file size.
44
     *
45
     * @api
46
     * @since 2.0.0
47
     * @var FileSize File size string.
48
     */
49
    public $file_size = null;
50

51
    /**
52
     * File extension.
53
     *
54
     * @api
55
     * @since 2.0.0
56
     * @var null|string A file extension.
57
     */
58
    public $file_extension = null;
59

60
    /**
61
     * Absolute URL.
62
     *
63
     * @var string The absolute URL to the attachment.
64
     */
65
    public $abs_url;
66

67
    /**
68
     * Attachment ID.
69
     *
70
     * @api
71
     * @var integer The attachment ID.
72
     */
73
    public $id;
74

75
    /**
76
     * Attached file.
77
     *
78
     * @var array The file as stored in the WordPress database.
79
     */
80
    protected $_wp_attached_file;
81

82
    /**
83
     * Gets the src for an attachment.
84
     *
85
     * @api
86
     *
87
     * @return string The src of the attachment.
88
     */
89
    public function __toString()
90
    {
91
        return $this->src();
×
92
    }
93

94
    /**
95
     * Gets the attachment information.
96
     *
97
     * @internal
98
     *
99
     * @param array $data Data to update.
100
     * @return array Attachment info as an array.
101
     */
102
    protected function get_info(array $data): array
103
    {
104
        $post_data = parent::get_info($data);
73✔
105
        $image_info = \wp_get_attachment_metadata($this->wp_object->ID) ?: [];
73✔
106
        $meta_values = $this->raw_meta();
73✔
107

108
        $data = \array_merge($post_data, $image_info, $meta_values);
73✔
109

110
        $basedir = \wp_get_upload_dir()['basedir'];
73✔
111

112
        if (isset($data['file'])) {
73✔
113
            $data['file_loc'] = $basedir . DIRECTORY_SEPARATOR . $data['file'];
16✔
114
        } elseif (isset($data['_wp_attached_file'])) {
58✔
115
            $data['file'] = $data['_wp_attached_file'];
58✔
116
            $data['file_loc'] = $basedir . DIRECTORY_SEPARATOR . $data['file'];
58✔
117
        }
118

119
        $data['file_size'] = new FileSize($data['file_loc']);
73✔
120

121
        return $data;
73✔
122
    }
123

124
    /**
125
     * Gets the link to an attachment.
126
     *
127
     * This returns a link to an attachment’s page, but not the link to the image src itself.
128
     *
129
     * @api
130
     * @example
131
     * ```twig
132
     * <a href="{{ image.link }}"><img src="{{ image.src }} "></a>
133
     * ```
134
     * ```html
135
     * <a href="http://example.org/my-cool-picture">
136
     *     <img src="http://example.org/wp-content/uploads/2015/whatever.jpg"/>
137
     * </a>
138
     * ```
139
     *
140
     * @return string The URL of the attachment.
141
     */
142
    public function link()
143
    {
144
        if ($this->abs_url) {
1✔
145
            return $this->abs_url;
×
146
        }
147

148
        return \get_permalink($this->ID);
1✔
149
    }
150

151
    /**
152
     * Gets the relative path to an attachment.
153
     *
154
     * @api
155
     * @example
156
     * ```twig
157
     * <img src="{{ image.path }}" />
158
     * ```
159
     * ```html
160
     * <img src="/wp-content/uploads/2015/08/pic.jpg" />
161
     * ```
162
     *
163
     * @return string The relative path to an attachment.
164
     */
165
    public function path(): string
166
    {
167
        return URLHelper::get_rel_path($this->src());
×
168
    }
169

170
    /**
171
     * Gets the source URL for an attachment.
172
     *
173
     * @api
174
     * @example
175
     * ```twig
176
     * <a href="{{ get_attachment(post.meta('job_pdf')).src }}" download>
177
     * ```
178
     * ```html
179
     * <a href="http://example.org/wp-content/uploads/2015/08/job-ad-5noe2304i.pdf" download>
180
     * ```
181
     *
182
     * @return bool|string
183
     */
184
    public function src()
185
    {
186
        if (isset($this->abs_url)) {
5✔
187
            return URLHelper::maybe_secure_url($this->abs_url) ?: null;
×
188
        }
189

190
        return \wp_get_attachment_url($this->ID) ?: null;
5✔
191
    }
192

193
    /**
194
     * Gets the caption of an attachment.
195
     *
196
     * @api
197
     * @since 2.0
198
     * @example
199
     * ```twig
200
     * <figure>
201
     *     <img src="{{ post.thumbnail.src }}">
202
     *
203
     *     {% if post.thumbnail is not empty %}
204
     *         <figcaption>{{ post.thumbnail.caption }}</figcaption
205
     *     {% endif %}
206
     * </figure>
207
     * ```
208
     *
209
     * @return string
210
     */
211
    public function caption(): string
212
    {
213
        /**
214
         * Filters the attachment caption.
215
         *
216
         * @since WordPress 4.6.0
217
         * @since 2.0.0
218
         *
219
         * @param string $caption Caption for the given attachment.
220
         * @param int    $post_id Attachment ID.
221
         */
222
        return \apply_filters('wp_get_attachment_caption', $this->post_excerpt, $this->ID);
1✔
223
    }
224

225
    /**
226
     * Gets filesize in a human readable format.
227
     *
228
     * This can be useful if you want to display the human readable filesize for a file. It’s
229
     * easier to read «16 KB» than «16555 bytes» or «1 MB» than «1048576 bytes».
230
     *
231
     * @api
232
     * @since 2.0.0
233
     * @example
234
     *
235
     * Use filesize information in a link that downloads a file:
236
     *
237
     * ```twig
238
     * <a class="download" href="{{ attachment.src }}" download="{{ attachment.title }}">
239
     *     <span class="download-title">{{ attachment.title }}</span>
240
     *     <span class="download-info">(Download, {{ attachment.size }})</span>
241
     * </a>
242
     * ```
243
     *
244
     * @return string|null The filesize string in a human-readable format or null if the
245
     *                     filesize can’t be read.
246
     */
247
    public function size(): ?string
248
    {
249
        return $this->file_size->size();
1✔
250
    }
251

252
    /**
253
     * Gets filesize in bytes.
254
     *
255
     * @api
256
     * @since 2.0.0
257
     * @example
258
     *
259
     * ```twig
260
     * <table>
261
     *     {% for attachment in Attachment(attachment_ids) %}
262
     *         <tr>
263
     *             <td>{{ attachment.title }}</td>
264
     *             <td>{{ attachment.extension }}</td>
265
     *             <td>{{ attachment.size_raw }} bytes</td>
266
     *         </tr>
267
     *     {% endfor %}
268
     * </table>
269
     * ```
270
     *
271
     * @return int|false The filesize string in bytes, or false if the filesize can’t be read.
272
     */
273
    public function size_raw()
274
    {
275
        return $this->file_size->size_raw();
1✔
276
    }
277

278
    /**
279
     * Gets the extension of the attached file.
280
     *
281
     * @api
282
     * @since 2.0.0
283
     * @example
284
     * Use extension information in a link that downloads a file:
285
     *
286
     * ```twig
287
     * <a class="download" href="{{ attachment.src }}" download="{{ attachment.title }}">
288
     *     <span class="download-title">{{ attachment.title }}</span>
289
     *     <span class="download-info">
290
     *         (Download {{ attachment.extension|upper }}, {{ attachment.size }})
291
     *     </span>
292
     * </a>
293
     * ```
294
     *
295
     * @return string|null An uppercase extension string.
296
     */
297
    public function extension(): ?string
298
    {
299
        if (!$this->file_extension) {
1✔
300
            $file_info = \wp_check_filetype($this->file);
1✔
301

302
            if (!empty($file_info['ext'])) {
1✔
303
                $this->file_extension = \strtoupper($file_info['ext']);
1✔
304
            }
305
        }
306

307
        return $this->file_extension;
1✔
308
    }
309

310
    /**
311
     * Gets the parent object.
312
     *
313
     * The parent object of an attachment is a post it is assigned to.
314
     *
315
     * @api
316
     * @example
317
     * ```twig
318
     * This image is assigned to {{ image.parent.title }}
319
     * ```
320
     *
321
     * @return false|\Timber\Post Parent object as a `Timber\Post`. Returns `false` if no parent
322
     *                            object is defined.
323
     */
324
    public function parent()
325
    {
326
        if (!$this->post_parent) {
4✔
327
            return false;
1✔
328
        }
329

330
        $factory = new PostFactory();
3✔
331

332
        return $factory->from($this->post_parent);
3✔
333
    }
334

335
    /**
336
     * Get a PHP array with pathinfo() info from the file
337
     *
338
     * @deprecated 2.0.0, use Attachment::pathinfo() instead
339
     * @return array
340
     */
341
    public function get_pathinfo()
342
    {
343
        Helper::deprecated(
×
344
            "{{ image.get_pathinfo }}",
×
345
            "{{ image.pathinfo }}",
×
346
            '2.0.0'
×
347
        );
×
348
        return PathHelper::pathinfo($this->file);
×
349
    }
350

351
    /**
352
     * Get a PHP array with pathinfo() info from the file
353
     *
354
     * @return array
355
     */
356
    public function pathinfo()
357
    {
358
        return PathHelper::pathinfo($this->file);
1✔
359
    }
360
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc