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