| 1 |
<?php |
| 2 |
/** |
| 3 |
* FluentCRM Actions |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Actions; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class FluentCRM_Actions |
| 16 |
*/ |
| 17 |
class FluentCRM_Actions extends Action { |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
$this->id = 'fluentcrm'; |
| 24 |
$this->group = __( 'FluentCRM', 'wpbot-automator' ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Execute the action |
| 29 |
* |
| 30 |
* @param array $action_data Configuration for this action from the workflow. |
| 31 |
* @param array $trigger_data Data passed from the trigger. |
| 32 |
* @return bool|array |
| 33 |
*/ |
| 34 |
public function execute( $action_data, $trigger_data ) { |
| 35 |
$action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : ''; |
| 36 |
|
| 37 |
if ( empty( $action_id ) ) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
switch ( $action_id ) { |
| 42 |
case 'create_contact': |
| 43 |
return $this->create_contact( $action_data, $trigger_data ); |
| 44 |
default: |
| 45 |
return false; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Create or Update Contact in FluentCRM |
| 51 |
*/ |
| 52 |
private function create_contact( $action_data, $trigger_data ) { |
| 53 |
if ( ! function_exists( 'FluentCrmApi' ) ) { |
| 54 |
return array( |
| 55 |
'success' => false, |
| 56 |
'message' => 'FluentCRM plugin is not active.', |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
$cfg = isset( $action_data['config'] ) ? $action_data['config'] : array(); |
| 61 |
$email = trim( $this->parse_tokens( $cfg['email'] ?? '', $trigger_data ) ); |
| 62 |
|
| 63 |
if ( empty( $email ) ) { |
| 64 |
//error_log( 'WPbot Automator - FluentCRM: Email is empty after parsing tokens.' ); |
| 65 |
return array( |
| 66 |
'success' => false, |
| 67 |
'message' => 'Email address is required for FluentCRM contact creation.', |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! is_email( $email ) ) { |
| 72 |
//error_log( 'WPbot Automator - FluentCRM: Invalid email address: ' . $email ); |
| 73 |
return array( |
| 74 |
'success' => false, |
| 75 |
'message' => 'Invalid email address provided for FluentCRM.', |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
$contact_data = array( |
| 80 |
'email' => $email, |
| 81 |
'first_name' => $this->parse_tokens( $cfg['first_name'] ?? '', $trigger_data ), |
| 82 |
'last_name' => $this->parse_tokens( $cfg['last_name'] ?? '', $trigger_data ), |
| 83 |
'status' => $cfg['status'] ?? 'subscribed', |
| 84 |
); |
| 85 |
|
| 86 |
//error_log( 'WPbot Automator - FluentCRM Contact Data: ' . wp_json_encode( $contact_data ) ); |
| 87 |
|
| 88 |
try { |
| 89 |
$api = FluentCrmApi( 'contacts' ); |
| 90 |
//error_log( 'WPbot Automator - FluentCRM API object type: ' . gettype( $api ) ); |
| 91 |
if ( is_object( $api ) ) { |
| 92 |
//error_log( 'WPbot Automator - FluentCRM API class: ' . get_class( $api ) ); |
| 93 |
} |
| 94 |
|
| 95 |
$contact = $api->createOrUpdate( $contact_data ); |
| 96 |
//error_log( 'WPbot Automator - FluentCRM createOrUpdate result: ' . ( is_object( $contact ) ? 'OBJECT (ID: ' . $contact->id . ')' : ( $contact ? 'TRUE/POS' : 'FALSE/NEG' ) ) ); |
| 97 |
|
| 98 |
if ( $contact ) { |
| 99 |
return array( |
| 100 |
'success' => true, |
| 101 |
'message' => sprintf( 'Contact %s successfully created/updated in FluentCRM.', $email ), |
| 102 |
'fluentcrm_contact_id' => isset( $contact->id ) ? $contact->id : 0, |
| 103 |
); |
| 104 |
} |
| 105 |
} catch ( \Exception $e ) { |
| 106 |
//error_log( 'WPbot Automator - FluentCRM Exception: ' . $e->getMessage() ); |
| 107 |
return array( |
| 108 |
'success' => false, |
| 109 |
'message' => 'FluentCRM Error: ' . $e->getMessage(), |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
return array( |
| 114 |
'success' => false, |
| 115 |
'message' => 'Failed to create contact in FluentCRM.', |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|