post_type && ! get_post_meta( $post->ID, 'form_key', true ) ) {
$form_key = 'form_' . uniqid();
update_post_meta( $post->ID, 'form_key', $form_key );
}
}
/**
* Stops new form keys from being saved to a form post if a key already exists.
* Some plugins that duplicate posts will cause trouble as forms will end up with multiple form keys.
*
* @since 1.9.0
*
*/
function should_add_form_key_meta( $check, $object_id, $meta_key ) {
if ( 'form_key' !== $meta_key ) {
return $check;
}
// If a form key already exists, we don't want to save another one
if ( metadata_exists( 'post', $object_id, $meta_key ) ) {
return false;
}
return $check;
}
/**
* Displays the form key after the title
*
* @since 1.0.0
*
*/
function display_form_key() {
global $post;
if ( 'af_form' == $post->post_type && $form_key = get_post_meta( $post->ID, 'form_key', true ) ) {
echo '
';
}
}
/**
* Display the form shortcode in the form settings.
*
* @since 1.6.4
*
*/
function display_form_shortcode( $field ) {
global $post;
if ( $post && $key = get_post_meta( $post->ID, 'form_key', true ) ) {
$shortcode = sprintf( '[advanced_form form="%s"]', $key );
$field['message'] = sprintf(
''
. '%s'
. ''
. '',
esc_html( $shortcode ),
esc_attr__( 'Copied!', 'advanced-forms' ),
esc_html__( 'Copy', 'advanced-forms' )
);
}
return $field;
}
/**
* Adds a meta box to the form edit page used to display the form fields
*
* @since 1.0.0
*
*/
function add_fields_meta_box() {
add_meta_box( 'af_form_fields', __( 'Fields', 'advanced-forms' ), array( $this, 'fields_meta_box_callback' ), 'af_form', 'normal', 'default', null );
}
/**
* Callback for the fields meta box
* Displays all fields registered to the current form
*
* @since 1.0.0
*
*/
function fields_meta_box_callback() {
global $post;
$form = af_get_form( $post->ID );
// Get field groups for the current form
$field_groups = af_get_form_field_groups( $form['key'] );
?>
__( 'Key', 'advanced-forms' ),
'fields' => __( 'Fields', 'advanced-forms' ),
'entries' => __( 'Entries', 'advanced-forms' ),
);
// Remove date column
unset( $columns['date'] );
return array_merge( array_splice( $columns, 0, 2 ), $new_columns, $columns );
}
/**
* Outputs the content for the custom columns
*
* @since 1.0.0
*
*/
function custom_columns_content( $column, $post_id ) {
$form = af_get_form( $post_id );
if ( 'key' == $column ) {
echo get_post_meta( $post_id, 'form_key', true );
} elseif ( 'fields' == $column ) {
$count = 0;
$field_groups = af_get_form_field_groups( $form['key'] );
// Count the number of fields in all field groups
foreach ( $field_groups as $field_group ) {
$fields = acf_get_fields( $field_group );
$count += count( $fields );
}
echo $count;
} elseif ( 'entries' == $column ) {
$entries = af_get_entries( $form['key'] );
echo sprintf( '%s', admin_url() . '/edit.php?post_type=af_entry&entry_form=' . $form['key'], count( $entries ) );
}
}
/**
* Add an "Insert field" button to WYSIWYG fields
*
* @since 1.0.0
*
*/
function add_wysiwyg_content_field_inserter( $id ) {
global $post;
if ( ! $post ) {
return;
}
$form = af_form_from_post( $post );
if ( ! $form ) {
return;
}
if ( 'acf-editor' == substr($id, 0, 10) ) {
_af_field_inserter_button( $form, 'all', false );
}
}
/**
* Outputs the sidebar on the forms list page
* Moved up by Javascript
*
* @since 1.5.0
*
*/
function add_forms_sidebar() {
$title = AF()->pro ? 'Advanced Forms Pro' : 'Advanced Forms';
$doc_url = 'https://advancedforms.github.io';
$pro_url = 'https://hookturn.io/downloads/advanced-forms';
$icon = '';
?>
post_type ) {
return;
}
echo '