/var/www/(Del)hsihk.com/wp-content/plugins/woocommerce/includes/class-wc-comments.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php

if ( ! defined'ABSPATH' ) ) {
    exit; 
// Exit if accessed directly
}

/**
 * Comments
 *
 * Handle comments (reviews and order notes)
 *
 * @class         WC_Post_types
 * @version        2.2.0
 * @package        WooCommerce/Classes/Products
 * @category    Class
 * @author         WooThemes
 */
class WC_Comments {

    
/**
     * Hook in methods
     */
    
public static function init() {
        
// Rating posts
        
add_filter'preprocess_comment', array( __CLASS__'check_comment_rating' ), );
        
add_action'comment_post', array( __CLASS__'add_comment_rating' ), );

        
// clear transients
        
add_action'wp_update_comment_count', array( __CLASS__'clear_transients' ) );

        
// Secure order notes
        
add_filter'comments_clauses', array( __CLASS__'exclude_order_comments' ), 10);
        
add_action'comment_feed_join', array( __CLASS__'exclude_order_comments_from_feed_join' ) );
        
add_action'comment_feed_where', array( __CLASS__'exclude_order_comments_from_feed_where' ) );

        
// Secure webhook comments
        
add_filter'comments_clauses', array( __CLASS__'exclude_webhook_comments' ), 10);
        
add_action'comment_feed_join', array( __CLASS__'exclude_webhook_comments_from_feed_join' ) );
        
add_action'comment_feed_where', array( __CLASS__'exclude_webhook_comments_from_feed_where' ) );

        
// Count comments
        
add_filter'wp_count_comments', array( __CLASS__'wp_count_comments' ), 10);
    }

    
/**
     * Exclude order comments from queries and RSS
     *
     * This code should exclude shop_order comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded
     * and are not filtered, however, the code current_user_can( 'read_post', $comment->comment_post_ID ) should keep them safe since only admin and
     * shop managers can view orders anyway.
     *
     * The frontend view order pages get around this filter by using remove_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments'), 10, 1 );
     *
     * @param array $clauses
     * @return array
     */
    
public static function exclude_order_comments$clauses ) {
        global 
$wpdb$typenow;

        if ( 
is_admin() && in_array$typenowwc_get_order_types() ) && current_user_can'manage_woocommerce' ) ) {
            return 
$clauses// Don't hide when viewing orders in admin
        
}

        if ( ! 
$clauses['join'] ) {
            
$clauses['join'] = '';
        }

        if ( ! 
strstr$clauses['join'], "JOIN $wpdb->posts) ) {
            
$clauses['join'] .= " LEFT JOIN $wpdb->posts ON comment_post_ID = $wpdb->posts.ID ";
        }

        if ( 
$clauses['where'] ) {
            
$clauses['where'] .= ' AND ';
        }

        
$clauses['where'] .= $wpdb->posts.post_type NOT IN ('" implode"','"wc_get_order_types() ) . "') ";

        return 
$clauses;
    }

    
/**
     * Exclude order comments from queries and RSS
     *
     * @param string $join
     * @return string
     */
    
public static function exclude_order_comments_from_feed_join$join ) {
        global 
$wpdb;

        if ( ! 
strstr$join$wpdb->posts ) ) {
            
$join " LEFT JOIN $wpdb->posts ON $wpdb->comments.comment_post_ID = $wpdb->posts.ID ";
        }

        return 
$join;
    }

    
/**
     * Exclude order comments from queries and RSS
     *
     * @param string $where
     * @return string
     */
    
public static function exclude_order_comments_from_feed_where$where ) {
        global 
$wpdb;

        if ( 
$where ) {
            
$where .= ' AND ';
        }

        
$where .= $wpdb->posts.post_type NOT IN ('" implode"','"wc_get_order_types() ) . "') ";

        return 
$where;
    }

    
/**
     * Exclude webhook comments from queries and RSS
     *
     * @since 2.2
     * @param array $clauses
     * @return array
     */
    
