@wp-operations/wp-ops 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/package.json +6 -2
- package/src/commands/platform.js +58 -2
- package/src/commands/start.js +2 -2
- package/src/main.js +19 -8
- package/wp-operations/CHANGELOG.md +14 -0
- package/wp-operations/README.md +39 -0
- package/wp-operations/agents/builder/AGENT.md +15 -0
- package/wp-operations/assets/admin.css +631 -0
- package/wp-operations/assets/admin.js +614 -0
- package/wp-operations/assets/app/app.css +1 -0
- package/wp-operations/assets/app/app.js +70 -0
- package/wp-operations/assets/app/fonts/inter-medium.woff +0 -0
- package/wp-operations/assets/app/fonts/inter-regular.woff +0 -0
- package/wp-operations/assets/app/fonts/inter-semibold.woff +0 -0
- package/wp-operations/assets/app/login.css +42 -0
- package/wp-operations/assets/brand/icon.svg +20 -0
- package/wp-operations/assets/brand/mark.svg +19 -0
- package/wp-operations/assets/editors/audiobook.css +311 -0
- package/wp-operations/assets/editors/audiofile.css +286 -0
- package/wp-operations/assets/editors/course.css +701 -0
- package/wp-operations/assets/editors/course.js +698 -0
- package/wp-operations/assets/editors/faq.css +258 -0
- package/wp-operations/assets/editors/faq.js +196 -0
- package/wp-operations/assets/editors/interactive.css +220 -0
- package/wp-operations/assets/editors/interactive.js +378 -0
- package/wp-operations/assets/editors/media-sources.css +3 -0
- package/wp-operations/assets/editors/media-sources.js +40 -0
- package/wp-operations/assets/editors/open-media.css +114 -0
- package/wp-operations/assets/editors/open-media.js +182 -0
- package/wp-operations/assets/editors/thumbnails.css +335 -0
- package/wp-operations/assets/editors/thumbnails.js +447 -0
- package/wp-operations/assets/editors/translator.css +818 -0
- package/wp-operations/functions.php +24 -0
- package/wp-operations/index.php +14 -0
- package/wp-operations/platform.json +1836 -0
- package/wp-operations/skills/ai-development-guardrails/SKILL.md +49 -0
- package/wp-operations/skills/gutenberg-reference/SKILL.md +45 -0
- package/wp-operations/skills/js-tooling-for-themes/SKILL.md +51 -0
- package/wp-operations/skills/tailwind-for-wp/SKILL.md +62 -0
- package/wp-operations/skills/wp-clean-classic-themes/SKILL.md +112 -0
- package/wp-operations/skills/wp-psr4-plugins/SKILL.md +2043 -0
- package/wp-operations/skills/wp-security/SKILL.md +90 -0
- package/wp-operations/src/Admin.php +78 -0
- package/wp-operations/src/App/Access.php +57 -0
- package/wp-operations/src/App/Admin.php +442 -0
- package/wp-operations/src/App/App.php +157 -0
- package/wp-operations/src/App/Apps.php +379 -0
- package/wp-operations/src/App/Bundle.php +111 -0
- package/wp-operations/src/App/Contents.php +676 -0
- package/wp-operations/src/App/Engine.php +554 -0
- package/wp-operations/src/App/Github.php +308 -0
- package/wp-operations/src/App/Module.php +23 -0
- package/wp-operations/src/App/Ops.php +470 -0
- package/wp-operations/src/App/Packages.php +289 -0
- package/wp-operations/src/App/Rest.php +539 -0
- package/wp-operations/src/App/Sandboxes.php +206 -0
- package/wp-operations/src/App/Store.php +104 -0
- package/wp-operations/src/App/Usage.php +268 -0
- package/wp-operations/src/Config.php +249 -0
- package/wp-operations/src/Content/Editor.php +223 -0
- package/wp-operations/src/Content/Fields.php +681 -0
- package/wp-operations/src/Content/Scoring.php +253 -0
- package/wp-operations/src/Content/Structure.php +293 -0
- package/wp-operations/src/Content/Types.php +150 -0
- package/wp-operations/src/Content/Uploads.php +50 -0
- package/wp-operations/src/Editors/AudiobookAuthor.php +61 -0
- package/wp-operations/src/Editors/AudiobookChapters.php +632 -0
- package/wp-operations/src/Editors/AudiobookCover.php +235 -0
- package/wp-operations/src/Editors/Audiofile.php +462 -0
- package/wp-operations/src/Editors/Course.php +784 -0
- package/wp-operations/src/Editors/Ebook.php +242 -0
- package/wp-operations/src/Editors/Editors.php +44 -0
- package/wp-operations/src/Editors/Faq.php +237 -0
- package/wp-operations/src/Editors/Files.php +139 -0
- package/wp-operations/src/Editors/Interactive/Builder.php +103 -0
- package/wp-operations/src/Editors/Interactive/Card.php +76 -0
- package/wp-operations/src/Editors/Interactive/Editor.php +42 -0
- package/wp-operations/src/Editors/Interactive/Field.php +272 -0
- package/wp-operations/src/Editors/Interactive/Map.php +84 -0
- package/wp-operations/src/Editors/Interactive/Models.php +227 -0
- package/wp-operations/src/Editors/Interactive/Report.php +59 -0
- package/wp-operations/src/Editors/Interactive/Save.php +323 -0
- package/wp-operations/src/Editors/Interactive/Scoring.php +244 -0
- package/wp-operations/src/Editors/Interactive/Structure.php +195 -0
- package/wp-operations/src/Editors/Legal.php +235 -0
- package/wp-operations/src/Editors/OpenMedia.php +140 -0
- package/wp-operations/src/Editors/OriginalTitle.php +112 -0
- package/wp-operations/src/Editors/Sources.php +28 -0
- package/wp-operations/src/Editors/Thumbnails/Admin.php +306 -0
- package/wp-operations/src/Editors/Thumbnails/Ajax.php +163 -0
- package/wp-operations/src/Editors/Thumbnails/Box.php +188 -0
- package/wp-operations/src/Editors/Thumbnails/Storage.php +228 -0
- package/wp-operations/src/Editors/Translator/Admin.php +913 -0
- package/wp-operations/src/Editors/Translator/Api.php +63 -0
- package/wp-operations/src/Editors/Translator/Bar.php +83 -0
- package/wp-operations/src/Editors/Translator/Fields.php +273 -0
- package/wp-operations/src/Editors/Translator/Storage.php +131 -0
- package/wp-operations/src/Editors/Translator/Thumbnail.php +215 -0
- package/wp-operations/src/Identity.php +104 -0
- package/wp-operations/src/Languages.php +45 -0
- package/wp-operations/src/Lockdown.php +85 -0
- package/wp-operations/src/Modules/Apps/Admin.php +472 -0
- package/wp-operations/src/Modules/Apps/Api.php +193 -0
- package/wp-operations/src/Modules/Apps/Apps.php +279 -0
- package/wp-operations/src/Modules/Apps/Formatters.php +478 -0
- package/wp-operations/src/Modules/Apps/Schemes.php +248 -0
- package/wp-operations/src/Modules/Apps/Table.php +101 -0
- package/wp-operations/src/Modules/Thumbnails.php +68 -0
- package/wp-operations/src/Modules/Translations.php +133 -0
- package/wp-operations/src/Platform.php +59 -0
- package/wp-operations/src/Setup.php +273 -0
- package/wp-operations/src/Status.php +99 -0
- package/wp-operations/src/Storage/Client.php +168 -0
- package/wp-operations/src/Storage/Offload.php +120 -0
- package/wp-operations/src/Storage/Settings.php +166 -0
- package/wp-operations/src/Storage/Storage.php +35 -0
- package/wp-operations/src/Updates.php +151 -0
- package/wp-operations/style.css +11 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Installs tables and roles, and moves data over from the previous plugins.
|
|
4
|
+
*
|
|
5
|
+
* @package Platform
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
namespace Platform;
|
|
9
|
+
|
|
10
|
+
defined( 'ABSPATH' ) || exit;
|
|
11
|
+
|
|
12
|
+
final class Setup {
|
|
13
|
+
|
|
14
|
+
const OPTION = 'platform_schema';
|
|
15
|
+
|
|
16
|
+
const TABLE_MOVES = [
|
|
17
|
+
'content_translations' => 'translations',
|
|
18
|
+
'thumbnails_groups' => 'thumbnail_groups',
|
|
19
|
+
'thumbnails_group_items' => 'thumbnail_items',
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
public static function register(): void {
|
|
23
|
+
add_action( 'after_switch_theme', [ __CLASS__, 'run' ] );
|
|
24
|
+
add_action( 'admin_init', [ __CLASS__, 'maybe_run' ] );
|
|
25
|
+
add_action( 'rest_api_init', [ __CLASS__, 'maybe_run' ] );
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public static function maybe_run(): void {
|
|
29
|
+
if ( (int) get_option( self::OPTION ) < Platform::SCHEMA ) {
|
|
30
|
+
self::run();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public static function run(): void {
|
|
35
|
+
self::tables();
|
|
36
|
+
self::roles();
|
|
37
|
+
self::move_tables();
|
|
38
|
+
self::move_apps();
|
|
39
|
+
self::move_options();
|
|
40
|
+
self::move_meta();
|
|
41
|
+
|
|
42
|
+
update_option( self::OPTION, Platform::SCHEMA );
|
|
43
|
+
flush_rewrite_rules( false );
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public static function table( string $name ): string {
|
|
47
|
+
global $wpdb;
|
|
48
|
+
|
|
49
|
+
return $wpdb->prefix . $name;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private static function tables(): void {
|
|
53
|
+
global $wpdb;
|
|
54
|
+
|
|
55
|
+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
|
56
|
+
|
|
57
|
+
$charset = $wpdb->get_charset_collate();
|
|
58
|
+
|
|
59
|
+
dbDelta(
|
|
60
|
+
[
|
|
61
|
+
'CREATE TABLE ' . self::table( 'apps' ) . " (
|
|
62
|
+
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
63
|
+
name varchar(255) NOT NULL DEFAULT '',
|
|
64
|
+
user_id bigint(20) unsigned NOT NULL DEFAULT 0,
|
|
65
|
+
environments text NOT NULL,
|
|
66
|
+
contents longtext NOT NULL,
|
|
67
|
+
links text NOT NULL,
|
|
68
|
+
languages text NOT NULL,
|
|
69
|
+
metadata longtext NOT NULL,
|
|
70
|
+
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
71
|
+
PRIMARY KEY (id),
|
|
72
|
+
KEY user_id (user_id)
|
|
73
|
+
) $charset;",
|
|
74
|
+
'CREATE TABLE ' . self::table( 'app_records' ) . " (
|
|
75
|
+
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
76
|
+
scheme varchar(50) NOT NULL DEFAULT '',
|
|
77
|
+
title varchar(255) NOT NULL DEFAULT '',
|
|
78
|
+
data longtext NOT NULL,
|
|
79
|
+
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
80
|
+
PRIMARY KEY (id),
|
|
81
|
+
KEY scheme (scheme)
|
|
82
|
+
) $charset;",
|
|
83
|
+
'CREATE TABLE ' . self::table( 'translations' ) . " (
|
|
84
|
+
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
85
|
+
object_id bigint(20) unsigned NOT NULL,
|
|
86
|
+
object_type varchar(20) NOT NULL DEFAULT 'post',
|
|
87
|
+
translations longtext NOT NULL,
|
|
88
|
+
updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
89
|
+
PRIMARY KEY (id),
|
|
90
|
+
UNIQUE KEY object (object_id,object_type)
|
|
91
|
+
) $charset;",
|
|
92
|
+
'CREATE TABLE ' . self::table( 'thumbnail_groups' ) . " (
|
|
93
|
+
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
94
|
+
name varchar(255) NOT NULL DEFAULT '',
|
|
95
|
+
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
96
|
+
PRIMARY KEY (id)
|
|
97
|
+
) $charset;",
|
|
98
|
+
'CREATE TABLE ' . self::table( 'thumbnail_items' ) . " (
|
|
99
|
+
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
100
|
+
group_id bigint(20) unsigned NOT NULL,
|
|
101
|
+
object_id bigint(20) unsigned NOT NULL,
|
|
102
|
+
object_type varchar(20) NOT NULL DEFAULT 'post',
|
|
103
|
+
asset_type varchar(50) NOT NULL DEFAULT 'thumbnail',
|
|
104
|
+
attachment_id bigint(20) unsigned NOT NULL DEFAULT 0,
|
|
105
|
+
metadata longtext NOT NULL,
|
|
106
|
+
updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
107
|
+
PRIMARY KEY (id),
|
|
108
|
+
UNIQUE KEY group_object (group_id,object_id,object_type,asset_type),
|
|
109
|
+
KEY object_id (object_id)
|
|
110
|
+
) $charset;",
|
|
111
|
+
'CREATE TABLE ' . self::table( 'prompts' ) . " (
|
|
112
|
+
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
113
|
+
user_id bigint(20) unsigned NOT NULL DEFAULT 0,
|
|
114
|
+
app_id bigint(20) unsigned NOT NULL DEFAULT 0,
|
|
115
|
+
kind varchar(40) NOT NULL DEFAULT 'chat',
|
|
116
|
+
mode varchar(20) NOT NULL DEFAULT '',
|
|
117
|
+
prompt longtext NOT NULL,
|
|
118
|
+
response longtext NOT NULL,
|
|
119
|
+
status varchar(20) NOT NULL DEFAULT 'ok',
|
|
120
|
+
error varchar(255) NOT NULL DEFAULT '',
|
|
121
|
+
credits decimal(10,2) NOT NULL DEFAULT 0,
|
|
122
|
+
tokens_in int(10) unsigned NOT NULL DEFAULT 0,
|
|
123
|
+
tokens_out int(10) unsigned NOT NULL DEFAULT 0,
|
|
124
|
+
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
125
|
+
PRIMARY KEY (id),
|
|
126
|
+
KEY user_created (user_id,created_at),
|
|
127
|
+
KEY app_id (app_id)
|
|
128
|
+
) $charset;",
|
|
129
|
+
]
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private static function roles(): void {
|
|
134
|
+
if ( ! get_role( Modules\Apps\Apps::ROLE ) ) {
|
|
135
|
+
add_role( Modules\Apps\Apps::ROLE, 'Service Client', [ 'read' => true, Modules\Apps\Apps::CAP => true ] );
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private static function move_tables(): void {
|
|
140
|
+
global $wpdb;
|
|
141
|
+
|
|
142
|
+
foreach ( self::TABLE_MOVES as $old => $new ) {
|
|
143
|
+
$from = self::table( $old );
|
|
144
|
+
$to = self::table( $new );
|
|
145
|
+
|
|
146
|
+
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $from ) ) !== $from ) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if ( (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$to}" ) > 0 ) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
$columns = array_intersect(
|
|
155
|
+
$wpdb->get_col( "SHOW COLUMNS FROM {$from}" ),
|
|
156
|
+
$wpdb->get_col( "SHOW COLUMNS FROM {$to}" )
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
if ( $columns ) {
|
|
160
|
+
$list = '`' . implode( '`,`', $columns ) . '`';
|
|
161
|
+
$wpdb->query( "INSERT INTO {$to} ({$list}) SELECT {$list} FROM {$from}" );
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private static function move_apps(): void {
|
|
167
|
+
global $wpdb;
|
|
168
|
+
|
|
169
|
+
$configs = self::table( 'service_clients' );
|
|
170
|
+
$old = self::table( 'service_apps' );
|
|
171
|
+
$apps = self::table( 'apps' );
|
|
172
|
+
|
|
173
|
+
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $configs ) ) !== $configs || (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$apps}" ) > 0 ) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
$linked = [];
|
|
178
|
+
|
|
179
|
+
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $old ) ) === $old ) {
|
|
180
|
+
foreach ( (array) $wpdb->get_results( "SELECT * FROM {$old}", ARRAY_A ) as $row ) {
|
|
181
|
+
$linked[ (int) $row['config_id'] ] = $row;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
foreach ( (array) $wpdb->get_results( "SELECT * FROM {$configs}", ARRAY_A ) as $config ) {
|
|
186
|
+
$app = $linked[ (int) $config['id'] ] ?? [];
|
|
187
|
+
|
|
188
|
+
$wpdb->insert(
|
|
189
|
+
$apps,
|
|
190
|
+
[
|
|
191
|
+
'id' => (int) $config['id'],
|
|
192
|
+
'name' => (string) $config['name'],
|
|
193
|
+
'user_id' => (int) $config['user_id'],
|
|
194
|
+
'environments' => wp_json_encode(
|
|
195
|
+
[
|
|
196
|
+
'prod' => [ 'url' => (string) ( $app['prod_url'] ?? '' ), 'last_pull' => (string) ( $app['last_pull'] ?? '' ) ],
|
|
197
|
+
'staging' => [ 'url' => (string) ( $app['staging_url'] ?? '' ), 'last_pull' => (string) ( $app['last_pull_staging'] ?? '' ) ],
|
|
198
|
+
]
|
|
199
|
+
),
|
|
200
|
+
'contents' => (string) ( $config['contents'] ?: '{}' ),
|
|
201
|
+
'links' => wp_json_encode(
|
|
202
|
+
[
|
|
203
|
+
'jurisdiction' => (int) $config['jurisdiction'],
|
|
204
|
+
'faq_template_id' => (int) ( $config['faq_template_id'] ?? 0 ),
|
|
205
|
+
]
|
|
206
|
+
),
|
|
207
|
+
'languages' => (string) ( $config['languages'] ?: '[]' ),
|
|
208
|
+
'metadata' => (string) ( $config['metadata'] ?: '{}' ),
|
|
209
|
+
'created_at' => (string) $config['created_at'],
|
|
210
|
+
]
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private static function move_options(): void {
|
|
216
|
+
$moves = [
|
|
217
|
+
'service_global_whitelisted_ips' => Modules\Apps\Apps::ALLOWED_IPS,
|
|
218
|
+
];
|
|
219
|
+
|
|
220
|
+
foreach ( $moves as $old => $new ) {
|
|
221
|
+
$value = get_option( $old, null );
|
|
222
|
+
|
|
223
|
+
if ( null !== $value && null === get_option( $new, null ) ) {
|
|
224
|
+
update_option( $new, $value, false );
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
global $wpdb;
|
|
229
|
+
|
|
230
|
+
foreach ( (array) $wpdb->get_col( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'vibe\\_%'" ) as $old ) {
|
|
231
|
+
$new = 'platform_' . substr( $old, 5 );
|
|
232
|
+
|
|
233
|
+
if ( null === get_option( $new, null ) ) {
|
|
234
|
+
update_option( $new, get_option( $old ), false );
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
delete_option( $old );
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
$engine = get_option( App\Engine::OPTION );
|
|
241
|
+
|
|
242
|
+
if ( is_array( $engine ) && 'vibe/{slug}' === ( $engine['branch'] ?? '' ) ) {
|
|
243
|
+
$engine['branch'] = 'platform/{slug}';
|
|
244
|
+
update_option( App\Engine::OPTION, $engine, false );
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
$storage = get_option( 'offload_r2_settings' );
|
|
248
|
+
|
|
249
|
+
if ( is_array( $storage ) && ! get_option( Storage\Settings::OPTION ) ) {
|
|
250
|
+
update_option(
|
|
251
|
+
Storage\Settings::OPTION,
|
|
252
|
+
[
|
|
253
|
+
'provider' => 'r2',
|
|
254
|
+
'account_id' => (string) ( $storage['account_id'] ?? '' ),
|
|
255
|
+
'endpoint' => '',
|
|
256
|
+
'region' => 'auto',
|
|
257
|
+
'bucket' => (string) ( $storage['bucket'] ?? '' ),
|
|
258
|
+
'access_key' => (string) ( $storage['access_key'] ?? '' ),
|
|
259
|
+
'secret_key' => (string) ( $storage['secret_key'] ?? '' ),
|
|
260
|
+
'public_url' => (string) ( $storage['public_url'] ?? '' ),
|
|
261
|
+
],
|
|
262
|
+
false
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private static function move_meta(): void {
|
|
268
|
+
global $wpdb;
|
|
269
|
+
|
|
270
|
+
$wpdb->update( $wpdb->postmeta, [ 'meta_key' => Storage\Offload::URL_META ], [ 'meta_key' => '_r2_url' ] );
|
|
271
|
+
$wpdb->query( "UPDATE {$wpdb->usermeta} SET meta_key = CONCAT('platform_', SUBSTRING(meta_key, 6)) WHERE meta_key LIKE 'vibe\\_%'" );
|
|
272
|
+
}
|
|
273
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Read-only overview of the loaded platform.json.
|
|
4
|
+
*
|
|
5
|
+
* @package Platform
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
namespace Platform;
|
|
9
|
+
|
|
10
|
+
defined( 'ABSPATH' ) || exit;
|
|
11
|
+
|
|
12
|
+
final class Status {
|
|
13
|
+
|
|
14
|
+
public static function register(): void {
|
|
15
|
+
add_action( 'admin_menu', [ __CLASS__, 'menu' ] );
|
|
16
|
+
add_action( 'admin_notices', [ __CLASS__, 'notice' ] );
|
|
17
|
+
add_action( 'admin_post_platform_setup', [ __CLASS__, 'run_setup' ] );
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static function menu(): void {
|
|
21
|
+
add_management_page( 'Platform', 'Platform', 'manage_options', 'platform', [ __CLASS__, 'render' ] );
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public static function notice(): void {
|
|
25
|
+
if ( Config::errors() && current_user_can( 'manage_options' ) ) {
|
|
26
|
+
printf(
|
|
27
|
+
'<div class="notice notice-error"><p><strong>platform.json:</strong> %s <a href="%s">Details</a></p></div>',
|
|
28
|
+
esc_html( Config::errors()[0] ),
|
|
29
|
+
esc_url( admin_url( 'tools.php?page=platform' ) )
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public static function render(): void {
|
|
35
|
+
$types = Config::types();
|
|
36
|
+
?>
|
|
37
|
+
<div class="wrap">
|
|
38
|
+
<h1>Platform</h1>
|
|
39
|
+
<?php if ( isset( $_GET['setup'] ) ) : ?>
|
|
40
|
+
<div class="notice notice-success"><p>Setup finished.</p></div>
|
|
41
|
+
<?php endif; ?>
|
|
42
|
+
<table class="widefat striped" style="max-width:900px">
|
|
43
|
+
<tbody>
|
|
44
|
+
<tr><th>platform.json</th><td><code><?php echo esc_html( Config::file() ?: 'not found' ); ?></code></td></tr>
|
|
45
|
+
<tr><th>Languages</th><td><?php echo esc_html( implode( ', ', Languages::all() ) ); ?> (default: <?php echo esc_html( Languages::default() ); ?>)</td></tr>
|
|
46
|
+
<tr><th>Storage</th><td><?php echo Storage\Storage::enabled() ? 'Connected' : 'Not set up'; ?> · <a href="<?php echo esc_url( admin_url( 'options-general.php?page=' . Storage\Settings::PAGE ) ); ?>">Settings</a></td></tr>
|
|
47
|
+
<tr><th>Modules</th><td><?php echo esc_html( implode( ', ', array_keys( array_filter( (array) Config::get( 'modules', [] ) ) ) ) ?: 'none' ); ?></td></tr>
|
|
48
|
+
<tr><th>Schema</th><td><?php echo (int) get_option( Setup::OPTION ); ?></td></tr>
|
|
49
|
+
</tbody>
|
|
50
|
+
</table>
|
|
51
|
+
|
|
52
|
+
<?php if ( Config::errors() ) : ?>
|
|
53
|
+
<h2>Problems</h2>
|
|
54
|
+
<ul class="ul-disc">
|
|
55
|
+
<?php foreach ( Config::errors() as $error ) : ?>
|
|
56
|
+
<li><?php echo esc_html( $error ); ?></li>
|
|
57
|
+
<?php endforeach; ?>
|
|
58
|
+
</ul>
|
|
59
|
+
<?php endif; ?>
|
|
60
|
+
|
|
61
|
+
<h2>Content types</h2>
|
|
62
|
+
<table class="widefat striped" style="max-width:900px">
|
|
63
|
+
<thead><tr><th>Key</th><th>Label</th><th>Parent</th><th>Fields</th><th>Taxonomies</th><th>Items</th></tr></thead>
|
|
64
|
+
<tbody>
|
|
65
|
+
<?php foreach ( $types as $type ) : ?>
|
|
66
|
+
<tr>
|
|
67
|
+
<td><code><?php echo esc_html( $type['key'] ); ?></code></td>
|
|
68
|
+
<td><?php echo esc_html( $type['label'] ); ?></td>
|
|
69
|
+
<td><?php echo esc_html( $type['parent'] ?: '—' ); ?></td>
|
|
70
|
+
<td><?php echo count( $type['fields'] ); ?></td>
|
|
71
|
+
<td><?php echo esc_html( implode( ', ', Config::taxonomies_for( $type['key'] ) ) ?: '—' ); ?></td>
|
|
72
|
+
<td><?php echo (int) array_sum( (array) wp_count_posts( $type['key'] ) ); ?></td>
|
|
73
|
+
</tr>
|
|
74
|
+
<?php endforeach; ?>
|
|
75
|
+
</tbody>
|
|
76
|
+
</table>
|
|
77
|
+
|
|
78
|
+
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin-top:20px">
|
|
79
|
+
<input type="hidden" name="action" value="platform_setup" />
|
|
80
|
+
<?php wp_nonce_field( 'platform_setup' ); ?>
|
|
81
|
+
<?php submit_button( 'Run setup again', 'secondary', 'submit', false ); ?>
|
|
82
|
+
<p class="description">Creates missing tables and roles and copies data from previous plugin tables into empty new ones.</p>
|
|
83
|
+
</form>
|
|
84
|
+
</div>
|
|
85
|
+
<?php
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public static function run_setup(): void {
|
|
89
|
+
if ( ! current_user_can( 'manage_options' ) ) {
|
|
90
|
+
wp_die( 'Not allowed.', 403 );
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
check_admin_referer( 'platform_setup' );
|
|
94
|
+
Setup::run();
|
|
95
|
+
|
|
96
|
+
wp_safe_redirect( admin_url( 'tools.php?page=platform&setup=1' ) );
|
|
97
|
+
exit;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Minimal S3-compatible client (AWS Signature V4), path-style addressing.
|
|
4
|
+
*
|
|
5
|
+
* @package Platform
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
namespace Platform\Storage;
|
|
9
|
+
|
|
10
|
+
defined( 'ABSPATH' ) || exit;
|
|
11
|
+
|
|
12
|
+
final class Client {
|
|
13
|
+
|
|
14
|
+
private string $endpoint;
|
|
15
|
+
private string $host;
|
|
16
|
+
private string $region;
|
|
17
|
+
private string $bucket;
|
|
18
|
+
private string $access_key;
|
|
19
|
+
private string $secret_key;
|
|
20
|
+
private string $public_url;
|
|
21
|
+
|
|
22
|
+
public function __construct( array $settings ) {
|
|
23
|
+
$this->endpoint = untrailingslashit( (string) $settings['endpoint'] );
|
|
24
|
+
$this->host = (string) wp_parse_url( $this->endpoint, PHP_URL_HOST );
|
|
25
|
+
$this->region = (string) ( $settings['region'] ?: 'auto' );
|
|
26
|
+
$this->bucket = (string) $settings['bucket'];
|
|
27
|
+
$this->access_key = (string) $settings['access_key'];
|
|
28
|
+
$this->secret_key = (string) $settings['secret_key'];
|
|
29
|
+
$this->public_url = untrailingslashit( (string) $settings['public_url'] );
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public function url( string $key ): string {
|
|
33
|
+
return $this->public_url . '/' . self::encode_key( $key );
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public function key_from_url( string $url ): string {
|
|
37
|
+
$base = $this->public_url . '/';
|
|
38
|
+
|
|
39
|
+
return 0 === strpos( $url, $base ) ? rawurldecode( substr( $url, strlen( $base ) ) ) : '';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public function put_file( string $key, string $path, string $mime = '' ): bool {
|
|
43
|
+
$body = is_readable( $path ) ? file_get_contents( $path ) : false;
|
|
44
|
+
|
|
45
|
+
if ( false === $body ) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return $this->request( 'PUT', $key, $body, $mime ?: self::mime( $path ) );
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public function delete( string $key ): bool {
|
|
53
|
+
return $this->request( 'DELETE', $key );
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public function exists( string $key ): bool {
|
|
57
|
+
return $this->request( 'HEAD', $key );
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public function presign_put( string $key, int $expires = 3600 ): string {
|
|
61
|
+
$time = time();
|
|
62
|
+
$date = gmdate( 'Ymd', $time );
|
|
63
|
+
$stamp = gmdate( 'Ymd\THis\Z', $time );
|
|
64
|
+
$scope = "{$date}/{$this->region}/s3/aws4_request";
|
|
65
|
+
$path = $this->path( $key );
|
|
66
|
+
|
|
67
|
+
$query = [
|
|
68
|
+
'X-Amz-Algorithm' => 'AWS4-HMAC-SHA256',
|
|
69
|
+
'X-Amz-Credential' => "{$this->access_key}/{$scope}",
|
|
70
|
+
'X-Amz-Date' => $stamp,
|
|
71
|
+
'X-Amz-Expires' => (string) $expires,
|
|
72
|
+
'X-Amz-SignedHeaders' => 'host',
|
|
73
|
+
];
|
|
74
|
+
ksort( $query );
|
|
75
|
+
|
|
76
|
+
$canonical_query = self::query_string( $query );
|
|
77
|
+
$canonical = implode( "\n", [ 'PUT', $path, $canonical_query, "host:{$this->host}\n", 'host', 'UNSIGNED-PAYLOAD' ] );
|
|
78
|
+
$signature = $this->sign( $stamp, $scope, $canonical, $date );
|
|
79
|
+
|
|
80
|
+
return "{$this->endpoint}{$path}?{$canonical_query}&X-Amz-Signature={$signature}";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private function request( string $method, string $key, string $body = '', string $mime = '' ): bool {
|
|
84
|
+
$time = time();
|
|
85
|
+
$date = gmdate( 'Ymd', $time );
|
|
86
|
+
$stamp = gmdate( 'Ymd\THis\Z', $time );
|
|
87
|
+
$scope = "{$date}/{$this->region}/s3/aws4_request";
|
|
88
|
+
$path = $this->path( $key );
|
|
89
|
+
$payload = hash( 'sha256', $body );
|
|
90
|
+
|
|
91
|
+
$headers = [
|
|
92
|
+
'host' => $this->host,
|
|
93
|
+
'x-amz-content-sha256' => $payload,
|
|
94
|
+
'x-amz-date' => $stamp,
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
if ( 'PUT' === $method && '' !== $mime ) {
|
|
98
|
+
$headers['content-type'] = $mime;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
ksort( $headers );
|
|
102
|
+
|
|
103
|
+
$canonical_headers = '';
|
|
104
|
+
foreach ( $headers as $name => $value ) {
|
|
105
|
+
$canonical_headers .= $name . ':' . trim( $value ) . "\n";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
$signed = implode( ';', array_keys( $headers ) );
|
|
109
|
+
$canonical = implode( "\n", [ $method, $path, '', $canonical_headers, $signed, $payload ] );
|
|
110
|
+
$signature = $this->sign( $stamp, $scope, $canonical, $date );
|
|
111
|
+
|
|
112
|
+
unset( $headers['host'] );
|
|
113
|
+
$headers['Authorization'] = "AWS4-HMAC-SHA256 Credential={$this->access_key}/{$scope}, SignedHeaders={$signed}, Signature={$signature}";
|
|
114
|
+
|
|
115
|
+
$response = wp_remote_request(
|
|
116
|
+
$this->endpoint . $path,
|
|
117
|
+
[
|
|
118
|
+
'method' => $method,
|
|
119
|
+
'headers' => $headers,
|
|
120
|
+
'body' => $body,
|
|
121
|
+
'timeout' => 300,
|
|
122
|
+
]
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
if ( is_wp_error( $response ) ) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
$code = (int) wp_remote_retrieve_response_code( $response );
|
|
130
|
+
|
|
131
|
+
return $code >= 200 && $code < 300;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private function sign( string $stamp, string $scope, string $canonical, string $date ): string {
|
|
135
|
+
$string_to_sign = implode( "\n", [ 'AWS4-HMAC-SHA256', $stamp, $scope, hash( 'sha256', $canonical ) ] );
|
|
136
|
+
|
|
137
|
+
$key = hash_hmac( 'sha256', $date, 'AWS4' . $this->secret_key, true );
|
|
138
|
+
$key = hash_hmac( 'sha256', $this->region, $key, true );
|
|
139
|
+
$key = hash_hmac( 'sha256', 's3', $key, true );
|
|
140
|
+
$key = hash_hmac( 'sha256', 'aws4_request', $key, true );
|
|
141
|
+
|
|
142
|
+
return hash_hmac( 'sha256', $string_to_sign, $key );
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private function path( string $key ): string {
|
|
146
|
+
return '/' . rawurlencode( $this->bucket ) . '/' . self::encode_key( $key );
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private static function encode_key( string $key ): string {
|
|
150
|
+
return implode( '/', array_map( 'rawurlencode', explode( '/', ltrim( $key, '/' ) ) ) );
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private static function query_string( array $query ): string {
|
|
154
|
+
$pairs = [];
|
|
155
|
+
|
|
156
|
+
foreach ( $query as $name => $value ) {
|
|
157
|
+
$pairs[] = rawurlencode( $name ) . '=' . rawurlencode( $value );
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return implode( '&', $pairs );
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private static function mime( string $path ): string {
|
|
164
|
+
$type = wp_check_filetype( $path );
|
|
165
|
+
|
|
166
|
+
return $type['type'] ?: 'application/octet-stream';
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Moves media library files into the bucket and serves them from there.
|
|
4
|
+
*
|
|
5
|
+
* @package Platform
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
namespace Platform\Storage;
|
|
9
|
+
|
|
10
|
+
defined( 'ABSPATH' ) || exit;
|
|
11
|
+
|
|
12
|
+
final class Offload {
|
|
13
|
+
|
|
14
|
+
const URL_META = '_storage_url';
|
|
15
|
+
const PREFIX = 'uploads/';
|
|
16
|
+
|
|
17
|
+
public static function register(): void {
|
|
18
|
+
add_filter( 'wp_generate_attachment_metadata', [ __CLASS__, 'upload' ], 999, 2 );
|
|
19
|
+
add_filter( 'wp_get_attachment_url', [ __CLASS__, 'attachment_url' ], 99, 2 );
|
|
20
|
+
add_filter( 'wp_get_attachment_image_src', [ __CLASS__, 'image_src' ], 99, 2 );
|
|
21
|
+
add_filter( 'the_content', [ __CLASS__, 'content_urls' ], 99 );
|
|
22
|
+
add_action( 'delete_attachment', [ __CLASS__, 'delete' ] );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public static function upload( $metadata, int $attachment_id ) {
|
|
26
|
+
$client = Storage::client();
|
|
27
|
+
$file = (string) get_post_meta( $attachment_id, '_wp_attached_file', true );
|
|
28
|
+
|
|
29
|
+
if ( ! $client || '' === $file ) {
|
|
30
|
+
return $metadata;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
$base = trailingslashit( wp_get_upload_dir()['basedir'] );
|
|
34
|
+
$dir = dirname( $file );
|
|
35
|
+
$files = [ $file ];
|
|
36
|
+
|
|
37
|
+
foreach ( (array) ( $metadata['sizes'] ?? [] ) as $size ) {
|
|
38
|
+
if ( ! empty( $size['file'] ) ) {
|
|
39
|
+
$files[] = ( '.' === $dir ? '' : $dir . '/' ) . $size['file'];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if ( ! empty( $metadata['original_image'] ) ) {
|
|
44
|
+
$files[] = ( '.' === $dir ? '' : $dir . '/' ) . $metadata['original_image'];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
$uploaded = [];
|
|
48
|
+
|
|
49
|
+
foreach ( array_unique( $files ) as $relative ) {
|
|
50
|
+
if ( ! $client->put_file( self::PREFIX . $relative, $base . $relative ) ) {
|
|
51
|
+
return $metadata;
|
|
52
|
+
}
|
|
53
|
+
$uploaded[] = $base . $relative;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
update_post_meta( $attachment_id, self::URL_META, $client->url( self::PREFIX . $file ) );
|
|
57
|
+
|
|
58
|
+
foreach ( $uploaded as $path ) {
|
|
59
|
+
wp_delete_file( $path );
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return $metadata;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public static function attachment_url( string $url, int $attachment_id ): string {
|
|
66
|
+
$stored = (string) get_post_meta( $attachment_id, self::URL_META, true );
|
|
67
|
+
|
|
68
|
+
if ( '' !== $stored ) {
|
|
69
|
+
return $stored;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
$file = (string) get_post_meta( $attachment_id, '_wp_attached_file', true );
|
|
73
|
+
$client = Storage::client();
|
|
74
|
+
|
|
75
|
+
return ( $client && '' !== $file && ! path_is_absolute( $file ) ) ? $client->url( self::PREFIX . $file ) : $url;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public static function image_src( $image, int $attachment_id ) {
|
|
79
|
+
$stored = (string) get_post_meta( $attachment_id, self::URL_META, true );
|
|
80
|
+
|
|
81
|
+
if ( is_array( $image ) && '' !== $stored ) {
|
|
82
|
+
$image[0] = $stored;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return $image;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public static function content_urls( string $content ): string {
|
|
89
|
+
$client = Storage::client();
|
|
90
|
+
|
|
91
|
+
if ( ! $client ) {
|
|
92
|
+
return $content;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return str_replace( trailingslashit( wp_get_upload_dir()['baseurl'] ), $client->url( self::PREFIX ), $content );
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public static function delete( int $attachment_id ): void {
|
|
99
|
+
$client = Storage::client();
|
|
100
|
+
$file = (string) get_post_meta( $attachment_id, '_wp_attached_file', true );
|
|
101
|
+
$metadata = wp_get_attachment_metadata( $attachment_id );
|
|
102
|
+
|
|
103
|
+
if ( ! $client || '' === $file ) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
$dir = dirname( $file );
|
|
108
|
+
$keys = [ self::PREFIX . $file ];
|
|
109
|
+
|
|
110
|
+
foreach ( (array) ( $metadata['sizes'] ?? [] ) as $size ) {
|
|
111
|
+
if ( ! empty( $size['file'] ) ) {
|
|
112
|
+
$keys[] = self::PREFIX . ( '.' === $dir ? '' : $dir . '/' ) . $size['file'];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
foreach ( array_unique( $keys ) as $key ) {
|
|
117
|
+
$client->delete( $key );
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|