• 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

94.0
/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 $post_id;
51

52
    public $_orderby = '';
53

54
    public $_order = 'ASC';
55

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

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

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

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

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

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

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

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

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

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

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

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

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