Skip to content

Commit

Permalink
Support Custom Field Suite. fixes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
hissy committed Jun 6, 2014
1 parent ddeae22 commit b114f88
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
21 changes: 14 additions & 7 deletions rs-csv-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,24 @@ public static function save_post($post,$meta,$terms,$thumbnail,$is_update) {
$ph = new wp_post_helper($post);

foreach ($meta as $key => $value) {
$is_cfs = 0;
$is_acf = 0;
if (function_exists('get_field_object')) {
if (strpos($key, 'field_') === 0) {
$fobj = get_field_object($key);
if (is_array($fobj) && isset($fobj['key']) && $fobj['key'] == $key) {
$ph->add_field($key,$value);
$is_acf = 1;
$cfs_prefix = 'cfs_';
if (strpos($key, $cfs_prefix) === 0) {
$ph->add_cfs_field( substr($key, strlen($cfs_prefix)), $value );
$is_cfs = 1;
} else {
if (function_exists('get_field_object')) {
if (strpos($key, 'field_') === 0) {
$fobj = get_field_object($key);
if (is_array($fobj) && isset($fobj['key']) && $fobj['key'] == $key) {
$ph->add_field($key,$value);
$is_acf = 1;
}
}
}
}
if (!$is_acf)
if (!$is_acf && !$is_cfs)
$ph->add_meta($key,$value,true);
}

Expand Down
55 changes: 47 additions & 8 deletions wp_post_helper/class-wp_post_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class wp_post_helper {
private $tags = array();
private $medias = array();
private $metas = array();
private $fields = array();
private $acf_fields = array();
private $cfs_fields = array();
private $media_count = 0;
private $terms = array();

Expand All @@ -64,7 +65,8 @@ public function init($args = array()){
$this->tags = array();
$this->medias = array();
$this->metas = array();
$this->fields = array();
$this->acf_fields = array();
$this->cfs_fields = array();
$this->media_count = 0;

if (is_numeric($args)) {
Expand Down Expand Up @@ -200,11 +202,17 @@ private function add_related_meta($postid){
$this->metas = array();

// add ACF Fields
foreach ($this->fields as $key => $val) {
foreach ($this->acf_fields as $key => $val) {
$this->add_field($key, $val);
}
$this->fields = array();

// add CFS Fields
if (count($this->cfs_fields) > 0) {
$this->save_cfs_fields();
}
$this->fields = array();

return true;
}

Expand Down Expand Up @@ -308,12 +316,43 @@ public function add_meta($metakey, $val, $unique = true){
}
}

// Add Advanced Custom Field
// Add Advanced Custom Fields field
public function add_field($field_key, $val){
if (!$this->postid)
$this->fields[$field_key] = $val;
else
return $val ? update_field($field_key, $val, $this->postid) : false;
if (!function_exists('update_field')) {
$this->add_meta($field_key, $val);
} else {
if (!$this->postid) {
$this->acf_fields[$field_key] = $val;
} else {
return $val ? update_field($field_key, $val, $this->postid) : false;
}
}
}

// Add Custom Field Suite field
public function add_cfs_field($field_key, $val){
global $cfs;
if (!is_object($cfs) || !$cfs instanceof Custom_Field_Suite) {
$this->add_meta($field_key, $val);
} else {
if (!$this->postid) {
$this->cfs_fields[$field_key] = $val;
} else {
return $val ? $cfs->save(array($field_key=>$val), array('ID'=>$this->postid)) : false;
}
}
}

// Save Custom Field Suite fields
public function save_cfs_fields() {
global $cfs;
if (is_object($cfs) && $cfs instanceof Custom_Field_Suite && $this->postid && !is_wp_error($this->postid)) {
$cfs->save($this->cfs_fields,array('ID'=>$this->postid));
} else {
foreach ($this->cfs_fields as $key => $val) {
$this->add_meta($key, $val);
}
}
}
}

Expand Down

0 comments on commit b114f88

Please sign in to comment.