Access Child Class of Plugin Main Class Instance

Question

In a WordPress plugin where the author has defined a parent class and uses a standard instance method for other plugin authors to access the plugin example:

function WP_Plugin() {
     return WP_PLUGIN_NAME::instance();
}

In the __construct function of initializing this plugin class it uses require_once on several other classes example:

require_once( 'includes/class-wp-plugin-child-class.php' );

This class also has a __construct function (which is run when the main class is initialized.

The problem is that the other classes that are loaded from the main class do not have an instance method or any way of directly accessing them (that I can find)

How do I access any of the functions/hooks/etc. in those other classes. Without of course redeclaring the class i.e.:

$subclass = new WpPluginChildClass();

Will not work (it will trigger all of the __construct functions, etc. to run again).


Edit – adding much more detail and exact code of sample plugin trying to do this with.

I’ve run into this with several different larger plugins (especially relating to WooCommerce). The current one I’m attempting to work on is a paid plugin so I feel it’s only responsible of me to include what should be relative to the question 🙂

The plugin uses a singleton instantiation pattern:

class WC_Freshdesk {
    protected static $instance = null;

    public static function get_instance() {
        // If the single instance hasn't been set, set it now.
        if ( null == self::$instance ) {
            self::$instance = new self;
        }
        return self::$instance;
    }
}

In the __construct() method the parent class includes several files. One of these is a child class.

class WC_Freshdesk_Integration extends WC_Integration {
    public function __construct() {
        add_action( 'woocommerce_view_order', array( $this, 'view_order_create_ticket' ), 40 );
    }
}

I am trying to access that add_action to remove it from a theme’s template.

I’ve tried the following:

$freshdesk = WC_Freshdesk::get_instance();

remove_action( 'woocommerce_view_order', array( $freshdesk, 'view_order_create_ticket', 40 ) );

No dice there.

Then when trying:

$freshdesk = new WC_Freshdesk_Integration();

remove_action( 'woocommerce_view_order', array( $freshdesk, 'view_order_create_ticket', 40 ) );

Evidently I end up re declaring the WC_Freshdesk_integration() class because the add_action hook runs twice.

What is the correct way of access this child class to remove that action?

0
, W00tW00t111 7 years 2016-03-01T11:03:40-05:00 0 Answers 85 views 0

Leave an answer

Browse
Browse