PluginProbe
Extendify / 3.1.0
Extendify v3.1.0
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 / AutoLaunch / Admin.php

Admin.php in Extendify 3.1.0, at app/AutoLaunch/Admin.php

264 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Admin.
5 */
6
7 namespace Extendify\AutoLaunch;
8
9 defined('ABSPATH') || die('No direct access.');
10
11 use Extendify\Config;
12 use Extendify\Insights;
13 use Extendify\PartnerData;
14
15 /**
16 * This class handles any file loading for the admin area.
17 */
18
19 class Admin
20 {
21 /**
22 * Adds various actions to set up the page
23 *
24 * @return void
25 */
26 public function __construct()
27 {
28 \add_action('admin_enqueue_scripts', [$this, 'addScopedScriptsAndStyles']);
29 }
30
31 /**
32 * Adds various JS scripts
33 *
34 * @return void
35 */
36 public function addScopedScriptsAndStyles()
37 {
38 $version = constant('EXTENDIFY_DEVMODE') ? uniqid() : Config::$version;
39 $scriptAssetPath = EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify-launch.php'];
40 $fallback = [
41 'dependencies' => [],
42 'version' => $version,
43 ];
44 $scriptAsset = file_exists($scriptAssetPath) ? require $scriptAssetPath : $fallback;
45 foreach ($scriptAsset['dependencies'] as $style) {
46 wp_enqueue_style($style);
47 }
48
49 \wp_enqueue_script(
50 Config::$slug . '-launch-scripts',
51 EXTENDIFY_BASE_URL . 'public/build/' . Config::$assetManifest['extendify-auto-launch.js'],
52 array_merge([Config::$slug . '-shared-scripts'], $scriptAsset['dependencies']),
53 $scriptAsset['version'],
54 true
55 );
56
57 if (constant('EXTENDIFY_DEVMODE')) {
58 // In dev, reset the variaton to the default.
59 wp_update_post([
60 'ID' => \WP_Theme_JSON_Resolver::get_user_global_styles_post_id(),
61 'post_content' => \wp_json_encode([
62 'styles' => [],
63 'settings' => [],
64 'isGlobalStylesUserThemeJSON' => true,
65 'version' => 2,
66 ]),
67 ]);
68 }
69
70 \wp_add_inline_script(
71 Config::$slug . '-launch-scripts',
72 'window.extLaunchData = ' . \wp_json_encode([
73 'editorStyles' => \get_block_editor_settings([], new \WP_Block_Editor_Context()),
74 'wpRoot' => \rest_url(),
75 'activeTests' => \get_option(Insights::ACTIVE_TESTS_OPTION, []),
76 'resetSiteInformation' => [
77 'pagesIds' => array_map('esc_attr', $this->getLaunchCreatedPages()),
78 'navigationsIds' => array_map('esc_attr', $this->getLaunchCreatedNavigations()),
79 'templatePartsIds' => array_map('esc_attr', $this->getTemplatePartIds()),
80 'pageWithTitleTemplateId' => esc_attr($this->getPageWithTitleTemplateId()),
81 ],
82 'urlParams' => (object) $this->getURLParams(),
83 'helloWorldPostSlug' =>
84 // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
85 \_x(
86 'hello-world',
87 'Default post slug'
88 ),
89 'hideAutoLaunchExitLink' => (bool) PartnerData::setting('hideLaunchExitLink'),
90 ]),
91 'before'
92 );
93
94 \wp_set_script_translations(
95 Config::$slug . '-launch-scripts',
96 'extendify-local',
97 EXTENDIFY_PATH . 'languages/js'
98 );
99
100 \wp_enqueue_style(
101 Config::$slug . '-launch-styles',
102 EXTENDIFY_BASE_URL . 'public/build/' . Config::$assetManifest['extendify-auto-launch.css'],
103 [Config::$slug . '-shared-common-styles'],
104 Config::$version
105 );
106 }
107
108 /**
109 * Returns all the pages created by Extendify.
110 *
111 * @return array
112 */
113 public static function getLaunchCreatedPages()
114 {
115 $posts = get_posts([
116 'numberposts' => -1,
117 'post_status' => 'publish',
118 'post_type' => ['page', 'post'],
119 // only return the ID field.
120 'fields' => 'ids',
121 ]);
122
123 return array_values(array_filter(array_map(function ($post) {
124 return get_post_meta($post, 'made_with_extendify_launch') ? $post : false;
125 }, $posts)));
126 }
127
128 /**
129 * Returns all the navigations created by Extendify.
130 *
131 * @return array
132 */
133 public static function getLaunchCreatedNavigations()
134 {
135 $posts = get_posts([
136 'numberposts' => -1,
137 'post_status' => 'publish',
138 'post_type' => 'wp_navigation',
139 // only return the ID field.
140 'fields' => 'ids',
141 ]);
142
143 return array_values(array_filter(array_map(function ($post) {
144 return get_post_meta($post, 'made_with_extendify_launch') ? $post : false;
145 }, $posts)));
146 }
147
148 /**
149 * Returns the idz of the header and footer template part created by extendify.
150 *
151 * @return array
152 */
153 public static function getTemplatePartIds()
154 {
155 return [
156 (get_block_template(get_stylesheet() . '//header', 'wp_template_part')->id ?? ''),
157 (get_block_template(get_stylesheet() . '//footer', 'wp_template_part')->id ?? ''),
158 ];
159 }
160
161
162 /**
163 * Returns the id of the page-with-title template for the current theme.
164 *
165 * @return string
166 */
167 public static function getPageWithTitleTemplateId()
168 {
169 $template = get_block_template(get_stylesheet() . '//page-with-title', 'wp_template');
170 return $template && !empty($template->id) ? $template->id : '';
171 }
172
173 /**
174 * Parses and sanitizes URL parameters against an allowlist.
175 *
176 * @return array
177 */
178 private function getURLParams()
179 {
180 $allowed = [
181 'type' => 'string',
182 'title' => 'string',
183 'description' => 'string',
184 'objective' => 'string',
185 'category' => 'string',
186 'structure' => 'string',
187 'tone' => 'string[]',
188 'products' => 'string|boolean',
189 'appointments' => 'boolean',
190 'events' => 'boolean',
191 'donations' => 'boolean',
192 'multilingual' => 'boolean',
193 'contact' => 'boolean',
194 'address' => 'string|boolean',
195 'blog' => 'boolean',
196 'landing-page' => 'string',
197 'cta-link' => 'string|boolean',
198 'build-id' => 'string',
199 'go' => 'boolean',
200 ];
201
202 $params = [];
203 foreach ($allowed as $param => $type) {
204 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
205 if (!isset($_GET[$param])) {
206 continue;
207 }
208
209 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
210 $raw = sanitize_text_field(wp_unslash($_GET[$param]));
211 if ($raw === '') {
212 continue;
213 }
214
215 if ($type === 'string[]') {
216 $parts = is_array($raw) ? $raw : explode(',', (string) $raw);
217
218 $items = array_values(array_filter(
219 array_map(
220 static function ($v) {
221 return sanitize_text_field((string) $v);
222 },
223 $parts
224 ),
225 static function ($v) {
226 return $v !== '';
227 }
228 ));
229
230 if (empty($items)) {
231 continue;
232 }
233
234 $params[$param] = $items;
235 continue;
236 }
237
238 if (strtolower($raw) === 'none') {
239 $params[$param] = false;
240 continue;
241 }
242
243 $asBool = filter_var($raw, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
244
245 if ($type === 'boolean') {
246 if ($asBool === null) {
247 continue;
248 }
249 $params[$param] = $asBool;
250 continue;
251 }
252
253 if ($type === 'string|boolean' && $asBool !== null) {
254 $params[$param] = $asBool;
255 continue;
256 }
257
258 $params[$param] = $raw;
259 }
260
261 return $params;
262 }
263 }
264