Advertisement
Guest User

Untitled

a guest
Nov 10th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.24 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * FindLab Order
  5. *
  6. * The FindLab Order class
  7. *
  8. * @class LLFCRM_Order
  9. * @version 1.0.0
  10. * @package FindLab/Includes/Core
  11. * @category Class
  12. * @author FloAgency
  13. *
  14. **/
  15.  
  16. if ( ! defined( 'ABSPATH' ) ) {
  17. exit; // Exit if accessed directly
  18. }
  19.  
  20.  
  21. class LLFCRM_MyAccount {
  22.  
  23. static $server_cookie = 'llc_crm_server_user';
  24.  
  25. /**
  26. * Constructor
  27. */
  28. public function __construct() {
  29.  
  30. add_shortcode( 'llfcrm_myaccount', array($this, 'shortcode_wr') );
  31.  
  32. add_filter( 'gform_field_value_llfcrm_order_id', array($this, 'gravity_form_order_id') );
  33. add_action( 'gform_post_submission', array($this, 'gravity_form_submitted'), 10, 2 );
  34.  
  35. add_action( 'init', array($this, 'acf_form_head') );
  36.  
  37.  
  38. // add_action('acf/pre_save_post', array($this, 'acf_pre_save_post') );
  39.  
  40. $this->init_ajax_actions();
  41.  
  42. }
  43.  
  44. /**
  45. * Load ACF form scripts and server checks
  46. */
  47. function acf_form_head(){
  48. if ( !is_admin() )acf_form_head();
  49. }
  50.  
  51.  
  52. /**
  53. * Event before storing form to DB
  54. *
  55. * @param $post_id
  56. */
  57. public function acf_pre_save_post( $post_id ){
  58. return;
  59. deb($post_id);
  60. deb($_POST);
  61. die();
  62. }
  63.  
  64. /**
  65. * The My Account shortcode wrapper
  66. *
  67. * @param $atts
  68. */
  69. public function shortcode_wr( $atts ){
  70.  
  71. ob_start(); ob_clean();
  72. include_once(LLF_CRM_PATH . 'templates/public/page-myaccount.php');
  73.  
  74. return ob_get_clean();
  75. }
  76.  
  77. /**
  78. * Procedure to Initialize Class Ajax Actions
  79. */
  80. public function init_ajax_actions() {
  81.  
  82. $ajax_actions_public = array(
  83. 'log_in',
  84. 'get_register_form',
  85. 'register',
  86. );
  87.  
  88. $ajax_private = array(
  89. 'update_user',
  90. 'get_payment_btn',
  91. 'get_adjustment_payment_btn',
  92. 'get_my_data',
  93. 'balance_payment',
  94. 'balance_payment_adjustment',
  95. 'get_colorprofile_form',
  96. 'update_colorprofile_form',
  97. 'send_customer_message',
  98. //'change_order_meta',
  99. //'get_searched_data',
  100. );
  101.  
  102. foreach ( $ajax_actions_public as $function ) {
  103. add_action( 'wp_ajax_crm_public_' . $function, array( 'LLFCRM_MyAccount', 'ajax_' . $function ) );
  104. add_action( 'wp_ajax_nopriv_crm_public_' . $function, array( 'LLFCRM_MyAccount', 'ajax_' . $function ) );
  105.  
  106. }
  107.  
  108. foreach ( $ajax_private as $function ) {
  109. add_action( 'wp_ajax_crm_public_' . $function, array( 'LLFCRM_MyAccount', 'ajax_' . $function ) );
  110. }
  111.  
  112. }
  113.  
  114. /**
  115. * Retrieve order data
  116. */
  117. static function ajax_get_my_data(){
  118.  
  119. $user_id = get_current_user_id();
  120.  
  121. $orders = LLFCRM_Search_Queries::get_orders_by_author( $user_id );
  122.  
  123. $response = array(
  124. 'orders' => $orders,
  125. );
  126.  
  127. echo json_encode( $response );
  128. exit();
  129.  
  130. }
  131.  
  132. /**
  133. * Log in
  134. */
  135. static public function ajax_log_in(){
  136.  
  137. // First check the nonce, if it fails the function will break
  138. check_ajax_referer( 'ajax-login-nonce', 'crm_login' );
  139.  
  140. // Nonce is checked, get the POST data and sign user on
  141.  
  142. $info = array();
  143. $info['user_login'] = $_POST['username'];
  144. $info['user_password'] = $_POST['password'];
  145. $info['remember'] = true;
  146.  
  147. $user_signon = wp_signon( $info, false );
  148.  
  149. if ( is_wp_error($user_signon) ){
  150. echo json_encode(array('loggedin' => false, 'message' => __('Wrong username or password.')));
  151. } else {
  152. echo json_encode(array('loggedin' => true, 'message' => __('Login successful, redirecting...')));
  153. }
  154.  
  155. exit();
  156. }
  157.  
  158. /**
  159. * Return ACF form about the user address details
  160. */
  161. static function ajax_get_register_form(){
  162.  
  163. $user_id = 'user_' . wp_get_current_user()->ID;
  164.  
  165. $redirect = esc_url( $_POST['redirect'] );
  166.  
  167. $options = array(
  168. 'post_id' => $user_id,
  169. 'field_groups' => array('group_59959d648ce03'),
  170. 'form' => true,
  171. 'return' => '',
  172. 'html_before_fields' => '',
  173. 'html_after_fields' => '',
  174. 'submit_value' => 'Update address',
  175. );
  176.  
  177. ob_start();
  178. ob_clean();
  179.  
  180. acf_form( $options );
  181.  
  182. echo ob_get_clean();
  183.  
  184. exit();
  185.  
  186. }
  187.  
  188. /**
  189. * Registers a user based on written details. If registration succeded, will log in current user
  190. */
  191. static function ajax_register(){
  192.  
  193. // Verify nonce
  194. if( !isset( $_POST['register_nonce'] ) || !wp_verify_nonce( $_POST['register_nonce'], 'llf_register_nonce' ) )
  195. die( 'Ooops, something went wrong, please try again later.' );
  196.  
  197.  
  198. $p = $_POST;
  199.  
  200. if ( empty( $p['username'] ) || empty( $p['password'] ) || empty( $p['email'] ) || empty( $p['name'] ) || empty( $p['last_name'] ) ){
  201. die( 'Not enough info' );
  202. }
  203.  
  204.  
  205. // Post values
  206. $username = $p['username'];
  207. $password = $p['password'];
  208. $email = sanitize_email( $p['email'] );
  209. $name = $p['name'];
  210. $last_name = $p['last_name'];
  211.  
  212. $userdata = array(
  213. 'user_login' => $username,
  214. 'user_pass' => $password,
  215. 'user_email' => $email,
  216. 'first_name' => $name,
  217. 'last_name' => $last_name,
  218. 'role' => 'client',
  219. );
  220.  
  221. $user_id = wp_insert_user( $userdata );
  222.  
  223. // Return
  224. if( !is_wp_error($user_id) ) {
  225.  
  226. // Save some meta on our fresh folk
  227. update_user_meta($user_id, LLFCRM_User::$customer_verification, 'unverified');
  228.  
  229. update_user_meta($user_id, LLFCRM_User::$account_step, '1');
  230.  
  231. // User registered successfully. Log him in.
  232.  
  233. $user_obj = get_user_by('id', $user_id);
  234.  
  235. $info = array();
  236. $info['user_login'] = $user_obj->data->user_login;
  237. $info['user_password'] = $password;
  238. $info['remember'] = true;
  239.  
  240. $user_signon = wp_signon( $info, false );
  241.  
  242. if ( is_wp_error($user_signon) ){
  243. echo json_encode(array('registered' => true, 'loggedin' => false, 'message' => __('Wrong username or password.')));
  244. } else {
  245. echo json_encode(array('registered' => true, 'loggedin' => true, 'message' => __('Login successful, redirecting...')));
  246. }
  247.  
  248. } else {
  249. echo json_encode(array('registered' => false, 'message' => $user_id->get_error_message()));
  250. }
  251. exit();
  252. }
  253.  
  254. /**
  255. * Updates user data
  256. */
  257. static function ajax_update_user(){
  258.  
  259. // Verify nonce
  260. if( !isset( $_POST['update_nonce'] ) || !wp_verify_nonce( $_POST['update_nonce'], 'llf_register_nonce' ) )
  261. die( 'Ooops, something went wrong, please try again later.' );
  262.  
  263. $p = $_POST;
  264.  
  265. if ( empty( $p['first_name'] ) || empty( $p['last_name'] ) || empty( $p['user_email']) ){
  266. echo json_encode(array('updated' => false, 'message' => __('Check for blank mandatory fields!', 'llf-crm'), 'field' => ''));
  267. exit();
  268. }
  269.  
  270. $current_user = wp_get_current_user();
  271.  
  272. $userdata = array(
  273. 'ID' => $current_user->ID,
  274. 'user_email' => $p['user_email'],
  275. 'first_name' => $p['first_name'],
  276. 'last_name' => $p['last_name'],
  277. );
  278.  
  279.  
  280. // Update password
  281. if( ! empty( $_POST['old_password'] ) && ! empty( $p['new_password'] ) ) {
  282. // Check if password user typed is correct
  283. if ( ! wp_check_password( $_POST['old_password'], $current_user->data->user_pass, $current_user->ID) ) {
  284. echo json_encode(array('updated' => false, 'message' => __('The old password seems to be wrong!', 'llf-crm'), 'field' => 'password'));
  285. exit();
  286. } else {
  287. $userdata['user_pass'] = $p['new_password'];
  288. }
  289.  
  290. }
  291.  
  292.  
  293. $user_id = wp_update_user( $userdata ) ;
  294.  
  295. // Return
  296. if( !is_wp_error($user_id) ) {
  297.  
  298. echo json_encode(array('updated' => true, 'message' => __('Update was successful, redirecting...', 'llf-crm')));
  299.  
  300. } else {
  301. echo json_encode(array('updated' => false, 'message' => $user_id->get_error_message(), 'field' => 'input'));
  302. }
  303. exit();
  304. }
  305.  
  306. /**
  307. * Send email from customer/my-account and memo email in our system
  308. */
  309. static public function ajax_send_customer_message(){
  310. // POST has:
  311. // [order_id] => 303
  312. // [subject] => New notification from Flothemes concerning your order
  313. // [content] =>
  314.  
  315. $data = $_POST;
  316.  
  317. $response = array('success' => 'no');
  318.  
  319. if ( ! empty( $data['order_id'] ) && ! empty( $data['subject'] ) && ! empty( $data['content'] ) ){
  320.  
  321. // Log this message in our system
  322. $saved = LLFCRM_Emails::add_order_message( $data['order_id'], 'local', 'My Account Page', $data['subject'],
  323. $data['content'], $id = time(), $received = false, $time = time(), $data['user'], array('in', $data['stage'] ), 'client' );
  324.  
  325. if ( $saved ) {
  326.  
  327. $response['success'] = 'yes';
  328. $response['order_messages'] = LLFCRM_Emails::get_emails( $data['order_id'] );
  329.  
  330. echo json_encode( $response );
  331. exit();
  332. }
  333.  
  334. }
  335.  
  336.  
  337. echo json_encode( $response );
  338.  
  339. exit();
  340. }
  341.  
  342.  
  343. /**
  344. * Return ACF form about the user color profile
  345. */
  346. static function ajax_get_colorprofile_form(){
  347.  
  348. $form_options = array(
  349. 'post_id' => 'new_post',
  350. 'field_groups' => array('group_59f2f83da1afb'),
  351. 'new_post' => array(
  352. 'post_type' => 'colorprofile',
  353. 'post_status' => 'pending'
  354. ),
  355. 'form' => false,
  356. 'return' => LLFCRM_Admin::get_page_permalink('myaccount') . '#!/color-profiles',
  357. 'updated_message' => 'Color Profile updated',
  358. 'uploader' => 'basic'
  359. );
  360.  
  361.  
  362. ob_start();
  363. ob_clean();
  364.  
  365. acf_form( $form_options );
  366.  
  367. echo ob_get_clean();
  368.  
  369. exit();
  370.  
  371. }
  372.  
  373. /**
  374. * Return ACF form about the user color profile
  375. */
  376. static function ajax_update_colorprofile_form(){
  377.  
  378. $id = ! empty($_GET['old']) ? intval( $_GET['old'] ) : 0;
  379.  
  380. if ( $id > 0 ) {
  381.  
  382. $form_options = array(
  383. 'post_id' => $id,
  384. 'field_groups' => array('group_59f2f83da1afb'),
  385. 'form' => false,
  386. 'return' => LLFCRM_Admin::get_page_permalink('myaccount') . '#!/color-profiles/' . $id,
  387. 'updated_message' => 'Color Profile updated',
  388. );
  389. } else {
  390. echo 0;
  391. exit();
  392. }
  393.  
  394. $old = get_post($id);
  395.  
  396. ob_start();
  397. ob_clean();
  398.  
  399. acf_form( $form_options );
  400.  
  401. echo json_encode(array(
  402. 'title' => $old->post_title,
  403. 'comment' => $old->post_excerpt,
  404. 'form' => htmlspecialchars_decode( ob_get_clean() )
  405. ));
  406.  
  407. exit();
  408.  
  409. }
  410.  
  411. /**
  412. * Return the Stripe payment button with the right amount
  413. */
  414. static function ajax_get_payment_btn(){
  415.  
  416. if ( empty( $_POST['price'] ) || empty( $_POST['form'] ) ) return;
  417.  
  418. $final = 0;
  419.  
  420. $price = intval ( $_POST['price'] );
  421.  
  422. $from_balance = $_POST['from_balance'] == 'true'; // is it a hybrid order
  423.  
  424. $minimal = LLFCRM_User::is_verified() ? 0 : intval( crm_get_field('minimum_order_amount', 'options') );
  425.  
  426. $current_user = wp_get_current_user();
  427.  
  428. $in_balance = 0;
  429.  
  430. // hybrid
  431. if ( $from_balance ) {
  432.  
  433. $payment_type = 'hybrid';
  434.  
  435.  
  436. $balance = LLFCRM_Balance::get_balance($current_user->ID);
  437.  
  438.  
  439. if ( $price < $minimal ) {
  440.  
  441. $diff_to_balance = $minimal - $price;
  442.  
  443. // We consider balance is not enough, as this is the only way going here
  444. $final = $minimal - $balance; // here balance should be 0 and final price without balance
  445.  
  446. // To be added or extracted from balance
  447. $in_balance = ($balance * (-1) ) + $diff_to_balance; // if negative, means will be subtracted
  448.  
  449.  
  450. } else {
  451. $final = $price - $balance;
  452.  
  453. $in_balance = ($balance * (-1) );
  454. }
  455.  
  456. // only card
  457. } else {
  458.  
  459. $payment_type = 'card';
  460.  
  461.  
  462. if ( $price < $minimal ) {
  463. $final = $minimal;
  464.  
  465. $in_balance = $minimal - $price;
  466.  
  467. } else {
  468. $final = $price;
  469. }
  470.  
  471. }
  472.  
  473. $form = $_POST['form'];
  474. $form['inbalance'] = $in_balance;
  475. $form['price_profile'] = intval($_POST['price_profile']);
  476.  
  477. $form_serialized = serialize($form);
  478.  
  479. $description = 'Payment from ' . $current_user->user_firstname . ' ' . $current_user->user_lastname . ' for image editing services';
  480.  
  481. // Add balance info to payment meta
  482. add_filter('sc_before_payment_button', function ($html) use ( $form_serialized, $payment_type ) {
  483.  
  484. // Let's encrypt the balance - we don't want clever guys to play with this value
  485. $encrypted_form = LLFCRM_Payment::encript_val($form_serialized);
  486.  
  487. $html .= '<input type="hidden" name="llf-crm-form" value="' . $encrypted_form . '" />';
  488. $html .= '<input type="hidden" name="llf-crm-payment" value="' . $payment_type . '" />';
  489.  
  490. return $html;
  491. }, 20, 2);
  492.  
  493. echo do_shortcode('[stripe name="LLF Order Payment" description="' . $description . '" amount="' . $final . '" prefill_email="true"]');
  494.  
  495. exit();
  496. }
  497.  
  498. /**
  499. * Return the Stripe payment button for cost adjustment
  500. */
  501. static function ajax_get_adjustment_payment_btn(){
  502.  
  503. if ( empty( $_POST['price'] ) || empty( $_POST['partial_obj'] ) ) return;
  504.  
  505. $final = 0;
  506.  
  507. $price = intval ( $_POST['price'] );
  508.  
  509. $from_balance = $_POST['from_balance'] == 'true'; // is it a hybrid order
  510.  
  511. $current_user = wp_get_current_user();
  512.  
  513. $in_balance = 0;
  514.  
  515. // hybrid
  516. if ( $from_balance ) {
  517.  
  518. $payment_type = 'hybrid';
  519.  
  520. $balance = LLFCRM_Balance::get_balance($current_user->ID);
  521.  
  522. $final = $price - $balance;
  523.  
  524. $in_balance = ($balance * (-1) );
  525.  
  526. // only card
  527. } else {
  528.  
  529. $payment_type = 'card';
  530. $final = $price;
  531.  
  532. }
  533.  
  534. $order_id = $_POST['order_id'];
  535.  
  536. $form = $_POST['partial_obj'];
  537. $form['order_id'] = $order_id;
  538. $form['inbalance'] = $in_balance;
  539.  
  540. $form_serialized = serialize($form);
  541.  
  542. $description = 'Payment from ' . $current_user->user_firstname . ' ' . $current_user->user_lastname . ' as a cost adjustment for order ' . $order_id;
  543.  
  544. // Add balance info to payment meta
  545. add_filter('sc_before_payment_button', function ($html) use ( $form_serialized, $payment_type ) {
  546.  
  547. // Let's encrypt the balance - we don't want clever guys to play with this value
  548. $encrypted_form = LLFCRM_Payment::encript_val($form_serialized);
  549.  
  550. $html .= '<input type="hidden" name="llf-crm-form" value="' . $encrypted_form . '" />';
  551. $html .= '<input type="hidden" name="llf-crm-payment" value="' . $payment_type . '" />';
  552.  
  553. return $html;
  554. }, 20, 2);
  555.  
  556. echo do_shortcode('[stripe name="Order #'. $order_id .' Partial Payment" description="' . $description . '" amount="' . $final . '" prefill_email="true"]');
  557.  
  558. exit();
  559. }
  560.  
  561. /**
  562. * Payment using only balance
  563. */
  564. static function ajax_balance_payment(){
  565.  
  566. $response = array();
  567.  
  568. if ( empty( $_POST['price'] ) || empty( $_POST['form'] ) ) return;
  569.  
  570. $price = intval ( $_POST['price'] );
  571.  
  572. $for_profile = intval($_POST['price_profile']);
  573.  
  574. $form = $_POST['form'];
  575.  
  576. $current_user = wp_get_current_user();
  577. // Append Order payment and status
  578. $new_order = LLFCRM_Order::create_order_from_balance( $price, $form, $for_profile );
  579. $note = '$' . (abs( $price ) / 100) .' have been withdrawn from user\'s #' . $current_user->ID . ' balance for order #'.$new_order .'.';
  580.  
  581. $response['new_balance'] = LLFCRM_Balance::withdraw($current_user->ID, intval($price) * (-1), $note );
  582. $response['new_order'] = $new_order;
  583.  
  584. echo json_encode( $response );
  585. exit();
  586. }
  587.  
  588. /**
  589. * Payment using only balance
  590. */
  591. static function ajax_balance_payment_adjustment(){
  592.  
  593. $response = array();
  594.  
  595. if ( empty( $_POST['price'] ) || empty( $_POST['partial_obj'] ) ) return;
  596.  
  597. $price = intval ( $_POST['price'] );
  598.  
  599. $form = $_POST['partial_obj'];
  600.  
  601. $order_id = $_POST['order_id'];
  602.  
  603. $current_user = wp_get_current_user();
  604.  
  605. $partial = array();
  606.  
  607. $partial['completed'] = 'yes';
  608. $partial['completed_time'] = date('F dS, Y');
  609. $partial['pay_method'] = 'balance';
  610. $partial['pay_user'] = $current_user->ID;
  611. $partial['pay_user_name'] = $current_user->user_firstname . ' ' . $current_user->user_lastname;
  612.  
  613.  
  614. LLFCRM_Order::set_cost_adjustment($order_id, $form['id'], $partial);
  615.  
  616. $note = '$' . (abs( $price ) / 100) .' have been withdrawn from user\'s #' . $current_user->ID . ' balance for order #'.$order_id .' partial payment.';
  617.  
  618. $response['new_balance'] = LLFCRM_Balance::withdraw($current_user->ID, intval($price) * (-1), $note );
  619. $response['new_order'] = $order_id;
  620.  
  621. echo json_encode( $response );
  622. exit();
  623. }
  624.  
  625.  
  626. }
  627.  
  628. new LLFCRM_MyAccount();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement