بشكل افتراضي ، يقوم WordPress بتجريد أي محتوى قد يكون HTML غير مفصول في تعليقات المستخدمين غير المسجلين ، وهو أمر جيد للحماية من XSS ، لكنه يمتد هذا التصفية دون داع إلى عناصر <pre>
أيضًا. على مدونتي ، حيث تُنشئ كل رسالة تقريبًا تعليقات تستفيد من مقتطفات أكواد HTML ، تسبب هذا التصفية في الكثير من المتاعب لدى المستخدمين (وأنا).
هل هناك طريقة "لإصلاح" هذا التصفية العدوانية المفرطة داخل عناصر <pre>
ضمن التعليقات غير المسجلة ، دون تعطيلها لبقية التعليق؟ من المفضل ، بطريقة تنجو من الترقيات.
حل صغير وكان تسليط الضوء في مدونتي عبر جافا سكريبت
function pre_esc_html($content) {
return preg_replace_callback(
'#(<pre.*?>)(.*?)(</pre>)#imsu',
create_function(
'$i',
'return $i[1].esc_html($i[2]).$i[3];'
),
$content
);
}
add_filter(
'the_content',
'pre_esc_html',
9
);
على الرغم من أن هذا قد يكون أكثر بقليل من الذي تبحث عنه ، يعزل WP-Syntax تصفية HTML داخل علامات <pre>
داخل المشاركات والتعليقات (AFAIK). كما أنه يعمل مع Wordpress 3.0 ، على الرغم من أن الموقع يقول إنه يعمل فقط مع 2.8.
إذا كنت تتطلع إلى جعل الأمر أكثر بساطة ، أقترح البحث في wp-syntax.php
داخل المكون الإضافي (وتحديداً في الجزء السفلي حيث يستخدمون add_filters()
لمعرفة كيف يقومون بتعطيل تصفية HTML التلقائية من Wordpress ضمن علامات <pre>
. ثم يمكنك تطبيق ذلك على التعليقات.
تحرير: لقد ألقيت نظرة على الملف ، وهم يستخدمون regex و PHP preg_replace_callback()
للحفاظ على HTML الأصلي داخل علامات <pre>
. قد تضطر إلى تعديله ليناسب احتياجاتك.
سيكون لديك ، على سبيل المثال (ملاحظة: كود غير مجرب):
<?php
// Unique string for placeholder
$custom_token = md5(uniqid(Rand()));
// Store all the matches in an array
$custom_matches = array();
function custom_substitute(&$match) {
global $custom_token, $custom_matches;
$i = count($custom_matches);
// Store the match for later use
$custom_matches[$i] = $match;
// Unique placeholder so that we know where to put the code that was ripped out
return '<p>' . $custom_token . '</p>';
}
function custom_replace($match) {
global $custom_matches;
$i = intval($match[1]);
$match = $custom_matches[$i];
// The index might be off - you might want to double-check it
return htmlentities($match[1]);
}
function custom_before_content_filter($content) {
return preg_replace_callback("/\s*<pre+>(.*)<\/pre>\s*/siU", 'custom_substitute', $content);
}
function custom_after_content_filter($content) {
global $custom_token;
return preg_replace_callback("/<p>\s*" . $custom_token . "\s*<\/p>/si", 'custom_replace', $content);
}
// Run the "before" filter first, with priority of 0
add_filter('comment_text', 'custom_before_content_filter', 0);
// Now run the "after" filter
add_filter('comment_text', 'custom_after_content_filter', 99);