• 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

93.33
/src/Factory/UserFactory.php
1
<?php
2

3
namespace Timber\Factory;
4

5
use InvalidArgumentException;
6
use Timber\CoreInterface;
7

8
use Timber\User;
9
use WP_User;
10
use WP_User_Query;
11

12
/**
13
 * Class UserFactory
14
 *
15
 * Internal class for instantiating User objects/collections. Responsible for applying
16
 * the `timber/user/class` filter.
17
 *
18
 * @internal
19
 */
20
class UserFactory
21
{
22
    /**
23
     * Internal method that does the heavy lifting for converting some kind of user
24
     * object or ID to a Timber\User object.
25
     *
26
     * Do not call this directly. Use Timber::get_user() or Timber::get_users() instead.
27
     *
28
     * @internal
29
     * @param mixed $params One of:
30
     * * a user ID (string or int)
31
     * * a WP_User_Query object
32
     * * a WP_User object
33
     * * a Timber\Core object (presumably a User)
34
     * * an array of IDs
35
     * * an associative array (interpreted as arguments for a WP_User_Query)
36
     * @return \Timber\User|array|null
37
     */
38
    public function from($params)
39
    {
40
        if (\is_int($params) || \is_string($params) && \is_numeric($params)) {
72✔
41
            return $this->from_id($params);
62✔
42
        }
43

44
        if ($params instanceof WP_User_Query) {
21✔
45
            return $this->from_wp_user_query($params);
1✔
46
        }
47

48
        if (\is_object($params)) {
20✔
49
            // assume we have some kind of WP user object, Timber or otherwise
50
            return $this->from_user_object($params);
12✔
51
        }
52

53
        if ($this->is_numeric_array($params)) {
9✔
54
            // we have a numeric array of objects and/or IDs
55
            return \array_map([$this, 'from'], $params);
8✔
56
        }
57

58
        if (\is_array($params)) {
1✔
59
            // we have a query array to be passed to WP_User_Query::__construct()
60
            return $this->from_wp_user_query(new WP_User_Query($params));
1✔
61
        }
62

63
        return null;
×
64
    }
65

66
    protected function from_id(int $id)
67
    {
68
        $wp_user = \get_user_by('id', $id);
62✔
69

70
        return $wp_user ? $this->build($wp_user) : null;
62✔
71
    }
72

73
    protected function from_user_object($obj): CoreInterface
74
    {
75
        if ($obj instanceof CoreInterface) {
12✔
76
            // we already have some kind of Timber Core object
77
            return $obj;
1✔
78
        }
79

80
        if ($obj instanceof WP_User) {
12✔
81
            return $this->build($obj);
11✔
82
        }
83

84
        throw new InvalidArgumentException(\sprintf(
1✔
85
            'Expected an instance of Timber\CoreInterface or WP_User, got %s',
1✔
86
            \get_class($obj)
1✔
87
        ));
1✔
88
    }
89

90
    protected function from_wp_user_query(WP_User_Query $query): iterable
91
    {
92
        return \array_map([$this, 'build'], $query->get_results());
2✔
93
    }
94

95
    protected function build(WP_User $user): CoreInterface
96
    {
97
        /**
98
         * Filters the name of the PHP class used to instantiate `Timber\User` objects.
99
         *
100
         * The User Class Map receives the default `Timber\User` class and a `WP_User` object. You
101
         * should be able to decide which class to use based on that user object.
102
         *
103
         * @api
104
         * @since 2.0.0
105
         * @example
106
         * ```php
107
         * use Administrator;
108
         * use Editor;
109
         *
110
         * add_filter( 'timber/user/class', function( $class, \WP_User $user ) {
111
         *     if ( in_array( 'editor', $user->roles, true ) ) {
112
         *         return Editor::class;
113
         *     } elseif ( in_array( 'author', $user->roles, true ) ) {
114
         *         return Author::class;
115
         *     }
116
         *
117
         *     return $class;
118
         * }, 10, 2 );
119
         * ```
120
         *
121
         * @param string   $class The name of the class. Default `Timber\User`.
122
         * @param WP_User $user  The `WP_User` object that is used as the base for the
123
         *                        `Timber\User` object.
124
         */
125
        $class = \apply_filters('timber/user/class', User::class, $user);
68✔
126

127
        return $class::build($user);
68✔
128
    }
129

130
    protected function is_numeric_array($arr)
131
    {
132
        if (!\is_array($arr)) {
9✔
133
            return false;
×
134
        }
135
        foreach (\array_keys($arr) as $k) {
9✔
136
            if (!\is_int($k)) {
8✔
137
                return false;
1✔
138
            }
139
        }
140
        return true;
8✔
141
    }
142
}
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