PluginProbe
Extendify / 0.10.2
Extendify v0.10.2
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / app / Onboarding / Admin.php

Admin.php in Extendify 0.10.2, at app/Onboarding/Admin.php

264 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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::$showOnboarding) {
101 return;
102 }
103
104 if (Config::$showAssist) {
105 \add_submenu_page('extendify-assist', 'Assist', 'Assist', Config::$requiredCapability, 'extendify-assist', '', 300);
106 \add_submenu_page('extendify-assist', 'Launch', 'Launch', Config::$requiredCapability, 'post-new.php?extendify=onboarding', '', 500);
107 return;
108 }
109
110 \add_submenu_page('extendify-welcome', \__('Welcome', 'extendify'), \__('Welcome', 'extendify'), Config::$requiredCapability, 'extendify-welcome', '', 400);
111 \add_submenu_page('extendify-welcome', 'Launch', 'Launch', Config::$requiredCapability, 'post-new.php?extendify=onboarding', '', 500);
112 });
113 }
114
115 /**
116 * Redirect once to Launch, only once (at least once) when
117 * the email matches the entry in WP Admin > Settings > General.
118 *
119 * @return void
120 */
121 public function redirectOnce()
122 {
123 \add_action('admin_init', function () {
124 if (\get_option('extendify_launch_loaded', 0)
125 // These are here for legacy reasons.
126 || \get_option('extendify_onboarding_skipped', 0)
127 || Config::$launchCompleted
128 ) {
129 return;
130 }
131
132 // Only redirect if we aren't already on the page.
133 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
134 if (isset($_GET['extendify'])) {
135 return;
136 }
137
138 $user = \wp_get_current_user();
139 if ($user
140 // Check the main admin email, and they have an admin role.
141 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
142 && \get_option('admin_email') === $user->user_email
143 && in_array('administrator', $user->roles, true)
144 ) {
145 \wp_safe_redirect(\admin_url() . 'post-new.php?extendify=onboarding');
146 }
147 });
148 }
149
150 /**
151 * Makes sure we are on the correct page
152 *
153 * @param string $hook - An optional hook provided by WP to identify the page.
154 * @return boolean
155 */
156 public function checkItsGutenbergPost($hook = '')
157 {
158 if (isset($GLOBALS['typenow']) && \use_block_editor_for_post_type($GLOBALS['typenow'])) {
159 return $hook && in_array($hook, ['post.php', 'post-new.php'], true);
160 }
161
162 return false;
163 }
164
165 /**
166 * Check if partner data is available.
167 *
168 * @return array
169 */
170 public function checkPartnerDataSources()
171 {
172 $return = [];
173
174 try {
175 if (defined('EXTENDIFY_ONBOARDING_BG')) {
176 $return['bgColor'] = constant('EXTENDIFY_ONBOARDING_BG');
177 $return['fgColor'] = constant('EXTENDIFY_ONBOARDING_TXT');
178 $return['logo'] = constant('EXTENDIFY_PARTNER_LOGO');
179 }
180
181 $data = get_option('extendify_partner_data');
182 if ($data) {
183 $return['bgColor'] = $data['backgroundColor'];
184 $return['fgColor'] = $data['foregroundColor'];
185 // Need this check to avoid errors if no partner logo is set in Airtable.
186 $return['logo'] = $data['logo'] ? $data['logo'][0]['thumbnails']['small']['url'] : null;
187 }
188 } catch (\Exception $e) {
189 // Do nothing here, set variables below. Coding Standards require something to be in the catch.
190 $e;
191 }//end try
192
193 return $return;
194 }
195
196 /**
197 * Adds various JS scripts
198 *
199 * @return void
200 */
201 public function addScopedScriptsAndStyles()
202 {
203 $version = Config::$environment === 'PRODUCTION' ? Config::$version : uniqid();
204
205 $partnerData = $this->checkPartnerDataSources();
206
207 $bgColor = isset($partnerData['bgColor']) ? $partnerData['bgColor'] : '#2c39bd';
208 $fgColor = isset($partnerData['fgColor']) ? $partnerData['fgColor'] : '#ffffff';
209 $logo = isset($partnerData['logo']) ? $partnerData['logo'] : null;
210
211 \wp_enqueue_script(
212 Config::$slug . '-onboarding-scripts',
213 EXTENDIFY_BASE_URL . 'public/build/extendify-onboarding.js',
214 [
215 'wp-i18n',
216 'wp-components',
217 'wp-element',
218 'wp-editor',
219 ],
220 $version,
221 true
222 );
223 \wp_add_inline_script(
224 Config::$slug . '-onboarding-scripts',
225 'window.extOnbData = ' . wp_json_encode([
226 'globalStylesPostID' => \WP_Theme_JSON_Resolver::get_user_global_styles_post_id(),
227 'site' => \esc_url_raw(\get_site_url()),
228 'adminUrl' => \esc_url_raw(\admin_url()),
229 'pluginUrl' => \esc_url_raw(EXTENDIFY_BASE_URL),
230 'home' => \esc_url_raw(\get_home_url()),
231 'root' => \esc_url_raw(\rest_url(Config::$slug . '/' . Config::$apiVersion)),
232 'config' => Config::$config,
233 'wpRoot' => \esc_url_raw(\rest_url()),
234 'nonce' => \wp_create_nonce('wp_rest'),
235 'partnerLogo' => $logo,
236 'partnerName' => \esc_attr(Config::$sdkPartner),
237 'partnerSkipSteps' => defined('EXTENDIFY_SKIP_STEPS') ? constant('EXTENDIFY_SKIP_STEPS') : [],
238 'devbuild' => \esc_attr(Config::$environment === 'DEVELOPMENT'),
239 'version' => Config::$version,
240 'insightsId' => \get_option('extendify_site_id', ''),
241 // Only send insights if they have opted in explicitly.
242 'insightsEnabled' => defined('EXTENDIFY_INSIGHTS_URL'),
243 'activeTests' => \get_option('extendify_active_tests', []),
244 ]),
245 'before'
246 );
247
248 \wp_set_script_translations(Config::$slug . '-onboarding-scripts', 'extendify');
249
250 \wp_enqueue_style(
251 Config::$slug . '-onboarding-styles',
252 EXTENDIFY_BASE_URL . 'public/build/extendify-onboarding.css',
253 [],
254 $version,
255 'all'
256 );
257
258 \wp_add_inline_style(Config::$slug . '-onboarding-styles', "body {
259 --ext-partner-theme-primary-bg: {$bgColor};
260 --ext-partner-theme-primary-text: {$fgColor};
261 }");
262 }
263 }
264