PluginProbe
Extendify / 3.0.6
Extendify v3.0.6
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.0.6, at app/AutoLaunch/Admin.php

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