function xt_admin_ajax_tag_add() { $result = array('code' => 0, 'msg' => '', 'result' => array()); if (!current_user_can('manage_options')) { exit('您无权操作此功能'); } if (!isset($_POST['title']) || empty($_POST['title'])) { exit('未指定分类标题'); } if ($result['code'] == 0) { $title = $_POST['title']; $cid = $_POST['catalog']; $sort = isset($_POST['sort']) ? absint($_POST['sort']) : 100; $data = array(); $data['title'] = $title; $data['sort'] = $sort; $data['is_hot'] = 0; if (!empty($data)) { $tag = xt_get_tag(0, $title); $tagSort = 100; $id = 0; if (empty($tag)) { $id = xt_new_tag($data); $tagSort = xt_tag_default_sort($title); } else { $id = $tag->id; $tagSort = $tag->sort; } if ($id > 0 && $cid > 0 && xt_catalog_exit($cid)) { if (!xt_tag_catalog_exit($id, $cid)) { xt_new_tag_catalog(array('id' => $id, 'cid' => $cid, 'sort' => $tagSort)); } } } exit; } exit('未知错误'); }
function xt_update_share_tags($tags, $cids = array()) { global $wpdb; foreach ($tags as $tag) { //第一步:判断标签是否已存在 //存在该标签:1.修改计数2.如指定cid,判断是否已有关系,无则插入关系表,有则不处理 //不存在该标签:1.插入标签2.如指定cid,则插入关系表 $sql = $wpdb->prepare("SELECT id,sort FROM " . XT_TABLE_SHARE_TAG . " WHERE title =%s", $tag); $tagRow = $wpdb->get_row($sql); if (!empty($tagRow)) { //当前标签已存在 $tag_id = $tagRow->id; $tag_sort = $tagRow->sort; $wpdb->query($wpdb->prepare("UPDATE " . XT_TABLE_SHARE_TAG . " SET count=(count+1) WHERE id =%d", $tag_id)); if (!empty($cids)) { foreach ($cids as $cid) { if (!xt_tag_catalog_exit(absint($tag_id), absint($cid))) { xt_new_tag_catalog(array('id' => absint($tag_id), 'cid' => absint($cid), 'sort' => $tag_sort)); } else { $wpdb->query("UPDATE " . XT_TABLE_SHARE_TAG_CATALOG . " SET count=count+1 WHERE id=" . absint($tag_id) . " AND cid=" . absint($cid)); } } } } else { if ($tag_id = xt_new_tag(array('title' => $tag))) { if (!empty($cids)) { foreach ($cids as $cid) { xt_new_tag_catalog(array('id' => absint($tag_id), 'cid' => absint($cid), 'sort' => xt_tag_default_sort($tag))); } } } } } }