| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Builds absolute URLs to this plugin's own admin screens. |
| 9 |
* |
| 10 |
* The plugin registers its menu in one of two places depending on the |
| 11 |
* `menuLocation` option (see WordPress_Connector::addMainSettingsPageLink), |
| 12 |
* so the admin file that owns the page is either `admin.php` (top-level menu) |
| 13 |
* or `options-general.php` (Settings submenu). A link that hard-codes the |
| 14 |
* wrong one still resolves, because the options page redirects to the correct |
| 15 |
* URL, but the redirect drops anything the link was carrying that only the |
| 16 |
* browser understands -- a `#fragment` in particular. Any link that has to |
| 17 |
* land on a specific field therefore has to be built against the right admin |
| 18 |
* file in the first place, which is what this class is for. |
| 19 |
* |
| 20 |
* // allow-no-test-found: exercised by SuggestionsPageAdminNoteLinkTargetsTest |
| 21 |
*/ |
| 22 |
class ABJ_404_Solution_AdminPageUrlBuilder { |
| 23 |
|
| 24 |
const FILE_TOP_LEVEL = 'admin.php'; |
| 25 |
const FILE_UNDER_SETTINGS = 'options-general.php'; |
| 26 |
|
| 27 |
/** |
| 28 |
* The wp-admin file that owns this plugin's pages for the current |
| 29 |
* `menuLocation` setting. |
| 30 |
* |
| 31 |
* @param array<string, mixed> $options The plugin options. |
| 32 |
* @return string 'admin.php' or 'options-general.php'. |
| 33 |
*/ |
| 34 |
public static function pageFile(array $options): string { |
| 35 |
$menuLocation = isset($options['menuLocation']) && is_string($options['menuLocation']) |
| 36 |
? $options['menuLocation'] : ''; |
| 37 |
return $menuLocation === 'settingsLevel' ? self::FILE_TOP_LEVEL : self::FILE_UNDER_SETTINGS; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* An absolute, unescaped URL to one of the plugin's subpages. |
| 42 |
* |
| 43 |
* Returned raw (not run through esc_url) so callers can append a |
| 44 |
* `#fragment` and escape once, at the point the URL is written into |
| 45 |
* markup. |
| 46 |
* |
| 47 |
* @param string $subpage The subpage slug, e.g. 'abj404_options'. |
| 48 |
* @param array<string, string> $queryArgs Extra query args, added in the |
| 49 |
* order given. Keys and values are both url-encoded. |
| 50 |
* @param array<string, mixed> $options The plugin options. |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
public static function subpageUrl(string $subpage, array $queryArgs, array $options): string { |
| 54 |
$path = self::pageFile($options) . '?page=' . rawurlencode(ABJ404_PP) . |
| 55 |
'&subpage=' . rawurlencode($subpage); |
| 56 |
foreach ($queryArgs as $key => $value) { |
| 57 |
$path .= '&' . rawurlencode((string)$key) . '=' . rawurlencode((string)$value); |
| 58 |
} |
| 59 |
return admin_url($path); |
| 60 |
} |
| 61 |
} |
| 62 |
|