PluginProbe
Extendify / 3.0.4
Extendify v3.0.4
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.4, at app/AutoLaunch/Admin.php

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