wordpress文章發佈接口開發

1.代碼懶得細看,先打上sql日誌。php

2.通過分析主要操做了posts,terms,term_taxonomy,term_relationships, postmeta,options這幾個表,首先去掉postmeta,options這兩個表的日誌,主要記錄後臺手動編輯記錄,用處不大,不須要處理。mysql

3.posts是文章主表,terms是標籤表(也包括菜單欄目等),term_taxonomy是標籤詳情表,term_relationships是文章,標籤,欄目id關聯表sql

4.更新邏輯是:post

1)向文章主表posts插入文章,返回文章idfetch

2)查詢items表標籤是否存在,存在返回標籤id,不存在插入標籤,返回標籤idui

3)向term_taxonomy插入分類,統計等記錄this

4)向term_relationships插入文章,欄目關聯記錄和文章標籤關聯記錄日誌

 

下面是代碼,已通過自測ip

 

<?php
class db{get

        private static $db;
        public function __construct(){

        }

        public static function getInstance(){
                if(!self::$db){
                        self::$db = self::connectdb();
                }

                return self::$db;
        }


        private static function connectdb(){
                $config = include COLLECTOR_DIR.'/conf/db.config.php';
                $db = new mysqli($config['host'], $config['user'], $config['password'], $config['db']);
                !$db && die('Error,cannot connect database!');
                $db->query("set names utf8");
                return $db;
        }

}

<?php
class postInterface{

    private $db;
    private static $term_taxonomy_id;

    public function __construct(){
        $this->db = db::getInstance();
        self::$term_taxonomy_id = [];
    }

    public function getPost($post_title){
        $sql = "SELECT ID FROM `pn_posts` WHERE `post_title`='{$post_title}' LIMIT 0, 1";
        
        DEBUG && print($sql)."\n";

        $result = $this->db->query($sql);

        $row = $result->fetch_assoc();

        return $row['ID'];
    }


    public function insertPost($post_title, $post_name, $post_content, $post_date){
        $post_id =$this->getPost($post_title);
        if($post_id){
            return $post_id;
        }


        $sql = "INSERT IGNORE INTO `pn_posts` (`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_content_filtered`, `post_title`, `post_excerpt`, `post_status`, `post_type`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_parent`, `menu_order`, `post_mime_type`, `guid`) VALUES (1, '{$post_date}', '{$post_date}', '{$post_content}', '', '{$post_title}', '', 'publish', 'post', 'open', 'open', '', '{$post_name}', '', '', '{$post_date}', '{$post_date}', 0, 0, '', '');";

        DEBUG && print($sql)."\n";

        $this->db->query($sql);

        $post_id = $this->db->insert_id;

        $sql = "UPDATE `pn_posts` SET `guid`='/?p={$this->db->insert_id}' WHERE `ID`='{$this->db->insert_id}' LIMIT 1";
        DEBUG && print($sql."\n");
        $this->db->query($sql);
        
        return $post_id;
    }


    public function getTagInfo($tag){
        $sql = "SELECT term_taxonomy_id,term_id FROM `pn_term_taxonomy` WHERE term_id IN (SELECT term_id FROM `pn_terms` WHERE name = '{$tag}') AND taxonomy = 'post_tag' LIMIT 0, 1";
        DEBUG && print($sql."\n");
        $result = $this->db->query($sql);
        $row = $result->fetch_assoc();
        return $row;
    }

 


