PluginProbe
Extendify / 3.2.1
Extendify v3.2.1
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 / Controllers / WPController.php

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

216 lines 5.8 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 \do_action('extendify_after_launch');
172
173 return new \WP_REST_Response('ok');
174 }
175
176 /**
177 * Apply pending theme / PUC-plugin upgrades before Launch runs (see
178 * LaunchUpdater::run, which never throws).
179 *
180 * @return \WP_REST_Response
181 */
182 public static function runUpdates()
183 {
184 return new \WP_REST_Response(LaunchUpdater::run());
185 }
186
187 /**
188 * Runs on every launch page load, so it must not discard site state.
189 *
190 * @return \WP_REST_Response
191 */
192 public static function preLaunch()
193 {
194 if (AutoUpdate::isEnabled()) {
195 AutoUpdate::enableAutoUpdateForPlugin(EXTENDIFY_PLUGIN_BASENAME);
196 AutoUpdate::addToAutoUpdateList('auto_update_themes', 'extendable');
197 AutoUpdate::enableAutoUpdateForCore();
198 }
199
200 return new \WP_REST_Response(['success' => true]);
201 }
202
203 /**
204 * Discards the launched state after the user confirms the restart.
205 *
206 * @return \WP_REST_Response
207 */
208 public static function resetLaunchState()
209 {
210 \delete_option('extendify_onboarding_completed');
211 ChatHistoryController::clear();
212
213 return new \WP_REST_Response(['success' => true]);
214 }
215 }
216