Wordpress - polylang in command launch post traduction - wordpress

Today I need some help with Wordpress, as is is far from being my cup of tea.
Currently, I'm tring to create a plugin to export translation from post in a xliff file then reimport this xliff file to set the translation. This plugin we only be used If wordpress uses polylang for translations. For now I'm only exporting meta data.
I've managed to find how polylang is linking two post, saying this one is in english and this one is in french. So for now I have a command to export a post in a xliff file, and a command to import a traduction from an xliff file.
Everything work fine as long, as the post I'm trying to translate already has existing translation (basically I'm just updating meta data).
But my problem, is when I have a post in french, but I do not have a matching post in english.
What I wanted to do was to do, was to create the missing post and after overwrite the meta data.
But I do not know how to create the missing post as a copy of my post and saying that this one is in english. Does anyone know how I can do that from my command ?
Thanks

Actually I found what I wanted. Polylang has commande line for duplicating a post in an other language. And I just had to use \WP_CLI::runcommand
$options = array(
'return' => true,
'parse' => 'json',
'launch' => false,
'exit_error' => true,
);
$command = 'pll post duplicate ' . $idPost . ' ' . $destLocale;
\WP_CLI::runcommand($command, $options);

Related

Getting notice is wordpress "WP_Scripts::localize was called incorrectly"

Complete message:
Notice: WP_Scripts::localize was called incorrectly. The $l10n parameter must be an array. To pass arbitrary data to scripts, use the wp_add_inline_script() function instead. Please see Debugging in WordPress for more information. (This message was added in version 5.7.0.) in /home3/dduconne/public_html/wp-includes/functions.php on line 5313
Appeared just after updating wordpress to 5.7.0
Ok since there quite a few views and the previous answer just throws it under the rug.
Yes it is a warning now, but who knows what will happen, it is a warning for a reason, disabling something is not resolving it, there is a reason devs set it as a warning for now.
While we wait for plugin developers to resolve it on their ends, you can find out the source of the problem by enabling php_xdebug in php. I recommend not leaving it after debugging, as I am not sure about the performance cost of it being enabled.
php_xdebug will return a stack of all files affected, from there you can trace it to the source of the problem. The fix once found the source is quite easy. Culprit most likely is wp_localize_script() which requires last parameter to be an array()!
So you would find something like this:
wp_localize_script( 'handle', 'object_name', 'l10n_culprit_string' );
Which should be turned into:
wp_localize_script( 'handle', 'object_name', array( 'l10n_culprit_string' ) );
Solution to problem came from here
This is a new warning appearing in Wordpress 5.7. If you don't want to see it, and still want to have WP_DEBUG set to true, it is possible to disable the message by adding the following for example in your theme's functions.php:
add_filter('doing_it_wrong_trigger_error', function () {return false;}, 10, 0);
It is now a notice if you are using wp5.7 and php7.4, but changing to php8 it may gets reported as error. For me, when i changed (in my own code) the passed value to array (yes, probably some changes also needed in the involved js), everything worked just fine.
This is because one of the PHP script in your wordpress installation uses wp_localize_script() to pass some PHP variables to a JS script through AJAX request, and it's not good practice anymore.
This can be inside a plugin or inside your custom theme or child-theme's functions.php.
If wp_localize_script() is not in a child-theme or custom theme's functions.php, try this :
backup your website
deactivate all plugins
reactivate them one by one until you get the notice again
when the notice shows up again, this means the plugin you just reactivated uses wp_localize_script() the wrong way
if it's not up-to-date, try updating it (if you want to) : maybe the notice is going to disappear
if it's already up-to-date, or you don't want to update it, search inside wp-content/plugins the corresponding plugin's folder
open it and try to find the file(s) where wp_localize_script() is called, you can also use CLI with your server's terminal to find string in files. For example, if your server runs on Linux : grep -H -r "wp_localize_script()" /root/pathtopluginfolder | cut -d: -f1
Then, replace the code as follow :
Old one should look almost like this :
wp_enqueue_script( 'wpdocs-my-script', 'https://url-to/my-script.js' );
wp_localize_script( 'wpdocs-my-script', 'MYSCRIPT', array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'otherParam' => 'some value',
) );
New one :
wp_enqueue_script( 'wpdocs-my-script', 'https://url-to/my-script.js' );
wp_add_inline_script( 'wpdocs-my-script', 'const MYSCRIPT = ' . json_encode( array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'otherParam' => 'some value',
) ), 'before' );
Note that you need to add 'before' as the third parameter to the wp_add_inline_script function.
Once it's done, the notice shouldn't show anymore. If so, contact the plugin dev to tell him about this so that it can be part of the next update.
For more informations, see Jules Colle's comment on this page : https://developer.wordpress.org/reference/functions/wp_add_inline_script/

Wordpress and Woocmmerce - how to access settings from theme?

Im trying to pull the settings from Woocommerce admin. I found this:
http://oik-plugins.eu/woocommerce-a2z/oik_api/woocommerce_settings_get_option/
$string = woocommerce_settings_get_option( $option_name, $default );
It looks to be a public function but I cannot access from my theme files. It just gives me Fatal error: Call to undefined. Anyone have any idea how you can access the setting from the theme?
I'm trying to get 'woocommerce_frontend_css_primary', $colors['primary'] so can tie them into the rest of the theme. Woocommerce currently just write the values directly to .less file.
Woocommerce docs are a bit misleading, but it turns out there is another function called get_option... as long as you know the name of the option you can use. EG. get array of front end colors:
$woo_styles = get_option( 'woocommerce_frontend_css_colors' );

Wordpress WPML not translating get_option

