| 1 |
<?php |
| 2 |
/** |
| 3 |
* Global style REST route |
| 4 |
* |
| 5 |
* @package automattic/jetpack-import |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Automattic\Jetpack\Import\Endpoints; |
| 9 |
|
| 10 |
use WP_Error; |
| 11 |
use WP_REST_Request; |
| 12 |
use WP_REST_Response; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit( 0 ); |
| 16 |
} |
| 17 |
|
| 18 |
require_once ABSPATH . 'wp-includes/class-wp-theme-json-resolver.php'; |
| 19 |
require_once ABSPATH . 'wp-includes/theme.php'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class Global_Style |
| 23 |
*/ |
| 24 |
class Global_Style extends \WP_REST_Posts_Controller { |
| 25 |
|
| 26 |
/** |
| 27 |
* Base class |
| 28 |
*/ |
| 29 |
use Import; |
| 30 |
|
| 31 |
/** |
| 32 |
* The Import ID add a new item to the schema. |
| 33 |
*/ |
| 34 |
use Import_ID; |
| 35 |
|
| 36 |
/** |
| 37 |
* Whether the controller supports batching. |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
protected $allow_batch = array( 'v1' => true ); |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructor. |
| 45 |
*/ |
| 46 |
public function __construct() { |
| 47 |
parent::__construct( 'wp_global_styles' ); |
| 48 |
|
| 49 |
$this->rest_base = 'global-styles'; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Adds the schema from additional fields to a schema array. |
| 54 |
* |
| 55 |
* The type of object is inferred from the passed schema. |
| 56 |
* |
| 57 |
* @param array $schema Schema array. |
| 58 |
* @return array Modified Schema array. |
| 59 |
*/ |
| 60 |
public function add_additional_fields_schema( $schema ) { |
| 61 |
$schema['properties']['theme'] = array( |
| 62 |
'description' => __( 'The name of the theme.', 'jetpack-import' ), |
| 63 |
'type' => 'string', |
| 64 |
'required' => true, |
| 65 |
); |
| 66 |
|
| 67 |
// The unique identifier is only required for PUT requests. |
| 68 |
return $this->add_unique_identifier_to_schema( $schema, isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'PUT' ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Update the globals style post. |
| 73 |
* |
| 74 |
* @param WP_REST_Request $request Full details about the request. |
| 75 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 76 |
*/ |
| 77 |
public function create_item( $request ) { |
| 78 |
// Set the WP_IMPORTING constant to prevent sync notifications |
| 79 |
$this->set_importing(); |
| 80 |
|
| 81 |
if ( ! class_exists( 'WP_Theme_JSON_Resolver' ) ) { |
| 82 |
require_once ABSPATH . 'wp-includes/class-wp-theme-json-resolver.php'; |
| 83 |
} |
| 84 |
|
| 85 |
$theme = \wp_get_theme( $request['theme'] ); |
| 86 |
|
| 87 |
// Check if the theme exists. |
| 88 |
if ( ! $theme->exists() ) { |
| 89 |
return new WP_Error( |
| 90 |
'theme_not_found', |
| 91 |
__( 'Theme not found.', 'jetpack-import' ), |
| 92 |
array( |
| 93 |
'status' => 400, |
| 94 |
'theme' => $request['theme'], |
| 95 |
) |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get the global styles post, or create it. |
| 101 |
* |
| 102 |
* A global style post is a post with wp-global-styles-{stylesheet} as post slug. |
| 103 |
*/ |
| 104 |
$post = \WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true ); |
| 105 |
|
| 106 |
if ( \is_wp_error( $post ) ) { |
| 107 |
return $post; |
| 108 |
} |
| 109 |
|
| 110 |
$args = array( |
| 111 |
'ID' => $post['ID'], |
| 112 |
'post_content' => $request['content'], |
| 113 |
); |
| 114 |
|
| 115 |
// Update the post with the passed data. |
| 116 |
$post_id = wp_update_post( $args, true, false ); |
| 117 |
|
| 118 |
if ( is_wp_error( $post_id ) ) { |
| 119 |
$post_id->add_data( array( 'status' => 400 ) ); |
| 120 |
|
| 121 |
return $post_id; |
| 122 |
} |
| 123 |
|
| 124 |
// Get the post. |
| 125 |
$post = get_post( $post_id ); |
| 126 |
|
| 127 |
$response = $this->prepare_item_for_response( $post, $request ); |
| 128 |
$response = rest_ensure_response( $response ); |
| 129 |
|
| 130 |
return $this->add_import_id_metadata( $request, $response ); |
| 131 |
} |
| 132 |
} |
| 133 |
|