qtranslate / qtranslate-xt

qTranslate-XT (eXTended) - reviving qTranslate-X multilingual plugin for WordPress. A new community-driven plugin soon. Built-in modules for WooCommerce, ACF, slugs and others.
GNU General Public License v2.0
553 stars 104 forks source link

Running a script from the php cli #1197

Open brunolustosa opened 2 years ago

brunolustosa commented 2 years ago

Hello. Tried to create a simple script for importing some stuff into WordPress. To get WP functions available to my script, I included 'wp-load.php', as I usually do all the time. However, importing wp-load.php with qtranslate-xt installed results in the script dying there. I debugged it a little, and saw that is tries to redirect the user somewhere, by checking HTTP_HOST and REQUEST_URI in $_SERVER. This is in qtranslate_core.php, in function qtranxf_init_language(). Ideally, it should not check for these variables if running from the CLI, as they will not be available. I simply created a $_SERVER, and added those two keys to it, and it worked, although with a few problems. To make it work, I set HTTP_HOST to the hostname of the site I was working on, and set REQUEST_URI to '/xx/' (xx being the language code). Tried setting it to '/', but it didn't work. The problem is, as I set REQUEST_URI to a specific language, most of the functions I called from WP passed data through filters. For example, when using get_field() (from ACF) or even get_post_meta() would return data translated to that language. To work around this, I used wpdb and did a direct query in the database, to get the whole string with the all the languages (with shortcodes). To summarize, from my understanding, running from the CLI should:

spleen1981 commented 2 years ago

It seems to be a very specific use case and non-standard use of wordpress to me. Don't think this needs to be handled within QTX. I guess standard way would be running any script of this kind from the WP admin interface (e.g. specific plugin, child theme, etc.), I can believe some hacking is needed otherwise.

brunolustosa commented 2 years ago

Hello! It is a bit specific, yes, but it's a well documented use of WP. wp-load.php is used to load the WP framework and make its functions available to scripts. As it is usually "one time run" scripts, like importing fields in a CSV file to posts, etc, a simple cli script with no output usually suffices, and is very quick to build. Sure, it could be done in a temporary plugin. Create a menu entry that when clicked, would run everything. And then, remove the plugin if everything is fine. A bit of unnecessary extra work, though.

herrvigg commented 2 years ago

Indeed it's for a specific use case. I can't tell yet if this is easily feasible or not. I leave it as a feature request for now.