| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin. |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace Extendify\Onboarding; |
| 7 |
|
| 8 |
use Extendify\Config; |
| 9 |
|
| 10 |
/** |
| 11 |
* This class handles any file loading for the admin area. |
| 12 |
*/ |
| 13 |
class Admin |
| 14 |
{ |
| 15 |
|
| 16 |
/** |
| 17 |
* The instance |
| 18 |
* |
| 19 |
* @var $instance |
| 20 |
*/ |
| 21 |
public static $instance = null; |
| 22 |
|
| 23 |
/** |
| 24 |
* Adds various actions to set up the page |
| 25 |
* |
| 26 |
* @return self|void |
| 27 |
*/ |
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
// Whether to load Extendify Launch or not. |
| 31 |
if (!Config::$showOnboarding) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
if (self::$instance) { |
| 36 |
return self::$instance; |
| 37 |
} |
| 38 |
|
| 39 |
self::$instance = $this; |
| 40 |
$this->loadScripts(); |
| 41 |
$this->addAdminMenu(); |
| 42 |
$this->redirectOnce(); |
| 43 |
$this->addMetaField(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Adds a meta field so we can indicate a page was made with launch |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function addMetaField() |
| 52 |
{ |
| 53 |
\add_action( |
| 54 |
'init', |
| 55 |
function () { |
| 56 |
register_post_meta( |
| 57 |
'page', |
| 58 |
'made_with_extendify_launch', |
| 59 |
[ |
| 60 |
'single' => true, |
| 61 |
'type' => 'boolean', |
| 62 |
'show_in_rest' => true, |
| 63 |
] |
| 64 |
); |
| 65 |
} |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Adds scripts to the admin |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function loadScripts() |
| 75 |
{ |
| 76 |
\add_action( |
| 77 |
'admin_enqueue_scripts', |
| 78 |
function ($hook) { |
| 79 |
if (!current_user_can(Config::$requiredCapability)) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
if (!$this->checkItsGutenbergPost($hook)) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$this->addScopedScriptsAndStyles(); |
| 88 |
} |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Adds settings menu |
| 94 |
* |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function addAdminMenu() |
| 98 |
{ |
| 99 |
\add_action('admin_menu', function () { |
| 100 |
if (Config::$environment === 'DEVELOPMENT' || Config::$showOnboarding) { |
| 101 |
if (Config::$showAssist) { |
| 102 |
\add_submenu_page('extendify-assist', 'Assist', 'Assist', Config::$requiredCapability, 'extendify-assist', '', 300); |
| 103 |
\add_submenu_page('extendify-assist', 'Launch', 'Launch', Config::$requiredCapability, 'post-new.php?extendify=onboarding', '', 500); |
| 104 |
} else { |
| 105 |
\add_submenu_page('extendify-welcome', \__('Welcome', 'extendify'), \__('Welcome', 'extendify'), Config::$requiredCapability, 'extendify-welcome', '', 400); |
| 106 |
\add_submenu_page('extendify-welcome', 'Launch', 'Launch', Config::$requiredCapability, 'post-new.php?extendify=onboarding', '', 500); |
| 107 |
} |
| 108 |
} |
| 109 |
}); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Redirect once to Launch, only once (at least once) when |
| 114 |
* the email matches the entry in WP Admin > Settings > General. |
| 115 |
* |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function redirectOnce() |
| 119 |
{ |
| 120 |
\add_action('admin_init', function () { |
| 121 |
if (\get_option('extendify_launch_loaded', 0) |
| 122 |
// These are here for legacy reasons. |
| 123 |
|| \get_option('extendify_onboarding_skipped', 0) |
| 124 |
|| Config::$launchCompleted |
| 125 |
) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
// Only redirect if we aren't already on the page. |
| 130 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 131 |
if (isset($_GET['extendify'])) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$user = \wp_get_current_user(); |
| 136 |
if ($user |
| 137 |
// Check the main admin email, and they have an admin role. |
| 138 |
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 139 |
&& \get_option('admin_email') === $user->user_email |
| 140 |
&& in_array('administrator', $user->roles, true) |
| 141 |
) { |
| 142 |
\wp_safe_redirect(\admin_url() . 'post-new.php?extendify=onboarding'); |
| 143 |
} |
| 144 |
}); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Makes sure we are on the correct page |
| 149 |
* |
| 150 |
* @param string $hook - An optional hook provided by WP to identify the page. |
| 151 |
* @return boolean |
| 152 |
*/ |
| 153 |
public function checkItsGutenbergPost($hook = '') |
| 154 |
{ |
| 155 |
if (isset($GLOBALS['typenow']) && \use_block_editor_for_post_type($GLOBALS['typenow'])) { |
| 156 |
return $hook && in_array($hook, ['post.php', 'post-new.php'], true); |
| 157 |
} |
| 158 |
|
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Check if partner data is available. |
| 164 |
* |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
public function checkPartnerDataSources() |
| 168 |
{ |
| 169 |
$return = []; |
| 170 |
|
| 171 |
try { |
| 172 |
if (defined('EXTENDIFY_ONBOARDING_BG')) { |
| 173 |
$return['bgColor'] = constant('EXTENDIFY_ONBOARDING_BG'); |
| 174 |
$return['fgColor'] = constant('EXTENDIFY_ONBOARDING_TXT'); |
| 175 |
$return['logo'] = constant('EXTENDIFY_PARTNER_LOGO'); |
| 176 |
} |
| 177 |
|
| 178 |
$data = get_option('extendify_partner_data'); |
| 179 |
if ($data) { |
| 180 |
$return['bgColor'] = $data['backgroundColor']; |
| 181 |
$return['fgColor'] = $data['foregroundColor']; |
| 182 |
// Need this check to avoid errors if no partner logo is set in Airtable. |
| 183 |
$return['logo'] = $data['logo'] ? $data['logo'][0]['thumbnails']['small']['url'] : null; |
| 184 |
} |
| 185 |
} catch (\Exception $e) { |
| 186 |
// Do nothing here, set variables below. Coding Standards require something to be in the catch. |
| 187 |
$e; |
| 188 |
}//end try |
| 189 |
|
| 190 |
return $return; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Adds various JS scripts |
| 195 |
* |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public function addScopedScriptsAndStyles() |
| 199 |
{ |
| 200 |
$version = Config::$environment === 'PRODUCTION' ? Config::$version : uniqid(); |
| 201 |
|
| 202 |
$partnerData = $this->checkPartnerDataSources(); |
| 203 |
|
| 204 |
$bgColor = isset($partnerData['bgColor']) ? $partnerData['bgColor'] : '#2c39bd'; |
| 205 |
$fgColor = isset($partnerData['fgColor']) ? $partnerData['fgColor'] : '#ffffff'; |
| 206 |
$logo = isset($partnerData['logo']) ? $partnerData['logo'] : null; |
| 207 |
|
| 208 |
\wp_enqueue_script( |
| 209 |
Config::$slug . '-onboarding-scripts', |
| 210 |
EXTENDIFY_BASE_URL . 'public/build/extendify-onboarding.js', |
| 211 |
[ |
| 212 |
'wp-i18n', |
| 213 |
'wp-components', |
| 214 |
'wp-element', |
| 215 |
'wp-editor', |
| 216 |
], |
| 217 |
$version, |
| 218 |
true |
| 219 |
); |
| 220 |
\wp_add_inline_script( |
| 221 |
Config::$slug . '-onboarding-scripts', |
| 222 |
'window.extOnbData = ' . wp_json_encode([ |
| 223 |
'globalStylesPostID' => \WP_Theme_JSON_Resolver::get_user_global_styles_post_id(), |
| 224 |
'site' => \esc_url_raw(\get_site_url()), |
| 225 |
'adminUrl' => \esc_url_raw(\admin_url()), |
| 226 |
'pluginUrl' => \esc_url_raw(EXTENDIFY_BASE_URL), |
| 227 |
'home' => \esc_url_raw(\get_home_url()), |
| 228 |
'root' => \esc_url_raw(\rest_url(Config::$slug . '/' . Config::$apiVersion)), |
| 229 |
'config' => Config::$config, |
| 230 |
'wpRoot' => \esc_url_raw(\rest_url()), |
| 231 |
'nonce' => \wp_create_nonce('wp_rest'), |
| 232 |
'partnerLogo' => $logo, |
| 233 |
'partnerName' => \esc_attr(Config::$sdkPartner), |
| 234 |
'partnerSkipSteps' => defined('EXTENDIFY_SKIP_STEPS') ? constant('EXTENDIFY_SKIP_STEPS') : [], |
| 235 |
'devbuild' => \esc_attr(Config::$environment === 'DEVELOPMENT'), |
| 236 |
'version' => Config::$version, |
| 237 |
'insightsId' => \get_option('extendify_site_id', ''), |
| 238 |
// Only send insights if they have opted in explicitly. |
| 239 |
'insightsEnabled' => defined('EXTENDIFY_INSIGHTS_URL'), |
| 240 |
'activeTests' => \get_option('extendify_active_tests', []), |
| 241 |
]), |
| 242 |
'before' |
| 243 |
); |
| 244 |
|
| 245 |
\wp_set_script_translations(Config::$slug . '-onboarding-scripts', 'extendify'); |
| 246 |
|
| 247 |
\wp_enqueue_style( |
| 248 |
Config::$slug . '-onboarding-styles', |
| 249 |
EXTENDIFY_BASE_URL . 'public/build/extendify-onboarding.css', |
| 250 |
[], |
| 251 |
$version, |
| 252 |
'all' |
| 253 |
); |
| 254 |
|
| 255 |
\wp_add_inline_style(Config::$slug . '-onboarding-styles', "body { |
| 256 |
--ext-partner-theme-primary-bg: {$bgColor}; |
| 257 |
--ext-partner-theme-primary-text: {$fgColor}; |
| 258 |
}"); |
| 259 |
} |
| 260 |
} |
| 261 |
|