博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wordpress文章发布接口开发
阅读量:6405 次
发布时间:2019-06-23

本文共 5811 字,大约阅读时间需要 19 分钟。

hot3.png

1.代码懒得细看,先打上sql日志。

2.经过分析主要操作了posts,terms,term_taxonomy,term_relationships, postmeta,options这几个表,首先去掉postmeta,options这两个表的日志,主要记录后台手动编辑记录,用处不大,不需要处理。

3.posts是文章主表,terms是标签表(也包括菜单栏目等),term_taxonomy是标签详情表,term_relationships是文章,标签,栏目id关联表

4.更新逻辑是:

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

2)查询items表标签是否存在,存在返回标签id,不存在插入标签,返回标签id

3)向term_taxonomy插入分类,统计等记录

4)向term_relationships插入文章,栏目关联记录和文章标签关联记录

 

下面是代码,已经过自测

 

<?php

class db{

        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类其实就是一个单列。

转载于:https://my.oschina.net/u/554660/blog/756331

你可能感兴趣的文章
【Vue】vue.js常用指令
查看>>
NFS学习
查看>>
MySql常用命令总结
查看>>
又一年...
查看>>
文件上传框的美化+预览+ajax
查看>>
Linux VFS
查看>>
ext不能选中复制属性_如何实现Extjs的grid单元格只让选择(即可以复制单元格内容)但是不让修改?...
查看>>
python中print的作用*8、不能+8_在 Python 3.x 中语句 print(*[1,2,3]) 不能正确执行。 (1.0分)_学小易找答案...
查看>>
python 生成html代码_使用Python Markdown 生成 html
查看>>
axure如何导出原件_Axure 教程:轻松导出图标字体所有图标
查看>>
laravel input值必须不等于0_框架不提供,动手造一个:Laravel表单验证自定义用法...
查看>>
cad填充图案乱理石_太快了吧!原来大神是这样用CAD图案填充的
查看>>
activator.createinstance 需要垃圾回收么_在垃圾回收器中有哪几种判断是否需要被回收的方法...
查看>>
rocketmq 消息指定_RocketMQ入坑系列(一)角色介绍及基本使用
查看>>
redis zset转set 反序列化失败_掌握好Redis的数据类型,面试心里有底了
查看>>
p图软件pⅰc_娱乐圈最塑料的夫妻,P图永远只P自己,太精彩了吧!
查看>>
jenkins 手动执行_Jenkins 入门
查看>>
怎么判断冠词用a还是an_葡语干货 | 葡萄牙语冠词用法整理大全
查看>>
js传参不是数字_JS的Reflect学习和应用
查看>>
三个不等_数学一轮复习05,从函数观点看方程与不等式,记住口诀与联系
查看>>