Feature request, issues, related changes with patch (long)
-
My migration test from qtranslate continues… =)
One thing I implemented to qtranslate was a way for themes to implement “this content is available in languages x, y, z” feature. Here’s a patch for this. And some related issues I noticed while coding.
1. Post / Language flags
– If title AND content is removed from a post, it can not be saved. At least one must be present.
– Personally I like to create title for a post for all languages and leave the content empty.
The content is most important piece of data to be present, if it is missing the admin interface should not show a flag. If the flag is present this could make user to believe that the page has been completely translated.
This is why I propose that the flag should be present only when the content has been created for the language.
2. Unnecessary Filters
add_filter( ‘the_post’, ‘wpm_translate_post’, 5 );
add_filter( ‘the_title’, ‘wpm_translate_string’, 5 );
add_filter( ‘the_content’, ‘wpm_translate_string’, 5 );
add_filter( ‘the_excerpt’, ‘wpm_translate_string’, 5 );All of these are filters of theme functions and they are only usable inside the loop. If there is a loop the $post is already filled with data and the data has already been run thru ‘get_pages’ and ‘posts_results’ filters.
I tracing translate term and object calls and come to conclusion that these should be disabled. I admit that this was very limited test.
But what are these filters as I could not find them from WordPress at all. And I still bet that they would have the same reason to be disables as the above do.
add_filter( ‘post_title’, ‘wpm_translate_string’, 5 );
add_filter( ‘post_excerpt’, ‘wpm_translate_value’, 5 );
add_filter( ‘post_content’, ‘wpm_translate_value’, 5 );3. Feature Available languages
– I just iterate the language string and create an array to the related post object to be available everywhere.
– “!isset($object->wpm_content_languages))” was necessary here as the object was translated multiple times. I don’t know should it be removed even if the redundant filters are disabled.
…
I can present example theme code how to utilize on the themes later on, if this seems to be a good idea.
And here’s the patch.
From 9837373ceb242c5a67d130b82bdf25f22a5c600a Mon Sep 17 00:00:00 2001 From: Mikael Willberg <[email protected]> Date: Thu, 7 Dec 2017 04:12:10 +0200 Subject: [PATCH] Add feature available languages and related changes --- includes/admin/class-wpm-admin-posts.php | 4 +++- includes/class-wpm-posts.php | 16 +++++++++------- includes/wpm-translation-functions.php | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/includes/admin/class-wpm-admin-posts.php b/includes/admin/class-wpm-admin-posts.php index f090b37..548111d 100644 --- a/includes/admin/class-wpm-admin-posts.php +++ b/includes/admin/class-wpm-admin-posts.php @@ -100,7 +100,9 @@ class WPM_Admin_Posts { $post = wpm_untranslate_post( get_post() ); $output = array(); - $text = $post->post_title . $post->post_content; + // MIG: Only content needs primary indicator + // $text = $post->post_title . $post->post_content; + $text = $post->post_content; $strings = wpm_value_to_ml_array( $text ); $languages = wpm_get_lang_option(); diff --git a/includes/class-wpm-posts.php b/includes/class-wpm-posts.php index facbe5a..8cfdc82 100644 --- a/includes/class-wpm-posts.php +++ b/includes/class-wpm-posts.php @@ -35,13 +35,15 @@ class WPM_Posts extends WPM_Object { public function __construct() { add_filter( 'get_pages', array( $this, 'translate_posts' ), 5 ); add_filter( 'posts_results', array( $this, 'translate_posts' ), 5 ); - add_filter( 'post_title', 'wpm_translate_string', 5 ); - add_filter( 'post_excerpt', 'wpm_translate_value', 5 ); - add_filter( 'post_content', 'wpm_translate_value', 5 ); - add_filter( 'the_post', 'wpm_translate_post', 5 ); - add_filter( 'the_title', 'wpm_translate_string', 5 ); - add_filter( 'the_content', 'wpm_translate_string', 5 ); - add_filter( 'the_excerpt', 'wpm_translate_string', 5 ); +//MIG: Do these even exsist ? And most likely they already been processed like below. +// add_filter( 'post_title', 'wpm_translate_string', 5 ); +// add_filter( 'post_excerpt', 'wpm_translate_value', 5 ); +// add_filter( 'post_content', 'wpm_translate_value', 5 ); +//MIG: Why are these needed ? These have already been processed by get_pages and posts_results filters ('translate_posts') +// add_filter( 'the_post', 'wpm_translate_post', 5 ); +// add_filter( 'the_title', 'wpm_translate_string', 5 ); +// add_filter( 'the_content', 'wpm_translate_string', 5 ); +// add_filter( 'the_excerpt', 'wpm_translate_string', 5 ); add_filter( 'the_editor_content', 'wpm_translate_string', 5 ); add_action( 'parse_query', array( $this, 'filter_posts_by_language' ) ); add_filter( "get_{$this->object_type}_metadata", array( $this, 'get_meta_field' ), 5, 3 ); diff --git a/includes/wpm-translation-functions.php b/includes/wpm-translation-functions.php index 56e2536..d0dafb1 100644 --- a/includes/wpm-translation-functions.php +++ b/includes/wpm-translation-functions.php @@ -373,6 +373,23 @@ function wpm_translate_object( $object, $lang = '' ) { case 'post_excerpt': case 'description': case 'post_content': + //MIG: Languages are analyzed for post_content only + //MIG: WPM 2.1.6 objects are translated multiple times (unnecessary filters ?) + //MIG: and so languages are analyzed only at the first pass + if (($key == 'post_content') && !isset($object->wpm_content_languages)){ + $object->wpm_content_languages=array(); + + //MIG: XXX Should Serialized or JSON be analyzed too ???!!! + + if ( wpm_is_ml_string( $content ) ) { + foreach(wpm_string_to_ml_array($content) as $mig_item => $mig_item_data) { + if (!empty(trim($mig_item_data))){ + $object->wpm_content_languages[]=$mig_item; + } + } + } + } + if ( is_serialized_string( $content ) ) { $object->$key = serialize( wpm_translate_value( unserialize( $content ), $lang ) ); break; -- 2.14.1.windows.1
- The topic ‘Feature request, issues, related changes with patch (long)’ is closed to new replies.