| 1 |
<?php |
| 2 |
/** |
| 3 |
* Simple router to handle admin page loading |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace Extendify; |
| 7 |
|
| 8 |
use Extendify\Config; |
| 9 |
use Extendify\Library\AdminPage as LibraryAdminPage; |
| 10 |
use Extendify\Assist\AdminPage as AssistAdminPage; |
| 11 |
|
| 12 |
/** |
| 13 |
* This class handles routing when the main admin button is pressed. |
| 14 |
*/ |
| 15 |
class AdminPageRouter |
| 16 |
{ |
| 17 |
|
| 18 |
/** |
| 19 |
* Adds various actions to set up the page |
| 20 |
* |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public function __construct() |
| 24 |
{ |
| 25 |
// Don't show the admin page if not using the standalone version (e.g. Redux). |
| 26 |
if (!Config::$standalone) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
// When Launch is finished, fire this to set the correct permalinks. |
| 31 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 32 |
if (isset($_GET['extendify-launch-success'])) { |
| 33 |
\add_action( 'admin_init', function () { |
| 34 |
\flush_rewrite_rules(); |
| 35 |
}); |
| 36 |
} |
| 37 |
|
| 38 |
// Add top admin page to handle redirects. Everything |
| 39 |
// else will be a subpage of this. |
| 40 |
\add_action('admin_menu', [ $this, 'addAdminMenu' ]); |
| 41 |
|
| 42 |
\add_action('admin_menu', function () { |
| 43 |
// Load the Assist page when Launch is finished. |
| 44 |
if (Config::$showAssist && Config::$launchCompleted) { |
| 45 |
$assist = new AssistAdminPage(); |
| 46 |
$cb = [$assist, 'pageContent']; |
| 47 |
$this->addSubMenu('Assist', $assist->slug, $cb); |
| 48 |
} |
| 49 |
|
| 50 |
// Always load the Library page. |
| 51 |
$library = new LibraryAdminPage(); |
| 52 |
$cb = [$library, 'pageContent']; |
| 53 |
$this->addSubMenu('Library', $library->slug, $cb); |
| 54 |
|
| 55 |
// Show the Launch menu for dev users. |
| 56 |
if (Config::$environment === 'DEVELOPMENT') { |
| 57 |
$this->addSubMenu('Launch', 'post-new.php?extendify=onboarding'); |
| 58 |
} |
| 59 |
}); |
| 60 |
|
| 61 |
// Hide the menu items unless in dev mode. |
| 62 |
if (Config::$environment === 'PRODUCTION') { |
| 63 |
add_action('admin_head', function () { |
| 64 |
echo '<style> |
| 65 |
#toplevel_page_extendify-admin-page .wp-submenu { |
| 66 |
display:none!important; |
| 67 |
} |
| 68 |
#toplevel_page_extendify-admin-page::after { |
| 69 |
content:none!important; |
| 70 |
} |
| 71 |
</style>'; |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
// If the user is redirected to this while visitng our url, intercept it. |
| 76 |
\add_filter('wp_redirect', function ($url) { |
| 77 |
// Check for extendify-launch-success as other plugins will not override |
| 78 |
// this as they intercept the request. |
| 79 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 80 |
if (isset($_GET['extendify-launch-success'])) { |
| 81 |
return \admin_url() . $this->getRoute(); |
| 82 |
} |
| 83 |
|
| 84 |
return $url; |
| 85 |
}, 9999); |
| 86 |
|
| 87 |
// Intercept requests and redirect as needed. |
| 88 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 89 |
if (isset($_GET['page']) && $_GET['page'] === 'extendify-admin-page') { |
| 90 |
header('Location: ' . \admin_url() . $this->getRoute(), true, 302); |
| 91 |
exit; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* A helper for handling sub menus |
| 97 |
* |
| 98 |
* @param string $name - The menu name. |
| 99 |
* @param string $slug - The menu slug. |
| 100 |
* @param callable $callback - The callback to render the page. |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
public function addSubMenu($name, $slug, $callback = '') |
| 105 |
{ |
| 106 |
\add_submenu_page( |
| 107 |
'extendify-admin-page', |
| 108 |
$name, |
| 109 |
$name, |
| 110 |
Config::$requiredCapability, |
| 111 |
$slug, |
| 112 |
$callback |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Adds Extendify top menu |
| 118 |
* |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
public function addAdminMenu() |
| 122 |
{ |
| 123 |
\add_menu_page( |
| 124 |
'Extendify', |
| 125 |
'Extendify', |
| 126 |
Config::$requiredCapability, |
| 127 |
'extendify-admin-page', |
| 128 |
'__return_null', |
| 129 |
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 130 |
'data:image/svg+xml;base64,' . base64_encode('<svg width="20" height="20" viewBox="0 0 60 62" fill="black" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M36.0201 0H49.2377C52.9815 0 54.3365 0.391104 55.7061 1.12116C57.0756 1.85412 58.1469 2.92893 58.8795 4.29635C59.612 5.66666 60 7.02248 60 10.7684V23.9935C60 27.7394 59.6091 29.0952 58.8795 30.4655C58.1469 31.8358 57.0727 32.9078 55.7061 33.6407C55.0938 33.9684 54.4831 34.2381 53.661 34.4312V44.9564C53.661 50.7417 53.0573 52.8356 51.9305 54.952C50.7991 57.0683 49.1401 58.7238 47.0294 59.8558C44.9143 60.9878 42.8215 61.5873 37.0395 61.5873H16.626C10.844 61.5873 8.75122 60.9833 6.63608 59.8558C4.52094 58.7238 2.86639 57.0638 1.73504 54.952C0.603687 52.8401 0 50.7417 0 44.9564V24.5358C0 18.7506 0.603687 16.6566 1.73057 14.5403C2.86192 12.424 4.52094 10.764 6.63608 9.63201C8.74675 8.5045 10.844 7.90047 16.626 7.90047H25.3664C25.5303 6.18172 25.8724 5.24393 26.3754 4.29924C27.1079 2.92893 28.1821 1.85412 29.5517 1.12116C30.9183 0.391104 32.2763 0 36.0201 0ZM29.2266 8.41812C29.2266 5.96352 31.2155 3.97368 33.6689 3.97368H51.5859C54.0393 3.97368 56.0282 5.96352 56.0282 8.41812V26.3438C56.0282 28.7984 54.0393 30.7882 51.5859 30.7882H33.6689C31.2155 30.7882 29.2266 28.7984 29.2266 26.3438V8.41812Z" fill="black"/></svg>') |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Routes pages accordingly |
| 136 |
* |
| 137 |
* @return string |
| 138 |
*/ |
| 139 |
public function getRoute() |
| 140 |
{ |
| 141 |
// If dev, redirect to assist always. |
| 142 |
if (Config::$environment === 'DEVELOPMENT') { |
| 143 |
return 'admin.php?page=extendify-assist'; |
| 144 |
} |
| 145 |
|
| 146 |
// If Launch/Assist isn't enabled, show the Library page. |
| 147 |
if (!Config::$showOnboarding) { |
| 148 |
return 'admin.php?page=extendify-welcome'; |
| 149 |
} |
| 150 |
|
| 151 |
// If they've yet to complete launch, send them back to Launch. |
| 152 |
if (!Config::$launchCompleted) { |
| 153 |
return 'post-new.php?extendify=onboarding'; |
| 154 |
} |
| 155 |
|
| 156 |
// If they made it this far, they can go to Assist. |
| 157 |
return 'admin.php?page=extendify-assist'; |
| 158 |
} |
| 159 |
} |
| 160 |
|