i am trying to translate my plugin options through WPML but it is not working.
Here is how i have placed my string in the plugin file
get_option(_e('my_label','my-text-domain'));
I have already scan my plugin through the WPML and have done translation in "German" while default is English.
Can anyone help me.
Thanks
The issue is that _e outputs the translated text.
You want __ (double underscore), which will return the translated text.
I would explore other solutions to this problem. In general you shouldn't be using translation to determine what option to pull from the database. The functionality of your plugin should not hinge on whether or not the translated text is a valid option name.
An alternative approach may be to use get_locale to fetch the current locale and then use that to determine the option name:
$option_name = get_locale() . 'my_label';
$label = get_option( $option_name );
Now you can still get the localized version of your option, without depending on translators to input the correct option name.
If you have to use the translation approach, I would use _x to explain that the translated text should be an option key.
get_option( _x('my_label', 'Must be a valid option name', 'my-text-domain') );
Provided that you have multiple entries in your meta table per language, try the double underscore function?:
get_option( __( 'my_label','my-text-domain' ) );

import .doc files to wordpress

I am modifying a website on wordpress (codex) and I would like to import all the .doc files (about 200) from the old website and make them into posts on the wordpress website.
Any ideas how to do this bulk import or is the only way to copy and paste each one, which will take a long time.
There Is a way to do this - but as far as I know - it is not easy ..
I struggled with it one time - but could achieve it in the end .
The reason is that a WORD *.doc is a complicated format , and even simple Copy&Paste can make problems (as it actually copies also formatting)
You need to use the COM interface in php , turn the DOC to a TXT file or a string , and then create the post with wp_insert_post( $my_post );
so - opening the DOC . (you need to loop for bulk )
$filename="file.doc";
$TXTfilename = $filename . ".txt";
$word = new COM("word.application") or die("Unable to instantiate Word object");
$word->Documents->Open($filename);
$word->Documents[1]->SaveAs($TXTfilename ,2);// '2' for txt format
$word->Documents[1]->Close(false);
$word->Quit();
$word->Release();
$word = NULL;
unset($word);
$my_doc_string = file_get_contents($TXTfilename); //write file
unlink($TXTfilename);
Then, either read a txt file , or if in the same function use the $my_doc_string directly.. :
$title_p = 'mypost no'.$i;
$my_post = array(
'post_title' => $title_p,
'post_content' => $my_doc_string,
'post_status' => 'publish',
'post_author' => 1
);
$post_id = wp_insert_post( $my_post );
wp_insert_post( $my_post );
NOTE : the COM interface , as far as I know , Is available only on the WINDOWS vesion of PHP , and you will also need to have MS WORD installed ...
some more reading for you :
http://php.net/manual/en/book.com.php
http://docstore.mik.ua/orelly/webprog/php/ch15_03.htm
http://www.gsdesign.ro/blog/extracting-text-from-word-documents-in-php-with-com-objects/
EDIT I -
After reading your question again - if you have an OLD website - why do you need the DOC files ? you might be able to do that with an HTML PARSER (if the OLD website has the same content as the DOCs - but in HTML)
EDIT II - unbelievably - today I have stumbled into this plugin, it might be of some help although I did not tried it .
http://wordpress.org/extend/plugins/auto-poster/screenshots/
l downloaded a plugin https://wordpress.org/plugins/mammoth-docx-converter/ that is able to import word documents into wordpress editor for publishing. Format mapping is not 100% but does the basic stuff. All i need to do now is to be able to populate metadata in association with that particular document (custom post) upon import using particular key words from the post Content to asynchronously populate the meta boxes. I am also trying to generate excerpts automatically from a particular paragraph (second paragraph) of the document.I read about wp all import plugin and find it quite close to what i want but cant really figure out how word documents can be imported at the same time automating the operations stated above. I was able to come up with an XML file from source using WRX specification leveraging exported Content from my Website and then uploading again for test purposes but that is not what i really want to achieve. Is there anyway wp all import plugin can be customized to do what i want?
I also find your php approach quite suitable and might be more appropriate for what i need it to do and my goal. Important for me is an automated import of the word document as a post keeping the formatting intact and and then asynchronously populating associated meta data automatically. Do you have any recommendations based on the above php COM Code?

Wordpress Plugin - Self-Hosted Update

I'm developing a Wordpress plugin that requires updates, although the plugin version is being checked from my server. There are several plugins that I have developed which use the exact same server to check for new versions. The problem I'm experiencing is that when all the plugins require an update and I click View Details, one of the plugins will show details of the update (version, description, and etc), but the other plugins won't show any information. After some debugging I can see that the server is returning data for sure.
My question is, how can I apply the plugins_api filter multiple times without it conflicting with the other plugins?
Your observation is right. It is not obvious. Even the book of Brad and Ozh (Plugin development ed. Wrox) includes an error in the example on page 267 in the chapter "make your own API repository".
Like you, I spent (lost) time to find the issue with a two plugins in alternate API...
The solution:
Remember that that first parameter in the WP filter is the original value passed to the filter.
So to concatenate the filters (listed by plugins using alternate api)... the first line must be:
function xiliw_altapi_information( $false, $action, $args ) {
$plugin_slug = plugin_basename( __FILE__ );
// Check if this plugins API is about this plugin
if( $args->slug != $plugin_slug ) {
return $false; // var to conserve the value of previous filter of plugins list in alternate api. fixes book error not val false
}
// POST data to send to your API
$args = array(
'action' => 'plugin_information',
'plugin_name' => $plugin_slug,
'version' => $transient->checked[$plugin_slug],
'registration' => $this->registration
);//../..
By doing this test, each time the list of hooks is called, only one - the concerned plugin - gives the right answer to display the information for the splash window.
If I have time, I probably will publish soon a more complete article about a class to manage this alternate powerful API and how to add it to a -private- plugin.

Resources