-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemeManagementTools.php
More file actions
189 lines (159 loc) · 5.43 KB
/
Copy pathThemeManagementTools.php
File metadata and controls
189 lines (159 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Theme management MCP tools.
*
* @package WP_Forge
*/
namespace WP_Forge\Tools;
use WP_Forge\Response;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Registers theme management tools.
*/
trait ThemeManagementTools {
/**
* Theme management abilities.
*
* @return void
*/
private function add_theme_abilities() {
$stylesheet_schema = $this->schema(
array(
'stylesheet' => $this->string_prop( 'Installed theme stylesheet directory name.' ),
),
array( 'stylesheet' )
);
$this->add_ability( self::INTERNAL_PREFIX . 'theme-list', 'List Themes', 'List installed WordPress themes and their activation state', $this->schema(), function () {
return $this->list_themes();
}, true, 'switch_themes' );
$this->add_ability( self::INTERNAL_PREFIX . 'theme-install', 'Install Theme', 'Install a WordPress theme from the WordPress.org theme directory by slug', $this->schema(
array(
'slug' => $this->string_prop( 'WordPress.org theme slug, such as twentytwentyfive.' ),
),
array( 'slug' )
), function ( $params ) {
return $this->install_theme( $params['slug'] );
}, false, 'install_themes' );
$this->add_ability( self::INTERNAL_PREFIX . 'theme-activate', 'Activate Theme', 'Activate an installed WordPress theme by stylesheet directory name', $stylesheet_schema, function ( $params ) {
return $this->activate_theme_tool( $params['stylesheet'] );
}, false, 'switch_themes', array( 'idempotent' => true ) );
$this->add_ability( self::INTERNAL_PREFIX . 'theme-delete', 'Delete Theme', 'Delete an installed WordPress theme by stylesheet directory name', $stylesheet_schema, function ( $params ) {
return $this->delete_theme_tool( $params['stylesheet'] );
}, false, 'delete_themes', array( 'destructive' => true, 'idempotent' => true ) );
}
/**
* List installed themes.
*
* @return array<string,mixed>|array<int,array<string,mixed>>
*/
private function list_themes() {
if ( ! function_exists( 'wp_get_themes' ) ) {
return Response::error( 'This ability requires a WordPress runtime.', 500 );
}
$active = function_exists( 'wp_get_theme' ) ? wp_get_theme()->get_stylesheet() : '';
$themes = array();
foreach ( wp_get_themes() as $stylesheet => $theme ) {
$themes[] = array(
'stylesheet' => $stylesheet,
'name' => $theme->get( 'Name' ),
'version' => $theme->get( 'Version' ),
'author' => wp_strip_all_tags( $theme->get( 'Author' ) ),
'description' => $theme->get( 'Description' ),
'template' => $theme->get_template(),
'active' => $stylesheet === $active,
);
}
return $themes;
}
/**
* Install a theme from WordPress.org.
*
* @param string $slug Theme slug.
* @return array<string,mixed>
*/
private function install_theme( $slug ) {
if ( ! defined( 'ABSPATH' ) ) {
return Response::error( 'This ability requires a WordPress runtime.', 500 );
}
if ( ! function_exists( 'themes_api' ) ) {
require_once ABSPATH . 'wp-admin/includes/theme.php';
}
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$slug = sanitize_key( $slug );
$api = themes_api( 'theme_information', array( 'slug' => $slug ) );
if ( is_wp_error( $api ) ) {
return Response::error( $api->get_error_message(), 400 );
}
$skin = new \Automatic_Upgrader_Skin();
$upgrader = new \Theme_Upgrader( $skin );
$result = $upgrader->install( $api->download_link );
if ( is_wp_error( $result ) ) {
return Response::error( $result->get_error_message(), 400 );
}
if ( is_wp_error( $skin->result ) ) {
return Response::error( $skin->result->get_error_message(), 400 );
}
if ( ! $result ) {
return Response::error( 'Theme installation failed.', 400 );
}
return array(
'slug' => $slug,
'name' => isset( $api->name ) ? $api->name : $slug,
'installed' => true,
'activated' => false,
);
}
/**
* Activate a theme.
*
* @param string $stylesheet Stylesheet.
* @return array<string,mixed>
*/
private function activate_theme_tool( $stylesheet ) {
if ( ! function_exists( 'wp_get_theme' ) || ! function_exists( 'switch_theme' ) ) {
return Response::error( 'This ability requires a WordPress runtime.', 500 );
}
$stylesheet = sanitize_key( $stylesheet );
$theme = wp_get_theme( $stylesheet );
if ( ! $theme->exists() ) {
return Response::error( 'Theme not found: ' . $stylesheet, 404 );
}
switch_theme( $stylesheet );
return array(
'stylesheet' => $stylesheet,
'active' => wp_get_theme()->get_stylesheet() === $stylesheet,
);
}
/**
* Delete a theme.
*
* @param string $stylesheet Stylesheet.
* @return array<string,mixed>
*/
private function delete_theme_tool( $stylesheet ) {
if ( ! function_exists( 'wp_get_theme' ) ) {
return Response::error( 'This ability requires a WordPress runtime.', 500 );
}
if ( ! function_exists( 'delete_theme' ) ) {
require_once ABSPATH . 'wp-admin/includes/theme.php';
}
$stylesheet = sanitize_key( $stylesheet );
if ( wp_get_theme()->get_stylesheet() === $stylesheet ) {
return Response::error( 'The active theme cannot be deleted.', 400 );
}
$theme = wp_get_theme( $stylesheet );
if ( ! $theme->exists() ) {
return Response::error( 'Theme not found: ' . $stylesheet, 404 );
}
$result = delete_theme( $stylesheet );
if ( is_wp_error( $result ) ) {
return Response::error( $result->get_error_message(), 400 );
}
return array(
'stylesheet' => $stylesheet,
'deleted' => (bool) $result,
);
}
}