@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,249 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Reads and normalizes platform.json.
|
|
4
|
+
*
|
|
5
|
+
* @package Platform
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
namespace Platform;
|
|
9
|
+
|
|
10
|
+
defined( 'ABSPATH' ) || exit;
|
|
11
|
+
|
|
12
|
+
final class Config {
|
|
13
|
+
|
|
14
|
+
private static array $data = [];
|
|
15
|
+
private static array $types = [];
|
|
16
|
+
private static array $taxonomies = [];
|
|
17
|
+
private static array $errors = [];
|
|
18
|
+
private static string $file = '';
|
|
19
|
+
|
|
20
|
+
public static function load(): void {
|
|
21
|
+
self::$file = self::locate();
|
|
22
|
+
|
|
23
|
+
if ( '' === self::$file ) {
|
|
24
|
+
self::$errors[] = 'platform.json was not found.';
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
$data = json_decode( (string) file_get_contents( self::$file ), true );
|
|
29
|
+
|
|
30
|
+
if ( ! is_array( $data ) ) {
|
|
31
|
+
self::$errors[] = 'platform.json is not valid JSON: ' . json_last_error_msg();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
self::$data = $data;
|
|
36
|
+
|
|
37
|
+
foreach ( (array) ( $data['content']['taxonomies'] ?? [] ) as $taxonomy ) {
|
|
38
|
+
self::add_taxonomy( (array) $taxonomy );
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
foreach ( (array) ( $data['content']['types'] ?? [] ) as $type ) {
|
|
42
|
+
self::add_type( (array) $type, '' );
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public static function file(): string {
|
|
47
|
+
return self::$file;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public static function errors(): array {
|
|
51
|
+
return self::$errors;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public static function get( string $path, $fallback = null ) {
|
|
55
|
+
$value = self::$data;
|
|
56
|
+
|
|
57
|
+
foreach ( explode( '.', $path ) as $segment ) {
|
|
58
|
+
if ( ! is_array( $value ) || ! array_key_exists( $segment, $value ) ) {
|
|
59
|
+
return $fallback;
|
|
60
|
+
}
|
|
61
|
+
$value = $value[ $segment ];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return $value;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public static function module( string $name ): array {
|
|
68
|
+
$module = self::get( 'modules.' . $name );
|
|
69
|
+
|
|
70
|
+
if ( true === $module ) {
|
|
71
|
+
return [ 'enabled' => true ];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return is_array( $module ) && false !== ( $module['enabled'] ?? true ) ? $module + [ 'enabled' => true ] : [];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public static function types(): array {
|
|
78
|
+
return self::$types;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public static function type( string $key ): array {
|
|
82
|
+
return self::$types[ $key ] ?? [];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public static function taxonomies(): array {
|
|
86
|
+
return self::$taxonomies;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public static function taxonomy( string $key ): array {
|
|
90
|
+
return self::$taxonomies[ $key ] ?? [];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public static function root( string $key ): array {
|
|
94
|
+
$type = self::type( $key );
|
|
95
|
+
|
|
96
|
+
while ( $type && '' !== $type['parent'] ) {
|
|
97
|
+
$type = self::type( $type['parent'] );
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return $type;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public static function taxonomies_for( string $type ): array {
|
|
104
|
+
return array_keys(
|
|
105
|
+
array_filter(
|
|
106
|
+
self::$taxonomies,
|
|
107
|
+
static fn( array $taxonomy ): bool => in_array( $type, $taxonomy['types'], true )
|
|
108
|
+
)
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private static function locate(): string {
|
|
113
|
+
$candidates = [];
|
|
114
|
+
|
|
115
|
+
if ( defined( 'PLATFORM_CONFIG' ) ) {
|
|
116
|
+
$candidates[] = (string) PLATFORM_CONFIG;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
$candidates[] = dirname( ABSPATH ) . '/platform.json';
|
|
120
|
+
$candidates[] = ABSPATH . 'platform.json';
|
|
121
|
+
$candidates[] = get_template_directory() . '/platform.json';
|
|
122
|
+
|
|
123
|
+
foreach ( $candidates as $candidate ) {
|
|
124
|
+
if ( is_readable( $candidate ) ) {
|
|
125
|
+
return $candidate;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return '';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private static function add_type( array $type, string $parent ): void {
|
|
133
|
+
$key = sanitize_key( $type['key'] ?? '' );
|
|
134
|
+
|
|
135
|
+
if ( '' === $key || strlen( $key ) > 20 ) {
|
|
136
|
+
self::$errors[] = sprintf( 'Content type "%s" needs a key of 1-20 characters.', $type['key'] ?? '' );
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if ( isset( self::$types[ $key ] ) ) {
|
|
141
|
+
self::$errors[] = sprintf( 'Content type "%s" is defined twice.', $key );
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
$label = (string) ( $type['label'] ?? ucfirst( $key ) );
|
|
146
|
+
$children = isset( $type['children'] ) && is_array( $type['children'] ) ? $type['children'] : [];
|
|
147
|
+
|
|
148
|
+
self::$types[ $key ] = [
|
|
149
|
+
'key' => $key,
|
|
150
|
+
'label' => $label,
|
|
151
|
+
'singular' => (string) ( $type['singular'] ?? $label ),
|
|
152
|
+
'kind' => (string) ( $type['kind'] ?? 'content' ),
|
|
153
|
+
'editor' => sanitize_key( (string) ( $type['editor'] ?? '' ) ),
|
|
154
|
+
'icon' => (string) ( $type['icon'] ?? 'dashicons-admin-post' ),
|
|
155
|
+
'position' => isset( $type['position'] ) ? (float) $type['position'] : null,
|
|
156
|
+
'supports' => array_values( (array) ( $type['supports'] ?? [ 'title', 'editor' ] ) ),
|
|
157
|
+
'boxes' => self::boxes( (array) ( $type['boxes'] ?? [] ) ),
|
|
158
|
+
'fields' => self::fields( (array) ( $type['fields'] ?? [] ) ),
|
|
159
|
+
'languages' => array_values( (array) ( $type['languages'] ?? [] ) ),
|
|
160
|
+
'folder' => sanitize_title( (string) ( $type['folder'] ?? $key ) ),
|
|
161
|
+
'translate' => ! empty( $type['translate'] ),
|
|
162
|
+
'api' => self::api( $type['api'] ?? false ),
|
|
163
|
+
'parent' => $parent,
|
|
164
|
+
'children' => $children ? sanitize_key( $children['key'] ?? '' ) : '',
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
if ( $children ) {
|
|
168
|
+
self::add_type( $children, $key );
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private static function add_taxonomy( array $taxonomy ): void {
|
|
173
|
+
$key = sanitize_key( $taxonomy['key'] ?? '' );
|
|
174
|
+
|
|
175
|
+
if ( '' === $key || strlen( $key ) > 32 ) {
|
|
176
|
+
self::$errors[] = sprintf( 'Taxonomy "%s" needs a key of 1-32 characters.', $taxonomy['key'] ?? '' );
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
$label = (string) ( $taxonomy['label'] ?? ucfirst( $key ) );
|
|
181
|
+
|
|
182
|
+
self::$taxonomies[ $key ] = [
|
|
183
|
+
'key' => $key,
|
|
184
|
+
'label' => $label,
|
|
185
|
+
'singular' => (string) ( $taxonomy['singular'] ?? $label ),
|
|
186
|
+
'hierarchical' => ! empty( $taxonomy['hierarchical'] ),
|
|
187
|
+
'types' => array_values( array_map( 'sanitize_key', (array) ( $taxonomy['types'] ?? [] ) ) ),
|
|
188
|
+
'relation' => 'one' === ( $taxonomy['relation'] ?? '' ) ? 'one' : 'many',
|
|
189
|
+
'fields' => self::fields( (array) ( $taxonomy['fields'] ?? [] ) ),
|
|
190
|
+
'translate' => ! empty( $taxonomy['translate'] ),
|
|
191
|
+
];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private static function boxes( array $boxes ): array {
|
|
195
|
+
$out = [];
|
|
196
|
+
|
|
197
|
+
foreach ( $boxes as $box ) {
|
|
198
|
+
$key = sanitize_key( $box['key'] ?? '' );
|
|
199
|
+
|
|
200
|
+
if ( '' !== $key ) {
|
|
201
|
+
$out[ $key ] = [
|
|
202
|
+
'key' => $key,
|
|
203
|
+
'label' => (string) ( $box['label'] ?? ucfirst( $key ) ),
|
|
204
|
+
'context' => in_array( $box['context'] ?? '', [ 'normal', 'side', 'advanced' ], true ) ? $box['context'] : 'normal',
|
|
205
|
+
];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return $out;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private static function fields( array $fields ): array {
|
|
213
|
+
$out = [];
|
|
214
|
+
|
|
215
|
+
foreach ( $fields as $field ) {
|
|
216
|
+
if ( ! is_array( $field ) || empty( $field['key'] ) || empty( $field['type'] ) ) {
|
|
217
|
+
self::$errors[] = 'Every field needs a key and a type.';
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
$field['key'] = (string) $field['key'];
|
|
222
|
+
$field['label'] = (string) ( $field['label'] ?? '' );
|
|
223
|
+
|
|
224
|
+
if ( 'ebook' === $field['type'] && empty( $field['languages'] ) ) {
|
|
225
|
+
$field['languages'] = 'map';
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if ( isset( $field['fields'] ) ) {
|
|
229
|
+
$field['fields'] = self::fields( (array) $field['fields'] );
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
$out[] = $field;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return $out;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private static function api( $api ): array {
|
|
239
|
+
if ( true === $api ) {
|
|
240
|
+
return [ 'format' => 'default' ];
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if ( ! is_array( $api ) ) {
|
|
244
|
+
return [];
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return $api + [ 'format' => 'default' ];
|
|
248
|
+
}
|
|
249
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Edit screens: field boxes for content types and fields on taxonomy terms.
|
|
4
|
+
*
|
|
5
|
+
* @package Platform
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
namespace Platform\Content;
|
|
9
|
+
|
|
10
|
+
use Platform\Config;
|
|
11
|
+
use Platform\Languages;
|
|
12
|
+
use Platform\Platform;
|
|
13
|
+
use Platform\Storage\Storage;
|
|
14
|
+
|
|
15
|
+
defined( 'ABSPATH' ) || exit;
|
|
16
|
+
|
|
17
|
+
final class Editor {
|
|
18
|
+
|
|
19
|
+
const NONCE = 'platform_fields';
|
|
20
|
+
|
|
21
|
+
public static function register(): void {
|
|
22
|
+
add_action( 'add_meta_boxes', [ __CLASS__, 'add_boxes' ], 10, 2 );
|
|
23
|
+
add_action( 'edit_form_after_title', [ __CLASS__, 'nonce' ] );
|
|
24
|
+
add_action( 'save_post', [ __CLASS__, 'save' ], 10, 2 );
|
|
25
|
+
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'assets' ] );
|
|
26
|
+
add_action( 'init', [ __CLASS__, 'term_hooks' ], 20 );
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public static function add_boxes( string $post_type, $post ): void {
|
|
30
|
+
$type = Config::type( $post_type );
|
|
31
|
+
|
|
32
|
+
if ( ! $type || '' !== $type['parent'] ) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if ( 'document' === $type['kind'] && Config::get( 'content.placeholders' ) ) {
|
|
37
|
+
add_meta_box( 'platform-placeholders', 'Placeholders', [ __CLASS__, 'render_placeholders' ], $post_type, 'side' );
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if ( '' !== $type['editor'] ) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
$boxes = $type['boxes'] + [ 'details' => [ 'key' => 'details', 'label' => 'Details', 'context' => 'normal' ] ];
|
|
45
|
+
|
|
46
|
+
foreach ( $boxes as $box ) {
|
|
47
|
+
$fields = self::fields_in_box( $type, $box['key'] );
|
|
48
|
+
|
|
49
|
+
if ( $fields ) {
|
|
50
|
+
add_meta_box( 'platform-' . $box['key'], $box['label'], [ __CLASS__, 'render_box' ], $post_type, $box['context'], 'side' === $box['context'] ? 'default' : 'high', [ 'fields' => $fields ] );
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if ( '' !== $type['children'] ) {
|
|
55
|
+
$child = Config::type( $type['children'] );
|
|
56
|
+
add_meta_box( 'platform-structure', $child['label'], [ Structure::class, 'render' ], $post_type, 'normal', 'high' );
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public static function render_placeholders(): void {
|
|
61
|
+
echo '<p class="description">Click to copy. Each app sees its own value.</p><p>';
|
|
62
|
+
|
|
63
|
+
foreach ( (array) Config::get( 'content.placeholders', [] ) as $key ) {
|
|
64
|
+
printf( '<button type="button" class="button button-small platform-placeholder" data-copy="%1$s">%1$s</button> ', esc_attr( '[' . sanitize_key( $key ) . ']' ) );
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
echo '</p>';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public static function nonce( \WP_Post $post ): void {
|
|
71
|
+
$type = Config::type( $post->post_type );
|
|
72
|
+
|
|
73
|
+
if ( $type && '' === $type['editor'] ) {
|
|
74
|
+
wp_nonce_field( self::NONCE, self::NONCE . '_nonce' );
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public static function render_box( \WP_Post $post, array $box ): void {
|
|
79
|
+
Fields::use_languages( Languages::for_type( $post->post_type ) );
|
|
80
|
+
Fields::use_folder( Config::root( $post->post_type )['folder'] ?? '' );
|
|
81
|
+
|
|
82
|
+
$fields = $box['args']['fields'];
|
|
83
|
+
|
|
84
|
+
echo '<div class="platform-fields">';
|
|
85
|
+
Fields::render( $fields, Fields::values( 'post', $post->ID, $fields ), 'fields' );
|
|
86
|
+
echo '</div>';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public static function save( int $post_id, \WP_Post $post ): void {
|
|
90
|
+
$type = Config::type( $post->post_type );
|
|
91
|
+
|
|
92
|
+
if ( ! $type || '' !== $type['editor'] || wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if ( ! isset( $_POST[ self::NONCE . '_nonce' ] ) || ! wp_verify_nonce( sanitize_key( $_POST[ self::NONCE . '_nonce' ] ), self::NONCE ) ) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if ( ! current_user_can( 'edit_post', $post_id ) ) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
Fields::use_languages( Languages::for_type( $post->post_type ) );
|
|
105
|
+
Fields::save( 'post', $post_id, $type['fields'], Fields::input() );
|
|
106
|
+
|
|
107
|
+
foreach ( (array) wp_unslash( $_POST['platform_terms'] ?? [] ) as $taxonomy => $term_id ) {
|
|
108
|
+
$taxonomy = sanitize_key( $taxonomy );
|
|
109
|
+
|
|
110
|
+
if ( 'one' === ( Config::taxonomy( $taxonomy )['relation'] ?? '' ) && is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
|
|
111
|
+
wp_set_object_terms( $post_id, absint( $term_id ) ? [ absint( $term_id ) ] : [], $taxonomy );
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public static function term_select_box( \WP_Post $post, array $box ): void {
|
|
117
|
+
$taxonomy = $box['args']['taxonomy'];
|
|
118
|
+
$current = wp_get_object_terms( $post->ID, $taxonomy, [ 'fields' => 'ids' ] );
|
|
119
|
+
$current = is_wp_error( $current ) ? 0 : (int) ( $current[0] ?? 0 );
|
|
120
|
+
$terms = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => false ] );
|
|
121
|
+
|
|
122
|
+
printf( '<select class="widefat" name="platform_terms[%s]"><option value="0">None</option>', esc_attr( $taxonomy ) );
|
|
123
|
+
|
|
124
|
+
foreach ( is_wp_error( $terms ) ? [] : $terms as $term ) {
|
|
125
|
+
printf( '<option value="%d"%s>%s</option>', (int) $term->term_id, selected( $current, (int) $term->term_id, false ), esc_html( $term->name ) );
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
echo '</select>';
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/* ---------------------------------------------------------------- term fields */
|
|
132
|
+
|
|
133
|
+
public static function term_hooks(): void {
|
|
134
|
+
foreach ( Config::taxonomies() as $taxonomy ) {
|
|
135
|
+
if ( ! $taxonomy['fields'] ) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
add_action( $taxonomy['key'] . '_add_form_fields', [ __CLASS__, 'render_new_term' ] );
|
|
140
|
+
add_action( $taxonomy['key'] . '_edit_form_fields', [ __CLASS__, 'render_edit_term' ], 10, 2 );
|
|
141
|
+
add_action( 'created_' . $taxonomy['key'], [ __CLASS__, 'save_term' ] );
|
|
142
|
+
add_action( 'edited_' . $taxonomy['key'], [ __CLASS__, 'save_term' ] );
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public static function render_new_term( string $taxonomy ): void {
|
|
147
|
+
Fields::use_languages( Languages::all() );
|
|
148
|
+
wp_nonce_field( self::NONCE, self::NONCE . '_nonce' );
|
|
149
|
+
|
|
150
|
+
echo '<div class="form-field platform-fields">';
|
|
151
|
+
Fields::render( Config::taxonomy( $taxonomy )['fields'], [], 'fields' );
|
|
152
|
+
echo '</div>';
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public static function render_edit_term( \WP_Term $term, string $taxonomy ): void {
|
|
156
|
+
$fields = Config::taxonomy( $taxonomy )['fields'];
|
|
157
|
+
|
|
158
|
+
Fields::use_languages( Languages::all() );
|
|
159
|
+
|
|
160
|
+
echo '<tr class="form-field"><th scope="row">Details</th><td>';
|
|
161
|
+
wp_nonce_field( self::NONCE, self::NONCE . '_nonce' );
|
|
162
|
+
echo '<div class="platform-fields">';
|
|
163
|
+
Fields::render( $fields, Fields::values( 'term', $term->term_id, $fields ), 'fields' );
|
|
164
|
+
echo '</div></td></tr>';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public static function save_term( int $term_id ): void {
|
|
168
|
+
$term = get_term( $term_id );
|
|
169
|
+
|
|
170
|
+
if ( ! $term instanceof \WP_Term || ! current_user_can( 'edit_term', $term_id ) ) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if ( ! isset( $_POST[ self::NONCE . '_nonce' ] ) || ! wp_verify_nonce( sanitize_key( $_POST[ self::NONCE . '_nonce' ] ), self::NONCE ) ) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
Fields::use_languages( Languages::all() );
|
|
179
|
+
Fields::save( 'term', $term_id, Config::taxonomy( $term->taxonomy )['fields'] ?? [], Fields::input() );
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/* ---------------------------------------------------------------- assets */
|
|
183
|
+
|
|
184
|
+
public static function assets( string $hook ): void {
|
|
185
|
+
$screen = get_current_screen();
|
|
186
|
+
|
|
187
|
+
if ( ! $screen ) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
$type = Config::type( $screen->post_type );
|
|
192
|
+
$on_type = in_array( $hook, [ 'post.php', 'post-new.php' ], true ) && $type && '' === $type['editor'];
|
|
193
|
+
$on_taxonomy = in_array( $hook, [ 'edit-tags.php', 'term.php' ], true ) && ! empty( Config::taxonomy( $screen->taxonomy )['fields'] );
|
|
194
|
+
|
|
195
|
+
if ( $on_type || $on_taxonomy ) {
|
|
196
|
+
self::enqueue();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
public static function enqueue(): void {
|
|
201
|
+
wp_enqueue_media();
|
|
202
|
+
wp_enqueue_style( 'platform-admin', Platform::url( 'assets/admin.css' ), [], Platform::asset_version( 'assets/admin.css' ) );
|
|
203
|
+
wp_enqueue_script( 'platform-admin', Platform::url( 'assets/admin.js' ), [ 'jquery', 'jquery-ui-sortable' ], Platform::asset_version( 'assets/admin.js' ), true );
|
|
204
|
+
wp_localize_script(
|
|
205
|
+
'platform-admin',
|
|
206
|
+
'platformAdmin',
|
|
207
|
+
[
|
|
208
|
+
'ajax' => admin_url( 'admin-ajax.php' ),
|
|
209
|
+
'nonce' => wp_create_nonce( 'platform_admin' ),
|
|
210
|
+
'storage' => Storage::enabled(),
|
|
211
|
+
]
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private static function fields_in_box( array $type, string $box ): array {
|
|
216
|
+
return array_values(
|
|
217
|
+
array_filter(
|
|
218
|
+
$type['fields'],
|
|
219
|
+
static fn( array $field ): bool => ( $field['box'] ?? 'details' ) === $box
|
|
220
|
+
)
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|