I'm learning to code Wordpress Multisite the OOP way, and since I'm pretty new to OOP now I am in a situation that I can't seem to solve on my own.
Specifically, I'm creating some classes to create admin pages (both at the network and subsite level) with an OOP approach. Here's my simplified code:
class AdminPage {
public function __construct( $args ) {
add_action( 'admin_menu', array( $this, 'add_admin_page' ) );
}
public function add_admin_page() {
add_menu_page( // arguments );
}
}
class AdminNetworkPage extends AdminPage {
public function __construct( $args ) {
add_action( 'network_admin_menu', array( $this, 'add_admin_page' ) );
}
}
The code works, but as you can see I have to extend the AdminPage class with the sole purpose of changing the hook in the constructor ( I need admin_network_menu for admin pages at network level instead of admin_menu).
Is there a better way to do this? A way to have one class, put both hooks in the constructor of that class and then selectively call either one or the other?
Unfortunately, creating a new instance of the class ($page = new AdminPage) and then calling a method on it (e.g. $page->add_admin_page() ) won't work in this case, because then I get an error saying add_menu_page is undefined... It all has to happen in the constructor.