| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* |
| 5 |
* @package BitForms |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration; |
| 9 |
|
| 10 |
/** |
| 11 |
* Provides details of available integration and helps to |
| 12 |
* execute available integrations |
| 13 |
*/ |
| 14 |
|
| 15 |
use WP_Error; |
| 16 |
use FilesystemIterator; |
| 17 |
|
| 18 |
final class Integrations |
| 19 |
{ |
| 20 |
public $integrations = array(); |
| 21 |
|
| 22 |
/** |
| 23 |
* Undocumented function |
| 24 |
* |
| 25 |
* @return all |
| 26 |
*/ |
| 27 |
public function getAllIntegrations() |
| 28 |
{ |
| 29 |
return $this->allIntegrations(); |
| 30 |
} |
| 31 |
/** |
| 32 |
* Undocumented function |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
protected function allIntegrations() |
| 37 |
{ |
| 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 |
* Checks a Integration Exists or not |
| 54 |
* |
| 55 |
* @param String $name Name of Integration |
| 56 |
* @return boolean |
| 57 |
*/ |
| 58 |
protected static function isExists($name) |
| 59 |
{ |
| 60 |
if (file_exists(__DIR__ . '/' . $name) && file_exists(__DIR__ . '/' . $name . '/' . $name . 'Handler.php')) { |
| 61 |
return __NAMESPACE__ . "\\{$name}\\{$name}Handler"; |
| 62 |
} else if (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 |
* This function helps to get single intgration information |
| 70 |
* |
| 71 |
* @return bool setIntegration() |
| 72 |
*/ |
| 73 |
public function getIntegration($integartionBaseName) |
| 74 |
{ |
| 75 |
return $this->setIntegration($integartionBaseName); |
| 76 |
} |
| 77 |
|
| 78 |
public function registerAjax() |
| 79 |
{ |
| 80 |
$dirs = new FilesystemIterator(__DIR__); |
| 81 |
foreach ($dirs as $dirInfo) { |
| 82 |
if ($dirInfo->isDir()) { |
| 83 |
$integartionBaseName = basename($dirInfo); |
| 84 |
if ( |
| 85 |
file_exists(__DIR__ . '/' . $integartionBaseName) |
| 86 |
&& file_exists(__DIR__ . '/' . $integartionBaseName . '/' . $integartionBaseName . 'Handler.php') |
| 87 |
) { |
| 88 |
$integration = __NAMESPACE__ . "\\{$integartionBaseName}\\{$integartionBaseName}Handler"; |
| 89 |
if (method_exists($integration, 'registerAjax')) { |
| 90 |
$integration::registerAjax(); |
| 91 |
// (new $integration(0, 0))->registerAjax(); |
| 92 |
} |
| 93 |
/* $handler = new $integration($integrationID, $formID); |
| 94 |
$handler->execute($integrationHandler, $integrationDetails, $fieldValues); */ |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* This function helps to execute Integration |
| 102 |
* |
| 103 |
* @param Array $integrations List of integrstion to execute. |
| 104 |
* Element will be json string like {"id":1} |
| 105 |
* @param Array $fieldValues Values of submitted fields |
| 106 |
* @param Integer $formID ID of current form |
| 107 |
* |
| 108 |
* @return void Nothing to return |
| 109 |
*/ |
| 110 |
public static function executeIntegrations($integrations, $fieldValues, $formID, $entryID = 0, $logID) |
| 111 |
{ |
| 112 |
$integrationHandler = new IntegrationHandler($formID); |
| 113 |
if (is_array($integrations)) { |
| 114 |
foreach ($integrations as $integrationIDStr) { |
| 115 |
$integrationID = intval(json_decode($integrationIDStr)->id, 10); |
| 116 |
if (!empty($integrationID) && is_int($integrationID)) { |
| 117 |
$integrationResult |
| 118 |
= $integrationHandler->getAIntegration($integrationID); |
| 119 |
$integrationDetails = is_wp_error($integrationResult) ? null : $integrationResult[0]; |
| 120 |
$integrationName = is_null($integrationDetails) ? null : ucfirst(str_replace(' ', null, $integrationDetails->integration_type)); |
| 121 |
if (!is_null($integrationName) && static::isExists($integrationName)) { |
| 122 |
$integration = static::isExists($integrationName); |
| 123 |
$handler = new $integration($integrationID, $formID); |
| 124 |
$handler->execute($integrationHandler, $integrationDetails, $fieldValues, $entryID, $logID); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
public static function integrationExecutionHelper($integrations, $fieldValue, $formID, $entryID, $logID) |
| 132 |
{ |
| 133 |
if (empty($integrations) || empty($formID)) { |
| 134 |
return; |
| 135 |
} |
| 136 |
foreach ($integrations as $key => $value) { |
| 137 |
self::executeIntegrations($value, $fieldValue, $formID, $entryID, $logID); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|