| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package WPEmerge |
| 4 |
* @author Atanas Angelov <hi@atanas.dev> |
| 5 |
* @copyright 2017-2019 Atanas Angelov |
| 6 |
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
| 7 |
* @link https://wpemerge.com/ |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace WPEmerge\Routing\Conditions; |
| 11 |
|
| 12 |
use WPEmerge\Requests\RequestInterface; |
| 13 |
|
| 14 |
/** |
| 15 |
* Check against the current ajax action. |
| 16 |
* |
| 17 |
* @codeCoverageIgnore |
| 18 |
*/ |
| 19 |
class AdminCondition implements ConditionInterface, UrlableInterface { |
| 20 |
/** |
| 21 |
* Menu slug. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $menu = ''; |
| 26 |
|
| 27 |
/** |
| 28 |
* Parent menu slug. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $parent_menu = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor |
| 36 |
* |
| 37 |
* @codeCoverageIgnore |
| 38 |
* @param string $menu |
| 39 |
* @param string $parent_menu |
| 40 |
*/ |
| 41 |
public function __construct( $menu, $parent_menu = '' ) { |
| 42 |
$this->menu = $menu; |
| 43 |
$this->parent_menu = $parent_menu; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Check if the admin page requirement matches. |
| 48 |
* |
| 49 |
* @return boolean |
| 50 |
*/ |
| 51 |
protected function isAdminPage() { |
| 52 |
return is_admin() && ! wp_doing_ajax(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* {@inheritDoc} |
| 57 |
*/ |
| 58 |
public function isSatisfied( RequestInterface $request ) { |
| 59 |
if ( ! $this->isAdminPage() ) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
$screen = get_current_screen(); |
| 64 |
|
| 65 |
if ( ! $screen ) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
return $screen->id === get_plugin_page_hookname( $this->menu, $this->parent_menu ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* {@inheritDoc} |
| 74 |
*/ |
| 75 |
public function getArguments( RequestInterface $request ) { |
| 76 |
return [ |
| 77 |
'menu' => $this->menu, |
| 78 |
'parent_menu' => $this->parent_menu, |
| 79 |
'hook' => get_plugin_page_hookname( $this->menu, $this->parent_menu ) |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* {@inheritDoc} |
| 85 |
*/ |
| 86 |
public function toUrl( $arguments = [] ) { |
| 87 |
if ( ! function_exists( 'menu_page_url' ) ) { |
| 88 |
// Attempted to resolve an admin url while not in the admin which can only happen |
| 89 |
// by mistake as admin routes are defined in the admin context only. |
| 90 |
return home_url( '/' ); |
| 91 |
} |
| 92 |
|
| 93 |
return menu_page_url( $this->menu, false ); |
| 94 |
} |
| 95 |
} |
| 96 |
|