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