-
Notifications
You must be signed in to change notification settings - Fork 12
/
sBotClass.php
346 lines (249 loc) · 8.35 KB
/
sBotClass.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* @author Savaş Can ALTUn <savascanaltun@gmail.com>
* @link http://savascanaltun.com.tr
* @link http://github.com/saltun
* @since 22.06.2014
* @example example.php
* @version 2.0
*/
cLass sBotClass{
/**
* @var string
*/
public $thumbnail;
/**
* @var string
*/
public $title;
/**
* @var string
*/
public $content;
/**
* @var int
*/
public $author=1;
/**
* @var string
*/
public $tags;
/**
* @var int
*/
public $cat;
/**
* @var string
*/
public $metas;
/**
* @var string
*/
public $status="publish";
/**
* @var null|string
*/
public $time=NULL;
/**
* @var null|string
*/
public $description=NULL;
/**
* @var string
*/
public $password;
/**
* Sınıfın Başlangıcı ve Gerekli fonksiyonlar için wordpress dosyalarının dahil edilişi.
* @return void
*/
public function __construct(){
if(!function_exists('wp_get_current_user')) {
include(ABSPATH . "wp-includes/pluggable.php");
}
if (!function_exists('wp_insert_category')) {
include(ABSPATH . "wp-admin/includes/taxonomy.php");
}
}
/**
* Kısaltma fonksiyonu
* @param string Yazı değeri alınır
* @param int Kısaltılacak karakter sayısı alınır
* @return string Düzenlenen içerik geri döndürülür
*/
public function shorten($keyword, $str = 10)
{
if (strlen($keyword) > $str)
{
if (function_exists("mb_substr")) $keyword = mb_substr($keyword, 0, $str, "UTF-8").'..';
else $keyword = substr($keyword, 0, $str).'..';
}
return $keyword;
}
/**
* Sef link oluşturma metotu
* @param string
* @return string
*/
public function sef($s) {
$tr = array('ş','Ş','ı','İ','ğ','Ğ','ü','Ü','ö','Ö','Ç','ç');
$eng = array('s','s','i','i','g','g','u','u','o','o','c','c');
$s = str_replace($tr,$eng,$s);
$s = strtolower($s);
$s = preg_replace('/[^%a-z0-9 _-]/', '', $s);
$s = preg_replace('/\s+/', '-', $s);
$s = preg_replace('|-+|', '-', $s);
$s = trim($s, '-');
return $s;
}
/**
* İçerik var mı ? yokmu ? kontrolü
* @return bool|boolean
*/
public function content_control(){
global $wpdb;
$count=$wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts where post_name ='".sanitize_title($this->title)."' ");
if ($count > 0) {
return false;
}else{
if ($xs = get_page_by_title($this->title, OBJECT, 'post' )) {
return false;
}
}
return true;
}
/**
* Görseli indirme metotu
* @param string Görsel Adresi
* @return string İndirilmiş görselin kaydedildiği adres
*/
public function download_image($url){
/* konu var ise resimleri indirme ! */
if (!$this->content_control()) {
return false;
}
$save=wp_upload_dir();
$savepath = $save['path'];
$file = explode('/',$url);
$count = count($file);
$fullfilename = $this->sef($this->title)."-".rand(0,100000).".jpg";
if (!function_exists('curl_version')) {
/* curl kurulu değil file_get_contents ile adresi okuyalım */
$data=file_get_contents($url);
}else{
$ch = curl_init("$url");
if (!$ch) {
die("Curl bu adres'e bağlanamadı.");
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
}
$saveFile=file_put_contents($savepath."/".$fullfilename, $data);
$saveUrl=$save['url']."/".$fullfilename;
return $saveUrl;
}
/**
* Görseli kaydetme metotu
* @param string
* @return string
*/
public function save_image($par){
/* save content image */
$newlink=$this->download_image($par);
return $newlink;
}
/**
* İçerikteki linkleri ( görselleri ) indirme metotu
* @param string
* @return string
* @see MyClass::save_image()
*/
public function new_content($kaynak){
$desen='#\bhttps?:\/\/\S+(?:png|jpg|gif|Jpeg)#si';
preg_match_all($desen,$kaynak,$linkler);
$linkler=array_unique($linkler[0]);
foreach ($linkler as $link) {
$newlink=$this->save_image($link);
$kaynak=str_replace($link,$newlink,$kaynak);
}
return $kaynak;
}
/**
* İçeriği Kaydetme metotu
* @param bool|boolean True yada False değeri
* @param bool|boolean True yada False değeri
* @return null|int Ya boş değer döner yada kaydedilen içeriğin id değeri
*/
public function addPost($allinoneseo=false,$varyok=false){
if ($varyok==true) {
if (!$this->content_control()) {
return false;
}
}
$my_post = array();
$my_post['post_title'] = $this->title;
$my_post['post_content'] = $this->content;
$my_post['post_date'] = $this->time;
$my_post['post_date_gmt'] = $this->time;
$my_post['post_status'] = $this->status;
$my_post['post_author'] = $this->author;
$my_post['post_category'] = $this->cat;
$my_post['tags_input'] = $this->tags;
$my_post['post_password']= $this->password;
remove_filter('content_save_pre', 'wp_filter_post_kses');
remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
$post_id= wp_insert_post( $my_post );
add_filter('content_save_pre', 'wp_filter_post_kses');
add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
if ($allinoneseo) {
if (empty($this->description)) {
$defaultdesc=strip_tags($this->content);
$this->description=$this->shorten($defaultdesc,160);
}
// all in one seo
add_post_meta($post_id,"_aioseop_title",$this->title);
add_post_meta($post_id,"_aioseop_description",$this->description);
add_post_meta($post_id,"_aioseop_keywords",$this->tags);
// all in one seo
}
// add meta tags ( özel alanları ekle )
if (isset($this->metas)) {
$count=count($this->metas);
$keys=array_keys($this->metas);
$values=array_values($this->metas);
for ($i=0; $i < $count; $i++) {
add_post_meta($post_id,$keys[$i],$values[$i]);
}
}
$filename = $this->thumbnail;
$filetype = wp_check_filetype( basename( $filename ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id=wp_insert_attachment($attachment, $filename, $post_id);
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail($post_id,$attachment_id);
return $post_id;
}
/**
* Yeni Kategori Oluşturma metotu
* @param string Kategori İsim değeri
* @param string Kategori açıklaması
* @param null|string Ya boş değer yada belirtilen sef slug adresi
* @return null|int Ya boş değer yada kategori id değeri
*/
public function add_category($name,$description,$slug=NULL){
$cat = array('cat_name' => $name,'category_description' => $description,'category_nicename' => $slug,'category_parent' => '' );
return $cat_id = wp_insert_category($cat);
}
}