[wp-trac] [WordPress Trac] #40649: parent_file filter seems to be overwritten by get_admin_page_parent call
WordPress Trac
noreply at wordpress.org
Mon Jun 10 22:21:53 UTC 2019
#40649: parent_file filter seems to be overwritten by get_admin_page_parent call
--------------------------+------------------------------
Reporter: jontis | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Menus | Version: 4.7.4
Severity: normal | Resolution:
Keywords: | Focuses: administration
--------------------------+------------------------------
Comment (by nohns):
So this is still an issue. I made really ugly hack to fix this, but it
works ¯\_(ツ)_/¯
What it does:
- It changes globals temporarily to fit the needs of the
{{{get_admin_page_parent()}}} function
- Then changes the globals back to their previous state after side menu
has rendered
Basically I think the the only function using these globals while they are
modified is {{{get_admin_page_parent()}}}, which only sets the
{{{$parent_file}}} variable, so it should be safe however pretty ugly and
dirty.
I made a little class to house this hack:
{{{#!php
<?php
class Parent_File_Hack
{
public $old_nopriv = null;
public $old_real_parent = null;
public $old_plugin_page = null;
public $old_pagenow = null;
/**
* Setup action and filters to complete the hack
*
* @return void
*/
public function listen()
{
// Fire the parent_file filter as late as possible to start the
hack
add_filter('parent_file', array($this, 'start_parent_file_hack'),
1000001);
// End the hack right after the menu is rendered
add_action('adminmenu', array($this, 'end_parent_file_hack'));
}
/**
* The function to call when we want to start preparing and setting
the new parent_file value
*/
public function start_parent_file_hack($parent_file)
{
global $_wp_menu_nopriv, $_wp_real_parent_file, $plugin_page,
$pagenow;
// Save the old variables
$this->old_nopriv = $_wp_menu_nopriv;
$this->old_real_parent = $_wp_real_parent_file;
$this->old_plugin_page = $plugin_page;
$this->old_pagenow = $pagenow;
// Set globals to artificial names to force the parent_file
variable to be assigned to what we want
$pagenow = 'admin.php';
$plugin_page = 'wgy_ambassadors';
$_wp_menu_nopriv[$plugin_page] = $plugin_page;
$_wp_real_parent_file[$plugin_page] = $parent_file;
return $parent_file;
}
/**
* The function to call when we want to revert the changes that were
made by the hack
*
* @return void
*/
public function end_parent_file_hack()
{
global $_wp_menu_nopriv, $_wp_real_parent_file, $plugin_page,
$pagenow;
// Change back the globals to their previous states
$_wp_menu_nopriv = $this->old_nopriv;
$_wp_real_parent_file = $this->old_real_parent;
$plugin_page = $this->old_plugin_page;
$pagenow = $this->old_pagenow;
}
}
}}}
To use this hack, simply initialize a {{{Parent_File_Hack}}} object, and
run the {{{listen()}}} method on it, somewhere in your code you see fit.
Just "set it and forget it", and then just use your filters as you
normally would (Make sure you don't pick a priority over 1000001):
{{{#!php
<?php
// Initialize the object and listen for the 'parent_file' and 'adminmenu'
hooks
$hack = new Parent_File_Hack();
$hack->listen();
// ...
add_filter('parent_file', function($parent_file) {
// ...
return 'your_parent_file';
});
add_filter('submenu_file', function($submenu_file) {
// ...
return 'your_submenu_file';
});
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/40649#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list