| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Fluent CRM Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\FluentCrm; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 15 |
use FluentCrm\App\Models\CustomContactField; |
| 16 |
use FluentCrm\App\Models\Lists; |
| 17 |
use FluentCrm\App\Models\Subscriber; |
| 18 |
use FluentCrm\App\Models\Tag; |
| 19 |
use WP_Error; |
| 20 |
|
| 21 |
/** |
| 22 |
* Provide functionality for ZohoCrm integration |
| 23 |
*/ |
| 24 |
class FluentCrmHandler |
| 25 |
{ |
| 26 |
private $_formID; |
| 27 |
private $_integrationID; |
| 28 |
|
| 29 |
public function __construct($integrationID, $fromID) |
| 30 |
{ |
| 31 |
$this->_formID = $fromID; |
| 32 |
$this->_integrationID = $integrationID; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Helps to register ajax function's with wp |
| 37 |
* |
| 38 |
* @return null |
| 39 |
*/ |
| 40 |
public static function registerAjax() |
| 41 |
{ |
| 42 |
add_action('wp_ajax_bitforms_fluent_crm_authorize', [__CLASS__, 'fluentCrmAuthorize']); |
| 43 |
add_action('wp_ajax_bitforms_refresh_fluent_crm_lists', [__CLASS__, 'fluentCrmLists']); |
| 44 |
add_action('wp_ajax_bitforms_fluent_crm_headers', [__CLASS__, 'fluentCrmFields']); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* for chen fluent crm plugins are exists |
| 49 |
*/ |
| 50 |
public static function checkedExistsFluentCRM() |
| 51 |
{ |
| 52 |
return is_plugin_active('fluent-crm/fluent-crm.php') ? true : false; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @return Fluent CRM lists |
| 57 |
*/ |
| 58 |
public static function fluentCrmLists() |
| 59 |
{ |
| 60 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 61 |
$response = null; |
| 62 |
if (self::checkedExistsFluentCRM()) { |
| 63 |
$lists = Lists::get(); |
| 64 |
$fluentCrmList = []; |
| 65 |
foreach ($lists as $list) { |
| 66 |
$fluentCrmList[$list->title] = (object) [ |
| 67 |
'id' => $list->id, |
| 68 |
'title' => $list->title |
| 69 |
]; |
| 70 |
} |
| 71 |
$tags = Tag::get(); |
| 72 |
$fluentCrmTags = []; |
| 73 |
foreach ($tags as $tag) { |
| 74 |
$fluentCrmTags[$tag->title] = (object) [ |
| 75 |
'id' => $tag->id, |
| 76 |
'title' => $tag->title |
| 77 |
]; |
| 78 |
} |
| 79 |
$response['fluentCrmList'] = $fluentCrmList; |
| 80 |
$response['fluentCrmTags'] = $fluentCrmTags; |
| 81 |
} else { |
| 82 |
wp_send_json_error( |
| 83 |
__( |
| 84 |
'Fluent CRM Plugins not found', |
| 85 |
'bit-form' |
| 86 |
), |
| 87 |
400 |
| 88 |
); |
| 89 |
} |
| 90 |
wp_send_json_success($response, 200); |
| 91 |
} else { |
| 92 |
wp_send_json_error( |
| 93 |
__( |
| 94 |
'Token expired', |
| 95 |
'bit-form' |
| 96 |
), |
| 97 |
401 |
| 98 |
); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
public static function fluentCrmFields() |
| 103 |
{ |
| 104 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 105 |
$response = null; |
| 106 |
if (self::checkedExistsFluentCRM()) { |
| 107 |
$fieldOptions = []; |
| 108 |
$primaryField = ['first_name', 'last_name', 'full_name', 'email']; |
| 109 |
|
| 110 |
foreach (Subscriber::mappables() as $key => $column) { |
| 111 |
if (in_array($key, $primaryField)) { |
| 112 |
if ('email' === $key) { |
| 113 |
$fieldOptions[$column] = (object) [ |
| 114 |
'key' => $key, |
| 115 |
'label' => $column, |
| 116 |
'type' => 'primary', |
| 117 |
'required' => true |
| 118 |
]; |
| 119 |
} else { |
| 120 |
$fieldOptions[$column] = (object) [ |
| 121 |
'key' => $key, |
| 122 |
'label' => $column, |
| 123 |
'type' => 'primary' |
| 124 |
]; |
| 125 |
} |
| 126 |
} else { |
| 127 |
$fieldOptions[$column] = (object) [ |
| 128 |
'key' => $key, |
| 129 |
'label' => $column, |
| 130 |
'type' => 'custom' |
| 131 |
]; |
| 132 |
} |
| 133 |
} |
| 134 |
foreach ((new CustomContactField())->getGlobalFields()['fields'] as $field) { |
| 135 |
$fieldOptions[$field['label']] = (object) [ |
| 136 |
'key' => $field['slug'], |
| 137 |
'label' => $field['label'], |
| 138 |
'type' => 'custom' |
| 139 |
]; |
| 140 |
} |
| 141 |
$response['fluentCrmFlelds'] = $fieldOptions; |
| 142 |
} else { |
| 143 |
wp_send_json_error( |
| 144 |
__( |
| 145 |
'Fluent CRM Plugins not found', |
| 146 |
'bit-form' |
| 147 |
), |
| 148 |
400 |
| 149 |
); |
| 150 |
} |
| 151 |
wp_send_json_success($response, 200); |
| 152 |
} else { |
| 153 |
wp_send_json_error( |
| 154 |
__( |
| 155 |
'Token expired', |
| 156 |
'bit-form' |
| 157 |
), |
| 158 |
401 |
| 159 |
); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @return True Fluent crm are exists |
| 165 |
*/ |
| 166 |
public static function fluentCrmAuthorize() |
| 167 |
{ |
| 168 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 169 |
if (self::checkedExistsFluentCRM()) { |
| 170 |
wp_send_json_success(true); |
| 171 |
} else { |
| 172 |
wp_send_json_error( |
| 173 |
__( |
| 174 |
'Please! Insatall Fluent CRM', |
| 175 |
'bit-form' |
| 176 |
), |
| 177 |
400 |
| 178 |
); |
| 179 |
} |
| 180 |
} else { |
| 181 |
wp_send_json_error( |
| 182 |
__( |
| 183 |
'Token expired', |
| 184 |
'bit-form' |
| 185 |
), |
| 186 |
401 |
| 187 |
); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 192 |
{ |
| 193 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 194 |
|
| 195 |
// wp_send_json_success($integrationDetails); |
| 196 |
|
| 197 |
$fieldMap = $integrationDetails->field_map; |
| 198 |
$defaultDataConf = $integrationDetails->default; |
| 199 |
$list_id = $integrationDetails->list_id; |
| 200 |
$tags = $integrationDetails->tags; |
| 201 |
$actions = $integrationDetails->actions; |
| 202 |
// wp_send_json_success($fieldMap); |
| 203 |
if (empty($fieldMap)) { |
| 204 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Fluent CRM api', 'bit-form')); |
| 205 |
} |
| 206 |
|
| 207 |
$recordApiHelper = new RecordApiHelper($this->_integrationID, $logID, $entryID); |
| 208 |
|
| 209 |
$fluentCrmApiResponse = $recordApiHelper->executeRecordApi( |
| 210 |
$fieldValues, |
| 211 |
$fieldMap, |
| 212 |
$actions, |
| 213 |
$list_id, |
| 214 |
$tags, |
| 215 |
$this->_formID |
| 216 |
); |
| 217 |
|
| 218 |
if (is_wp_error($fluentCrmApiResponse)) { |
| 219 |
return $fluentCrmApiResponse; |
| 220 |
} |
| 221 |
return $fluentCrmApiResponse; |
| 222 |
} |
| 223 |
} |
| 224 |
|