PluginProbe
Extendify / 3.2.3
Extendify v3.2.3
3.2.3 3.2.2 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 All 129 releases
extendify / app / AutoLaunch / Controllers / WPController.php

WPController.php in Extendify 3.2.3, at app/AutoLaunch/Controllers/WPController.php

220 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WP Controller
5 */
6
7 namespace Extendify\AutoLaunch\Controllers;
8
9 defined('ABSPATH') || die('No direct access.');
10
11 use Extendify\Agent\Controllers\ChatHistoryController;
12 use Extendify\PartnerData;
13 use Extendify\Shared\DataProvider\ResourceData;
14 use Extendify\Shared\Services\AutoUpdate\AutoUpdate;
15 use Extendify\Shared\Services\LaunchUpdate\LaunchUpdater;
16 use Extendify\Shared\Services\Sanitizer;
17 use Extendify\SiteVisibility;
18
19 /**
20 * The controller for interacting with WordPress.
21 */
22
23 class WPController
24 {
25 /**
26 * Persist the data
27 *
28 * @param \WP_REST_Request $request - The request.
29 * @return \WP_REST_Response
30 */
31 public static function updateOption($request)
32 {
33 // TODO: Move unprefixed updates to the server, or create an allowlist.
34 $params = $request->get_json_params();
35 \update_option($params['option'], Sanitizer::sanitizeUnknown($params['value']));
36
37 return new \WP_REST_Response('ok');
38 }
39 /**
40 * Save a block to the database
41 *
42 * @param \WP_REST_Request $request - The request.
43 * @return \WP_REST_Response
44 */
45 public static function savePattern($request)
46 {
47 $params = $request->get_json_params();
48 $key = $params['option'];
49 // Remove the 'extendify_' prefix if it exists.
50 if (strpos($key, 'extendify_') === 0) {
51 $key = substr($key, 10);
52 }
53
54 \update_option('extendify_' . $key, Sanitizer::sanitizeBlocks($params['value']));
55
56 return new \WP_REST_Response('ok');
57 }
58
59 /**
60 * Get a setting from the options table
61 *
62 * @param \WP_REST_Request $request - The request.
63 * @return \WP_REST_Response
64 */
65 public static function getOption($request)
66 {
67 $value = \get_option($request->get_param('option'), null);
68 return new \WP_REST_Response($value);
69 }
70
71 /**
72 * Get the list of active plugins slugs
73 *
74 * @return \WP_REST_Response
75 */
76 public static function getActivePlugins()
77 {
78 return new \WP_REST_Response(array_values(\get_option('active_plugins', [])));
79 }
80
81 /**
82 * This function will force the regenerating of the cache.
83 *
84 * @return \WP_REST_Response
85 */
86 public static function prefetchAssistData()
87 {
88 if (class_exists(ResourceData::class)) {
89 (new ResourceData())->getData();
90 }
91
92 return new \WP_REST_Response('ok', 200);
93 }
94
95 /**
96 * Create a post of type wp_navigation with meta.
97 *
98 * @param \WP_REST_Request $request - The request.
99 * @return \WP_REST_Response
100 */
101 public static function createNavigationWithMeta($request)
102 {
103 $post = \wp_insert_post([
104 'post_type' => 'wp_navigation',
105 'post_title' => $request->get_param('title'),
106 'post_name' => $request->get_param('slug'),
107 'post_status' => 'publish',
108 'post_content' => $request->get_param('content'),
109 ]);
110
111 \add_post_meta($post, 'made_with_extendify_launch', 1);
112
113 return new \WP_REST_Response([
114 'id' => $post,
115 ]);
116 }
117
118 /**
119 * Returns a nav menu matching the given slug.
120 *
121 * @return \WP_REST_Response
122 */
123 public static function getNavigation($request)
124 {
125 $slug = $request->get_param('slug');
126 $args = [
127 'post_type' => 'wp_navigation',
128 'name' => $slug,
129 'post_status' => 'publish',
130 'numberposts' => 1,
131 ];
132
133 $posts = \get_posts($args);
134
135 if (empty($posts)) {
136 return new \WP_REST_Response([
137 'message' => 'Navigation not found',
138 ], 404);
139 }
140
141 $post = $posts[0];
142
143 return new \WP_REST_Response([
144 'id' => $post->ID,
145 'title' => $post->post_title,
146 'slug' => $post->post_name,
147 'content' => $post->post_content,
148 ]);
149 }
150
151 /**
152 * This function will be called post finishing the Launch..
153 *
154 * @return \WP_REST_Response
155 */
156 public static function postLaunch()
157 {
158 // Set the state to import images.
159 \update_option('extendify_check_for_image_imports', true, false);
160 \delete_transient('extendify_import_images_check_delay');
161
162 \update_option('extendify_onboarding_completed', gmdate('Y-m-d\TH:i:s\Z'));
163 // Anything already in the chat reads as an onboarding offer the user answered.
164 ChatHistoryController::clear();
165 LaunchUpdater::clearAttempts();
166
167 if (PartnerData::setting('useComingSoon')) {
168 SiteVisibility::markUnpublished();
169 }
170
171 if (PartnerData::setting('useSearchEngineBlock')) {
172 SiteVisibility::blockIndexing();
173 }
174
175 \do_action('extendify_after_launch');
176
177 return new \WP_REST_Response('ok');
178 }
179
180 /**
181 * Apply pending theme / PUC-plugin upgrades before Launch runs (see
182 * LaunchUpdater::run, which never throws).
183 *
184 * @return \WP_REST_Response
185 */
186 public static function runUpdates()
187 {
188 return new \WP_REST_Response(LaunchUpdater::run());
189 }
190
191 /**
192 * Runs on every launch page load, so it must not discard site state.
193 *
194 * @return \WP_REST_Response
195 */
196 public static function preLaunch()
197 {
198 if (AutoUpdate::isEnabled()) {
199 AutoUpdate::enableAutoUpdateForPlugin(EXTENDIFY_PLUGIN_BASENAME);
200 AutoUpdate::addToAutoUpdateList('auto_update_themes', 'extendable');
201 AutoUpdate::enableAutoUpdateForCore();
202 }
203
204 return new \WP_REST_Response(['success' => true]);
205 }
206
207 /**
208 * Discards the launched state after the user confirms the restart.
209 *
210 * @return \WP_REST_Response
211 */
212 public static function resetLaunchState()
213 {
214 \delete_option('extendify_onboarding_completed');
215 ChatHistoryController::clear();
216
217 return new \WP_REST_Response(['success' => true]);
218 }
219 }
220