class NA_Core {
public static function init() {
// تسجيل Custom Post Type
add_action('init', array(__CLASS__, 'register_news_cpt'));
// تسجيل التصنيفات
add_action('init', array(__CLASS__, 'register_taxonomies'));
// تحميل الوحدات
self::load_modules();
}
public static function register_news_cpt() {
$args = array(
'public' => true,
'label' => 'الأخبار',
'supports' => array('title', 'thumbnail'),
'has_archive' => true,
'rewrite' => array('slug' => 'news'),
'show_in_menu' => false
);
register_post_type('news', $args);
}
public static function register_taxonomies() {
register_taxonomy('news_source', 'news', array(
'label' => 'مصادر الأخبار',
'hierarchical' => true,
'show_admin_column' => true
));
}
private static function load_modules() {
new NA_Admin();
new NA_Cron();
new NA_Analytics();
}
}class NA_Admin {
public function __construct() {
add_action('admin_menu', array($this, 'add_admin_menu'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
}
public function add_admin_menu() {
add_menu_page(
'إدارة الأخبار',
'الأخبار',
'manage_options',
'news-manager',
array($this, 'sources_list_page'),
'dashicons-admin-site',
30
);
add_submenu_page(
'news-manager',
'إضافة مصدر جديد',
'إضافة مصدر',
'manage_options',
'add-news-source',
array($this, 'add_source_page')
);
}
public function sources_list_page() {
include NA_PLUGIN_PATH . 'admin/partials/sources-list.php';
}
public function add_source_page() {
include NA_PLUGIN_PATH . 'admin/partials/add-source.php';
}
}class NA_Cron {
private $schedules;
public function __construct() {
$this->schedules = array(
'hourly' => 3600,
'six_hours' => 21600,
'twelve_hours' => 43200,
'daily' => 86400
);
add_filter('cron_schedules', array($this, 'add_custom_schedules'));
add_action('na_fetch_news_event', array($this, 'fetch_news_for_source'));
}
public function add_custom_schedules($schedules) {
foreach ($this->schedules as $key => $interval) {
$schedules[$key] = array(
'interval' => $interval,
'display' => $this->get_schedule_display_name($key)
);
}
return $schedules;
}
public function fetch_news_for_source($source_id) {
$source = $this->get_source($source_id);
if ($source->type == 'rss') {
NA_RSS_Parser::fetch($source);
} else {
NA_Scraper::fetch($source);
}
}
}class NA_Analytics {
public function __construct() {
add_action('wp_ajax_na_track_impression', array($this, 'track_impression'));
add_action('wp_ajax_nopriv_na_track_impression', array($this, 'track_impression'));
add_action('wp_ajax_na_track_view_time', array($this, 'track_view_time'));
add_action('wp_ajax_nopriv_na_track_view_time', array($this, 'track_view_time'));
}
public function track_impression() {
$news_id = intval($_POST['news_id']);
$this->increment_impressions($news_id);
wp_die();
}
public function track_view_time() {
$news_id = intval($_POST['news_id']);
$view_time = intval($_POST['view_time']);
$this->update_view_time($news_id, $view_time);
wp_die();
}
}class NA_RSS_Parser {
public static function fetch($source) {
$rss_url = $source->rss_url;
// جلب محتوى RSS
$response = wp_remote_get($rss_url, array(
'timeout' => 30,
'user-agent' => 'Mozilla/5.0 News Aggregator Plugin'
));
if (is_wp_error($response)) {
self::log_error($source->id, 'فشل في جلب RSS: ' . $response->get_error_message());
return false;
}
$rss_content = wp_remote_retrieve_body($response);
$xml = simplexml_load_string($rss_content);
if (!$xml) {
self::log_error($source->id, 'فشل في تحليل XML');
return false;
}
// معالجة العناصر
$items = $xml->channel->item;
$news_count = 0;
foreach ($items as $item) {
if (self::save_news_item($item, $source)) {
$news_count++;
}
}
self::update_source_last_fetch($source->id, $news_count);
return $news_count;
}
private static function save_news_item($item, $source) {
$title = (string) $item->title;
$link = (string) $item->link;
// التحقق من عدم التكرار
if (self::news_exists($link)) {
return false;
}
// إضافة الخبر
$news_id = wp_insert_post(array(
'post_title' => $title,
'post_type' => 'news',
'post_status' => 'publish',
'meta_input' => array(
'_news_source_url' => $link,
'_news_source_id' => $source->id,
'_fetch_time' => current_time('mysql')
)
));
if (!is_wp_error($news_id)) {
wp_set_post_terms($news_id, array($source->category_id), 'news_source');
return true;
}
return false;
}
private static function news_exists($link) {
global $wpdb;
$query = "SELECT post_id FROM {$wpdb->postmeta}
WHERE meta_key = '_news_source_url' AND meta_value = %s";
return $wpdb->get_var($wpdb->prepare($query, $link));
}
}class NA_Scraper {
public static function fetch($source) {
$url = $source->url;
$response = wp_remote_get($url, array(
'timeout' => 30,
'headers' => array(
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
)
));
if (is_wp_error($response)) {
self::log_error($source->id, 'فشل في جلب الصفحة: ' . $response->get_error_message());
return false;
}
$html = wp_remote_retrieve_body($response);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
// محاولة العثور على عناوين الأخبار (قابلة للتخصيص)
$headlines = self::extract_headlines($xpath);
$news_count = 0;
foreach ($headlines as $headline) {
if (self::save_scraped_news($headline, $source)) {
$news_count++;
}
}
self::update_source_last_fetch($source->id, $news_count);
return $news_count;
}
private static function extract_headlines($xpath) {
$headlines = array();
// أنماط شائعة لعناوين الأخبار
$patterns = array(
'//h1/a',
'//h2/a',
'//h3/a',
'//article//a',
'//.news-item//a',
'//.headline//a'
);
foreach ($patterns as $pattern) {
$elements = $xpath->query($pattern);
foreach ($elements as $element) {
$title = trim($element->nodeValue);
$link = $element->getAttribute('href');
if (!empty($title) && !empty($link) && strlen($title) > 10) {
// جعل الرابط مطلقاً إذا كان نسبياً
if (strpos($link, 'http') !== 0) {
$link = self::make_absolute_url($link, $source->url);
}
$headlines[] = array(
'title' => $title,
'link' => $link
);
}
}
}
return $headlines;
}
private static function make_absolute_url($relative, $base) {
// تحويل الروابط النسبية إلى مطلقة
return rtrim($base, '/') . '/' . ltrim($relative, '/');
}
}
Warning: Cannot modify header information - headers already sent by (output started at /home/max4arab/public_html/wp-content/plugins/news-aggregator/includes/class-core.php:1) in /home/max4arab/public_html/wp-includes/pluggable.php on line 1450
Warning: Cannot modify header information - headers already sent by (output started at /home/max4arab/public_html/wp-content/plugins/news-aggregator/includes/class-core.php:1) in /home/max4arab/public_html/wp-includes/pluggable.php on line 1453