4
<?php

namespace wp_gdpr_wc\controller;

use wp_gdpr\lib\Gdpr_Language;

class Controller_Menu_Page_Wc {

    const PRIVACY_POLICY_TEXT_WOO_REQUEST = 'gdpr_priv_pov_text_woo_request';
    const NOT_CONSENT_WOO_REQUEST = 'gdpr_not_consent_woo_request';

    /**
     * Controller_Menu_Page constructor.
     */
    public function __construct() {
        add_action( 'add_on_settings_menu_page', array( $this, 'build_form_to_enter_license' ), 11 );
        //display woocommerce privacy policies
        add_action( 'gdpr_display_custom_privacy_policy', array( $this, 'display_woocommerce_privacy_policies' ) );

        add_action( 'gdpr_save_custom_privacy_policy', array( $this, 'save_woocommerce_privacy_policies' ), 10, 1 );
    }

    /**
     * build form to include licens
     */
    public function build_form_to_enter_license() {
        require_once GDPR_WC_DIR . 'view/admin/menu-page.php';
    }

    /**
     * Display WooCommerce privacy policies
     *
     * @since 1.1
     */
    public function display_woocommerce_privacy_policies() {
        $privacy_policy_strings = static::get_privacy_policy_strings();

        include GDPR_WC_DIR . 'view/admin/privacy-policy.php';
    }

    /**
     * Saves WooCommerce privacy policies
     *
     * @since 1.1
     */
    public function save_woocommerce_privacy_policies( $lang ) {
        update_option( self::PRIVACY_POLICY_TEXT_WOO_REQUEST . $lang, $_REQUEST['gdpr_priv_pov_text_woo_request'] );
    }

    /**
     * TODO re-read the default text
     *
     * Returns WooCommerce privacy policy strings
     *
     * @return array
     *
     * @since 1.1
     */
    public static function get_privacy_policy_strings() {
        $lang = new Gdpr_Language();
        $lang = $lang->get_language();

        $privacy_policy_text_woo_request = get_option( self::PRIVACY_POLICY_TEXT_WOO_REQUEST . $lang, null );
        $not_consent_woo_request         = get_option( self::NOT_CONSENT_WOO_REQUEST . $lang, null );

        if ( ! isset( $privacy_policy_text_woo_request ) ) {
            $string                           = __( 'I consent to having %s collect my personal data and use it for administrative purposes.
            For more info check our privacy policy where you\'ll get more info on where, how and why we store your data.', 'wp_gdpr' );
            $blog_name                        = get_bloginfo( 'name' );
            $privacy_policy_text_data_request = sprintf( $string, $blog_name );
            update_option( self::PRIVACY_POLICY_TEXT_WOO_REQUEST . $lang, $privacy_policy_text_data_request );
        }

        if ( ! isset( $not_consent_woo_request ) ) {
            $string                  = __( 'The consent checkbox was not checked.', 'wp_gdpr' );
            $not_consent_woo_request = $string;
            update_option( self::NOT_CONSENT_WOO_REQUEST . $lang, $not_consent_woo_request );
        }

        $privacy_policy_strings = array(
            self::PRIVACY_POLICY_TEXT_WOO_REQUEST => $privacy_policy_text_woo_request,
            self::NOT_CONSENT_WOO_REQUEST         => $not_consent_woo_request
        );

        return $privacy_policy_strings;
    }
}

I want to edit this text $string = __( 'I consent to having %s collect my personal data and use it for administrative purposes. For more info check our privacy policy where you\'ll get more info on where, how and why we store your data.', 'wp_gdpr' ); How can I do it since there are no hooks in the plugin? I thought maybe to extend the class, but I don't know if it's possible to edit an existing function.

I tried this and it's not working.

use wp_gdpr_wc\controller\Controller_Menu_Page_Wc;
class test extends Controller_Menu_Page_Wc{
    public static function get_privacy_policy_strings() {
        $lang = new Gdpr_Language();
        $lang = $lang->get_language();

        $privacy_policy_text_woo_request = get_option( self::PRIVACY_POLICY_TEXT_WOO_REQUEST . $lang, null );
        $not_consent_woo_request         = get_option( self::NOT_CONSENT_WOO_REQUEST . $lang, null );

        if ( ! isset( $privacy_policy_text_woo_request ) ) {
            $string                           = __( 'I consent to having %s collect my personal data and use it for administrative purposes.
            For more info check our privacy policyss where you\'ll get more info on where, how and why we store your data.', 'wp_gdpr' );
            $blog_name                        = get_bloginfo( 'name' );
            $privacy_policy_text_data_request = sprintf( $string, $blog_name );
            update_option( self::PRIVACY_POLICY_TEXT_WOO_REQUEST . $lang, $privacy_policy_text_data_request );
        }

        if ( ! isset( $not_consent_woo_request ) ) {
            $string                  = __( 'The consent checkbox was not checked.', 'wp_gdpr' );
            $not_consent_woo_request = $string;
            update_option( self::NOT_CONSENT_WOO_REQUEST . $lang, $not_consent_woo_request );
        }

        $privacy_policy_strings = array(
            self::PRIVACY_POLICY_TEXT_WOO_REQUEST => $privacy_policy_text_woo_request,
            self::NOT_CONSENT_WOO_REQUEST         => $not_consent_woo_request
        );

        return $privacy_policy_strings;
    }
}

Edit: Fortunately I could edit that text from wp_options table in database, If someone has any idea how to do it from code, that would be useful for times!

2 Answers 2

6

There is a filter you can use, but it is well hidden. Take a look at the WordPress function __. As you can see it calls another function, translate. And there it is, a filter called gettext. You can use it to intercept any (translated) text that is run through __.

Basically, what you do is overwrite the translation, which will be the same as the original text if no translation is available for the current language. Like this:

add_filter ('gettext','wpse305425_change_string',10,3);
function wpse305425_change_string ($translation, $text, $domain) {
    if ($text == 'I consent to ...') $translation = 'your text';
    return $translation;
    }
1
3

Editing other plugins' or themes' files that don't have proper hooks or filters is complicated.

Changing text that is going to be translated is not. Try using a plugin such as Loco translate, you should be able to overwrite that message with it.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.