| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* WP Controller |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Launch\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(['success' => true]); |
| 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(['success' => true]); |
| 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([ |
| 64 |
'success' => true, |
| 65 |
'data' => $value, |
| 66 |
]); |
| 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([ |
| 77 |
'success' => true, |
| 78 |
'data' => array_values(\get_option('active_plugins', [])), |
| 79 |
]); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* This function will force the regenerating of the cache. |
| 84 |
* |
| 85 |
* @return \WP_REST_Response |
| 86 |
*/ |
| 87 |
public static function prefetchAssistData() |
| 88 |
{ |
| 89 |
if (class_exists(ResourceData::class)) { |
| 90 |
(new ResourceData())->getData(); |
| 91 |
} |
| 92 |
|
| 93 |
return new \WP_REST_Response(true, 200); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Create a post of type wp_navigation with meta. |
| 98 |
* |
| 99 |
* @param \WP_REST_Request $request - The request. |
| 100 |
* @return \WP_REST_Response |
| 101 |
*/ |
| 102 |
public static function createNavigationWithMeta($request) |
| 103 |
{ |
| 104 |
$post = \wp_insert_post([ |
| 105 |
'post_type' => 'wp_navigation', |
| 106 |
'post_title' => $request->get_param('title'), |
| 107 |
'post_name' => $request->get_param('slug'), |
| 108 |
'post_status' => 'publish', |
| 109 |
'post_content' => $request->get_param('content'), |
| 110 |
]); |
| 111 |
|
| 112 |
\add_post_meta($post, 'made_with_extendify_launch', 1); |
| 113 |
|
| 114 |
return new \WP_REST_Response([ |
| 115 |
'success' => true, |
| 116 |
'id' => $post, |
| 117 |
]); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* This function will be called post finishing the Launch.. |
| 122 |
* |
| 123 |
* @return \WP_REST_Response |
| 124 |
*/ |
| 125 |
public static function postLaunch() |
| 126 |
{ |
| 127 |
// Set the state to import images. |
| 128 |
\update_option('extendify_check_for_image_imports', true, false); |
| 129 |
\delete_transient('extendify_import_images_check_delay'); |
| 130 |
|
| 131 |
return new \WP_REST_Response(['success' => true]); |
| 132 |
} |
| 133 |
} |
| 134 |
|