I have something similar as here.

In a third party plugin class we have a function that I want to override by extending that class.

The original class function:

class Import_Facebook_Events_Facebook {
    public function get_location( $facebook_event ) {

        if ( !isset( $facebook_event->place->id ) ) {
            return null;
        }
        //other code here
}

What I tried, but this didn’t work (no effect):

class Import_Facebook_Events_Facebook_Ext extends Import_Facebook_Events_Facebook {
    public function get_location( $facebook_event ) {

        if ( !isset( $facebook_event->place->id ) ) {
            $facebook_event->place->id = ''; //added this line
            //return null;
        }
        //other code here
}

new Import_Facebook_Events_Facebook_Ext();

What’s wrong here? How can I get the desired effect?

The original Import_Facebook_Events_Facebook() class is instantiated from another class:

class Import_Facebook_Events{

    private static $instance;

    public static function instance() {
        if( ! isset( self::$instance ) && ! (self::$instance instanceof Import_Facebook_Events ) ) {
            self::$instance = new Import_Facebook_Events;

            self::$instance->facebook = new Import_Facebook_Events_Facebook();

        }
        return self::$instance; 
    }
}

And the above class is instantiated from a separate function:

function run_import_facebook_events() {
    return Import_Facebook_Events::instance();
}

1 Answer
1

Finally, I figured out how to solve my problem:

class Import_Facebook_Events_Facebook_Ext extends Import_Facebook_Events_Facebook {

    function get_location( $facebook_event ) {

        if ( !isset( $facebook_event->place->id ) ) {
            $facebook_event->place->id = '';
            //return null;
        }
        //other code here

        return $event_location;
    }
}

$new_ife_events = run_import_facebook_events();
$new_ife_events->facebook = new Import_Facebook_Events_Facebook_Ext();

A question: the last two code lines must be included in a function/action?

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *