/** * Save histoy in the post-type `acsv-log`. * * @param array $inserted_posts Inserted IDs. * @return string ID. * @since 0.1.0 */ public static function save_history($inserted_posts) { if (defined('WP_CLI') && WP_CLI) { $from = 'WP-CLI'; } else { $from = 'admin screen'; } $uid = get_current_user_id(); $post = array('post_author' => $uid, 'post_title' => 'Imported from ' . $from . '.', 'post_type' => 'acsv-log', 'post_status' => 'publish', 'post_name' => self::get_log_name($inserted_posts)); $helper = new \Megumi\WP\Post\Helper($post); $post_id = $helper->insert(); update_post_meta($post_id, '_import_log', serialize($inserted_posts)); }
/** * Insert post * * @param array $post_object The post object. * @return mixed The post id or WP_Error object. * @since 0.1.0 */ public static function insert_post($post) { // unset all empty fields foreach ($post as $key => $value) { if (!is_array($value) && !strlen($value)) { unset($post[$key]); } elseif (is_array($value) && !count($value)) { unset($post[$key]); } } // insert and set category if (isset($post['post_category']) && is_array($post['post_category']) && count($post['post_category'])) { $post['post_category'] = wp_create_categories($post['post_category']); } if (isset($post['post_date']) && $post['post_date']) { $post['post_date'] = date("Y-m-d H:i:s", strtotime($post['post_date'])); } // setup author if (isset($post['post_author']) && !intval($post['post_author'])) { $field = apply_filters('acsv_get_user_by_field', 'login'); $u = get_user_by($field, $post['post_author']); if ($u) { $post['post_author'] = $u->ID; } else { unset($post['post_author']); } } // setup post ID if (isset($post['ID']) && !intval($post['ID'])) { unset($post['ID']); } // set default to the post. foreach (Defaults\Config::get_post_defaults() as $key => $value) { if (!isset($post[$key])) { $post[$key] = $value; } } $helper = new \Megumi\WP\Post\Helper($post); $post_id = $helper->insert(); if (is_wp_error($post_id)) { return $post_id; } else { do_action('acsv_after_insert_post', $post_id, $post, $helper); return $post_id; } }