Notes.php
3.7 KB
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
<?php
namespace ACA\WC\Column\Order;
use AC;
use ACA\WC\Editing;
use ACA\WC\Search;
use ACA\WC\Settings;
use ACA\WC\Settings\ShopOrder\NoteType;
use ACP;
use DateTime;
class Notes extends AC\Column implements ACP\Editing\Editable {
public function __construct() {
$this->set_type( 'column-order_note' )
->set_label( __( 'Order Notes', 'woocommerce' ) )
->set_group( 'woocommerce' );
}
public function get_value( $id ) {
switch ( $this->get_display_property() ) {
case Settings\ShopOrder\Notes::LATEST_VALUE:
$value = $this->get_latest_value( $id );
break;
case Settings\ShopOrder\Notes::COUNT_VALUE :
default:
$value = $this->get_count_value( $id );
}
return $value ?: $this->get_empty_char();
}
public function get_last_order_note( $id ) {
$notes = $this->get_order_notes( $id );
return count( $notes ) > 0 ? reset( $notes ) : null;
}
private function get_count_value( int $id ): string {
$notes = $this->get_order_notes( $id );
if ( ! $notes ) {
return '';
}
$content = [];
foreach ( $notes as $note ) {
$content[] = sprintf( '<small>%s</small><br>%s', DateTime::createFromFormat( 'Y-m-d H:i:s', $note->date )->format( 'F j, Y - H:i' ), $note->content );
}
array_map( 'strip_tags', $content );
return ac_helper()->html->tooltip( ac_helper()->html->rounded( count( $content ) ), implode( '<br><br>', $content ) );
}
private function get_latest_value( int $id ): string {
$note = $this->get_last_order_note( (int) $id );
return $note
? sprintf( '<small>%s</small><br>%s', DateTime::createFromFormat( 'Y-m-d H:i:s', $note->date )->format( 'F j, Y - H:i' ), $note->content )
: $this->get_empty_char();
}
private function get_order_notes( int $order_id ) {
global $wpdb;
$sql = $wpdb->prepare( "
SELECT cc.comment_content AS content, cc.comment_ID AS id, cc.comment_date AS date, cc.comment_author AS author, cm.meta_value AS is_customer_note
FROM $wpdb->comments AS cc
LEFT JOIN $wpdb->commentmeta AS cm ON cc.comment_ID = cm.comment_id AND cm.meta_key = 'is_customer_note'
WHERE cc.comment_post_ID = %d
ORDER BY cc.comment_date DESC
", $order_id );
$notes = $wpdb->get_results( $sql );
switch ( $this->get_note_type() ) {
case Settings\ShopOrder\NoteType::CUSTOMER_NOTE :
return array_filter( $notes, [ $this, 'is_customer_note' ] );
case Settings\ShopOrder\NoteType::PRIVATE_NOTE :
return array_filter( $notes, [ $this, 'is_private_note' ] );
case Settings\ShopOrder\NoteType::SYSTEM_NOTE :
return array_filter( $notes, [ $this, 'is_system_note' ] );
default :
return $notes;
}
}
public function register_settings(): void {
$this->add_setting( new Settings\ShopOrder\NoteType( $this ) );
$this->add_setting( new Settings\ShopOrder\Notes( $this ) );
}
private function is_private_note( $note ): bool {
return ! $this->is_customer_note( $note ) && ! $this->is_system_note( $note );
}
private function is_system_note( $note ): bool {
return __( 'WooCommerce', 'woocommerce' ) === $note->author;
}
private function is_customer_note( $note ): bool {
return '1' === $note->is_customer_note;
}
private function get_note_type(): string {
return $this->get_setting( Settings\ShopOrder\NoteType::NAME )->get_value();
}
private function get_display_property(): string {
return $this->get_setting( Settings\ShopOrder\Notes::NAME )->get_value();
}
public function editing() {
switch ( $this->get_note_type() ) {
case NoteType::PRIVATE_NOTE :
return new Editing\ShopOrder\NotesPrivate();
case NoteType::CUSTOMER_NOTE :
return new Editing\ShopOrder\NotesToCustomer();
case NoteType::SYSTEM_NOTE :
return new Editing\ShopOrder\NotesSystem();
default:
return false;
}
}
}