| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\WPAsync; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\App\Modules\Form\FormDataParser; |
| 7 |
use FluentForm\App\Modules\Form\FormFieldsParser; |
| 8 |
use FluentForm\Framework\Foundation\Application; |
| 9 |
|
| 10 |
class FluentFormAsyncRequest |
| 11 |
{ |
| 12 |
/** |
| 13 |
* $prefix The prefix for the identifier |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
protected $table = 'ff_scheduled_actions'; |
| 17 |
|
| 18 |
/** |
| 19 |
* $action The action for the identifier |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $action = 'fluentform_background_process'; |
| 23 |
|
| 24 |
/** |
| 25 |
* $actions Actions to be fired when an async request is sent |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
protected $actions = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* $app Instance of Application/Framework |
| 32 |
* @var FluentForm\Framework\Foundation\Application |
| 33 |
*/ |
| 34 |
protected $app = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Construct the Object |
| 38 |
* @param FluentForm\Framework\Foundation\Application $app |
| 39 |
*/ |
| 40 |
public function __construct(Application $app) |
| 41 |
{ |
| 42 |
$this->app = $app; |
| 43 |
} |
| 44 |
|
| 45 |
public function queueFeeds($feeds) |
| 46 |
{ |
| 47 |
return wpFluent()->table($this->table) |
| 48 |
->insert($feeds); |
| 49 |
} |
| 50 |
|
| 51 |
public function dispatchAjax($data = []) |
| 52 |
{ |
| 53 |
$args = array( |
| 54 |
'timeout' => 0.1, |
| 55 |
'blocking' => false, |
| 56 |
'body' => $data, |
| 57 |
'cookies' => $_COOKIE, |
| 58 |
'sslverify' => apply_filters('fluentform_https_local_ssl_verify', false), |
| 59 |
); |
| 60 |
|
| 61 |
$queryArgs = array( |
| 62 |
'action' => $this->action, |
| 63 |
'nonce' => wp_create_nonce($this->action), |
| 64 |
); |
| 65 |
|
| 66 |
$url = add_query_arg($queryArgs, admin_url( 'admin-ajax.php' )); |
| 67 |
wp_remote_post(esc_url_raw($url), $args); |
| 68 |
} |
| 69 |
|
| 70 |
public function handleBackgroundCall() |
| 71 |
{ |
| 72 |
$originId = false; |
| 73 |
if(isset($_REQUEST['origin_id'])) { |
| 74 |
$originId = intval($_REQUEST['origin_id']); |
| 75 |
} |
| 76 |
|
| 77 |
$this->processActions($originId); |
| 78 |
echo 'success'; |
| 79 |
die(); |
| 80 |
} |
| 81 |
|
| 82 |
public function processActions($originId = false) |
| 83 |
{ |
| 84 |
$actionFeedQuery = wpFluent()->table($this->table) |
| 85 |
->where('status', 'pending'); |
| 86 |
if($originId) { |
| 87 |
$actionFeedQuery = $actionFeedQuery->where('origin_id', $originId); |
| 88 |
} |
| 89 |
|
| 90 |
$actionFeeds = $actionFeedQuery->get(); |
| 91 |
|
| 92 |
if(!$actionFeeds) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
$formCache = []; |
| 97 |
$submissionCache = []; |
| 98 |
$entryCache = []; |
| 99 |
|
| 100 |
foreach ($actionFeeds as $actionFeed) { |
| 101 |
$action = $actionFeed->action; |
| 102 |
$feed = maybe_unserialize($actionFeed->data); |
| 103 |
$feed['scheduled_action_id'] = $actionFeed->id; |
| 104 |
if(isset($submissionCache[$actionFeed->origin_id])) { |
| 105 |
$submission = $submissionCache[$actionFeed->origin_id]; |
| 106 |
} else { |
| 107 |
$submission = wpFluent()->table('fluentform_submissions')->find($actionFeed->origin_id); |
| 108 |
$submissionCache[$submission->id] = $submission; |
| 109 |
} |
| 110 |
if(isset($formCache[$submission->form_id])) { |
| 111 |
$form = $formCache[$submission->form_id]; |
| 112 |
} else { |
| 113 |
$form = wpFluent()->table('fluentform_forms')->find($submission->form_id); |
| 114 |
$formCache[$form->id] = $form; |
| 115 |
} |
| 116 |
|
| 117 |
if(isset($entryCache[$submission->id])) { |
| 118 |
$entry = $entryCache[$submission->id]; |
| 119 |
} else { |
| 120 |
$entry = $this->getEntry($submission, $form); |
| 121 |
$entryCache[$submission->id] = $entry; |
| 122 |
} |
| 123 |
$formData = json_decode($submission->response, true); |
| 124 |
|
| 125 |
wpFluent()->table($this->table) |
| 126 |
->where('id', $actionFeed->id) |
| 127 |
->update([ |
| 128 |
'status' => 'processing', |
| 129 |
'retry_count' => $actionFeed->retry_count + 1, |
| 130 |
'updated_at' => current_time('mysql') |
| 131 |
]); |
| 132 |
|
| 133 |
do_action($action, $feed, $formData, $entry, $form); |
| 134 |
} |
| 135 |
|
| 136 |
if($originId && !empty($form) && !empty($submission)) { |
| 137 |
do_action('fluentform_global_notify_completed', $submission->id, $form); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
private function getEntry($submission, $form) |
| 142 |
{ |
| 143 |
$formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'raw']); |
| 144 |
return FormDataParser::parseFormEntry($submission, $form, $formInputs); |
| 145 |
} |
| 146 |
} |