| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form\Settings; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
|
| 7 |
class FormCssJs |
| 8 |
{ |
| 9 |
public function addCssJs($formId) |
| 10 |
{ |
| 11 |
// @todo: Limit 3 sometimes make things double |
| 12 |
$metas = wpFluent()->table('fluentform_form_meta') |
| 13 |
->where('form_id', $formId) |
| 14 |
->whereIn('meta_key', [ |
| 15 |
'_custom_form_css', |
| 16 |
'_custom_form_js', |
| 17 |
'_ff_form_styler_css' |
| 18 |
]) |
| 19 |
->groupBy('meta_key') |
| 20 |
//->limit(3) |
| 21 |
->get(); |
| 22 |
|
| 23 |
if (!$metas) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
foreach ($metas as $meta) { |
| 29 |
if ($meta->meta_key == '_custom_form_css' && $meta->value) { |
| 30 |
$css = $meta->value; |
| 31 |
$css = str_replace('{form_id}', $formId, $css); |
| 32 |
$css = str_replace('FF_ID', $formId, $css); |
| 33 |
$this->addCss($formId, $css, 'fluentform_custom_css_'.$formId ); |
| 34 |
} else if(($meta->meta_key == '_ff_form_styler_css' && $meta->value)) { |
| 35 |
$css = $meta->value; |
| 36 |
$this->addCss($formId, $css, 'fluentform_styler_css_'.$formId ); |
| 37 |
} else if ($meta->meta_key == '_custom_form_js' && $meta->value) { |
| 38 |
$this->addJs($formId, $meta->value); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public function getCss($formId) |
| 44 |
{ |
| 45 |
$cssMeta = wpFluent()->table('fluentform_form_meta') |
| 46 |
->where('form_id', $formId) |
| 47 |
->where('meta_key', '_custom_form_css') |
| 48 |
->first(); |
| 49 |
|
| 50 |
if(!$cssMeta || !$cssMeta->value) { |
| 51 |
return ''; |
| 52 |
} |
| 53 |
|
| 54 |
$css = $cssMeta->value; |
| 55 |
$css = str_replace('{form_id}', $formId, $css); |
| 56 |
$css = str_replace('FF_ID', $formId, $css); |
| 57 |
return $this->escCss($css); |
| 58 |
} |
| 59 |
|
| 60 |
public function getJs($formId) |
| 61 |
{ |
| 62 |
$jsMeta = wpFluent()->table('fluentform_form_meta') |
| 63 |
->where('form_id', $formId) |
| 64 |
->where('meta_key', '_custom_form_js') |
| 65 |
->first(); |
| 66 |
|
| 67 |
if(!$jsMeta || !$jsMeta->value) { |
| 68 |
return ''; |
| 69 |
} |
| 70 |
|
| 71 |
return $jsMeta->value; |
| 72 |
} |
| 73 |
|
| 74 |
public function addCss($formId, $css, $cssId = 'fluentform_custom_css') |
| 75 |
{ |
| 76 |
if ($css) { |
| 77 |
if (!did_action('wp_head')) { |
| 78 |
add_action('wp_head', function () use ($css, $formId, $cssId) { |
| 79 |
?> |
| 80 |
|
| 81 |
<style id="<?php echo esc_attr($cssId); ?>" type="text/css"> |
| 82 |
<?php echo $this->escCss($css); ?> |
| 83 |
</style> |
| 84 |
<?php |
| 85 |
}, 10); |
| 86 |
} else { |
| 87 |
?> |
| 88 |
|
| 89 |
<style id="<?php echo esc_attr($cssId); ?>" type="text/css"> |
| 90 |
<?php echo $this->escCss($css); ?> |
| 91 |
</style> |
| 92 |
<?php |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public function addJs($formId, $customJS) |
| 98 |
{ |
| 99 |
if (trim($customJS)) { |
| 100 |
add_action('wp_footer', function () use ($formId, $customJS) { |
| 101 |
?> |
| 102 |
<script type="text/javascript"> |
| 103 |
jQuery(document.body).on('fluentform_init_<?php echo esc_attr($formId); ?>', function (event, data) { |
| 104 |
var $form = jQuery(data[0]); |
| 105 |
var formId = "<?php echo esc_attr($formId); ?>"; |
| 106 |
var $ = jQuery; |
| 107 |
try { |
| 108 |
<?php fluentFormPrintUnescapedInternalString($customJS); ?> |
| 109 |
} catch (e) { |
| 110 |
console.warn('Error in custom JS of Fluentform ID: ' + $form.data('form_id')); |
| 111 |
console.error(e); |
| 112 |
} |
| 113 |
}); |
| 114 |
</script> |
| 115 |
<?php |
| 116 |
}, 100); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get settings for a particular form by id |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function getSettingsAjax() |
| 125 |
{ |
| 126 |
$formId = absint($_REQUEST['form_id']); |
| 127 |
wp_send_json_success(array( |
| 128 |
'custom_css' => $this->getData($formId, '_custom_form_css'), |
| 129 |
'custom_js' => $this->getData($formId, '_custom_form_js'), |
| 130 |
), 200); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Save settings for a particular form by id |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
public function saveSettingsAjax() |
| 138 |
{ |
| 139 |
$formId = absint($_REQUEST['form_id']); |
| 140 |
|
| 141 |
$css = wp_strip_all_tags(wp_unslash($_REQUEST['custom_css'])); |
| 142 |
$js = wp_unslash($_REQUEST['custom_js']); |
| 143 |
|
| 144 |
if (preg_match('#</?\w+#', $css)) { |
| 145 |
$css = ''; |
| 146 |
} |
| 147 |
|
| 148 |
$this->store($formId, '_custom_form_css', $css); |
| 149 |
$this->store($formId, '_custom_form_js', $js); |
| 150 |
|
| 151 |
wp_send_json_success([ |
| 152 |
'message' => __('Custom CSS and JS successfully updated', 'fluentform') |
| 153 |
], 200); |
| 154 |
} |
| 155 |
|
| 156 |
protected function escCss($css) |
| 157 |
{ |
| 158 |
return preg_match('#</?\w+#', $css) ? '' : $css; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
protected function getData($formId, $metaKey) |
| 163 |
{ |
| 164 |
$row = wpFluent()->table('fluentform_form_meta') |
| 165 |
->where('form_id', $formId) |
| 166 |
->where('meta_key', $metaKey) |
| 167 |
->first(); |
| 168 |
if ($row) { |
| 169 |
return $row->value; |
| 170 |
} |
| 171 |
return ''; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
protected function store($formId, $metaKey, $metaValue) |
| 176 |
{ |
| 177 |
$row = wpFluent()->table('fluentform_form_meta') |
| 178 |
->where('form_id', $formId) |
| 179 |
->where('meta_key', $metaKey) |
| 180 |
->first(); |
| 181 |
|
| 182 |
if (!$row) { |
| 183 |
return wpFluent()->table('fluentform_form_meta') |
| 184 |
->insert([ |
| 185 |
'form_id' => $formId, |
| 186 |
'meta_key' => $metaKey, |
| 187 |
'value' => $metaValue |
| 188 |
]); |
| 189 |
} |
| 190 |
|
| 191 |
return wpFluent()->table('fluentform_form_meta') |
| 192 |
->where('id', $row->id) |
| 193 |
->update([ |
| 194 |
'value' => $metaValue |
| 195 |
]); |
| 196 |
} |
| 197 |
} |
| 198 |
|