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