PluginProbe
Extendify / 1.12.2
Extendify v1.12.2
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 0.7.0 All 126 releases
extendify / app / Library / Admin.php

Admin.php in Extendify 1.12.2, at app/Library/Admin.php

249 lines 8.5 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\Library;
7
8 use Extendify\PartnerData;
9 use Extendify\Config;
10
11 /**
12 * This class handles any file loading for the admin area.
13 */
14 class Admin
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 if (self::$instance) {
31 return self::$instance;
32 }
33
34 self::$instance = $this;
35 $this->loadScripts();
36
37 $this->registerUserMeta();
38 }
39
40 /**
41 * Adds scripts to the admin
42 *
43 * @return void
44 */
45 public function loadScripts()
46 {
47 \add_action(
48 'admin_enqueue_scripts',
49 function ($hook) {
50 if (!current_user_can(Config::$requiredCapability)) {
51 return;
52 }
53
54 $this->maybeAddDeactivationScript();
55
56 if (!$this->isGutenbergEditor($hook)) {
57 return;
58 }
59
60 $this->addScopedScriptsAndStyles();
61 }
62 );
63 }
64
65 /**
66 * Makes sure we are on the correct page
67 *
68 * @param string $hook - An optional hook provided by WP to identify the page.
69 * @return boolean
70 */
71 public function isGutenbergEditor($hook = '')
72 {
73 // Check for the post type, or on the FSE page.
74 $type = isset($GLOBALS['typenow']) ? $GLOBALS['typenow'] : '';
75 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
76 if (!$type && isset($_GET['postType'])) {
77 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
78 $type = sanitize_text_field(wp_unslash($_GET['postType']));
79 }
80
81 if (\use_block_editor_for_post_type($type)) {
82 return $hook && in_array($hook, ['post.php', 'post-new.php'], true);
83 }
84
85 // Temporarily disable the library on the site editor page until the issues with 6.3 are fixed.
86 return false;
87 }
88
89 /**
90 * Adds various scripts and styles
91 *
92 * @return void
93 */
94 public function addScopedScriptsAndStyles()
95 {
96 $userInfo = \get_user_option('extendify_library_user');
97 $partnerData = PartnerData::getPartnerData();
98 $hasPartner = PartnerData::$id && PartnerData::$id !== 'no-partner';
99 $userInfo = $userInfo ? json_decode($userInfo, true) : [
100 'state' => ['openOnNewPage' => $hasPartner],
101 'version' => 0,
102 ];
103 $siteInfo = \get_option('extendify_library_site_data', [
104 'state' => ['siteType' => \get_option('extendify_siteType', new \stdClass())],
105 'version' => 0,
106 ]);
107 $version = Config::$environment === 'PRODUCTION' ? Config::$version : uniqid();
108 $scriptAssetPath = EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify-library.php'];
109 $fallback = [
110 'dependencies' => [],
111 'version' => $version,
112 ];
113 $scriptAsset = file_exists($scriptAssetPath) ? require $scriptAssetPath : $fallback;
114 foreach ($scriptAsset['dependencies'] as $style) {
115 \wp_enqueue_style($style);
116 }
117
118 \wp_register_script(
119 Config::$slug . 'library-scripts',
120 EXTENDIFY_BASE_URL . 'public/build/' . Config::$assetManifest['extendify-library.js'],
121 $scriptAsset['dependencies'],
122 $scriptAsset['version'],
123 true
124 );
125
126 \wp_add_inline_script(
127 Config::$slug . 'library-scripts',
128 'window.extLibraryData = ' . \wp_json_encode([
129 'devbuild' => Config::$environment === 'DEVELOPMENT',
130 'siteId' => \get_option('extendify_site_id', ''),
131 'insightsEnabled' => defined('EXTENDIFY_INSIGHTS_URL'),
132 'root' => \esc_url_raw(\rest_url(Config::$slug . '/' . Config::$apiVersion)),
133 'nonce' => \wp_create_nonce('wp_rest'),
134 'partnerLogo' => \esc_attr(PartnerData::$logo),
135 'partnerName' => \esc_attr(PartnerData::$name),
136 'partnerId' => \esc_attr(PartnerData::$id),
137 'showLocalizedCopy' => array_key_exists('showLocalizedCopy', $partnerData),
138 'userInfo' => $userInfo,
139 'siteInfo' => $siteInfo,
140 'wpVersion' => \get_bloginfo('version'),
141 'themeSlug' => \get_option('stylesheet'),
142 'globalStylesPostID' => \WP_Theme_JSON_Resolver::get_user_global_styles_post_id(),
143 'wpLanguage' => \get_locale(),
144 'asset_path' => \esc_url(EXTENDIFY_URL . 'public/assets'),
145 ]),
146 'before'
147 );
148
149 \wp_enqueue_script(Config::$slug . 'library-scripts');
150 \wp_set_script_translations(Config::$slug . 'library-scripts', 'extendify-local', EXTENDIFY_PATH . 'languages/js');
151
152 // Inline the library styles to keep them out of the iframe live preview.
153 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
154 $css = file_get_contents(
155 EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify-library.css']
156 );
157 \wp_register_style(Config::$slug, false, [], Config::$version);
158 \wp_enqueue_style(Config::$slug);
159 \wp_add_inline_style(Config::$slug, $css);
160
161 $cssColorVars = PartnerData::cssVariableMapping();
162 $cssString = implode('; ', array_map(function ($k, $v) {
163 return "$k: $v";
164 }, array_keys($cssColorVars), $cssColorVars));
165 wp_add_inline_style(Config::$slug, "body { $cssString; }");
166 }
167
168 /**
169 * Adds user meta to the user profile
170 *
171 * @return void
172 */
173 public function registerUserMeta()
174 {
175 add_action('rest_api_init', function () {
176 register_rest_field('user',
177 'extendify_library_user',
178 [
179 'get_callback' => function ($user) {
180 return \get_user_option('extendify_library_user', $user['id']);
181 },
182 'update_callback' => function ($value, $user) {
183 return \update_user_option($user->ID, 'extendify_library_user', $value);
184 },
185 'schema' => [
186 'description' => __('Extendify Library User Settings', 'extendify-local'),
187 'type' => 'string',
188 ],
189 ]
190 );
191 });
192 }
193
194 /**
195 * Adds deactivation prompt JS script
196 *
197 * @return void
198 */
199 public function maybeAddDeactivationScript()
200 {
201 $screen = get_current_screen();
202 if (!isset($screen->id) || $screen->id !== 'plugins') {
203 return;
204 }
205
206 if (!get_option('extendify_pattern_was_imported', false)) {
207 return;
208 }
209
210 $version = Config::$environment === 'PRODUCTION' ? Config::$version : uniqid();
211 $scriptAssetPath = EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify-deactivate.php'];
212 $fallback = [
213 'dependencies' => [],
214 'version' => $version,
215 ];
216 $scriptAsset = file_exists($scriptAssetPath) ? require $scriptAssetPath : $fallback;
217 foreach ($scriptAsset['dependencies'] as $style) {
218 wp_enqueue_style($style);
219 }
220
221 \wp_register_script(
222 Config::$slug . '-deactivate-scripts',
223 EXTENDIFY_BASE_URL . 'public/build/' . Config::$assetManifest['extendify-deactivate.js'],
224 $scriptAsset['dependencies'],
225 $scriptAsset['version'],
226 true
227 );
228
229 \wp_localize_script(
230 Config::$slug . '-deactivate-scripts',
231 'extendifyData',
232 array_merge([
233 'root' => \esc_url_raw(rest_url(Config::$slug . '/' . Config::$apiVersion)),
234 'nonce' => \wp_create_nonce('wp_rest'),
235 'partnerLogo' => \esc_attr(PartnerData::$logo),
236 'partnerName' => \esc_attr(PartnerData::$name),
237 'adminUrl' => \esc_url_raw(\admin_url()),
238 ])
239 );
240
241 \wp_enqueue_script(Config::$slug . '-deactivate-scripts');
242
243 \wp_set_script_translations(Config::$slug . '-deactivate-scripts', 'extendify-local', EXTENDIFY_PATH . 'languages/js');
244
245 \wp_enqueue_style(Config::$slug, EXTENDIFY_BASE_URL . '/public/build/' . Config::$assetManifest['extendify-library.css'], [], Config::$version);
246 }
247
248 }
249