Szkielet skryptu to importu danych do bazy WordPress z dowolnej bazy MySQL
<?php /* Name: Import Data to WordPress Auhor: Paweł Kosmala Website: https://pawelkosmala.pl/ Version: 20200805 License: GNU General Public License */ function str2url($text) { $text = trim($text); $znaki = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'); $tmp = strtolower(str_replace(array("ę", "ó", "ą", "ś", "ł", "ż", "ź", "ć", "ń", "Ę", "Ó", "Ą", "Ś", "Ł", "Ż", "Ź", "Ć"), array("e", "o", "a", "s", "l", "z", "z", "c", "n", "E", "O", "A", "S", "L", "Z", "Z", "C"), $text)); $ile = strlen($tmp); $text = ""; $oz = ""; for ($i = 0; $i <= $ile; $i++) { $znak = substr($tmp, $i, 1); //echo $znak; if (in_array($znak, $znaki)) { $oz = $znak; $text.=$znak; } elseif ($oz != "-") { $text.="-"; $oz = "-"; } } return $text; } class WordPress { public $db=""; public function __construct() { $this->db=new mysqli(db::$wp_host,db::$wp_user,db::$wp_pass,db::$wp_db); $this->db->query("SET NAMES 'utf8'"); } private function returnId($sql,$id) { $result=$this->db->query($sql); $row=mysqli_fetch_assoc($result); return (int)@$row[$id]; } public function isPost($post_author, $post_date, $post_title, $post_content) { $sql="SELECT id FROM wp_posts WHERE " . "post_author='".$post_author."' " . "AND post_date='".$post_date."' " . "AND post_title='".$post_title."' " . "AND post_content='".$post_content."' "; $result=$this->db->query($sql); $row=mysqli_fetch_assoc($result); return (int)@$row["id"]; } public function addPost($post_author, $post_date, $post_title, $post_content, $post_name="") { $sql="INSERT INTO wp_posts " . "(post_author, post_date, post_title, post_content, post_name) " . "VALUES " . "('".$post_author."', '".$post_date."', '".$post_title."', '".$post_content."', '".$post_name."') "; $this->db->query($sql); } public function savePost($post_author, $post_date, $post_title, $post_content, $post_name="") { $postId=$this->isPost($post_author, $post_date, $post_title, $post_content); if ($postId) { echo $post_name."\n"; $sql="UPDATE wp_posts SET " ." post_author='".$post_author."', " ." post_date='".$post_date."', " ." post_title='".$post_title."', " ." post_content='".$post_content."', " ." post_name='".$post_name."' " . "WHERE id=".$postId; //echo $sql."\n"; die(); $this->db->query($sql); } else $this->addPost ($post_author, $post_date, $post_title, $post_content, $post_name=""); } public function saveImage($post_author,$nazwa, $fromUrl) { $sql="SELECT id FROM wp_posts WHERE " . "post_author='".$post_author."' " . "AND post_name='".$nazwa."' " . "AND post_type='attachment' " . "AND post_mime_type='image/jpeg' "; $postId=$this->returnId($sql, "id"); if (!$postId) { $year=date("Y"); $m=date("m"); file_put_contents(db::$wp_dirupload.$year."/".$m."/".$nazwa,file_get_contents($fromUrl)); $url=db::$wp_url."wp-content/uploads/".$year."/".$m."/".$nazwa; $sql="INSERT INTO wp_posts " . "(post_author, post_date, post_title, post_name, post_type, post_mime_type, guid, post_status ) " . "VALUES " . "('".$post_author."', '".date("Y-m-d H:i:s")."', '".$nazwa."', '".$nazwa."', 'attachment', 'image/jpeg', '".$url."', 'inherit') "; $this->db->query($sql); $sql="SELECT id FROM wp_posts WHERE " . "post_author='".$post_author."' " . "AND post_name='".$nazwa."' " . "AND post_type='attachment' " . "AND post_mime_type='image/jpeg' "; $postId=$this->returnId($sql, "id"); } return $postId; } public function saveThumbnail($postId, $photoId) { $sql="SELECT meta_id FROM wp_postmeta WHERE post_id=".$postId." AND meta_key='_thumbnail_id' AND meta_value='".$photoId."' LIMIT 1"; $metaId=$this->returnId($sql, "meta_id"); if ($metaId) { $sql="UPDATE wp_postmeta SET meta_value='".$photoId."' WHERE post_id=".$postId." AND meta_key='_thumbnail_id'"; } else { $sql="INSERT INTO wp_postmeta (post_id,meta_key,meta_value) VALUES (" . "'".$postId."'," . "'_thumbnail_id'," . "'".$photoId."'" . ") "; } $this->db->query($sql); } public function saveRedirection($fromUrl, $toUrl) { $sql="SELECT id FROM wp_redirection_items WHERE url='".$fromUrl."' LIMIT 1 "; $result=$this->db->query($sql); $row=mysqli_fetch_assoc($result); $id=(int)@$row["id"]; if (!$id) { $sql="INSERT INTO wp_redirection_items " . "(url, match_url, status, action_type, action_code, action_data, match_type) " . "VALUES " . "('".$fromUrl."', '".$fromUrl."', 'enabled', 'url', '301', '".$toUrl."', 'url') "; $this->db->query($sql); } } public function id2Url($postId) { $sql="SELECT post_name FROM wp_posts WHERE id=".$postId; $result=$this->db->query($sql); $row=mysqli_fetch_assoc($result); return @$row["post_name"]; } public function isTerm($name,$slug) { $sql="SELECT term_id FROM wp_terms WHERE name='".$name."' AND slug='".$slug."' LIMIT 1 "; $result=$this->db->query($sql); $row=mysqli_fetch_assoc($result); return (int)@$row['term_id']; } public function saveTerm($name,$slug, $description="", $taxonomy='category') { $termId=$this->isTerm($name, $slug); if (!$termId) { $sql="INSERT INTO wp_terms (name,slug) VALUES ('".$name."','".$slug."') "; $result=$this->db->query($sql); $termId=$this->isTerm($name, $slug); $sql="INSERT INTO wp_term_taxonomy (term_id,taxonomy,description) VALUES ('".$termId."', '".$taxonomy."', '".$description."') "; $result=$this->db->query($sql); } return $termId; } public function post2Term($postId, $termId) { $sql="SELECT object_id FROM wp_term_relationships WHERE object_id=".$postId." AND term_taxonomy_id=".$termId." LIMIT 1"; if (!$this->returnId($sql, "object_id")) { $sql="INSERT INTO wp_term_relationships (object_id,term_taxonomy_id) VALUES ('".$postId."','".$termId."') "; $this->db->query($sql); } } } class dbImport { public $db=""; public function __construct() { $this->db=new mysqli(db::$from_host,db::$from_user,db::$from_pass,db::$from_db); $this->db->query("SET NAMES 'utf8'"); } public function posts() { $sql="SELECT teksty.*, kategorie.name AS kategorieUrl " . "FROM teksty " . "LEFT JOIN kategorie ON (kategorie.id=teksty.kategorie_id) "; $result=$this->db->query($sql); $posts=array(); while ($row = mysqli_fetch_assoc($result)) { $posts[]=$row; } return $posts; } public function id2Kategorie($id) { $sql="SELECT * FROM kategorie WHERE id=".$id." "; $result=$this->db->query($sql); return mysqli_fetch_assoc($result); } public function zdjecie($id) { $sql="SELECT * FROM zdjecia WHERE id=".$id." "; $result=$this->db->query($sql); return mysqli_fetch_assoc($result); } public function tagi() { $sql="SELECT * FROM tagi "; $result=$this->db->query($sql); $tagi=array(); while ($row = mysqli_fetch_assoc($result)) { $tagi[]=$row; } return $tagi; } }
<?php /* Name: Import Data to WordPress Auhor: Paweł Kosmala Website: https://pawelkosmala.pl/ Version: 20200805 License: GNU General Public License */ class db { static $wp_host="localhost"; static $wp_db="wordpress"; static $wp_user="root"; static $wp_pass=""; static $post_author_default=1; static $wp_url=""; /*Docelowa domena WordPress */ static $wp_dirupload="/var/www/domena.pl"; /* ścieżka do instalacji WordPress*/ static $from_host=""; //Host bazy danych importu static $from_db=""; //nazwa bazy danych importu static $from_user=""; //użytkownik bazy danych importu static $from_pass=""; //hasło użytkownika bazy danych importu static $hostImportImage="https://jakasdomena.pl/useruploads/"; } $termsMaps=array(); include 'class.php'; $wp=new WordPress(); $import=new dbImport(); $posts=$import->posts(); foreach($posts as $post) { $postId=$wp->isPost(db::$post_author_default, $post['data'] , $post['title'], $post['text']); $zdjecie=$import->zdjecie($post["zdjecie_id"]); $photoId=$wp->saveImage(db::$post_author_default, $zdjecie["nazwa"], db::$hostImportImage.$zdjecie["nazwa"]); $wp->saveThumbnail($postId, $photoId); } ?>