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

timber / timber / 20695674007

04 Jan 2026 04:14PM UTC coverage: 89.681% (+1.5%) from 88.211%
20695674007

push

travis-ci

nlemoine
test: Fix ancestors post tests

4615 of 5146 relevant lines covered (89.68%)

63.45 hits per line

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

92.5
/src/CoreEntity.php
1
<?php
2

3
namespace Timber;
4

5
abstract class CoreEntity extends Core implements CoreInterface, CoreEntityInterface, MetaInterface
6
{
7
    /**
8
     * Gets an object meta value.
9
     *
10
     * Returns a meta value or all meta values for all custom fields of an object saved in the
11
     * meta database table.
12
     *
13
     * Fetching all values is only advised during development, because it can have a big performance
14
     * impact, when all filters are applied.
15
     *
16
     * @api
17
     *
18
     * @param string $field_name Optional. The field name for which you want to get the value. If
19
     *                           no field name is provided, this function will fetch values for all
20
     *                           custom fields. Default empty string.
21
     * @param array $args An array of arguments for getting the meta value. Third-party integrations
22
     *                    can use this argument to make their API arguments available in Timber.
23
     *                    Default empty array.
24
     * @return mixed The custom field value or an array of custom field values. Null if no value
25
     *               could be found.
26
     */
27
    public function meta($field_name = '', $args = [])
105✔
28
    {
29
        return $this->fetch_meta($field_name, $args);
105✔
30
    }
31

32
    /**
33
     * Gets an object meta value directly from the database.
34
     *
35
     * Returns a raw meta value or all raw meta values saved in the meta database table. In
36
     * comparison to `meta()`, this function will return raw values that are not filtered by third-
37
     * party plugins.
38
     *
39
     * Fetching raw values for all custom fields will not have a big performance impact, because
40
     * WordPress gets all meta values, when the first meta value is accessed.
41
     *
42
     * @api
43
     * @since 2.0.0
44
     *
45
     * @param string $field_name Optional. The field name for which you want to get the value. If
46
     *                           no field name is provided, this function will fetch values for all
47
     *                           custom fields. Default empty string.
48
     *
49
     * @return null|mixed The meta field value(s). Null if no value could be found, an empty array
50
     *                    if all fields were requested but no values could be found.
51
     */
52
    public function raw_meta($field_name = '')
3✔
53
    {
54
        return $this->fetch_meta($field_name, [], false);
3✔
55
    }
56

57
    /**
58
     * Gets an object meta value.
59
     *
60
     * Returns a meta value or all meta values for all custom fields of an object saved in the object
61
     * meta database table.
62
     *
63
     * Fetching all values is only advised during development, because it can have a big performance
64
     * impact, when all filters are applied.
65
     *
66
     * @api
67
     *
68
     * @param string $field_name Optional. The field name for which you want to get the value. If
69
     *                           no field name is provided, this function will fetch values for all
70
     *                           custom fields. Default empty string.
71
     * @param array  $args       {
72
     *      An array of arguments for getting the meta value. Third-party integrations can use this
73
     *      argument to make their API arguments available in Timber. Default empty array.
74
     * }
75
     * @param bool $apply_filters Whether to apply filtering of meta values. You can also use
76
     *                               the `raw_meta()` method as a shortcut to apply this argument.
77
     *                            Default true.
78
     *
79
     * @return mixed The custom field value or an array of custom field values. Null if no value
80
     *               could be found.
81
     */
82
    protected function fetch_meta($field_name = '', $args = [], $apply_filters = true)
107✔
83
    {
84
        /**
85
         * Filters whether to transform a meta value.
86
         *
87
         * If the filter returns `true`, all meta value will be transformed to Timber/standard PHP objects if possible.
88
         *
89
         * @api
90
         * @since 2.0.0
91
         * @example
92
         * ```php
93
         * // Transforms all meta value.
94
         * add_filter( 'timber/meta/transform_value', '__return_true' );
95
         * ```
96
         *
97
         * @param bool $transform_value
98
         */
99
        $transform_value = \apply_filters('timber/meta/transform_value', false);
107✔
100

101
        $args = \wp_parse_args($args, [
107✔
102
            'transform_value' => $transform_value,
107✔
103
        ]);
107✔
104

105
        $object_meta = null;
107✔
106

107
        $object_type = $this->get_object_type();
107✔
108

109
        if ($apply_filters) {
107✔
110
            /**
111
             * Filters object meta data before it is fetched from the database.
112
             *
113
             * @since 2.0.0
114
             *
115
             * @example
116
             * ```php
117
             * // Disable fetching meta values.
118
             * add_filter( 'timber/post/pre_meta', '__return_false' );
119
             *
120
             * // Add your own meta data.
121
             * add_filter( 'timber/post/pre_meta', function( $post_meta, $post_id, $post ) {
122
             *     $post_meta = array_merge( $post_meta, [
123
             *         'custom_data_1' => 73,
124
             *         'custom_data_2' => 274,
125
             *     ] );
126
             *
127
             *     return $post_meta;
128
             * }, 10, 3 );
129
             * ```
130
             *
131
             * @param string|null $object_meta The field value. Default null. Passing a non-null
132
             *                                 value will skip fetching the value from the database
133
             *                                 and will use the value from the filter instead.
134
             * @param int         $post_id     The post ID.
135
             * @param string      $field_name  The name of the meta field to get the value for.
136
             * @param object      $object      The Timber object.
137
             * @param array       $args        An array of arguments.
138
             */
139
            $object_meta = \apply_filters(
105✔
140
                "timber/{$object_type}/pre_meta",
105✔
141
                $object_meta,
105✔
142
                $this->ID,
105✔
143
                $field_name,
105✔
144
                $this,
105✔
145
                $args
105✔
146
            );
105✔
147

148
            if ($object_type !== 'term') {
105✔
149
                /**
150
                 * Filters the value for a post meta field before it is fetched from the database.
151
                 *
152
                 * @deprecated 2.0.0, use `timber/{object_type}/pre_meta`
153
                 */
154
                $object_meta = \apply_filters_deprecated(
91✔
155
                    "timber_{$object_type}_get_meta_field_pre",
91✔
156
                    [$object_meta, $this->ID, $field_name, $this],
91✔
157
                    '2.0.0',
91✔
158
                    "timber/{$object_type}/pre_meta"
91✔
159
                );
91✔
160

161
                /**
162
                 * Filters post meta data before it is fetched from the database.
163
                 *
164
                 * @deprecated 2.0.0, use `timber/{object_type}/pre_meta`
165
                 */
166
                \do_action_deprecated(
91✔
167
                    "timber_{$object_type}_get_meta_pre",
91✔
168
                    [$object_meta, $this->ID, $this],
91✔
169
                    '2.0.0',
91✔
170
                    "timber/{$object_type}/pre_meta"
91✔
171
                );
91✔
172
            }
173
        }
174

175
        if (null === $object_meta) {
107✔
176
            // Fetch values. Auto-fetches all values if $field_name is empty.
177
            $object_meta = \get_metadata($object_type, $this->ID, $field_name, true);
84✔
178

179
            // Mimic $single argument when fetching all meta values.
180
            if (empty($field_name) && \is_array($object_meta)) {
84✔
181
                $object_meta = \array_map(function ($meta) {
1✔
182
                    /**
183
                     * We use array_key_exists() instead of isset(), because when the meta value is null, isset() would
184
                     * return false, even though null is a valid value to return.
185
                     *
186
                     * @ticket #2519
187
                     */
188
                    if (1 === \count($meta) && \array_key_exists(0, $meta)) {
1✔
189
                        return $meta[0];
1✔
190
                    }
191

192
                    return $meta;
1✔
193
                }, $object_meta);
1✔
194
            }
195
        }
196

197
        if ($apply_filters) {
107✔
198
            /**
199
             * Filters the value for a post meta field.
200
             *
201
             * This filter is used by the ACF Integration.
202
             *
203
             * @see   \Timber\Post::meta()
204
             * @since 2.0.0
205
             *
206
             * @example
207
             * ```php
208
             * add_filter( 'timber/post/meta', function( $post_meta, $post_id, $field_name, $post ) {
209
             *     if ( 'event' === $post->post_type ) {
210
             *         // Do something special.
211
             *         $post_meta['foo'] = $post_meta['foo'] . ' bar';
212
             *     }
213
             *
214
             *     return $post_meta;
215
             * }, 10, 4 );
216
             * ```
217
             *
218
             * @param string             $post_meta  The field value.
219
             * @param int                $post_id    The post ID.
220
             * @param string             $field_name The name of the meta field to get the value for.
221
             * @param CoreEntity $post       The post object.
222
             * @param array              $args       An array of arguments.
223
             */
224
            $object_meta = \apply_filters(
105✔
225
                "timber/{$object_type}/meta",
105✔
226
                $object_meta,
105✔
227
                $this->ID,
105✔
228
                $field_name,
105✔
229
                $this,
105✔
230
                $args
105✔
231
            );
105✔
232

233
            if ($object_type === 'term') {
105✔
234
                /**
235
                 * Filters the value for a term meta field.
236
                 *
237
                 * @deprecated 2.0.0, use `timber/term/meta`
238
                 */
239
                $object_meta = \apply_filters_deprecated(
25✔
240
                    'timber/term/meta/field',
25✔
241
                    [$object_meta, $this->ID, $field_name, $this],
25✔
242
                    '2.0.0',
25✔
243
                    'timber/term/meta'
25✔
244
                );
25✔
245
            }
246

247
            /**
248
             * Filters the value for an object meta field.
249
             *
250
             * @deprecated 2.0.0, use `timber/{object_type}/meta`
251
             */
252
            $object_meta = \apply_filters_deprecated(
105✔
253
                "timber_{$object_type}_get_meta_field",
105✔
254
                [$object_meta, $this->ID, $field_name, $this],
105✔
255
                '2.0.0',
105✔
256
                "timber/{$object_type}/meta"
105✔
257
            );
105✔
258

259
            /**
260
             * Filters object meta data fetched from the database.
261
             *
262
             * @deprecated 2.0.0, use `timber/{object_type}/meta`
263
             */
264
            $object_meta = \apply_filters_deprecated(
105✔
265
                "timber_{$object_type}_get_meta",
105✔
266
                [$object_meta, $this->ID, $this],
105✔
267
                '2.0.0',
105✔
268
                "timber/{$object_type}/meta"
105✔
269
            );
105✔
270

271
            // Maybe convert values to Timber objects.
272
            if ($args['transform_value']) {
105✔
273
                $object_meta = $this->convert($object_meta);
12✔
274
            }
275
        }
276

277
        return $object_meta;
107✔
278
    }
279

280
    /**
281
     * Finds any WP_Post objects and converts them to Timber\Posts
282
     *
283
     * @api
284
     * @param array|CoreEntity $data
285
     */
286
    public function convert($data)
×
287
    {
288
        if (\is_object($data)) {
×
289
            $data = Helper::convert_wp_object($data);
×
290
        } elseif (\is_array($data)) {
×
291
            $data = \array_map([$this, 'convert'], $data);
×
292
        }
293
        return $data;
×
294
    }
295

296
    /**
297
     * Get the base object type
298
     *
299
     * @return string
300
     */
301
    protected function get_object_type()
107✔
302
    {
303
        return $this->object_type;
107✔
304
    }
305
}
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