• 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

93.1
/src/CommentThread.php
1
<?php
2

3
namespace Timber;
4

5
use ArrayObject;
6
use Timber\Factory\CommentFactory;
7

8
/**
9
 * Class CommentThread
10
 *
11
 * This object is a special type of array that hold WordPress comments as `Timber\Comment` objects.
12
 * You probably won't use this directly. This object is returned when calling `{{ post.comments }}`
13
 * in Twig.
14
 *
15
 * @example
16
 * ```twig
17
 * {# single.twig #}
18
 * <div id="post-comments">
19
 *   <h4>Comments on {{ post.title }}</h4>
20
 *   <ul>
21
 *     {% for comment in post.comments %}
22
 *       {% include 'comment.twig' %}
23
 *     {% endfor %}
24
 *   </ul>
25
 *   <div class="comment-form">
26
 *     {{ function('comment_form') }}
27
 *   </div>
28
 * </div>
29
 * ```
30
 *
31
 * ```twig
32
 * {# comment.twig #}
33
 * <li>
34
 *   <div>{{ comment.content }}</div>
35
 *   <p class="comment-author">{{ comment.author.name }}</p>
36
 *   {{ function('comment_form') }}
37
 *   <!-- nested comments here -->
38
 *   {% if comment.children %}
39
 *     <div class="replies">
40
 *             {% for child_comment in comment.children %}
41
 *         {% include 'comment.twig' with { comment:child_comment } %}
42
 *       {% endfor %}
43
 *     </div>
44
 *   {% endif %}
45
 * </li>
46
 * ```
47
 */
48
class CommentThread extends ArrayObject
49
{
50
    public $_orderby = '';
51

52
    public $_order = 'ASC';
53

54
    /**
55
     * Creates a new `Timber\CommentThread` object.
56
     *
57
     * @param int           $post_id The post ID.
58
     * @param array|boolean $args    Optional. An array of arguments or false if initialization
59
     *                               should be skipped.
60
     */
61
    public function __construct(
15✔
62
        public $post_id,
63
        $args = []
64
    ) {
65
        parent::__construct();
15✔
66
        if ($args || \is_array($args)) {
15✔
67
            $this->init($args);
2✔
68
        }
69
    }
70

71
    /**
72
     * @internal
73
     */
74
    protected function fetch_comments($args = [])
15✔
75
    {
76
        $args['post_id'] = $this->post_id;
15✔
77
        $comments = \get_comments($args);
15✔
78
        return $comments;
15✔
79
    }
80

81
    /**
82
     * Gets the number of comments on a post.
83
     *
84
     * @return int The number of comments on a post.
85
     */
86
    public function mecount()
×
87
    {
88
        return \get_comments_number($this->post_id);
×
89
    }
90

91
    protected function merge_args($args)
15✔
92
    {
93
        $base = [
15✔
94
            'status' => 'approve',
15✔
95
            'order' => $this->_order,
15✔
96
        ];
15✔
97
        return \array_merge($base, $args);
15✔
98
    }
99

100
    /**
101
     * @internal
102
     */
103
    public function order($order = 'ASC')
2✔
104
    {
105
        $this->_order = $order;
2✔
106
        $this->init();
2✔
107
        return $this;
2✔
108
    }
109

110
    /**
111
       * @internal
112
       */
113
    public function orderby($orderby = 'wp')
1✔
114
    {
115
        $this->_orderby = $orderby;
1✔
116
        $this->init();
1✔
117
        return $this;
1✔
118
    }
119

120
    /**
121
     * Inits the object.
122
     *
123
   * @internal
124
     * @param array $args Optional.
125
     */
126
    public function init($args = [])
15✔
127
    {
128
        global $overridden_cpage;
129
        $args = self::merge_args($args);
15✔
130
        $comments = $this->fetch_comments($args);
15✔
131
        $tcs = [];
15✔
132
        if ('' == \get_query_var('cpage') && \get_option('page_comments')) {
15✔
133
            \set_query_var('cpage', 'newest' == \get_option('default_comments_page') ? \get_comment_pages_count() : 1);
×
134
            $overridden_cpage = true;
×
135
        }
136
        foreach ($comments as $key => &$comment) {
15✔
137
            $factory = new CommentFactory();
15✔
138
            $timber_comment = $factory->from($comment);
15✔
139
            $tcs[$timber_comment->id] = $timber_comment;
15✔
140
        }
141

142
        $parents = [];
15✔
143
        $children = [];
15✔
144

145
        foreach ($tcs as $comment) {
15✔
146
            if ($comment->is_child()) {
15✔
147
                $children[$comment->ID] = $comment;
7✔
148
            } else {
149
                $parents[$comment->ID] = $comment;
15✔
150
            }
151
        }
152

153
        foreach ($children as &$comment) {
15✔
154
            $parent_id = $comment->comment_parent;
7✔
155
            if (isset($parents[$parent_id])) {
7✔
156
                $parents[$parent_id]->add_child($comment);
7✔
157
            }
158
            if (isset($children[$parent_id])) {
7✔
159
                $children[$parent_id]->add_child($comment);
6✔
160
            }
161
        }
162
        //there's something in update_depth that breaks order?
163

164
        foreach ($parents as $comment) {
15✔
165
            $comment->update_depth();
15✔
166
        }
167
        $this->import_comments($parents);
15✔
168
    }
169

170
    /**
171
     * @internal
172
     */
173
    protected function clear()
15✔
174
    {
175
        $this->exchangeArray([]);
15✔
176
    }
177

178
    /**
179
     * @internal
180
     */
181
    protected function import_comments($arr)
15✔
182
    {
183
        $this->clear();
15✔
184
        $i = 0;
15✔
185
        foreach ($arr as $comment) {
15✔
186
            $this[$i] = $comment;
15✔
187
            $i++;
15✔
188
        }
189
    }
190
}
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