Error Trying to access array offset while activating WordPress debug

abbas

i do define( 'WP_DEBUG', true ); in public_html/wp-config.php and I saw this error messag in wp-admin

Notice: Trying to access array offset on value of type bool in /home/urbanit1/public_html/wp-content/plugins/no-captcha-recaptcha-for-woocommerce/base-class.php on line 24

line 24 in base-class.php :

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

Please help to solve this problem

code:

<?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' );
    }
}
Barkermn01

your problem is self::$plugin_options = get_option( 'wc_ncr_options' ); it does not provide a default value so if the option is not saved it will just return false so then when you try and use it as an array your getting these errors, effectively what your doing is $test = false; $test['site_key'] that will always fail with some array error.

you should provide default values for the get_option

E.G

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Codeigniter 4 while unlinking shows the error: Trying to access array offset on value of type null

Error : Trying to access array offset on value of type int

Trying to access array offset on value of type bool error

mySQL error 'Trying to access array offset on value of type null'

error : Trying to access array offset on value of type null ( php 8 )

Get Error "Warning: Trying to access array offset on value of type null in"

Trying to access array offset on value of type null in Laravel while adding multiple values in database

PHP 7.4 Trying to access array offset

Trying to access array offset on value of type null

Trying to access array offset on value of type null in

How to solve PHP ACF error: "Trying to access array offset on value of type bool"?

Custom Class for a plugin options give error, Notice: Trying to access array offset on value of type bool in C

how can i solve "Trying to access array offset on value of type int" error in php

DATE_FORMAT in query generate this error: Notice: Trying to access array offset on value of type bool in

How to solve error: Trying to access array offset on value of type null with Prepared Statement PHP

Error: Trying to access array offset on value of type null in my PHP code

Error while trying to debug D program

Showing this error while trying to debug an App in Flutter

Illegal string offset while trying print the value with in the array

php 7.4 xampp Trying to access array offset on value of type null

Trying to access array offset on value of type null in laravel

Trying to access array offset on value of type bool in PHP 7.4

Warning: Trying to access array offset on value of type bool

php Notice: Trying to access array offset on value of type null in

Trying to access array offset on value of type null...in login details

Notice: Trying to access array offset on value of type null in line(static)

Message: Trying to access array offset on value of type null

Laravel 8 ErrorException: Trying to access array offset on value of type null

Notice: Trying to access array offset on value of type bool on line 83