- الحالة
- مفتوحة
- وسوم الموضوع
- WCML
نظرة عامة على المشكلة
يعتمد الوصف القصير لمنتج WooCommerce على حقل مقتطف المقالة، وهو في العادة حقل نصي بسيط لا يتعامل مع الأكواد المختصرة. ولذلك، ليس من المتوقع أن تعمل الأكواد المختصرة الخاصة بأداة بناء الصفحات إذا ألصقتها في مقتطف المقالة، وعند ترجمة مقتطفات المقالة، لا تحاول WPML التعامل مع هذه الأكواد المختصرة.
لكن WC تتعامل مع هذه «المقتطفات» بشكل مختلف قليلاً، وتعمل الأكواد المختصرة بالفعل، لكن WPML لا تزال تتصرف وكأنها لا تعمل، وإذا كان الكود المختصر يتضمن سمات تحتاج إلى ترجمة أو روابط داخلية يجب التعامل معها تلقائياً، فإن ذلك ليس ممكناً بسهولة.
حل بديل مؤقت
أعددنا كوداً يمكن إضافته كإضافة لتوفير هذه الوظيفة. انسخ الكود أدناه والصقه في ملف يمكنك تسميته wpmlpb-excerpts.php واحفظه في دليل wp-content/plugins/ لديك، ثم فعّل الإضافة.
<?php
/**
* Plugin Name: WPML Page Builders - Proces Excerpts
* Description: Parses shortcode strings in exceprts.
* Version: 1.0
*/
namespace WPMLPB686;
( new Excerpts() )->add_hooks();
class Excerpts {
const SEPARATOR_START = '<!--WPML_EXCERPT_START-->';
const SEPARATOR_END = '<!--WPML_EXCERPT_END-->';
public function add_hooks() {
add_filter( 'wpml_pb_shortcode_content_for_translation', [ $this, 'joinWithContent' ], 10, 2 );
add_filter( 'wpml_tm_translation_job_data', [ $this, 'excludeFromJob' ], 10, 2 );
add_action( 'wpml_pro_translation_completed', [ $this, 'splitFromContent' ], 999 );
}
public function joinWithContent( $content, $id ) {
$post = get_post( $id );
if ( $post && $post->post_excerpt ) {
$content .= self::SEPARATOR_START . $post->post_excerpt . self::SEPARATOR_END;
}
return $content;
}
public function excludeFromJob( $package, $post ) {
if ( preg_match( '/[[^]]+]/', $post->post_excerpt ) ) {
$package['contents']['excerpt']['translate'] = 0;
$package['contents']['excerpt']['data'] = '';
}
return $package;
}
public function splitFromContent( $post_id ) {
$post = get_post( $post_id );
if ( $post && false !== strpos( $post->post_content, self::SEPARATOR_START ) ) {
$start_pos = strpos( $post->post_content, self::SEPARATOR_START );
$end_pos = strpos( $post->post_content, self::SEPARATOR_END, $start_pos );
if ( false !== $end_pos ) {
$content = substr( $post->post_content, 0, $start_pos );
$excerpt = substr( $post->post_content, $start_pos + strlen( self::SEPARATOR_START ), $end_pos - $start_pos - strlen( self::SEPARATOR_START ) );
if ( $excerpt ) {
wp_update_post(
[
'ID' => $post_id,
'post_content' => $content,
'post_excerpt' => $excerpt,
]
);
}
}
}
}
}