table('fluentform_form_meta') ->where('form_id', $formId) ->whereIn('meta_key', [ '_custom_form_css', '_custom_form_js', '_ff_form_styler_css' ]) ->limit(3) ->get(); if (!$metas) { return; } foreach ($metas as $meta) { if ($meta->meta_key == '_custom_form_css' && $meta->value) { $css = $meta->value; $css = str_replace('{form_id}', $formId, $css); $css = str_replace('FF_ID', $formId, $css); $this->addCss($formId, $css, 'fluentform_custom_css_'.$formId ); } else if(($meta->meta_key == '_ff_form_styler_css' && $meta->value)) { $css = $meta->value; $this->addCss($formId, $css, 'fluentform_styler_css_'.$formId ); } else if ($meta->meta_key == '_custom_form_js' && $meta->value) { $this->addJs($formId, $meta->value); } } } public function addCss($formId, $css, $cssId = 'fluentform_custom_css') { if ($css) { if (!did_action('wp_head')) { add_action('wp_head', function () use ($css, $formId, $cssId) { ?> $this->getData($formId, '_custom_form_css'), 'custom_js' => $this->getData($formId, '_custom_form_js'), ), 200); } /** * Save settings for a particular form by id * @return void */ public function saveSettingsAjax() { $formId = absint($_REQUEST['form_id']); $css = $_REQUEST['custom_css']; $js = $_REQUEST['custom_js']; $css = wp_strip_all_tags(wp_unslash($css)); $js = wp_unslash($js); $this->store($formId, '_custom_form_css', $css); $this->store($formId, '_custom_form_js', $js); wp_send_json_success([ 'message' => __('Custom CSS and JS successfully updated', 'fluentform') ], 200); } protected function getData($formId, $metaKey) { $row = wpFluent()->table('fluentform_form_meta') ->where('form_id', $formId) ->where('meta_key', $metaKey) ->first(); if ($row) { return $row->value; } return ''; } protected function store($formId, $metaKey, $metaValue) { $row = wpFluent()->table('fluentform_form_meta') ->where('form_id', $formId) ->where('meta_key', $metaKey) ->first(); if (!$row) { return wpFluent()->table('fluentform_form_meta') ->insert([ 'form_id' => $formId, 'meta_key' => $metaKey, 'value' => $metaValue ]); } return wpFluent()->table('fluentform_form_meta') ->where('id', $row->id) ->update([ 'value' => $metaValue ]); } }