PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.28
Booktics – Appointment Booking Calendar for Service Businesses v1.0.28
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / core / module / fluentcrm / fluentcrm-service.php

fluentcrm-service.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.28, at core/module/fluentcrm/fluentcrm-service.php

101 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * FluentCRM Service
4 *
5 * Handles FluentCRM integration for Booktics orders
6 */
7
8 namespace Booktics\Module\FluentCrm;
9
10 if ( ! defined( 'ABSPATH' ) ) exit;
11
12 use Booktics\Contracts\Hookable_Service_Contract;
13 use Booktics\Abstracts\Booktics_Database;
14 use Booktics\Models\Guest_Model;
15
16 class FluentCRM_Service implements Hookable_Service_Contract {
17 /**
18 * Register hooks
19 */
20 public function register() {
21 add_action( 'booktics_after_order_created', array( $this, 'add_contact_to_fluentcrm' ), 10, 2 );
22 }
23
24 /**
25 * Add contact to FluentCRM
26 *
27 * @param object $order_model The order model
28 * @param array $appointments Created appointments (unused).
29 */
30 public function add_contact_to_fluentcrm( $order_model, $appointments = array() ) {
31 // Check if FluentCRM is active
32 if ( ! defined( 'FLUENTCRM' ) || ! function_exists( 'FluentCrmApi' ) ) {
33 return;
34 }
35
36 // Get customer details. customer_id is a Booktics guest record id, not a WP user id.
37 $customer = ( new Guest_Model( new Booktics_Database() ) )->find( array( 'id' => $order_model->customer_id ) );
38 if ( ! $customer || is_wp_error( $customer ) ) {
39 return;
40 }
41
42 $name_parts = preg_split( '/\s+/', trim( (string) $customer->name ), 2 );
43
44 $customer_data = array(
45 'email' => $customer->email,
46 'first_name' => $name_parts[0] ?? '',
47 'last_name' => $name_parts[1] ?? '',
48 'status' => 'subscribed',
49 );
50
51 try {
52 // Check if FluentCRM Pro is active (has webhooks)
53 $is_pro_active = is_plugin_active('fluent-crm-pro/fluentcampaign-pro.php');
54 $fluentcrm_webhook_exists = !empty( booktics_get_option( 'fluentcrm_webhook_url') );
55
56 if ( $is_pro_active && $fluentcrm_webhook_exists ) {
57 // Use webhook if Pro is active
58 $webhook_url = booktics_get_option( 'fluentcrm_webhook_url');
59
60 if ( ! empty( $webhook_url ) ) {
61 $this->send_webhook(
62 $webhook_url,
63 $customer_data,
64 );
65
66 // Returns if fluentCRM pro exists and is working properly
67 return;
68 }
69 }
70
71 // Fallback to direct API for free version, gets used when fluentCRM pro is not active or webhook is not set.
72 $contact_api = FluentCrmApi( 'contacts' );
73 $contact_api->createOrUpdate( $customer_data );
74 } catch ( \Throwable $e ) {
75 // Ensures silent failure in case of any error
76 if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
77 error_log( 'FluentCRM Error: ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
78 }
79 }
80 }
81
82 private function send_webhook( $url, $data ) {
83 $response = wp_remote_post(
84 $url,
85 array(
86 'body' => wp_json_encode( $data ),
87 'headers' => array(
88 'Content-Type' => 'application/json',
89 ),
90 'timeout' => 15,
91 )
92 );
93
94 if ( is_wp_error( $response ) ) {
95 if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
96 error_log( 'FluentCRM Webhook Error: ' . $response->get_error_message() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
97 }
98 }
99 }
100 }
101