在unctions.php中编辑没有挂钩的插件

时间:2018-06-06 作者:Alex

<?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;
    }
}
我想编辑此文本$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\' );既然插件中没有挂钩,我该怎么做?我想也许可以扩展这个类,但我不知道是否可以编辑现有的函数。

我试过了,但没用。

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;
    }
}
编辑:幸运的是,我可以从wp_options 数据库中的表,若有人知道如何从代码中执行它,那个将非常有用!

2 个回复
SO网友:cjbj

有一个过滤器可以使用,但它隐藏得很好。看看WordPress function __. 正如您所看到的,它调用了另一个函数,translate. 这就是一个叫做gettext. 您可以使用它来截取通过__.

基本上,您要做的是覆盖翻译,如果当前语言没有可用的翻译,则将与原始文本相同。像这样:

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;
    }

SO网友:kero

编辑没有适当挂钩或过滤器的其他插件或主题文件很复杂。

无法更改要翻译的文本。尝试使用插件,如Loco translate, 您应该能够用它覆盖该消息。

结束