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 / Controllers / WPController.php

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

186 lines 5.0 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\Shared\DataProvider\ResourceData;
13 use Extendify\Shared\Services\AutoUpdate\AutoUpdate;
14 use Extendify\Shared\Services\Sanitizer;
15
16 /**
17 * The controller for interacting with WordPress.
18 */
19
20 class WPController
21 {
22 /**
23 * Persist the data
24 *
25 * @param \WP_REST_Request $request - The request.
26 * @return \WP_REST_Response
27 */
28 public static function updateOption($request)
29 {
30 // TODO: Move unprefixed updates to the server, or create an allowlist.
31 $params = $request->get_json_params();
32 \update_option($params['option'], Sanitizer::sanitizeUnknown($params['value']));
33
34 return new \WP_REST_Response('ok');
35 }
36 /**
37 * Save a block to the database
38 *
39 * @param \WP_REST_Request $request - The request.
40 * @return \WP_REST_Response
41 */
42 public static function savePattern($request)
43 {
44 $params = $request->get_json_params();
45 $key = $params['option'];
46 // Remove the 'extendify_' prefix if it exists.
47 if (strpos($key, 'extendify_') === 0) {
48 $key = substr($key, 10);
49 }
50
51 \update_option('extendify_' . $key, Sanitizer::sanitizeBlocks($params['value']));
52
53 return new \WP_REST_Response('ok');
54 }
55
56 /**
57 * Get a setting from the options table
58 *
59 * @param \WP_REST_Request $request - The request.
60 * @return \WP_REST_Response
61 */
62 public static function getOption($request)
63 {
64 $value = \get_option($request->get_param('option'), null);
65 return new \WP_REST_Response($value);
66 }
67
68 /**
69 * Get the list of active plugins slugs
70 *
71 * @return \WP_REST_Response
72 */
73 public static function getActivePlugins()
74 {
75 return new \WP_REST_Response(array_values(\get_option('active_plugins', [])));
76 }
77
78 /**
79 * This function will force the regenerating of the cache.
80 *
81 * @return \WP_REST_Response
82 */
83 public static function prefetchAssistData()
84 {
85 if (class_exists(ResourceData::class)) {
86 (new ResourceData())->getData();
87 }
88
89 return new \WP_REST_Response('ok', 200);
90 }
91
92 /**
93 * Create a post of type wp_navigation with meta.
94 *
95 * @param \WP_REST_Request $request - The request.
96 * @return \WP_REST_Response
97 */
98 public static function createNavigationWithMeta($request)
99 {
100 $post = \wp_insert_post([
101 'post_type' => 'wp_navigation',
102 'post_title' => $request->get_param('title'),
103 'post_name' => $request->get_param('slug'),
104 'post_status' => 'publish',
105 'post_content' => $request->get_param('content'),
106 ]);
107
108 \add_post_meta($post, 'made_with_extendify_launch', 1);
109
110 return new \WP_REST_Response([
111 'id' => $post,
112 ]);
113 }
114
115 /**
116 * Returns a nav menu matching the given slug.
117 *
118 * @return \WP_REST_Response
119 */
120 public static function getNavigation($request)
121 {
122 $slug = $request->get_param('slug');
123 $args = [
124 'post_type' => 'wp_navigation',
125 'name' => $slug,
126 'post_status' => 'publish',
127 'numberposts' => 1,
128 ];
129
130 $posts = \get_posts($args);
131
132 if (empty($posts)) {
133 return new \WP_REST_Response([
134 'message' => 'Navigation not found',
135 ], 404);
136 }
137
138 $post = $posts[0];
139
140 return new \WP_REST_Response([
141 'id' => $post->ID,
142 'title' => $post->post_title,
143 'slug' => $post->post_name,
144 'content' => $post->post_content,
145 ]);
146 }
147
148 /**
149 * This function will be called post finishing the Launch..
150 *
151 * @return \WP_REST_Response
152 */
153 public static function postLaunch()
154 {
155 // Set the state to import images.
156 \update_option('extendify_check_for_image_imports', true, false);
157 \delete_transient('extendify_import_images_check_delay');
158
159 \update_option('extendify_onboarding_completed', gmdate('Y-m-d\TH:i:s\Z'));
160
161 \do_action('extendify_after_launch');
162
163 return new \WP_REST_Response('ok');
164 }
165
166 /**
167 * Runs every time the launch page loads: resets launch state (a no-op on a
168 * fresh site, the expected reset on an existing one) and enables auto-updates.
169 *
170 * @return \WP_REST_Response
171 */
172 public static function preLaunch()
173 {
174 \delete_option('extendify_onboarding_completed');
175 ChatHistoryController::clear();
176
177 if (AutoUpdate::isEnabled()) {
178 AutoUpdate::enableAutoUpdateForPlugin(EXTENDIFY_PLUGIN_BASENAME);
179 AutoUpdate::addToAutoUpdateList('auto_update_themes', 'extendable');
180 AutoUpdate::enableAutoUpdateForCore();
181 }
182
183 return new \WP_REST_Response(['success' => true]);
184 }
185 }
186