Passed
Pull Request — master (#41)
by Kiran
04:19
created

WPInv_Notes::delete_comments_count_cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
    exit; // Exit if accessed directly
4
}
5
6
class WPInv_Notes {
7
    private $invoice_post_type  = 'wpi_invoice';
8
    private $comment_type       = 'wpinv_note';
9
    
10
    public function __construct() {
11
        $this->init();
12
        $this->includes();
13
        $this->actions();
14
    }
15
    
16
    public function init() {
17
        do_action( 'wpinv_class_notes_init', $this );
18
    }
19
    
20
    public function includes() {
21
        do_action( 'wpinv_class_notes_includes', $this );
22
    }
23
    
24
    public function actions() {
25
        // Secure inovice notes
26
        add_action( 'pre_get_comments', array( $this, 'set_invoice_note_type' ), 11, 1 );
27
        
28
        // Count comments
29
        add_filter( 'wp_count_comments', array( $this, 'wp_count_comments' ), 11, 2 );
30
        
31
        // Delete comments count cache whenever there is a new comment or a comment status changes
32
        add_action( 'wp_insert_comment', array( $this, 'delete_comments_count_cache' ) );
33
        add_action( 'wp_set_comment_status', array( $this, 'delete_comments_count_cache' ) );
34
        
35
        do_action( 'wpinv_class_notes_actions', $this );
36
    }
37
        
38
    public function set_invoice_note_type( $query ) {
39
        $post_ID        = !empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id'];
40
        
41
        if ( $post_ID && get_post_type( $post_ID ) == $this->invoice_post_type ) {
42
            $query->query_vars['type__in']      = $this->comment_type;
43
            $query->query_vars['type__not_in']  = '';
44
        } else {        
45
            if ( isset( $query->query_vars['type__in'] ) && $type_in = $query->query_vars['type__in'] ) {
46
                if ( is_array( $type_in ) && in_array( $this->comment_type, $type_in ) ) {
47
                    $key = array_search( $this->comment_type, $type_in );
48
                    unset( $query->query_vars['type__in'][$key] );
49
                } else if ( !is_array( $type_in ) && $type_in == $this->comment_type ) {
50
                    $query->query_vars['type__in'] = '';
51
                }
52
            }
53
            
54
            if ( isset( $query->query_vars['type__not_in'] ) && $type_not_in = $query->query_vars['type__not_in'] ) {
55
                if ( is_array( $type_not_in ) && !in_array( $this->comment_type, $type_not_in ) ) {
56
                    $query->query_vars['type__not_in'][] = $this->comment_type;
57
                } else if ( !is_array( $type_not_in ) && $type_not_in != $this->comment_type ) {
58
                    $query->query_vars['type__not_in'] = (array)$query->query_vars['type__not_in'];
59
                    $query->query_vars['type__not_in'][] = $this->comment_type;
60
                }
61
            } else {
62
                $query->query_vars['type__not_in']  = $this->comment_type;
63
            }
64
        }
65
        
66
        return $query;
67
    }
68
    
69
    public function get_invoice_notes( $invoice_id = 0, $type = '' ) {
70
        $args = array( 
71
            'post_id'   => $invoice_id,
72
            'order'     => 'comment_date_gmt',
73
            'order'     => 'DESC',
74
        );
75
        
76
        if ( $type == 'customer' ) {
77
            $args['meta_key']   = '_wpi_customer_note';
78
            $args['meta_value'] = 1;
79
        }
80
        
81
        $args   = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type );
82
        
83
        return get_comments( $args );
84
    }
85
    
86
    /**
87
     * Delete comments count cache whenever there is new comment or the 
88
     * status of a comment changes. Cache will be regenerated next time 
89
     * WPInv_Notes::wp_count_comments() is called.
90
     *
91
     * @return void
92
     */
93
    public function delete_comments_count_cache() {
94
        delete_transient( 'wpinv_count_comments' );
95
    }
96
    
97
    /**
98
     * Remove invoice notes from wp_count_comments().
99
     *
100
     * @since  1.0.0
101
     * @param  object $stats   Comment stats.
102
     * @param  int    $post_id Post ID.
103
     * @return object
104
     */
105
    public function wp_count_comments( $stats, $post_id ) {
106
        global $wpdb;
107
108
        if ( 0 === $post_id ) {
109
            $stats = get_transient( 'wpinv_count_comments' );
110
111
            if ( ! $stats ) {
112
                $stats = array();
113
114
                $count = $wpdb->get_results( "SELECT comment_approved, COUNT(*) AS num_comments FROM {$wpdb->comments} WHERE comment_type NOT IN ('" . $this->comment_type . "') GROUP BY comment_approved", ARRAY_A );
115
116
                $total = 0;
117
                $approved = array(
118
                    '0'            => 'moderated',
119
                    '1'            => 'approved',
120
                    'spam'         => 'spam',
121
                    'trash'        => 'trash',
122
                    'post-trashed' => 'post-trashed',
123
                );
124
125
                foreach ( (array) $count as $row ) {
126
                    // Do not count post-trashed toward totals.
127
                    if ( 'post-trashed' !== $row['comment_approved'] && 'trash' !== $row['comment_approved'] ) {
128
                        $total += $row['num_comments'];
129
                    }
130
                    if ( isset( $approved[ $row['comment_approved'] ] ) ) {
131
                        $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
132
                    }
133
                }
134
135
                $stats['total_comments'] = $total;
136
                $stats['all'] = $total;
137
                foreach ( $approved as $key ) {
138
                    if ( empty( $stats[ $key ] ) ) {
139
                        $stats[ $key ] = 0;
140
                    }
141
                }
142
143
                $stats = (object) $stats;
144
                set_transient( 'wpinv_count_comments', $stats );
145
            }
146
        }
147
148
        return $stats;
149
    }
150
}
151