otomatik sayfa yenilemede çeker
Kod:Tümünü seç
<?php
/*
Plugin Name: RSS Haber Scraper - 61Saat Trabzon
Plugin URI: https://example.com
Description: 61Saat Trabzon haberlerini RSS ile çeker ve WordPress’e otomatik ekler.
Version: 1.1
Author: Örnek Geliştirici
Author URI: https://example.com
License: GPL2
*/
if (!defined('ABSPATH')) {
exit;
}
class RSS_Haber_Scraper {
private $rss_url = 'https://www.61saat.com/rss/trabzon';
public function __construct() {
add_action('admin_menu', array($this, 'add_admin_page'));
add_action('wp_ajax_rss_scrape', array($this, 'handle_scrape_request'));
}
public function add_admin_page() {
add_menu_page(
'RSS Haber Scraper',
'RSS Haber Scraper',
'manage_options',
'rss-haber-scraper',
array($this, 'admin_page_content'),
'dashicons-rss',
20
);
}
public function admin_page_content() {
?>
<div class="wrap">
<h1>RSS Haber Scraper</h1>
<p>RSS üzerinden haberleri manuel olarak çekmek için aşağıdaki butona tıklayın.</p>
<button id="fetch-rss-news" class="button button-primary">Haberleri Çek</button>
<div id="rss-news-log" style="margin-top: 20px; padding: 10px; background: #f9f9f9; border: 1px solid #ddd; max-height: 300px; overflow-y: auto;"></div>
</div>
<script>
document.getElementById("fetch-rss-news").addEventListener("click", function() {
let log = document.getElementById("rss-news-log");
log.innerHTML += "<p>Haberler çekiliyor...</p>";
fetch(ajaxurl, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: "action=rss_scrape"
})
.then(response => response.json())
.then(data => {
if (data.success) {
log.innerHTML += "<p style='color: green;'>" + data.message + "</p>";
} else {
log.innerHTML += "<p style='color: red;'>" + data.message + "</p>";
}
})
.catch(error => {
log.innerHTML += "<p style='color: red;'>Bağlantı hatası!</p>";
});
});
</script>
<?php
}
public function handle_scrape_request() {
$rss_feed = simplexml_load_file($this->rss_url);
if (!$rss_feed) {
wp_send_json_error(array('message' => 'RSS bağlantısı başarısız.'));
}
$category_id = get_cat_ID('Trabzon');
if ($category_id == 0) {
$category_id = wp_create_category('Trabzon');
}
foreach ($rss_feed->channel->item as $item) {
$title = (string) $item->title;
$link = (string) $item->link;
$description = (string) strip_tags($item->description);
$content = (string) strip_tags($item->children('content', true)->encoded);
$category = (string) $item->category;
$image_url = (string) $item->enclosure['url'];
if (post_exists($title)) continue;
$tags = [];
if (!empty($category)) $tags[] = strtolower($category);
$extra_tags = $this->extract_tags_from_content($description . ' ' . $content);
$tags = array_merge($tags, $extra_tags);
$tags = array_unique($tags);
$tags = array_slice($tags, 0, 5);
if (empty($title) || empty($content)) continue;
$post_content = '<p>' . esc_html($description) . '</p><p>' . esc_html($content) . '</p>';
$post_id = wp_insert_post(array(
'post_title' => wp_strip_all_tags($title),
'post_content' => $post_content,
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'post',
'post_category' => array($category_id)
));
if (!is_wp_error($post_id)) {
if (!empty($tags)) wp_set_post_tags($post_id, $tags);
if (!empty($image_url)) {
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_id = media_sideload_image($image_url, $post_id, $title, 'id');
if (!is_wp_error($attachment_id)) set_post_thumbnail($post_id, $attachment_id);
}
wp_send_json_success(array('message' => 'Yeni haber eklendi.'));
}
}
wp_send_json_error(array('message' => 'Yeni haber bulunamadı.'));
}
private function extract_tags_from_content($content) {
$words = explode(' ', $content);
$tags = [];
foreach ($words as $word) {
$word = trim($word, ".,!?()[]{}\"'");
if (preg_match('/^[A-ZĞÜŞİÖÇ]+$/u', $word) && strlen($word) > 2) {
$tags[] = strtolower($word);
} elseif (preg_match('/^[A-ZĞÜŞİÖÇ][a-zğüşıöç]+$/u', $word) && !in_array($word, ['Ve', 'Bir', 'İçin', 'Ancak'])) {
$tags[] = strtolower($word);
}
}
return array_slice(array_unique($tags), 0, 5);
}
}
new RSS_Haber_Scraper();