    public function insertTag($post_id, $cateid, $tag, $slug){
        $tag = trim($tag);    
        $tagInfo = $this->getTagInfo($tag);

        if(empty($tagInfo)){
            $tagInfo = [];
                
            $sql = "INSERT INTO `pn_terms` (`name`, `slug`, `term_group`) VALUES ('{$tag}', '{$slug}', 0);";
            DEBUG && print($sql."\n");
            $this->db->query($sql);

            $tagInfo['term_id'] = $this->db->insert_id;
        
            $sql="INSERT IGNORE INTO `pn_term_taxonomy` (`term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES ({$tagInfo['term_id']}, 'post_tag', '', 0, 0);";
                    DEBUG && print($sql."\n");
            $this->db->query($sql);

                    $tagInfo['term_taxonomy_id'] = $this->db->insert_id;
        }

 

        $sql = "INSERT IGNORE INTO `pn_term_relationships` (`object_id`, `term_taxonomy_id`) VALUES ({$post_id}, {$tagInfo['term_taxonomy_id']});";
        DEBUG && print($sql."\n");
        $this->db->query($sql);

        $term_taxonomy_id = $this->getTermTaxonomyId($cateid);
        $sql = "INSERT IGNORE INTO `pn_term_relationships` (`object_id`, `term_taxonomy_id`) VALUES ({$post_id}, {$term_taxonomy_id});";
        DEBUG && print($sql."\n");
        $this->db->query($sql);
        
        $num = $this->getTagPostNum($tagInfo['term_id']);
        $sql = "UPDATE `pn_term_taxonomy` SET `count`={$num} WHERE `term_id` = {$tagInfo['term_id']}";
        DEBUG && print($sql."\n");
        $this->db->query($sql);

        $num = $this->getCategoryPostNum($cateid);
        $sql ="UPDATE `pn_term_taxonomy` SET `count` = {$num} WHERE `term_id` = {$cateid};";
        DEBUG && print($sql."\n");
        $this->db->query($sql);

    }

    public function getCategoryPostNum($cateid){
        $term_taxonomy_id = $this->getTermTaxonomyId($cateid);

        $sql = "SELECT COUNT(*) AS num FROM `pn_term_relationships` WHERE `term_taxonomy_id` = {$term_taxonomy_id}  AND `object_id` != {$cateid}";
        DEBUG && print($sql."\n");
        $result = $this->db->query($sql);

        $row = $result->fetch_assoc();

        return (int)$row['num'];

    }

    public function getTagPostNum($tagid){
                $term_taxonomy_id = $this->getTermTaxonomyId($tagid);

                $sql = "SELECT COUNT(*) AS num FROM `pn_term_relationships` WHERE `term_taxonomy_id` = {$term_taxonomy_id}  AND `object_id` != {$tagid}";
                DEBUG && print($sql."\n");
                $result = $this->db->query($sql);

                $row = $result->fetch_assoc();

                return (int)$row['num'];

        }

    public function getTermTaxonomyId($termid){
        if(isset(self::$term_taxonomy_id[$termid])){
            return self::$term_taxonomy_id[$termid];
        }

        $sql = "SELECT `term_taxonomy_id` FROM `pn_term_taxonomy` WHERE `term_id` = {$termid} LIMIT 0,1";
        DEBUG && print($sql."\n");
        $result = $this->db->query($sql);

                $row = $result->fetch_assoc();
        
        self::$term_taxonomy_id[$termid] = $row['term_taxonomy_id'];
                return $row['term_taxonomy_id'];
    
    }


}

/*
include 'db.class.php';

$pi = new postInterface();

$cateid = 1;
$post_title = 'AAAAAABBBBB';
$post_content = 'AAAAAA,BBBBBB';
$post_tag1 = 'AA';
$post_tag2 = 'BB';
$post_date = date("Y-m-d H:i:s");

$post_id = $pi->insertPost($post_title, $post_content, $post_date);
$pi->insertTag($post_id, $cateid, $post_tag1);
$pi->insertTag($post_id, $cateid, $post_tag2);


$post_title = 'AAAAAAABBBBBB';
$post_content = 'AAAAAAA,BBBBBBB';
$post_tag1 = 'AA';
$post_tag2 = 'BB';
$post_date = date("Y-m-d H:i:s");

$post_id = $pi->insertPost($post_title, $post_content, $post_date);
$pi->insertTag($post_id, $cateid, $post_tag1);
$pi->insertTag($post_id, $cateid, $post_tag2);
*/

 

 

注:原版的有bug,代碼已更新。db類其實就是一個單列。

相關文章
相關標籤/搜索