Making Loco Translate Wordpress plugin to identify MU-PLUGINS - wordpress

I'm using Loco Translate translation plugin with my Wordpress 4.7 installation.
I have my MU-Plugin correctly registered and properly configured to load its text domain.
Yet Loco Translate only manages to recognize themes and regular plugins, therefore I cannot use Loco Translate UI for translating my plugin.
Help will be appreciated.
Thanks

Welcome to stack overflow Jonathan Darchy!
Here is an example of adding an unregistered MU plugin to Loco Translate
Raw, taken from this gist
I think it answers your question:
<?php
/**
* MU plugins inside directories are not returned in `get_mu_plugins`.
* This filter modifies the array obtained from Wordpress when Loco grabs it.
*
* Note that this filter only runs once per script execution, because the value is cached.
* Define the function *before* Loco Translate plugin is even included by WP.
*/
function add_unregistered_plugins_to_loco( array $plugins ){
// we know the plugin by this handle, even if WordPress doesn't
$handle = 'foo-bar/foo-bar.php';
// fetch the plugin's meta data from the would-be plugin file
$data = get_plugin_data( trailingslashit(WPMU_PLUGIN_DIR).$handle );
// extra requirement of Loco - $handle must be resolvable to full path
$data['basedir'] = WPMU_PLUGIN_DIR;
// add to array and return back to Loco Translate
$plugins[$handle] = $data;
return $plugins;
}
add_filter('loco_plugins_data', 'add_unregistered_plugins_to_loco', 10, 1 );

Related

How can I Override the woocommerce_download_file_redirect function?

