| 1 |
<?php |
| 2 |
// phpcs:disable WordPress.DateTime.RestrictedFunctions.date_date |
| 3 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared |
| 4 |
namespace WML\Classes; |
| 5 |
|
| 6 |
use WML\Classes\Settings; |
| 7 |
|
| 8 |
use WP_REST_Controller; |
| 9 |
use WP_REST_Server; |
| 10 |
use WP_Query; |
| 11 |
use WP_REST_Request; |
| 12 |
use WP_Error; |
| 13 |
use WP_REST_Response; |
| 14 |
|
| 15 |
/** |
| 16 |
* Plugin API Endpoints |
| 17 |
* |
| 18 |
* This class is to manage plugin api endpoints |
| 19 |
* |
| 20 |
* @extends WP_REST_Controller |
| 21 |
* |
| 22 |
*/ |
| 23 |
class API extends WP_REST_Controller { |
| 24 |
|
| 25 |
|
| 26 |
protected $namespace = 'wml/v1'; |
| 27 |
/** |
| 28 |
* The constructor of class. Automatically call when class object create |
| 29 |
* |
| 30 |
* @since 0.3 |
| 31 |
* @return void |
| 32 |
* @access public |
| 33 |
* |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* This function is called on rest_api_init action in bootstrap file. |
| 40 |
* |
| 41 |
* This function will register the rest api endpoints. |
| 42 |
* |
| 43 |
* @since 0.3 |
| 44 |
* @return void |
| 45 |
* @access public |
| 46 |
* |
| 47 |
*/ |
| 48 |
|
| 49 |
public function register_routes() { |
| 50 |
register_rest_route( |
| 51 |
$this->namespace, |
| 52 |
'/wml_logs', |
| 53 |
// Get Log -> done |
| 54 |
[ |
| 55 |
[ |
| 56 |
'methods' => WP_REST_Server::CREATABLE, |
| 57 |
'callback' => [ $this, 'view_log' ], |
| 58 |
'permission_callback' => [ $this, 'check_permission' ], |
| 59 |
], |
| 60 |
// // Delete Log -> done |
| 61 |
// [ |
| 62 |
// 'methods' => WP_REST_Server::DELETABLE, |
| 63 |
// 'callback' => [ $this, 'delete_log' ], |
| 64 |
// 'permission_callback' => [ $this, 'check_permission' ], |
| 65 |
// ], |
| 66 |
] |
| 67 |
); |
| 68 |
register_rest_route( |
| 69 |
$this->namespace, |
| 70 |
'/wml_logs/delete', |
| 71 |
// Get Log -> done |
| 72 |
[ |
| 73 |
// Delete Log -> done |
| 74 |
[ |
| 75 |
'methods' => WP_REST_Server::CREATABLE, |
| 76 |
'callback' => [ $this, 'delete_log' ], |
| 77 |
'permission_callback' => [ $this, 'check_permission' ], |
| 78 |
], |
| 79 |
] |
| 80 |
); |
| 81 |
register_rest_route( |
| 82 |
$this->namespace, |
| 83 |
'/settings', |
| 84 |
// Save Settings done |
| 85 |
[ |
| 86 |
[ |
| 87 |
'methods' => WP_REST_Server::CREATABLE, |
| 88 |
'callback' => [ $this, 'save_settings' ], |
| 89 |
'permission_callback' => [ $this, 'check_permission' ], |
| 90 |
], |
| 91 |
] |
| 92 |
); |
| 93 |
register_rest_route( |
| 94 |
$this->namespace, |
| 95 |
'/wml_logs/send_mail', |
| 96 |
// Save Settings done |
| 97 |
[ |
| 98 |
[ |
| 99 |
'methods' => WP_REST_Server::CREATABLE, |
| 100 |
'callback' => [ $this, 'send_email' ], |
| 101 |
'permission_callback' => [ $this, 'check_permission' ], |
| 102 |
], |
| 103 |
] |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* This function return the JSON params to array from request. |
| 109 |
* |
| 110 |
* @since 0.3 |
| 111 |
* @param object $reuqest WP Rest Request |
| 112 |
* @return array |
| 113 |
* @access private |
| 114 |
* |
| 115 |
*/ |
| 116 |
|
| 117 |
private function get_params( $request ) { |
| 118 |
return $request->get_json_params(); |
| 119 |
} |
| 120 |
private function make_params() { |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* This function is called on wml_log api endpoint with creatable method. |
| 125 |
* |
| 126 |
* This function return the result of logs based on params. |
| 127 |
* |
| 128 |
* @since 0.3 |
| 129 |
* @param \WP_REST_Request $request Full data about the request. |
| 130 |
* @return \WP_REST_Response Response object log result based on params |
| 131 |
* @access public |
| 132 |
* |
| 133 |
*/ |
| 134 |
public function view_log( WP_REST_Request $request ) { |
| 135 |
global $wpdb; |
| 136 |
$params = $this->get_params( $request ); |
| 137 |
|
| 138 |
$table_name = $wpdb->prefix . 'wml_entries'; |
| 139 |
|
| 140 |
$query_cols = [ 'id', 'to_email', 'subject', 'message', 'headers', 'attachments', "DATE_FORMAT(sent_date, '%Y/%m/%d %H:%i:%S') as sent_date", 'attachments_file as files' ]; |
| 141 |
$entry_query = 'SELECT distinct ' . implode( ',', $query_cols ) . ' FROM ' . $table_name; |
| 142 |
$where[] = '1 = 1'; |
| 143 |
|
| 144 |
if ( empty( $params['startDate'] ) ) { |
| 145 |
$params['startDate'] = date( 'Y-m-d H:i:s', strtotime( '-30 days' ) ); |
| 146 |
} |
| 147 |
if ( empty( $params['endDate'] ) ) { |
| 148 |
$params['endDate'] = date( 'Y-m-d H:i:s' ); |
| 149 |
} |
| 150 |
if ( $params['startDate'] !== '' && $params['startDate'] !== null ) { |
| 151 |
$orignalStartDateTS = strtotime( $params['startDate'] ); |
| 152 |
$params['startDate'] = date( 'Y-m-d', $orignalStartDateTS ); |
| 153 |
$where[] = " DATE_FORMAT(sent_date,GET_FORMAT(DATE,'JIS')) >= '" . $params['startDate'] . "'"; |
| 154 |
} |
| 155 |
if ( $params['endDate'] !== '' && $params['endDate'] !== null ) { |
| 156 |
$orignalEndDateTS = strtotime( $params['endDate'] ); |
| 157 |
$params['endDate'] = date( 'Y-m-d', $orignalEndDateTS ); |
| 158 |
if ( $params['startDate'] !== '' ) { |
| 159 |
$where[] = " DATE_FORMAT(sent_date,GET_FORMAT(DATE,'JIS')) <= '" . $params['endDate'] . "'"; |
| 160 |
} else { |
| 161 |
$where[] = "DATE_FORMAT(sent_date,GET_FORMAT(DATE,'JIS')) <= '" . $params['endDate'] . "'"; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// Filter Query |
| 166 |
$vars = []; |
| 167 |
if($params['filter']){ |
| 168 |
foreach ($params['filter'] as $key => $value) { |
| 169 |
if($value['key'] !== ''){ |
| 170 |
$operator = $value['operator']; |
| 171 |
if ( $operator === 'LIKE' || $operator == 'NOT LIKE' ) { |
| 172 |
$value['value'] = "%{$value['value']}%"; |
| 173 |
} |
| 174 |
$vars[] = " ( {$value['key']} {$value['operator']} '{$value['value']}' ) "; |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
if($vars){ |
| 180 |
$where[] = implode( $params['filterRelation'], $vars ); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
// Order By |
| 185 |
$orderby = ' order by id desc'; |
| 186 |
|
| 187 |
if ( $params['pageIndex'] >= 1 ) { |
| 188 |
$limit = ' limit ' . $params['pageSize'] * $params['pageIndex'] . ',' . $params['pageSize']; |
| 189 |
} else { |
| 190 |
$limit = ' limit ' . $params['pageSize']; |
| 191 |
} |
| 192 |
$entry_query .= ' WHERE ' . implode( ' and ', $where ) . $orderby . $limit; |
| 193 |
// echo 'query ' . $entry_query; |
| 194 |
// die(); |
| 195 |
$sql = $wpdb->get_results( $entry_query ); |
| 196 |
|
| 197 |
$cols = []; |
| 198 |
|
| 199 |
foreach ( $wpdb->get_col( 'DESC ' . $table_name, 0 ) as $column_name ) { |
| 200 |
$cols[] = $column_name; |
| 201 |
} |
| 202 |
$entry_count_query = 'SELECT count(id) from ' . $table_name . ' WHERE ' . implode( ' and ', $where ); |
| 203 |
|
| 204 |
$entry_result = $wpdb->get_var( $entry_count_query ); |
| 205 |
$rowcount = $wpdb->num_rows; |
| 206 |
$columns = [ 'id', 'to_email', 'subject', 'message', 'headers', 'sent_date', 'files' ]; |
| 207 |
|
| 208 |
foreach ( $sql as $key => $row ) { |
| 209 |
|
| 210 |
if($row->files !== '' && $row->files !== null){ |
| 211 |
$files = explode(',',$row->files); |
| 212 |
$attachments = []; |
| 213 |
if($files){ |
| 214 |
foreach ($files as $key => $value) { |
| 215 |
$url = wp_upload_dir()['baseurl'].$value; |
| 216 |
$fileExist = file_exists(wp_upload_dir()['basedir'].$value); |
| 217 |
|
| 218 |
$fileName = substr($value,strripos($value, '/') + 1, strlen($value)); |
| 219 |
$attachments[$key] = [ |
| 220 |
'name'=> $fileName, |
| 221 |
'path'=> $value, |
| 222 |
'exist'=> $fileExist, |
| 223 |
]; |
| 224 |
} |
| 225 |
$row->files = implode(' ', $files) ; |
| 226 |
$row->dataFile = $attachments; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
// Issue with wp forms data so commented |
| 231 |
// $formatedTag = wp_kses( $row->message, $this->wml_kses_allowed_html( 'post' ) ); |
| 232 |
// $row->message = $formatedTag; |
| 233 |
} |
| 234 |
|
| 235 |
$res = [ |
| 236 |
'columns' => $columns, |
| 237 |
'data' => $sql, |
| 238 |
'totalRows' => $entry_result, |
| 239 |
'rowCount' => $rowcount, |
| 240 |
]; |
| 241 |
|
| 242 |
return rest_ensure_response( $res ); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* This function is called on wml_log api endpoint with deletable method. |
| 247 |
* |
| 248 |
* This function return wp rest response on basis of result of delete. |
| 249 |
* |
| 250 |
* @since 0.6 |
| 251 |
* @param int $id id to get data |
| 252 |
* @return $results query results |
| 253 |
* @access public |
| 254 |
* |
| 255 |
*/ |
| 256 |
public function get_data_by_id( $id ) { |
| 257 |
global $wpdb; |
| 258 |
|
| 259 |
$table_name = $wpdb->prefix . 'wml_entries'; |
| 260 |
|
| 261 |
$query_cols = [ 'id', 'subject', 'message', 'headers', 'attachments', "DATE_FORMAT(sent_date, '%Y/%m/%d %H:%i:%S') as sent_date, attachments_file as files" ]; |
| 262 |
$entry_query = 'SELECT distinct ' . implode( ',', $query_cols ) . ' FROM ' . $table_name . ' WHERE id=' . $id; |
| 263 |
|
| 264 |
$result = $wpdb->get_results( $entry_query ); |
| 265 |
|
| 266 |
return $result[0]; |
| 267 |
} |
| 268 |
/** |
| 269 |
* This function is called on wml_log api endpoint with deletable method. |
| 270 |
* |
| 271 |
* This function return wp rest response on basis of result of delete. |
| 272 |
* |
| 273 |
* @since 0.3 |
| 274 |
* @param \WP_REST_Request $request Full data about the request. |
| 275 |
* @return \WP_REST_Response Response object on success |
| 276 |
* @access public |
| 277 |
* |
| 278 |
*/ |
| 279 |
public function delete_log( WP_REST_Request $request ) { |
| 280 |
|
| 281 |
global $wpdb; |
| 282 |
$ids = $this->get_params( $request ); |
| 283 |
$message = []; |
| 284 |
|
| 285 |
$table_name = $wpdb->prefix . 'wml_entries'; |
| 286 |
// $deleteRow = "Delete from {$table_name} where id IN (" . implode( ',', $ids ) . ')'; |
| 287 |
$idsPlaceholder = implode( ', ', array_fill( 0, count( $ids ), '%d' ) ); |
| 288 |
// PHPCS:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 289 |
$deleteRow = $wpdb->prepare( "Delete from {$table_name} where id IN ($idsPlaceholder)", $ids ); |
| 290 |
|
| 291 |
$dl1 = $wpdb->query( $deleteRow ); |
| 292 |
if ( $dl1 === 0 ) { |
| 293 |
$message['status'] = 'failed'; |
| 294 |
$message['message'] = 'Could not able to delete Entries'; |
| 295 |
} else { |
| 296 |
$message['status'] = 'passed'; |
| 297 |
$message['message'] = 'Entries Deleted'; |
| 298 |
} |
| 299 |
return rest_ensure_response( $message ); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* This function is called on wml_log api endpoint with deletable method. |
| 304 |
* |
| 305 |
* This function return wp rest response on basis of result of delete. |
| 306 |
* |
| 307 |
* @since 0.3 |
| 308 |
* @param string $context |
| 309 |
* @return array allowed html tags in content |
| 310 |
* @access protected |
| 311 |
* |
| 312 |
*/ |
| 313 |
protected function wml_kses_allowed_html( $context = 'post' ) { |
| 314 |
|
| 315 |
$allowed_tags = wp_kses_allowed_html( $context ); |
| 316 |
|
| 317 |
$allowed_tags['link'] = [ |
| 318 |
'rel' => true, |
| 319 |
'href' => true, |
| 320 |
'type' => true, |
| 321 |
'media' => true, |
| 322 |
]; |
| 323 |
|
| 324 |
return $allowed_tags; |
| 325 |
} |
| 326 |
/** |
| 327 |
* Check if the user has the permission to edit posts |
| 328 |
* @access public |
| 329 |
* @since 0.3 |
| 330 |
* @return bool|\WP_Error True on has permission, or WP_Error object on failure. |
| 331 |
*/ |
| 332 |
public function check_permission() { |
| 333 |
// Restrict endpoint to only users who have the edit_posts capability. |
| 334 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 335 |
return new WP_Error( 'rest_forbidden', esc_html__( 'OMG you can not view private data.', 'wpv-wml' ), [ 'status' => 401 ] ); |
| 336 |
} |
| 337 |
|
| 338 |
// This is a black-listing approach. You could alternatively do this via white-listing, by returning false here and changing the permissions check. |
| 339 |
return true; |
| 340 |
} |
| 341 |
/** |
| 342 |
* This function is to save settings |
| 343 |
* @access public |
| 344 |
* @since 0.3 |
| 345 |
* @param \WP_REST_Request $request Full data about the request. |
| 346 |
* @return \WP_REST_Response Response object on success |
| 347 |
*/ |
| 348 |
public function save_settings( WP_REST_Request $request ) { |
| 349 |
// TODO :: need to update return type with wp_rest_respnse |
| 350 |
$params = $this->get_params( $request ); |
| 351 |
$callback = $params['callback']; |
| 352 |
$settings = new Settings(); |
| 353 |
$res = $settings->$callback( $params ); |
| 354 |
wp_send_json( $res ); |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* This function is to send email |
| 359 |
* @access public |
| 360 |
* @since 0.5 |
| 361 |
* @param \WP_REST_Request $request Full data about the request. |
| 362 |
* @return \WP_REST_Response Response object on success |
| 363 |
*/ |
| 364 |
|
| 365 |
public function send_email( WP_REST_Request $request ) { |
| 366 |
$uploadDir = trailingslashit(wp_get_upload_dir()['basedir']); |
| 367 |
|
| 368 |
$params = $request->get_body_params(); |
| 369 |
$files = $request->get_file_params(); |
| 370 |
|
| 371 |
$id = $params['id']; |
| 372 |
$type = $params['type']; |
| 373 |
$mail_data = (array) $this->get_data_by_id( $id ); |
| 374 |
|
| 375 |
$email = $params['to_email']; |
| 376 |
$subject = $mail_data['subject']; |
| 377 |
$message = $mail_data['message']; |
| 378 |
$attachments = []; |
| 379 |
$newFiles = []; |
| 380 |
|
| 381 |
$includeAttachment = json_decode($params['includeAttachment']); |
| 382 |
|
| 383 |
// Attach original files |
| 384 |
if($includeAttachment){ |
| 385 |
foreach ($includeAttachment as $key => $value) { |
| 386 |
$attachments[] = $uploadDir . $key; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
// Attach Uploaded files |
| 391 |
foreach ($files as $key => $value) { |
| 392 |
$extension = pathinfo($files[$key]['name'], PATHINFO_EXTENSION); |
| 393 |
$time = time(); |
| 394 |
$targetFile = trailingslashit(wp_upload_dir()['basedir']). $time . '.' . $extension; |
| 395 |
$targetUrl = $uploadDir . $time . '.' . $extension; |
| 396 |
|
| 397 |
move_uploaded_file($files[$key]['tmp_name'], $targetFile); |
| 398 |
$attachments[] = $targetUrl; |
| 399 |
$newFiles[] = $targetFile; |
| 400 |
} |
| 401 |
|
| 402 |
$headers = ''; |
| 403 |
if ( $type === 'forward' ) { |
| 404 |
$headers = $mail_data['headers']; |
| 405 |
if($headers == ''){ |
| 406 |
$headers = 'Content-Type: text/html'; |
| 407 |
} |
| 408 |
} else { |
| 409 |
$orignalFiles = explode(',',$mail_data['files']); |
| 410 |
foreach ($orignalFiles as $key => $value ) { |
| 411 |
if($value){ |
| 412 |
$attachments[] = $uploadDir . trim($value); |
| 413 |
} |
| 414 |
} |
| 415 |
$headers = $params['headers']; |
| 416 |
} |
| 417 |
|
| 418 |
$response = wp_mail( $email, $subject, $message, $headers, $attachments ); |
| 419 |
|
| 420 |
foreach ($newFiles as $key => $value) { |
| 421 |
wp_delete_file($value); |
| 422 |
} |
| 423 |
return rest_ensure_response( $response ); |
| 424 |
} |
| 425 |
} |
| 426 |
|