Skip to content

Instantly share code, notes, and snippets.

@hawkidoki
Created December 25, 2017 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hawkidoki/a23b9655f044bdfb3a119b1ef486cc75 to your computer and use it in GitHub Desktop.
Save hawkidoki/a23b9655f044bdfb3a119b1ef486cc75 to your computer and use it in GitHub Desktop.
<?php
add_filter('acf/load_field/type=flexible_content', 'hwk_acf_flexible_filter');
function hwk_acf_flexible_filter($field){
if(!is_admin() || wp_doing_ajax() || get_post_type() == 'acf-field-group')
return $field;
$parent = _acf_get_field_group_by_key($field['parent']);
if(!isset($parent['hwk_flexible_dynamique']) || empty($parent['hwk_flexible_dynamique']))
return $field;
$parent_name = str_replace('-', '_', sanitize_title($parent['title']));
$match = array();
foreach(hwk_acf_get_layouts($field['name']) as $group_key){
$group = _acf_get_field_group_by_key($group_key);
$name = str_replace('-', '_', sanitize_title($group['title']));
$match['layouts'][] = 'field_hwk_layout_' . $name . '_' . $parent_name;
$match['groups'][] = $group;
}
if(!empty($match['layouts'])){
foreach($field['layouts'] as $k => $layout){
if(!in_array($layout['key'], $match['layouts']))
unset($field['layouts'][$k]);
}
}
if(empty($match['layouts']))
$field['layouts'] = array();
if(!empty($match['groups'])){
global $post, $pagenow, $typenow, $plugin_page;
$filters = array();
if(($pagenow === 'post.php' || $pagenow === 'post-new.php') && $typenow !== 'acf'){
$filters['post_id'] = $post->ID;
$filters['post_type'] = $typenow;
}elseif($pagenow === 'admin.php' && isset($plugin_page) && acf_get_options_page($plugin_page)){
$filters['post_id'] = acf_get_valid_post_id('options');
}elseif(($pagenow === 'edit-tags.php' || $pagenow === 'term.php') && isset($_GET['taxonomy'])){
$filters['taxonomy'] = filter_var($_GET['taxonomy'], FILTER_SANITIZE_STRING);
}elseif($pagenow === 'profile.php'){
$filters['user_id'] = get_current_user_id();
$filters['user_form'] = 'edit';
}elseif($pagenow === 'user-edit.php' && isset($_GET['user_id'])){
$filters['user_id'] = filter_var($_GET['user_id'], FILTER_SANITIZE_NUMBER_INT);
$filters['user_form'] = 'edit';
}elseif($pagenow === 'user-new.php'){
$filters['user_id'] = 'new';
$filters['user_form'] = 'edit';
}elseif($pagenow === 'media.php' || $pagenow === 'upload.php'){
$filters['attachment'] = 'All';
}elseif(acf_is_screen('widgets') || acf_is_screen('customize')){
$filters['widget'] = 'all';
}
foreach($match['groups'] as $group){
$name = str_replace('-', '_', sanitize_title($group['title']));
$screen = acf_get_location_screen($filters, $group);
$match_or = array();
foreach($group['location'] as $location => $rule){
if(empty($rule))
continue;
$match_and = true;
foreach($rule as $rule_id => $val){
if( !acf_match_location_rule($val, $screen) || ($val['param'] == 'hwk_flexible_content_rule' && $val['value'] != $field['name']) ){
$match_and = false;
break;
}
}
$match_or[] = $match_and;
}
$cancel = false;
foreach($match_or as $and){
if($and === true){
$cancel = true;
break;
}
}
if(!$cancel){
foreach($field['layouts'] as $k => $layout){
if($layout['key'] == 'field_hwk_layout_' . $name . '_' . $parent_name)
unset($field['layouts'][$k]);
}
}
}
}
if(empty($field['layouts']))
$field = array(
'key' => '',
'ID' => '',
'type' => '',
'value' => '',
'name' => 'hwk_hidden_parent',
'_name' => '',
'class' => 'acf-hidden',
'wrapper' => array(
'class' => 'acf-hidden'
)
);
return $field;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment