debugging 激活WordPress调试时尝试访问数组偏移量时出错

holgip5t  于 2022-11-14  发布在  WordPress
关注(0)|答案(1)|浏览(146)

我在public_html/wp-config.php中执行define( 'WP_DEBUG', true );,并在wp-admin中看到此错误消息
注意:尝试访问第24行中bool类型值的数组偏移量,位置为/home/urbanit 1/public_html/wp-content/plugins/no-captcha-captcha-for-woocommerce/base-class. php
php中的第24行:

self::$site_key = self::$plugin_options['site_key'];

请帮忙解决这个问题
密码:

<?php

class WC_Ncr_No_Captcha_Recaptcha {

    /** @var string captcha site key */
    static private $site_key;

    /** @var string captcha secrete key */
    static private $secret_key;

    static private $theme;

    static private $language;

    static protected $error_message;

    static protected $plugin_options;

    public static function initialize() {

        self::$plugin_options = get_option( 'wc_ncr_options' );

        self::$site_key = self::$plugin_options['site_key'];

        self::$secret_key = self::$plugin_options['secrete_key'];

        self::$theme = self::$plugin_options['theme'];

        self::$language = self::$plugin_options['language'];

        self::$error_message = self::$plugin_options['error_message'];

        add_action( 'plugins_loaded', array( __CLASS__, 'load_plugin_textdomain' ) );

        // initialize if login is activated
        if ( ( isset( self::$plugin_options['captcha_wc_registration'] ) && self::$plugin_options['captcha_wc_registration'] == 'yes' ) || ( isset( self::$plugin_options['captcha_wc_login'] ) && self::$plugin_options['captcha_wc_login'] == 'yes' ) || ( isset( self::$plugin_options['captcha_wc_password_reset'] ) && self::$plugin_options['captcha_wc_password_reset'] == 'yes' ) ) {
            add_action( 'wp_head', array( __CLASS__, 'header_script' ) );
        }
    }

    public static function load_plugin_textdomain() {
        load_plugin_textdomain( 'wc-no-captcha', false, basename( dirname( __FILE__ ) ) . '/languages/' );
    }

    /** reCAPTCHA header script */
    public static function header_script() {

        $lang_option = self::$plugin_options['language'];

        // if language is empty (auto detected chosen) do nothing otherwise add the lang query to the
        // reCAPTCHA script url
        if ( isset( $lang_option ) && ( ! empty( $lang_option ) ) ) {
            $lang = "?hl=$lang_option";
        } else {
            $lang = null;
        }

        echo '<script src="https://www.google.com/recaptcha/api.js' . $lang . '" async defer></script>' . "\r\n";
    }

    /** Output the reCAPTCHA form field. */
    public static function display_captcha() {

        echo '<div class="g-recaptcha" data-sitekey="' . self::$site_key . '" data-theme="' . self::$theme . '"></div>';
    }

    /**
     * Send a GET request to verify captcha challenge
     *
     * @return bool
     */
    public static function captcha_wc_verification() {

        $response = isset( $_POST['g-recaptcha-response'] ) ? esc_attr( $_POST['g-recaptcha-response'] ) : '';

        $remote_ip = $_SERVER["REMOTE_ADDR"];

        // make a GET request to the Google reCAPTCHA Server
        $request = wp_remote_get(
            'https://www.google.com/recaptcha/api/siteverify?secret=' . self::$secret_key . '&response=' . $response . '&remoteip=' . $remote_ip
        );

        // get the request response body
        $response_body = wp_remote_retrieve_body( $request );

        $result = json_decode( $response_body, true );

        return $result['success'];
    }

    public static function on_activation() {
        if ( ! current_user_can( 'activate_plugins' ) ) {
            return;
        }
        $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
        check_admin_referer( "activate-plugin_{$plugin}" );

        $default_options = array(
            'captcha_wc_registration' => 'yes',
            'captcha_wc_comment'      => 'yes',
            'theme'                => 'light',
            'error_message'        => __('<strong>ERROR</strong>: Please confirm you are not a robot', 'wc-no-captcha')
        );

        add_option( 'wc_ncr_options', $default_options );
    }

    public static function on_uninstall() {
        if ( ! current_user_can( 'activate_plugins' ) ) {
            return;
        }

        delete_option( 'wc_ncr_options' );
    }
}
gt0wga4j

gt0wga4j1#

您的问题是self::$plugin_options = get_option( 'wc_ncr_options' );,它没有提供默认值,因此如果不保存该选项,它将只是return false,因此当您尝试将其用作数组时,您会得到这些错误,实际上您所做的是$test = false; $test['site_key'],它将始终因某些数组错误而失败。
您应该为get_option提供默认值
例如,G

get_option( 'wc_ncr_options', ['site_key' => "some value", ...] );

相关问题