• 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

97.44
/src/Factory/CommentFactory.php
1
<?php
2

3
namespace Timber\Factory;
4

5
use InvalidArgumentException;
6
use Timber\Comment;
7

8
use Timber\CoreInterface;
9
use WP_Comment;
10
use WP_Comment_Query;
11

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

23
        if ($params instanceof WP_Comment_Query) {
21✔
24
            return $this->from_wp_comment_query($params);
1✔
25
        }
26

27
        if (\is_object($params)) {
20✔
28
            return $this->from_comment_object($params);
19✔
29
        }
30

31
        if ($this->is_numeric_array($params)) {
3✔
32
            return \array_map([$this, 'from'], $params);
2✔
33
        }
34

35
        if (\is_array($params)) {
1✔
36
            return $this->from_wp_comment_query(new WP_Comment_Query($params));
1✔
37
        }
38
    }
39

40
    protected function from_id(int $id)
41
    {
42
        $wp_comment = \get_comment($id);
50✔
43

44
        if (!$wp_comment) {
50✔
45
            return null;
1✔
46
        }
47

48
        return $this->build($wp_comment);
49✔
49
    }
50

51
    protected function from_comment_object(object $comment): CoreInterface
52
    {
53
        if ($comment instanceof CoreInterface) {
19✔
54
            // We already have some kind of Timber Core object
55
            return $comment;
1✔
56
        }
57

58
        if ($comment instanceof WP_Comment) {
19✔
59
            return $this->build($comment);
18✔
60
        }
61

62
        throw new InvalidArgumentException(\sprintf(
1✔
63
            'Expected an instance of Timber\CoreInterface or WP_Comment, got %s',
1✔
64
            \get_class($comment)
1✔
65
        ));
1✔
66
    }
67

68
    protected function from_wp_comment_query(WP_Comment_Query $query): iterable
69
    {
70
        return \array_map([$this, 'build'], $query->get_comments());
2✔
71
    }
72

73
    protected function get_comment_class(WP_Comment $comment): string
74
    {
75
        // Get the user-configured Class Map
76
        $map = \apply_filters('timber/comment/classmap', []);
68✔
77

78
        $type = \get_post_type($comment->comment_post_ID);
68✔
79
        $class = $map[$type] ?? null;
68✔
80

81
        if (\is_callable($class)) {
68✔
82
            $class = $class($comment);
1✔
83
        }
84

85
        $class = $class ?? Comment::class;
68✔
86

87
        /**
88
         * Filters the comment class based on your custom criteria.
89
         *
90
         * Maybe you want to set a custom class based upon the comment type?
91
         * This allows you to filter the PHP class, utilizing data from the WP_Comment object.
92
         *
93
         * @since 2.0.0
94
         * @example
95
         * ```
96
         * add_filter( 'timber/comment/class', function( $class, $comment ) {
97
         *     if ( $comment->comment_type === 'pingback' ) {
98
         *         return PingBackComment::class;
99
         *     }
100
         *     return $class;
101
         * }, 10, 2 );
102
         * ```
103
         *
104
         * @param string $class The class to use.
105
         * @param WP_Comment $comment The comment object.
106
         */
107
        $class = \apply_filters('timber/comment/class', $class, $comment);
68✔
108

109
        return $class;
68✔
110
    }
111

112
    protected function build(WP_Comment $comment): CoreInterface
113
    {
114
        $class = $this->get_comment_class($comment);
68✔
115

116
        return $class::build($comment);
68✔
117
    }
118

119
    protected function is_numeric_array($arr)
120
    {
121
        if (!\is_array($arr)) {
3✔
122
            return false;
×
123
        }
124
        foreach (\array_keys($arr) as $k) {
3✔
125
            if (!\is_int($k)) {
3✔
126
                return false;
1✔
127
            }
128
        }
129
        return true;
2✔
130
    }
131
}
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