PluginProbe
Extendify / 0.11.1
Extendify v0.11.1
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.11.1, at app/Onboarding/Admin.php

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