woocommerce / woocommerce-paypal-payments

https://wordpress.org/plugins/woocommerce-paypal-payments/
GNU General Public License v2.0
61 stars 47 forks source link

Add WPML compatibility to the settings (223) #2308

Closed danieldudzic closed 3 weeks ago

danieldudzic commented 1 month ago

Description

This PR adds a filter woocommerce_paypal_payments_settings_value that allows for filtering of plugin settings value.

Note

I wasn't able to create a compat filter from the compat modules, as the compat module always executes later.

MU plugin to test

Save the following code to a file, and add it to the wp-content/mu-plugins folder:

<?php
/*
 * Plugin Name: WooCommerce PayPal Payments Settings Filter
 * Description: Adds a filter to translate WooCommerce PayPal Payments settings before other plugins are loaded.
 * Author: Danny
 * Version: 1.0
 */

add_filter(
    'woocommerce_paypal_payments_settings_value',
    function( $value, $key ) {
        return ! is_array( $value ) ? apply_filters( 'wpml_translate_single_string', $value, 'admin_texts_woocommerce-ppcp-settings', '[woocommerce-ppcp-settings]' . $key ) : $value;
    },
    10,
    2
);

Steps to Test

  1. Install WPML, WooCommerce WPML and String Translations.
  2. Set it up.
  3. Go to the String Translation page in the WPML settings.
  4. Search for Pay via PayPal.
  5. Click on Can't find the strings you're looking to translate? and then on the Choose texts for translation button.
  6. Add the Pay via PayPal. to translation and actually translate it.
  7. Go to the Checkout page with the translated string.
  8. Verify that the translated string appears correctly.
stracker-phil commented 1 month ago

Nice workaround, and works as expected. I understand, it's more of a temporary solution, until implementing PCP-1241.

@danieldudzic is there a reason for the mu-plugin, instead of adding that filter directly into the plugin?

From my perspective, adding that code into the Settings.php file would be a better choice (e.g. in the top of the load() method). That way, we can adapt, or remove, the logic directly when needed, vs. having a mu-plugin file on client websites that is not required anymore

danieldudzic commented 1 month ago

Nice workaround, and works as expected. I understand, it's more of a temporary solution, until implementing PCP-1241.

Correct, although I'm don't think PCP-1241 will be happening anytime soon.

@danieldudzic is there a reason for the mu-plugin, instead of adding that filter directly into the plugin?

It's basically a test work-around to be able to hook into the filter before it gets executed.

From my perspective, adding that code into the Settings.php file would be a better choice (e.g. in the top of the load() method). That way, we can adapt, or remove, the logic directly when needed, vs. having a mu-plugin file on client websites that is not required anymore.

I would prefer not to pollute the core plugin code with 3rd party compatibility code. All 3rd party compatibility code should live exclusively in the ppcp-compat module - and unfortunately, I haven't found a good solution to load the add_filter before it gets called on the Settings with the current order of loading.

I believe this still is an improvement, as WPML customers willing to overwrite settings won't need to modify plugin files, but will be able to add a "fix" externally.