1.5-2 sene önce şöyle bir class yazmıştım hocam
<?php
class Bot{
public function oku($url, $postdata = null){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0) Gecko/20100101 Firefox/23.0');
curl_setopt($ch, CURLOPT_HEADER, 0);
if($postdata){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$sonuc = curl_exec($ch);
curl_close($ch);
return trim(preg_replace('/\s+/', ' ', $sonuc));
}
public function wp_load(){
// wordpress dosyasını yükle
if ( !defined('WP_LOAD_PATH') ) {
$classic_root = dirname(dirname(dirname(dirname(__FILE__)))) . '/' ;
if (file_exists( $classic_root . 'wp-load.php') )
define( 'WP_LOAD_PATH', $classic_root);
else
if (file_exists( $path . 'wp-load.php') ){
define( 'WP_LOAD_PATH', $path);
}
else
exit("Dosya Bulunamadı. wp-load.php");
}
require_once( WP_LOAD_PATH . 'wp-load.php');
}
public function wp_kategoriler(){
$this->wp_load();
return str_replace('<select ',"<select multiple='multiple' ", wp_dropdown_categories(array('show_count' => true,'hide_empty' => false,'class'=>'form-control','echo' => false,'name' => 'kategori[]','id' => 'kategori[]')));
}
public function wp_baslik($baslik){
$this->wp_load();
global $wpdb;
$basliklar = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts where post_title='$baslik'");
if($basliklar>0) return true;
return false;
}
public function wp_ekle($arr,$durum){
$this->wp_load();
parse_str($arr,$array);
$post = array();
$post['post_title'] = stripslashes_deep($array['baslik']);
$post['post_content'] = $array['icerik'];
$post['tags_input'] = $array['etiket'];
$post['post_status'] = $durum;
$post['post_category'] = $array['kategori']; //array(implode(",", $array['kategori']));
$post['post_author'] = 1;
$post_id = wp_insert_post($post);
if($post_id){
add_post_meta($post_id, '_aioseop_keywords', $array['etiket']);
add_post_meta($post_id, '_aioseop_description', $array['aciklama']);
add_post_meta($post_id, '_aioseop_title', stripslashes_deep($array['baslik']));
if(trim($array['resim'])){
$upload_dir = wp_upload_dir();
//$image_data = file_get_contents($array['resim']);
$filename = sanitize_title($array['baslik']).".jpg";
if(wp_mkdir_p($upload_dir['path']))
$file = $upload_dir['path'] . '/' . $filename;
else
$file = $upload_dir['basedir'] . '/' . $filename;
$this->watermark($array['resim'], $file);
//file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
}
return $post_id;
}else{
return false;
}
}
public function watermark($remote,$local){
if(file_exists('watermark.png')){
$stamp = imagecreatefrompng('watermark.png');
$im = imagecreatefromjpeg($remote);
$marge_right = 10;
$marge_bottom = 20;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
imagejpeg($im, $local);
imagedestroy($im);
}else{
file_put_contents($local, file_get_contents($remote));
}
}
}wp_baslik fonksiyonu ile başlık daha önce bir yazıda kullanıldımı kontrol edebilirsin.
wp_ekle fonksiyonuna ilk parametre olarak bir dizi vermen gerekiyor, içinde olması gereken değerlere fonksiyondan bakabilirsin, resim anahtarı verilirse onu öne çıkan görsel olarak ekler, ek olarak; dosyanın olduğu dizinde watermark.png varsa onuda görsel'in üstüne basar. ikinci parametre ise yazının durumu, 'draft' | 'publish' | 'pending'| 'future' | 'private' dan biri olabilir.
biraz eski odluğu için hata verebilir, test edemedim. ama mantığını anlamak açısından işine yarar