| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Classes; |
| 4 |
|
| 5 |
use StoreEngine\Classes\Exceptions\StoreEngineException; |
| 6 |
use StoreEngine\Classes\Exceptions\StoreEngineNotFoundException; |
| 7 |
use StoreEngine\Utils\Formatting; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class OrderNote extends AbstractEntity { |
| 14 |
protected string $table = 'comments'; |
| 15 |
protected string $meta_type = 'comment'; |
| 16 |
protected string $primary_key = 'comment_ID'; |
| 17 |
|
| 18 |
protected array $data = [ |
| 19 |
'date_created' => '0000-00-00 00:00:00', |
| 20 |
'date_created_gmt' => '0000-00-00 00:00:00', |
| 21 |
'content' => '', |
| 22 |
'is_customer_note' => false, |
| 23 |
'added_by' => '', |
| 24 |
'user_id' => 0, |
| 25 |
'user_email' => '', |
| 26 |
'user_agent' => '', |
| 27 |
'is_added_by_user' => false, |
| 28 |
'order_id' => 0, |
| 29 |
]; |
| 30 |
|
| 31 |
protected array $internal_meta_keys = [ 'is_customer_note' ]; |
| 32 |
|
| 33 |
protected function read_data(): array { |
| 34 |
$data = get_comment( $this->get_id() ); |
| 35 |
|
| 36 |
if ( ! $data ) { |
| 37 |
if ( $this->wpdb->last_error ) { |
| 38 |
/* translators: %s: Error message. */ |
| 39 |
throw new StoreEngineException( sprintf( esc_html__( 'Error reading order note from database. Error: %s', 'storeengine' ), esc_html( $this->wpdb->last_error ) ), 'db-read-error', [ 'id' => $this->get_id() ], 500 ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 40 |
} |
| 41 |
|
| 42 |
/* translators: %s: Data object type (order-note). */ |
| 43 |
throw new StoreEngineNotFoundException( sprintf( esc_html__( 'Order note (%s) not found.', 'storeengine' ), 'order-note' ), [ 'id' => $this->get_id() ] ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 44 |
} |
| 45 |
|
| 46 |
return apply_filters( 'storeengine/get_order_note', [ |
| 47 |
'date_created' => Formatting::string_to_datetime( $data->comment_date ), |
| 48 |
'date_created_gmt' => Formatting::string_to_datetime( $data->comment_date_gmt ), |
| 49 |
'content' => $data->comment_content, |
| 50 |
'is_customer_note' => (bool) $this->get_metadata( 'is_customer_note' ), |
| 51 |
'added_by' => _x( 'StoreEngine', 'System Comment Author', 'storeengine' ) === $data->comment_author ? 'system' : $data->comment_author, |
| 52 |
'user_id' => absint( $data->user_id ), |
| 53 |
'user_email' => $data->comment_author_email, |
| 54 |
'user_agent' => 'StoreEngine' === $data->comment_agent ? 'system' : $data->comment_agent, |
| 55 |
'is_added_by_user' => 'StoreEngine' !== $data->comment_agent, |
| 56 |
'order_id' => absint( $data->comment_post_ID ), |
| 57 |
], $data ); |
| 58 |
} |
| 59 |
|
| 60 |
public function create() { |
| 61 |
if ( is_user_logged_in() && current_user_can( 'edit_shop_orders', $this->get_id() ) && $this->get_is_added_by_user( 'edit' ) ) { |
| 62 |
$user = get_user_by( 'id', get_current_user_id() ); |
| 63 |
$comment_author = $user->display_name; |
| 64 |
$comment_author_email = $user->user_email; |
| 65 |
} else { |
| 66 |
$comment_author = _x( 'StoreEngine', 'System Comment Author', 'storeengine' ); |
| 67 |
$comment_author_email = strtolower( $comment_author ) . '@' . str_replace( 'www.', '', get_site_url( null, '', 'relative' ) ); // WPCS: input var ok. |
| 68 |
$comment_author_email = sanitize_email( $comment_author_email ); |
| 69 |
} |
| 70 |
|
| 71 |
$comment_data = apply_filters( |
| 72 |
'storeengine/new_order_note_data', |
| 73 |
[ |
| 74 |
'comment_post_ID' => $this->get_order_id(), |
| 75 |
'comment_author' => $comment_author, |
| 76 |
'comment_author_email' => $comment_author_email, |
| 77 |
'comment_author_url' => '', |
| 78 |
'comment_content' => $this->get_content( 'edit' ), |
| 79 |
'comment_agent' => 'StoreEngine', |
| 80 |
'comment_type' => 'order_note', |
| 81 |
'comment_parent' => 0, |
| 82 |
'comment_approved' => 1, |
| 83 |
], |
| 84 |
[ |
| 85 |
'order_id' => $this->get_order_id(), |
| 86 |
'is_customer_note' => $this->get_is_customer_note( 'edit' ), |
| 87 |
] |
| 88 |
); |
| 89 |
|
| 90 |
$comment_id = wp_insert_comment( $comment_data ); |
| 91 |
|
| 92 |
if ( ! $comment_id && $this->wpdb->last_error ) { |
| 93 |
throw new StoreEngineException( wp_kses_post( $this->wpdb->last_error ), 'db-error-insert-record' ); |
| 94 |
} |
| 95 |
|
| 96 |
$this->set_id( $comment_id ); |
| 97 |
$this->save_meta_data(); |
| 98 |
$this->apply_changes(); |
| 99 |
$this->clear_cache(); |
| 100 |
|
| 101 |
if ( $this->get_is_customer_note( 'edit' ) ) { |
| 102 |
add_comment_meta( $comment_id, 'is_customer_note', 1 ); |
| 103 |
do_action( 'storeengine/new_customer_note', [ |
| 104 |
'order_id' => $this->get_order_id(), |
| 105 |
'customer_note' => $comment_data['comment_content'], |
| 106 |
] ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public function update() { |
| 111 |
if ( ! $this->get_id() ) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
$this->save_meta_data(); |
| 116 |
|
| 117 |
$update = wp_update_comment( [ |
| 118 |
'comment_ID' => $this->get_id(), |
| 119 |
'comment_content' => $this->get_content( 'edit' ), |
| 120 |
], true ); |
| 121 |
|
| 122 |
if ( is_wp_error( $update ) ) { |
| 123 |
if ( 'db_update_error' === $update->get_error_code() ) { |
| 124 |
throw new StoreEngineException( esc_html__( 'Could not update order note in the database.', 'storeengine' ), 'db_update_error' ); |
| 125 |
} |
| 126 |
|
| 127 |
throw StoreEngineException::from_wp_error( $update ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 128 |
} |
| 129 |
|
| 130 |
if ( $this->wpdb->last_error ) { |
| 131 |
throw new StoreEngineException( wp_kses_post( $this->wpdb->last_error ), 'db-error-update-record' ); |
| 132 |
} |
| 133 |
|
| 134 |
$this->apply_changes(); |
| 135 |
$this->clear_cache(); |
| 136 |
} |
| 137 |
|
| 138 |
// getters |
| 139 |
|
| 140 |
public function get_date_created( string $context = 'view' ) { |
| 141 |
return $this->get_prop( 'date_created', $context ); |
| 142 |
} |
| 143 |
|
| 144 |
public function get_date_created_gmt( string $context = 'view' ) { |
| 145 |
return $this->get_prop( 'date_created_gmt', $context ); |
| 146 |
} |
| 147 |
|
| 148 |
public function get_content( string $context = 'view' ) { |
| 149 |
return $this->get_prop( 'content', $context ); |
| 150 |
} |
| 151 |
|
| 152 |
public function get_is_added_by_user( string $context = 'view' ) { |
| 153 |
return Formatting::string_to_bool( $this->get_prop( 'is_added_by_user', $context ) ); |
| 154 |
} |
| 155 |
|
| 156 |
public function get_is_customer_note( string $context = 'view' ) { |
| 157 |
return Formatting::string_to_bool( $this->get_prop( 'is_customer_note', $context ) ); |
| 158 |
} |
| 159 |
|
| 160 |
public function get_added_by( string $context = 'view' ) { |
| 161 |
return $this->get_prop( 'added_by', $context ); |
| 162 |
} |
| 163 |
|
| 164 |
public function get_user_id( string $context = 'view' ) { |
| 165 |
return $this->get_prop( 'user_id', $context ); |
| 166 |
} |
| 167 |
|
| 168 |
public function get_user_email( string $context = 'view' ) { |
| 169 |
return $this->get_prop( 'user_email', $context ); |
| 170 |
} |
| 171 |
|
| 172 |
public function get_user_agent( string $context = 'view' ) { |
| 173 |
return $this->get_prop( 'user_agent', $context ); |
| 174 |
} |
| 175 |
|
| 176 |
public function get_order_id( string $context = 'view' ) { |
| 177 |
return $this->get_prop( 'order_id', $context ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
// setters |
| 182 |
|
| 183 |
public function set_content( $value ) { |
| 184 |
$this->set_prop( 'content', $value ); |
| 185 |
} |
| 186 |
|
| 187 |
public function set_is_added_by_user( $value ) { |
| 188 |
if ( $this->get_is_added_by_user() && $this->get_id() ) { |
| 189 |
throw new StoreEngineException( esc_html__( 'Can not remove is-added-by-user flag from order note.', 'storeengine' ) ); |
| 190 |
} |
| 191 |
|
| 192 |
$this->set_prop( 'is_added_by_user', Formatting::string_to_bool( $value ) ); |
| 193 |
} |
| 194 |
|
| 195 |
public function set_is_customer_note( $value ) { |
| 196 |
if ( $this->get_is_customer_note() && $this->get_id() ) { |
| 197 |
throw new StoreEngineException( esc_html__( 'Can not remove is-customer-note flag from order note.', 'storeengine' ) ); |
| 198 |
} |
| 199 |
|
| 200 |
$this->set_prop( 'is_customer_note', Formatting::string_to_bool( $value ) ); |
| 201 |
} |
| 202 |
|
| 203 |
public function set_added_by( $value ) { |
| 204 |
if ( $this->get_added_by() && $this->get_id() ) { |
| 205 |
throw new StoreEngineException( esc_html__( 'Order note added-by can not be changed.', 'storeengine' ) ); |
| 206 |
} |
| 207 |
|
| 208 |
$this->set_prop( 'added_by', $value ); |
| 209 |
} |
| 210 |
|
| 211 |
public function set_user_id( $value ) { |
| 212 |
if ( $this->get_user_id() && $this->get_id() ) { |
| 213 |
throw new StoreEngineException( esc_html__( 'Order not user-id can not be changed.', 'storeengine' ) ); |
| 214 |
} |
| 215 |
|
| 216 |
$this->set_prop( 'user_id', absint( $value ) ); |
| 217 |
} |
| 218 |
|
| 219 |
public function set_user_email( string $value ) { |
| 220 |
if ( $this->get_user_email() && $this->get_id() ) { |
| 221 |
throw new StoreEngineException( esc_html__( 'Order not user-email can not be changed.', 'storeengine' ) ); |
| 222 |
} |
| 223 |
if ( $value && ! is_email( $value ) ) { |
| 224 |
$this->error( 'order_note_email', esc_html__( 'Invalid order note user email address.', 'storeengine' ) ); |
| 225 |
} |
| 226 |
|
| 227 |
$this->set_prop( 'user_email', sanitize_email( $value ) ); |
| 228 |
} |
| 229 |
|
| 230 |
public function set_user_agent( string $value ) { |
| 231 |
if ( $this->get_user_agent() && $this->get_id() ) { |
| 232 |
throw new StoreEngineException( esc_html__( 'Order not user-agent can not be changed.', 'storeengine' ) ); |
| 233 |
} |
| 234 |
|
| 235 |
$this->set_prop( 'user_agent', $value ); |
| 236 |
} |
| 237 |
|
| 238 |
public function set_order_id( $value ) { |
| 239 |
if ( $this->get_order_id() && $this->get_id() ) { |
| 240 |
throw new StoreEngineException( esc_html__( 'Order ID can not be changed.', 'storeengine' ) ); |
| 241 |
} |
| 242 |
|
| 243 |
$this->set_prop( 'order_id', absint( $value ) ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
// End of file order-note.php |
| 248 |
|