File "ai_api.php"

Full Path: /www/wwwroot/shphe-en.com/admin/ai_api.php
File size: 5.72 KB
MIME-type: --
Charset: utf-8

<?php
require_once( dirname(__FILE__) . '/../wp-load.php' );

global $wpdb;


$type = $_GET['w'];
switch($type){
    case 'cat_tags'://产品分类和tag列表
        $tags_tmp = get_terms('post_tag',['hide_empty'=>false]);
        $tags = [];
        if($tags_tmp){
            foreach ($tags_tmp as $v){
                $tmp['id'] = $v->term_id;
                $tmp['name'] = $v->name;
                $tags[] = $tmp;
            }
        }
        $cats = get_categories(['hide_empty'=>false]);
        $catgory = [];
        if($cats){
            foreach ($cats as $v){
                if(!in_array($v->slug,['featured','featured-products'])){
                    $tmp['id'] = $v->term_id;
                    $tmp['name'] = $v->name;
                    $catgory[] = $tmp;
                }
            }
        }
        $data['tags'] = $tags;
        $data['cats'] = $catgory;
        print_r(json_encode($data));exit;
        break;
    case 'post_pages'://产品和页面
        $page = isset($_GET['page'])?trim($_GET['page']):1;
        $pagesize = isset($_GET['pagesize'])?trim($_GET['pagesize']):10;
        $start = $pagesize*($page-1);
        $count_posts = $wpdb->get_row("select count(*) as cnt from wp_posts where post_date_gmt!='2016-01-08 00:00:00' and post_type='post' and post_status='publish'",ARRAY_A);
        $data['products_count'] = $count_posts['cnt'];
        $arr = [];
        $posts = $wpdb->get_results("select ID,post_title from wp_posts where post_date_gmt!='2016-01-08 00:00:00' and post_type='post' and post_status='publish' order by ID DESC limit $start,$pagesize");
        if($posts){
            foreach ($posts as $post){
                $cats_post = wp_get_post_terms($post->ID,'category');
                $category = 'Products';
                $cat_slug = [];
                if($cats_post){
                    $cat_slug = array_column(json_decode(json_encode($cats_post),true),'slug');
                }
                if($cat_slug){
                    $flg_featured = 0;
                    foreach ($cat_slug as $slug){
                        if(in_array($slug,['featured'])){
                            $flg_featured = 1;
                        }
                    }
                    if($flg_featured == 1){
                        continue;
                    }
                }
                $tmp_cats = [];
                foreach ($cats_post as $v){
                    if($v->slug != 'products'){
                        $tmp_cats[] = $v;
                    }
                }
                if(count($tmp_cats) == 1){
                    $category = $tmp_cats[0]->name;
                }
                if(count($tmp_cats) == 2){
                    if($tmp_cats[0]->parent == $tmp_cats[1]->term_id){
                        $category = $tmp_cats[0]->name;
                    }else{
                        $category = $tmp_cats[1]->name;
                    }
                }
                $tmp['i'] = $post->ID;
                $tmp['t'] = $post->post_title;
                $tmp['c'] = $category;
                $arr[] = $tmp;
            }
        }
        $data['products'] = $arr;
        
        $ar_pages = [];
        query_posts(array( 'post_type' => 'page','showposts' => 500) ); while (have_posts()) : the_post();
			$tmp_pg['i'] = $post->ID;
			$tmp_pg['t'] = $post->post_title;
			$tmp_pg['c'] = '';
			$ar_pages[] = $tmp_pg;
		endwhile; wp_reset_query();
        $data['pages'] = $ar_pages;
        echo json_encode($data);exit;
        break;
    case 'edit_tdk'://添加ai标签
        $is_first = isset($_GET['first'])?1:0;
        $table_name = $wpdb->prefix . "tdks";
        $is_use_ai = 1;
        if($is_first == 1){
            use_ai($is_use_ai);
            if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
                $sql = "CREATE TABLE `".$table_name."` (
            		  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
                      `type` int(11) unsigned NOT NULL DEFAULT '0',
                      `pid` int(11) unsigned NOT NULL DEFAULT '0',
                      `page_title` varchar(255) DEFAULT NULL,
                      `meta_keywords` text DEFAULT NULL,
                      `meta_description` text DEFAULT NULL,
                      `content_title` varchar(255) DEFAULT NULL,
                      `content_description` text DEFAULT NULL,
                      `ctime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
                      PRIMARY KEY (`id`)
            		) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8";	 
            	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
                dbDelta($sql);
            }
        }
        $insertt_data['type'] = $_POST['type'];
        $insertt_data['pid'] = $_POST['pid'];
        $insertt_data['page_title'] = $_POST['page_title'];
        $insertt_data['meta_keywords'] = $_POST['meta_keywords'];
        $insertt_data['meta_description'] = $_POST['meta_description'];
        $insertt_data['content_title'] = $_POST['content_title'];
        $insertt_data['content_description'] = $_POST['content_description'];
        $insertt_data['ctime'] = date("Y-m-d H:i:s");
        
        $wpdb->insert($table_name,$insertt_data,array('%s','%s','%s','%s','%s','%s','%s','%s'));
		echo json_encode(['code'=>200,'msg'=>'success']);
        break;
    case 'update_tdk_ai_use':
        $is_use_ai = isset($_GET['is_user_ai'])?(int)$_GET['is_user_ai']:0;
        if($is_use_ai > 0){
            use_ai($is_use_ai);
        }
        echo 200;exit;
        break;
    default:
        echo [];exit;
}

//是否使用AI tdk
function use_ai($num=1){
    $has_option = get_option('tdk_use_ai');
    if($has_option){
        update_option('tdk_use_ai',$num);
    }else{
        add_option('tdk_use_ai',$num);
    }
}
?>