public havePostInDatabase ( array $data = [] ) : integer | ||
$data | array | An associative array of post data to override default and random generated values. |
return | integer | post_id The inserted post ID. |
public function havePostInDatabase(array $data = [])
{
$postTableName = $this->grabPostsTableName();
$idColumn = 'ID';
$id = $this->grabLatestEntryByFromDatabase($postTableName, $idColumn) + 1;
$post = Post::makePost($id, $this->config['url'], $data);
$hasMeta = !empty($data['meta']) || !empty($data['meta_input']);
$hasTerms = !empty($data['terms']) || !empty($data['tax_input']);
$meta = [];
if ($hasMeta) {
$meta = !empty($data['meta']) ? $data['meta'] : $data['meta_input'];
unset($post['meta']);
unset($post['meta_input']);
}
$terms = [];
if ($hasTerms) {
$terms = !empty($data['terms']) ? $data['terms'] : $data['tax_input'];
unset($post['terms']);
unset($post['tax_input']);
}
$postId = $this->haveInDatabase($postTableName, $post);
if ($hasMeta) {
foreach ($meta as $meta_key => $meta_value) {
$this->havePostmetaInDatabase($postId, $meta_key, $meta_value);
}
}
if ($hasTerms) {
foreach ($terms as $taxonomy => $termNames) {
foreach ($termNames as $termName) {
$termId = $this->grabTermIdFromDatabase(['name' => $termName]);
if (empty($termId)) {
$termId = $this->grabTermIdFromDatabase(['slug' => $termName]);
}
if (empty($termId)) {
$termIds = $this->haveTermInDatabase($termName, $taxonomy);
$termId = reset($termIds);
}
$termTaxonomyId = $this->grabTermTaxonomyIdFromDatabase(['term_id' => $termId, 'taxonomy' => $taxonomy]);
$this->haveTermRelationshipInDatabase($postId, $termTaxonomyId);
$this->increaseTermCountBy($termTaxonomyId, 1);
}
}
}
return $postId;
}