• 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

93.51
/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 = [])
28
    {
29
        return $this->fetch_meta($field_name, $args);
148✔
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 = '')
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)
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);
150✔
100

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

105
        $object_meta = null;
150✔
106

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

109
        if ($apply_filters) {
150✔
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(
148✔
140
                "timber/{$object_type}/pre_meta",
148✔
141
                $object_meta,
148✔
142
                $this->ID,
148✔
143
                $field_name,
148✔
144
                $this,
148✔
145
                $args
148✔
146
            );
148✔
147

148
            if ($object_type !== 'term') {
148✔
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(
134✔
155
                    "timber_{$object_type}_get_meta_field_pre",
134✔
156
                    [$object_meta, $this->ID, $field_name, $this],
134✔
157
                    '2.0.0',
134✔
158
                    "timber/{$object_type}/pre_meta"
134✔
159
                );
134✔
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(
134✔
167
                    "timber_{$object_type}_get_meta_pre",
134✔
168
                    [$object_meta, $this->ID, $this],
134✔
169
                    '2.0.0',
134✔
170
                    "timber/{$object_type}/pre_meta"
134✔
171
                );
134✔
172
            }
173
        }
174

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

179
            // Mimick $single argument when fetching all meta values.
180
            if (empty($field_name) && \is_array($object_meta) && !empty($object_meta)) {
109✔
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
            // Empty result.
197
            if (empty($object_meta)) {
109✔
198
                $object_meta = empty($field_name) ? [] : null;
84✔
199
            }
200
        }
201

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

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

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

264
            /**
265
             * Filters object meta data fetched from the database.
266
             *
267
             * @deprecated 2.0.0, use `timber/{object_type}/meta`
268
             */
269
            $object_meta = \apply_filters_deprecated(
148✔
270
                "timber_{$object_type}_get_meta",
148✔
271
                [$object_meta, $this->ID, $this],
148✔
272
                '2.0.0',
148✔
273
                "timber/{$object_type}/meta"
148✔
274
            );
148✔
275

276
            // Maybe convert values to Timber objects.
277
            if ($args['transform_value']) {
148✔
278
                $object_meta = $this->convert($object_meta);
12✔
279
            }
280
        }
281

282
        return $object_meta;
150✔
283
    }
284

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

301
    /**
302
     * Get the base object type
303
     *
304
     * @return string
305
     */
306
    protected function get_object_type()
307
    {
308
        return $this->object_type;
150✔
309
    }
310
}
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