8 add_action(
'wp_ajax_gv_field_options', array( $this,
'get_field_options' ) );
11 add_action(
'wp_ajax_gv_available_fields', array( $this,
'get_available_fields_html' ) );
14 add_action(
'wp_ajax_gv_get_active_areas', array( $this,
'get_active_areas' ) );
17 add_action(
'wp_ajax_gv_get_preset_fields', array( $this,
'get_preset_fields_config' ) );
20 add_action(
'wp_ajax_gv_set_preset_form', array( $this,
'create_preset_form' ) );
22 add_action(
'wp_ajax_gv_sortable_fields_form', array( $this,
'get_sortable_fields' ) );
33 private function _exit( $mixed = NULL ) {
39 if( defined(
'DOING_GRAVITYVIEW_TESTS' ) && DOING_GRAVITYVIEW_TESTS ) {
53 if ( ! isset( $_POST[
'nonce'] ) || ! wp_verify_nonce( $_POST[
'nonce'],
'gravityview_ajaxviews' ) ) {
54 $this->
_exit(
false );
68 $context = rgpost(
'context' );
73 esc_attr( $context ) =>
'' 83 if ( is_array( rgpost(
'form_preset_ids' ) ) ) {
84 $form_ids = rgpost(
'form_preset_ids' );
86 $this->
_exit(
false );
90 foreach ( $data as $context => $markup ) {
93 do_action(
'gravityview_render_field_pickers', $context, $form_ids );
95 $data[ $context ] = trim( ob_get_clean() );
98 if ( defined(
'DOING_GRAVITYVIEW_TESTS' ) && DOING_GRAVITYVIEW_TESTS ) {
102 wp_send_json_success( $data );
114 if( empty( $_POST[
'template_id'] ) ) {
115 $this->
_exit(
false );
119 do_action(
'gravityview_render_directory_active_areas', \
GV\Utils::_POST(
'template_id'),
'directory',
'',
true, \
GV\Utils::_POST(
'form_id', 0 ) );
120 $response[
'directory'] = ob_get_clean();
123 do_action(
'gravityview_render_directory_active_areas', \
GV\Utils::_POST(
'template_id'),
'single',
'',
true, \
GV\Utils::_POST(
'form_id', 0 ) );
124 $response[
'single'] = ob_get_clean();
126 $response = array_map(
'gravityview_strip_whitespace', $response );
128 $this->
_exit( json_encode( $response ) );
139 if( empty( $_POST[
'template_id'] ) ) {
140 $this->
_exit(
false );
144 $preset_fields_path = apply_filters(
'gravityview_template_fieldsxml', array(), $_POST[
'template_id'] );
146 if( !empty( $preset_fields_path ) ) {
149 $presets = array(
'widgets' => array(),
'fields' => array() );
152 $template_id = esc_attr( $_POST[
'template_id'] );
155 $template_areas_directory = apply_filters(
'gravityview_template_active_areas', array(), $template_id,
'directory' );
156 $template_areas_single = apply_filters(
'gravityview_template_active_areas', array(), $template_id,
'single' );
162 do_action(
'gravityview_render_active_areas', $template_id,
'widget',
'header', $default_widget_areas, $presets[
'widgets'] );
163 $response[
'header'] = ob_get_clean();
166 do_action(
'gravityview_render_active_areas', $template_id,
'widget',
'footer', $default_widget_areas, $presets[
'widgets'] );
167 $response[
'footer'] = ob_get_clean();
170 do_action(
'gravityview_render_active_areas', $template_id,
'field',
'directory', $template_areas_directory, $presets[
'fields'] );
171 $response[
'directory'] = ob_get_clean();
174 do_action(
'gravityview_render_active_areas', $template_id,
'field',
'single', $template_areas_single, $presets[
'fields'] );
175 $response[
'single'] = ob_get_clean();
177 $response = array_map(
'gravityview_strip_whitespace', $response );
179 gravityview()->log->debug(
'[get_preset_fields_config] AJAX Response', array(
'data' => $response ) );
181 $this->
_exit( json_encode( $response ) );
193 if( empty( $_POST[
'template_id'] ) ) {
194 gravityview()->log->error(
'Cannot create preset form; the template_id is empty.' );
195 $this->
_exit(
false );
199 $preset_form_xml_path = apply_filters(
'gravityview_template_formxml',
'', $_POST[
'template_id'] );
205 if(
false ===
$form ) {
207 gravityview()->log->error(
'Error importing form for template id: {template_id}', array(
'template_id' => (
int) $_POST[
'template_id'] ) );
209 $this->
_exit(
false );
212 $this->
_exit(
'<option value="'.esc_attr(
$form[
'id'] ).
'" selected="selected">'.esc_html(
$form[
'title'] ).
'</option>' );
224 gravityview()->log->debug(
'[import_form] Import Preset Form. (File) {path}', array(
'path' => $xml_or_json_path ) );
226 if( empty( $xml_or_json_path ) || !class_exists(
'GFExport') || !file_exists( $xml_or_json_path ) ) {
227 gravityview()->log->error(
'Class GFExport or file not found. file: {path}', array(
'path' => $xml_or_json_path ) );
233 $count = GFExport::import_file( $xml_or_json_path,
$forms );
235 gravityview()->log->debug(
'[import_form] Importing form (Result) {count}', array(
'count' => $count ) );
236 gravityview()->log->debug(
'[import_form] Importing form (Form) ', array(
'data' =>
$forms ) );
238 if( $count != 1 || empty(
$forms[0][
'id'] ) ) {
239 gravityview()->log->error(
'Form Import Failed!' );
257 if( empty( $_POST[
'template'] ) || empty( $_POST[
'area'] ) || empty( $_POST[
'field_id'] ) || empty( $_POST[
'field_type'] ) ) {
258 gravityview()->log->error(
'Required fields were not set in the $_POST request. ' );
259 $this->
_exit(
false );
263 $_post = array_map(
'stripslashes_deep', $_POST );
266 $_post = array_map(
'esc_attr', $_post );
269 $input_type = isset($_post[
'input_type']) ? esc_attr( $_post[
'input_type'] ) : NULL;
270 $context = isset($_post[
'context']) ? esc_attr( $_post[
'context'] ) : NULL;
272 $form_id = empty( $_post[
'form_id'] ) ? null : $_post[
'form_id'];
277 $this->
_exit( $response );
293 if( !empty( $_POST[
'form_id'] ) ) {
295 $form = (int) $_POST[
'form_id'];
299 elseif( !empty( $_POST[
'template_id'] ) ) {
309 $this->
_exit( $response );
320 if( empty( $template_id ) ) {
321 gravityview()->log->error(
'Template ID not set.' );
324 $form_file = apply_filters(
'gravityview_template_formxml',
'', $template_id );
325 if( !file_exists( $form_file ) ) {
326 gravityview()->log->error(
'[{template_id}] form file does not exist: {path}.', array(
'template_id' => $template_id,
'path' => $form_file ) );
332 $forms_json = file_get_contents( $form_file );
334 $forms = json_decode( $forms_json,
true );
337 gravityview()->log->error(
'Could not read the {path} template file.', array(
'path' => $form_file ) );
342 $form = GFFormsModel::convert_field_objects(
$forms[0] );
343 $form = GFFormsModel::sanitize_settings(
$form );
345 gravityview()->log->debug(
'[pre_get_form_fields] Importing Form Fields for preset [{template_id}]. (Form)', array(
'template_id' => $template_id,
'data' =>
$form ) );
357 if( empty( $file ) || !file_exists( $file ) ) {
358 gravityview()->log->error(
'Importing Preset Fields. File not found. (File) {path}', array(
'path' => $file ) );
362 if( !class_exists(
'WXR_Parser') ) {
366 $parser =
new WXR_Parser();
367 $presets = $parser->parse( $file );
369 if(is_wp_error( $presets )) {
370 gravityview()->log->error(
'Importing Preset Fields failed. Threw WP_Error.', array(
'data' => $presets ) );
374 if( empty( $presets[
'posts'][0][
'postmeta'] ) && !is_array( $presets[
'posts'][0][
'postmeta'] ) ) {
375 gravityview()->log->error(
'Importing Preset Fields failed. Meta not found in file. {path}', array(
'path' => $file ) );
379 gravityview()->log->debug(
'[import_fields] postmeta', array(
'data' => $presets[
'posts'][0][
'postmeta'] ) );
381 $fields = $widgets = array();
382 foreach( $presets[
'posts'][0][
'postmeta'] as $meta ) {
383 switch ($meta[
'key']) {
384 case '_gravityview_directory_fields':
385 $fields = maybe_unserialize( $meta[
'value'] );
387 case '_gravityview_directory_widgets':
388 $widgets = maybe_unserialize( $meta[
'value'] );
393 gravityview()->log->debug(
'[import_fields] Imported Preset (Fields)', array(
'data' => $fields ) );
394 gravityview()->log->debug(
'[import_fields] Imported Preset (Widgets)', array(
'data' => $widgets ) );
398 'widgets' => $widgets
const GRAVITYVIEW_DIR
"GRAVITYVIEW_DIR" "./" The absolute path to the plugin directory, with trailing slash ...
import_form( $xml_or_json_path='')
Import Gravity Form XML or JSON.
static render_field_options( $form_id, $field_type, $template_id, $field_id, $field_label, $area, $input_type=NULL, $uniqid='', $current='', $context='single', $item=array())
Render Field Options html (shown through a dialog box)
if(! function_exists( 'gravityview_sanitize_html_class')) gravityview_strip_whitespace( $string)
Replace multiple newlines, tabs, and spaces with a single space.
import_fields( $file)
Import fields configuration from an exported WordPress View preset.
_exit( $mixed=NULL)
Handle exiting the script (for unit testing)
create_preset_form()
Create the preset form requested before the View save.
if(gravityview() ->plugin->is_GF_25()) $form
get_sortable_fields()
Given a View id, calculates the assigned form, and returns the form fields (only the sortable ones ) ...
get_field_options()
Returns field options - called by ajax when dropping fields into active areas AJAX callback...
static pre_get_form_fields( $template_id='')
Get the form fields for a preset (no form created yet)
get_available_fields_html()
AJAX action to get HTML markup for form(s) or template fields AJAX callback.
gravityview_get_sortable_fields( $formid, $current='')
Render dropdown (select) with the list of sortable fields from a form ID.
check_ajax_nonce()
--—— AJAX ----——
if(empty( $created_by)) $form_id
gravityview()
The main GravityView wrapper function.
get_active_areas()
Returns template active areas given a template ID AJAX callback.
get_preset_fields_config()
Fill in active areas with preset configuration according to the template selected.