public function save_id() { $verbose = Config::$env == "dev" ? true : false; $id = null; if ($verbose) { Log::log("<blue>Creating the category `" . $this->name . "`.."); } $slug = Database::escape(strtolower(App::clean($this->name))); $query = "INSERT INTO `" . Config::$db_name . "`.`" . Config::$wp_prefix . "terms`( `name`, `slug`, `term_group` ) VALUE( '" . Database::escape($this->name) . "', '" . $slug . "', 0 )"; $exec = Database::exec($query); if ($exec === false) { Log::log("<red>Cannot save the category:"); Log::log("<red>[" . Database::$conn->errno . "] " . Database::$conn->error); Log::log("<red>" . $query); exit; } else { $term_id = Database::$conn->insert_id; if ($verbose) { Log::log("<green>Created the category `" . $this->name . "` with the slug `" . $slug . "` and ID '" . $term_id . "'."); } } $query = "INSERT INTO `" . Config::$db_name . "`.`" . Config::$wp_prefix . "term_taxonomy`( `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUE( '" . $term_id . "', 'category', '', 0, 0 );"; $exec = Database::exec($query); if ($exec === false) { Log::log("<red>Cannot save the taxonomy:"); Log::log("<red>[" . Database::$conn->errno . "] " . Database::$conn->error); Log::log("<red>" . $query); exit; } else { if ($verbose) { Log::log("<green>Taxonomy created."); } $tax_id = Database::$conn->insert_id; } return [$term_id, $tax_id]; }
public function save() { $verbose = Config::$env == "dev" ? true : false; $post_save_query = $this->get_query(); $post_saved = Database::exec($post_save_query); if ($post_saved === false) { Log::log("<red>Cannot save the article:"); Log::log("<red>[" . Database::$conn->errno . "] " . Database::$conn->error); Log::log("<red>" . $post_save_query); exit; } $this->post_id = Database::$conn->insert_id; if ($verbose) { Log::log("<green>Article with ID [" . $this->post_id . "] saved."); } foreach ($this->category as $cat_key => $cat) { $cat->check_exists(); $relation_query = "INSERT INTO `" . Config::$db_name . "`.`" . Config::$wp_prefix . "term_relationships` ( `object_id`, `term_taxonomy_id`, `term_order` ) VALUE ( " . $this->post_id . ", " . $cat->tax_id . ", 0 )"; $relation_exec = Database::exec($relation_query); if ($relation_exec === false) { Log::log("<red>Cannot save the link between the article and the taxonomy:"); Log::log("<red>[" . Database::$conn->errno . "] " . Database::$conn->error); Log::log("<red>" . $relation_query); exit; } $count_query = "UPDATE `" . Config::$db_name . "`.`" . Config::$wp_prefix . "term_taxonomy` SET count = count +1 WHERE `term_taxonomy_id` = " . $cat->tax_id . ";"; $count_exec = Database::exec($count_query); if ($count_exec === false) { Log::log("<red>Cannot add the article to the counter:"); Log::log("<red>[" . Database::$conn->errno() . "] " . Database::$conn->error); Log::log("<red>" . $count_query); exit; } } Log::log("<green>Article #" . $this->post_id . " saved with its categories."); }