| 1 |
<?php |
| 2 |
|
| 3 |
namespace VisualComposer\Modules\Vendors\Plugins; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
header('Status: 403 Forbidden'); |
| 7 |
header('HTTP/1.1 403 Forbidden'); |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
use VisualComposer\Framework\Container; |
| 12 |
use VisualComposer\Framework\Illuminate\Support\Module; |
| 13 |
use VisualComposer\Helpers\Request; |
| 14 |
use VisualComposer\Helpers\Traits\EventsFilters; |
| 15 |
use VisualComposer\Helpers\Traits\WpFiltersActions; |
| 16 |
|
| 17 |
/** |
| 18 |
* Backward compatibility with "Ninja Forms" wordPress plugin. |
| 19 |
* |
| 20 |
* @see https://wordpress.org/plugins/ninja-forms/ |
| 21 |
*/ |
| 22 |
class NinjaFormsController extends Container implements Module |
| 23 |
{ |
| 24 |
use WpFiltersActions; |
| 25 |
use EventsFilters; |
| 26 |
|
| 27 |
protected static $ninjaCount; |
| 28 |
|
| 29 |
public function __construct() |
| 30 |
{ |
| 31 |
$this->wpAddAction('plugins_loaded', 'initialize', 16); |
| 32 |
} |
| 33 |
|
| 34 |
protected function initialize(Request $requestHelper) |
| 35 |
{ |
| 36 |
if (class_exists('Ninja_Forms') && $requestHelper->isAjax()) { |
| 37 |
/** @see \VisualComposer\Modules\Vendors\Plugins\NinjaFormsController::replaceIds */ |
| 38 |
$this->addFilter( |
| 39 |
'vcv:ajax:elements:ajaxShortcode:adminNonce', |
| 40 |
'replaceIds', |
| 41 |
2 |
| 42 |
); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
protected function replaceIds($response) |
| 47 |
{ |
| 48 |
if (!vcIsBadResponse($response)) { |
| 49 |
if (is_null(self::$ninjaCount)) { |
| 50 |
self::$ninjaCount = 1; |
| 51 |
} else { |
| 52 |
self::$ninjaCount++; |
| 53 |
} |
| 54 |
$patterns = [ |
| 55 |
'(nf-form-)(\d+)(-cont)', |
| 56 |
'(nf-form-title-)(\d+)()', |
| 57 |
'(nf-form-errors-)(\d+)()', |
| 58 |
'(form.id\s*=\s*\')(\d+)(\')', |
| 59 |
]; |
| 60 |
$time = time() . self::$ninjaCount . wp_rand(100, 999); |
| 61 |
foreach ($patterns as $pattern) { |
| 62 |
$response = preg_replace('/' . $pattern . '/', '${1}' . $time . '${3}', $response); |
| 63 |
} |
| 64 |
$replaceTo = <<<JS |
| 65 |
if (typeof nfForms !== 'undefined') { |
| 66 |
nfForms = nfForms.filter( function(item, index) { |
| 67 |
if (item && item.id) { |
| 68 |
return document.querySelector('#nf-form-' + item.id + '-cont') |
| 69 |
} |
| 70 |
return false |
| 71 |
}) |
| 72 |
} |
| 73 |
JS; |
| 74 |
|
| 75 |
$response = str_replace('var nfForms', $replaceTo . ';var nfForms', $response); |
| 76 |
} |
| 77 |
|
| 78 |
return $response; |
| 79 |
} |
| 80 |
} |
| 81 |
|