forked from darkrain/woo-export-yml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source-manager.php
117 lines (71 loc) · 2.6 KB
/
source-manager.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
<?php
class WooExportYmlSourceManager {
public function __construct( $id ) {
$this->id = $id;
add_action( 'wp_ajax_add_yml_source', array( $this, 'ajax_add_source' ) );
add_action( 'wp_ajax_del_yml_source', array( $this, 'ajax_del_source' ) );
}
public function addSource( $name ){
if( empty( $name ) )
return false;
$sources = (array)get_option($this->id.'_sources');
$key = $this->transliterate( $name );
if( $key != 'yandex_market' && !isset( $sources[ $key ] ) ){
$sources[ $key ] = $name;
update_option( $this->id.'_sources', $sources );
return true;
}else
return false;
}
public function deleteSource( $key ){
$sources = (array)get_option($this->id.'_sources');
if( isset( $sources[ $key ] ) ){
unset( $sources[ $key ] );
update_option( $this->id.'_sources', $sources );
return true;
}else
return false;
}
public function ajax_add_source(){
if( $this->addSource( $_POST['name'] ) ){
echo die( array( 'code' => 'success' ) );
}else{
echo die( array( 'code' => 'failed' ) );
}
}
public function ajax_del_source(){
if( $this->deleteSource( $_POST['key'] ) ){
echo die( array( 'code' => 'success' ) );
}else{
echo die( array( 'code' => 'failed' ) );
}
}
public function get_sources(){
$def_sources = array(
'yandex_market' => 'Яндекс.Маркет'
);
$sources = (array)get_option($this->id.'_sources');
$out_sources = array_merge( $def_sources, $sources );
foreach( $out_sources as $key => $name ){
if( empty( $name ) )
unset( $out_sources[ $key ] );
}
return $out_sources;
}
public function transliterate( $str ) {
$trans = array("а"=>"a","б"=>"b","в"=>"v","г"=>"g","д"=>"d","е"=>"e",
"ё"=>"yo","ж"=>"j","з"=>"z","и"=>"i","й"=>"i","к"=>"k","л"=>"l",
"м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r","с"=>"s","т"=>"t",
"у"=>"y","ф"=>"f","х"=>"h","ц"=>"c","ч"=>"ch", "ш"=>"sh","щ"=>"shh",
"ы"=>"i","э"=>"e","ю"=>"u","я"=>"ya","ї"=>"i","'"=>"","ь"=>"","Ь"=>"",
"ъ"=>"","Ъ"=>"","і"=>"i","А"=>"A","Б"=>"B","В"=>"V","Г"=>"G","Д"=>"D",
"Е"=>"E", "Ё"=>"Yo","Ж"=>"J","З"=>"Z","И"=>"I","Й"=>"I","К"=>"K", "Л"=>"L",
"М"=>"M","Н"=>"N","О"=>"O","П"=>"P", "Р"=>"R","С"=>"S","Т"=>"T","У"=>"Y",
"Ф"=>"F", "Х"=>"H","Ц"=>"C","Ч"=>"Ch","Ш"=>"Sh","Щ"=>"Sh", "Ы"=>"I","Э"=>"E",
"Ю"=>"U","Я"=>"Ya","Ї"=>"I","І"=>"I");
$res = str_replace(" ","-",strtr(strtolower($str),$trans));
$res = preg_replace("|[^a-zA-Z0-9-]|","",$res);
return $res;
}
}
new WooExportYmlSourceManager('export_yml');