public static function exclude_webhook_comments$clauses ) {
        global 
$wpdb;

        if ( ! 
$clauses['join'] ) {
            
$clauses['join'] = '';
        }

        if ( ! 
strstr$clauses['join'], "JOIN $wpdb->posts) ) {
            
$clauses['join'] .= " LEFT JOIN $wpdb->posts ON comment_post_ID = $wpdb->posts.ID ";
        }

        if ( 
$clauses['where'] ) {
            
$clauses['where'] .= ' AND ';
        }

        
$clauses['where'] .= $wpdb->posts.post_type <> 'shop_webhook' ";

        return 
$clauses;
    }

    
/**
     * Exclude webhook comments from queries and RSS
     *
     * @since 2.2
     * @param string $join
     * @return string
     */
    
public static function exclude_webhook_comments_from_feed_join$join ) {
        global 
$wpdb;

        if ( ! 
strstr$join$wpdb->posts ) ) {
            
$join " LEFT JOIN $wpdb->posts ON $wpdb->comments.comment_post_ID = $wpdb->posts.ID ";
        }

        return 
$join;
    }

    
/**
     * Exclude webhook comments from queries and RSS
     *
     * @since 2.1
     * @param string $where
     * @return string
     */
    
public static function exclude_webhook_comments_from_feed_where$where ) {
        global 
$wpdb;

        if ( 
$where ) {
            
$where .= ' AND ';
        }

        
$where .= $wpdb->posts.post_type <> 'shop_webhook' ";

        return 
$where;
    }

    
/**
     * Validate the comment ratings.
     *
     * @param array $comment_data
     * @return array
     */
    
public static function check_comment_rating$comment_data ) {
        
// If posting a comment (not trackback etc) and not logged in
        
if ( isset( $_POST['rating'] ) && empty( $_POST['rating'] ) && '' === $comment_data['comment_type'] && 'yes' === get_option'woocommerce_review_rating_required' ) ) {
            
wp_die__'Please rate the product.''woocommerce' ) );
            exit;
        }
        return 
$comment_data;
    }

    
/**
     * Rating field for comments.
     *
     * @param mixed $comment_id
     */
    
public static function add_comment_rating$comment_id ) {
        if ( isset( 
$_POST['rating'] ) ) {
            if ( ! 
$_POST['rating'] || $_POST['rating'] > || $_POST['rating'] < ) {
                return;
            }

            
add_comment_meta$comment_id'rating', (int) esc_attr$_POST['rating'] ), true );
        }
    }

    
/**
     * Clear transients for a review.
     *
     * @param mixed $post_id
     */
    
public static function clear_transients$post_id ) {
        
$post_id absint$post_id );
        
delete_transient'wc_average_rating_' $post_id );
        
delete_transient'wc_rating_count_' $post_id );
        
delete_transient'wc_rating_count_' $post_id '_1' );
        
delete_transient'wc_rating_count_' $post_id '_2' );
        
delete_transient'wc_rating_count_' $post_id '_3' );
        
delete_transient'wc_rating_count_' $post_id '_4' );
        
delete_transient'wc_rating_count_' $post_id '_5' );
    }

    
/**
     * Remove order notes from wp_count_comments()
     *
     * @since 2.2
     * @param object $stats
     * @param int $post_id
     * @return object
     */
    
public static function wp_count_comments$stats$post_id ) {
        global 
$wpdb;

        if ( 
=== $post_id ) {

            
$count wp_cache_get'comments-0''counts' );
            if ( 
false !== $count ) {
                return 
$count;
            }

            
$count $wpdb->get_results"SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} WHERE comment_type != 'order_note' GROUP BY comment_approved"ARRAY_A );

            
$total 0;
            
$approved = array( '0' => 'moderated''1' => 'approved''spam' => 'spam''trash' => 'trash''post-trashed' => 'post-trashed' );

            foreach ( (array) 
$count as $row ) {
                
// Don't count post-trashed toward totals
                
if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) {
                    
$total += $row['num_comments'];
                }
                if ( isset( 
$approved$row['comment_approved'] ] ) ) {
                    
$stats$approved$row['comment_approved'] ] ] = $row['num_comments'];
                }
            }

            
$stats['total_comments'] = $total;
            foreach ( 
$approved as $key ) {
                if ( empty( 
$stats$key ] ) ) {
                    
$stats$key ] = 0;
                }
            }

            
$stats = (object) $stats;
            
wp_cache_set'comments-0'$stats'counts' );
        }

        return 
$stats;
    }
}

WC_Comments::init();