PluginProbe
404 Solution / trunk
404 Solution vtrunk
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / admin / AdminPageUrlBuilder.php

AdminPageUrlBuilder.php in 404 Solution trunk, at includes/admin/AdminPageUrlBuilder.php

62 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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