| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* |
| 5 |
* @package BitForms |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Database\FormEntryModel; |
| 11 |
use BitCode\BitForm\Core\Database\FormModel; |
| 12 |
use BitCode\BitForm\Core\Util\ApiResponse; |
| 13 |
use BitCode\BitForm\Core\Util\SmartTags; |
| 14 |
use BitCode\BitForm\Core\Util\Utilities; |
| 15 |
/** |
| 16 |
* Provides details of available integration and helps to |
| 17 |
* execute available integrations |
| 18 |
*/ |
| 19 |
|
| 20 |
use FilesystemIterator; |
| 21 |
|
| 22 |
final class Integrations |
| 23 |
{ |
| 24 |
public $integrations = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* Undocumented function |
| 28 |
* |
| 29 |
* @return array |
| 30 |
*/ |
| 31 |
public function getAllIntegrations() |
| 32 |
{ |
| 33 |
return $this->allIntegrations(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Undocumented function |
| 38 |
* |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
protected function allIntegrations() |
| 42 |
{ |
| 43 |
$dirs = new FilesystemIterator(__DIR__); |
| 44 |
foreach ($dirs as $dirInfo) { |
| 45 |
if ($dirInfo->isDir()) { |
| 46 |
$integrationsBaseName = basename($dirInfo); |
| 47 |
if ( |
| 48 |
file_exists(__DIR__ . '/' . $integrationsBaseName) |
| 49 |
&& file_exists(__DIR__ . '/' . $integrationsBaseName . '/' . $integrationsBaseName . 'Handler.php') |
| 50 |
) { |
| 51 |
$integrations[] = $integrationsBaseName; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
return $integrations; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Checks a Integration Exists or not |
| 60 |
* |
| 61 |
* @param string $name Name of Integration |
| 62 |
* @return boolean | string |
| 63 |
*/ |
| 64 |
protected static function isExists($name) |
| 65 |
{ |
| 66 |
$path = Utilities::getRealPath(__DIR__, $name); |
| 67 |
if (!empty($path) && $fileName = Utilities::getRealPath(__DIR__ . "/{$path}", "{$name}Handler.php")) { |
| 68 |
$className = Utilities::getFileNameExceptExtension($fileName); |
| 69 |
return __NAMESPACE__ . "\\{$path}\\{$className}"; |
| 70 |
} |
| 71 |
|
| 72 |
if (!defined('BITFORMPRO_PLUGIN_DIR_PATH')) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
$proPath = BITFORMPRO_PLUGIN_DIR_PATH . '/includes/Integration'; |
| 77 |
$proNamespace = 'BitCode\\BitFormPro\\Integration'; |
| 78 |
$path = Utilities::getRealPath($proPath, $name); |
| 79 |
if (!empty($path) && $fileName = Utilities::getRealPath("{$proPath}/{$path}", "{$name}Handler.php")) { |
| 80 |
$className = Utilities::getFileNameExceptExtension($fileName); |
| 81 |
return "{$proNamespace}\\{$path}\\{$className}"; |
| 82 |
} |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* This function helps to get single integration information |
| 89 |
* |
| 90 |
* @return bool setIntegration() |
| 91 |
*/ |
| 92 |
public function getIntegration($integrationBaseName) |
| 93 |
{ |
| 94 |
return $this->setIntegration($integrationBaseName); |
| 95 |
} |
| 96 |
|
| 97 |
public function registerAjax() |
| 98 |
{ |
| 99 |
$dirs = new FilesystemIterator(__DIR__); |
| 100 |
foreach ($dirs as $dirInfo) { |
| 101 |
if ($dirInfo->isDir()) { |
| 102 |
$integrationBaseName = basename($dirInfo); |
| 103 |
if ( |
| 104 |
file_exists(__DIR__ . '/' . $integrationBaseName) |
| 105 |
&& file_exists(__DIR__ . '/' . $integrationBaseName . '/' . $integrationBaseName . 'Handler.php') |
| 106 |
) { |
| 107 |
$integration = __NAMESPACE__ . "\\{$integrationBaseName}\\{$integrationBaseName}Handler"; |
| 108 |
if (method_exists($integration, 'registerAjax')) { |
| 109 |
$integration::registerAjax(); |
| 110 |
// (new $integration(0, 0))->registerAjax(); |
| 111 |
} |
| 112 |
/* $handler = new $integration($integrationID, $formID); |
| 113 |
$handler->execute($integrationHandler, $integrationDetails, $fieldValues); */ |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
protected static function entryDeleted($formId, $entryId) |
| 120 |
{ |
| 121 |
$formModel = new FormModel(); |
| 122 |
$formContent = $formModel->get( |
| 123 |
[ |
| 124 |
'id', |
| 125 |
'form_content', |
| 126 |
], |
| 127 |
[ |
| 128 |
'id' => $formId, |
| 129 |
] |
| 130 |
); |
| 131 |
if (!is_wp_error($formContent)) { |
| 132 |
$content = \json_decode($formContent[0]->form_content); |
| 133 |
if (isset($content->additional->enabled->submission)) { |
| 134 |
global $wpdb; |
| 135 |
$prefix = $wpdb->prefix; |
| 136 |
$formEntryModel = new FormEntryModel(); |
| 137 |
$formEntryModel->bulkDelete( |
| 138 |
[ |
| 139 |
"`{$prefix}bitforms_form_entries`.`id`" => $entryId, |
| 140 |
"`{$prefix}bitforms_form_entries`.`form_id`" => $formId, |
| 141 |
] |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
public static function specialTagFields($fieldMap) |
| 148 |
{ |
| 149 |
$specialTagFieldValue = []; |
| 150 |
|
| 151 |
foreach ($fieldMap as $value) { |
| 152 |
$triggerValue = $value->formField; |
| 153 |
$specialTagFieldValue[$value->formField] = SmartTags::getSmartTagValue($triggerValue); |
| 154 |
} |
| 155 |
return $specialTagFieldValue; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* This function helps to execute Integration |
| 160 |
* |
| 161 |
* @param array $integrations List of integration to execute. |
| 162 |
* Element will be json string like {"id":1} |
| 163 |
* @param array $fieldValues Values of submitted fields |
| 164 |
* @param integer $formID ID of current form |
| 165 |
* |
| 166 |
* @return void Nothing to return |
| 167 |
*/ |
| 168 |
public static function executeIntegrations($integrations, $fieldValues, $formID, $logID = null, $entryID = 0) |
| 169 |
{ |
| 170 |
$integrationHandler = new IntegrationHandler($formID); |
| 171 |
$logResponse = new ApiResponse(); |
| 172 |
if (is_array($integrations)) { |
| 173 |
foreach ($integrations as $integrationIDStr) { |
| 174 |
if (!is_string($integrationIDStr)) { |
| 175 |
return; |
| 176 |
} |
| 177 |
$integrationID = intval(json_decode($integrationIDStr)->id, 10); |
| 178 |
if (!empty($integrationID) && is_int($integrationID)) { |
| 179 |
$integrationResult |
| 180 |
= $integrationHandler->getAIntegration($integrationID); |
| 181 |
$integrationDetails = is_wp_error($integrationResult) ? null : $integrationResult[0]; |
| 182 |
$integrationName = is_null($integrationDetails) ? null : ucfirst(str_replace(' ', '', $integrationDetails->integration_type)); |
| 183 |
if ('Brevo(SendinBlue)' === $integrationName) { |
| 184 |
$integrationName = 'SendinBlue'; |
| 185 |
} |
| 186 |
if (!is_null($integrationName) && static::isExists($integrationName)) { |
| 187 |
$integration = static::isExists($integrationName); |
| 188 |
$handler = new $integration($integrationID, $formID); |
| 189 |
$integDetails = is_string($integrationDetails->integration_details) ? json_decode($integrationDetails->integration_details) : $integrationDetails->integration_details; |
| 190 |
if (isset($integDetails->field_map)) { |
| 191 |
$sptagData = self::specialTagFields($integDetails->field_map); |
| 192 |
$fieldValues = $fieldValues + $sptagData; |
| 193 |
} |
| 194 |
$handler->execute($integrationHandler, $integrationDetails, $fieldValues, $entryID, $logID); |
| 195 |
} else { |
| 196 |
do_action('bitform_integration_run_failure', $integrationID, $formID); |
| 197 |
|
| 198 |
$errorMsg = apply_filters('bitform_filter_integration_run_failure_message', __('Integration execution failed', 'bit-form'), $integrationID, $formID); |
| 199 |
|
| 200 |
error_log('[+] Integration execution failed: ' . print_r($errorMsg, true)); |
| 201 |
|
| 202 |
$logResponse->apiResponse( |
| 203 |
$logID, |
| 204 |
$integrationID, |
| 205 |
['type' => 'record', 'type_name' => $integrationName || 'Integration'], |
| 206 |
'errors', |
| 207 |
$errorMsg |
| 208 |
); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
public static function integrationExecutionHelper($integrations, $fieldValue, $formID, $entryID, $logID) |
| 216 |
{ |
| 217 |
if (empty($integrations) || empty($formID)) { |
| 218 |
return; |
| 219 |
} |
| 220 |
$trnasientData = get_transient("bitform_trigger_transient_{$entryID}"); |
| 221 |
$trnasientData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData; |
| 222 |
if (!empty($trnasientData['fields'])) { |
| 223 |
$fieldValue = array_merge($fieldValue, $trnasientData['fields']); |
| 224 |
} |
| 225 |
foreach ($integrations as $key => $value) { |
| 226 |
self::executeIntegrations($value, $fieldValue, $formID, $logID, $entryID); |
| 227 |
} |
| 228 |
self::entryDeleted($formID, $entryID); |
| 229 |
} |
| 230 |
} |
| 231 |
|