Support » Plugin: Simple Google reCAPTCHA » Convert class to singlton

  • Suggestion so we can furthre hook into your plugin for when using other custom WordPress authentication hooks.

    add a static function instance() in order to initiate the class via this static file (as singleton instance)

    #!php
    <?php
    
    public static $instance;
    public static function instance($params = []) {
        if (!self::$instance) self::$instance = new self($params);
        return(self::$instance);
    }
    
    

    at the end of the file initiate the instance

    #!php
    <?php
    SimpleGoogleRecaptcha::instance();
    
    

    instead of the
    new SimpleGoogleRecaptcha();

    this way we can hook into with the recaptcha using this plugin, as simple as similar to this ‘partial code’

    #!php
    <?php
    add_filter( 'wp_authenticate_user', function( $user, $password ) {
        if ( !class_exists( 'SimpleGoogleRecaptcha' ) ) return( $user );
        $recaptcha = SimpleGoogleRecaptcha::instance();
        if ( !$recaptcha->verify( $user ) ) {
            return new WP_Error(
                'invalid_recaptcha', // incorrect_password
                __( 'Recaptcha Invalid, Check and try again.' )
            );
        }
    }, 50, 2 );
    
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Convert class to singlton’ is closed to new replies.