I am trying to build some nice metabox functionality with some oop.
I have my main DazSEO class, which inside contains a Metabox Class.
When I set WordPress save data hooks up inside the metabox class, they won't fire, but when I set them up from the root class, they will.
Is using hooks inside embedded classes incompatible with the WordPress hook system?
Any help would be appreciated here as I have spent a lot of time trying to figure out what is the issue here, I guess it is some object scope issue for the wp engine, but I am not convinced. class DazSEO { public static $plugins = array();
public $metaBox;
public function __construct() {
add_action('admin_init', array($this, 'adminInit') );
add_action('init', array($this, 'init'));
}
public function adminInit() {
$this->setupMetaBox();
}
public function setupMetaBox() {
if( $pagenow === 'term.php' ) { // returns true
if( isset($_GET['taxonomy']) ) {
$term = get_term( $_GET['tag_ID'], $_GET['taxonomy'], OBJECT );
require_once('admin\metabox\TermMetaBox.php');
$this->metaBox = new TermMetabox( $_GET['taxonomy'] ); // confirmed this does fire
DazSEO::$mode = 'term';
DazSEO::$wpID = $_GET['tag_ID'];
DazSEO::$typeName = $_GET['taxonomy'];
}
}
}
}
class TermMetabox extends MetaBoxBase {
private $_taxonomy;
public function __construct(string $taxonomy) {
if( strlen($taxonomy) === 0 ) { echo 'no taxonomy passed'; exit(); }
$this->_taxonomy = $taxonomy;
add_action( $this->_taxonomy . '_edit_form', array($this, 'buildMetaBox') );
add_action('edit_terms', array($this, 'onTermSave')); // SETUP TERM SAVE
}
public function loadHooks() {
}
// NOT BEING CALLED
public function onTermSave(int $term_id) {
echo '<pre>' . print_r($_REQUEST, 1) . '</pre>'; exit();
if( $this->onSaveChecks($term_id) ) {
return;
}
foreach(DazSEO::$plugins as $plugin) {
$plugin->onTermSave($term_id, $_REQUEST);
}
}
}
The TermMetabox:: onTermSave never gets called
If this is called from the root class (DazSEO), and I move the save method in there too it works...
add_action('edit_terms', array($this, 'onTermSave'));
But it's messy design like that.
This plugin is initialized like this...
$dazSEO = new DazSEO();
$wpCleanHeader = new RemoveWPHeaderJunk();
$dazSEO->loadPlugin($wpCleanHeader);