@wp-operations/wp-ops 0.2.0 → 0.3.1

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.
Files changed (122) hide show
  1. package/README.md +30 -27
  2. package/bin/wp-app.js +2 -0
  3. package/bin/wp-code.js +2 -0
  4. package/bin/wp-ide.js +2 -0
  5. package/package.json +12 -3
  6. package/src/commands/platform.js +16 -2
  7. package/src/commands/sandbox.js +3 -3
  8. package/src/main.js +10 -4
  9. package/wp-operations/CHANGELOG.md +14 -0
  10. package/wp-operations/README.md +39 -0
  11. package/wp-operations/agents/builder/AGENT.md +15 -0
  12. package/wp-operations/assets/admin.css +631 -0
  13. package/wp-operations/assets/admin.js +614 -0
  14. package/wp-operations/assets/app/app.css +1 -0
  15. package/wp-operations/assets/app/app.js +70 -0
  16. package/wp-operations/assets/app/fonts/inter-medium.woff +0 -0
  17. package/wp-operations/assets/app/fonts/inter-regular.woff +0 -0
  18. package/wp-operations/assets/app/fonts/inter-semibold.woff +0 -0
  19. package/wp-operations/assets/app/login.css +42 -0
  20. package/wp-operations/assets/brand/icon.svg +20 -0
  21. package/wp-operations/assets/brand/mark.svg +19 -0
  22. package/wp-operations/assets/editors/audiobook.css +311 -0
  23. package/wp-operations/assets/editors/audiofile.css +286 -0
  24. package/wp-operations/assets/editors/course.css +701 -0
  25. package/wp-operations/assets/editors/course.js +698 -0
  26. package/wp-operations/assets/editors/faq.css +258 -0
  27. package/wp-operations/assets/editors/faq.js +196 -0
  28. package/wp-operations/assets/editors/interactive.css +220 -0
  29. package/wp-operations/assets/editors/interactive.js +378 -0
  30. package/wp-operations/assets/editors/media-sources.css +3 -0
  31. package/wp-operations/assets/editors/media-sources.js +40 -0
  32. package/wp-operations/assets/editors/open-media.css +114 -0
  33. package/wp-operations/assets/editors/open-media.js +182 -0
  34. package/wp-operations/assets/editors/thumbnails.css +335 -0
  35. package/wp-operations/assets/editors/thumbnails.js +447 -0
  36. package/wp-operations/assets/editors/translator.css +818 -0
  37. package/wp-operations/functions.php +24 -0
  38. package/wp-operations/index.php +14 -0
  39. package/wp-operations/platform.json +1836 -0
  40. package/wp-operations/skills/ai-development-guardrails/SKILL.md +49 -0
  41. package/wp-operations/skills/gutenberg-reference/SKILL.md +45 -0
  42. package/wp-operations/skills/js-tooling-for-themes/SKILL.md +51 -0
  43. package/wp-operations/skills/tailwind-for-wp/SKILL.md +62 -0
  44. package/wp-operations/skills/wp-clean-classic-themes/SKILL.md +112 -0
  45. package/wp-operations/skills/wp-psr4-plugins/SKILL.md +2043 -0
  46. package/wp-operations/skills/wp-security/SKILL.md +90 -0
  47. package/wp-operations/src/Admin.php +78 -0
  48. package/wp-operations/src/App/Access.php +57 -0
  49. package/wp-operations/src/App/Admin.php +442 -0
  50. package/wp-operations/src/App/App.php +157 -0
  51. package/wp-operations/src/App/Apps.php +379 -0
  52. package/wp-operations/src/App/Bundle.php +111 -0
  53. package/wp-operations/src/App/Contents.php +676 -0
  54. package/wp-operations/src/App/Engine.php +554 -0
  55. package/wp-operations/src/App/Github.php +308 -0
  56. package/wp-operations/src/App/Module.php +23 -0
  57. package/wp-operations/src/App/Ops.php +470 -0
  58. package/wp-operations/src/App/Packages.php +289 -0
  59. package/wp-operations/src/App/Rest.php +539 -0
  60. package/wp-operations/src/App/Sandboxes.php +206 -0
  61. package/wp-operations/src/App/Store.php +104 -0
  62. package/wp-operations/src/App/Usage.php +268 -0
  63. package/wp-operations/src/Config.php +249 -0
  64. package/wp-operations/src/Content/Editor.php +223 -0
  65. package/wp-operations/src/Content/Fields.php +681 -0
  66. package/wp-operations/src/Content/Scoring.php +253 -0
  67. package/wp-operations/src/Content/Structure.php +293 -0
  68. package/wp-operations/src/Content/Types.php +150 -0
  69. package/wp-operations/src/Content/Uploads.php +50 -0
  70. package/wp-operations/src/Editors/AudiobookAuthor.php +61 -0
  71. package/wp-operations/src/Editors/AudiobookChapters.php +632 -0
  72. package/wp-operations/src/Editors/AudiobookCover.php +235 -0
  73. package/wp-operations/src/Editors/Audiofile.php +462 -0
  74. package/wp-operations/src/Editors/Course.php +784 -0
  75. package/wp-operations/src/Editors/Ebook.php +242 -0
  76. package/wp-operations/src/Editors/Editors.php +44 -0
  77. package/wp-operations/src/Editors/Faq.php +237 -0
  78. package/wp-operations/src/Editors/Files.php +139 -0
  79. package/wp-operations/src/Editors/Interactive/Builder.php +103 -0
  80. package/wp-operations/src/Editors/Interactive/Card.php +76 -0
  81. package/wp-operations/src/Editors/Interactive/Editor.php +42 -0
  82. package/wp-operations/src/Editors/Interactive/Field.php +272 -0
  83. package/wp-operations/src/Editors/Interactive/Map.php +84 -0
  84. package/wp-operations/src/Editors/Interactive/Models.php +227 -0
  85. package/wp-operations/src/Editors/Interactive/Report.php +59 -0
  86. package/wp-operations/src/Editors/Interactive/Save.php +323 -0
  87. package/wp-operations/src/Editors/Interactive/Scoring.php +244 -0
  88. package/wp-operations/src/Editors/Interactive/Structure.php +195 -0
  89. package/wp-operations/src/Editors/Legal.php +235 -0
  90. package/wp-operations/src/Editors/OpenMedia.php +140 -0
  91. package/wp-operations/src/Editors/OriginalTitle.php +112 -0
  92. package/wp-operations/src/Editors/Sources.php +28 -0
  93. package/wp-operations/src/Editors/Thumbnails/Admin.php +306 -0
  94. package/wp-operations/src/Editors/Thumbnails/Ajax.php +163 -0
  95. package/wp-operations/src/Editors/Thumbnails/Box.php +188 -0
  96. package/wp-operations/src/Editors/Thumbnails/Storage.php +228 -0
  97. package/wp-operations/src/Editors/Translator/Admin.php +913 -0
  98. package/wp-operations/src/Editors/Translator/Api.php +63 -0
  99. package/wp-operations/src/Editors/Translator/Bar.php +83 -0
  100. package/wp-operations/src/Editors/Translator/Fields.php +273 -0
  101. package/wp-operations/src/Editors/Translator/Storage.php +131 -0
  102. package/wp-operations/src/Editors/Translator/Thumbnail.php +215 -0
  103. package/wp-operations/src/Identity.php +104 -0
  104. package/wp-operations/src/Languages.php +45 -0
  105. package/wp-operations/src/Lockdown.php +85 -0
  106. package/wp-operations/src/Modules/Apps/Admin.php +472 -0
  107. package/wp-operations/src/Modules/Apps/Api.php +193 -0
  108. package/wp-operations/src/Modules/Apps/Apps.php +279 -0
  109. package/wp-operations/src/Modules/Apps/Formatters.php +478 -0
  110. package/wp-operations/src/Modules/Apps/Schemes.php +248 -0
  111. package/wp-operations/src/Modules/Apps/Table.php +101 -0
  112. package/wp-operations/src/Modules/Thumbnails.php +68 -0
  113. package/wp-operations/src/Modules/Translations.php +133 -0
  114. package/wp-operations/src/Platform.php +59 -0
  115. package/wp-operations/src/Setup.php +273 -0
  116. package/wp-operations/src/Status.php +99 -0
  117. package/wp-operations/src/Storage/Client.php +168 -0
  118. package/wp-operations/src/Storage/Offload.php +120 -0
  119. package/wp-operations/src/Storage/Settings.php +166 -0
  120. package/wp-operations/src/Storage/Storage.php +35 -0
  121. package/wp-operations/src/Updates.php +151 -0
  122. package/wp-operations/style.css +11 -0
