| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoSheet Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\MailPoet; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use WP_Error; |
| 12 |
|
| 13 |
/** |
| 14 |
* Provide functionality for ZohoCrm integration |
| 15 |
*/ |
| 16 |
class MailpoetHandler |
| 17 |
{ |
| 18 |
private $_integrationID; |
| 19 |
|
| 20 |
public function __construct($integrationID, $fromID) |
| 21 |
{ |
| 22 |
$this->_integrationID = $integrationID; |
| 23 |
} |
| 24 |
|
| 25 |
/**bitforms_zsheet_refresh_worksheet_headers |
| 26 |
* Helps to register ajax function's with wp |
| 27 |
* |
| 28 |
* @return null |
| 29 |
*/ |
| 30 |
public static function registerAjax() |
| 31 |
{ |
| 32 |
add_action('wp_ajax_bitforms_mail_poet_authorize', [__CLASS__, 'mailPoetAuthorize']); |
| 33 |
add_action('wp_ajax_bitforms_refresh_news_letter', [__CLASS__, 'refreshNeswLetter']); |
| 34 |
add_action('wp_ajax_bitforms_mail_poet_list_headers', [__CLASS__, 'mailPoetListHeaders']); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Process ajax request for generate_token |
| 39 |
* |
| 40 |
* @return JSON zoho crm api response and status |
| 41 |
*/ |
| 42 |
public static function mailPoetAuthorize() |
| 43 |
{ |
| 44 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 45 |
if (class_exists(\MailPoet\API\API::class)) { |
| 46 |
wp_send_json_success(true); |
| 47 |
} else { |
| 48 |
wp_send_json_error( |
| 49 |
__( |
| 50 |
'Please! Insatall MailPoet', |
| 51 |
'bit-form' |
| 52 |
), |
| 53 |
400 |
| 54 |
); |
| 55 |
} |
| 56 |
} else { |
| 57 |
wp_send_json_error( |
| 58 |
__( |
| 59 |
'Token expired', |
| 60 |
'bit-form' |
| 61 |
), |
| 62 |
401 |
| 63 |
); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Process ajax request for refresh crm modules |
| 69 |
* |
| 70 |
* @return JSON crm module data |
| 71 |
*/ |
| 72 |
public static function refreshNeswLetter() |
| 73 |
{ |
| 74 |
$response = null; |
| 75 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 76 |
if (class_exists(\MailPoet\API\API::class)) { |
| 77 |
$mailpoet_api = \MailPoet\API\API::MP('v1'); |
| 78 |
$newsletterList = $mailpoet_api->getLists(); |
| 79 |
|
| 80 |
$allList = []; |
| 81 |
|
| 82 |
foreach ($newsletterList as $newsletter) { |
| 83 |
$allList[$newsletter['name']] = (object) [ |
| 84 |
'newsletterId' => $newsletter['id'], |
| 85 |
'newsletterName' => $newsletter['name'] |
| 86 |
]; |
| 87 |
} |
| 88 |
$response['newsletterList'] = $allList; |
| 89 |
wp_send_json_success($response, 200); |
| 90 |
} else { |
| 91 |
wp_send_json_error( |
| 92 |
__( |
| 93 |
'Please! Insatall MailPoet', |
| 94 |
'bit-form' |
| 95 |
), |
| 96 |
400 |
| 97 |
); |
| 98 |
} |
| 99 |
} else { |
| 100 |
wp_send_json_error( |
| 101 |
__( |
| 102 |
'Token expired', |
| 103 |
'bit-form' |
| 104 |
), |
| 105 |
401 |
| 106 |
); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public static function mailPoetListHeaders() |
| 111 |
{ |
| 112 |
$response = null; |
| 113 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 114 |
if (class_exists(\MailPoet\API\API::class)) { |
| 115 |
$mailpoet_api = \MailPoet\API\API::MP('v1'); |
| 116 |
$subscriber_form_fields = $mailpoet_api->getSubscriberFields(); |
| 117 |
|
| 118 |
$allList = []; |
| 119 |
|
| 120 |
foreach ($subscriber_form_fields as $fields) { |
| 121 |
$allList[$fields['name']] = (object) [ |
| 122 |
'id' => $fields['id'], |
| 123 |
'name' => $fields['name'], |
| 124 |
'required' => $fields['params']['required'] |
| 125 |
]; |
| 126 |
} |
| 127 |
$response['mailPoetFields'] = $allList; |
| 128 |
wp_send_json_success($response, 200); |
| 129 |
} else { |
| 130 |
wp_send_json_error( |
| 131 |
__( |
| 132 |
'Please! Insatall MailPoet', |
| 133 |
'bit-form' |
| 134 |
), |
| 135 |
400 |
| 136 |
); |
| 137 |
} |
| 138 |
} else { |
| 139 |
wp_send_json_error( |
| 140 |
__( |
| 141 |
'Token expired', |
| 142 |
'bit-form' |
| 143 |
), |
| 144 |
401 |
| 145 |
); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 150 |
{ |
| 151 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 152 |
|
| 153 |
$fieldMap = $integrationDetails->field_map; |
| 154 |
$defaultDataConf = $integrationDetails->default; |
| 155 |
$lists = $integrationDetails->lists; |
| 156 |
if (empty($fieldMap)) { |
| 157 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Mail Poet api', 'bit-form')); |
| 158 |
} |
| 159 |
|
| 160 |
$recordApiHelper = new RecordApiHelper($this->_integrationID, $logID, $entryID); |
| 161 |
|
| 162 |
$maiPoetApiResponse = $recordApiHelper->executeRecordApi( |
| 163 |
$fieldValues, |
| 164 |
$fieldMap, |
| 165 |
$lists |
| 166 |
); |
| 167 |
|
| 168 |
if (is_wp_error($maiPoetApiResponse)) { |
| 169 |
return $maiPoetApiResponse; |
| 170 |
} |
| 171 |
return $maiPoetApiResponse; |
| 172 |
} |
| 173 |
} |
| 174 |
|