Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * FindLab Order
- *
- * The FindLab Order class
- *
- * @class LLFCRM_Order
- * @version 1.0.0
- * @package FindLab/Includes/Core
- * @category Class
- * @author FloAgency
- *
- **/
- if ( ! defined( 'ABSPATH' ) ) {
- exit; // Exit if accessed directly
- }
- class LLFCRM_MyAccount {
- static $server_cookie = 'llc_crm_server_user';
- /**
- * Constructor
- */
- public function __construct() {
- add_shortcode( 'llfcrm_myaccount', array($this, 'shortcode_wr') );
- add_filter( 'gform_field_value_llfcrm_order_id', array($this, 'gravity_form_order_id') );
- add_action( 'gform_post_submission', array($this, 'gravity_form_submitted'), 10, 2 );
- add_action( 'init', array($this, 'acf_form_head') );
- // add_action('acf/pre_save_post', array($this, 'acf_pre_save_post') );
- $this->init_ajax_actions();
- }
- /**
- * Load ACF form scripts and server checks
- */
- function acf_form_head(){
- if ( !is_admin() )acf_form_head();
- }
- /**
- * Event before storing form to DB
- *
- * @param $post_id
- */
- public function acf_pre_save_post( $post_id ){
- return;
- deb($post_id);
- deb($_POST);
- die();
- }
- /**
- * The My Account shortcode wrapper
- *
- * @param $atts
- */
- public function shortcode_wr( $atts ){
- ob_start(); ob_clean();
- include_once(LLF_CRM_PATH . 'templates/public/page-myaccount.php');
- return ob_get_clean();
- }
- /**
- * Procedure to Initialize Class Ajax Actions
- */
- public function init_ajax_actions() {
- $ajax_actions_public = array(
- 'log_in',
- 'get_register_form',
- 'register',
- );
- $ajax_private = array(
- 'update_user',
- 'get_payment_btn',
- 'get_adjustment_payment_btn',
- 'get_my_data',
- 'balance_payment',
- 'balance_payment_adjustment',
- 'get_colorprofile_form',
- 'update_colorprofile_form',
- 'send_customer_message',
- //'change_order_meta',
- //'get_searched_data',
- );
- foreach ( $ajax_actions_public as $function ) {
- add_action( 'wp_ajax_crm_public_' . $function, array( 'LLFCRM_MyAccount', 'ajax_' . $function ) );
- add_action( 'wp_ajax_nopriv_crm_public_' . $function, array( 'LLFCRM_MyAccount', 'ajax_' . $function ) );
- }
- foreach ( $ajax_private as $function ) {
- add_action( 'wp_ajax_crm_public_' . $function, array( 'LLFCRM_MyAccount', 'ajax_' . $function ) );
- }
- }
- /**
- * Retrieve order data
- */
- static function ajax_get_my_data(){
- $user_id = get_current_user_id();
- $orders = LLFCRM_Search_Queries::get_orders_by_author( $user_id );
- $response = array(
- 'orders' => $orders,
- );
- echo json_encode( $response );
- exit();
- }
- /**
- * Log in
- */
- static public function ajax_log_in(){
- // First check the nonce, if it fails the function will break
- check_ajax_referer( 'ajax-login-nonce', 'crm_login' );
- // Nonce is checked, get the POST data and sign user on
- $info = array();
- $info['user_login'] = $_POST['username'];
- $info['user_password'] = $_POST['password'];
- $info['remember'] = true;
- $user_signon = wp_signon( $info, false );
- if ( is_wp_error($user_signon) ){
- echo json_encode(array('loggedin' => false, 'message' => __('Wrong username or password.')));
- } else {
- echo json_encode(array('loggedin' => true, 'message' => __('Login successful, redirecting...')));
- }
- exit();
- }
- /**
- * Return ACF form about the user address details
- */
- static function ajax_get_register_form(){
- $user_id = 'user_' . wp_get_current_user()->ID;
- $redirect = esc_url( $_POST['redirect'] );
- $options = array(
- 'post_id' => $user_id,
- 'field_groups' => array('group_59959d648ce03'),
- 'form' => true,
- 'return' => '',
- 'html_before_fields' => '',
- 'html_after_fields' => '',
- 'submit_value' => 'Update address',
- );
- ob_start();
- ob_clean();
- acf_form( $options );
- echo ob_get_clean();
- exit();
- }
- /**
- * Registers a user based on written details. If registration succeded, will log in current user
- */
- static function ajax_register(){
- // Verify nonce
- if( !isset( $_POST['register_nonce'] ) || !wp_verify_nonce( $_POST['register_nonce'], 'llf_register_nonce' ) )
- die( 'Ooops, something went wrong, please try again later.' );
- $p = $_POST;
- if ( empty( $p['username'] ) || empty( $p['password'] ) || empty( $p['email'] ) || empty( $p['name'] ) || empty( $p['last_name'] ) ){
- die( 'Not enough info' );
- }
- // Post values
- $username = $p['username'];
- $password = $p['password'];
- $email = sanitize_email( $p['email'] );
- $name = $p['name'];
- $last_name = $p['last_name'];
- $userdata = array(
- 'user_login' => $username,
- 'user_pass' => $password,
- 'user_email' => $email,
- 'first_name' => $name,
- 'last_name' => $last_name,
- 'role' => 'client',
- );
- $user_id = wp_insert_user( $userdata );
- // Return
- if( !is_wp_error($user_id) ) {
- // Save some meta on our fresh folk
- update_user_meta($user_id, LLFCRM_User::$customer_verification, 'unverified');
- update_user_meta($user_id, LLFCRM_User::$account_step, '1');
- // User registered successfully. Log him in.
- $user_obj = get_user_by('id', $user_id);
- $info = array();
- $info['user_login'] = $user_obj->data->user_login;
- $info['user_password'] = $password;
- $info['remember'] = true;
- $user_signon = wp_signon( $info, false );
- if ( is_wp_error($user_signon) ){
- echo json_encode(array('registered' => true, 'loggedin' => false, 'message' => __('Wrong username or password.')));
- } else {
- echo json_encode(array('registered' => true, 'loggedin' => true, 'message' => __('Login successful, redirecting...')));
- }
- } else {
- echo json_encode(array('registered' => false, 'message' => $user_id->get_error_message()));
- }
- exit();
- }
- /**
- * Updates user data
- */
- static function ajax_update_user(){
- // Verify nonce
- if( !isset( $_POST['update_nonce'] ) || !wp_verify_nonce( $_POST['update_nonce'], 'llf_register_nonce' ) )
- die( 'Ooops, something went wrong, please try again later.' );
- $p = $_POST;
- if ( empty( $p['first_name'] ) || empty( $p['last_name'] ) || empty( $p['user_email']) ){
- echo json_encode(array('updated' => false, 'message' => __('Check for blank mandatory fields!', 'llf-crm'), 'field' => ''));
- exit();
- }
- $current_user = wp_get_current_user();
- $userdata = array(
- 'ID' => $current_user->ID,
- 'user_email' => $p['user_email'],
- 'first_name' => $p['first_name'],
- 'last_name' => $p['last_name'],
- );
- // Update password
- if( ! empty( $_POST['old_password'] ) && ! empty( $p['new_password'] ) ) {
- // Check if password user typed is correct
- if ( ! wp_check_password( $_POST['old_password'], $current_user->data->user_pass, $current_user->ID) ) {
- echo json_encode(array('updated' => false, 'message' => __('The old password seems to be wrong!', 'llf-crm'), 'field' => 'password'));
- exit();
- } else {
- $userdata['user_pass'] = $p['new_password'];
- }
- }
- $user_id = wp_update_user( $userdata ) ;
- // Return
- if( !is_wp_error($user_id) ) {
- echo json_encode(array('updated' => true, 'message' => __('Update was successful, redirecting...', 'llf-crm')));
- } else {
- echo json_encode(array('updated' => false, 'message' => $user_id->get_error_message(), 'field' => 'input'));
- }
- exit();
- }
- /**
- * Send email from customer/my-account and memo email in our system
- */
- static public function ajax_send_customer_message(){
- // POST has:
- // [order_id] => 303
- // [subject] => New notification from Flothemes concerning your order
- // [content] =>
- $data = $_POST;
- $response = array('success' => 'no');
- if ( ! empty( $data['order_id'] ) && ! empty( $data['subject'] ) && ! empty( $data['content'] ) ){
- // Log this message in our system
- $saved = LLFCRM_Emails::add_order_message( $data['order_id'], 'local', 'My Account Page', $data['subject'],
- $data['content'], $id = time(), $received = false, $time = time(), $data['user'], array('in', $data['stage'] ), 'client' );
- if ( $saved ) {
- $response['success'] = 'yes';
- $response['order_messages'] = LLFCRM_Emails::get_emails( $data['order_id'] );
- echo json_encode( $response );
- exit();
- }
- }
- echo json_encode( $response );
- exit();
- }
- /**
- * Return ACF form about the user color profile
- */
- static function ajax_get_colorprofile_form(){
- $form_options = array(
- 'post_id' => 'new_post',
- 'field_groups' => array('group_59f2f83da1afb'),
- 'new_post' => array(
- 'post_type' => 'colorprofile',
- 'post_status' => 'pending'
- ),
- 'form' => false,
- 'return' => LLFCRM_Admin::get_page_permalink('myaccount') . '#!/color-profiles',
- 'updated_message' => 'Color Profile updated',
- 'uploader' => 'basic'
- );
- ob_start();
- ob_clean();
- acf_form( $form_options );
- echo ob_get_clean();
- exit();
- }
- /**
- * Return ACF form about the user color profile
- */
- static function ajax_update_colorprofile_form(){
- $id = ! empty($_GET['old']) ? intval( $_GET['old'] ) : 0;
- if ( $id > 0 ) {
- $form_options = array(
- 'post_id' => $id,
- 'field_groups' => array('group_59f2f83da1afb'),
- 'form' => false,
- 'return' => LLFCRM_Admin::get_page_permalink('myaccount') . '#!/color-profiles/' . $id,
- 'updated_message' => 'Color Profile updated',
- );
- } else {
- echo 0;
- exit();
- }
- $old = get_post($id);
- ob_start();
- ob_clean();
- acf_form( $form_options );
- echo json_encode(array(
- 'title' => $old->post_title,
- 'comment' => $old->post_excerpt,
- 'form' => htmlspecialchars_decode( ob_get_clean() )
- ));
- exit();
- }
- /**
- * Return the Stripe payment button with the right amount
- */
- static function ajax_get_payment_btn(){
- if ( empty( $_POST['price'] ) || empty( $_POST['form'] ) ) return;
- $final = 0;
- $price = intval ( $_POST['price'] );
- $from_balance = $_POST['from_balance'] == 'true'; // is it a hybrid order
- $minimal = LLFCRM_User::is_verified() ? 0 : intval( crm_get_field('minimum_order_amount', 'options') );
- $current_user = wp_get_current_user();
- $in_balance = 0;
- // hybrid
- if ( $from_balance ) {
- $payment_type = 'hybrid';
- $balance = LLFCRM_Balance::get_balance($current_user->ID);
- if ( $price < $minimal ) {
- $diff_to_balance = $minimal - $price;
- // We consider balance is not enough, as this is the only way going here
- $final = $minimal - $balance; // here balance should be 0 and final price without balance
- // To be added or extracted from balance
- $in_balance = ($balance * (-1) ) + $diff_to_balance; // if negative, means will be subtracted
- } else {
- $final = $price - $balance;
- $in_balance = ($balance * (-1) );
- }
- // only card
- } else {
- $payment_type = 'card';
- if ( $price < $minimal ) {
- $final = $minimal;
- $in_balance = $minimal - $price;
- } else {
- $final = $price;
- }
- }
- $form = $_POST['form'];
- $form['inbalance'] = $in_balance;
- $form['price_profile'] = intval($_POST['price_profile']);
- $form_serialized = serialize($form);
- $description = 'Payment from ' . $current_user->user_firstname . ' ' . $current_user->user_lastname . ' for image editing services';
- // Add balance info to payment meta
- add_filter('sc_before_payment_button', function ($html) use ( $form_serialized, $payment_type ) {
- // Let's encrypt the balance - we don't want clever guys to play with this value
- $encrypted_form = LLFCRM_Payment::encript_val($form_serialized);
- $html .= '<input type="hidden" name="llf-crm-form" value="' . $encrypted_form . '" />';
- $html .= '<input type="hidden" name="llf-crm-payment" value="' . $payment_type . '" />';
- return $html;
- }, 20, 2);
- echo do_shortcode('[stripe name="LLF Order Payment" description="' . $description . '" amount="' . $final . '" prefill_email="true"]');
- exit();
- }
- /**
- * Return the Stripe payment button for cost adjustment
- */
- static function ajax_get_adjustment_payment_btn(){
- if ( empty( $_POST['price'] ) || empty( $_POST['partial_obj'] ) ) return;
- $final = 0;
- $price = intval ( $_POST['price'] );
- $from_balance = $_POST['from_balance'] == 'true'; // is it a hybrid order
- $current_user = wp_get_current_user();
- $in_balance = 0;
- // hybrid
- if ( $from_balance ) {
- $payment_type = 'hybrid';
- $balance = LLFCRM_Balance::get_balance($current_user->ID);
- $final = $price - $balance;
- $in_balance = ($balance * (-1) );
- // only card
- } else {
- $payment_type = 'card';
- $final = $price;
- }
- $order_id = $_POST['order_id'];
- $form = $_POST['partial_obj'];
- $form['order_id'] = $order_id;
- $form['inbalance'] = $in_balance;
- $form_serialized = serialize($form);
- $description = 'Payment from ' . $current_user->user_firstname . ' ' . $current_user->user_lastname . ' as a cost adjustment for order ' . $order_id;
- // Add balance info to payment meta
- add_filter('sc_before_payment_button', function ($html) use ( $form_serialized, $payment_type ) {
- // Let's encrypt the balance - we don't want clever guys to play with this value
- $encrypted_form = LLFCRM_Payment::encript_val($form_serialized);
- $html .= '<input type="hidden" name="llf-crm-form" value="' . $encrypted_form . '" />';
- $html .= '<input type="hidden" name="llf-crm-payment" value="' . $payment_type . '" />';
- return $html;
- }, 20, 2);
- echo do_shortcode('[stripe name="Order #'. $order_id .' Partial Payment" description="' . $description . '" amount="' . $final . '" prefill_email="true"]');
- exit();
- }
- /**
- * Payment using only balance
- */
- static function ajax_balance_payment(){
- $response = array();
- if ( empty( $_POST['price'] ) || empty( $_POST['form'] ) ) return;
- $price = intval ( $_POST['price'] );
- $for_profile = intval($_POST['price_profile']);
- $form = $_POST['form'];
- $current_user = wp_get_current_user();
- // Append Order payment and status
- $new_order = LLFCRM_Order::create_order_from_balance( $price, $form, $for_profile );
- $note = '$' . (abs( $price ) / 100) .' have been withdrawn from user\'s #' . $current_user->ID . ' balance for order #'.$new_order .'.';
- $response['new_balance'] = LLFCRM_Balance::withdraw($current_user->ID, intval($price) * (-1), $note );
- $response['new_order'] = $new_order;
- echo json_encode( $response );
- exit();
- }
- /**
- * Payment using only balance
- */
- static function ajax_balance_payment_adjustment(){
- $response = array();
- if ( empty( $_POST['price'] ) || empty( $_POST['partial_obj'] ) ) return;
- $price = intval ( $_POST['price'] );
- $form = $_POST['partial_obj'];
- $order_id = $_POST['order_id'];
- $current_user = wp_get_current_user();
- $partial = array();
- $partial['completed'] = 'yes';
- $partial['completed_time'] = date('F dS, Y');
- $partial['pay_method'] = 'balance';
- $partial['pay_user'] = $current_user->ID;
- $partial['pay_user_name'] = $current_user->user_firstname . ' ' . $current_user->user_lastname;
- LLFCRM_Order::set_cost_adjustment($order_id, $form['id'], $partial);
- $note = '$' . (abs( $price ) / 100) .' have been withdrawn from user\'s #' . $current_user->ID . ' balance for order #'.$order_id .' partial payment.';
- $response['new_balance'] = LLFCRM_Balance::withdraw($current_user->ID, intval($price) * (-1), $note );
- $response['new_order'] = $order_id;
- echo json_encode( $response );
- exit();
- }
- }
- new LLFCRM_MyAccount();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement