function add_field_group($field_group)
 {
     // validate
     $field_group = acf_get_valid_field_group($field_group);
     // don't allow overrides
     if (acf_is_local_field_group($field_group['key'])) {
         return;
     }
     // remove fields
     $fields = acf_extract_var($field_group, 'fields');
     // format fields
     $fields = acf_prepare_fields_for_import($fields);
     // add field group
     $this->groups[$field_group['key']] = $field_group;
     // add fields
     foreach ($fields as $field) {
         // add parent
         if (empty($field['parent'])) {
             $field['parent'] = $field_group['key'];
         }
         // add field group reference
         //$field['field_group'] = $field_group['key'];
         // add field
         $this->add_field($field);
     }
 }
Пример #2
0
function _acf_get_field_group_by_key($key = '', $search_trash = false)
{
    // vars
    $field_group = false;
    // try JSON before DB to save query time
    if (acf_is_local_field_group($key)) {
        $field_group = acf_get_local_field_group($key);
        // validate
        $field_group = acf_get_valid_field_group($field_group);
        // return
        return $field_group;
    }
    // vars
    $args = array('posts_per_page' => 1, 'post_type' => 'acf-field-group', 'orderby' => 'menu_order title', 'order' => 'ASC', 'suppress_filters' => false, 'acf_group_key' => $key);
    // search trash?
    if ($search_trash) {
        $args['post_status'] = 'publish, trash';
    }
    // load posts
    $posts = get_posts($args);
    // validate
    if (empty($posts[0])) {
        return $field_group;
    }
    // load from ID
    $field_group = _acf_get_field_group_by_id($posts[0]->ID);
    // return
    return $field_group;
}
 function rebuild_group($group_key)
 {
     // check for group in cache
     $found = false;
     $cache = wp_cache_get('acf_reusable/rebuilt_group_' . $group_key, 'acf_resuable', false, $found);
     if ($found) {
         $group = $cache;
     }
     // check for json field
     $json_found = false;
     /*
     // no need to check local groups because 
     // if they existed they would have been loaded by maybe_load_local
     // and we would not be here
     // will remove this code if the early loading of local json files works
     if (!$found) {
     	$json_path = plugin_dir_path(__FILE__).'acf-json';
     	if (!is_dir($json_path)) {
     		@mkdir($json_path);
     	}
     	if (is_multisite()) {
     		$json_path .= '/'.get_current_blog_id();
     		if (!is_dir($json_path)) {
     			@mkdir($json_path);
     		}
     	}
     	if (is_dir($json_path) && 
     			($files = scandir($json_path)) !== false &&
     			count($files)) {
     		foreach ($files as $file) {
     			$file_path = $json_path.'/'.$file;
     			if (!is_dir($file_path) && preg_match('/\.json$/', $file)) {
     				$file_group_key = preg_replace('/\.json$/', '', $file);
     				if ($file_group_key == $group_key) {
     					if (($json = file_get_contents($file_path)) !== false &&
     							($group = json_decode($json, true)) !== NULL) {
     						$json_found = true;
     						$found = true;
     					}
     				} // end if match
     			} // end if json file
     		} // end foreach file
     	} // end if is_dir etc
     } // end if !found
     */
     // neither found the rebuild group
     if (!$found) {
         $group = $this->field_groups[$group_key];
         $group['fields'] = $this->rebuild_fields($group_key, $group['fields']);
         $group['fields'] = $this->replace_keys($group['fields']);
         $group['fields'] = $this->correct_keys($group['fields']);
         //echo preg_replace('/\_\[[0-9]+\]\_/', '', 'field_566f61991b2c4_566f60e177211_[1]_');die;
         //echo '<pre>'; print_r($group['fields']); die;
         $this->replaced_keys = array();
         unset($group['ID']);
     }
     // make sure json path exists
     $json_path = plugin_dir_path(__FILE__) . 'acf-json';
     if (!is_dir($json_path)) {
         @mkdir($json_path);
     }
     if (is_multisite()) {
         $json_path .= '/' . get_current_blog_id();
         if (!is_dir($json_path)) {
             @mkdir($json_path);
         }
     }
     // if json files was not found then save json file
     // attempt to save file/ silently fail if folder is not writable
     if (!$json_found && is_dir($json_path)) {
         if (($handle = @fopen($json_path . '/' . $group_key . '.json', 'w')) !== false) {
             $json = acf_json_encode($group);
             fwrite($handle, $json, strlen($json));
             fclose($handle);
         }
     }
     // end if !json found
     // save in cache
     wp_cache_set('acf_reusable/rebuilt_group_' . $group_key, $group, 'acf_resuable');
     $this->field_groups[$group_key] = $group;
     $this->new_field_groups[] = $group;
     if (acf_is_local_field_group($group_key)) {
         // this is already a local field group
         // remove the existing version before replacing
         acf_remove_local_fields($group_key);
         // there currently isn't a remove group function in acf_local
         //echo $group_key,'<br>';
         $this->clear_acf_cache();
         $acf_local = acf_local();
         unset($acf_local->groups[$group_key]);
         //echo '<pre>'; print_r($acf_local->groups); die;
         //unset(acf_local()->groups[$group_key]);
     }
     //echo '<pre>'; print_r($group); die;
     // add or replace the field group to local groups
     acf_add_local_field_group($group);
     //echo $group_key,'<br><pre>'; print_r(acf_get_field_groups()); die;
 }
 function rebuild_group($group_key)
 {
     $group = $this->field_groups[$group_key];
     $group['fields'] = $this->rebuild_fields($group_key, $group['fields']);
     $group['fields'] = $this->replace_keys($group['fields']);
     unset($group['ID']);
     $this->new_field_groups[] = $group;
     if (acf_is_local_field_group($group_key)) {
         // this is already a local field group
         // remove the existing version before replacing
         acf_remove_local_fields($group_key);
         // there currently isn't a remove group function in acf_local
         unset(acf_local()->groups[$group_key]);
     }
     // add or replace the field group to local groups
     acf_add_local_field_group($group);
 }
 function rebuild_group($group_key)
 {
     // check for group in cache
     $found = false;
     $cache = wp_cache_get('acf_reusable/rebuilt_group_' . $group_key, 'acf_resuable', false, $found);
     if ($found) {
         $group = $cache;
     }
     // check for json field
     $json_found = false;
     // neither found the rebuild group
     if (!$found) {
         $group = $this->field_groups[$group_key];
         $group['fields'] = $this->rebuild_fields($group_key, $group['fields']);
         $group['fields'] = $this->replace_keys($group['fields']);
         $group['fields'] = $this->correct_keys($group['fields']);
         //echo preg_replace('/\_\[[0-9]+\]\_/', '', 'field_566f61991b2c4_566f60e177211_[1]_');die;
         //echo '<pre>'; print_r($group['fields']); die;
         $this->replaced_keys = array();
         unset($group['ID']);
     }
     if (!defined('ACF_REUSABLE_DISABLE_JSON') || !ACF_REUSABLE_DISABLE_JSON) {
         // make sure json path exists
         $json_path = plugin_dir_path(__FILE__) . 'acf-json';
         if (!is_dir($json_path)) {
             @mkdir($json_path);
         }
         if (is_multisite()) {
             $json_path .= '/' . get_current_blog_id();
             if (!is_dir($json_path)) {
                 @mkdir($json_path);
             }
         }
         // if json files was not found then save json file
         // attempt to save file/ silently fail if folder is not writable
         if (!$json_found && is_dir($json_path)) {
             if (($handle = @fopen($json_path . '/' . $group_key . '.json', 'w')) !== false) {
                 $json = acf_json_encode($group);
                 fwrite($handle, $json, strlen($json));
                 fclose($handle);
             }
         }
         // end if !json found
     }
     // save in cache
     wp_cache_set('acf_reusable/rebuilt_group_' . $group_key, $group, 'acf_resuable');
     $this->field_groups[$group_key] = $group;
     $this->new_field_groups[] = $group;
     if (acf_is_local_field_group($group_key)) {
         // this is already a local field group
         // remove the existing version before replacing
         acf_remove_local_fields($group_key);
         // there currently isn't a remove group function in acf_local
         //echo $group_key,'<br>';
         $this->clear_acf_cache();
         $acf_local = acf_local();
         unset($acf_local->groups[$group_key]);
         //echo '<pre>'; print_r($acf_local->groups); die;
         //unset(acf_local()->groups[$group_key]);
     }
     //echo '<pre>'; print_r($group); die;
     // add or replace the field group to local groups
     acf_add_local_field_group($group);
     //echo $group_key,'<br><pre>'; print_r(acf_get_field_groups()); die;
 }
 function rebuild_group($group_key)
 {
     // check for group in cache
     $found = false;
     $cache = wp_cache_get('acf_reusable/rebuilt_group_' . $group_key, 'acf_resuable', false, $found);
     if ($found) {
         $group = $cache;
     }
     // check for json field
     $json_found = false;
     if (!$found) {
         $json_path = plugin_dir_path(__FILE__) . 'acf-json';
         if (is_dir($json_path) && ($files = scandir($json_path)) !== false && count($files)) {
             foreach ($files as $file) {
                 $file_path = $json_path . '/' . $file;
                 if (!is_dir($file_path) && preg_match('/\\.json$/', $file)) {
                     $file_group_key = preg_replace('/\\.json$/', '', $file);
                     if ($file_group_key == $group_key) {
                         if (($json = file_get_contents($file_path)) !== false && ($group = json_decode($json, true)) !== NULL) {
                             $json_found = true;
                             $found = true;
                         }
                     }
                     // end if match
                 }
                 // end if json file
             }
             // end foreach file
         }
         // end if is_dir etc
     }
     // end if !found
     // neither found the rebuild group
     if (!$found) {
         $group = $this->field_groups[$group_key];
         $group['fields'] = $this->rebuild_fields($group_key, $group['fields']);
         $group['fields'] = $this->replace_keys($group['fields']);
         $group['fields'] = $this->correct_keys($group['fields']);
         //echo preg_replace('/\_\[[0-9]+\]\_/', '', 'field_566f61991b2c4_566f60e177211_[1]_');die;
         //echo '<pre>'; print_r($group['fields']); die;
         $this->replaced_keys = array();
         unset($group['ID']);
     }
     // if json files was not found then save json file
     // attempt to save file
     if (!$json_found && is_dir($json_path)) {
         if (($handle = @fopen($json_path . '/' . $group_key . '.json', 'w')) !== false) {
             $json = acf_json_encode($group);
             fwrite($handle, $json, strlen($json));
             fclose($handle);
         }
     }
     // end if !json found
     // save in cache
     wp_cache_set('acf_reusable/rebuilt_group_' . $group_key, $group, 'acf_resuable');
     $this->field_groups[$group_key] = $group;
     $this->new_field_groups[] = $group;
     if (acf_is_local_field_group($group_key)) {
         // this is already a local field group
         // remove the existing version before replacing
         acf_remove_local_fields($group_key);
         // there currently isn't a remove group function in acf_local
         unset(acf_local()->groups[$group_key]);
     }
     // add or replace the field group to local groups
     acf_add_local_field_group($group);
 }