| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Autonami Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\Autonami; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 12 |
use BWFCRM_Fields; |
| 13 |
use BWFCRM_Lists; |
| 14 |
use BWFCRM_Tag; |
| 15 |
use WP_Error; |
| 16 |
|
| 17 |
class AutonamiHandler |
| 18 |
{ |
| 19 |
private $_formID; |
| 20 |
private $_integrationID; |
| 21 |
|
| 22 |
public function __construct($integrationID, $fromID) |
| 23 |
{ |
| 24 |
$this->_formID = $fromID; |
| 25 |
$this->_integrationID = $integrationID; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Helps to register ajax function's with wp |
| 30 |
* |
| 31 |
* @return null |
| 32 |
*/ |
| 33 |
public static function registerAjax() |
| 34 |
{ |
| 35 |
add_action('wp_ajax_bitforms_autonami_authorize', [__CLASS__, 'autonamiAuthorize']); |
| 36 |
add_action('wp_ajax_bitforms_autonami_lists_and_tags', [__CLASS__, 'autonamiListsAndTags']); |
| 37 |
add_action('wp_ajax_bitforms_autonami_fields', [__CLASS__, 'autonamiFields']); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* for chen autonami pro plugins are exists |
| 42 |
*/ |
| 43 |
public static function checkedExistsAutonami() |
| 44 |
{ |
| 45 |
if (!class_exists('BWFCRM_Contact')) { |
| 46 |
wp_send_json_error(__('Autonami Pro Plugins not found', 'bit-form'), 400); |
| 47 |
} else { |
| 48 |
return true; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @return Autonami lists |
| 54 |
*/ |
| 55 |
public static function autonamiListsAndTags() |
| 56 |
{ |
| 57 |
if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 58 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 59 |
} |
| 60 |
|
| 61 |
self::checkedExistsAutonami(); |
| 62 |
|
| 63 |
$lists = BWFCRM_Lists::get_lists(); |
| 64 |
$autonamiList = []; |
| 65 |
foreach ($lists as $list) { |
| 66 |
$autonamiList[$list['name']] = (object) [ |
| 67 |
'id' => $list['ID'], |
| 68 |
'title' => $list['name'] |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
$tags = BWFCRM_Tag::get_tags(); |
| 73 |
$autonamiTags = []; |
| 74 |
foreach ($tags as $tag) { |
| 75 |
$autonamiTags[$tag['name']] = (object) [ |
| 76 |
'id' => $tag['ID'], |
| 77 |
'title' => $tag['name'] |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
$response['autonamiList'] = $autonamiList; |
| 82 |
$response['autonamiTags'] = $autonamiTags; |
| 83 |
wp_send_json_success($response, 200); |
| 84 |
} |
| 85 |
|
| 86 |
public static function autonamiFields() |
| 87 |
{ |
| 88 |
if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 89 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 90 |
} |
| 91 |
|
| 92 |
self::checkedExistsAutonami(); |
| 93 |
|
| 94 |
$fieldOptions = []; |
| 95 |
$fieldOptions['Email'] = (object) [ |
| 96 |
'key' => 'email', |
| 97 |
'label' => 'Email', |
| 98 |
'type' => 'primary', |
| 99 |
'required' => true |
| 100 |
]; |
| 101 |
foreach (BWFCRM_Fields::get_default_fields() as $key => $column) { |
| 102 |
$fieldOptions[$column] = (object) [ |
| 103 |
'key' => $key, |
| 104 |
'label' => $column, |
| 105 |
'type' => 'primary' |
| 106 |
]; |
| 107 |
} |
| 108 |
foreach (BWFCRM_Fields::get_custom_fields(1, 1) as $field) { |
| 109 |
$fieldOptions[$field['slug']] = (object) [ |
| 110 |
'key' => $field['slug'], |
| 111 |
'label' => $field['name'], |
| 112 |
'type' => 'custom' |
| 113 |
]; |
| 114 |
} |
| 115 |
$response['autonamiFields'] = $fieldOptions; |
| 116 |
wp_send_json_success($response, 200); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @return True Autonami are exists |
| 121 |
*/ |
| 122 |
public static function autonamiAuthorize() |
| 123 |
{ |
| 124 |
if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 125 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 126 |
} |
| 127 |
|
| 128 |
if (self::checkedExistsAutonami()) { |
| 129 |
wp_send_json_success(true); |
| 130 |
} else { |
| 131 |
wp_send_json_error(__('Autonami Pro Plugins not found', 'bit-form'), 400); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 136 |
{ |
| 137 |
if (!class_exists('BWFCRM_Contact')) { |
| 138 |
(new UtilApiResponse())->apiResponse($logID, $this->_integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Autonami Pro Plugins not found'); |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 143 |
|
| 144 |
$fieldMap = $integrationDetails->field_map; |
| 145 |
$lists = $integrationDetails->lists; |
| 146 |
$tags = $integrationDetails->tags; |
| 147 |
$actions = $integrationDetails->actions; |
| 148 |
|
| 149 |
if (empty($fieldMap)) { |
| 150 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Autonami api', 'bit-form')); |
| 151 |
} |
| 152 |
|
| 153 |
$recordApiHelper = new RecordApiHelper($this->_integrationID, $logID, $entryID); |
| 154 |
$autonamiApiResponse = $recordApiHelper->executeRecordApi($fieldValues, $fieldMap, $actions, $lists, $tags); |
| 155 |
|
| 156 |
if (is_wp_error($autonamiApiResponse)) { |
| 157 |
return $autonamiApiResponse; |
| 158 |
} |
| 159 |
return $autonamiApiResponse; |
| 160 |
} |
| 161 |
} |
| 162 |
|