'Inline Title Edit',
'toggle' => 'Toggle Layout',
'copy' => 'Copy/paste Layout',
'lock' => 'Lock Layouts',
'close' => 'Close Button',
);
if(acf_version_compare(acf_get_setting('version'), '<', '5.9')){
$choices['clone'] = 'Clone';
}
acf_render_field_setting($field, array(
'label' => __('Additional Actions'),
'name' => 'acfe_flexible_add_actions',
'key' => 'acfe_flexible_add_actions',
'instructions' => __('Additional actions'),
'type' => 'checkbox',
'default_value' => '',
'layout' => 'horizontal',
'choices' => $choices,
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_advanced',
'operator' => '==',
'value' => '1',
),
)
)
));
}
function validate_actions($field){
$actions = acf_get_array($field['acfe_flexible_add_actions']);
// acfe_flexible_title_edition
if(acf_maybe_get($field, 'acfe_flexible_title_edition')){
if(!in_array('title', $actions)) $actions[] = 'title';
acfe_unset($field, 'acfe_flexible_title_edition');
}
// acfe_flexible_toggle
if(acf_maybe_get($field, 'acfe_flexible_toggle')){
if(!in_array('toggle', $actions)) $actions[] = 'toggle';
acfe_unset($field, 'acfe_flexible_toggle');
}
// acfe_flexible_copy_paste
if(acf_maybe_get($field, 'acfe_flexible_copy_paste')){
if(!in_array('copy', $actions)) $actions[] = 'copy';
acfe_unset($field, 'acfe_flexible_copy_paste');
}
// acfe_flexible_lock
if(acf_maybe_get($field, 'acfe_flexible_lock')){
if(!in_array('lock', $actions)) $actions[] = 'lock';
acfe_unset($field, 'acfe_flexible_lock');
}
// acfe_flexible_close_button
if(acf_maybe_get($field, 'acfe_flexible_close_button')){
if(!in_array('close', $actions)) $actions[] = 'close';
acfe_unset($field, 'acfe_flexible_close_button');
}
// acfe_flexible_clone
if(acf_maybe_get($field, 'acfe_flexible_clone')){
if(!in_array('clone', $actions)) $actions[] = 'clone';
acfe_unset($field, 'acfe_flexible_clone');
}
$field['acfe_flexible_add_actions'] = $actions;
return $field;
}
function wrapper_attributes($wrapper, $field){
$actions = $field['acfe_flexible_add_actions'];
// Title
if(in_array('title', $actions))
$wrapper['data-acfe-flexible-title-edition'] = 1;
// Toggle
if(in_array('toggle', $actions))
$wrapper['data-acfe-flexible-toggle'] = 1;
// Copy
if(in_array('copy', $actions))
$wrapper['data-acfe-flexible-copy-paste'] = 1;
// Lock
$lock = in_array('lock', $actions);
$lock = apply_filters("acfe/flexible/lock", $lock, $field);
$lock = apply_filters("acfe/flexible/lock/name={$field['_name']}", $lock, $field);
$lock = apply_filters("acfe/flexible/lock/key={$field['key']}", $lock, $field);
if($lock)
$wrapper['data-acfe-flexible-lock'] = 1;
// Clone
if(in_array('close', $actions))
$wrapper['data-acfe-flexible-close-button'] = 1;
return $wrapper;
}
function load_fields($fields, $field){
// Actions
$actions = $field['acfe_flexible_add_actions'];
foreach($field['layouts'] as $i => $layout){
// Title
if(in_array('title', $actions)){
// Vars
$key = "field_{$layout['key']}_title";
$name = 'acfe_flexible_layout_title';
$label = $layout['label'];
// Add local
acf_add_local_field(array(
'label' => false,
'key' => $key,
'name' => $name,
'type' => 'text',
'required' => false,
'maxlength' => false,
'default_value' => $label,
'placeholder' => $label,
'parent_layout' => $layout['key'],
'parent' => $field['key']
));
// Add sub field
array_unshift($fields, acf_get_field($key));
}
// Toggle
if(in_array('toggle', $actions)){
// Vars
$key = "field_{$layout['key']}_toggle";
$name = 'acfe_flexible_toggle';
// Add local
acf_add_local_field(array(
'label' => false,
'key' => $key,
'name' => $name,
'type' => 'acfe_hidden',
'required' => false,
'default_value' => false,
'parent_layout' => $layout['key'],
'parent' => $field['key']
));
// Add sub field
array_unshift($fields, acf_get_field($key));
}
}
return $fields;
}
function prepare_layout($layout, $field, $i, $value, $prefix){
if(empty($layout['sub_fields']))
return $layout;
// Actions
$actions = $field['acfe_flexible_add_actions'];
// Title
if(in_array('title', $actions)){
$sub_field = acfe_extract_sub_field($layout, 'acfe_flexible_layout_title', $value);
if($sub_field){
// update prefix to allow for nested values
$sub_field['prefix'] = $prefix;
$sub_field['class'] = 'acfe-flexible-control-title';
$sub_field['data-acfe-flexible-control-title-input'] = 1;
$sub_field = acf_validate_field($sub_field);
$sub_field = acf_prepare_field($sub_field);
$input_attrs = array();
foreach(array('type', 'id', 'class', 'name', 'value', 'placeholder', 'maxlength', 'pattern', 'readonly', 'disabled', 'required', 'data-acfe-flexible-control-title-input') as $k){
if(isset($sub_field[$k])){
$input_attrs[$k] = $sub_field[$k];
}
}
// render input
echo acf_get_text_input(acf_filter_attrs($input_attrs));
}
}
// Toggle
if(in_array('toggle', $actions)){
$sub_field = acfe_extract_sub_field($layout, 'acfe_flexible_toggle', $value);
if($sub_field){
// update prefix to allow for nested values
$sub_field['prefix'] = $prefix;
$sub_field['class'] = 'acfe-flexible-layout-toggle';
$sub_field = acf_validate_field($sub_field);
$sub_field = acf_prepare_field($sub_field);
$input_attrs = array();
foreach(array('type', 'id', 'class', 'name', 'value') as $k){
if(isset($sub_field[$k])){
$input_attrs[$k] = $sub_field[$k];
}
}
// render input
echo acf_get_hidden_input(acf_filter_attrs($input_attrs));
}
}
return $layout;
}
function layout_icons($icons, $layout, $field){
$actions = $field['acfe_flexible_add_actions'];
// Toggle
if(in_array('toggle', $actions)){
$icons = array_merge(array(
'toggle' => ''
), $icons);
}
// Copy
if(in_array('copy', $actions)){
$icons = array_merge(array(
'copy' => ''
), $icons);
}
// Clone
if(in_array('clone', $actions) && acf_version_compare(acf_get_setting('version'), '<', '5.9')){
$icons = array_merge($icons, array(
'clone' => ''
));
}
return $icons;
}
function secondary_actions($actions, $field){
if(!in_array('copy', $field['acfe_flexible_add_actions']))
return $actions;
$actions['copy'] = '' . __('Copy layouts', 'acfe') . '';
$actions['paste'] = '' . __('Paste layouts', 'acfe') . '';
return $actions;
}
function layout_title($title, $field, $layout, $i){
if(!in_array('title', $field['acfe_flexible_add_actions']))
return $title;
// Get Layout Title
$value = get_sub_field('acfe_flexible_layout_title');
if(!empty($value))
$title = wp_unslash($value);
return '' . $title . '';
}
function load_value($value, $post_id, $field){
// Bail early if admin
if(is_admin() && !wp_doing_ajax())
return $value;
// Bail early if preview
if(acf_maybe_get_POST('action') === 'acfe/flexible/layout_preview')
return $value;
if(empty($field['layouts']))
return $value;
if(!in_array('toggle', $field['acfe_flexible_add_actions']))
return $value;
$models = array();
foreach($field['layouts'] as $layout_key => $layout){
$models[$layout['name']] = array(
'key' => $layout['key'],
'name' => $layout['name'],
'toggle' => "field_{$layout['key']}_toggle"
);
}
$value = acf_get_array($value);
foreach($value as $k => $layout){
if(!isset($models[$layout['acf_fc_layout']]))
continue;
if(!acf_maybe_get($layout, $models[$layout['acf_fc_layout']]['toggle']))
continue;
unset($value[$k]);
}
return $value;
}
}
acf_new_instance('acfe_field_flexible_content_actions');
endif;