web-dev-qa-db-ja.com

Functions.phpでフックなしでプラグインを編集する

<?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テーブルからそのテキストを編集することができます。誰かがコードからそれをする方法を何か考えがあるならば、それは時のために役に立つでしょう!

4
Alex

あなたが使用できるフィルタがあります、しかしそれはよく隠されています。 WordPress関数__ を見てください。ご覧のとおり、別の関数 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;
    }
6
cjbj

適切なフックやフィルタを持たない他のプラグインやテーマのファイルを編集するのは複雑です。

翻訳される予定のテキストを変更することはできません。 Loco translate のようなプラグインを使ってみると、そのメッセージを上書きすることができるはずです。

3
kero