__('Stylised button'),
'name' => 'acfe_flexible_stylised_button',
'key' => 'acfe_flexible_stylised_button',
'instructions' => __('Better layouts button integration'),
'type' => 'true_false',
'message' => '',
'default_value' => false,
'ui' => true,
'ui_on_text' => '',
'ui_off_text' => '',
), true);
// Hide Empty Message
acf_render_field_setting($field, array(
'label' => __('Hide Empty Message'),
'name' => 'acfe_flexible_hide_empty_message',
'key' => 'acfe_flexible_hide_empty_message',
'instructions' => __('Hide the empty message box'),
'type' => 'true_false',
'message' => '',
'default_value' => false,
'ui' => true,
'ui_on_text' => '',
'ui_off_text' => '',
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_stylised_button',
'operator' => '!=',
'value' => '1',
),
)
)
), true);
// Empty Message
acf_render_field_setting($field, array(
'label' => __('Empty Message'),
'name' => 'acfe_flexible_empty_message',
'key' => 'acfe_flexible_empty_message',
'instructions' => __('Text displayed when the flexible field is empty'),
'type' => 'text',
'placeholder' => __('Click the "Add Row" button below to start creating your layout'),
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_stylised_button',
'operator' => '!=',
'value' => '1',
),
array(
'field' => 'acfe_flexible_hide_empty_message',
'operator' => '!=',
'value' => '1',
)
)
)
), true);
// Layouts thumbnails
acf_render_field_setting($field, array(
'label' => __('Layouts Thumbnails'),
'name' => 'acfe_flexible_layouts_thumbnails',
'key' => 'acfe_flexible_layouts_thumbnails',
'instructions' => __('Set a thumbnail for each layouts. You must save the field group to apply this setting'),
'type' => 'true_false',
'message' => '',
'default_value' => false,
'ui' => true,
'ui_on_text' => '',
'ui_off_text' => '',
), true);
// Layouts render
acf_render_field_setting($field, array(
'label' => __('Layouts Render'),
'name' => 'acfe_flexible_layouts_templates',
'key' => 'acfe_flexible_layouts_templates',
'instructions' => __('Set template, style & javascript files for each layouts.. You must save the field group to apply this setting'),
'type' => 'true_false',
'message' => '',
'default_value' => false,
'ui' => true,
'ui_on_text' => '',
'ui_off_text' => '',
), true);
// Layouts preview
acf_render_field_setting($field, array(
'label' => __('Layouts Dynamic Preview'),
'name' => 'acfe_flexible_layouts_previews',
'key' => 'acfe_flexible_layouts_previews',
'instructions' => __('Use layouts render settings to display a dynamic preview in the post administration'),
'type' => 'true_false',
'message' => '',
'default_value' => false,
'ui' => true,
'ui_on_text' => '',
'ui_off_text' => '',
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_layouts_templates',
'operator' => '==',
'value' => '1',
)
)
)
), true);
// Modal: Edition
acf_render_field_setting($field, array(
'label' => __('Modal: Edition'),
'name' => 'acfe_flexible_modal_edition',
'key' => 'acfe_flexible_modal_edition',
'instructions' => __('Edit layout content in a modal'),
'type' => 'true_false',
'message' => '',
'default_value' => false,
'ui' => true,
'ui_on_text' => '',
'ui_off_text' => '',
), true);
// Modal: Selection
acf_render_field_setting($field, array(
'label' => __('Modal: Selection'),
'name' => 'acfe_flexible_modal',
'key' => 'acfe_flexible_modal',
'instructions' => __('Select layouts in a modal'),
'type' => 'group',
'layout' => 'block',
'sub_fields' => array(
array(
'label' => '',
'name' => 'acfe_flexible_modal_enabled',
'key' => 'acfe_flexible_modal_enabled',
'type' => 'true_false',
'instructions' => '',
'required' => false,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => false,
'ui' => true,
'ui_on_text' => '',
'ui_off_text' => '',
'conditional_logic' => false,
),
array(
'label' => '',
'name' => 'acfe_flexible_modal_title',
'key' => 'acfe_flexible_modal_title',
'type' => 'text',
'prepend' => __('Modal Title'),
'placeholder' => 'Add Row',
'instructions' => false,
'required' => false,
'wrapper' => array(
'width' => '35',
'class' => '',
'id' => '',
),
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_modal_enabled',
'operator' => '==',
'value' => '1',
)
)
)
),
array(
'label' => '',
'name' => 'acfe_flexible_modal_col',
'key' => 'acfe_flexible_modal_col',
'type' => 'select',
'prepend' => '',
'instructions' => false,
'required' => false,
'choices' => array(
'1' => '1 column',
'2' => '2 columns',
'3' => '3 columns',
'4' => '4 columns',
'5' => '5 columns',
'6' => '6 columns',
),
'default_value' => '4',
'wrapper' => array(
'width' => '15',
'class' => '',
'id' => '',
),
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_modal_enabled',
'operator' => '==',
'value' => '1',
)
)
)
),
array(
'label' => '',
'name' => 'acfe_flexible_modal_categories',
'key' => 'acfe_flexible_modal_categories',
'type' => 'true_false',
'message' => __('Categories'),
'instructions' => false,
'required' => false,
'wrapper' => array(
'width' => '25',
'class' => '',
'id' => '',
),
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_modal_enabled',
'operator' => '==',
'value' => '1',
)
)
)
),
)
), true);
// Layouts State
acf_render_field_setting($field, array(
'label' => __('Layouts State'),
'name' => 'acfe_flexible_layouts_state',
'key' => 'acfe_flexible_layouts_state',
'instructions' => __('Force layouts to be collapsed or opened'),
'type' => 'select',
'allow_null' => true,
'choices' => array(
'collapse' => 'Collapsed',
'open' => 'Opened',
),
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_modal_edition',
'operator' => '!=',
'value' => '1',
)
)
)
), true);
}
add_action('acf/render_field', 'acfe_flexible_layouts_settings_before', 0);
function acfe_flexible_layouts_settings_before($field){
if($field['_name'] != 'label' || stripos($field['name'], 'layout_') === false)
return;
echo '';
acf_render_field_wrap(array(
'label' => __('Settings'),
'type' => 'hidden',
'name' => 'acfe_flexible_settings_label'
), 'ul');
echo '
';
}
add_action('acf/render_field', 'acfe_flexible_layouts_settings', 10);
function acfe_flexible_layouts_settings($field){
if($field['_name'] != 'max' || stripos($field['name'], 'layout_') === false)
return;
$layout_prefix = $field['prefix'];
parse_str($layout_prefix, $output);
$keys = acfe_array_keys_r($output);
$_field_id = $keys[1];
$_layout_key = $keys[3];
$field_flexible = acf_get_field($_field_id);
$layout = $field_flexible['layouts'][$_layout_key];
$is_flexible_layouts_thumbnails = isset($field_flexible['acfe_flexible_layouts_thumbnails']) && !empty($field_flexible['acfe_flexible_layouts_thumbnails']);
$is_flexible_layouts_templates = isset($field_flexible['acfe_flexible_layouts_templates']) && !empty($field_flexible['acfe_flexible_layouts_templates']);
$is_flexible_modal_enabled = isset($field_flexible['acfe_flexible_modal']['acfe_flexible_modal_enabled']) && !empty($field_flexible['acfe_flexible_modal']['acfe_flexible_modal_enabled']);
$is_flexible_modal_categories = isset($field_flexible['acfe_flexible_modal']['acfe_flexible_modal_categories']) && !empty($field_flexible['acfe_flexible_modal']['acfe_flexible_modal_categories']);
// Category
if($is_flexible_modal_enabled && $is_flexible_modal_categories){
$acfe_flexible_category = isset($layout['acfe_flexible_category']) ? $layout['acfe_flexible_category'] : '';
acf_render_field_wrap(array(
'prepend' => __('Category'),
'name' => 'acfe_flexible_category',
'type' => 'text',
'class' => 'acf-fc-meta-name',
'prefix' => $layout_prefix,
'value' => $acfe_flexible_category,
/*
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_modal_enabled',
'operator' => '==',
'value' => '1',
),
array(
'field' => 'acfe_flexible_modal_categories',
'operator' => '==',
'value' => '1',
)
)
)
*/
), 'ul');
}
// Template
if($is_flexible_layouts_templates){
$acfe_flexible_render_template = isset($layout['acfe_flexible_render_template']) ? $layout['acfe_flexible_render_template'] : '';
acf_render_field_wrap(array(
'label' => __('Render'),
'prepend' => str_replace(home_url(), '', ACFE_THEME_URL) . '/',
'name' => 'acfe_flexible_render_template',
'type' => 'text',
'class' => 'acf-fc-meta-name',
'prefix' => $layout_prefix,
'value' => $acfe_flexible_render_template,
'placeholder' => 'template.php',
/*
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_layouts_templates',
'operator' => '==',
'value' => '1',
)
)
)
*/
), 'ul');
$acfe_flexible_render_style = isset($layout['acfe_flexible_render_style']) ? $layout['acfe_flexible_render_style'] : '';
acf_render_field_wrap(array(
'prepend' => str_replace(home_url(), '', ACFE_THEME_URL) . '/',
'name' => 'acfe_flexible_render_style',
'type' => 'text',
'class' => 'acf-fc-meta-name',
'prefix' => $layout_prefix,
'value' => $acfe_flexible_render_style,
'placeholder' => 'style.css',
/*
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_layouts_templates',
'operator' => '==',
'value' => '1',
)
)
)
*/
), 'ul');
$acfe_flexible_render_script = isset($layout['acfe_flexible_render_script']) ? $layout['acfe_flexible_render_script'] : '';
acf_render_field_wrap(array(
'prepend' => str_replace(home_url(), '', ACFE_THEME_URL) . '/',
'name' => 'acfe_flexible_render_script',
'type' => 'text',
'class' => 'acf-fc-meta-name',
'prefix' => $layout_prefix,
'value' => $acfe_flexible_render_script,
'placeholder' => 'script.js',
/*
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_layouts_templates',
'operator' => '==',
'value' => '1',
)
)
)
*/
), 'ul');
}
// Thumbnail
if($is_flexible_layouts_thumbnails){
$acfe_flexible_thumbnail = isset($layout['acfe_flexible_thumbnail']) ? $layout['acfe_flexible_thumbnail'] : '';
acf_render_field_wrap(array(
'label' => __('Thumbnail'),
'name' => 'acfe_flexible_thumbnail',
'type' => 'image',
'class' => '',
'prefix' => $layout_prefix,
'value' => $acfe_flexible_thumbnail,
'return_format' => 'array',
'preview_size' => 'thumbnail',
'library' => 'all',
/*
'conditional_logic' => array(
array(
array(
'field' => 'acfe_flexible_layouts_thumbnails',
'operator' => '==',
'value' => '1',
)
)
)
*/
), 'ul');
}
}
add_filter('acf/field_wrapper_attributes', 'acfe_flexible_wrapper', 10, 2);
function acfe_flexible_wrapper($wrapper, $field){
if($field['type'] != 'flexible_content')
return $wrapper;
// Stylised button
if(isset($field['acfe_flexible_stylised_button']) && !empty($field['acfe_flexible_stylised_button'])){
$wrapper['data-acfe-flexible-stylised-button'] = 1;
}
// Hide Empty Message
if(isset($field['acfe_flexible_hide_empty_message']) && !empty($field['acfe_flexible_hide_empty_message']) || isset($field['acfe_flexible_stylised_button']) && !empty($field['acfe_flexible_stylised_button'])){
$wrapper['data-acfe-flexible-hide-empty-message'] = 1;
}
// Modal: Edition
if(isset($field['acfe_flexible_modal_edition']) && !empty($field['acfe_flexible_modal_edition'])){
$wrapper['data-acfe-flexible-modal-edition'] = 1;
}
// Modal: Selection
if(isset($field['acfe_flexible_modal']['acfe_flexible_modal_enabled']) && !empty($field['acfe_flexible_modal']['acfe_flexible_modal_enabled'])){
$wrapper['data-acfe-flexible-modal'] = 1;
// Columns
if(isset($field['acfe_flexible_modal']['acfe_flexible_modal_col']) && !empty($field['acfe_flexible_modal']['acfe_flexible_modal_col']))
$wrapper['data-acfe-flexible-modal-col'] = $field['acfe_flexible_modal']['acfe_flexible_modal_col'];
// Title
if(isset($field['acfe_flexible_modal']['acfe_flexible_modal_title']) && !empty($field['acfe_flexible_modal']['acfe_flexible_modal_title']))
$wrapper['data-acfe-flexible-modal-title'] = $field['acfe_flexible_modal']['acfe_flexible_modal_title'];
}
// Layouts State
if(isset($field['acfe_flexible_layouts_state']) && !empty($field['acfe_flexible_layouts_state'])){
// Collapse
if($field['acfe_flexible_layouts_state'] == 'collapse'){
$wrapper['data-acfe-flexible-collapse'] = 1;
}
// Open
elseif($field['acfe_flexible_layouts_state'] == 'open'){
$wrapper['data-acfe-flexible-open'] = 1;
}
}
$thumbnails = array();
// Layouts Previews
if(isset($field['acfe_flexible_layouts_templates']) && !empty($field['acfe_flexible_layouts_templates']) && isset($field['acfe_flexible_layouts_previews']) && !empty($field['acfe_flexible_layouts_previews'])){
$wrapper['data-acfe-flexible-preview'] = 1;
}
return $wrapper;
}
add_filter('acf/fields/flexible_content/no_value_message', 'acfe_flexible_empty_message', 10, 2);
function acfe_flexible_empty_message($message, $field){
if(!isset($field['acfe_flexible_empty_message']) || empty($field['acfe_flexible_empty_message']))
return $message;
return $field['acfe_flexible_empty_message'];
}
add_filter('acf/prepare_field/type=flexible_content', 'acfe_flexible_layout_title');
function acfe_flexible_layout_title($field){
if(empty($field['layouts']))
return $field;
foreach($field['layouts'] as $k => &$layout){
$thumbnail = false;
if(isset($field['acfe_flexible_layouts_thumbnails']) && !empty($field['acfe_flexible_layouts_thumbnails'])){
$class = $style = array();
$class[] = 'acfe-flexible-layout-thumbnail';
// Modal disabled
if(!isset($field['acfe_flexible_modal']['acfe_flexible_modal_enabled']) || empty($field['acfe_flexible_modal']['acfe_flexible_modal_enabled']))
$class[] = 'acfe-flexible-layout-thumbnail-no-modal';
// Thumbnail is set
$thumbnail_found = false;
if(isset($layout['acfe_flexible_thumbnail']) && !empty($layout['acfe_flexible_thumbnail'])){
// Thumbnail exists
if($thumbnail_src = wp_get_attachment_url($layout['acfe_flexible_thumbnail'])){
$thumbnail_found = true;
$style[] = 'background-image:url(' . $thumbnail_src . ');';
}
}
// Thumbnail not found
if(!$thumbnail_found){
$class[] = 'acfe-flexible-layout-thumbnail-not-found';
}
$thumbnail = '';
}
$category = '';
if(isset($layout['acfe_flexible_category']) && !empty($layout['acfe_flexible_category'])){
$category = 'data-acfe-flexible-category="' . $layout['acfe_flexible_category'] . '"';
}
$layout['label'] = $thumbnail . '' . $layout['label'] . '';
}
return $field;
}
add_filter('acf/fields/flexible_content/layout_title', 'acfe_flexible_layout_title_remove', 0, 4);
function acfe_flexible_layout_title_remove($title, $field, $layout, $i){
// Remove thumbnail
$title = preg_replace('#' . $title . '';
}
add_action('wp_ajax_acfe/flexible/layout_preview', 'acfe_flexible_layout_preview');
function acfe_flexible_layout_preview(){
// Options
$options = acf_parse_args($_POST, array(
'post_id' => 0,
'i' => 0,
'field_key' => '',
'nonce' => '',
'layout' => '',
'value' => array()
));
// Load field
$field = acf_get_field($options['field_key']);
if(!$field)
die;
// Get Flexible
$flexible = acf_get_field_type('flexible_content');
// Vars
$layout = $flexible->get_layout($options['layout'], $field);
if(!$layout)
die;
$get_field_object = get_field_object($options['field_key'], $options['post_id'], false, false);
$preview_key = 'preview_' . $options['field_key'];
$get_field_object['key'] = $preview_key;
acf_add_local_field($get_field_object);
add_filter('acf/load_value/key=' . $preview_key, function($value, $post_id, $field) use($options){
$value = array();
$value[0] = $options['value'];
return $value;
}, 10, 3);
$is_preview = true;
if(have_rows($preview_key)):
while(have_rows($preview_key)): the_row();
$render = acfe_flexible_render_layout($layout, $field);
if(empty($render['template']))
break;
// Style
if(!empty($render['style']))
echo $render['style'];
// Style
if(!empty($render['script']))
echo $render['script'];
// Template
include($render['template']);
endwhile;
endif;
die;
}