the call at the anonymous function is done after all the executions of add_new_page
. then you need to store all elements to create menu items.
try to modify the class Helper like that
public function __construct() // method to modify
{
add_action("admin_menu", [$this, "add_admin_menu_items"]);
}
public function add_new_page( $opt ) // method to modify
{
$element = array_merge( $this->default_options, $opt );
$this->options[] = $element;
}
public function add_admin_menu_items() // method to create
{
$helper = $this;
foreach ($this->options as $element) {
add_menu_page(
$element['page_title'],
$element['title'],
$element['capability'],
$element['slug'],
function () use ($helper, $element) {
$helper->display_page_template($element);
},
$element['icon'],
$element['position']
);
}
}
public function display_page_template($itemDatas)
{
?>
<pre><?php
echo htmlspecialchars(var_export($itemDatas, TRUE));
?></pre>
<?php
}
}