Version Description
- Bug fix: Fix to enable to update post meta values.
Download this release
Release Info
Developer | hissy |
Plugin | Really Simple CSV Importer |
Version | 0.5.5 |
Comparing to | |
See all releases |
Code changes from version 0.5.6 to 0.5.5
- readme.txt +29 -14
- rs-csv-importer.php +48 -22
- sample/custom_fields.csv +3 -0
- sample/custom_fields.ods +0 -0
- wp_post_helper/class-wp_post_helper.php +64 -38
readme.txt
CHANGED
@@ -2,25 +2,27 @@
|
|
2 |
Contributors: hissy, wokamoto
|
3 |
Tags: importer, csv, acf
|
4 |
Requires at least: 3.0
|
5 |
-
Tested up to: 3.
|
6 |
-
Stable tag: 0.
|
7 |
License: GPLv2 or later
|
8 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
9 |
|
10 |
-
|
11 |
|
12 |
== Description ==
|
13 |
|
14 |
-
Alternative CSV Importer plugin. Simple and powerful.
|
15 |
|
16 |
* Category support
|
17 |
* Tag support
|
18 |
* Custom field support
|
19 |
-
*
|
|
|
20 |
* Custom Taxonomy support
|
21 |
* Custom Post Type support
|
|
|
22 |
|
23 |
-
|
24 |
|
25 |
= Available column names and values: =
|
26 |
* `ID` or `post_id`: (int) post id.
|
@@ -37,10 +39,11 @@ Contains CSV file examples in `/wp-content/plugins/really-simple-csv-importer/sa
|
|
37 |
* `post_type`: ('post' or 'page' or any other post type name) *(required)* The post type slug, not labels.
|
38 |
* `post_thumbnail`: (string) The uri or path of the post thumbnail.
|
39 |
E.g. http://example.com/example.jpg or /path/to/example.jpg
|
40 |
-
* `post_category`: (string, comma
|
41 |
-
* `post_tags`: (string, comma
|
42 |
-
* `{
|
43 |
-
* `
|
|
|
44 |
|
45 |
Note: Empty cells in the csv file means "keep it", not "delete it".
|
46 |
Note: To set the page template of a page, use custom field key of `_wp_page_template`.
|
@@ -98,10 +101,15 @@ Because PHP cannot read multibyte text cells in some cases.
|
|
98 |
|
99 |
> Locale setting is taken into account by this function. If LANG is e.g. en_US.UTF-8, files in one-byte encoding are read wrong by this function.
|
100 |
|
101 |
-
= Can I insert multiple
|
102 |
|
103 |
Yes. Please use `really_simple_csv_importer_save_meta` filter to make array data.
|
104 |
|
|
|
|
|
|
|
|
|
|
|
105 |
== How to customize import post data ==
|
106 |
|
107 |
There are three filters available in the importer.
|
@@ -194,13 +202,20 @@ function really_simple_csv_importer_save_tax_filter( $tax, $post, $is_update ) {
|
|
194 |
add_filter( 'really_simple_csv_importer_save_tax', 'really_simple_csv_importer_save_tax_filter', 10, 3 );
|
195 |
`
|
196 |
|
197 |
-
|
198 |
|
199 |
-
|
200 |
-
|
|
|
201 |
|
202 |
== Changelog ==
|
203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
= 0.5.6 =
|
205 |
* Bug fix: Fails to update empty custom field value.
|
206 |
= 0.5.5 =
|
2 |
Contributors: hissy, wokamoto
|
3 |
Tags: importer, csv, acf
|
4 |
Requires at least: 3.0
|
5 |
+
Tested up to: 3.9.1
|
6 |
+
Stable tag: 0.6
|
7 |
License: GPLv2 or later
|
8 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
9 |
|
10 |
+
Alternative CSV Importer plugin. Simple and powerful, best for geeks.
|
11 |
|
12 |
== Description ==
|
13 |
|
14 |
+
Alternative CSV Importer plugin. Simple and powerful, best for geeks.
|
15 |
|
16 |
* Category support
|
17 |
* Tag support
|
18 |
* Custom field support
|
19 |
+
* Custom Field Suite support
|
20 |
+
* Advanced Custom Fields support
|
21 |
* Custom Taxonomy support
|
22 |
* Custom Post Type support
|
23 |
+
* Many useful filter hooks
|
24 |
|
25 |
+
You can download CSV file examples in `/wp-content/plugins/really-simple-csv-importer/sample` directory.
|
26 |
|
27 |
= Available column names and values: =
|
28 |
* `ID` or `post_id`: (int) post id.
|
39 |
* `post_type`: ('post' or 'page' or any other post type name) *(required)* The post type slug, not labels.
|
40 |
* `post_thumbnail`: (string) The uri or path of the post thumbnail.
|
41 |
E.g. http://example.com/example.jpg or /path/to/example.jpg
|
42 |
+
* `post_category`: (string, comma separated) slug of post categories
|
43 |
+
* `post_tags`: (string, comma separated) name of post tags
|
44 |
+
* `tax_{taxonomy}`: (string, comma separated) Any field prefixed with `tax_` will be used as a custom taxonomy. Taxonomy must already exist. Entries are names or slugs of terms.
|
45 |
+
* `{custom_field_key}`: (string) Any other column labels used as custom field
|
46 |
+
* `cfs_{field_name}`: (string) If you would like to import data to custom fields set by Custom Field Suite, please add prefix `cfs_`
|
47 |
|
48 |
Note: Empty cells in the csv file means "keep it", not "delete it".
|
49 |
Note: To set the page template of a page, use custom field key of `_wp_page_template`.
|
101 |
|
102 |
> Locale setting is taken into account by this function. If LANG is e.g. en_US.UTF-8, files in one-byte encoding are read wrong by this function.
|
103 |
|
104 |
+
= Can I insert multiple values to CFS or ACF fields like Select or Checkbox? =
|
105 |
|
106 |
Yes. Please use `really_simple_csv_importer_save_meta` filter to make array data.
|
107 |
|
108 |
+
== How to debug import data ==
|
109 |
+
|
110 |
+
*Really Simple CSV Importer Debugger add-on* enables you to dry-run-testing and show more detailed post, meta, taxonomy data of each csv row.
|
111 |
+
Download from [gist](https://gist.github.com/hissy/7175656).
|
112 |
+
|
113 |
== How to customize import post data ==
|
114 |
|
115 |
There are three filters available in the importer.
|
202 |
add_filter( 'really_simple_csv_importer_save_tax', 'really_simple_csv_importer_save_tax_filter', 10, 3 );
|
203 |
`
|
204 |
|
205 |
+
= really_simple_csv_importer_class =
|
206 |
|
207 |
+
This filter provides availability to completely replace the `RS_CSV_Importer::save_post` method.
|
208 |
+
|
209 |
+
Example: [gist](https://gist.github.com/hissy/1ea54a46fd07be9f4334)
|
210 |
|
211 |
== Changelog ==
|
212 |
|
213 |
+
= 0.6 =
|
214 |
+
* Enhancement: Custom Field Suite support
|
215 |
+
* Enhancement: Add ability to override save_post method
|
216 |
+
* Performance Improvements
|
217 |
+
= 0.5.7 =
|
218 |
+
* Enhancement: Add dry run filter
|
219 |
= 0.5.6 =
|
220 |
* Bug fix: Fails to update empty custom field value.
|
221 |
= 0.5.5 =
|
rs-csv-importer.php
CHANGED
@@ -7,7 +7,7 @@ Author: Takuro Hishikawa, wokamoto
|
|
7 |
Author URI: https://en.digitalcube.jp/
|
8 |
Text Domain: rs-csv-importer
|
9 |
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
10 |
-
Version: 0.
|
11 |
*/
|
12 |
|
13 |
if ( !defined('WP_LOAD_IMPORTERS') )
|
@@ -56,15 +56,17 @@ class RS_CSV_Importer extends WP_Importer {
|
|
56 |
// Step 1
|
57 |
function greet() {
|
58 |
echo '<p>'.__( 'Choose a CSV (.csv) file to upload, then click Upload file and import.', 'rs-csv-importer' ).'</p>';
|
59 |
-
echo '<p>'.__( '
|
|
|
60 |
echo '<ol>';
|
61 |
echo '<li>'.__( 'Select UTF-8 as charset.', 'rs-csv-importer' ).'</li>';
|
62 |
echo '<li>'.sprintf( __( 'You must use field delimiter as "%s"', 'rs-csv-importer'), RS_CSV_Helper::DELIMITER ).'</li>';
|
63 |
echo '<li>'.__( 'You must quote all text cells.', 'rs-csv-importer' ).'</li>';
|
64 |
echo '</ol>';
|
65 |
-
echo '<p>'.__( '
|
66 |
echo ' <a href="'.plugin_dir_url( __FILE__ ).'sample/sample.csv">'.__( 'csv', 'rs-csv-importer' ).'</a>,';
|
67 |
-
echo ' <a href="'.plugin_dir_url( __FILE__ ).'sample/sample.ods">'.__( 'ods
|
|
|
68 |
echo '</p>';
|
69 |
wp_import_upload_form( add_query_arg('step', 1) );
|
70 |
}
|
@@ -103,21 +105,28 @@ class RS_CSV_Importer extends WP_Importer {
|
|
103 |
* @param bool $is_update
|
104 |
* @return int|false Saved post id. If failed, return false.
|
105 |
*/
|
106 |
-
function save_post($post,$meta,$terms,$thumbnail,$is_update) {
|
107 |
$ph = new wp_post_helper($post);
|
108 |
|
109 |
foreach ($meta as $key => $value) {
|
|
|
110 |
$is_acf = 0;
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
}
|
118 |
}
|
119 |
}
|
120 |
-
if (!$is_acf)
|
121 |
$ph->add_meta($key,$value,true);
|
122 |
}
|
123 |
|
@@ -169,7 +178,7 @@ class RS_CSV_Importer extends WP_Importer {
|
|
169 |
if (post_type_exists($post_type)) {
|
170 |
$post['post_type'] = $post_type;
|
171 |
} else {
|
172 |
-
$error->add( 'post_type_exists', sprintf(__('
|
173 |
}
|
174 |
} else {
|
175 |
echo __('Note: Please include post_type value if that is possible.', 'rs-csv-importer').'<br>';
|
@@ -187,7 +196,7 @@ class RS_CSV_Importer extends WP_Importer {
|
|
187 |
$post['ID'] = $post_id;
|
188 |
$is_update = true;
|
189 |
} else {
|
190 |
-
$error->add( 'post_type_check', sprintf(__('The post
|
191 |
}
|
192 |
}
|
193 |
}
|
@@ -254,7 +263,7 @@ class RS_CSV_Importer extends WP_Importer {
|
|
254 |
$post['menu_order'] = $menu_order;
|
255 |
}
|
256 |
|
257 |
-
// (string, comma
|
258 |
$post_category = $h->get_data($this,$data,'post_category');
|
259 |
if ($post_category) {
|
260 |
$categories = preg_split("/,+/", $post_category);
|
@@ -263,13 +272,10 @@ class RS_CSV_Importer extends WP_Importer {
|
|
263 |
}
|
264 |
}
|
265 |
|
266 |
-
// (string, comma
|
267 |
$post_tags = $h->get_data($this,$data,'post_tags');
|
268 |
if ($post_tags) {
|
269 |
-
$
|
270 |
-
if ($tags) {
|
271 |
-
$post['post_tags'] = $tags;
|
272 |
-
}
|
273 |
}
|
274 |
|
275 |
// (string) post thumbnail image uri
|
@@ -323,9 +329,29 @@ class RS_CSV_Importer extends WP_Importer {
|
|
323 |
*/
|
324 |
$tax = apply_filters( 'really_simple_csv_importer_save_tax', $tax, $post, $is_update );
|
325 |
|
326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
327 |
// save post data
|
328 |
-
$
|
|
|
|
|
|
|
|
|
|
|
329 |
if ($result) {
|
330 |
echo esc_html(sprintf(__('Processing "%s" done.', 'rs-csv-importer'), $post_title));
|
331 |
} else {
|
7 |
Author URI: https://en.digitalcube.jp/
|
8 |
Text Domain: rs-csv-importer
|
9 |
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
10 |
+
Version: 0.6
|
11 |
*/
|
12 |
|
13 |
if ( !defined('WP_LOAD_IMPORTERS') )
|
56 |
// Step 1
|
57 |
function greet() {
|
58 |
echo '<p>'.__( 'Choose a CSV (.csv) file to upload, then click Upload file and import.', 'rs-csv-importer' ).'</p>';
|
59 |
+
echo '<p>'.__( 'Excel-style CSV file is unconventional and not recommended. LibreOffice has enough export options and recommended for most users.', 'rs-csv-importer' ).'</p>';
|
60 |
+
echo '<p>'.__( 'Requirements:', 'rs-csv-importer' ).'</p>';
|
61 |
echo '<ol>';
|
62 |
echo '<li>'.__( 'Select UTF-8 as charset.', 'rs-csv-importer' ).'</li>';
|
63 |
echo '<li>'.sprintf( __( 'You must use field delimiter as "%s"', 'rs-csv-importer'), RS_CSV_Helper::DELIMITER ).'</li>';
|
64 |
echo '<li>'.__( 'You must quote all text cells.', 'rs-csv-importer' ).'</li>';
|
65 |
echo '</ol>';
|
66 |
+
echo '<p>'.__( 'Download example CSV files:', 'rs-csv-importer' );
|
67 |
echo ' <a href="'.plugin_dir_url( __FILE__ ).'sample/sample.csv">'.__( 'csv', 'rs-csv-importer' ).'</a>,';
|
68 |
+
echo ' <a href="'.plugin_dir_url( __FILE__ ).'sample/sample.ods">'.__( 'ods', 'rs-csv-importer' ).'</a>';
|
69 |
+
echo ' '.__('(OpenDocument Spreadsheet file format for LibreOffice. Please export as csv before import)', 'rs-csv-importer' );
|
70 |
echo '</p>';
|
71 |
wp_import_upload_form( add_query_arg('step', 1) );
|
72 |
}
|
105 |
* @param bool $is_update
|
106 |
* @return int|false Saved post id. If failed, return false.
|
107 |
*/
|
108 |
+
public static function save_post($post,$meta,$terms,$thumbnail,$is_update) {
|
109 |
$ph = new wp_post_helper($post);
|
110 |
|
111 |
foreach ($meta as $key => $value) {
|
112 |
+
$is_cfs = 0;
|
113 |
$is_acf = 0;
|
114 |
+
$cfs_prefix = 'cfs_';
|
115 |
+
if (strpos($key, $cfs_prefix) === 0) {
|
116 |
+
$ph->add_cfs_field( substr($key, strlen($cfs_prefix)), $value );
|
117 |
+
$is_cfs = 1;
|
118 |
+
} else {
|
119 |
+
if (function_exists('get_field_object')) {
|
120 |
+
if (strpos($key, 'field_') === 0) {
|
121 |
+
$fobj = get_field_object($key);
|
122 |
+
if (is_array($fobj) && isset($fobj['key']) && $fobj['key'] == $key) {
|
123 |
+
$ph->add_field($key,$value);
|
124 |
+
$is_acf = 1;
|
125 |
+
}
|
126 |
}
|
127 |
}
|
128 |
}
|
129 |
+
if (!$is_acf && !$is_cfs)
|
130 |
$ph->add_meta($key,$value,true);
|
131 |
}
|
132 |
|
178 |
if (post_type_exists($post_type)) {
|
179 |
$post['post_type'] = $post_type;
|
180 |
} else {
|
181 |
+
$error->add( 'post_type_exists', sprintf(__('Invalid post type "%s".', 'rs-csv-importer'), $post_type) );
|
182 |
}
|
183 |
} else {
|
184 |
echo __('Note: Please include post_type value if that is possible.', 'rs-csv-importer').'<br>';
|
196 |
$post['ID'] = $post_id;
|
197 |
$is_update = true;
|
198 |
} else {
|
199 |
+
$error->add( 'post_type_check', sprintf(__('The post type value from your csv file does not match the existing data in your database. post_id: %d, post_type(csv): %s, post_type(db): %s', 'rs-csv-importer'), $post_id, $post_type, $post_exist->post_type) );
|
200 |
}
|
201 |
}
|
202 |
}
|
263 |
$post['menu_order'] = $menu_order;
|
264 |
}
|
265 |
|
266 |
+
// (string, comma separated) slug of post categories
|
267 |
$post_category = $h->get_data($this,$data,'post_category');
|
268 |
if ($post_category) {
|
269 |
$categories = preg_split("/,+/", $post_category);
|
272 |
}
|
273 |
}
|
274 |
|
275 |
+
// (string, comma separated) name of post tags
|
276 |
$post_tags = $h->get_data($this,$data,'post_tags');
|
277 |
if ($post_tags) {
|
278 |
+
$post['post_tags'] = $post_tags;
|
|
|
|
|
|
|
279 |
}
|
280 |
|
281 |
// (string) post thumbnail image uri
|
329 |
*/
|
330 |
$tax = apply_filters( 'really_simple_csv_importer_save_tax', $tax, $post, $is_update );
|
331 |
|
332 |
+
/**
|
333 |
+
* Option for dry run
|
334 |
+
*
|
335 |
+
* @param bool false
|
336 |
+
*/
|
337 |
+
$dry_run = apply_filters( 'really_simple_csv_importer_dry_run', false );
|
338 |
+
|
339 |
+
if (!$error->get_error_codes() && $dry_run == false) {
|
340 |
+
|
341 |
+
/**
|
342 |
+
* Get Alternative Importer Class name.
|
343 |
+
*
|
344 |
+
* @param string Class name to override Importer class. Default to null (do not override).
|
345 |
+
*/
|
346 |
+
$class = apply_filters( 'really_simple_csv_importer_class', null );
|
347 |
+
|
348 |
// save post data
|
349 |
+
if ($class && class_exists($class,false)) {
|
350 |
+
$result = $class::save_post($post,$meta,$tax,$post_thumbnail,$is_update);
|
351 |
+
} else {
|
352 |
+
$result = self::save_post($post,$meta,$tax,$post_thumbnail,$is_update);
|
353 |
+
}
|
354 |
+
|
355 |
if ($result) {
|
356 |
echo esc_html(sprintf(__('Processing "%s" done.', 'rs-csv-importer'), $post_title));
|
357 |
} else {
|
sample/custom_fields.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
1 |
+
"post_title","post_id","post_type","post_status","post_tags","normal_field","cfs_date","cfs_color","cfs_boolean","cfs_select","cfs_post","cfs_file","field_5391c98483502"
|
2 |
+
"Import test",,"post","publish","Apple, Banana","Doll","2014-06-06","#ff0099","1","A","1,100","101","ACF Test"
|
3 |
+
"Update test",1,"post","draft","Cherry",,,,0,"B",,,"ACF update test"
|
sample/custom_fields.ods
ADDED
Binary file
|
wp_post_helper/class-wp_post_helper.php
CHANGED
@@ -34,11 +34,14 @@ class wp_post_helper {
|
|
34 |
|
35 |
private $postid = false;
|
36 |
private $attachment_id = array();
|
|
|
|
|
37 |
|
38 |
private $tags = array();
|
39 |
private $medias = array();
|
40 |
private $metas = array();
|
41 |
-
private $
|
|
|
42 |
private $media_count = 0;
|
43 |
private $terms = array();
|
44 |
|
@@ -64,13 +67,14 @@ class wp_post_helper {
|
|
64 |
$this->tags = array();
|
65 |
$this->medias = array();
|
66 |
$this->metas = array();
|
67 |
-
$this->
|
|
|
68 |
$this->media_count = 0;
|
69 |
|
70 |
if (is_numeric($args)) {
|
71 |
$post = get_post(intval($args));
|
72 |
if ($post && isset($post->ID) && !is_wp_error($post)) {
|
73 |
-
$this->
|
74 |
$this->post = $post;
|
75 |
return true;
|
76 |
} else {
|
@@ -97,10 +101,11 @@ class wp_post_helper {
|
|
97 |
$post_id = isset($args['ID']) ? $args['ID'] : $args['post_id'];
|
98 |
$post = get_post($post_id, 'ARRAY_A');
|
99 |
if (isset($post['ID'])) {
|
100 |
-
$this->
|
101 |
$this->post->ID = $post_id;
|
102 |
unset($post['ID']);
|
103 |
$this->set($post);
|
|
|
104 |
}
|
105 |
unset($post);
|
106 |
}
|
@@ -118,11 +123,7 @@ class wp_post_helper {
|
|
118 |
$this->post = $post;
|
119 |
|
120 |
if (isset($args['post_tags'])) {
|
121 |
-
$this->add_tags(
|
122 |
-
is_array($args['post_tags'])
|
123 |
-
? $args['post_tags']
|
124 |
-
: explode(',', $args['post_tags'])
|
125 |
-
);
|
126 |
}
|
127 |
|
128 |
return true;
|
@@ -130,21 +131,7 @@ class wp_post_helper {
|
|
130 |
|
131 |
// Add Post
|
132 |
public function insert(){
|
133 |
-
|
134 |
-
return false;
|
135 |
-
|
136 |
-
$this->postid = 0;
|
137 |
-
$this->post->ID = 0;
|
138 |
-
$postid = wp_insert_post($this->post);
|
139 |
-
if ($postid && !is_wp_error($postid)) {
|
140 |
-
$this->postid = $postid;
|
141 |
-
$this->post->ID = $postid;
|
142 |
-
return $this->add_related_meta($postid) ? $postid : false;
|
143 |
-
} else {
|
144 |
-
$this->postid = $postid;
|
145 |
-
$this->post->ID = 0;
|
146 |
-
return false;
|
147 |
-
}
|
148 |
}
|
149 |
|
150 |
// Update Post
|
@@ -152,10 +139,12 @@ class wp_post_helper {
|
|
152 |
if (!isset($this->post))
|
153 |
return false;
|
154 |
|
155 |
-
$
|
156 |
-
$this->
|
157 |
-
|
158 |
-
|
|
|
|
|
159 |
if ($postid && !is_wp_error($postid)) {
|
160 |
$this->postid = $postid;
|
161 |
$this->post->ID = $postid;
|
@@ -200,17 +189,23 @@ class wp_post_helper {
|
|
200 |
$this->metas = array();
|
201 |
|
202 |
// add ACF Fields
|
203 |
-
foreach ($this->
|
204 |
$this->add_field($key, $val);
|
205 |
}
|
206 |
$this->fields = array();
|
207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
return true;
|
209 |
}
|
210 |
|
211 |
// Add Tag
|
212 |
public function add_tags($tags = array()){
|
213 |
-
$tags = is_array($tags) ? $tags : explode(',', $tags);
|
214 |
foreach ($tags as $tag) {
|
215 |
if (!empty($tag) && !array_search($tag, $this->tags))
|
216 |
$this->tags[] = $tag;
|
@@ -218,9 +213,9 @@ class wp_post_helper {
|
|
218 |
unset($tags);
|
219 |
|
220 |
if ($this->postid) {
|
221 |
-
$tags =
|
222 |
$this->tags = array();
|
223 |
-
return
|
224 |
}
|
225 |
}
|
226 |
|
@@ -234,7 +229,7 @@ class wp_post_helper {
|
|
234 |
$this->terms[$taxonomy][] = $term;
|
235 |
}
|
236 |
} else {
|
237 |
-
return wp_set_object_terms($this->postid, $terms, $taxonomy);
|
238 |
}
|
239 |
}
|
240 |
|
@@ -308,12 +303,43 @@ class wp_post_helper {
|
|
308 |
}
|
309 |
}
|
310 |
|
311 |
-
// Add Advanced Custom
|
312 |
public function add_field($field_key, $val){
|
313 |
-
if (
|
314 |
-
$this->
|
315 |
-
else
|
316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
}
|
318 |
}
|
319 |
|
34 |
|
35 |
private $postid = false;
|
36 |
private $attachment_id = array();
|
37 |
+
|
38 |
+
private $is_insert = true;
|
39 |
|
40 |
private $tags = array();
|
41 |
private $medias = array();
|
42 |
private $metas = array();
|
43 |
+
private $acf_fields = array();
|
44 |
+
private $cfs_fields = array();
|
45 |
private $media_count = 0;
|
46 |
private $terms = array();
|
47 |
|
67 |
$this->tags = array();
|
68 |
$this->medias = array();
|
69 |
$this->metas = array();
|
70 |
+
$this->acf_fields = array();
|
71 |
+
$this->cfs_fields = array();
|
72 |
$this->media_count = 0;
|
73 |
|
74 |
if (is_numeric($args)) {
|
75 |
$post = get_post(intval($args));
|
76 |
if ($post && isset($post->ID) && !is_wp_error($post)) {
|
77 |
+
$this->postid = $post->ID;
|
78 |
$this->post = $post;
|
79 |
return true;
|
80 |
} else {
|
101 |
$post_id = isset($args['ID']) ? $args['ID'] : $args['post_id'];
|
102 |
$post = get_post($post_id, 'ARRAY_A');
|
103 |
if (isset($post['ID'])) {
|
104 |
+
$this->postid = $post_id;
|
105 |
$this->post->ID = $post_id;
|
106 |
unset($post['ID']);
|
107 |
$this->set($post);
|
108 |
+
$this->is_insert = false;
|
109 |
}
|
110 |
unset($post);
|
111 |
}
|
123 |
$this->post = $post;
|
124 |
|
125 |
if (isset($args['post_tags'])) {
|
126 |
+
$this->add_tags($args['post_tags']);
|
|
|
|
|
|
|
|
|
127 |
}
|
128 |
|
129 |
return true;
|
131 |
|
132 |
// Add Post
|
133 |
public function insert(){
|
134 |
+
return $this->update();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
}
|
136 |
|
137 |
// Update Post
|
139 |
if (!isset($this->post))
|
140 |
return false;
|
141 |
|
142 |
+
if ($this->is_insert) {
|
143 |
+
$postid = wp_insert_post($this->post);
|
144 |
+
} else {
|
145 |
+
$postid = wp_update_post($this->post);
|
146 |
+
}
|
147 |
+
|
148 |
if ($postid && !is_wp_error($postid)) {
|
149 |
$this->postid = $postid;
|
150 |
$this->post->ID = $postid;
|
189 |
$this->metas = array();
|
190 |
|
191 |
// add ACF Fields
|
192 |
+
foreach ($this->acf_fields as $key => $val) {
|
193 |
$this->add_field($key, $val);
|
194 |
}
|
195 |
$this->fields = array();
|
196 |
|
197 |
+
// add CFS Fields
|
198 |
+
if (count($this->cfs_fields) > 0) {
|
199 |
+
$this->save_cfs_fields();
|
200 |
+
}
|
201 |
+
$this->fields = array();
|
202 |
+
|
203 |
return true;
|
204 |
}
|
205 |
|
206 |
// Add Tag
|
207 |
public function add_tags($tags = array()){
|
208 |
+
$tags = is_array($tags) ? $tags : explode( ',', trim( $tags, " \n\t\r\0\x0B," ) );
|
209 |
foreach ($tags as $tag) {
|
210 |
if (!empty($tag) && !array_search($tag, $this->tags))
|
211 |
$this->tags[] = $tag;
|
213 |
unset($tags);
|
214 |
|
215 |
if ($this->postid) {
|
216 |
+
$tags = $this->tags;
|
217 |
$this->tags = array();
|
218 |
+
return wp_set_post_tags($this->postid, $tags, $this->is_insert);
|
219 |
}
|
220 |
}
|
221 |
|
229 |
$this->terms[$taxonomy][] = $term;
|
230 |
}
|
231 |
} else {
|
232 |
+
return wp_set_object_terms($this->postid, $terms, $taxonomy, $this->is_insert);
|
233 |
}
|
234 |
}
|
235 |
|
303 |
}
|
304 |
}
|
305 |
|
306 |
+
// Add Advanced Custom Fields field
|
307 |
public function add_field($field_key, $val){
|
308 |
+
if (!function_exists('update_field')) {
|
309 |
+
$this->add_meta($field_key, $val);
|
310 |
+
} else {
|
311 |
+
if (!$this->postid) {
|
312 |
+
$this->acf_fields[$field_key] = $val;
|
313 |
+
} else {
|
314 |
+
return $val ? update_field($field_key, $val, $this->postid) : false;
|
315 |
+
}
|
316 |
+
}
|
317 |
+
}
|
318 |
+
|
319 |
+
// Add Custom Field Suite field
|
320 |
+
public function add_cfs_field($field_key, $val){
|
321 |
+
global $cfs;
|
322 |
+
if (!is_object($cfs) || !$cfs instanceof Custom_Field_Suite) {
|
323 |
+
$this->add_meta($field_key, $val);
|
324 |
+
} else {
|
325 |
+
if (!$this->postid) {
|
326 |
+
$this->cfs_fields[$field_key] = $val;
|
327 |
+
} else {
|
328 |
+
return $val ? $cfs->save(array($field_key=>$val), array('ID'=>$this->postid)) : false;
|
329 |
+
}
|
330 |
+
}
|
331 |
+
}
|
332 |
+
|
333 |
+
// Save Custom Field Suite fields
|
334 |
+
public function save_cfs_fields() {
|
335 |
+
global $cfs;
|
336 |
+
if (is_object($cfs) && $cfs instanceof Custom_Field_Suite && $this->postid && !is_wp_error($this->postid)) {
|
337 |
+
$cfs->save($this->cfs_fields,array('ID'=>$this->postid));
|
338 |
+
} else {
|
339 |
+
foreach ($this->cfs_fields as $key => $val) {
|
340 |
+
$this->add_meta($key, $val);
|
341 |
+
}
|
342 |
+
}
|
343 |
}
|
344 |
}
|
345 |
|