function update_field_group($field_group)
 {
     // vars
     $path = acf_get_setting('save_json');
     $file = $field_group['key'] . '.json';
     // remove trailing slash
     $path = untrailingslashit($path);
     // bail early if dir does not exist
     if (!is_writable($path)) {
         //error_log( 'ACF failed to save field group to .json file. Path does not exist: ' . $path );
         return;
     }
     // load fields
     $fields = acf_get_fields($field_group);
     // prepare fields
     $fields = acf_prepare_fields_for_export($fields);
     // add to field group
     $field_group['fields'] = $fields;
     // extract field group ID
     $id = acf_extract_var($field_group, 'ID');
     // write file
     $f = fopen("{$path}/{$file}", 'w');
     fwrite($f, acf_json_encode($field_group));
     fclose($f);
 }
Example #2
0
 function export()
 {
     // vars
     $json = $this->get_json();
     // validate
     if ($json === false) {
         acf_add_admin_notice(__("No field groups selected", 'acf'), 'error');
         return;
     }
     // set headers
     $file_name = 'acf-export-' . date('Y-m-d') . '.json';
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename={$file_name}");
     header("Content-Type: application/json; charset=utf-8");
     echo acf_json_encode($json);
     die;
 }
Example #3
0
function acf_write_json_field_group($field_group)
{
    // vars
    $path = acf_get_setting('save_json');
    $file = $field_group['key'] . '.json';
    // remove trailing slash
    $path = untrailingslashit($path);
    // bail early if dir does not exist
    if (!is_writable($path)) {
        return false;
    }
    // prepare for export
    $id = acf_extract_var($field_group, 'ID');
    $field_group = acf_prepare_field_group_for_export($field_group);
    // add modified time
    $field_group['modified'] = get_post_modified_time('U', true, $id, true);
    // write file
    $f = fopen("{$path}/{$file}", 'w');
    fwrite($f, acf_json_encode($field_group));
    fclose($f);
    // return
    return true;
}
 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 export($args, $assoc_args)
 {
     // if empty it will show export all fields
     $export_field = '';
     if (is_multisite()) {
         $choice = $this->select_blog();
         switch_to_blog($choice);
     }
     //if export all is used skip the question popup
     if (empty($args) || $args[0] != 'all') {
         $export_field = $this->select_acf_field();
     }
     if (empty($export_field)) {
         WP_CLI::success("Exporting all fieldgroups \n");
     } else {
         WP_CLI::success("Exporting fieldgroup \n");
     }
     $field_groups = get_posts(array('numberposts' => -1, 'post_type' => 'acf-field-group', 'sort_column' => 'menu_order', 'order' => 'ASC', 'include' => $export_field));
     if ($field_groups) {
         if (isset($assoc_args['export-path']) && isset($this->paths[$assoc_args['export-path']])) {
             $export_path = $this->paths[$assoc_args['export-path']];
         } else {
             $export_path = $this->select_export_path();
         }
         $acf_fld_grp = new acf_field();
         if (!is_dir($export_path) && !mkdir($export_path, 0755, false)) {
             WP_CLI::line('fieldgroup directory exists or cant be created!');
         }
         foreach ($field_groups as $group) {
             $title = get_the_title($group->ID);
             $sanitized_title = sanitize_title($title);
             $subpath = $export_path . $sanitized_title;
             $uniquid_path = $subpath . '/uniqid';
             $field_group_array = array();
             $field_group = acf_get_field_group($group->ID);
             // validate field group
             if (empty($field_group)) {
                 continue;
             }
             // load fields
             $fields = acf_get_fields($field_group);
             // prepare fields
             $fields = acf_prepare_fields_for_export($fields);
             // add to field group
             $field_group['fields'] = $fields;
             // extract field group ID
             $id = acf_extract_var($field_group, 'ID');
             $json = acf_json_encode($field_group);
             // retrieve the uniquid from the file if it exists else we make a new one
             $uniqid = file_exists($uniquid_path) ? file_get_contents($uniquid_path) : uniqid();
             // each field_group gets it's own folder by field_group name
             if (!is_dir($subpath) && !mkdir($subpath, 0755, false)) {
                 WP_CLI::line('fieldgroup subdirectory exists or cant be created!');
             } else {
                 // let's write the array to a data.php file so it can be used later on
                 $fp = fopen($subpath . '/' . "data.php", "w");
                 $output = "<?php \n\$group = " . var_export($field_group, true) . ';';
                 fwrite($fp, $output);
                 fclose($fp);
                 $fp = fopen($subpath . '/' . "data.json", "w");
                 $output = $json;
                 fwrite($fp, $output);
                 fclose($fp);
                 // write the uniquid file if it doesn't exist
                 if (!file_exists($uniquid_path)) {
                     $fp = fopen($subpath . '/' . "uniqid", "w");
                     $output = $uniqid;
                     fwrite($fp, $output);
                     fclose($fp);
                 }
                 WP_CLI::success("Fieldgroup " . $title . " exported ");
             }
         }
     } else {
         //error seems to be returning and break out of my loop
         //WP_CLI::error( 'No field groups were found in the database' );
         echo 'No field groups were found in the database';
         echo ' ';
     }
     if (is_multisite()) {
         restore_current_blog();
     }
 }
 function export()
 {
     // validate
     if (empty($_POST['acf_export_keys'])) {
         return;
     }
     // construct JSON
     foreach ($_POST['acf_export_keys'] as $key) {
         // load field group
         $field_group = acf_get_field_group($key);
         // validate field group
         if (empty($field_group)) {
             continue;
         }
         // load fields
         $fields = acf_get_fields($field_group);
         // prepare fields
         $fields = acf_prepare_fields_for_export($fields);
         // add to field group
         $field_group['fields'] = $fields;
         // extract field group ID
         $id = acf_extract_var($field_group, 'ID');
         // add to json array
         $json[] = $field_group;
     }
     // end foreach
     // set headers
     $file_name = 'acf-export-' . date('Y-m-d') . '.json';
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename={$file_name}");
     header("Content-Type: application/json");
     echo acf_json_encode($json);
     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;
     // 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);
 }
Example #9
0
 /**
  * Create a new ACF Commit
  *
  * @param $commit_message - Detailed Message describing change to ACF Field Group
  * @param int $post_id - Post ID of ACF Field Group
  */
 public function create_commit($commit_message, $post_id = 0)
 {
     $_SESSION['commit-complete'] = true;
     $field_groups = acf_get_field_groups();
     $exportJSON = array();
     if (!empty($field_groups)) {
         foreach ($field_groups as $field_group) {
             $field_group['fields'] = acf_get_fields($field_group);
             $field_group['fields'] = acf_prepare_fields_for_export($field_group['fields']);
             acf_extract_var($field_group, 'ID');
             $exportJSON[] = $field_group;
         }
     }
     $export = acf_json_encode($exportJSON);
     $commitID = wp_insert_post(array('post_type' => 'acf-commit', 'post_status' => 'publish', 'post_content' => $export, 'post_title' => 'ACF Revision: ' . date('Y-m-d h:i:s')));
     update_field('commit_message', $commit_message, $commitID);
     update_field('acf_field_group', get_the_title($post_id), $commitID);
 }