@@ -0,0 +1,63 @@
1
+ <?php
2
+ namespace Platform\Editors\Translator;
3
+
4
+ use Platform\Languages;
5
+ use Platform\Platform;
6
+
7
+ defined( 'ABSPATH' ) || exit;
8
+
9
+ class Api {
10
+ public static function post( int $post_id ): array {
11
+ return Storage::get( $post_id, 'post' );
12
+ }
13
+
14
+ public static function post_lang( int $post_id, string $lang ): array {
15
+ return self::extract_lang( Storage::get( $post_id, 'post' ), $lang );
16
+ }
17
+
18
+ public static function post_field( int $post_id, string $field, string $lang ): string {
19
+ $translations = Storage::get( $post_id, 'post' );
20
+ return (string) ( $translations[ $field ][ $lang ] ?? '' );
21
+ }
22
+
23
+ public static function posts_lang( array $post_ids, string $lang ): array {
24
+ $rows = Storage::get_many( $post_ids, 'post' );
25
+ $result = [];
26
+ foreach ( $rows as $id => $translations ) {
27
+ $result[ $id ] = self::extract_lang( $translations, $lang );
28
+ }
29
+ return $result;
30
+ }
31
+
32
+ public static function term( int $term_id ): array {
33
+ return Storage::get( $term_id, 'term' );
34
+ }
35
+
36
+ public static function term_lang( int $term_id, string $lang ): array {
37
+ return self::extract_lang( Storage::get( $term_id, 'term' ), $lang );
38
+ }
39
+
40
+ public static function term_field( int $term_id, string $field, string $lang ): string {
41
+ $translations = Storage::get( $term_id, 'term' );
42
+ return (string) ( $translations[ $field ][ $lang ] ?? '' );
43
+ }
44
+
45
+ public static function terms_lang( array $term_ids, string $lang ): array {
46
+ $rows = Storage::get_many( $term_ids, 'term' );
47
+ $result = [];
48
+ foreach ( $rows as $id => $translations ) {
49
+ $result[ $id ] = self::extract_lang( $translations, $lang );
50
+ }
51
+ return $result;
52
+ }
53
+
54
+ private static function extract_lang( array $translations, string $lang ): array {
55
+ $out = [];
56
+ foreach ( $translations as $key => $langs ) {
57
+ if ( isset( $langs[ $lang ] ) && $langs[ $lang ] !== '' ) {
58
+ $out[ $key ] = $langs[ $lang ];
59
+ }
60
+ }
61
+ return $out;
62
+ }
63
+ }
@@ -0,0 +1,83 @@
1
+ <?php
2
+ namespace Platform\Editors\Translator;
3
+
4
+ use Platform\Languages;
5
+ use Platform\Platform;
6
+
7
+ defined( 'ABSPATH' ) || exit;
8
+
9
+ class Bar {
10
+
11
+ public static function register(): void {
12
+ add_action( 'admin_bar_menu', [ __CLASS__, 'add_admin_bar_node' ], 100 );
13
+ }
14
+
15
+ public static function add_admin_bar_node( \WP_Admin_Bar $wp_admin_bar ): void {
16
+ if ( ! current_user_can( 'manage_options' ) ) {
17
+ return;
18
+ }
19
+
20
+ $object_id = 0;
21
+ $object_type = '';
22
+ $tab = '';
23
+
24
+ if ( is_admin() ) {
25
+ $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
26
+ if ( $screen ) {
27
+ if ( $screen->base === 'post' && isset( $_GET['post'] ) ) {
28
+ $object_id = (int) $_GET['post'];
29
+ $object_type = 'post';
30
+ $tab = get_post_type( $object_id );
31
+ } elseif ( $screen->base === 'term' && isset( $_GET['tag_ID'] ) ) {
32
+ $object_id = (int) $_GET['tag_ID'];
33
+ $object_type = 'term';
34
+ if ( isset( $_GET['taxonomy'] ) ) {
35
+ $tab = sanitize_key( $_GET['taxonomy'] );
36
+ }
37
+ }
38
+ }
39
+ } else {
40
+ if ( is_singular() ) {
41
+ $object_id = get_queried_object_id();
42
+ $object_type = 'post';
43
+ $tab = get_post_type( $object_id );
44
+ } elseif ( is_tax() || is_category() || is_tag() ) {
45
+ $object_id = get_queried_object_id();
46
+ $object_type = 'term';
47
+ $term = get_queried_object();
48
+ if ( $term ) {
49
+ $tab = $term->taxonomy;
50
+ }
51
+ }
52
+ }
53
+
54
+ if ( ! $object_id || ! $tab ) {
55
+ return;
56
+ }
57
+
58
+ $post_types = Fields::translatable_post_types();
59
+ $taxonomies = Fields::translatable_taxonomies();
60
+
61
+ if ( ! isset( $post_types[ $tab ] ) && ! isset( $taxonomies[ $tab ] ) ) {
62
+ return;
63
+ }
64
+
65
+ $edit_url = add_query_arg(
66
+ [
67
+ 'page' => 'content-translator',
68
+ 'ct_tab' => $tab,
69
+ 'ct_edit' => $object_id,
70
+ ],
71
+ admin_url( 'admin.php' )
72
+ );
73
+
74
+ $wp_admin_bar->add_node( [
75
+ 'id' => 'ct-translate',
76
+ 'title' => '<span class="ab-icon dashicons dashicons-translation" style="margin-top:2px;"></span><span class="ab-label">Translate Content</span>',
77
+ 'href' => $edit_url,
78
+ 'meta' => [
79
+ 'title' => 'Translate this content',
80
+ ],
81
+ ] );
82
+ }
83
+ }
@@ -0,0 +1,273 @@
1
+ <?php
2
+ namespace Platform\Editors\Translator;
3
+
4
+ use Platform\Languages;
5
+ use Platform\Platform;
6
+
7
+ defined( 'ABSPATH' ) || exit;
8
+
9
+ class Fields {
10
+ public static array $default_skip_meta = [
11
+ '_edit_lock', '_edit_last', '_wp_old_slug', '_wp_page_template',
12
+ '_thumbnail_id', '_wp_attached_file', '_wp_attachment_metadata',
13
+ '_wp_trash_meta_status', '_wp_trash_meta_time', '_wp_desired_post_slug',
14
+ '_product_attributes', '_default_attributes', '_variation_description',
15
+ '_children', '_price', '_regular_price', '_sale_price',
16
+ '_stock', '_stock_status', '_manage_stock', '_backorders',
17
+ '_virtual', '_downloadable', '_tax_status', '_tax_class',
18
+ ];
19
+
20
+ public static array $default_allow_private_meta = [
21
+ '_audiobook_chapters',
22
+ ];
23
+
24
+ public static function get_skip_meta(): array {
25
+ $saved = get_option( 'ct_skip_meta', null );
26
+ if ( $saved === null ) {
27
+ update_option( 'ct_skip_meta', self::$default_skip_meta, false );
28
+ return self::$default_skip_meta;
29
+ }
30
+ return is_array( $saved ) ? $saved : self::$default_skip_meta;
31
+ }
32
+
33
+ public static function get_allow_private_meta(): array {
34
+ $saved = get_option( 'ct_allow_private_meta', null );
35
+ if ( $saved === null ) {
36
+ update_option( 'ct_allow_private_meta', self::$default_allow_private_meta, false );
37
+ return self::$default_allow_private_meta;
38
+ }
39
+ return is_array( $saved ) ? $saved : self::$default_allow_private_meta;
40
+ }
41
+
42
+ public static function for_post( int $post_id ): array {
43
+ $post = get_post( $post_id );
44
+ $fields = [];
45
+
46
+ if ( ! $post ) {
47
+ return [];
48
+ }
49
+
50
+ $core = [
51
+ 'post_title' => 'Title',
52
+ 'post_content' => 'Content',
53
+ 'post_excerpt' => 'Excerpt',
54
+ ];
55
+
56
+ foreach ( $core as $field => $label ) {
57
+ $val = $post->$field ?? '';
58
+ if ( self::is_translatable_string( $val ) ) {
59
+ $fields[] = [
60
+ 'key' => $field,
61
+ 'label' => $label,
62
+ 'original' => $val,
63
+ 'type' => 'core',
64
+ ];
65
+ }
66
+ }
67
+
68
+ $meta = get_post_meta( $post_id );
69
+ foreach ( $meta as $key => $values ) {
70
+ if ( in_array( $key, self::get_skip_meta(), true ) ) {
71
+ continue;
72
+ }
73
+
74
+ if ( str_starts_with( $key, '_' ) && ! in_array( $key, self::get_allow_private_meta(), true ) ) {
75
+ continue;
76
+ }
77
+
78
+ $raw = is_array( $values ) ? $values[0] : $values;
79
+ $val = self::maybe_decode( $raw );
80
+
81
+ if ( is_string( $val ) ) {
82
+ if ( self::is_translatable_string( $val ) ) {
83
+ $fields[] = [
84
+ 'key' => 'meta_' . $key,
85
+ 'label' => 'Meta: ' . $key,
86
+ 'original' => $val,
87
+ 'type' => 'meta',
88
+ 'meta_key' => $key,
89
+ ];
90
+ }
91
+ } elseif ( is_array( $val ) || is_object( $val ) ) {
92
+ $leaves = self::extract_leaf_strings( (array) $val );
93
+ foreach ( $leaves as $path => $leaf_val ) {
94
+ $fields[] = [
95
+ 'key' => 'meta_' . $key . '.' . $path,
96
+ 'label' => $key . ' › ' . str_replace( '.', ' › ', $path ),
97
+ 'original' => $leaf_val,
98
+ 'type' => 'meta_json',
99
+ 'meta_key' => $key,
100
+ 'json_path'=> $path,
101
+ ];
102
+ }
103
+ }
104
+ }
105
+
106
+ return $fields;
107
+ }
108
+
109
+ public static function for_term( int $term_id, string $taxonomy ): array {
110
+ $term = get_term( $term_id, $taxonomy );
111
+ $fields = [];
112
+
113
+ if ( ! $term || is_wp_error( $term ) ) {
114
+ return [];
115
+ }
116
+
117
+ $core = [
118
+ 'name' => 'Name',
119
+ 'description' => 'Description',
120
+ ];
121
+
122
+ foreach ( $core as $field => $label ) {
123
+ $val = $term->$field ?? '';
124
+ if ( self::is_translatable_string( $val ) ) {
125
+ $fields[] = [
126
+ 'key' => $field,
127
+ 'label' => $label,
128
+ 'original' => $val,
129
+ 'type' => 'core',
130
+ ];
131
+ }
132
+ }
133
+
134
+ $meta = get_term_meta( $term_id );
135
+ foreach ( $meta as $key => $values ) {
136
+ if ( in_array( $key, self::get_skip_meta(), true ) ) {
137
+ continue;
138
+ }
139
+ if ( str_starts_with( $key, '_' ) && ! in_array( $key, self::get_allow_private_meta(), true ) ) {
140
+ continue;
141
+ }
142
+
143
+ $raw = is_array( $values ) ? $values[0] : $values;
144
+ $val = self::maybe_decode( $raw );
145
+
146
+ if ( is_string( $val ) ) {
147
+ if ( self::is_translatable_string( $val ) ) {
148
+ $fields[] = [
149
+ 'key' => 'meta_' . $key,
150
+ 'label' => 'Meta: ' . $key,
151
+ 'original' => $val,
152
+ 'type' => 'meta',
153
+ 'meta_key' => $key,
154
+ ];
155
+ }
156
+ } elseif ( is_array( $val ) || is_object( $val ) ) {
157
+ $leaves = self::extract_leaf_strings( (array) $val );
158
+ foreach ( $leaves as $path => $leaf_val ) {
159
+ $fields[] = [
160
+ 'key' => 'meta_' . $key . '.' . $path,
161
+ 'label' => $key . ' › ' . str_replace( '.', ' › ', $path ),
162
+ 'original' => $leaf_val,
163
+ 'type' => 'meta_json',
164
+ 'meta_key' => $key,
165
+ 'json_path'=> $path,
166
+ ];
167
+ }
168
+ }
169
+ }
170
+
171
+ return $fields;
172
+ }
173
+
174
+ private static array $skip_keys = [
175
+ 'audio_url', 'image', 'image_url', 'thumbnail', 'thumbnail_url',
176
+ 'file', 'file_url', 'url', 'link', 'href', 'src', 'icon', 'icon_url',
177
+ 'background', 'background_image', 'photo', 'video', 'video_url',
178
+ 'attachment', 'attachment_url', 'download_url',
179
+ 'id', 'ID', 'post_id', 'term_id', 'user_id', 'order', 'sort',
180
+ 'allow_download', 'enable', 'enabled', 'disabled', 'status',
181
+ 'color', 'colour', 'hex', 'rgb',
182
+ 'date', 'time', 'timestamp', 'created_at', 'updated_at', 'slug', 'key',
183
+ ];
184
+
185
+ private static function extract_leaf_strings( array $data, string $prefix = '' ): array {
186
+ $result = [];
187
+
188
+ foreach ( $data as $key => $value ) {
189
+ if ( in_array( (string) $key, self::$skip_keys, true ) ) {
190
+ continue;
191
+ }
192
+
193
+ $path = $prefix !== '' ? $prefix . '.' . $key : (string) $key;
194
+
195
+ if ( is_array( $value ) || is_object( $value ) ) {
196
+ $result = array_merge( $result, self::extract_leaf_strings( (array) $value, $path ) );
197
+ } elseif ( is_string( $value ) ) {
198
+ $decoded = self::maybe_decode( $value );
199
+ if ( is_array( $decoded ) || is_object( $decoded ) ) {
200
+ $result = array_merge( $result, self::extract_leaf_strings( (array) $decoded, $path ) );
201
+ } elseif ( self::is_translatable_string( $value ) ) {
202
+ $result[ $path ] = $value;
203
+ }
204
+ }
205
+ }
206
+
207
+ return $result;
208
+ }
209
+
210
+ private static function maybe_decode( $raw ): mixed {
211
+ if ( ! is_string( $raw ) ) {
212
+ return $raw;
213
+ }
214
+
215
+ if ( is_serialized( $raw ) ) {
216
+ $unserialized = @unserialize( $raw );
217
+ if ( $unserialized !== false ) {
218
+ return $unserialized;
219
+ }
220
+ }
221
+
222
+ if ( str_starts_with( ltrim( $raw ), '{' ) || str_starts_with( ltrim( $raw ), '[' ) ) {
223
+ $decoded = json_decode( $raw, true );
224
+ if ( json_last_error() === JSON_ERROR_NONE && ( is_array( $decoded ) || is_object( $decoded ) ) ) {
225
+ return $decoded;
226
+ }
227
+ }
228
+
229
+ return $raw;
230
+ }
231
+
232
+ private static function is_translatable_string( $val ): bool {
233
+ if ( ! is_string( $val ) || trim( $val ) === '' ) {
234
+ return false;
235
+ }
236
+
237
+ if ( filter_var( trim( $val ), FILTER_VALIDATE_URL ) ) {
238
+ return false;
239
+ }
240
+
241
+ if ( is_numeric( trim( $val ) ) ) {
242
+ return false;
243
+ }
244
+
245
+ return true;
246
+ }
247
+
248
+ public static function translatable_post_types(): array {
249
+ $types = get_post_types( [], 'objects' );
250
+ $result = [];
251
+
252
+ foreach ( $types as $type ) {
253
+ if ( ! empty( $type->content_translation ) ) {
254
+ $result[ $type->name ] = $type;
255
+ }
256
+ }
257
+
258
+ return $result;
259
+ }
260
+
261
+ public static function translatable_taxonomies(): array {
262
+ $taxes = get_taxonomies( [], 'objects' );
263
+ $result = [];
264
+
265
+ foreach ( $taxes as $tax ) {
266
+ if ( ! empty( $tax->content_translation ) ) {
267
+ $result[ $tax->name ] = $tax;
268
+ }
269
+ }
270
+
271
+ return $result;
272
+ }
273
+ }
@@ -0,0 +1,131 @@
1
+ <?php
2
+ namespace Platform\Editors\Translator;
3
+
4
+ use Platform\Languages;
5
+ use Platform\Platform;
6
+
7
+ defined( 'ABSPATH' ) || exit;
8
+
9
+ class Storage {
10
+
11
+ public static function table(): string {
12
+ global $wpdb;
13
+
14
+ return $wpdb->prefix . 'translations';
15
+ }
16
+
17
+ public static function create_table() {
18
+ global $wpdb;
19
+
20
+ $charset = $wpdb->get_charset_collate();
21
+
22
+ $sql = "CREATE TABLE IF NOT EXISTS " . Storage::table() . " (
23
+ `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
24
+ `object_id` BIGINT UNSIGNED NOT NULL,
25
+ `object_type` VARCHAR(20) NOT NULL DEFAULT 'post',
26
+ `translations` LONGTEXT NOT NULL,
27
+ `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
28
+ PRIMARY KEY (`id`),
29
+ UNIQUE KEY `object` (`object_id`, `object_type`)
30
+ ) $charset;";
31
+
32
+ require_once ABSPATH . 'wp-admin/includes/upgrade.php';
33
+ dbDelta( $sql );
34
+ }
35
+
36
+ public static function get( int $object_id, string $object_type = 'post' ): array {
37
+ global $wpdb;
38
+
39
+ $row = $wpdb->get_var( $wpdb->prepare(
40
+ "SELECT translations FROM " . Storage::table() . " WHERE object_id = %d AND object_type = %s",
41
+ $object_id,
42
+ $object_type
43
+ ) );
44
+
45
+ if ( ! $row ) {
46
+ return [];
47
+ }
48
+
49
+ $decoded = json_decode( $row, true );
50
+ return is_array( $decoded ) ? $decoded : [];
51
+ }
52
+
53
+ public static function get_many( array $ids, string $object_type = 'post' ): array {
54
+ global $wpdb;
55
+
56
+ if ( empty( $ids ) ) {
57
+ return [];
58
+ }
59
+
60
+ $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) );
61
+ $params = array_merge( $ids, [ $object_type ] );
62
+
63
+ $rows = $wpdb->get_results( $wpdb->prepare(
64
+ "SELECT object_id, translations FROM " . Storage::table() . "
65
+ WHERE object_id IN ($placeholders) AND object_type = %s",
66
+ ...$params
67
+ ) );
68
+
69
+ $map = [];
70
+ foreach ( $rows as $row ) {
71
+ $decoded = json_decode( $row->translations, true );
72
+ $map[ (int) $row->object_id ] = is_array( $decoded ) ? $decoded : [];
73
+ }
74
+
75
+ return $map;
76
+ }
77
+
78
+ public static function save( int $object_id, string $object_type, array $data ): bool {
79
+ global $wpdb;
80
+
81
+ $existing = self::get( $object_id, $object_type );
82
+
83
+ $merged = array_merge( $existing, $data );
84
+
85
+ $result = $wpdb->replace(
86
+ Storage::table(),
87
+ [
88
+ 'object_id' => $object_id,
89
+ 'object_type' => $object_type,
90
+ 'translations' => wp_json_encode( $merged ),
91
+ 'updated_at' => current_time( 'mysql' ),
92
+ ],
93
+ [ '%d', '%s', '%s', '%s' ]
94
+ );
95
+
96
+ return $result !== false;
97
+ }
98
+
99
+ public static function completion( array $ids, string $object_type, array $languages ): array {
100
+ $rows = self::get_many( $ids, $object_type );
101
+ $result = [];
102
+
103
+ foreach ( $ids as $id ) {
104
+ $translations = $rows[ $id ] ?? [];
105
+ $totals = [];
106
+ $filled = array_fill_keys( $languages, 0 );
107
+ $count = 0;
108
+
109
+ foreach ( $translations as $key => $langs ) {
110
+ $count++;
111
+ foreach ( $languages as $lang ) {
112
+ if ( ! empty( $langs[ $lang ] ) ) {
113
+ $filled[ $lang ]++;
114
+ }
115
+ }
116
+ }
117
+
118
+ $percentages = [];
119
+ foreach ( $languages as $lang ) {
120
+ $percentages[ $lang ] = $count > 0 ? (int) round( ( $filled[ $lang ] / $count ) * 100 ) : 0;
121
+ }
122
+
123
+ $result[ $id ] = [
124
+ 'total' => $count,
125
+ 'percentages' => $percentages,
126
+ ];
127
+ }
128
+
129
+ return $result;
130
+ }
131
+ }