PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.20
Booktics – Appointment Booking Calendar for Service Businesses v1.0.20
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.20, at core/module/fluentcrm/fluentcrm-service.php

96 lines 2.6 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
14 class FluentCRM_Service implements Hookable_Service_Contract {
15 /**
16 * Register hooks
17 */
18 public function register() {
19 add_action( 'booktics_order_created', array( $this, 'add_contact_to_fluentcrm' ) );
20 }
21
22 /**
23 * Add contact to FluentCRM
24 *
25 * @param object $order_model The order model
26 */
27 public function add_contact_to_fluentcrm( $order_model ) {
28 // Check if FluentCRM is active
29 if ( ! defined( 'FLUENTCRM' ) || ! function_exists( 'FluentCrmApi' ) ) {
30 return;
31 }
32
33 // Get customer details
34 $customer = get_user_by( 'id', $order_model->customer_id );
35 if ( ! $customer ) {
36 return;
37 }
38
39 $customer_data = array(
40 'email' => $customer->user_email,
41 'first_name' => $customer->first_name,
42 'last_name' => $customer->last_name ?: '',
43 'status' => 'subscribed',
44 );
45
46 try {
47 // Check if FluentCRM Pro is active (has webhooks)
48 $is_pro_active = is_plugin_active('fluent-crm-pro/fluentcampaign-pro.php');
49 $fluentcrm_webhook_exists = !empty( booktics_get_option( 'fluentcrm_webhook_url') );
50
51 if ( $is_pro_active && $fluentcrm_webhook_exists ) {
52 // Use webhook if Pro is active
53 $webhook_url = booktics_get_option( 'fluentcrm_webhook_url');
54
55 if ( ! empty( $webhook_url ) ) {
56 $this->send_webhook(
57 $webhook_url,
58 $customer_data,
59 );
60
61 // Returns if fluentCRM pro exists and is working properly
62 return;
63 }
64 }
65
66 // Fallback to direct API for free version, gets used when fluentCRM pro is not active or webhook is not set.
67 $contact_api = FluentCrmApi( 'contacts' );
68 $contact_api->createOrUpdate( $customer_data );
69 } catch ( \Throwable $e ) {
70 // Ensures silent failure in case of any error
71 if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
72 error_log( 'FluentCRM Error: ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
73 }
74 }
75 }
76
77 private function send_webhook( $url, $data ) {
78 $response = wp_remote_post(
79 $url,
80 array(
81 'body' => wp_json_encode( $data ),
82 'headers' => array(
83 'Content-Type' => 'application/json',
84 ),
85 'timeout' => 15,
86 )
87 );
88
89 if ( is_wp_error( $response ) ) {
90 if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
91 error_log( 'FluentCRM Webhook Error: ' . $response->get_error_message() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
92 }
93 }
94 }
95 }
96