PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.3.13
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.3.13
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / includes / admin / class-ajax.php

class-ajax.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.3.13, at includes/admin/class-ajax.php

539 lines 18.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WeDevs\ERP\Admin;
3
4 use WeDevs\ERP\Admin\Models\Company_Locations;
5 use WeDevs\ERP\Company;
6 use WeDevs\ERP\Framework\Traits\Hooker;
7 use WeDevs\ERP\Framework\Models\APIKey;
8
9 /**
10 * The ajax handler class
11 *
12 * Handles the requests from core ERP, not from modules
13 */
14 class Ajax {
15
16 use \WeDevs\ERP\Framework\Traits\Ajax;
17 use Hooker;
18
19 /**
20 * Bind events
21 */
22 public function __construct() {
23 $this->action( 'wp_ajax_erp-company-location', 'location_create');
24 $this->action( 'wp_ajax_erp-delete-comp-location', 'location_remove');
25 $this->action( 'wp_ajax_erp_audit_log_view', 'view_edit_log_changes');
26 $this->action( 'wp_ajax_erp_file_upload', 'file_uploader' );
27 $this->action( 'wp_ajax_erp_file_del', 'file_delete' );
28 $this->action( 'wp_ajax_erp_people_exists', 'check_people' );
29 $this->action( 'wp_ajax_erp_smtp_test_connection', 'smtp_test_connection' );
30 $this->action( 'wp_ajax_erp_imap_test_connection', 'imap_test_connection' );
31 $this->action( 'wp_ajax_erp_import_users_as_contacts', 'import_users_as_contacts' );
32 $this->action( 'wp_ajax_erp-api-key', 'new_api_key');
33 $this->action( 'wp_ajax_erp-api-delete-key', 'delete_api_key');
34 $this->action( 'wp_ajax_erp-dismiss-promotional-offer-notice', 'dismiss_promotional_offer');
35 }
36
37 function file_delete() {
38 $this->verify_nonce( 'erp-nonce' );
39
40 $attach_id = isset( $_POST['attach_id'] ) ? $_POST['attach_id'] : 0;
41 $custom_attr = isset( $_POST['custom_attr'] ) ? $_POST['custom_attr'] : [];
42 $upload = new \WeDevs\ERP\Uploader();
43
44 if ( is_array( $attach_id) ) {
45 foreach ( $attach_id as $id ) {
46 do_action( 'erp_before_delete_file', $id, $custom_attr );
47 $delete = $upload->delete_file( $id );
48 }
49 } else {
50 do_action( 'erp_before_delete_file', $attach_id, $custom_attr );
51 $delete = $upload->delete_file( intval( $attach_id ) );
52 }
53
54 if ( $delete ) {
55 $this->send_success();
56 } else {
57 $this->send_error();
58 }
59 }
60
61 /**
62 * Upload a new file
63 *
64 * @return void
65 */
66 function file_uploader() {
67 $this->verify_nonce( 'erp-nonce' );
68 $upload = new \WeDevs\ERP\Uploader();
69 $file = $upload->upload_file();
70 $this->send_success( $file );
71 }
72
73 /**
74 * Create a new company location
75 *
76 * @return void
77 */
78 public function location_create() {
79 $this->verify_nonce( 'erp-company-location' );
80
81 $location_name = isset( $_POST['location_name'] ) ? sanitize_text_field( $_POST['location_name'] ) : '';
82 $address_1 = isset( $_POST['address_1'] ) ? sanitize_text_field( $_POST['address_1'] ) : '';
83 $address_2 = isset( $_POST['address_2'] ) ? sanitize_text_field( $_POST['address_2'] ) : '';
84 $city = isset( $_POST['city'] ) ? sanitize_text_field( $_POST['city'] ) : '';
85 $state = isset( $_POST['state'] ) ? sanitize_text_field( $_POST['state'] ) : '';
86 $zip = isset( $_POST['zip'] ) ? sanitize_text_field( $_POST['zip'] ) : '';
87 $country = isset( $_POST['country'] ) ? sanitize_text_field( $_POST['country'] ) : '';
88 $location_id = isset( $_POST['location_id'] ) ? intval( $_POST['location_id'] ) : 0;
89
90 $args = [
91 'id' => $location_id,
92 'name' => $location_name,
93 'address_1' => $address_1,
94 'address_2' => $address_2,
95 'city' => $city,
96 'state' => $state,
97 'zip' => $zip,
98 'country' => $country
99 ];
100
101 $company = new Company();
102 $location_id = $company->create_location( $args );
103
104 if ( is_wp_error( $location_id ) ) {
105 $this->send_error( $location_id->get_error_message() );
106 }
107
108 $this->send_success( array( 'id' => $location_id, 'title' => $location_name ) );
109 }
110
111 /**
112 * Remove a location
113 *
114 * @return void
115 */
116 public function location_remove() {
117 $this->verify_nonce( 'erp-nonce' );
118
119 $location_id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
120
121 if ( $location_id ) {
122 Company_Locations::find( $location_id )->delete();
123 }
124
125 $this->send_success();
126 }
127
128 public function view_edit_log_changes() {
129
130 $this->verify_nonce( 'wp-erp-hr-nonce' );
131
132 $log_id = intval( $_POST['id'] );
133
134 if ( ! $log_id ) {
135 $this->send_error();
136 }
137
138 $log = \WeDevs\ERP\Admin\Models\Audit_Log::find( $log_id );
139 $old_value = maybe_unserialize( base64_decode( $log->old_value ) );
140 $new_value = maybe_unserialize( base64_decode( $log->new_value ) );
141 ob_start();
142 ?>
143 <div class="wrap">
144 <table class="wp-list-table widefat fixed audit-log-change-table">
145 <thead>
146 <tr>
147 <th class="col-date"><?php _e( 'Field/Items', 'erp' ); ?></th>
148 <th class="col"><?php _e( 'Old Value', 'erp' ); ?></th>
149 <th class="col"><?php _e( 'New Value', 'erp' ); ?></th>
150 </tr>
151 </thead>
152
153 <tfoot>
154 <tr>
155 <th class="col-items"><?php _e( 'Field/Items', 'erp' ); ?></th>
156 <th class="col"><?php _e( 'Old Value', 'erp' ); ?></th>
157 <th class="col"><?php _e( 'New Value', 'erp' ); ?></th>
158 </tr>
159 </tfoot>
160
161 <tbody>
162 <?php $i=1; ?>
163 <?php foreach( $old_value as $key => $value ) { ?>
164 <tr class="<?php echo $i % 2 == 0 ? 'alternate' : 'odd'; ?>">
165 <td class="col-date"><?php echo ucfirst( str_replace('_', ' ', $key ) ); ?></td>
166 <td><?php echo ( $value ) ? stripslashes( $value ) : '--'; ?></td>
167 <td><?php echo ( $new_value[$key] ) ? stripslashes( $new_value[$key] ) : '--'; ?></td>
168 </tr>
169 <?php $i++; } ?>
170 </tbody>
171 </table>
172 </div>
173 <?php
174 $content = ob_get_clean();
175
176 $data = [
177 'title' => __( 'Log changes', 'erp' ),
178 'content' => $content
179 ];
180
181 $this->send_success( $data );
182 }
183
184 /**
185 * Check if a people exists
186 *
187 * @return void
188 */
189 public function check_people() {
190 $email = isset( $_REQUEST['email'] ) ? sanitize_text_field( $_REQUEST['email'] ) : false;
191
192 if ( ! $email ) {
193 $this->send_error( __( 'No email address provided', 'erp' ) );
194 }
195
196 $user = \get_user_by( 'email', $email );
197
198 if ( false === $user ) {
199 $people = erp_get_people_by( 'email', $email );
200 } else {
201 $peep = \WeDevs\ERP\Framework\Models\People::with('types')->whereUserId( $user->ID )->first();
202
203 if ( null === $peep ) {
204 $user->data->types = 'wp_user';
205 $people = $user;
206 } else {
207 $people = (object) $peep->toArray();
208 $people->types = wp_list_pluck( $peep->types->toArray(), 'name' );
209 }
210 }
211
212 // we didn't found any user with this email address
213 if ( !$people ) {
214 $this->send_error();
215 }
216
217 // seems like we found one
218 $this->send_success( $people );
219 }
220
221 /**
222 * Test the SMTP connection.
223 *
224 * @return void
225 */
226 public function smtp_test_connection() {
227 $this->verify_nonce( 'erp-smtp-test-connection-nonce' );
228
229 if ( empty( $_REQUEST['mail_server'] ) ) {
230 $this->send_error( __( 'No host address provided', 'erp' ) );
231 }
232
233 if ( empty( $_REQUEST['port'] ) ) {
234 $this->send_error( __( 'No port address provided', 'erp' ) );
235 }
236
237 if ( $_REQUEST['authentication'] !== '' ) {
238 if ( empty( $_REQUEST['username'] ) ) {
239 $this->send_error( __( 'No email address provided', 'erp' ) );
240 }
241
242 if ( empty( $_REQUEST['password'] ) ) {
243 $this->send_error( __( 'No email password provided', 'erp' ) );
244 }
245 }
246
247 if ( empty( $_REQUEST['to'] ) ) {
248 $this->send_error( __( 'No testing email address provided', 'erp' ) );
249 }
250
251 $mail_server = $_REQUEST['mail_server'];
252 $port = isset( $_REQUEST['port'] ) ? $_REQUEST['port'] : 465;
253 $authentication = isset( $_REQUEST['authentication'] ) ? $_REQUEST['authentication'] : 'smtp';
254 $username = $_REQUEST['username'];
255 $password = $_REQUEST['password'];
256
257 global $phpmailer;
258
259 if ( ! is_object( $phpmailer ) || ! is_a( $phpmailer, 'PHPMailer' ) ) {
260 require_once ABSPATH . WPINC . '/class-phpmailer.php';
261 require_once ABSPATH . WPINC . '/class-smtp.php';
262 $phpmailer = new \PHPMailer( true );
263 }
264
265 $to = $_REQUEST['to'];
266 $subject = __( 'ERP SMTP Test Mail', 'erp' );
267 $message = __( 'This is a test email by WP ERP.', 'erp' );
268
269 $erp_email_settings = get_option( 'erp_settings_erp-email_general', [] );
270
271 if ( ! isset( $erp_email_settings['from_email'] ) ) {
272 $from_email = get_option( 'admin_email' );
273 } else {
274 $from_email = $erp_email_settings['from_email'];
275 }
276
277 if ( ! isset( $erp_email_settings['from_name'] ) ) {
278 global $current_user;
279
280 $from_name = $current_user->display_name;
281 } else {
282 $from_name = $erp_email_settings['from_name'];
283 }
284
285 $content_type = 'text/html';
286
287 $phpmailer->AddAddress( $to );
288 $phpmailer->From = $from_email;
289 $phpmailer->FromName = $from_name;
290 $phpmailer->Sender = $phpmailer->From;
291 $phpmailer->Subject = $subject;
292 $phpmailer->Body = $message;
293 $phpmailer->Mailer = 'smtp';
294 $phpmailer->Host = $mail_server;
295 $phpmailer->SMTPSecure = $authentication;
296 $phpmailer->Port = $port;
297
298 if ( $_REQUEST['authentication'] !== '' ) {
299 $phpmailer->SMTPAuth = true;
300 $phpmailer->Username = $username;
301 $phpmailer->Password = $password;
302 }
303
304 $phpmailer->isHTML(true);
305
306 try {
307 $result = $phpmailer->Send();
308
309 $this->send_success( __( 'Test email has been sent.', 'erp' ) );
310 } catch( \Exception $e ) {
311 $this->send_error( $e->getMessage() );
312 }
313 }
314
315 /**
316 * Test the Imap connection.
317 *
318 * @return void
319 */
320 public function imap_test_connection() {
321 $this->verify_nonce( 'erp-imap-test-connection-nonce' );
322
323 if ( empty( $_REQUEST['mail_server'] ) ) {
324 $this->send_error( __( 'No host address provided', 'erp' ) );
325 }
326
327 if ( empty( $_REQUEST['username'] ) ) {
328 $this->send_error( __( 'No email address provided', 'erp' ) );
329 }
330
331 if ( empty( $_REQUEST['password'] ) ) {
332 $this->send_error( __( 'No email password provided', 'erp' ) );
333 }
334
335 if ( empty( $_REQUEST['port'] ) ) {
336 $this->send_error( __( 'No port address provided', 'erp' ) );
337 }
338
339 $mail_server = $_REQUEST['mail_server'];
340 $username = $_REQUEST['username'];
341 $password = $_REQUEST['password'];
342 $protocol = $_REQUEST['protocol'];
343 $port = isset( $_REQUEST['port'] ) ? $_REQUEST['port'] : 993;
344 $authentication = isset( $_REQUEST['authentication'] ) ? $_REQUEST['authentication'] : 'ssl';
345
346 try {
347 $imap = new \WeDevs\ERP\Imap( $mail_server, $port, $protocol, $username, $password, $authentication );
348 $imap->is_connected();
349
350 $this->send_success( __( 'Your IMAP connection is established.', 'erp' ) );
351 } catch( \Exception $e ) {
352 $this->send_error( $e->getMessage() );
353 }
354 }
355
356 /**
357 * Import users as crm contacts.
358 *
359 * @since 1.1.2
360 * @since 1.1.18 Introduce `ERP_IS_IMPORTING`
361 * @since 1.1.19 Import partial data in case of existing contacts
362 *
363 * @return void
364 */
365 public function import_users_as_contacts() {
366 $this->verify_nonce( 'erp-import-export-nonce' );
367
368 define( 'ERP_IS_IMPORTING' , true );
369
370 $limit = 50; // Limit to import per request
371
372 $attempt = get_option( 'erp_users_to_contacts_import_attempt', 1 );
373 update_option( 'erp_users_to_contacts_import_attempt', $attempt + 1 );
374 $offset = ( $attempt - 1 ) * $limit;
375
376 $user_role = $_REQUEST['user_role'];
377 $contact_owner = sanitize_text_field( $_REQUEST['contact_owner'] );
378 $life_stage = sanitize_text_field( $_REQUEST['life_stage'] );
379 $contact_group = sanitize_text_field( $_REQUEST['contact_group'] );
380
381 if ( ! empty( $user_role ) ) {
382 $user_query = new \WP_User_Query( ['role__in' => $user_role, 'number' => $limit, 'offset' => $offset] );
383 $users = $user_query->get_results();
384 $total_items = $user_query->get_total();
385 } else {
386 $user_query = new \WP_User_Query( ['number' => $limit, 'offset' => $offset] );
387 $users = $user_query->get_results();
388 $total_items = $user_query->get_total();
389 }
390
391 $user_ids = [];
392 $user_ids = wp_list_pluck( $users, 'ID' );
393
394 foreach ( $user_ids as $user_id ) {
395 $wp_user = get_user_by( 'id', $user_id );
396 $phone = get_user_meta( $user_id, 'phone', true );
397 $street_1 = get_user_meta( $user_id, 'street_1', true );
398 $street_2 = get_user_meta( $user_id, 'street_2', true );
399 $city = get_user_meta( $user_id, 'city', true );
400 $state = get_user_meta( $user_id, 'state', true );
401 $postal_code = get_user_meta( $user_id, 'postal_code', true );
402 $country = get_user_meta( $user_id, 'country', true );
403
404 $data = [
405 'type' => 'contact',
406 'user_id' => absint( $user_id ),
407 'first_name' => $wp_user->first_name,
408 'last_name' => $wp_user->last_name,
409 'email' => $wp_user->user_email,
410 'phone' => $phone,
411 'street_1' => $street_1,
412 'street_2' => $street_2,
413 'city' => $city,
414 'state' => $state,
415 'postal_code' => $postal_code,
416 'country' => $country,
417 'contact_owner' => $contact_owner,
418 'life_stage' => $life_stage
419 ];
420
421 $people = erp_insert_people( $data, true );
422
423 if ( is_wp_error( $people ) ) {
424 continue;
425 } else {
426 $contact = new \WeDevs\ERP\CRM\Contact( absint( $people->id ), 'contact' );
427
428 if ( ! $people->exists) {
429 $contact->update_life_stage($life_stage);
430 $contact->update_contact_owner( $contact_owner );
431
432 } else {
433 if ( ! $contact->get_life_stage() ) {
434 $contact->update_life_stage($life_stage);
435 }
436
437 if ( ! $contact->get_contact_owner() ) {
438 $contact->update_contact_owner( $contact_owner );
439 }
440 }
441
442 $existing_data = \WeDevs\ERP\CRM\Models\ContactSubscriber::where( [ 'group_id' => $contact_group, 'user_id' => $people->id ] )->first();
443
444 if ( empty( $existing_data ) ) {
445 $hash = sha1( microtime() . 'erp-subscription-form' . $contact_group . $people->id );
446
447 erp_crm_create_new_contact_subscriber([
448 'group_id' => $contact_group,
449 'user_id' => $people->id,
450 'status' => 'subscribe',
451 'subscribe_at' => current_time( 'mysql' ),
452 'unsubscribe_at' => null,
453 'hash' => $hash
454 ]);
455 }
456 }
457 }
458
459 // re-calculate stats
460 if ( $total_items <= ( $attempt * $limit ) ) {
461 $left = 0;
462 } else {
463 $left = $total_items - ( $attempt * $limit );
464 }
465
466 if ( $left === 0 ) {
467 delete_option( 'erp_users_to_contacts_import_attempt' );
468 }
469
470 $this->send_success( [ 'left' => $left, 'total_items' => $total_items, 'exists' => 0 ] );
471 }
472
473 /**
474 * New api key
475 *
476 * @return void
477 */
478 public function new_api_key() {
479 $this->verify_nonce( 'erp-api-key' );
480
481 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
482
483 if ( $id ) {
484 $api_key = \WeDevs\ERP\Framework\Models\APIKey::find( $id );
485
486 $api_key->update( [
487 'name' => sanitize_text_field( $_POST['name'] ),
488 'user_id' => intval( $_POST['user_id'] ),
489 ] );
490
491 $this->send_success( $api_key );
492 }
493
494 $api_key = [
495 'name' => sanitize_text_field( $_POST['name'] ),
496 'api_key' => 'ck_' . erp_generate_key(),
497 'api_secret' => 'cs_' . erp_generate_key(),
498 'user_id' => intval( $_POST['user_id'] ),
499 'created_at' => current_time( 'mysql' ),
500 ];
501
502 $data = \WeDevs\ERP\Framework\Models\APIKey::create( $api_key );
503
504 $this->send_success( $data );
505 }
506
507 /**
508 * Delete api key
509 *
510 * @return void
511 */
512 public function delete_api_key() {
513 $this->verify_nonce( 'erp-nonce' );
514
515 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
516
517 if ( $id ) {
518 APIKey::find( $id )->delete();
519 }
520
521 $this->send_success();
522 }
523
524 /**
525 * Dismiss promotional offer
526 *
527 * @since 1.1.15
528 *
529 * @return void
530 */
531 public function dismiss_promotional_offer() {
532 if ( ! empty( $_POST['dismissed'] ) ) {
533 update_option( 'erp_promotional_offer_notice_quiz-aug17', 'hide' );
534 }
535 }
536 }
537
538 new Ajax();
539