Advertisement
nlozovan

Untitled

Jan 23rd, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.03 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Flo CRM Server plugin
  4. Description: This small plugin should be installed on main server to send info to remote CRM
  5. Version: 0.1.0
  6. Author: Lozovan Nicolae
  7. Author URI: http://flothemes.com
  8. */
  9.  
  10. if ( ! class_exists( 'FloCRM_Server' ) ) :
  11.  
  12.     class FloCRM_Server {
  13.  
  14.         /**
  15.          * @var string CRM Url to make post actions
  16.          */
  17.         static $crm_url = ''; // http://192.168.100.111/flocrm/one/
  18.  
  19.  
  20.         /**
  21.          * FloCRM_Server constructor.
  22.          */
  23.         function __construct() {
  24.  
  25.             add_action( 'admin_init', array($this, 'settings_api_init') );
  26.  
  27.             self::$crm_url = get_option( 'flocrm_site_url' );
  28.  
  29.  
  30.             // Includes endpoint class
  31.             require 'includes/class-endpoint.php';
  32.  
  33.             //add_action('woocommerce_order_status_completed', array($this, 'on_order_complete') );
  34.             add_action('woocommerce_order_status_changed', array($this, 'on_order_complete'), 99, 3);
  35.  
  36.             add_action('init', array($this, 'buy_additional_service'));
  37.  
  38.             //session_start();
  39.  
  40.  
  41.         }
  42.  
  43.         /**
  44.          * Trigger function when order is complete
  45.          *
  46.          * @return mixed
  47.          */
  48.         public function on_order_complete( $order_id, $old_status, $new_status ){
  49.  
  50.             if( $new_status !== "completed" && $new_status !== "partial-payment" ) return;
  51.  
  52.             $new_service_order = false;
  53.  
  54.             // For additional purchased services
  55.             if ( $parent_order = $this->if_is_additional_service( $order_id) ){
  56.  
  57.                 $new_order = new WC_Order( $order_id );
  58.  
  59.                 $items = array();
  60.  
  61.                 $need_this = array(
  62.                     'order_id',
  63.                     'name',
  64.                     'product_id',
  65.                     'variation_id',
  66.                     'quantity',
  67.                     'tax_class',
  68.                     'subtotal',
  69.                     'subtotal_tax',
  70.                     'total',
  71.                     'total_tax',
  72.                 );
  73.                 foreach ($new_order->get_items() as $key => $value) {
  74.                     $local = array();
  75.                     foreach ( $need_this as $prop ) {
  76.                         $local[ $prop ] = $value[ $prop ];
  77.                     }
  78.                     $items[] = $local;
  79.                 }
  80.  
  81.                 $response  = wp_remote_post( self::$crm_url . 'api/flocrm/additional_service', array(
  82.                     'method' => 'POST',
  83.                     'timeout' => 45,
  84.                     'redirection' => 5,
  85.                     'httpversion' => '1.0',
  86.                     'blocking' => true,
  87.                     'headers' => array(),
  88.                     'body' => array(
  89.                         'parent_order' => $parent_order,
  90.                         'child_order' => $order_id,
  91.                         'child_order_items' => $items,
  92.                     ),
  93.                     'cookies' => array()
  94.                 ));
  95.  
  96.                 // Check for error
  97.                 if ( is_wp_error( $response ) ) {
  98.                     return;
  99.                 }
  100.  
  101.                 return;
  102.  
  103.             } else {
  104.  
  105.                 $new_service_order = $this->order_has_service( $order_id );
  106.  
  107.             }
  108.  
  109.             if ( $new_service_order ) {
  110.                 $request  = wp_remote_get( self::$crm_url . 'api/crm_new_order/' . $order_id );
  111.             }
  112.  
  113.         }
  114.  
  115.         /**
  116.          * Returns TRUE if at least one product of this order contains taxonomy with slug "service"
  117.          *
  118.          * @param $order_id
  119.          * @return bool
  120.          */
  121.         function order_has_service( $order_id  ){
  122.             $new_order = new WC_Order( $order_id );
  123.  
  124.             $is_service = false;
  125.  
  126.             $service_cat = get_option( 'flocrm_sync_category' );
  127.  
  128.             foreach ($new_order->get_items() as $key => $value) {
  129.  
  130.                 $ids = $value->get_data('id');
  131.                 $prod_id = $ids['product_id'];
  132.  
  133.                 $args = array( 'taxonomy' => 'product_cat' );
  134.                 $terms = wp_get_post_terms($prod_id,'product_cat', $args);
  135.  
  136.                 if ( is_array($terms) ) {
  137.                     foreach ($terms as $term){
  138.  
  139.                         if ($term->slug == $service_cat) {
  140.                             $is_service = true;
  141.                             break;
  142.                         }
  143.                     }
  144.                 }
  145.  
  146.                 if ($is_service) break;
  147.  
  148.             }
  149.  
  150.             return $is_service;
  151.         }
  152.  
  153.         /**
  154.          * Checks and sets a child additional service to it's parent service
  155.          *
  156.          * @param $order_id
  157.          * @return bool true if it's a child order, and meta was updated, overwise false
  158.          */
  159.         public function if_is_additional_service( $order_id ){
  160.  
  161.             session_start();
  162.  
  163.             if ( empty($_SESSION['flocrm_add_service']) ) return false;
  164.  
  165.             $parent_id = $_SESSION['flocrm_add_service'];
  166.  
  167.             wp_update_post(
  168.                 array(
  169.                     'ID' => $order_id,
  170.                     'post_parent' => $parent_id
  171.                 )
  172.             );
  173.  
  174.             unset($_SESSION['flocrm_add_service']);
  175.             session_destroy();
  176.  
  177.             return $parent_id;
  178.  
  179.         }
  180.  
  181.         /**
  182.          *
  183.          * After logging in to flothemes, redirect back to CRM
  184.          * @param $redirect_to
  185.          * @return string
  186.          */
  187.         static function redirect_after_login( $redirect_to ){
  188.  
  189.             if ( isset( $_GET['crm_redirect'] ) ) {
  190.                 $redirect_to = self::$crm_url . 'my-account';
  191.             }
  192.  
  193.             return $redirect_to;
  194.         }
  195.  
  196.         /**
  197.          * Add allowed redirect link to the list
  198.          * @param $content
  199.          */
  200.         static function allowed_redirect_hosts( $content ){
  201.             $content[] = 'crm.flothemes.com';
  202.             $content[] = '192.168.100.111'; // localhost
  203.  
  204.             return $content;
  205.         }
  206.  
  207.         /**
  208.          * Callback for registering CRM settings in GENERAL page
  209.          */
  210.         public function settings_api_init() {
  211.  
  212.             // Add the section to general settings
  213.             add_settings_section(
  214.                 'flocrm_setting_section',
  215.                 'FloCRM Server Settings Section',
  216.                 array($this, 'flocrm_setting_section_callback_function'),
  217.                 'general'
  218.             );
  219.  
  220.             // Add the field with the names and function to use
  221.             add_settings_field(
  222.                 'flocrm_site_url',
  223.                 'FloCRM Site URL',
  224.                 array($this, 'flocrm_site_url_callback_function'),
  225.                 'general',
  226.                 'flocrm_setting_section'
  227.             );
  228.  
  229.  
  230.             register_setting( 'general', 'flocrm_site_url' );
  231.  
  232.             // Add the field with the names and function to use
  233.             add_settings_field(
  234.                 'flocrm_sync_category',
  235.                 'Product Category To Sync',
  236.                 array($this, 'flocrm_sync_category_callback_function'),
  237.                 'general',
  238.                 'flocrm_setting_section'
  239.             );
  240.  
  241.  
  242.             register_setting( 'general', 'flocrm_sync_category' );
  243.         }
  244.         /**
  245.          * Callback for CRM setting section
  246.          */
  247.         public function flocrm_setting_section_callback_function() {
  248.             //echo '<p>Settings for FloCRM Server-Side plugin</p>';
  249.         }
  250.         /**
  251.          * Callback for CRM url setting input
  252.          */
  253.         public function flocrm_site_url_callback_function() {
  254.             echo '<input name="flocrm_site_url" id="flocrm_site_url" type="url" value="'.get_option( 'flocrm_site_url' ).'" class="regular-text code" />
  255.            <p class="description" id="flocrm_site_url-description">The link should end with a closing slash "/".</p>';
  256.         }
  257.  
  258.         /**
  259.          * Callback for CRM sync categ setting input
  260.          */
  261.         public function flocrm_sync_category_callback_function() {
  262.             echo '<input name="flocrm_sync_category" id="flocrm_sync_category" type="text" value="'.get_option( 'flocrm_sync_category' ).'" class="regular-text code" />
  263.            <p class="description" id="flocrm_sync_category-description">Add only the category slug for products marked as CRM Services.</p>';
  264.         }
  265.  
  266.         /**
  267.          * Logic when additional order is purchased, to relate it to parent order
  268.          */
  269.         public function buy_additional_service(){
  270.  
  271.             if ( ! empty( $_GET['adserv'] ) ) {
  272.  
  273.                 $order_id = intval ( $_GET['adserv'] );
  274.  
  275.                 if ( get_post_type( $order_id ) != 'shop_order' )  return;
  276.  
  277.                 session_start();
  278.                 $_SESSION['flocrm_add_service'] = $order_id;
  279.                 session_write_close();
  280.  
  281.             }
  282.  
  283.         }
  284.  
  285.     }
  286.  
  287. endif;
  288.  
  289. new FloCRM_Server();
  290.  
  291. add_filter('woocommerce_login_redirect', array('FloCRM_Server', 'redirect_after_login'));
  292. add_filter( 'allowed_redirect_hosts' , array('FloCRM_Server', 'allowed_redirect_hosts') , 10 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement