var form = " . json_encode( $form ) . ';';
} else {
echo "";
}
?>
array(
'name' => 'standard_fields',
'label' => __( 'Standard Fields', 'gravityforms' ),
'tooltip_class' => 'tooltip_bottomleft',
'fields' => array(),
'fields' => array(
array( 'data-type' => 'text', 'value' => GFCommon::get_field_type_title( 'text' ) ),
array( 'data-type' => 'textarea', 'value' => GFCommon::get_field_type_title( 'textarea' ) ),
array( 'data-type' => 'select', 'value' => GFCommon::get_field_type_title( 'select' ) ),
array( 'data-type' => 'number', 'value' => GFCommon::get_field_type_title( 'number' ) ),
array( 'data-type' => 'checkbox', 'value' => GFCommon::get_field_type_title( 'checkbox' ) ),
array( 'data-type' => 'radio', 'value' => GFCommon::get_field_type_title( 'radio' ) ),
array( 'data-type' => 'hidden', 'value' => GFCommon::get_field_type_title( 'hidden' ) ),
array( 'data-type' => 'html', 'value' => GFCommon::get_field_type_title( 'html' ) ),
array( 'data-type' => 'section', 'value' => GFCommon::get_field_type_title( 'section' ) ),
array( 'data-type' => 'page', 'value' => GFCommon::get_field_type_title( 'page' ) ),
),
),
'advanced_fields' => array(
'name' => 'advanced_fields',
'label' => __( 'Advanced Fields', 'gravityforms' ),
'fields' => array(
array( 'data-type' => 'name', 'value' => GFCommon::get_field_type_title( 'name' ) ),
array( 'data-type' => 'date', 'value' => GFCommon::get_field_type_title( 'date' ) ),
array( 'data-type' => 'time', 'value' => GFCommon::get_field_type_title( 'time' ) ),
array( 'data-type' => 'phone', 'value' => GFCommon::get_field_type_title( 'phone' ) ),
array( 'data-type' => 'address', 'value' => GFCommon::get_field_type_title( 'address' ) ),
array( 'data-type' => 'website', 'value' => GFCommon::get_field_type_title( 'website' ) ),
array( 'data-type' => 'email', 'value' => GFCommon::get_field_type_title( 'email' ) ),
array( 'data-type' => 'fileupload', 'value' => GFCommon::get_field_type_title( 'fileupload' ) ),
array( 'data-type' => 'captcha', 'value' => GFCommon::get_field_type_title( 'captcha' ) ),
array( 'data-type' => 'list', 'value' => GFCommon::get_field_type_title( 'list' ) ),
array( 'data-type' => 'multiselect', 'value' => GFCommon::get_field_type_title( 'multiselect' ) ),
),
),
'post_fields' => array(
'name' => 'post_fields',
'label' => __( 'Post Fields', 'gravityforms' ),
'fields' => array(
array( 'data-type' => 'post_title', 'value' => GFCommon::get_field_type_title( 'post_title' ) ),
array( 'data-type' => 'post_content', 'value' => GFCommon::get_field_type_title( 'post_content' ) ),
array( 'data-type' => 'post_excerpt', 'value' => GFCommon::get_field_type_title( 'post_excerpt' ) ),
array( 'data-type' => 'post_tags', 'value' => GFCommon::get_field_type_title( 'post_tags' ) ),
array( 'data-type' => 'post_category', 'value' => GFCommon::get_field_type_title( 'post_category' ) ),
array( 'data-type' => 'post_image', 'value' => GFCommon::get_field_type_title( 'post_image' ) ),
array( 'data-type' => 'post_custom_field', 'value' => GFCommon::get_field_type_title( 'post_custom_field' ) ),
),
),
'pricing_fields' => array(
'name' => 'pricing_fields',
'label' => __( 'Pricing Fields', 'gravityforms' ),
'fields' => array(
array( 'data-type' => 'product', 'value' => GFCommon::get_field_type_title( 'product' ) ),
array( 'data-type' => 'quantity', 'value' => GFCommon::get_field_type_title( 'quantity' ) ),
array( 'data-type' => 'option', 'value' => GFCommon::get_field_type_title( 'option' ) ),
array( 'data-type' => 'shipping', 'value' => GFCommon::get_field_type_title( 'shipping' ) ),
array( 'data-type' => 'total', 'value' => GFCommon::get_field_type_title( 'total' ) ),
),
),
);
// If enabled insert the password field between the email and fileupload fields.
if ( apply_filters( 'gform_enable_password_field', false ) ) {
$password = array(
'data-type' => 'password',
'value' => GFCommon::get_field_type_title( 'password' )
);
array_splice( $field_groups['advanced_fields']['fields'], 7, 0, array( $password ) );
}
// Add credit card field, if enabled.
if ( apply_filters( 'gform_enable_credit_card_field', false ) ) {
$field_groups['pricing_fields']['fields'][] = array(
'data-type' => 'creditcard',
'value' => GFCommon::get_field_type_title( 'creditcard' )
);
}
/**
* Modify the field groups before fields are added.
*
* @since 2.2.6
*
* @param array $field_groups The field groups, including group name, label and fields.
*/
$field_groups = apply_filters( 'gform_field_groups_form_editor', $field_groups );
// Remove array keys from field groups array.
$field_groups = array_values( $field_groups );
// Add buttons to fields.
foreach ( GF_Fields::get_all() as $gf_field ) {
$field_groups = $gf_field->add_button( $field_groups );
}
/**
* Add/edit/remove "Add Field" buttons from the form editor's floating toolbox.
*
* @param array $field_groups The field groups, including group name, label and fields.
*/
return apply_filters( 'gform_add_field_buttons', $field_groups );
}
public static function color_picker( $field_name, $callback ) {
?>
>
rgar( $button, 'data-icon' ) ) ); ?>
0 );
if ( ! empty( $_POST['search'] ) )
$args['search'] = $_POST['search'];
$categories = get_categories( $args );
}
if ( ! $categories )
return false;
$children = self::_get_term_hierarchy( 'category' );
$start = ( $page - 1 ) * $per_page;
$end = $start + $per_page;
$i = - 1;
foreach ( $categories as $category ) {
if ( $count >= $end )
break;
$i ++;
if ( $category->parent != $parent )
continue;
// If the page starts in a subtree, print the parents.
if ( $count == $start && $category->parent > 0 ) {
$my_parents = array();
while ( $my_parent ) {
$my_parent = get_category( $my_parent );
$my_parents[] = $my_parent;
if ( ! $my_parent->parent )
break;
$my_parent = $my_parent->parent;
}
$num_parents = count( $my_parents );
while ( $my_parent = array_pop( $my_parents ) ) {
self::_cat_row( $my_parent, $level - $num_parents, $output );
$num_parents --;
}
}
if ( $count >= $start )
self::_cat_row( $category, $level, $output );
//unset($categories[ $i ]); // Prune the working set
$count ++;
if ( isset( $children[ $category->term_id ] ) )
self::_cat_rows( $categories, $count, $output, $category->term_id, $level + 1, $page, $per_page );
}
}
private static function _cat_row( $category, $level, &$output, $name_override = false ) {
static $row_class = '';
$cat = get_category( $category, OBJECT, 'display' );
$default_cat_id = (int) get_option( 'default_category' );
$pad = str_repeat( '— ', $level );
$name = ( $name_override ? $name_override : $pad . ' ' . $cat->name );
$cat->count = number_format_i18n( $cat->count );
$output .= "
$name
";
}
private static function _get_term_hierarchy( $taxonomy ) {
if ( ! is_taxonomy_hierarchical( $taxonomy ) )
return array();
$children = get_option( "{$taxonomy}_children" );
if ( is_array( $children ) )
return $children;
$children = array();
$terms = get_terms( $taxonomy, 'get=all' );
foreach ( $terms as $term ) {
if ( $term->parent > 0 )
$children[ $term->parent ][] = $term->term_id;
}
update_option( "{$taxonomy}_children", $children );
return $children;
}
private static function insert_variable_prepopulate( $element_id, $callback = '' ) {
?>
(mm/dd/yyyy)
(dd/mm/yyyy)
sanitize_settings();
$index = rgpost( 'index' );
if ( $index != 'undefined' ) {
$index = absint( $index );
}
require_once( GFCommon::get_base_path() . '/form_display.php' );
$form_id = absint( rgpost( 'form_id' ) );
$form = GFFormsModel::get_form_meta( $form_id );
$field_html = GFFormDisplay::get_field( $field, '', true, $form );
$field_html_json = json_encode( $field_html );
$field_json = json_encode( $field );
die( "EndAddField($field_json, " . $field_html_json . ", $index);" );
}
public static function duplicate_field() {
check_ajax_referer( 'rg_duplicate_field', 'rg_duplicate_field' );
$source_field_id = absint( rgpost( 'source_field_id' ) );
$field_json = stripslashes_deep( $_POST['field'] );
$field_properties = GFCommon::json_decode( $field_json, true );
$field = GF_Fields::create( $field_properties );
$form_id = absint( rgpost( 'form_id' ) );
$form = GFFormsModel::get_form_meta( $form_id );
require_once( GFCommon::get_base_path() . '/form_display.php' );
$field_html = GFFormDisplay::get_field( $field, '', true, $form );
$args['field'] = $field;
$args['sourceFieldId'] = $source_field_id;
$args['fieldString'] = $field_html;
$args_json = json_encode( $args );
die( $args_json );
}
public static function change_input_type() {
check_ajax_referer( 'rg_change_input_type', 'rg_change_input_type' );
$field_json = stripslashes_deep( $_POST['field'] );
$field_properties = GFCommon::json_decode( $field_json, true );
$field = GF_Fields::create( $field_properties );
$id = absint( $field->id );
$type = $field->inputType;
$form_id = absint( rgpost( 'form_id' ) );
$form = GFFormsModel::get_form_meta( $form_id );
require_once( GFCommon::get_base_path() . '/form_display.php' );
$field_content = GFFormDisplay::get_field_content( $field, '', true, $form_id, $form );
$args['id'] = $id;
$args['type'] = $type;
$args['fieldString'] = $field_content;
$args_json = json_encode( $args );
die( "EndChangeInputType($args_json);" );
}
public static function refresh_field_preview() {
check_ajax_referer( 'rg_refresh_field_preview', 'rg_refresh_field_preview' );
$field_json = stripslashes_deep( $_POST['field'] );
$field_properties = GFCommon::json_decode( $field_json, true );
$field = GF_Fields::create( $field_properties );
$field->sanitize_settings();
$form_id = absint( $_POST['formId'] );
$form = GFFormsModel::get_form_meta( $form_id );
$form = GFFormsModel::maybe_sanitize_form_settings( $form );
require_once( GFCommon::get_base_path() . '/form_display.php' );
$field_content = GFFormDisplay::get_field_content( $field, '', true, $form_id, $form );
$args['fieldString'] = $field_content;
$args['fieldId'] = absint( $field->id );
$args_json = json_encode( $args );
die( $args_json );
}
public static function delete_custom_choice() {
check_ajax_referer( 'gf_delete_custom_choice', 'gf_delete_custom_choice' );
RGFormsModel::delete_custom_choice( rgpost( 'name' ) );
exit();
}
public static function save_custom_choice() {
check_ajax_referer( 'gf_save_custom_choice', 'gf_save_custom_choice' );
RGFormsModel::save_custom_choice( rgpost( 'previous_name' ), rgpost( 'new_name' ), GFCommon::json_decode( rgpost( 'choices' ) ) );
exit();
}
/**
* Saves form meta. Note the special requirements for the meta string.
*
* @param int $id
* @param string $form_json A valid JSON string. The JSON is manipulated before decoding and is designed to work together with jQuery.toJSON() rather than json_encode. Avoid using json_encode as it will convert unicode characters into their respective entities with slashes. These slashes get stripped so unicode characters won't survive intact.
*
* @return array
*/
public static function save_form_info( $id, $form_json ) {
global $wpdb;
// Clean up form meta JSON.
$form_json = stripslashes( $form_json );
$form_json = nl2br( $form_json );
GFCommon::log_debug( 'GFFormDetail::save_form_info(): Form meta json: ' . $form_json );
// Convert form meta JSON to array.
$form_meta = json_decode( $form_json, true );
$form_meta = GFFormsModel::convert_field_objects( $form_meta );
// Set version of Gravity Forms form was created with.
if ( $id === 0 ) {
$form_meta['version'] = GFForms::$version;
}
// Sanitize form settings.
$form_meta = GFFormsModel::maybe_sanitize_form_settings( $form_meta );
// Extract deleted field IDs.
$deleted_fields = rgar( $form_meta, 'deletedFields' );
unset( $form_meta['deletedFields'] );
GFCommon::log_debug( 'GFFormDetail::save_form_info(): Form meta => ' . print_r( $form_meta, true ) );
// If form meta is not found, exit.
if ( ! $form_meta ) {
return array( 'status' => 'invalid_json', 'meta' => null );
}
// Get form table name.
$form_table_name = GFFormsModel::get_form_table_name();
// Get all forms.
$forms = GFFormsModel::get_forms();
// Loop through forms.
foreach ( $forms as $form ) {
// If form has a duplicate title, exit.
if ( strtolower( $form->title ) == strtolower( $form_meta['title'] ) && rgar( $form_meta, 'id' ) != $form->id ) {
return array( 'status' => 'duplicate_title', 'meta' => $form_meta );
}
}
// If an ID exists, update existing form.
if ( $id > 0 ) {
// Trim form meta values.
$form_meta = GFFormsModel::trim_form_meta_values( $form_meta );
// Delete fields.
if ( ! empty( $deleted_fields ) ) {
// Loop through fields.
foreach ( $deleted_fields as $deleted_field ) {
// Delete field.
$form_meta = GFFormsModel::delete_field( $form_meta, $deleted_field, false );
}
}
// Save form meta.
GFFormsModel::update_form_meta( $id, $form_meta );
// Update form title.
GFAPI::update_form_property( $id, 'title', $form_meta['title'] );
// Get form meta.
$form_meta = RGFormsModel::get_form_meta( $id );
if ( ! empty( $deleted_fields ) ) {
// Remove logic/routing rules based on deleted fields from confirmations and notifications.
foreach ( $deleted_fields as $deleted_field ) {
$form_meta = GFFormsModel::delete_field_from_confirmations( $form_meta, $deleted_field );
$form_meta = GFFormsModel::delete_field_from_notifications( $form_meta, $deleted_field );
}
}
/**
* Fires after a form is saved
*
* Used to run additional actions after the form is saved
*
* @since 2.4.6.1 Added the $deleted_fields param.
* @since unknown
*
* @param array $form_meta The form meta
* @param bool false Returns false if the form ID already exists.
* @param array $deleted_fields The IDs of any fields which have been deleted.
*/
do_action( 'gform_after_save_form', $form_meta, false, $deleted_fields );
return array( 'status' => $id, 'meta' => $form_meta );
} else {
//inserting form
$id = RGFormsModel::insert_form( $form_meta['title'] );
//updating object's id property
$form_meta['id'] = $id;
//creating default notification
if ( apply_filters( 'gform_default_notification', true ) ) {
$default_notification = array(
'id' => uniqid(),
'isActive' => true,
'to' => '{admin_email}',
'name' => __( 'Admin Notification', 'gravityforms' ),
'event' => 'form_submission',
'toType' => 'email',
'subject' => __( 'New submission from', 'gravityforms' ) . ' {form_title}',
'message' => '{all_fields}',
);
$notifications = array( $default_notification['id'] => $default_notification );
//updating notifications form meta
RGFormsModel::save_form_notifications( $id, $notifications );
}
// Add default confirmation when saving a new form.
$confirmation = GFFormsModel::get_default_confirmation();
$confirmations = array( $confirmation['id'] => $confirmation );
GFFormsModel::save_form_confirmations( $id, $confirmations );
// Adding markup version. Increment this when we make breaking changes to form markup.
$form_meta['markupVersion'] = 2;
//updating form meta
GFFormsModel::update_form_meta( $id, $form_meta );
// Get form meta.
$form_meta = GFFormsModel::get_form_meta( $id );
/**
* Fires after a form is saved
*
* Used to run additional actions after the form is saved
*
* @since 2.4.6.1 Added the $deleted_fields param.
* @since unknown
*
* @param array $form_meta The form meta
* @param bool true Returns true if this is a new form.
* @param array $deleted_fields The IDs of any fields which have been deleted.
*/
do_action( 'gform_after_save_form', $form_meta, true, array() );
return array( 'status' => $id * - 1, 'meta' => $form_meta );
}
}
public static function save_form() {
check_ajax_referer( 'rg_save_form', 'rg_save_form' );
$id = absint( $_POST['id'] );
$form_json = absint( $_POST['form'] );
$result = self::save_form_info( $id, $form_json );
switch ( rgar( $result, 'status' ) ) {
case 'invalid_json' :
die( 'EndUpdateForm(0);' );
break;
case 'duplicate_title' :
die( 'DuplicateTitleMessage();' );
break;
default :
$form_id = absint( $result['status'] );
if ( $form_id < 0 ) {
die( 'EndInsertForm(' . $form_id . ');' );
} else {
die( "EndUpdateForm({$form_id});" );
}
break;
}
}
public static function get_post_category_values() {
$has_input_name = strtolower( rgpost( 'inputName' ) ) !== 'false';
$id = ! $has_input_name ? rgpost( 'objectType' ) . '_rule_value_' . rgpost( 'ruleIndex' ) : rgpost( 'inputName' );
$selected = rgempty( 'selectedValue' ) ? 0 : rgpost( 'selectedValue' );
$dropdown = wp_dropdown_categories( array( 'class' => 'gfield_rule_select gfield_rule_value_dropdown gfield_category_dropdown', 'orderby' => 'name', 'id' => $id, 'name' => $id, 'selected' => $selected, 'hierarchical' => true, 'hide_empty' => 0, 'echo' => false ) );
die( $dropdown );
}
public static function inline_scripts( $echo = true ) {
$script_str = '';
$conditional_logic_fields = array();
$field_settings = array();
foreach ( GF_Fields::get_all() as $gf_field ) {
$settings_arr = $gf_field->get_form_editor_field_settings();
if ( ! is_array( $settings_arr ) || empty( $settings_arr ) ) {
continue;
}
$settings = join( ', .', $settings_arr );
$settings = '.' . $settings;
$field_settings[ $gf_field->type ] = $settings;
if ( $gf_field->is_conditional_logic_supported() ) {
$conditional_logic_fields[] = $gf_field->type;
}
$field_script = $gf_field->get_form_editor_inline_script_on_page_render();
if ( ! empty( $field_script ) ){
$script_str .= $field_script . PHP_EOL;
}
}
$script_str .= sprintf( 'fieldSettings = %s;', json_encode( $field_settings ) ) . PHP_EOL;
$script_str .= sprintf( 'function GetConditionalLogicFields(){return %s;}', json_encode( $conditional_logic_fields ) ) . PHP_EOL;
if ( ! empty( $script_str ) ) {
$script_str = sprintf( '', $script_str );
if ( $echo ) {
echo $script_str;
}
}
return $script_str;
}
/**
* Adds the form ID to the beginning of the list of recently opened forms and stores the array for the current user.
*
* @since 2.0
* @param $form_id
*/
public static function update_recent_forms( $form_id ) {
GFFormsModel::update_recent_forms( $form_id );
}
/**
* Display a notice at the top of the form editor.
*
* In the future, this can be expanded to show other messages.
*
* @since 2.5
*
* @param array $form
*/
public static function editor_notices( $form ) {
if ( GFFormDetail::need_deprecated_class_message( $form ) ) {
?>