/** * فیلتر کردن محتوای پست بر اساس کلمات کلیدی، طول و تگهای HTML مجاز. * * @param string $content محتوای اصلی پست. * @param string $title عنوان اصلی پست. * @param array $feed_settings تنظیمات فید مربوطه. * @return string|WP_Error محتوای فیلتر شده یا یک شیء WP_Error در صورت رد شدن پست. */ function rss_importer_pro_filter_content($content, $title, $feed_settings) { // فیلتر کلمات ممنوعه (Blacklist) if (!empty($feed_settings['blacklist_keywords'])) { $blacklist = array_map('trim', explode(',', $feed_settings['blacklist_keywords'])); foreach ($blacklist as $keyword) { if (empty($keyword)) continue; if (stripos($content, $keyword) !== false || stripos($title, $keyword) !== false) { return new WP_Error('blacklist_match', 'محتوا یا عنوان شامل کلمه ممنوعه است.'); } } } // فیلتر کلمات مجاز (Whitelist) if (!empty($feed_settings['whitelist_keywords'])) { $whitelist = array_map('trim', explode(',', $feed_settings['whitelist_keywords'])); $match_found = false; foreach ($whitelist as $keyword) { if (empty($keyword)) continue; if (stripos($content, $keyword) !== false || stripos($title, $keyword) !== false) { $match_found = true; break; } } if (!$match_found) { return new WP_Error('whitelist_no_match', 'محتوا یا عنوان شامل هیچ یک از کلمات مجاز نیست.'); } } // محدودسازی طول عنوان if (!empty($feed_settings['title_max_length']) && mb_strlen($title) > $feed_settings['title_max_length']) { $title = mb_substr($title, 0, $feed_settings['title_max_length']) . '...'; } // محدودسازی طول محتوا if (!empty($feed_settings['content_max_length']) && mb_strlen($content) > $feed_settings['content_max_length']) { $content = mb_substr($content, 0, $feed_settings['content_max_length']) . '...'; } // حذف تگهای HTML خاص if (!empty($feed_settings['strip_html_tags'])) { $tags_to_strip = array_map('trim', explode(',', $feed_settings['strip_html_tags'])); foreach ($tags_to_strip as $tag) { if (empty($tag)) continue; $content = preg_replace('/<' . preg_quote($tag, '/') . '.*?>.*?<\/' . preg_quote($tag, '/') . '>/is', '', $content); $content = preg_replace('/<' . preg_quote($tag, '/') . '.*?\/>/is', '', $content); } } // تعریف تگهای HTML مجاز $allowed_html_tags = array(); if (!empty($feed_settings['allowed_html_tags'])) { $tags_array = array_map('trim', explode(',', $feed_settings['allowed_html_tags'])); foreach ($tags_array as $tag) { // افزودن ویژگیهای بیشتر برای تگ img if ($tag === 'img') { $allowed_html_tags['img'] = array( 'src' => true, 'alt' => true, 'title' => true, 'class' => true, 'id' => true, 'width' => true, 'height' => true, 'style' => true, // برای پشتیبانی از استایلهای احتمالی 'srcset' => true, // برای تصاویر ریسپانسیو 'sizes' => true, // برای تصاویر ریسپانسیو ); } else { $allowed_html_tags[$tag] = array( 'class' => true, 'id' => true, 'style' => true, ); } } } else { // تگهای پیشفرض وردپرس با پشتیبانی کامل از img $allowed_html_tags = wp_kses_allowed_html('post'); $allowed_html_tags['img'] = array( 'src' => true, 'alt' => true, 'title' => true, 'class' => true, 'id' => true, 'width' => true, 'height' => true, 'style' => true, 'srcset' => true, 'sizes' => true, ); } // حفظ تصاویر در محتوا $content = wp_kses($content, $allowed_html_tags); // افزودن امضا یا متن دلخواه به انتهای محتوا if (!empty($feed_settings['post_signature'])) { $content .= '
' . wp_kses_post($feed_settings['post_signature']) . '
'; } return $content; }