-
Notifications
You must be signed in to change notification settings - Fork 0
/
taggenerator.php
316 lines (307 loc) · 14.8 KB
/
taggenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/*
Plugin Name: Tag Generator
Description: Generates tags for posts, using Yahoo and Yandex API. Supported text language: Russian, English, Polish, Ukrainian, German, French, Belorussian, Spanish, Italian, Bulgarian, Czech, Turkish, Romanian, Serbian.
Version: 0.1.1
Author: Nikitian
Author URI: http://nikitian.ru/
*/
class TagGenerator{
public $pluginuid = 'taggenerator';
public $default_language = null;
public $use_title = null;
public function TagGenerator(){
$this->RegisterHooks();
load_plugin_textdomain($this->pluginuid, PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)).'/languages');
}
private function autoTagSetup(){
add_action('publish_post', array(&$this, 'TagOne'), 20);
$this->autoTag = 1;
}
private function autoTagRemove(){
remove_action('publish_post', array(&$this, 'TagOne'), 20);
$this->autoTag = 0;
}
private function RegisterHooks(){
add_action('admin_menu', array(&$this, 'AdminMenu'));
$this->autoTag = get_option($this->pluginuid.'_autoTag');
if($this->autoTag===false){//Not specified
add_option($this->pluginuid.'_autoTag', 1);
$this->autoTagSetup();
}
elseif($this->autoTag){//1
$this->autoTagSetup();
}
elseif($this->autoTag!==false){
$this->autoTagRemove();
}
if($this->default_language===null){
$this->default_language = get_option($this->pluginuid.'_language');
if($this->default_language===false){
add_option($this->pluginuid.'_language','ru');
$this->default_language = 'ru';
}
}
if($this->use_title===null){
$this->use_title = get_option($this->pluginuid.'_use_title');
if($this->use_title===false){
add_option($this->pluginuid.'_use_title','1');
}
}
}
public function Prefences(){
echo'<h2>'.__('Tag Generation',$this->pluginuid).' <a href="?page='.$_REQUEST['page'].'" class="button" style="font-size: 20px;" title="'.__('Return back',$this->pluginuid).'">←</a></h2>';
echo'<h3>'.__('Prefences',$this->pluginuid).'</h3>';
include_once(dirname(__FILE__) . '/Yandex_Translate.php');
$translate = new Yandex_Translate();
$froms = $translate->yandexGet_FROM_Langs();
if(!empty($_POST) && array_key_exists('lang',$_POST)){
update_option($this->pluginuid.'_language', $_POST['lang']);
$this->default_language = $_POST['lang'];
update_option($this->pluginuid.'_use_title', $_POST['use_title']);
$this->use_title = $_POST['use_title'];
if($_POST['auto_tag']){
$this->autoTagSetup();
}
else{
$this->autoTagRemove();
}
update_option($this->pluginuid.'_autoTag', $_POST['auto_tag']);
echo'<div id="ajax-response"><div class="updated"><p>'.__('New propertys saved',$this->pluginuid).'</p></div></div>';
}
echo'<form action="" method="post">
'.__('Select language',$this->pluginuid).': <select name="lang">';
foreach($froms as $from){
echo'<option value="'.$from.'"'.($from==$this->default_language?' selected="selected"':'').'>'.__('Language '.$from,$this->pluginuid).'</option>';
}
echo'</select><br />
<input type="hidden" name="use_title" value="1" />
<label>
'.__('Use only text without title',$this->pluginuid).':
<input type="checkbox" name="use_title" value="0"'.($this->use_title?'':' checked="checked"').' />
</label><br />
<input type="hidden" name="auto_tag" value="0" />
<label>
'.__('Auto generate tag for new posts',$this->pluginuid).':
<input type="checkbox" name="auto_tag" value="1"'.($this->autoTag?' checked="checked"':'').' />
</label><br />
<input type="submit" value="'.__('Save',$this->pluginuid).'" />
<input type="button" value="'.__('Return back',$this->pluginuid).'" onclick="window.location.href=\'?page='.$_REQUEST['page'].'\';" />
</form>
';
}
public function TagOne($post_id = null,$text = ''){
$object = get_post($post_id);
if ($object == false || $object == null){
return false;
}
//if this post already has tags then no tagging needed
/*$has_tags = get_the_tags($object->ID);
if ($has_tags) {
unset($object);
if($text!=''){
return __('Nothing to do. Post already has tags.',$this->pluginuid);
}
return false;
}*/
$objectarr = (array)$object;
if($this->default_language!='en'&&!property_exists($this, 'translator')){
include_once(dirname(__FILE__) . '/Yandex_Translate.php');
$this->translator = new Yandex_Translate();
}
if($text==''){
//Build searched string
if($this->use_title){
$text = $objectarr['post_title'].' '.$objectarr['post_content'];
}
else{
$text = $objectarr['post_content'];
}
}
if($this->default_language!='en'){
$text = strip_tags(stripslashes($this->translator->yandexTranslate($this->default_language, 'en', strip_tags(stripslashes($text)))));
}
else{
$text = strip_tags(stripslashes($text));
}
$tags = $this->YahooYqlTags($text);
$ret = '';
if(sizeof($tags)>0){
$tags = array_unique($tags);
if($this->default_language!='en'){
$rutagsstr = $this->translator->yandexTranslate('en', $this->default_language, implode(',', $tags));
}
else{
$rutagsstr = implode(',', $tags);
}
$ret = $rutagsstr.'<br />';
wp_set_post_terms($post_id,$rutagsstr,'post_tag',true);
}
else{
$ret = __('Can\'t generate tags',$this->pluginuid);
}
if(trim(strip_tags($ret))==''){
$ret = __('Can\'t generate tags',$this->pluginuid);
}
return$ret;
}
public function AdminMenu() {
//manage page
add_management_page(
__('Tag Generator', $this->pluginuid)
, __('Tag Generator', $this->pluginuid)
, 'administrator'
, __FILE__
, array(&$this, 'TagAll')
);
}
public function UpdateDirect(){
echo'<h2>'.__('Tag Generation',$this->pluginuid).' <a href="?page='.$_REQUEST['page'].'" class="button" style="font-size: 20px;" title="'.__('Return back',$this->pluginuid).'">←</a></h2>';
echo'<h3>'.__('Custom load tags',$this->pluginuid).'</h3>';
echo'<div id="ajax-response"><div class="updated"><p>';
echo'#'.$_REQUEST['id'].'<br />';
echo $this->TagOne($_REQUEST['id']);
echo'</p></div></div>';
echo'<script type="text/javascript">
var timeout = setTimeout(function(){
var url = "'.$_REQUEST['return'].'";
/*alert("we go to url "+url);*/
window.location.href = url;
},5000);
</script>
<a class="button" href="'.$_REQUEST['return'].'" onclick="clearTimeout(timeout);return true;">'.__('Return back',$this->pluginuid).'</a>
';
}
public function ListPosts(){
echo'<h2>'.__('Tag Generation',$this->pluginuid).' <a href="?page='.$_REQUEST['page'].'" class="button" style="font-size: 20px;" title="'.__('Return back',$this->pluginuid).'">←</a></h2>';
echo'<h3>'.__('List posts',$this->pluginuid).'</h3>';
global $wpdb;
$onpage = 20;
$countdb = $wpdb->get_results("SELECT count(*) as c FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'",ARRAY_A);
$count = $countdb[0]['c'];
$posts = $wpdb->get_results("SELECT id FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post' order by id desc limit ".intval($_REQUEST['paged']*$onpage).",".$onpage,ARRAY_A);
echo'<a class="button" href="?page='.$_REQUEST['page'].'">'.__('Load all',$this->pluginuid).'</a><br />';
echo'<table class="wp-list-table widefat fixed posts" cellspacing="0">';
echo'<thead>
<tr>
<th id="cb" class="manage-column column-comments">#</th>
<th class="manage-column column-cb column-title sortable desc">'.__('Title',$this->pluginuid).'</th>
<th class="manage-column column-cb column-tags">'.__('Tags',$this->pluginuid).'</th>
<th class="manage-column column-cb column-tags">'.__('Action',$this->pluginuid).'</th>
</tr>
</thead>';
echo'<tbody id="the-list">';
$i=0;
foreach($posts as $post){
$i++;
$object = get_post($post['id']);
$has_tags = get_the_tags($object->ID);
echo'<tr>
<td>'.$object->ID.'</td>
<th>'.$object->post_title.'</th>
<td>';
if($has_tags){
foreach($has_tags as $tag){
echo'<a href="/wp-admin/edit.php?tag='.$tag->slug.'">'.$tag->name.'</a> ';
}
}
echo '</td>
<td><a class="button" href="?subact=UpdateDirect&id='.$object->ID.'&page='.$_REQUEST['page'].'&return='.urlencode($_SERVER['REQUEST_URI']).'">'.__('Load tags from Yahoo',$this->pluginuid).'</a></td>
</tr>';
}
echo'</tbody></table>';
$get = $_GET;
if(array_key_exists('paged',$_GET)){
unset($get['paged']);
}
echo '<div class="tablenav bottom"><div class="tablenav-pages">'.paginate_links(array(
'current'=>(array_key_exists('paged',$_REQUEST)?$_REQUEST['paged']:1),
'total'=>floor($count/$onpage),
'add_args'=>$get,
'format'=>'?paged=%#%',
'end_size'=>$onpage,
)).'</div></div>';
}
public function TagAll(){
if(array_key_exists('subact',$_REQUEST) && method_exists($this,$_REQUEST['subact'])){
call_user_method($_REQUEST['subact'], $this);
return;
}
echo'<h2>'.__('Tag Generation',$this->pluginuid).'</h2>';
$byiter = 5;
global $wpdb;
$countdb = $wpdb->get_results("SELECT count(*) as c FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'",ARRAY_A);
$count = $countdb[0]['c'];
echo __('Count posts',$this->pluginuid).': '.$count.'<br />';
if(array_key_exists('action',$_REQUEST) && $_REQUEST['action']=='gotag'){
if(array_key_exists('lastiter',$_REQUEST)){
$lastiter = $_REQUEST['lastiter'];
}
else{
$lastiter = 0;
}
if($lastiter>=$count){
_e('all work done',$this->pluginuid);
echo '<a class="button" href="?page='.$_REQUEST['page'].'&action=gotag" onclick="return confirm(\''.__('Really load tags for all posts?',$this->pluginuid).'\');">'.str_replace('##COUNT##',$count,__('Load tags for all ##COUNT## posts',$this->pluginuid)).'</a><br />';
}
else{
$results = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post' order by id desc limit ".intval($lastiter).",".$byiter,ARRAY_A);
$lastiter+=$byiter;
echo ($count>$lastiter?($count-$lastiter):'0').' '.__('left',$this->pluginuid).'<br />';
foreach($results as $result){
echo'#'.$result['ID'].'. '.$result['post_title'].': <pre style="padding-left:20px;">Tags: ';
if($this->use_title){
$text = $result['post_title'].' '.$result['post_content'];
}
else{
$text = $result['post_content'];
}
echo $this->TagOne($result['ID'],$text);
echo'</pre><br />';
}
echo'<script type="text/javascript">
var timeout = setTimeout(function(){
var url = "tools.php?page='.$_REQUEST['page'].'&action='.$_REQUEST['action'].'&lastiter='.$lastiter.'";
/*alert("we go to url "+url);*/
window.location.href = url;
},5000);
</script>
<a class="button" href="?page='.$_REQUEST['page'].'" onclick="clearTimeout(timeout);return true;">'.__('Stop',$this->pluginuid).'</a>
';
}
return;
}
echo '<a class="button" href="?page='.$_REQUEST['page'].'&action=gotag" onclick="return confirm(\''.__('Really load tags for all posts?',$this->pluginuid).'\');">'.str_replace('##COUNT##',$count,__('Load tags for all ##COUNT## posts',$this->pluginuid)).'</a><br />';
echo '<a class="button" href="?page='.$_REQUEST['page'].'&subact=ListPosts">'.__('Custom load tags from list posts',$this->pluginuid).'</a><br />';
echo '<a class="button" href="?page='.$_REQUEST['page'].'&subact=Prefences">'.__('Prefences',$this->pluginuid).'</a><br />';
}
private function YahooYqlTags($text) {
$text = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', stripslashes($text));
if (strlen($text) > 1000) {
$text = substr($text, 0, 1000);
$text = iconv('ISO-8859-1', 'ISO-8859-1//TRANSLIT', $text);
}
$url = 'http://query.yahooapis.com/v1/public/yql';
$fields = 'q=' . urlencode('select * from search.termextract where context="'.str_replace('"',' ',$text).'"') . '&format=json&diagnostics=true';
$vch = curl_init();
curl_setopt($vch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1");
curl_setopt($vch, CURLOPT_URL, $url.'?'.$fields);
curl_setopt($vch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($vch, CURLOPT_CONNECTTIMEOUT, 5);
$vurlcontents = curl_exec($vch);
$vhttp_code = curl_getinfo($vch, CURLINFO_HTTP_CODE);
if ($vhttp_code !== 200) {
//throw new Exception('Response code '.$vhttp_code);
echo __('Wrong response code',$this->pluginuid).': '.$vhttp_code.PHP_EOL;
return array();
}
curl_close($vch);
unset($vch);
$arr = json_decode($vurlcontents, true);
if (array_key_exists('results', $arr['query']) && array_key_exists('Result', $arr['query']['results'])) {
return $arr['query']['results']['Result'];
}
return array();
}
}
$TagGenerator = new TagGenerator();