<?php
register_activation_hook(__FILE__, 'unikalizator_set_options');
register_deactivation_hook(__FILE__, 'unikalizator_unset_options');
add_action('admin_menu', 'unikalizator_admin_menu');
add_filter('content_save_pre', 'unikalizator_do_action', 0);
$unik_syn_table = unik_get_syn_table();
function unikalizator_set_options () {
global $wpdb;
add_option('unik_version', "1.0");
add_option('unik_mashup_sentence', 0);
add_option('unik_mashup_paragraph', 0);
add_option('unik_pressing', 0);
add_option('unik_pressing_level', 70);
add_option('unik_ruslat', 0);
add_option('unik_ruslat_level', 5);
add_option('unik_synonymizer', 0);
add_option('unik_synonymizer_level', 3);
$unik_syn_table = unik_get_syn_table();
$charset_collate = '';
if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') )
$charset_collate = "DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
if($wpdb->get_var("SHOW TABLES LIKE '$unik_syn_table'") != $unik_syn_table) {
$sql = "CREATE TABLE " . $unik_syn_table . " (
s_id int(11) NOT NULL auto_increment,
keyword varchar(255) NOT NULL default '',
syn varchar(255) NOT NULL default '',
PRIMARY KEY (s_id),
KEY kkey (keyword)
)$charset_collate";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
function unikalizator_unset_options () {
global $wpdb, $unik_syn_table;
delete_option('unik_version');
delete_option('unik_mashup_sentence');
delete_option('unik_mashup_paragraph');
delete_option('unik_pressing');
delete_option('unik_pressing_level');
delete_option('unik_ruslat');
delete_option('unik_ruslat_level');
delete_option('unik_synonymizer');
delete_option('unik_synonymizer_level');
$sql = "DROP TABLE $unik_syn_table";
$wpdb->query( $sql );
}
function unikalizator_admin_menu() {
add_options_page('Уникализатор', 'Уникализатор', 8, __FILE__, 'unikalizator_options_page');
}
function unikalizator_options_page() {
global $wpdb, $unik_syn_table;
$unik_options = array(
'unik_mashup_sentence',
'unik_mashup_paragraph',
'unik_pressing',
'unik_pressing_level',
'unik_ruslat',
'unik_ruslat_level',
'unik_synonymizer',
'unik_synonymizer_level'
);
$cmd = $_POST['cmd'];
foreach ($unik_options as $unik_opt) {
$$unik_opt = get_option($unik_opt);
}
if ($cmd == "del_syn") {
$sql = "TRUNCATE TABLE $unik_syn_table";
$wpdb->query( $sql );
echo '<div class="updated"><p><strong>Все синонимы удалены из базы</strong></p></div>';
}
if ($cmd == "add_syn" && $_POST['synonims_dict']) {
$lines = explode("\n", $_POST['synonims_dict']);
foreach($lines as $line){
$line = trim($line);
if (!$line) continue;
list($key, $syn) = explode("|", $line);
$sql = "INSERT INTO $unik_syn_table (keyword, syn) VALUES('$key','$syn')";
$wpdb->query( $sql );
}
echo '<div class="updated"><p><strong>Синонимы добавлены в базу</strong></p></div>';
}
if ($cmd == "unik_save_opt") {
foreach ($unik_options as $unik_opt) {
$$unik_opt = $_POST[$unik_opt];
}
// Save the posted value in the database
foreach ($unik_options as $unik_opt) {
update_option($unik_opt, $$unik_opt);
}
echo '<div class="updated"><p><strong>Опции сохранены</strong></p></div>';
}
$unik_total_synonyms = $wpdb->get_var("SELECT COUNT(*) FROM $unik_syn_table");
?>
<div class="wrap">
<h2>Уникализатор</h2>
<h3>Настройки</h3>
<form method="post" action="<? echo $_SERVER['REQUEST_URI'];?>">
<table class="form-table">
<tr>
<th colspan=2 scope="row">
<input name="unik_mashup_paragraph" type="checkbox" <?if($unik_mashup_paragraph)echo "checked";?>> Перемешивать абзацы
</th>
</tr>
<tr>
<th colspan=2 scope="row">
<input name="unik_mashup_sentence" type="checkbox" <?if($unik_mashup_sentence)echo "checked";?>> Перемешивать предложения в пределах абзаца
</th>
</tr>
<tr>
<th>
<input name="unik_pressing" type="checkbox" <?if($unik_pressing)echo "checked";?>> Сделать выжимку (маленькую статью из большой)
</th>
<td>
Уровень:
<select name="unik_pressing_level">
<option value=100 <?if($unik_pressing_level==100)echo "selected";?>>Больше текста
<option value=90 <?if($unik_pressing_level==90)echo "selected";?>>90
<option value=80 <?if($unik_pressing_level==80)echo "selected";?>>80
<option value=70 <?if($unik_pressing_level==70)echo "selected";?><?if (!$unik_pressing_level) echo "selected";?>>70
<option value=60 <?if($unik_pressing_level==60)echo "selected";?>>60
<option value=50 <?if($unik_pressing_level==50)echo "selected";?>>50
<option value=40 <?if($unik_pressing_level==40)echo "selected";?>>40
<option value=30 <?if($unik_pressing_level==30)echo "selected";?>>30
<option value=20 <?if($unik_pressing_level==20)echo "selected";?>>20
<option value=10 <?if($unik_pressing_level==10)echo "selected";?>>Меньше текста
</select>
</td>
</tr>
<tr>
<th>
<input name="unik_ruslat" type="checkbox" <?if($unik_ruslat)echo "checked";?>> Заменять русские буквы на эквиваленты
</th>
<td>
Уровень:
<select name="unik_ruslat_level">
<option value=100 <?if($unik_ruslat_level==100)echo "selected";?>>100% замена
<option value=20 <?if($unik_ruslat_level==20)echo "selected";?>>20%
<option value=15 <?if($unik_ruslat_level==15)echo "selected";?>>15%
<option value=10 <?if($unik_ruslat_level==10)echo "selected";?>>10%
<option value=9 <?if($unik_ruslat_level==9)echo "selected";?>>9%
<option value=8 <?if($unik_ruslat_level==8)echo "selected";?>>8%
<option value=7 <?if($unik_ruslat_level==7)echo "selected";?>>7%
<option value=5 <?if($unik_ruslat_level==5)echo "selected";?><?if (!$unik_ruslat_level) echo "selected";?>>5%
<option value=3 <?if($unik_ruslat_level==3)echo "selected";?>>3%
<option value=2 <?if($unik_ruslat_level==2)echo "selected";?>>2%
<option value=1 <?if($unik_ruslat_level==1)echo "selected";?>>1% слов
</select>
</td>
</tr>
<tr>
<th>
<input name="unik_synonymizer" type="checkbox" <?if($unik_synonymizer)echo "checked";?>> Синонимизация
</th>
<td>
Глубина:
<select name="unik_synonymizer_level">
<option value=0 <?if($unik_synonymizer_level==0)echo "selected";?>>Максимальная
<option value=1 <?if($unik_synonymizer_level==1)echo "selected";?>>1
<option value=2 <?if($unik_synonymizer_level==2)echo "selected";?>>2
<option value=3 <?if($unik_synonymizer_level==3)echo "selected";?>>3
<option value=4 <?if($unik_synonymizer_level==4)echo "selected";?>>4
<option value=5 <?if($unik_synonymizer_level==5)echo "selected";?>>5
<option value=6 <?if($unik_synonymizer_level==6)echo "selected";?>>6
<option value=7 <?if($unik_synonymizer_level==7)echo "selected";?>>7
<option value=8 <?if($unik_synonymizer_level==8)echo "selected";?>>8
<option value=9 <?if($unik_synonymizer_level==9)echo "selected";?>>9
<option value=10 <?if($unik_synonymizer_level==10)echo "selected";?>>Слабо
</select>
</td>
</tr>
</table>
<input type="hidden" name="cmd" value="unik_save_opt">
<p class="submit">
<input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
</p>
</form>
<h3>Уникализатор plugin разработан при поддержке:</h3>
<table class="form-table">
<tr><th>
<ul>
<li><a href="http://kisto4ka.keywordrush.com/" target="_blank">Кисточка</a> - генератор сателлитов, с функцией автоматического поиска контента.</li
<li><a href="http://brush.keywordrush.com/" target="_blank">BRush</a> - генератор англоязычных сайтов, со встроенными партнерскими программами, качественным тематическим контентом и уникализацией текста.</li>
</ul>
</th></tr></table>
<h3>Словарь Синонимайзера</h3>
<table class="form-table" width="300px">
<tr>
<td>
Добавить словарь синонимов (большой словарь добавляйте частями)<br />
<b>формат:</b> слово|синоним1,синоним2,синоним3<br />
Всего слов в базе: <b><?php echo $unik_total_synonyms; ?></b><br />
<form method="post" action="<? echo $_SERVER['REQUEST_URI'];?>">
<input type="hidden" name="cmd" value="del_syn">
<input type="submit" name="Submit" value="Очистить базу словаря" />
</form>
<form method="post" action="<? echo $_SERVER['REQUEST_URI'];?>">
<textarea cols=110 rows=12 name="synonims_dict"></textarea>
</td>
</tr>
</table>
<input type="hidden" name="cmd" value="add_syn">
<p class="submit">
<input type="submit" name="Submit" value="Добавить синонимы" />
</p>
</form>
</div>
<?php
}
function unik_get_syn_table() {
global $wpdb;
return $wpdb->prefix . "unik_synonyms_ru";
}
function unikalizator_do_action($text){
if (get_option('unik_pressing')) {
$text = unik_text_pressing($text, get_option('unik_pressing_level'));
}
if (get_option('unik_mashup_paragraph') || get_option('unik_mashup_sentence')) {
$text = unik_mashup_paragraph_sentence($text, get_option('unik_mashup_paragraph'), get_option('unik_mashup_sentence'), "\r\n");
}
if (get_option('unik_ruslat')) {
$text = unik_replace_ruslat($text, get_option('unik_ruslat_level'));
}
if (get_option('unik_synonymizer')) {
$text = unik_synonymizer_ru($text, get_option('unik_synonymizer_level'));
}
return $text;
}
function unik_mashup_paragraph_sentence($text, $mashup_paragraph = false, $mashup_sentence = false, $paragraph_symbol = "</p>") {
$paragraphs = preg_split("/".str_replace("/", "\/", $paragraph_symbol)."+?/uims", $text, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
if ($mashup_paragraph) {
srand((float) microtime() * 10000000);
shuffle($paragraphs);
}
if ($mashup_sentence) {
foreach ($paragraphs as $key => $paragraph) {
$paragraph = strip_tags($paragraph);
if ($paragraph_symbol == "</p>")
$paragraphs[$key] = "<p>";
else
$paragraphs[$key] = "";
$paragraphs[$key] .= unik_mashup_sentence($paragraph);
}
}
return join($paragraph_symbol, $paragraphs) . $paragraph_symbol;
}
function unik_mashup_sentence($text) {
$sentences = unik_split_sentence($text);
srand((float) microtime() * 10000000);
shuffle($sentences);
return join(" ", $sentences);
}
function unik_split_sentence($text) {
$sentences = preg_split("/([\.\!\?])\s/u", trim($text), -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
$sentences2 = array();
$num = 0;
foreach($sentences as $key => $sentence) {
if ($sentence == "." || $sentence == "!" || $sentence == "?") {
$sentences2[$num - 1] .= $sentence;
continue;
}
$sentences2[$num] .= trim($sentence);
$num++;
}
return $sentences2;
}
function unik_text_pressing($text, $pressing_level) {
if ($pressing_level >= 100)
return $text;
$text = strip_tags($text);
$sentences = unik_split_sentence($text);
$press = round((count($sentences)*$pressing_level)/100);
$rand_keys = array_rand($sentences, count($sentences) - $press);
if (!is_array($rand_keys))
return $text;
foreach ($rand_keys as $rand_key) {
unset($sentences[$rand_key]);
}
return join(" ", $sentences);
}
function unik_replace_ruslat($text, $level = 100) {
$words = preg_split("/(\s+)/uims", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$words_count = 0;
$not_empty_words = array();
foreach ($words as $key => $word) {
if (trim($word)) {
$not_empty_words[$key] = $word;
$words_count++;
}elseif (!preg_match('/\n/ims', $word))
unset($words[$key]);
}
if ($words_count == 0) $words_count = 1;
if ($words_count > count($not_empty_words)) $words_count = count($not_empty_words);
$replace_count = round(($words_count * $level) / 100);
$rand_keys = array_rand($not_empty_words, $words_count);
$count = 0;
foreach($rand_keys as $key => $rand_key) {
if ($rword = unik_ruslat($words[$rand_key])) {
$words[$rand_key] = $rword;
$count++;
}
if ($count >= $replace_count)
break;
}
$text = join(" ", $words);
return $text;
}
function unik_ruslat($str) {
$ruslat = array("а"=>"a","А"=>"A","е"=>"e","Е"=>"E","о"=>"o","О"=>"O","х"=>"x","Х"=>"X");
$news_str = strtr($str, $ruslat);
if ($str !== $news_str )
return $news_str;
else
return false;
}
function unik_synonymizer_ru($text, $depth = 0) {
$words = preg_split('/([a-яА-Я]+)/is', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$bwords = array();
if ($bloked_words) {
foreach ($bloked_words as $bw) {
$tmp = preg_split("/\s+/", mb_strtolower($bw, "UTF-8"));
$bwords = array_merge($bwords , $tmp);
}
}
$new_text = "";
$open_tag = false;
$last_depth = 1000;
foreach ($words as $word) {
if ($open_tag) {
$new_text .= $word;
if (unik_is_opened_tag($word)) continue;
if (unik_is_closed_tag($word)) {
$open_tag = false;
}
continue;
}
if (unik_is_opened_tag($word)) {
$open_tag = true;
$new_text .= $word;
continue;
}
if ($word == ' ') {$new_text .= $word; continue;}
if (strlen($word) < 4) {$new_text .= $word; continue;}
if (in_array(mb_strtolower($word, "UTF-8"), $bwords)) {$new_text .= $word; continue;}
if ($ignore_stop_words) {
if (in_array(mb_strtolower($word, "UTF-8"), $stop_words)) {
$new_text .= $word; continue;
}
}
if ($last_depth < $depth) {
$new_text .= $word;
$last_depth++;
continue;
}
if ($synonym = unik_get_synonym($word)) {
if (unik_my_mb_ucfirst($word) == $word) {
$synonym = unik_my_mb_ucfirst($synonym);
}
$new_text .= $synonym;
$last_depth = 0;
}else {
$new_text .= $word;
}
}
return $new_text;
}
function unik_is_opened_tag($str) {
if (strpos($str, '<') !== false)
return true;
else
return false;
}
function unik_is_closed_tag($str) {
if (strpos($str, '>') !== false)
return true;
else
return false;
}
function unik_get_synonym ($word){
global $unik_syn_table, $wpdb;
$keyword = mb_strtolower($word, "UTF-8");
$sql = "SELECT syn FROM $unik_syn_table WHERE keyword='$keyword' LIMIT 1";
$syns = $wpdb->get_var($sql);
if ($syns) {
$words = explode(',', $syns);
srand((float) microtime() * 10000000);
return $words[array_rand($words)];
} else {
return false;
}
}
function unik_my_mb_ucfirst($str, $e='UTF-8') {
if (function_exists('mb_strtoupper')) {
$fc = mb_strtoupper(mb_substr($str, 0, 1, $e), $e);
return $fc.mb_substr($str, 1, mb_strlen($str, $e), $e);
}else
return ($str);
}
?>
[свернуть]