Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Flo CRM Server plugin
- Description: This small plugin should be installed on main server to send info to remote CRM
- Version: 0.1.0
- Author: Lozovan Nicolae
- Author URI: http://flothemes.com
- */
- if ( ! class_exists( 'FloCRM_Server' ) ) :
- class FloCRM_Server {
- /**
- * @var string CRM Url to make post actions
- */
- static $crm_url = ''; // http://192.168.100.111/flocrm/one/
- /**
- * FloCRM_Server constructor.
- */
- function __construct() {
- add_action( 'admin_init', array($this, 'settings_api_init') );
- self::$crm_url = get_option( 'flocrm_site_url' );
- // Includes endpoint class
- require 'includes/class-endpoint.php';
- //add_action('woocommerce_order_status_completed', array($this, 'on_order_complete') );
- add_action('woocommerce_order_status_changed', array($this, 'on_order_complete'), 99, 3);
- add_action('init', array($this, 'buy_additional_service'));
- //session_start();
- }
- /**
- * Trigger function when order is complete
- *
- * @return mixed
- */
- public function on_order_complete( $order_id, $old_status, $new_status ){
- if( $new_status !== "completed" && $new_status !== "partial-payment" ) return;
- $new_service_order = false;
- // For additional purchased services
- if ( $parent_order = $this->if_is_additional_service( $order_id) ){
- $new_order = new WC_Order( $order_id );
- $items = array();
- $need_this = array(
- 'order_id',
- 'name',
- 'product_id',
- 'variation_id',
- 'quantity',
- 'tax_class',
- 'subtotal',
- 'subtotal_tax',
- 'total',
- 'total_tax',
- );
- foreach ($new_order->get_items() as $key => $value) {
- $local = array();
- foreach ( $need_this as $prop ) {
- $local[ $prop ] = $value[ $prop ];
- }
- $items[] = $local;
- }
- $response = wp_remote_post( self::$crm_url . 'api/flocrm/additional_service', array(
- 'method' => 'POST',
- 'timeout' => 45,
- 'redirection' => 5,
- 'httpversion' => '1.0',
- 'blocking' => true,
- 'headers' => array(),
- 'body' => array(
- 'parent_order' => $parent_order,
- 'child_order' => $order_id,
- 'child_order_items' => $items,
- ),
- 'cookies' => array()
- ));
- // Check for error
- if ( is_wp_error( $response ) ) {
- return;
- }
- return;
- } else {
- $new_service_order = $this->order_has_service( $order_id );
- }
- if ( $new_service_order ) {
- $request = wp_remote_get( self::$crm_url . 'api/crm_new_order/' . $order_id );
- }
- }
- /**
- * Returns TRUE if at least one product of this order contains taxonomy with slug "service"
- *
- * @param $order_id
- * @return bool
- */
- function order_has_service( $order_id ){
- $new_order = new WC_Order( $order_id );
- $is_service = false;
- $service_cat = get_option( 'flocrm_sync_category' );
- foreach ($new_order->get_items() as $key => $value) {
- $ids = $value->get_data('id');
- $prod_id = $ids['product_id'];
- $args = array( 'taxonomy' => 'product_cat' );
- $terms = wp_get_post_terms($prod_id,'product_cat', $args);
- if ( is_array($terms) ) {
- foreach ($terms as $term){
- if ($term->slug == $service_cat) {
- $is_service = true;
- break;
- }
- }
- }
- if ($is_service) break;
- }
- return $is_service;
- }
- /**
- * Checks and sets a child additional service to it's parent service
- *
- * @param $order_id
- * @return bool true if it's a child order, and meta was updated, overwise false
- */
- public function if_is_additional_service( $order_id ){
- session_start();
- if ( empty($_SESSION['flocrm_add_service']) ) return false;
- $parent_id = $_SESSION['flocrm_add_service'];
- wp_update_post(
- array(
- 'ID' => $order_id,
- 'post_parent' => $parent_id
- )
- );
- unset($_SESSION['flocrm_add_service']);
- session_destroy();
- return $parent_id;
- }
- /**
- *
- * After logging in to flothemes, redirect back to CRM
- * @param $redirect_to
- * @return string
- */
- static function redirect_after_login( $redirect_to ){
- if ( isset( $_GET['crm_redirect'] ) ) {
- $redirect_to = self::$crm_url . 'my-account';
- }
- return $redirect_to;
- }
- /**
- * Add allowed redirect link to the list
- * @param $content
- */
- static function allowed_redirect_hosts( $content ){
- $content[] = 'crm.flothemes.com';
- $content[] = '192.168.100.111'; // localhost
- return $content;
- }
- /**
- * Callback for registering CRM settings in GENERAL page
- */
- public function settings_api_init() {
- // Add the section to general settings
- add_settings_section(
- 'flocrm_setting_section',
- 'FloCRM Server Settings Section',
- array($this, 'flocrm_setting_section_callback_function'),
- 'general'
- );
- // Add the field with the names and function to use
- add_settings_field(
- 'flocrm_site_url',
- 'FloCRM Site URL',
- array($this, 'flocrm_site_url_callback_function'),
- 'general',
- 'flocrm_setting_section'
- );
- register_setting( 'general', 'flocrm_site_url' );
- // Add the field with the names and function to use
- add_settings_field(
- 'flocrm_sync_category',
- 'Product Category To Sync',
- array($this, 'flocrm_sync_category_callback_function'),
- 'general',
- 'flocrm_setting_section'
- );
- register_setting( 'general', 'flocrm_sync_category' );
- }
- /**
- * Callback for CRM setting section
- */
- public function flocrm_setting_section_callback_function() {
- //echo '<p>Settings for FloCRM Server-Side plugin</p>';
- }
- /**
- * Callback for CRM url setting input
- */
- public function flocrm_site_url_callback_function() {
- echo '<input name="flocrm_site_url" id="flocrm_site_url" type="url" value="'.get_option( 'flocrm_site_url' ).'" class="regular-text code" />
- <p class="description" id="flocrm_site_url-description">The link should end with a closing slash "/".</p>';
- }
- /**
- * Callback for CRM sync categ setting input
- */
- public function flocrm_sync_category_callback_function() {
- echo '<input name="flocrm_sync_category" id="flocrm_sync_category" type="text" value="'.get_option( 'flocrm_sync_category' ).'" class="regular-text code" />
- <p class="description" id="flocrm_sync_category-description">Add only the category slug for products marked as CRM Services.</p>';
- }
- /**
- * Logic when additional order is purchased, to relate it to parent order
- */
- public function buy_additional_service(){
- if ( ! empty( $_GET['adserv'] ) ) {
- $order_id = intval ( $_GET['adserv'] );
- if ( get_post_type( $order_id ) != 'shop_order' ) return;
- session_start();
- $_SESSION['flocrm_add_service'] = $order_id;
- session_write_close();
- }
- }
- }
- endif;
- new FloCRM_Server();
- add_filter('woocommerce_login_redirect', array('FloCRM_Server', 'redirect_after_login'));
- add_filter( 'allowed_redirect_hosts' , array('FloCRM_Server', 'allowed_redirect_hosts') , 10 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement