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

timber / timber / 5690057835

pending completion
5690057835

push

github

nlemoine
Merge branch '2.x' of github.com:timber/timber into 2.x-refactor-file-models

# Conflicts:
#	src/Attachment.php
#	src/ExternalImage.php
#	src/FileSize.php
#	src/URLHelper.php

1134 of 1134 new or added lines in 55 files covered. (100.0%)

3923 of 4430 relevant lines covered (88.56%)

59.08 hits per line

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

81.52
/src/Integration/AcfIntegration.php
1
<?php
2
/**
3
 * Integration with Advanced Custom Fields (ACF)
4
 *
5
 * @package Timber
6
 */
7

8
namespace Timber\Integration;
9

10
use ACF;
11

12
use DateTimeImmutable;
13
use Timber\Timber;
14

15
/**
16
 * Class used to handle integration with Advanced Custom Fields
17
 */
18
class AcfIntegration implements IntegrationInterface
19
{
20
    public function should_init(): bool
21
    {
22
        return \class_exists(ACF::class);
×
23
    }
24

25
    public function init(): void
26
    {
27
        \add_filter('timber/post/pre_meta', [__CLASS__, 'post_get_meta_field'], 10, 5);
×
28
        \add_filter('timber/post/meta_object_field', [__CLASS__, 'post_meta_object'], 10, 3);
×
29
        \add_filter('timber/term/pre_meta', [__CLASS__, 'term_get_meta_field'], 10, 5);
×
30
        \add_filter('timber/user/pre_meta', [__CLASS__, 'user_get_meta_field'], 10, 5);
×
31

32
        /**
33
         * Allowed a user to set a meta value
34
         *
35
         * @deprecated 2.0.0 with no replacement
36
         */
37
        \add_filter('timber/term/meta/set', [__CLASS__, 'term_set_meta'], 10, 4);
×
38
    }
39

40
    /**
41
     * Gets meta value for a post through ACF’s API.
42
     *
43
     * @param string       $value      The field value. Default null.
44
     * @param int          $post_id    The post ID.
45
     * @param string       $field_name The name of the meta field to get the value for.
46
     * @param \Timber\Post $post       The post object.
47
     * @param array        $args       An array of arguments.
48
     * @return mixed|false
49
     */
50
    public static function post_get_meta_field($value, $post_id, $field_name, $post, $args)
51
    {
52
        return self::get_meta($value, $post_id, $field_name, $args);
98✔
53
    }
54

55
    public static function post_meta_object($value, $post_id, $field_name)
56
    {
57
        return \get_field_object($field_name, $post_id);
1✔
58
    }
59

60
    /**
61
     * Gets meta value for a term through ACF’s API.
62
     *
63
     * @param string       $value      The field value. Default null.
64
     * @param int          $term_id    The term ID.
65
     * @param string       $field_name The name of the meta field to get the value for.
66
     * @param \Timber\Term $term       The term object.
67
     * @param array        $args       An array of arguments.
68
     * @return mixed|false
69
     */
70
    public static function term_get_meta_field($value, $term_id, $field_name, $term, $args)
71
    {
72
        return self::get_meta($value, $term->taxonomy . '_' . $term_id, $field_name, $args);
10✔
73
    }
74

75
    /**
76
     * @deprecated 2.0.0, with no replacement
77
     *
78
     * @return mixed
79
     */
80
    public static function term_set_meta($value, $field, $term_id, $term)
81
    {
82
        $searcher = $term->taxonomy . '_' . $term->ID;
×
83
        \update_field($field, $value, $searcher);
×
84
        return $value;
×
85
    }
86

87
    /**
88
     * Gets meta value for a user through ACF’s API.
89
     *
90
     * @param string       $value      The field value. Default null.
91
     * @param int          $user_id    The user ID.
92
     * @param string       $field_name The name of the meta field to get the value for.
93
     * @param \Timber\User $user       The user object.
94
     * @param array        $args       An array of arguments.
95
     * @return mixed|false
96
     */
97
    public static function user_get_meta_field($value, $user_id, $field_name, $user, $args)
98
    {
99
        return self::get_meta($value, 'user_' . $user_id, $field_name, $args);
7✔
100
    }
101

102
    /**
103
     * Transform ACF file field
104
     *
105
     * @param string $value
106
     * @param int    $id
107
     * @param array  $field
108
     */
109
    public static function transform_file($value, $id, $field)
110
    {
111
        if (empty($value)) {
1✔
112
            return false;
1✔
113
        }
114
        return Timber::get_attachment($value);
×
115
    }
116

117
    /**
118
     * Transform ACF image field
119
     *
120
     * @param string $value
121
     * @param int    $id
122
     * @param array  $field
123
     */
124
    public static function transform_image($value, $id, $field)
125
    {
126
        if (empty($value)) {
2✔
127
            return false;
×
128
        }
129
        return Timber::get_image($value);
2✔
130
    }
131

132
    /**
133
     * Transform ACF gallery field
134
     *
135
     * @param array $value
136
     * @param int   $id
137
     * @param array $field
138
     */
139
    public static function transform_gallery($value, $id, $field)
140
    {
141
        if (empty($value)) {
×
142
            return false;
×
143
        }
144
        return Timber::get_posts($value);
×
145
    }
146

147
    /**
148
     * Transform ACF date picker field
149
     *
150
     * @param string $value
151
     * @param int    $id
152
     * @param array  $field
153
     */
154
    public static function transform_date_picker($value, $id, $field)
155
    {
156
        if (!$value) {
2✔
157
            return $value;
×
158
        }
159
        return new DateTimeImmutable(\acf_format_date($value, 'Y-m-d H:i:s'));
2✔
160
    }
161

162
    /**
163
     * Transform ACF post object field
164
     *
165
     * @param string $value
166
     * @param int    $id
167
     * @param array  $field
168
     */
169
    public static function transform_post_object($value, $id, $field)
170
    {
171
        if (empty($value)) {
2✔
172
            return false;
×
173
        }
174
        if (!$field['multiple']) {
2✔
175
            return Timber::get_post($value);
1✔
176
        }
177
        return Timber::get_posts($value);
1✔
178
    }
179

180
    /**
181
     * Transform ACF relationship field
182
     *
183
     * @param string $value
184
     * @param int    $id
185
     * @param array  $field
186
     */
187
    public static function transform_relationship($value, $id, $field)
188
    {
189
        if (empty($value)) {
1✔
190
            return false;
×
191
        }
192
        return Timber::get_posts($value);
1✔
193
    }
194

195
    /**
196
     * Transform ACF taxonomy field
197
     *
198
     * @param string $value
199
     * @param int    $id
200
     * @param array  $field
201
     */
202
    public static function transform_taxonomy($value, $id, $field)
203
    {
204
        if ($field['field_type'] === 'select' || $field['field_type'] === 'radio') {
2✔
205
            return Timber::get_term((int) $value);
1✔
206
        }
207
        return Timber::get_terms((array) $value);
1✔
208
    }
209

210
    /**
211
     * Transform ACF user field
212
     *
213
     * @param string $value
214
     * @param int    $id
215
     * @param array  $field
216
     */
217
    public static function transform_user($value, $id, $field)
218
    {
219
        if (!$field['multiple']) {
2✔
220
            return Timber::get_user((int) $value);
1✔
221
        }
222
        return Timber::get_users((array) $value);
1✔
223
    }
224

225
    /**
226
     * Gets meta value through ACF’s API.
227
     *
228
     * @param string     $value
229
     * @param int|string $id
230
     * @param string     $field_name
231
     * @param array      $args
232
     * @return mixed|false
233
     */
234
    private static function get_meta($value, $id, $field_name, $args)
235
    {
236
        $args = \wp_parse_args($args, [
115✔
237
            'format_value' => true,
115✔
238
            'transform_value' => false,
115✔
239
        ]);
115✔
240

241
        if (!$args['transform_value']) {
115✔
242
            return \get_field($field_name, $id, $args['format_value']);
105✔
243
        }
244

245
        /**
246
         * We use acf()->fields->get_field_type() instead of acf_get_field_type(), because of some function stub issues
247
         * in the php-stubs/acf-pro-stubs package. The ACF plugin doesn’t use the right parameter and return values for
248
         * some functions in the DocBlocks.
249
         *
250
         * @ticket https://github.com/timber/timber/pull/2630
251
         */
252
        $file_field_type = \acf_get_field_type('file');
12✔
253
        $image_field_type = \acf_get_field_type('image');
12✔
254
        $gallery_field_type = \acf_get_field_type('gallery');
12✔
255
        $date_picker_field_type = \acf_get_field_type('date_picker');
12✔
256
        $date_time_picker_field_type = \acf_get_field_type('date_time_picker');
12✔
257
        $post_object_field_type = \acf_get_field_type('post_object');
12✔
258
        $relationship_field_type = \acf_get_field_type('relationship');
12✔
259
        $taxonomy_field_type = \acf_get_field_type('taxonomy');
12✔
260
        $user_field_type = \acf_get_field_type('user');
12✔
261

262
        \remove_filter('acf/format_value/type=file', [$file_field_type, 'format_value']);
12✔
263
        \remove_filter('acf/format_value/type=image', [$image_field_type, 'format_value']);
12✔
264
        \remove_filter('acf/format_value/type=gallery', [$gallery_field_type, 'format_value']);
12✔
265
        \remove_filter('acf/format_value/type=date_picker', [$date_picker_field_type, 'format_value']);
12✔
266
        \remove_filter('acf/format_value/type=date_time_picker', [$date_time_picker_field_type, 'format_value']);
12✔
267
        \remove_filter('acf/format_value/type=post_object', [$post_object_field_type, 'format_value']);
12✔
268
        \remove_filter('acf/format_value/type=relationship', [$relationship_field_type, 'format_value']);
12✔
269
        \remove_filter('acf/format_value/type=taxonomy', [$taxonomy_field_type, 'format_value']);
12✔
270
        \remove_filter('acf/format_value/type=user', [$user_field_type, 'format_value']);
12✔
271

272
        \add_filter('acf/format_value/type=file', [__CLASS__, 'transform_file'], 10, 3);
12✔
273
        \add_filter('acf/format_value/type=image', [__CLASS__, 'transform_image'], 10, 3);
12✔
274
        \add_filter('acf/format_value/type=gallery', [__CLASS__, 'transform_gallery'], 10, 3);
12✔
275
        \add_filter('acf/format_value/type=date_picker', [__CLASS__, 'transform_date_picker'], 10, 3);
12✔
276
        \add_filter('acf/format_value/type=date_time_picker', [__CLASS__, 'transform_date_picker'], 10, 3);
12✔
277
        \add_filter('acf/format_value/type=post_object', [__CLASS__, 'transform_post_object'], 10, 3);
12✔
278
        \add_filter('acf/format_value/type=relationship', [__CLASS__, 'transform_relationship'], 10, 3);
12✔
279
        \add_filter('acf/format_value/type=taxonomy', [__CLASS__, 'transform_taxonomy'], 10, 3);
12✔
280
        \add_filter('acf/format_value/type=user', [__CLASS__, 'transform_user'], 10, 3);
12✔
281

282
        $value = \get_field($field_name, $id, true);
12✔
283

284
        \add_filter('acf/format_value/type=file', [$file_field_type, 'format_value'], 10, 3);
12✔
285
        \add_filter('acf/format_value/type=image', [$image_field_type, 'format_value'], 10, 3);
12✔
286
        \add_filter('acf/format_value/type=gallery', [$gallery_field_type, 'format_value'], 10, 3);
12✔
287
        \add_filter('acf/format_value/type=date_picker', [$date_picker_field_type, 'format_value'], 10, 3);
12✔
288
        \add_filter('acf/format_value/type=date_time_picker', [$date_time_picker_field_type, 'format_value'], 10, 3);
12✔
289
        \add_filter('acf/format_value/type=post_object', [$post_object_field_type, 'format_value'], 10, 3);
12✔
290
        \add_filter('acf/format_value/type=relationship', [$relationship_field_type, 'format_value'], 10, 3);
12✔
291
        \add_filter('acf/format_value/type=taxonomy', [$taxonomy_field_type, 'format_value'], 10, 3);
12✔
292
        \add_filter('acf/format_value/type=user', [$taxonomy_field_type, 'format_value'], 10, 3);
12✔
293

294
        \remove_filter('acf/format_value/type=file', [__CLASS__, 'transform_file']);
12✔
295
        \remove_filter('acf/format_value/type=image', [__CLASS__, 'transform_image']);
12✔
296
        \remove_filter('acf/format_value/type=gallery', [__CLASS__, 'transform_gallery']);
12✔
297
        \remove_filter('acf/format_value/type=date_picker', [__CLASS__, 'transform_date_picker']);
12✔
298
        \remove_filter('acf/format_value/type=date_time_picker', [__CLASS__, 'transform_date_picker']);
12✔
299
        \remove_filter('acf/format_value/type=post_object', [__CLASS__, 'transform_post_object']);
12✔
300
        \remove_filter('acf/format_value/type=relationship', [__CLASS__, 'transform_relationship']);
12✔
301
        \remove_filter('acf/format_value/type=taxonomy', [__CLASS__, 'transform_taxonomy']);
12✔
302
        \remove_filter('acf/format_value/type=user', [__CLASS__, 'transform_user']);
12✔
303

304
        return $value;
12✔
305
    }
306
}
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