@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,913 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
namespace Platform\Editors\Translator;
|
|
3
|
+
|
|
4
|
+
use Platform\Config;
|
|
5
|
+
use Platform\Languages;
|
|
6
|
+
use Platform\Platform;
|
|
7
|
+
|
|
8
|
+
defined( 'ABSPATH' ) || exit;
|
|
9
|
+
|
|
10
|
+
class Admin {
|
|
11
|
+
private static array $languages = [];
|
|
12
|
+
|
|
13
|
+
public static function register(): void {
|
|
14
|
+
add_action( 'admin_menu', [ __CLASS__, 'register_menu' ] );
|
|
15
|
+
add_action( 'admin_post_ct_save_translation', [ __CLASS__, 'handle_save' ] );
|
|
16
|
+
add_action( 'admin_post_ct_save_settings', [ __CLASS__, 'handle_save_settings' ] );
|
|
17
|
+
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'enqueue_assets' ] );
|
|
18
|
+
|
|
19
|
+
$base = Languages::all();
|
|
20
|
+
self::$languages = apply_filters( 'ct_languages', $base );
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public static function register_menu(): void {
|
|
24
|
+
$module = (array) Config::module( 'translations' );
|
|
25
|
+
$label = (string) ( $module['label'] ?? 'Translator' );
|
|
26
|
+
|
|
27
|
+
add_menu_page(
|
|
28
|
+
$label,
|
|
29
|
+
$label,
|
|
30
|
+
'manage_options',
|
|
31
|
+
'content-translator',
|
|
32
|
+
[ __CLASS__, 'render_page' ],
|
|
33
|
+
(string) ( $module['icon'] ?? 'dashicons-translation' ),
|
|
34
|
+
$module['position'] ?? 79
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public static function enqueue_assets( string $hook ): void {
|
|
39
|
+
if ( $hook !== 'toplevel_page_content-translator' ) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
wp_enqueue_style( 'ct-admin', Platform::url( 'assets/editors/translator.css' ), [], Platform::asset_version( 'assets/editors/translator.css' ) );
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public static function render_page(): void {
|
|
46
|
+
$post_types = Fields::translatable_post_types();
|
|
47
|
+
$taxonomies = Fields::translatable_taxonomies();
|
|
48
|
+
|
|
49
|
+
$active_tab = sanitize_key( $_GET['ct_tab'] ?? '' );
|
|
50
|
+
$is_settings = ( $active_tab === 'ct_settings' );
|
|
51
|
+
|
|
52
|
+
if ( empty( $post_types ) && empty( $taxonomies ) && ! $is_settings ) {
|
|
53
|
+
$is_settings = true;
|
|
54
|
+
$active_tab = 'ct_settings';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
$content_tabs = array_merge( array_keys( $post_types ), array_keys( $taxonomies ) );
|
|
58
|
+
|
|
59
|
+
if ( ! $is_settings && ! in_array( $active_tab, $content_tabs, true ) ) {
|
|
60
|
+
$active_tab = $content_tabs[0] ?? 'ct_settings';
|
|
61
|
+
$is_settings = ( $active_tab === 'ct_settings' );
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
$edit_id = isset( $_GET['ct_edit'] ) ? (int) $_GET['ct_edit'] : 0;
|
|
65
|
+
|
|
66
|
+
$settings_url = add_query_arg( [ 'page' => 'content-translator', 'ct_tab' => 'ct_settings' ], admin_url( 'admin.php' ) );
|
|
67
|
+
|
|
68
|
+
?>
|
|
69
|
+
<div class="wrap ct-wrap">
|
|
70
|
+
<h1 class="ct-heading">Content Translator</h1>
|
|
71
|
+
|
|
72
|
+
<nav class="ct-tabs">
|
|
73
|
+
<?php foreach ( $post_types as $slug => $type ) : ?>
|
|
74
|
+
<a href="<?php echo esc_url( self::tab_url( $slug ) ); ?>"
|
|
75
|
+
class="ct-tab <?php echo $active_tab === $slug ? 'ct-tab--active' : ''; ?>">
|
|
76
|
+
<?php echo esc_html( $type->labels->name ); ?>
|
|
77
|
+
</a>
|
|
78
|
+
<?php endforeach; ?>
|
|
79
|
+
<?php foreach ( $taxonomies as $slug => $tax ) : ?>
|
|
80
|
+
<a href="<?php echo esc_url( self::tab_url( $slug ) ); ?>"
|
|
81
|
+
class="ct-tab ct-tab--tax <?php echo $active_tab === $slug ? 'ct-tab--active' : ''; ?>">
|
|
82
|
+
<?php echo esc_html( self::tax_label( $tax ) ); ?>
|
|
83
|
+
</a>
|
|
84
|
+
<?php endforeach; ?>
|
|
85
|
+
|
|
86
|
+
<a href="<?php echo esc_url( $settings_url ); ?>"
|
|
87
|
+
class="ct-tab ct-tab--settings <?php echo $is_settings ? 'ct-tab--active' : ''; ?>">
|
|
88
|
+
⚙ Settings
|
|
89
|
+
</a>
|
|
90
|
+
</nav>
|
|
91
|
+
|
|
92
|
+
<div class="ct-panel">
|
|
93
|
+
<?php
|
|
94
|
+
if ( $is_settings ) {
|
|
95
|
+
self::render_settings();
|
|
96
|
+
} elseif ( isset( $post_types[ $active_tab ] ) ) {
|
|
97
|
+
if ( $edit_id ) {
|
|
98
|
+
self::render_post_edit( $edit_id, $active_tab );
|
|
99
|
+
} else {
|
|
100
|
+
self::render_post_list( $active_tab, $post_types[ $active_tab ] );
|
|
101
|
+
}
|
|
102
|
+
} elseif ( isset( $taxonomies[ $active_tab ] ) ) {
|
|
103
|
+
if ( $edit_id ) {
|
|
104
|
+
self::render_term_edit( $edit_id, $active_tab );
|
|
105
|
+
} else {
|
|
106
|
+
self::render_term_list( $active_tab, $taxonomies[ $active_tab ] );
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
?>
|
|
110
|
+
</div>
|
|
111
|
+
</div>
|
|
112
|
+
<?php
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private static function render_settings(): void {
|
|
116
|
+
$skip_meta = Fields::get_skip_meta();
|
|
117
|
+
$allow_private = Fields::get_allow_private_meta();
|
|
118
|
+
$default_skip = Fields::$default_skip_meta;
|
|
119
|
+
$default_allow = Fields::$default_allow_private_meta;
|
|
120
|
+
|
|
121
|
+
$saved_notice = ! empty( $_GET['ct_settings_saved'] );
|
|
122
|
+
$reset_notice = ! empty( $_GET['ct_settings_reset'] );
|
|
123
|
+
?>
|
|
124
|
+
<div class="ct-settings-wrap">
|
|
125
|
+
|
|
126
|
+
<div class="ct-settings-intro">
|
|
127
|
+
<h2>Language Settings</h2>
|
|
128
|
+
<p>Configure languages used by the thumbnail manager for per-language thumbnail columns.</p>
|
|
129
|
+
</div>
|
|
130
|
+
|
|
131
|
+
<p class="description" style="margin-bottom:32px;">Languages come from platform.json: <?php echo esc_html( implode( ', ', Languages::all() ) ); ?>.</p>
|
|
132
|
+
|
|
133
|
+
<div class="ct-settings-intro">
|
|
134
|
+
<h2>Meta Key Settings</h2>
|
|
135
|
+
<p>
|
|
136
|
+
Control which <code>_prefixed</code> post/term meta keys are offered for translation.
|
|
137
|
+
One key per line. Changes take effect immediately for all subsequent edits.
|
|
138
|
+
</p>
|
|
139
|
+
</div>
|
|
140
|
+
|
|
141
|
+
<?php if ( $saved_notice ) : ?>
|
|
142
|
+
<div class="notice notice-success is-dismissible"><p>Settings saved.</p></div>
|
|
143
|
+
<?php elseif ( $reset_notice ) : ?>
|
|
144
|
+
<div class="notice notice-info is-dismissible"><p>Settings reset to defaults.</p></div>
|
|
145
|
+
<?php endif; ?>
|
|
146
|
+
|
|
147
|
+
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="ct-settings-form">
|
|
148
|
+
<?php wp_nonce_field( 'ct_save_settings', 'ct_settings_nonce' ); ?>
|
|
149
|
+
<input type="hidden" name="action" value="ct_save_settings">
|
|
150
|
+
|
|
151
|
+
<div class="ct-settings-grid">
|
|
152
|
+
|
|
153
|
+
<div class="ct-settings-card">
|
|
154
|
+
<div class="ct-settings-card-header">
|
|
155
|
+
<h3>Blocked Meta Keys</h3>
|
|
156
|
+
<span class="ct-settings-badge ct-settings-badge--block"><?php echo count( $skip_meta ); ?> keys</span>
|
|
157
|
+
</div>
|
|
158
|
+
<p class="ct-settings-desc">
|
|
159
|
+
These <code>_prefixed</code> keys are <strong>never</strong> offered for translation,
|
|
160
|
+
even if they contain text. Use this for internal plugin flags, IDs, prices, URLs, etc.
|
|
161
|
+
</p>
|
|
162
|
+
<textarea
|
|
163
|
+
name="ct_skip_meta"
|
|
164
|
+
class="ct-settings-textarea"
|
|
165
|
+
rows="16"
|
|
166
|
+
placeholder="One meta key per line, e.g. _my_internal_flag"
|
|
167
|
+
><?php echo esc_textarea( implode( "
|
|
168
|
+
", $skip_meta ) ); ?></textarea>
|
|
169
|
+
<p class="ct-settings-hint">
|
|
170
|
+
<a href="#" class="ct-restore-link" data-target="ct_skip_meta"
|
|
171
|
+
data-default="<?php echo esc_attr( implode( "
|
|
172
|
+
", $default_skip ) ); ?>">
|
|
173
|
+
Restore defaults
|
|
174
|
+
</a>
|
|
175
|
+
</p>
|
|
176
|
+
</div>
|
|
177
|
+
|
|
178
|
+
<div class="ct-settings-card">
|
|
179
|
+
<div class="ct-settings-card-header">
|
|
180
|
+
<h3>Allowed Private Meta Keys</h3>
|
|
181
|
+
<span class="ct-settings-badge ct-settings-badge--allow"><?php echo count( $allow_private ); ?> keys</span>
|
|
182
|
+
</div>
|
|
183
|
+
<p class="ct-settings-desc">
|
|
184
|
+
These <code>_prefixed</code> keys <strong>are</strong> offered for translation
|
|
185
|
+
even though they start with <code>_</code>. Add any plugin key that stores
|
|
186
|
+
real translatable content (titles, descriptions, structured data, etc.).
|
|
187
|
+
</p>
|
|
188
|
+
<textarea
|
|
189
|
+
name="ct_allow_private_meta"
|
|
190
|
+
class="ct-settings-textarea"
|
|
191
|
+
rows="16"
|
|
192
|
+
placeholder="One meta key per line, e.g. _audiobook_chapters"
|
|
193
|
+
><?php echo esc_textarea( implode( "
|
|
194
|
+
", $allow_private ) ); ?></textarea>
|
|
195
|
+
<p class="ct-settings-hint">
|
|
196
|
+
<a href="#" class="ct-restore-link" data-target="ct_allow_private_meta"
|
|
197
|
+
data-default="<?php echo esc_attr( implode( "
|
|
198
|
+
", $default_allow ) ); ?>">
|
|
199
|
+
Restore defaults
|
|
200
|
+
</a>
|
|
201
|
+
</p>
|
|
202
|
+
</div>
|
|
203
|
+
|
|
204
|
+
</div>
|
|
205
|
+
|
|
206
|
+
<div class="ct-form-footer">
|
|
207
|
+
<button type="submit" class="button button-primary">Save Settings</button>
|
|
208
|
+
<button type="submit" name="ct_reset_defaults" value="1" class="button">Reset All to Defaults</button>
|
|
209
|
+
</div>
|
|
210
|
+
</form>
|
|
211
|
+
|
|
212
|
+
</div>
|
|
213
|
+
|
|
214
|
+
<script>
|
|
215
|
+
document.querySelectorAll('.ct-restore-link').forEach(function(link) {
|
|
216
|
+
link.addEventListener('click', function(e) {
|
|
217
|
+
e.preventDefault();
|
|
218
|
+
var target = this.dataset.target;
|
|
219
|
+
var def = this.dataset.default;
|
|
220
|
+
var ta = document.querySelector('[name="' + target + '"]');
|
|
221
|
+
if (ta && confirm('Restore this list to defaults?')) {
|
|
222
|
+
ta.value = def;
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
</script>
|
|
227
|
+
<?php
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
public static function handle_save_settings(): void {
|
|
231
|
+
if ( ! check_admin_referer( 'ct_save_settings', 'ct_settings_nonce' ) ) {
|
|
232
|
+
wp_die( 'Security check failed.' );
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if ( ! current_user_can( 'manage_options' ) ) {
|
|
236
|
+
wp_die( 'Insufficient permissions.' );
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
$settings_url = add_query_arg(
|
|
240
|
+
[ 'page' => 'content-translator', 'ct_tab' => 'ct_settings' ],
|
|
241
|
+
admin_url( 'admin.php' )
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
if ( ! empty( $_POST['ct_reset_defaults'] ) ) {
|
|
245
|
+
update_option( 'ct_skip_meta', Fields::$default_skip_meta, false );
|
|
246
|
+
update_option( 'ct_allow_private_meta', Fields::$default_allow_private_meta, false );
|
|
247
|
+
wp_safe_redirect( add_query_arg( 'ct_settings_reset', '1', $settings_url ) );
|
|
248
|
+
exit;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
$raw_skip = sanitize_textarea_field( wp_unslash( $_POST['ct_skip_meta'] ?? '' ) );
|
|
252
|
+
$raw_allow = sanitize_textarea_field( wp_unslash( $_POST['ct_allow_private_meta'] ?? '' ) );
|
|
253
|
+
|
|
254
|
+
$skip_meta = array_values( array_filter(
|
|
255
|
+
array_map( 'sanitize_key', preg_split( '/
|
|
256
|
+
?
|
|
257
|
+
/', $raw_skip ) )
|
|
258
|
+
) );
|
|
259
|
+
|
|
260
|
+
$allow_meta = array_values( array_filter(
|
|
261
|
+
array_map( 'sanitize_key', preg_split( '/
|
|
262
|
+
?
|
|
263
|
+
/', $raw_allow ) )
|
|
264
|
+
) );
|
|
265
|
+
|
|
266
|
+
update_option( 'ct_skip_meta', $skip_meta, false );
|
|
267
|
+
update_option( 'ct_allow_private_meta', $allow_meta, false );
|
|
268
|
+
|
|
269
|
+
wp_safe_redirect( add_query_arg( 'ct_settings_saved', '1', $settings_url ) );
|
|
270
|
+
exit;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private static function tax_label( object $tax ): string {
|
|
274
|
+
$types = array_filter( array_map( static fn( $type ) => get_post_type_object( $type )->labels->name ?? '', (array) ( $tax->object_type ?? [] ) ) );
|
|
275
|
+
|
|
276
|
+
return $types ? implode( ' & ', $types ) . ' ' . strtolower( (string) $tax->labels->name ) : (string) $tax->labels->name;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private static function site_language(): string {
|
|
280
|
+
return (string) Languages::default();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private static function get_supported_languages( string $cpt_key ): array {
|
|
284
|
+
$site_lang = self::site_language();
|
|
285
|
+
|
|
286
|
+
return array_filter( Languages::for_type( $cpt_key ), fn( $code ) => $code !== $site_lang, ARRAY_FILTER_USE_KEY );
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private static function render_post_list( string $post_type, object $type_obj ): void {
|
|
290
|
+
$per_page = 20;
|
|
291
|
+
$paged = max( 1, (int) ( $_GET['paged'] ?? 1 ) );
|
|
292
|
+
$search = sanitize_text_field( $_GET['ct_s'] ?? '' );
|
|
293
|
+
|
|
294
|
+
$args = [
|
|
295
|
+
'post_type' => $post_type,
|
|
296
|
+
'post_status' => 'any',
|
|
297
|
+
'posts_per_page' => $per_page,
|
|
298
|
+
'paged' => $paged,
|
|
299
|
+
'post_parent' => 0,
|
|
300
|
+
'orderby' => 'title',
|
|
301
|
+
'order' => 'ASC',
|
|
302
|
+
];
|
|
303
|
+
|
|
304
|
+
if ( $search ) {
|
|
305
|
+
$args['s'] = $search;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
$query = new \WP_Query( $args );
|
|
309
|
+
$posts = $query->posts;
|
|
310
|
+
$total = $query->found_posts;
|
|
311
|
+
$pages = ceil( $total / $per_page );
|
|
312
|
+
|
|
313
|
+
$cpt_languages = self::get_supported_languages( $post_type );
|
|
314
|
+
$ids = array_map( fn( $p ) => $p->ID, $posts );
|
|
315
|
+
$completion = Storage::completion( $ids, 'post', array_keys( $cpt_languages ) );
|
|
316
|
+
|
|
317
|
+
?>
|
|
318
|
+
<div class="ct-list-header">
|
|
319
|
+
<div class="ct-list-title">
|
|
320
|
+
<h2><?php echo esc_html( $type_obj->labels->name ); ?></h2>
|
|
321
|
+
<?php if ( $total > 0 ) : ?>
|
|
322
|
+
<span class="ct-list-count"><?php echo (int) $total; ?> items</span>
|
|
323
|
+
<?php endif; ?>
|
|
324
|
+
</div>
|
|
325
|
+
|
|
326
|
+
<form method="get" class="ct-search-form">
|
|
327
|
+
<input type="hidden" name="page" value="content-translator">
|
|
328
|
+
<input type="hidden" name="ct_tab" value="<?php echo esc_attr( $post_type ); ?>">
|
|
329
|
+
<input type="search" name="ct_s" value="<?php echo esc_attr( $search ); ?>"
|
|
330
|
+
placeholder="Search posts…" class="ct-search">
|
|
331
|
+
<button type="submit" class="button">Search</button>
|
|
332
|
+
<?php if ( $search ) : ?>
|
|
333
|
+
<a href="<?php echo esc_url( self::tab_url( $post_type ) ); ?>" class="button">Clear</a>
|
|
334
|
+
<?php endif; ?>
|
|
335
|
+
</form>
|
|
336
|
+
</div>
|
|
337
|
+
|
|
338
|
+
<?php if ( empty( $posts ) ) : ?>
|
|
339
|
+
<p class="ct-empty">No posts found<?php echo $search ? ' for “' . esc_html( $search ) . '”' : ''; ?>.</p>
|
|
340
|
+
<?php else : ?>
|
|
341
|
+
|
|
342
|
+
<table class="ct-table">
|
|
343
|
+
<thead>
|
|
344
|
+
<tr>
|
|
345
|
+
<th class="ct-col-title">Title</th>
|
|
346
|
+
<th class="ct-col-status">
|
|
347
|
+
Translation Status
|
|
348
|
+
<?php if ( ! empty( $cpt_languages ) ) : ?>
|
|
349
|
+
<span class="ct-lang-count"><?php echo count( $cpt_languages ); ?> lang<?php echo count( $cpt_languages ) !== 1 ? 's' : ''; ?></span>
|
|
350
|
+
<?php endif; ?>
|
|
351
|
+
</th>
|
|
352
|
+
<th class="ct-col-action"></th>
|
|
353
|
+
</tr>
|
|
354
|
+
</thead>
|
|
355
|
+
<tbody>
|
|
356
|
+
<?php foreach ( $posts as $post ) :
|
|
357
|
+
$stat = $completion[ $post->ID ] ?? [ 'total' => 0, 'percentages' => [] ];
|
|
358
|
+
$total_fields = $stat['total'];
|
|
359
|
+
$status_obj = get_post_status_object( $post->post_status );
|
|
360
|
+
?>
|
|
361
|
+
<tr>
|
|
362
|
+
<td class="ct-col-title">
|
|
363
|
+
<span class="ct-post-title"><?php echo esc_html( $post->post_title ?: '(no title)' ); ?></span>
|
|
364
|
+
<?php if ( $status_obj ) : ?>
|
|
365
|
+
<span class="ct-status-pill ct-status-pill--<?php echo esc_attr( $post->post_status ); ?>">
|
|
366
|
+
<?php echo esc_html( $status_obj->label ); ?>
|
|
367
|
+
</span>
|
|
368
|
+
<?php endif; ?>
|
|
369
|
+
</td>
|
|
370
|
+
<td class="ct-col-status">
|
|
371
|
+
<?php if ( $total_fields === 0 || empty( $cpt_languages ) ) : ?>
|
|
372
|
+
<span class="ct-badge ct-badge--none">Not started</span>
|
|
373
|
+
<?php else : ?>
|
|
374
|
+
<div class="ct-progress-group">
|
|
375
|
+
<?php foreach ( $cpt_languages as $code => $label ) :
|
|
376
|
+
$pct = $stat['percentages'][ $code ] ?? 0;
|
|
377
|
+
$fill_class = $pct >= 100 ? 'ct-progress-fill--done' : ( $pct > 0 ? 'ct-progress-fill--partial' : '' );
|
|
378
|
+
?>
|
|
379
|
+
<div class="ct-progress-row">
|
|
380
|
+
<span class="ct-lang-label" title="<?php echo esc_attr( $label ); ?>">
|
|
381
|
+
<?php echo esc_html( strtoupper( $code ) ); ?>
|
|
382
|
+
</span>
|
|
383
|
+
<div class="ct-progress-bar" title="<?php echo esc_attr( $label . ': ' . (int) $pct . '%' ); ?>">
|
|
384
|
+
<div class="ct-progress-fill <?php echo esc_attr( $fill_class ); ?>" style="width:<?php echo (int) $pct; ?>%"></div>
|
|
385
|
+
</div>
|
|
386
|
+
<span class="ct-pct"><?php echo (int) $pct; ?>%</span>
|
|
387
|
+
</div>
|
|
388
|
+
<?php endforeach; ?>
|
|
389
|
+
</div>
|
|
390
|
+
<?php endif; ?>
|
|
391
|
+
</td>
|
|
392
|
+
<td class="ct-col-action">
|
|
393
|
+
<a href="<?php echo esc_url( self::edit_url( $post->ID, $post_type ) ); ?>"
|
|
394
|
+
class="button button-primary ct-btn-translate">Translate</a>
|
|
395
|
+
</td>
|
|
396
|
+
</tr>
|
|
397
|
+
<?php endforeach; ?>
|
|
398
|
+
</tbody>
|
|
399
|
+
</table>
|
|
400
|
+
|
|
401
|
+
<?php self::pagination( $paged, (int) $pages, $post_type ); ?>
|
|
402
|
+
<?php endif; ?>
|
|
403
|
+
<?php
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
private static function render_post_edit( int $post_id, string $post_type ): void {
|
|
407
|
+
$post = get_post( $post_id );
|
|
408
|
+
|
|
409
|
+
if ( ! $post ) {
|
|
410
|
+
echo '<p>Post not found.</p>';
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
$back_url = self::tab_url( $post_type );
|
|
415
|
+
|
|
416
|
+
if ( ! empty( $_GET['saved'] ) ) {
|
|
417
|
+
echo '<div class="notice notice-success is-dismissible"><p>Translations saved.</p></div>';
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
?>
|
|
421
|
+
<div class="ct-edit-header">
|
|
422
|
+
<a href="<?php echo esc_url( $back_url ); ?>" class="ct-back">← Back</a>
|
|
423
|
+
<h2><?php echo esc_html( $post->post_title ?: '(no title)' ); ?></h2>
|
|
424
|
+
</div>
|
|
425
|
+
|
|
426
|
+
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="ct-form">
|
|
427
|
+
<?php wp_nonce_field( 'ct_save_' . $post_id, 'ct_nonce' ); ?>
|
|
428
|
+
<input type="hidden" name="action" value="ct_save_translation">
|
|
429
|
+
<input type="hidden" name="ct_object_id" value="<?php echo (int) $post_id; ?>">
|
|
430
|
+
<input type="hidden" name="ct_object_type" value="post">
|
|
431
|
+
<input type="hidden" name="ct_tab" value="<?php echo esc_attr( $post_type ); ?>">
|
|
432
|
+
|
|
433
|
+
<?php self::render_post_fields( $post_id, $post_type, true ); ?>
|
|
434
|
+
<?php self::render_child_posts( $post_id, $post_type, 0 ); ?>
|
|
435
|
+
|
|
436
|
+
<div class="ct-form-footer">
|
|
437
|
+
<button type="submit" class="button button-primary">Save Translations</button>
|
|
438
|
+
<a href="<?php echo esc_url( $back_url ); ?>" class="button">Cancel</a>
|
|
439
|
+
</div>
|
|
440
|
+
</form>
|
|
441
|
+
<?php
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
private static function render_post_fields( int $post_id, string $post_type = '', bool $wysiwyg = true ): void {
|
|
445
|
+
$fields = Fields::for_post( $post_id );
|
|
446
|
+
$translations = Storage::get( $post_id, 'post' );
|
|
447
|
+
$cpt_languages = Languages::all();
|
|
448
|
+
|
|
449
|
+
if ( empty( $fields ) ) {
|
|
450
|
+
echo '<p class="ct-empty">No translatable text fields found for this post.</p>';
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
$content_field = null;
|
|
455
|
+
$table_fields = [];
|
|
456
|
+
|
|
457
|
+
foreach ( $fields as $field ) {
|
|
458
|
+
if ( $wysiwyg && $field['key'] === 'post_content' ) {
|
|
459
|
+
$content_field = $field;
|
|
460
|
+
} else {
|
|
461
|
+
$table_fields[] = $field;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if ( $content_field ) {
|
|
466
|
+
$key = $content_field['key'];
|
|
467
|
+
?>
|
|
468
|
+
<div class="ct-wysiwyg-block">
|
|
469
|
+
|
|
470
|
+
<div class="ct-original-section">
|
|
471
|
+
<button type="button" class="ct-original-toggle" aria-expanded="false"
|
|
472
|
+
onclick="(function(btn){
|
|
473
|
+
var body = btn.closest('.ct-original-section').querySelector('.ct-original-body');
|
|
474
|
+
var open = body.style.display !== 'none' && body.style.display !== '';
|
|
475
|
+
body.style.display = open ? 'none' : 'block';
|
|
476
|
+
btn.setAttribute('aria-expanded', !open);
|
|
477
|
+
btn.querySelector('.ct-chevron').style.transform = open ? '' : 'rotate(180deg)';
|
|
478
|
+
btn.querySelector('.ct-toggle-text').textContent = open ? 'Show original (EN)' : 'Hide original';
|
|
479
|
+
})(this)">
|
|
480
|
+
<span class="ct-field-label">post_content</span>
|
|
481
|
+
<span class="ct-toggle-text">Show original (EN)</span>
|
|
482
|
+
<span class="ct-chevron" aria-hidden="true">▼</span>
|
|
483
|
+
</button>
|
|
484
|
+
<div class="ct-original-body" style="display:none">
|
|
485
|
+
<div class="ct-original-full">
|
|
486
|
+
<?php echo wp_kses_post( $content_field['original'] ); ?>
|
|
487
|
+
</div>
|
|
488
|
+
</div>
|
|
489
|
+
</div>
|
|
490
|
+
|
|
491
|
+
<?php foreach ( $cpt_languages as $code => $label ) :
|
|
492
|
+
$saved = $translations[ $key ][ $code ] ?? '';
|
|
493
|
+
$editor_id = 'ct_editor_' . $post_id . '_' . $code;
|
|
494
|
+
$field_name = "ct_fields[{$post_id}][{$key}][{$code}]";
|
|
495
|
+
$has_value = trim( $saved ) !== '';
|
|
496
|
+
$item_id = 'ct_acc_' . $post_id . '_' . $code;
|
|
497
|
+
?>
|
|
498
|
+
<div class="ct-accordion-item" id="<?php echo esc_attr( $item_id ); ?>">
|
|
499
|
+
<button type="button" class="ct-accordion-toggle" aria-expanded="false"
|
|
500
|
+
onclick="(function(btn){
|
|
501
|
+
var item = btn.closest('.ct-accordion-item');
|
|
502
|
+
var body = item.querySelector('.ct-accordion-body');
|
|
503
|
+
var open = item.classList.contains('ct-accordion-item--open');
|
|
504
|
+
item.classList.toggle('ct-accordion-item--open', !open);
|
|
505
|
+
body.style.display = open ? 'none' : 'block';
|
|
506
|
+
btn.setAttribute('aria-expanded', !open);
|
|
507
|
+
btn.querySelector('.ct-chevron').style.transform = open ? '' : 'rotate(180deg)';
|
|
508
|
+
})(this)">
|
|
509
|
+
<span class="ct-accordion-lang">
|
|
510
|
+
<span class="ct-lang-pill"><?php echo esc_html( strtoupper( $code ) ); ?></span>
|
|
511
|
+
<?php echo esc_html( $label ); ?>
|
|
512
|
+
</span>
|
|
513
|
+
<?php if ( $has_value ) : ?>
|
|
514
|
+
<span class="ct-acc-status ct-acc-status--filled">Translated</span>
|
|
515
|
+
<?php else : ?>
|
|
516
|
+
<span class="ct-acc-status ct-acc-status--empty">Not translated</span>
|
|
517
|
+
<?php endif; ?>
|
|
518
|
+
<span class="ct-chevron" aria-hidden="true">▼</span>
|
|
519
|
+
</button>
|
|
520
|
+
<div class="ct-accordion-body" style="display:none">
|
|
521
|
+
<div class="ct-accordion-inner">
|
|
522
|
+
<?php
|
|
523
|
+
wp_editor( $saved, $editor_id, [
|
|
524
|
+
'textarea_name' => $field_name,
|
|
525
|
+
'textarea_rows' => 14,
|
|
526
|
+
'media_buttons' => false,
|
|
527
|
+
'teeny' => false,
|
|
528
|
+
'quicktags' => true,
|
|
529
|
+
] );
|
|
530
|
+
?>
|
|
531
|
+
</div>
|
|
532
|
+
</div>
|
|
533
|
+
</div>
|
|
534
|
+
<?php endforeach; ?>
|
|
535
|
+
|
|
536
|
+
</div>
|
|
537
|
+
<?php
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if ( ! empty( $table_fields ) ) : ?>
|
|
541
|
+
<table class="ct-fields-table">
|
|
542
|
+
<thead>
|
|
543
|
+
<tr>
|
|
544
|
+
<th>Field</th>
|
|
545
|
+
<th>Original (EN)</th>
|
|
546
|
+
<?php foreach ( $cpt_languages as $code => $label ) : ?>
|
|
547
|
+
<th>
|
|
548
|
+
<?php echo esc_html( $label ); ?>
|
|
549
|
+
<span class="ct-th-code"><?php echo esc_html( strtoupper( $code ) ); ?></span>
|
|
550
|
+
</th>
|
|
551
|
+
<?php endforeach; ?>
|
|
552
|
+
</tr>
|
|
553
|
+
</thead>
|
|
554
|
+
<tbody>
|
|
555
|
+
<?php foreach ( $table_fields as $field ) :
|
|
556
|
+
$key = $field['key'];
|
|
557
|
+
$name_prefix = "ct_fields[{$post_id}][{$key}]";
|
|
558
|
+
?>
|
|
559
|
+
<tr>
|
|
560
|
+
<td class="ct-field-key"><code><?php echo esc_html( $key ); ?></code></td>
|
|
561
|
+
<td class="ct-field-original">
|
|
562
|
+
|
|
563
|
+
<div class="ct-original-text"><?php echo esc_html( $field['original'] ); ?></div>
|
|
564
|
+
</td>
|
|
565
|
+
<?php foreach ( $cpt_languages as $code => $label ) :
|
|
566
|
+
$saved = $translations[ $key ][ $code ] ?? '';
|
|
567
|
+
?>
|
|
568
|
+
<td>
|
|
569
|
+
<?php if ( strlen( $field['original'] ) > 100 ) : ?>
|
|
570
|
+
<textarea name="<?php echo esc_attr( "{$name_prefix}[{$code}]" ); ?>"
|
|
571
|
+
rows="3" class="ct-textarea"><?php echo esc_textarea( $saved ); ?></textarea>
|
|
572
|
+
<?php else : ?>
|
|
573
|
+
<input type="text"
|
|
574
|
+
name="<?php echo esc_attr( "{$name_prefix}[{$code}]" ); ?>"
|
|
575
|
+
value="<?php echo esc_attr( $saved ); ?>"
|
|
576
|
+
class="ct-input">
|
|
577
|
+
<?php endif; ?>
|
|
578
|
+
</td>
|
|
579
|
+
<?php endforeach; ?>
|
|
580
|
+
</tr>
|
|
581
|
+
<?php endforeach; ?>
|
|
582
|
+
</tbody>
|
|
583
|
+
</table>
|
|
584
|
+
<?php endif;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
private static function render_child_posts(
|
|
588
|
+
int $parent_id,
|
|
589
|
+
string $post_type,
|
|
590
|
+
int $depth,
|
|
591
|
+
array $post_type_list = []
|
|
592
|
+
): void {
|
|
593
|
+
if ( empty( $post_type_list ) ) {
|
|
594
|
+
$post_type_list = array_values(
|
|
595
|
+
array_diff( get_post_types(), [ 'attachment' ] )
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
$children = get_posts( [
|
|
600
|
+
'post_type' => $post_type_list,
|
|
601
|
+
'post_parent' => $parent_id,
|
|
602
|
+
'posts_per_page' => -1,
|
|
603
|
+
'post_status' => 'any',
|
|
604
|
+
'orderby' => 'menu_order title',
|
|
605
|
+
'order' => 'ASC',
|
|
606
|
+
] );
|
|
607
|
+
|
|
608
|
+
if ( empty( $children ) ) {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
foreach ( $children as $child ) :
|
|
613
|
+
?>
|
|
614
|
+
<div class="ct-child-box" style="--depth:<?php echo (int) $depth; ?>">
|
|
615
|
+
<div class="ct-child-header">
|
|
616
|
+
<span class="ct-child-label">Child: <?php echo esc_html( get_post_type_object( $child->post_type )?->labels->singular_name ?? $child->post_type ); ?></span>
|
|
617
|
+
<strong><?php echo esc_html( $child->post_title ?: '(no title)' ); ?></strong>
|
|
618
|
+
</div>
|
|
619
|
+
|
|
620
|
+
<input type="hidden" name="ct_child_ids[]" value="<?php echo (int) $child->ID; ?>">
|
|
621
|
+
|
|
622
|
+
<?php self::render_post_fields( $child->ID, $post_type, false ); ?>
|
|
623
|
+
|
|
624
|
+
<?php
|
|
625
|
+
self::render_child_posts( $child->ID, $post_type, $depth + 1, $post_type_list );
|
|
626
|
+
?>
|
|
627
|
+
</div>
|
|
628
|
+
<?php
|
|
629
|
+
endforeach;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
private static function render_term_list( string $taxonomy, object $tax_obj ): void {
|
|
633
|
+
$per_page = 30;
|
|
634
|
+
$paged = max( 1, (int) ( $_GET['paged'] ?? 1 ) );
|
|
635
|
+
$offset = ( $paged - 1 ) * $per_page;
|
|
636
|
+
$search = sanitize_text_field( $_GET['ct_s'] ?? '' );
|
|
637
|
+
|
|
638
|
+
$args = [
|
|
639
|
+
'taxonomy' => $taxonomy,
|
|
640
|
+
'hide_empty' => false,
|
|
641
|
+
'number' => $per_page,
|
|
642
|
+
'offset' => $offset,
|
|
643
|
+
'orderby' => 'name',
|
|
644
|
+
'order' => 'ASC',
|
|
645
|
+
];
|
|
646
|
+
|
|
647
|
+
if ( $search ) {
|
|
648
|
+
$args['search'] = $search;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
$terms = get_terms( $args );
|
|
652
|
+
$total = wp_count_terms( $taxonomy, [ 'hide_empty' => false ] );
|
|
653
|
+
$pages = ceil( $total / $per_page );
|
|
654
|
+
$tax_languages = self::get_supported_languages( $taxonomy );
|
|
655
|
+
|
|
656
|
+
$ids = is_array( $terms ) ? array_map( fn( $t ) => $t->term_id, $terms ) : [];
|
|
657
|
+
$completion = Storage::completion( $ids, 'term', array_keys( $tax_languages ) );
|
|
658
|
+
|
|
659
|
+
?>
|
|
660
|
+
<div class="ct-list-header">
|
|
661
|
+
<div class="ct-list-title">
|
|
662
|
+
<h2><?php echo esc_html( self::tax_label( $tax_obj ) ); ?></h2>
|
|
663
|
+
<?php if ( $total > 0 ) : ?>
|
|
664
|
+
<span class="ct-list-count"><?php echo (int) $total; ?> terms</span>
|
|
665
|
+
<?php endif; ?>
|
|
666
|
+
</div>
|
|
667
|
+
|
|
668
|
+
<form method="get" class="ct-search-form">
|
|
669
|
+
<input type="hidden" name="page" value="content-translator">
|
|
670
|
+
<input type="hidden" name="ct_tab" value="<?php echo esc_attr( $taxonomy ); ?>">
|
|
671
|
+
<input type="search" name="ct_s" value="<?php echo esc_attr( $search ); ?>"
|
|
672
|
+
placeholder="Search terms…" class="ct-search">
|
|
673
|
+
<button type="submit" class="button">Search</button>
|
|
674
|
+
<?php if ( $search ) : ?>
|
|
675
|
+
<a href="<?php echo esc_url( self::tab_url( $taxonomy ) ); ?>" class="button">Clear</a>
|
|
676
|
+
<?php endif; ?>
|
|
677
|
+
</form>
|
|
678
|
+
</div>
|
|
679
|
+
|
|
680
|
+
<?php if ( empty( $terms ) || is_wp_error( $terms ) ) : ?>
|
|
681
|
+
<p class="ct-empty">No terms found<?php echo $search ? ' for “' . esc_html( $search ) . '”' : ''; ?>.</p>
|
|
682
|
+
<?php else : ?>
|
|
683
|
+
|
|
684
|
+
<table class="ct-table">
|
|
685
|
+
<thead>
|
|
686
|
+
<tr>
|
|
687
|
+
<th class="ct-col-title">Name</th>
|
|
688
|
+
<th class="ct-col-status">
|
|
689
|
+
Translation Status
|
|
690
|
+
<?php if ( ! empty( $tax_languages ) ) : ?>
|
|
691
|
+
<span class="ct-lang-count"><?php echo count( $tax_languages ); ?> lang<?php echo count( $tax_languages ) !== 1 ? 's' : ''; ?></span>
|
|
692
|
+
<?php endif; ?>
|
|
693
|
+
</th>
|
|
694
|
+
<th class="ct-col-action"></th>
|
|
695
|
+
</tr>
|
|
696
|
+
</thead>
|
|
697
|
+
<tbody>
|
|
698
|
+
<?php foreach ( $terms as $term ) :
|
|
699
|
+
$stat = $completion[ $term->term_id ] ?? [ 'total' => 0, 'percentages' => [] ];
|
|
700
|
+
?>
|
|
701
|
+
<tr>
|
|
702
|
+
<td class="ct-col-title">
|
|
703
|
+
<span class="ct-post-title"><?php echo esc_html( $term->name ); ?></span>
|
|
704
|
+
<?php if ( $term->count > 0 ) : ?>
|
|
705
|
+
<span class="ct-count-pill"><?php echo (int) $term->count; ?></span>
|
|
706
|
+
<?php endif; ?>
|
|
707
|
+
</td>
|
|
708
|
+
<td class="ct-col-status">
|
|
709
|
+
<?php if ( $stat['total'] === 0 || empty( $tax_languages ) ) : ?>
|
|
710
|
+
<span class="ct-badge ct-badge--none">Not started</span>
|
|
711
|
+
<?php else : ?>
|
|
712
|
+
<div class="ct-progress-group">
|
|
713
|
+
<?php foreach ( $tax_languages as $code => $label ) :
|
|
714
|
+
$pct = $stat['percentages'][ $code ] ?? 0;
|
|
715
|
+
$fill_class = $pct >= 100 ? 'ct-progress-fill--done' : ( $pct > 0 ? 'ct-progress-fill--partial' : '' );
|
|
716
|
+
?>
|
|
717
|
+
<div class="ct-progress-row">
|
|
718
|
+
<span class="ct-lang-label" title="<?php echo esc_attr( $label ); ?>">
|
|
719
|
+
<?php echo esc_html( strtoupper( $code ) ); ?>
|
|
720
|
+
</span>
|
|
721
|
+
<div class="ct-progress-bar" title="<?php echo esc_attr( $label . ': ' . (int) $pct . '%' ); ?>">
|
|
722
|
+
<div class="ct-progress-fill <?php echo esc_attr( $fill_class ); ?>" style="width:<?php echo (int) $pct; ?>%"></div>
|
|
723
|
+
</div>
|
|
724
|
+
<span class="ct-pct"><?php echo (int) $pct; ?>%</span>
|
|
725
|
+
</div>
|
|
726
|
+
<?php endforeach; ?>
|
|
727
|
+
</div>
|
|
728
|
+
<?php endif; ?>
|
|
729
|
+
</td>
|
|
730
|
+
<td class="ct-col-action">
|
|
731
|
+
<a href="<?php echo esc_url( self::edit_url( $term->term_id, $taxonomy ) ); ?>"
|
|
732
|
+
class="button button-primary ct-btn-translate">Translate</a>
|
|
733
|
+
</td>
|
|
734
|
+
</tr>
|
|
735
|
+
<?php endforeach; ?>
|
|
736
|
+
</tbody>
|
|
737
|
+
</table>
|
|
738
|
+
|
|
739
|
+
<?php self::pagination( $paged, (int) $pages, $taxonomy ); ?>
|
|
740
|
+
<?php endif; ?>
|
|
741
|
+
<?php
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
private static function render_term_edit( int $term_id, string $taxonomy ): void {
|
|
745
|
+
$term = get_term( $term_id, $taxonomy );
|
|
746
|
+
|
|
747
|
+
if ( ! $term || is_wp_error( $term ) ) {
|
|
748
|
+
echo '<p>Term not found.</p>';
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
$back_url = self::tab_url( $taxonomy );
|
|
753
|
+
$fields = Fields::for_term( $term_id, $taxonomy );
|
|
754
|
+
$translations = Storage::get( $term_id, 'term' );
|
|
755
|
+
$tax_languages = Languages::all();
|
|
756
|
+
|
|
757
|
+
if ( ! empty( $_GET['saved'] ) ) {
|
|
758
|
+
echo '<div class="notice notice-success is-dismissible"><p>Translations saved.</p></div>';
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
?>
|
|
762
|
+
<div class="ct-edit-header">
|
|
763
|
+
<a href="<?php echo esc_url( $back_url ); ?>" class="ct-back">← Back</a>
|
|
764
|
+
<h2><?php echo esc_html( $term->name ); ?></h2>
|
|
765
|
+
</div>
|
|
766
|
+
|
|
767
|
+
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="ct-form">
|
|
768
|
+
<?php wp_nonce_field( 'ct_save_' . $term_id, 'ct_nonce' ); ?>
|
|
769
|
+
<input type="hidden" name="action" value="ct_save_translation">
|
|
770
|
+
<input type="hidden" name="ct_object_id" value="<?php echo (int) $term_id; ?>">
|
|
771
|
+
<input type="hidden" name="ct_object_type" value="term">
|
|
772
|
+
<input type="hidden" name="ct_tab" value="<?php echo esc_attr( $taxonomy ); ?>">
|
|
773
|
+
|
|
774
|
+
<?php if ( empty( $fields ) ) : ?>
|
|
775
|
+
<p class="ct-empty">No translatable text fields found for this term.</p>
|
|
776
|
+
<?php else : ?>
|
|
777
|
+
|
|
778
|
+
<table class="ct-fields-table">
|
|
779
|
+
<thead>
|
|
780
|
+
<tr>
|
|
781
|
+
<th>Field</th>
|
|
782
|
+
<th>Original (EN)</th>
|
|
783
|
+
<?php foreach ( $tax_languages as $code => $label ) : ?>
|
|
784
|
+
<th>
|
|
785
|
+
<?php echo esc_html( $label ); ?>
|
|
786
|
+
<span class="ct-th-code"><?php echo esc_html( strtoupper( $code ) ); ?></span>
|
|
787
|
+
</th>
|
|
788
|
+
<?php endforeach; ?>
|
|
789
|
+
</tr>
|
|
790
|
+
</thead>
|
|
791
|
+
<tbody>
|
|
792
|
+
<?php foreach ( $fields as $field ) :
|
|
793
|
+
$key = $field['key'];
|
|
794
|
+
$name_prefix = "ct_fields[{$term_id}][{$key}]";
|
|
795
|
+
?>
|
|
796
|
+
<tr>
|
|
797
|
+
<td class="ct-field-key"><code><?php echo esc_html( $key ); ?></code></td>
|
|
798
|
+
<td class="ct-field-original">
|
|
799
|
+
<div class="ct-original-text"><?php echo esc_html( $field['original'] ); ?></div>
|
|
800
|
+
</td>
|
|
801
|
+
<?php foreach ( $tax_languages as $code => $label ) :
|
|
802
|
+
$saved = $translations[ $key ][ $code ] ?? '';
|
|
803
|
+
?>
|
|
804
|
+
<td>
|
|
805
|
+
<?php if ( strlen( $field['original'] ) > 100 ) : ?>
|
|
806
|
+
<textarea name="<?php echo esc_attr( "{$name_prefix}[{$code}]" ); ?>"
|
|
807
|
+
rows="3" class="ct-textarea"><?php echo esc_textarea( $saved ); ?></textarea>
|
|
808
|
+
<?php else : ?>
|
|
809
|
+
<input type="text"
|
|
810
|
+
name="<?php echo esc_attr( "{$name_prefix}[{$code}]" ); ?>"
|
|
811
|
+
value="<?php echo esc_attr( $saved ); ?>"
|
|
812
|
+
class="ct-input">
|
|
813
|
+
<?php endif; ?>
|
|
814
|
+
</td>
|
|
815
|
+
<?php endforeach; ?>
|
|
816
|
+
</tr>
|
|
817
|
+
<?php endforeach; ?>
|
|
818
|
+
</tbody>
|
|
819
|
+
</table>
|
|
820
|
+
|
|
821
|
+
<?php endif; ?>
|
|
822
|
+
|
|
823
|
+
<div class="ct-form-footer">
|
|
824
|
+
<button type="submit" class="button button-primary">Save Translations</button>
|
|
825
|
+
<a href="<?php echo esc_url( $back_url ); ?>" class="button">Cancel</a>
|
|
826
|
+
</div>
|
|
827
|
+
</form>
|
|
828
|
+
<?php
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
public static function handle_save(): void {
|
|
832
|
+
$object_id = (int) ( $_POST['ct_object_id'] ?? 0 );
|
|
833
|
+
$object_type = sanitize_key( $_POST['ct_object_type'] ?? '' );
|
|
834
|
+
$tab = sanitize_key( $_POST['ct_tab'] ?? '' );
|
|
835
|
+
|
|
836
|
+
if ( ! $object_id || ! in_array( $object_type, [ 'post', 'term' ], true ) ) {
|
|
837
|
+
wp_die( 'Invalid request.' );
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
if ( ! check_admin_referer( 'ct_save_' . $object_id, 'ct_nonce' ) ) {
|
|
841
|
+
wp_die( 'Security check failed.' );
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
if ( ! current_user_can( 'manage_options' ) ) {
|
|
845
|
+
wp_die( 'Insufficient permissions.' );
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
$all_fields = $_POST['ct_fields'] ?? [];
|
|
849
|
+
|
|
850
|
+
if ( ! empty( $all_fields[ $object_id ] ) ) {
|
|
851
|
+
$sanitized = self::sanitize_fields( $all_fields[ $object_id ] );
|
|
852
|
+
Storage::save( $object_id, $object_type, $sanitized );
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
if ( $object_type === 'post' ) {
|
|
856
|
+
$child_ids = array_map( 'intval', (array) ( $_POST['ct_child_ids'] ?? [] ) );
|
|
857
|
+
foreach ( $child_ids as $child_id ) {
|
|
858
|
+
if ( $child_id && ! empty( $all_fields[ $child_id ] ) ) {
|
|
859
|
+
$sanitized = self::sanitize_fields( $all_fields[ $child_id ] );
|
|
860
|
+
Storage::save( $child_id, 'post', $sanitized );
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
$redirect = add_query_arg( [
|
|
866
|
+
'page' => 'content-translator',
|
|
867
|
+
'ct_tab' => $tab,
|
|
868
|
+
'ct_edit' => $object_id,
|
|
869
|
+
'saved' => '1',
|
|
870
|
+
], admin_url( 'admin.php' ) );
|
|
871
|
+
|
|
872
|
+
wp_safe_redirect( $redirect );
|
|
873
|
+
exit;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
private static function sanitize_fields( array $raw ): array {
|
|
877
|
+
$clean = [];
|
|
878
|
+
foreach ( $raw as $key => $langs ) {
|
|
879
|
+
$key = implode( '.', array_map( 'sanitize_key', explode( '.', (string) $key ) ) );
|
|
880
|
+
|
|
881
|
+
if ( $key === '' || ! is_array( $langs ) ) {
|
|
882
|
+
continue;
|
|
883
|
+
}
|
|
884
|
+
foreach ( $langs as $lang => $val ) {
|
|
885
|
+
$lang = sanitize_key( $lang );
|
|
886
|
+
$clean[ $key ][ $lang ] = sanitize_textarea_field( $val );
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
return $clean;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
private static function pagination( int $current, int $total, string $tab ): void {
|
|
893
|
+
if ( $total <= 1 ) {
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
echo '<div class="ct-pagination">';
|
|
898
|
+
for ( $i = 1; $i <= $total; $i++ ) {
|
|
899
|
+
$url = add_query_arg( [ 'page' => 'content-translator', 'ct_tab' => $tab, 'paged' => $i ], admin_url( 'admin.php' ) );
|
|
900
|
+
$class = $i === $current ? 'button button-primary' : 'button';
|
|
901
|
+
printf( '<a href="%s" class="%s">%d</a> ', esc_url( $url ), $class, $i );
|
|
902
|
+
}
|
|
903
|
+
echo '</div>';
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
private static function tab_url( string $tab ): string {
|
|
907
|
+
return add_query_arg( [ 'page' => 'content-translator', 'ct_tab' => $tab ], admin_url( 'admin.php' ) );
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
private static function edit_url( int $id, string $tab ): string {
|
|
911
|
+
return add_query_arg( [ 'page' => 'content-translator', 'ct_tab' => $tab, 'ct_edit' => $id ], admin_url( 'admin.php' ) );
|
|
912
|
+
}
|
|
913
|
+
}
|