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