• 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

91.67
/src/Factory/TermFactory.php
1
<?php
2

3
namespace Timber\Factory;
4

5
use InvalidArgumentException;
6
use Timber\CoreInterface;
7

8
use Timber\Term;
9
use WP_Term;
10
use WP_Term_Query;
11

12
/**
13
 * Internal API class for instantiating Terms
14
 */
15
class TermFactory
16
{
17
    public function from($params)
18
    {
19
        if (\is_int($params) || \is_string($params) && \is_numeric($params)) {
103✔
20
            return $this->from_id((int) $params);
75✔
21
        }
22

23
        if (\is_string($params)) {
36✔
24
            return $this->from_taxonomy_names([$params]);
2✔
25
        }
26

27
        if ($params instanceof WP_Term_Query) {
34✔
28
            return $this->from_wp_term_query($params);
1✔
29
        }
30

31
        if (\is_object($params)) {
33✔
32
            return $this->from_term_object($params);
12✔
33
        }
34

35
        if ($this->is_numeric_array($params)) {
29✔
36
            if ($this->is_array_of_strings($params)) {
14✔
37
                return $this->from_taxonomy_names($params);
×
38
            }
39

40
            return \array_map([$this, 'from'], $params);
14✔
41
        }
42

43
        if (\is_array($params)) {
20✔
44
            return $this->from_wp_term_query(new WP_Term_Query(
20✔
45
                $this->filter_query_params($params)
20✔
46
            ));
20✔
47
        }
48

49
        return null;
×
50
    }
51

52
    protected function from_id(int $id): ?Term
53
    {
54
        $wp_term = \get_term($id);
75✔
55

56
        if (!$wp_term) {
75✔
57
            return null;
1✔
58
        }
59

60
        return $this->build($wp_term);
74✔
61
    }
62

63
    protected function from_wp_term_query(WP_Term_Query $query): iterable
64
    {
65
        return \array_map([$this, 'build'], $query->get_terms());
23✔
66
    }
67

68
    protected function from_term_object(object $obj): CoreInterface
69
    {
70
        if ($obj instanceof CoreInterface) {
12✔
71
            // We already have a Timber Core object of some kind
72
            return $obj;
1✔
73
        }
74

75
        if ($obj instanceof WP_Term) {
12✔
76
            return $this->build($obj);
11✔
77
        }
78

79
        throw new InvalidArgumentException(\sprintf(
1✔
80
            'Expected an instance of Timber\CoreInterface or WP_Term, got %s',
1✔
81
            \get_class($obj)
1✔
82
        ));
1✔
83
    }
84

85
    protected function from_taxonomy_names(array $names)
86
    {
87
        return $this->from_wp_term_query(new WP_Term_Query(
2✔
88
            $this->filter_query_params([
2✔
89
                'taxonomy' => $names,
2✔
90
            ])
2✔
91
        ));
2✔
92
    }
93

94
    protected function get_term_class(WP_Term $term): string
95
    {
96
        // Get the user-configured Class Map
97
        $map = \apply_filters('timber/term/classmap', [
99✔
98
            'post_tag' => Term::class,
99✔
99
            'category' => Term::class,
99✔
100
        ]);
99✔
101

102
        $class = $map[$term->taxonomy] ?? null;
99✔
103

104
        if (\is_callable($class)) {
99✔
105
            $class = $class($term);
1✔
106
        }
107

108
        $class = $class ?? Term::class;
99✔
109

110
        /**
111
         * Filters the term class based on your custom criteria.
112
         *
113
         * Maybe you want to set a custom class based upon a certain category?
114
         * This allows you to filter the PHP class, utilizing data from the WP_Term object.
115
         *
116
         * @since 2.0.0
117
         * @example
118
         * ```
119
         * add_filter( 'timber/term/class', function( $class, $term ) {
120
         *     if ( get_term_meta($term->term_id, 'is_special_category', true) ) {
121
         *         return MyCustomTermClass::class;
122
         *     }
123
         *
124
         *     return $class;
125
         * }, 10, 2 );
126
         * ```
127
         *
128
         * @param string $class The class to use.
129
         * @param WP_Term $term The term object.
130
         */
131
        $class = \apply_filters('timber/term/class', $class, $term);
99✔
132

133
        return $class;
99✔
134
    }
135

136
    protected function build(WP_Term $term): CoreInterface
137
    {
138
        $class = $this->get_term_class($term);
99✔
139

140
        return $class::build($term);
99✔
141
    }
142

143
    protected function correct_tax_key(array $params)
144
    {
145
        $corrections = [
22✔
146
            'taxonomies' => 'taxonomy',
22✔
147
            'taxs' => 'taxonomy',
22✔
148
            'tax' => 'taxonomy',
22✔
149
        ];
22✔
150

151
        foreach ($corrections as $mistake => $correction) {
22✔
152
            if (isset($params[$mistake])) {
22✔
153
                $params[$correction] = $params[$mistake];
×
154
            }
155
        }
156

157
        return $params;
22✔
158
    }
159

160
    protected function correct_taxonomies($tax): array
161
    {
162
        $taxonomies = \is_array($tax) ? $tax : [$tax];
20✔
163

164
        $corrections = [
20✔
165
            'categories' => 'category',
20✔
166
            'tags' => 'post_tag',
20✔
167
            'tag' => 'post_tag',
20✔
168
        ];
20✔
169

170
        return \array_map(function ($taxonomy) use ($corrections) {
20✔
171
            return $corrections[$taxonomy] ?? $taxonomy;
20✔
172
        }, $taxonomies);
20✔
173
    }
174

175
    protected function filter_query_params(array $params)
176
    {
177
        $params = $this->correct_tax_key($params);
22✔
178

179
        if (isset($params['taxonomy'])) {
22✔
180
            $params['taxonomy'] = $this->correct_taxonomies($params['taxonomy']);
20✔
181
        }
182

183
        $include = $params['term_id'] ?? null;
22✔
184
        if ($include) {
22✔
185
            $params['include'] = \is_array($include) ? $include : [$include];
×
186
        }
187

188
        return $params;
22✔
189
    }
190

191
    protected function is_numeric_array($arr)
192
    {
193
        if (!\is_array($arr)) {
29✔
194
            return false;
×
195
        }
196
        foreach (\array_keys($arr) as $k) {
29✔
197
            if (!\is_int($k)) {
29✔
198
                return false;
20✔
199
            }
200
        }
201
        return true;
14✔
202
    }
203

204
    protected function is_array_of_strings($arr)
205
    {
206
        if (!\is_array($arr)) {
14✔
207
            return false;
×
208
        }
209
        foreach ($arr as $v) {
14✔
210
            if (!\is_string($v)) {
14✔
211
                return false;
14✔
212
            }
213
        }
214
        return true;
×
215
    }
216
}
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

© 2025 Coveralls, Inc