<?php
define( 'SITE_URL', $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'] );
define( 'DOING_AJAX', true );
define( 'WP_ADMIN', true );
/** Load WordPress Bootstrap */
require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
/** Allow for cross-domain requests (from the frontend). */
send_origin_headers();
/** Load WordPress Administration APIs */
require_once( ABSPATH . 'wp-admin/includes/admin.php' );
/** Load Ajax Handlers for WordPress Core */
require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
@header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
@header( 'X-Robots-Tag: noindex' );
send_nosniff_header();
nocache_headers();
do_action( 'admin_init' );
if ( !is_user_logged_in() ){
echo json_encode(array('errorcode'=>2,'msg'=>'登录超时!请重新登录!'));exit;
}
if(!isset($_POST['keywords'])||!$_POST['keywords']){
echo json_encode(array('errorcode'=>2,'msg'=>'请输入关键字!'));exit;
}
$tempArr=$_POST['keywords'];
$tagNameArr = array_filter($tempArr);//去空数组
foreach ($tagNameArr as $k => $v) {
$tagNameArr[$k] = trim($v);//去除两端空字符
}
$tagNameArr = array_unique($tagNameArr);//去除重复值
if ($tagNameArr && count($tagNameArr) > 0) {
$taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
global $wpdb;
if(isset($_POST['is_del'])){//删除关键词
foreach($tagNameArr as $vo){
$tag = array();
$slug=sanitize_title(stripslashes(wp_unslash($vo)));//去除特殊字符 大写并且转小写 别名
$term=get_term_by('slug',$slug,'post_tag');//获取关键词id
if($term){
wp_delete_term( $term->term_id, 'post_tag' );
}
}
do_action("wpfc_clear_all_cache", true);
echo json_encode(array('errorcode'=>1,'msg'=>'刪除成功!'));exit;
}else{//添加关键词
$level = 0;
$return_arr=array();
$term_id_arr=array();
$level=0;
$description='';
$parent='';
foreach($tagNameArr as $vo){
$tag = array();
$slug=sanitize_title(stripslashes(wp_unslash($vo)));//去除特殊字符 大写并且转小写 别名
//根据别名查询
$existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name,term_id FROM $wpdb->terms WHERE slug = %s", $slug), ARRAY_A );
if(!$existing_term){
$name=wp_unslash($vo);
$term_res = $wpdb->insert( $wpdb->terms, compact( 'name', 'slug' ));//添加关键词表
if(!$term_res){
echo json_encode(array('errorcode'=>2,'msg'=>'关键词添加失败,请稍后重试!'));exit;
}
$term_id= (int) $wpdb->insert_id;
$term_taxonomy_res= $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );//添加关键词分类数据
if(!$term_taxonomy_res){
echo json_encode(array('errorcode'=>2,'msg'=>'关键词分类添加失败,请稍后重试!'));exit;
}
$term_taxonomy_id = (int) $wpdb->insert_id;
$tag['term_id']=$term_id;
$tag['term_taxonomy_id']=$term_taxonomy_id;
$tag['count']=$level;
do_action("create_term", $term_id, $term_taxonomy_id, $taxonomy);
do_action("create_$taxonomy", $term_id, $term_taxonomy_id);
$term_id = apply_filters('term_id_filter', $term_id, $term_taxonomy_id);
clean_term_cache($term_id, $taxonomy);
do_action("created_term", $term_id, $term_taxonomy_id, $taxonomy);
do_action("created_$taxonomy", $term_id, $term_taxonomy_id);
}else{
$term_id=$existing_term['term_id'];
$term_taxonomy_res = $wpdb->get_row( $wpdb->prepare( "SELECT term_taxonomy_id,count FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id), ARRAY_A );
if(!$term_taxonomy_res){
$term_taxonomy_res= $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );//添加关键词分类数据
if(!$term_taxonomy_res){
echo json_encode(array('errorcode'=>2,'msg'=>'关键词分类添加失败,请稍后重试!'));exit;
}
$term_taxonomy_id = (int) $wpdb->insert_id;
}else{
$term_taxonomy_id=$term_taxonomy_res['term_taxonomy_id'];
}
$tag['term_id']=$term_id;
$tag['term_taxonomy_id']=$term_taxonomy_id;
$tag['count']=$term_taxonomy_res['count']?$term_taxonomy_res['count']:0;
}
$tbnm = 'wp_term_flag';
$is_tab= $wpdb->query('SHOW TABLES LIKE "' . $tbnm . '"');
if($is_tab){
$term_flag=$wpdb->get_var( $wpdb->prepare( "SELECT tfalg FROM wp_term_flag WHERE term_id = %d", $tag['term_id'] ) );
if(!$term_flag){
$tm=date('Y-m-d H:i:s');
$term_res = $wpdb->query($wpdb->prepare("INSERT INTO wp_term_flag (term,term_id,intime) VALUES (%s,%d,%s)",$vo,$tag['term_id'],$tm));
$term_id_arr[$tag['term_id']]=$vo;
}
}
$return_arr[]='<tr id="tag-"'.$tag['term_id'].' class="alternate"><th scope="row" class="check-column"><label class="screen-reader-text" for="cb-select-"'.$tag['term_id'].'>'.$vo.'</label><div class="cata"'.$tag['term_id'].'><input type="checkbox" name="delete_tags[]" value='.$tag['term_id'].' id="cb-select-"'.$tag['term_id'].'></div></th><td class="name column-name"><strong><a class="row-title" style="margin:0 20px 0 0" href="'.SITE_URL.'/'.$slug.'/'.'" target="_bank" title="查看分类页面">'.$vo.'</a></strong> <a class="row-title" href="'.SITE_URL.'/wp-admin/edit-tags.php?action=edit&taxonomy=post_tag&tag_ID='.$tag['term_id'].'&post_type=post">编辑</a><br><div class="row-actions"><span class="edit"><div style="float:left;" class="cata_"'.$tag['term_id'].'><a href="'.SITE_URL.'/wp-admin/edit-tags.php?action=edit&taxonomy=post_tag&tag_ID='.$tag['term_id'].'&post_type=post">编辑</a><span class="view"></span></div></span></div><div class="hidden" id="inline_"'.$tag['term_id'].'><div class="name">'.$vo.'</div><div class="slug">'.$slug.'</div><div class="parent">0</div></div></td><td class="description column-description"></td><td class="slug column-slug">'.$slug.'</td><td class="posts column-posts"><a href="'.SITE_URL.'/wp-admin/edit.php?tag='.$slug.'">'.$tag['count'].'</a></td></tr>';
}
echo json_encode(array('errorcode'=>1,'return_data'=>$return_arr));exit;
}
} else {
echo json_encode(array('errorcode'=>2,'msg'=>'请输入关键字!'));exit;
}