I was asked to help a friend with virtual product download issues with their WooCommerce site. I solved the problem in the short term by moving them off their Hostgator shared hosting to a dedicated VPS through Digital Ocean (DO). However, I need to take this one step further as storing their files on droplets is going to get expensive really fast.
What I am wanting to do is use DOs Spaces, which are effectively S3 buckets. The tests we have done suggest this will be a really good setup as the storage space is very cheap compared to droplets, and we can use a CDN feature. Unfortunately, I am having issues working out how to best integrate WooCommerce digital downloads with Spaces. WooCommerce provides three download options:
The first two seem to proxy the file through your webserver to hide the origin URL
The third option 'Redirect (insecure)' updates the database to show you've downloaded the product, and then redirects you to a static link.
I want to use the redirect option and override the plugin function woocommerce_download_file_redirect with my own code that would use DOs API to generate a pre-signed download URL for a protected resource that is valid for no more than 60 seconds, thus protecting the download from unauthorized users.
After looking through the documentation, I came across the function which handles the redirect, and on line 25 of the same class the line of code that registered that action. Knowing about these functions I wrote a simple plugin to test overriding that code
<?php
/**
* plugin meta here (name, uri, description etc)
*/
function download_redirect_override() {
var_dump('test to see if my code works');
// Once I get this to work, this method will take the product URL
// explode it on '/' to get the parts required to make an API call to Spaces
// to generate a pre-signed URL with a 60-second life-span
// this URL will then be returned to the user using Header('Location: '.$url); die;
}
add_action( 'init', function() {
global $WC_Download_Handler;
// remove the default action so I can replace it with my one that generates short-life download URLs
remove_action( 'woocommerce_download_file_redirect', array( $WC_Download_Handler, 'download_file_redirect' ), 10, 2 );
add_action( 'woocommerce_download_file_redirect', 'download_redirect_override', 10, 2 );
});
Once my plugin is enabled, I try hitting the download button and WooCommerce ignores my code continuing to use the default function. I know my add_action( 'init', ..) is getting called because I have inserted a var_dump('test'); in the function and that prints to my screen.
I am not a PHP developer so I don't fully understand the nuances of Word Press. I am hoping someone could either point out what I am doing wrong or point me in the right direction. Thanks.
After taking a break for a few days, I spent last night and finally got it working.
While the code below works, I may well be doing it wrong so I am still open to feedback. Here is my code:
The function `spaces_download` is in another file, I have not posted it below as it is not relevant, however, it ends with a Header('Location: '.$Url); Exit();
<?php
/**
* Plugin Name: <redacted>
* Plugin URI: <redacted>
* Description: <redacted>
* Version: 0.0.1
* Author: <redacted>
* Author URI: <redacted>
*/
add_action('plugins_loaded', function() {
// Override the default Woo Commerce function that handles the redirect download
// The third arg, '1', is the priority, we use this to override the default
add_action( 'woocommerce_download_file_redirect', 'spaces_download', 1, 1 );
}

How to make image availible at an arbitrary URL with WordPress

Having a working WordPress installation, is it possible to have an image at an arbitrary URL on the WordPress site?
E.g. http://mysite.wordpress.com/this/is/an/arbitrary/path/image.png
you can use wp_upload_bits() function with some parameters.
here is an Example for that.
<?php
if(isset($_FILES['your-file-input']['name']) && sizeof($_FILES['your-file-input']['name'])){
$file = wp_upload_bits(sanitize_file_name($_FILES['your-file-input']['name']),null,file_get_contents($_FILES['your-input-file']['tmp_name']),'path/to');
print_r($file);// it returns an array of file information [url/size/type...]
}

WordPress constant scope using define method

I'm defining plugin path as a constant through constant define method as shown below.
define( 'MY_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
It is accessible within plugin all files and themes. But when I call this constant in another plugin this constant becomes undefined.
How I can get this plugin constant in another plugin? Any help will be appreciated.
Thanks,
Probably your problem is the order that wordpress is calling your plugins: the plugins that sets the constant is loaded after the one who calls it.
A better explanation of how to force your plugin be loaded first can be found here. I'm putting here a quote of the part that matters:
The order that plugins are loaded (as of WP 2.9.1 at least) is determined by the order of an array stored as "active_plugins" in the WP options table. Whenever a plugin is activated, it is added to this array, the array is sorted alphabetically by plugin name, and the array is saved to the DB.
Fortunately, there is a convenient action hook, "activated_plugins", that is called after the active plugins array is saved to the DB. This allows you to manipulate the order of the plugins stored in this array after the array is initially saved.
You'll have to the following PHP code in the plugin who defines the constant, then deactivate and reactivate it (copied from the link I providaded above).
function this_plugin_first() {
// ensure path to this file is via main wp plugin path
$wp_path_to_this_file = preg_replace('/(.*)plugins\/(.*)$/', WP_PLUGIN_DIR."/$2", __FILE__);
$this_plugin = plugin_basename(trim($wp_path_to_this_file));
$active_plugins = get_option('active_plugins');
$this_plugin_key = array_search($this_plugin, $active_plugins);
if ($this_plugin_key) { // if it's 0 it's the first plugin already, no need to continue
array_splice($active_plugins, $this_plugin_key, 1);
array_unshift($active_plugins, $this_plugin);
update_option('active_plugins', $active_plugins);
}
}
add_action("activated_plugin", "this_plugin_first");
Hope it helps!

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' );

Drupal language negotiation

I have a multi-language drupal setup (2 languages, english default). I want users to receive always content in the other language (lets say spanish) on initial page request, but keep english as default language for future language switch. So users will be redirected on initial load to site.com/es, but through the language switch will be able to go to site.com/ (which is english).
Any suggestions? Thank you.
(Apache, PHP)
Redirect users using preprocess in template.php file of your theme:
Approximate code:
/**
* Override or insert variables into the page templates.
*
* #param $vars
* An array of variables to pass to the theme template.
* #param $hook
* The name of the template being rendered ("page" in this case.)
*/
function THEMENAME_preprocess_page(&$vars, $hook) {
global $language;
if ($language->language == 'en') { // Add here some checking for page, see print_r($vars)
drupal_goto(url().'/es/'.$GET['q']); //goto es version
}
}

Resources