@wp-operations/wp-ops 0.3.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 (117) hide show
  1. package/package.json +6 -2
  2. package/src/commands/platform.js +16 -2
  3. package/src/main.js +9 -3
  4. package/wp-operations/CHANGELOG.md +14 -0
  5. package/wp-operations/README.md +39 -0
  6. package/wp-operations/agents/builder/AGENT.md +15 -0
  7. package/wp-operations/assets/admin.css +631 -0
  8. package/wp-operations/assets/admin.js +614 -0
  9. package/wp-operations/assets/app/app.css +1 -0
  10. package/wp-operations/assets/app/app.js +70 -0
  11. package/wp-operations/assets/app/fonts/inter-medium.woff +0 -0
  12. package/wp-operations/assets/app/fonts/inter-regular.woff +0 -0
  13. package/wp-operations/assets/app/fonts/inter-semibold.woff +0 -0
  14. package/wp-operations/assets/app/login.css +42 -0
  15. package/wp-operations/assets/brand/icon.svg +20 -0
  16. package/wp-operations/assets/brand/mark.svg +19 -0
  17. package/wp-operations/assets/editors/audiobook.css +311 -0
  18. package/wp-operations/assets/editors/audiofile.css +286 -0
  19. package/wp-operations/assets/editors/course.css +701 -0
  20. package/wp-operations/assets/editors/course.js +698 -0
  21. package/wp-operations/assets/editors/faq.css +258 -0
  22. package/wp-operations/assets/editors/faq.js +196 -0
  23. package/wp-operations/assets/editors/interactive.css +220 -0
  24. package/wp-operations/assets/editors/interactive.js +378 -0
  25. package/wp-operations/assets/editors/media-sources.css +3 -0
  26. package/wp-operations/assets/editors/media-sources.js +40 -0
  27. package/wp-operations/assets/editors/open-media.css +114 -0
  28. package/wp-operations/assets/editors/open-media.js +182 -0
  29. package/wp-operations/assets/editors/thumbnails.css +335 -0
  30. package/wp-operations/assets/editors/thumbnails.js +447 -0
  31. package/wp-operations/assets/editors/translator.css +818 -0
  32. package/wp-operations/functions.php +24 -0
  33. package/wp-operations/index.php +14 -0
  34. package/wp-operations/platform.json +1836 -0
  35. package/wp-operations/skills/ai-development-guardrails/SKILL.md +49 -0
  36. package/wp-operations/skills/gutenberg-reference/SKILL.md +45 -0
  37. package/wp-operations/skills/js-tooling-for-themes/SKILL.md +51 -0
  38. package/wp-operations/skills/tailwind-for-wp/SKILL.md +62 -0
  39. package/wp-operations/skills/wp-clean-classic-themes/SKILL.md +112 -0
  40. package/wp-operations/skills/wp-psr4-plugins/SKILL.md +2043 -0
  41. package/wp-operations/skills/wp-security/SKILL.md +90 -0
  42. package/wp-operations/src/Admin.php +78 -0
  43. package/wp-operations/src/App/Access.php +57 -0
  44. package/wp-operations/src/App/Admin.php +442 -0
  45. package/wp-operations/src/App/App.php +157 -0
  46. package/wp-operations/src/App/Apps.php +379 -0
  47. package/wp-operations/src/App/Bundle.php +111 -0
  48. package/wp-operations/src/App/Contents.php +676 -0
  49. package/wp-operations/src/App/Engine.php +554 -0
  50. package/wp-operations/src/App/Github.php +308 -0
  51. package/wp-operations/src/App/Module.php +23 -0
  52. package/wp-operations/src/App/Ops.php +470 -0
  53. package/wp-operations/src/App/Packages.php +289 -0
  54. package/wp-operations/src/App/Rest.php +539 -0
  55. package/wp-operations/src/App/Sandboxes.php +206 -0
  56. package/wp-operations/src/App/Store.php +104 -0
  57. package/wp-operations/src/App/Usage.php +268 -0
  58. package/wp-operations/src/Config.php +249 -0
  59. package/wp-operations/src/Content/Editor.php +223 -0
  60. package/wp-operations/src/Content/Fields.php +681 -0
  61. package/wp-operations/src/Content/Scoring.php +253 -0
  62. package/wp-operations/src/Content/Structure.php +293 -0
  63. package/wp-operations/src/Content/Types.php +150 -0
  64. package/wp-operations/src/Content/Uploads.php +50 -0
  65. package/wp-operations/src/Editors/AudiobookAuthor.php +61 -0
  66. package/wp-operations/src/Editors/AudiobookChapters.php +632 -0
  67. package/wp-operations/src/Editors/AudiobookCover.php +235 -0
  68. package/wp-operations/src/Editors/Audiofile.php +462 -0
  69. package/wp-operations/src/Editors/Course.php +784 -0
  70. package/wp-operations/src/Editors/Ebook.php +242 -0
  71. package/wp-operations/src/Editors/Editors.php +44 -0
  72. package/wp-operations/src/Editors/Faq.php +237 -0
  73. package/wp-operations/src/Editors/Files.php +139 -0
  74. package/wp-operations/src/Editors/Interactive/Builder.php +103 -0
  75. package/wp-operations/src/Editors/Interactive/Card.php +76 -0
  76. package/wp-operations/src/Editors/Interactive/Editor.php +42 -0
  77. package/wp-operations/src/Editors/Interactive/Field.php +272 -0
  78. package/wp-operations/src/Editors/Interactive/Map.php +84 -0
  79. package/wp-operations/src/Editors/Interactive/Models.php +227 -0
  80. package/wp-operations/src/Editors/Interactive/Report.php +59 -0
  81. package/wp-operations/src/Editors/Interactive/Save.php +323 -0
  82. package/wp-operations/src/Editors/Interactive/Scoring.php +244 -0
  83. package/wp-operations/src/Editors/Interactive/Structure.php +195 -0
  84. package/wp-operations/src/Editors/Legal.php +235 -0
  85. package/wp-operations/src/Editors/OpenMedia.php +140 -0
  86. package/wp-operations/src/Editors/OriginalTitle.php +112 -0
  87. package/wp-operations/src/Editors/Sources.php +28 -0
  88. package/wp-operations/src/Editors/Thumbnails/Admin.php +306 -0
  89. package/wp-operations/src/Editors/Thumbnails/Ajax.php +163 -0
  90. package/wp-operations/src/Editors/Thumbnails/Box.php +188 -0
  91. package/wp-operations/src/Editors/Thumbnails/Storage.php +228 -0
  92. package/wp-operations/src/Editors/Translator/Admin.php +913 -0
  93. package/wp-operations/src/Editors/Translator/Api.php +63 -0
  94. package/wp-operations/src/Editors/Translator/Bar.php +83 -0
  95. package/wp-operations/src/Editors/Translator/Fields.php +273 -0
  96. package/wp-operations/src/Editors/Translator/Storage.php +131 -0
  97. package/wp-operations/src/Editors/Translator/Thumbnail.php +215 -0
  98. package/wp-operations/src/Identity.php +104 -0
  99. package/wp-operations/src/Languages.php +45 -0
  100. package/wp-operations/src/Lockdown.php +85 -0
  101. package/wp-operations/src/Modules/Apps/Admin.php +472 -0
  102. package/wp-operations/src/Modules/Apps/Api.php +193 -0
  103. package/wp-operations/src/Modules/Apps/Apps.php +279 -0
  104. package/wp-operations/src/Modules/Apps/Formatters.php +478 -0
  105. package/wp-operations/src/Modules/Apps/Schemes.php +248 -0
  106. package/wp-operations/src/Modules/Apps/Table.php +101 -0
  107. package/wp-operations/src/Modules/Thumbnails.php +68 -0
  108. package/wp-operations/src/Modules/Translations.php +133 -0
  109. package/wp-operations/src/Platform.php +59 -0
  110. package/wp-operations/src/Setup.php +273 -0
  111. package/wp-operations/src/Status.php +99 -0
  112. package/wp-operations/src/Storage/Client.php +168 -0
  113. package/wp-operations/src/Storage/Offload.php +120 -0
  114. package/wp-operations/src/Storage/Settings.php +166 -0
  115. package/wp-operations/src/Storage/Storage.php +35 -0
  116. package/wp-operations/src/Updates.php +151 -0
  117. package/wp-operations/style.css +11 -0
@@ -0,0 +1,163 @@
1
+ <?php
2
+ namespace Platform\Editors\Thumbnails;
3
+
4
+ use Platform\Languages;
5
+ use Platform\Platform;
6
+
7
+ defined( 'ABSPATH' ) || exit;
8
+
9
+ class Ajax {
10
+ public static function register(): void {
11
+ add_action( 'wp_ajax_thumbnails_search_posts', [ __CLASS__, 'search_posts' ] );
12
+ add_action( 'wp_ajax_thumbnails_save_thumbnail', [ __CLASS__, 'save_thumbnail' ] );
13
+ add_action( 'wp_ajax_thumbnails_remove_thumbnail', [ __CLASS__, 'remove_thumbnail' ] );
14
+ }
15
+
16
+ private static function check_access(): void {
17
+ if ( ! current_user_can( 'manage_options' ) ) {
18
+ wp_send_json_error( [ 'message' => 'Forbidden.' ], 403 );
19
+ }
20
+ if ( ! wp_verify_nonce( $_REQUEST['_nonce'] ?? '', 'thumbnails_ajax' ) ) {
21
+ wp_send_json_error( [ 'message' => 'Invalid nonce.' ], 403 );
22
+ }
23
+ }
24
+
25
+ public static function search_posts(): void {
26
+ self::check_access();
27
+
28
+ $post_type = sanitize_key( $_POST['post_type'] ?? '' );
29
+ $search = sanitize_text_field( wp_unslash( $_POST['search'] ?? '' ) );
30
+ $paged = max( 1, (int) ( $_POST['paged'] ?? 1 ) );
31
+ $group_id = (int) ( $_POST['group_id'] ?? 0 );
32
+ $tax_filters = $_POST['tax_filters'] ?? [];
33
+ $per_page = 20;
34
+
35
+ if ( ! $post_type || ! post_type_exists( $post_type ) ) {
36
+ wp_send_json_error( [ 'message' => 'Invalid post type.' ] );
37
+ }
38
+
39
+ $args = [
40
+ 'post_type' => $post_type,
41
+ 'post_status' => 'publish',
42
+ 'posts_per_page' => $per_page,
43
+ 'paged' => $paged,
44
+ 'orderby' => 'title',
45
+ 'order' => 'ASC',
46
+ 'no_found_rows' => false,
47
+ ];
48
+
49
+ if ( $search !== '' ) {
50
+ $args['s'] = $search;
51
+ }
52
+
53
+ if ( ! empty( $tax_filters ) && is_array( $tax_filters ) ) {
54
+ $args['tax_query'] = [ 'relation' => 'AND' ];
55
+ foreach ( $tax_filters as $tax => $ids ) {
56
+ $ids = array_filter( array_map( 'intval', (array) $ids ) );
57
+ if ( ! empty( $ids ) ) {
58
+ $args['tax_query'][] = [
59
+ 'taxonomy' => sanitize_key( $tax ),
60
+ 'terms' => $ids,
61
+ 'field' => 'term_id',
62
+ ];
63
+ }
64
+ }
65
+ }
66
+
67
+ $query = new \WP_Query( $args );
68
+ $posts = $query->posts;
69
+ $total = (int) $query->found_posts;
70
+ $pages = (int) ceil( $total / $per_page );
71
+ $has_more = $paged < $pages;
72
+
73
+ $languages = Languages::all();
74
+ $site_lang = Languages::default();
75
+
76
+ $ids = array_map( fn( $p ) => $p->ID, $posts );
77
+ $items = [];
78
+ if ( $group_id && ! empty( $ids ) ) {
79
+ $items = Storage::get_group_items( $group_id, $ids, $post_type, null );
80
+ }
81
+
82
+ $data = [];
83
+ foreach ( $posts as $post ) {
84
+ $post_items = $items[ $post->ID ] ?? [];
85
+
86
+ $thumbnails = [];
87
+
88
+ $default = $post_items['thumbnail'] ?? null;
89
+ $thumbnails['thumbnail'] = [
90
+ 'attachment_id' => $default ? (int) $default['attachment_id'] : 0,
91
+ 'url' => $default ? ( wp_get_attachment_image_url( (int) $default['attachment_id'], 'medium' ) ?: '' ) : '',
92
+ ];
93
+
94
+ foreach ( $languages as $code => $name ) {
95
+ if ( $code === $site_lang ) {
96
+ continue;
97
+ }
98
+ $key = 'thumbnail_' . $code;
99
+ $lang = $post_items[ $key ] ?? null;
100
+ $thumbnails[ $key ] = [
101
+ 'attachment_id' => $lang ? (int) $lang['attachment_id'] : 0,
102
+ 'url' => $lang ? ( wp_get_attachment_image_url( (int) $lang['attachment_id'], 'medium' ) ?: '' ) : '',
103
+ ];
104
+ }
105
+
106
+ $data[] = [
107
+ 'id' => $post->ID,
108
+ 'title' => $post->post_title ?: '(no title)',
109
+ 'thumbnails' => $thumbnails,
110
+ ];
111
+ }
112
+
113
+ wp_send_json_success( [
114
+ 'posts' => $data,
115
+ 'has_more' => $has_more,
116
+ 'total' => $total,
117
+ 'paged' => $paged,
118
+ ] );
119
+ }
120
+
121
+ public static function save_thumbnail(): void {
122
+ self::check_access();
123
+
124
+ $group_id = (int) ( $_POST['group_id'] ?? 0 );
125
+ $object_id = (int) ( $_POST['object_id'] ?? 0 );
126
+ $attachment_id = (int) ( $_POST['attachment_id'] ?? 0 );
127
+ $asset_type = sanitize_key( $_POST['asset_type'] ?? 'thumbnail' );
128
+
129
+ if ( ! $group_id || ! $object_id || ! $attachment_id ) {
130
+ wp_send_json_error( [ 'message' => 'Missing required fields.' ] );
131
+ }
132
+
133
+ $group = Storage::get_group( $group_id );
134
+ if ( ! $group ) {
135
+ wp_send_json_error( [ 'message' => 'Group not found.' ] );
136
+ }
137
+
138
+ Storage::save_group_item( $group_id, $object_id, $attachment_id, get_post_type( $object_id ) ?: 'post', $asset_type );
139
+
140
+ $thumb_url = wp_get_attachment_image_url( $attachment_id, 'medium' ) ?: '';
141
+
142
+ wp_send_json_success( [
143
+ 'thumb_url' => $thumb_url,
144
+ 'message' => 'Thumbnail saved.',
145
+ ] );
146
+ }
147
+
148
+ public static function remove_thumbnail(): void {
149
+ self::check_access();
150
+
151
+ $group_id = (int) ( $_POST['group_id'] ?? 0 );
152
+ $object_id = (int) ( $_POST['object_id'] ?? 0 );
153
+ $asset_type = sanitize_key( $_POST['asset_type'] ?? 'thumbnail' );
154
+
155
+ if ( ! $group_id || ! $object_id ) {
156
+ wp_send_json_error( [ 'message' => 'Missing required fields.' ] );
157
+ }
158
+
159
+ Storage::remove_group_item( $group_id, $object_id, get_post_type( $object_id ) ?: 'post', $asset_type );
160
+
161
+ wp_send_json_success( [ 'message' => 'Thumbnail removed.' ] );
162
+ }
163
+ }
@@ -0,0 +1,188 @@
1
+ <?php
2
+ namespace Platform\Editors\Thumbnails;
3
+
4
+ use Platform\Languages;
5
+ use Platform\Platform;
6
+
7
+ defined( 'ABSPATH' ) || exit;
8
+
9
+ class Box {
10
+ public static function register(): void {
11
+ add_action( 'admin_enqueue_scripts', [ __CLASS__, 'enqueue' ] );
12
+ add_filter( 'admin_post_thumbnail_html', [ __CLASS__, 'append_branding_button' ], 10, 2 );
13
+ add_action( 'admin_footer', [ __CLASS__, 'render_modal' ] );
14
+ add_action( 'thumbnail_after_buttons', [ __CLASS__, 'render_ct_button' ] );
15
+ }
16
+
17
+ public static function append_branding_button( string $html, int $post_id ): string {
18
+ if ( ! $post_id ) {
19
+ return $html;
20
+ }
21
+ $screen = get_current_screen();
22
+ if ( ! $screen || ! self::is_service_post_type( $screen->post_type ) ) {
23
+ return $html;
24
+ }
25
+ $groups = Storage::get_groups();
26
+ if ( empty( $groups ) ) {
27
+ return $html;
28
+ }
29
+ $html .= self::get_button_html();
30
+ return $html;
31
+ }
32
+
33
+ public static function render_ct_button( \WP_Post $post ): void {
34
+ $groups = Storage::get_groups();
35
+ if ( empty( $groups ) ) {
36
+ return;
37
+ }
38
+ echo self::get_button_html();
39
+ }
40
+
41
+ private static function get_button_html(): string {
42
+ return '<p><button type="button" class="thumbnails-branding-btn" id="thumbnails-branding-btn">' .
43
+ '<span class="dashicons dashicons-format-image"></span> Add Branding Alt</button></p>';
44
+ }
45
+
46
+ public static function enqueue( string $hook ): void {
47
+ if ( ! in_array( $hook, [ 'post.php', 'post-new.php' ], true ) ) {
48
+ return;
49
+ }
50
+
51
+ $screen = get_current_screen();
52
+ if ( ! $screen || ! self::is_service_post_type( $screen->post_type ) ) {
53
+ return;
54
+ }
55
+
56
+ wp_enqueue_media();
57
+
58
+ $css_ver = Platform::asset_version( 'assets/editors/thumbnails.css' );
59
+ $js_ver = Platform::asset_version( 'assets/editors/thumbnails.js' );
60
+
61
+ wp_enqueue_style(
62
+ 'thumbnails-meta-box',
63
+ Platform::url( 'assets/editors/thumbnails.css' ),
64
+ [],
65
+ $css_ver
66
+ );
67
+
68
+ wp_enqueue_script(
69
+ 'thumbnails-meta-box',
70
+ Platform::url( 'assets/editors/thumbnails.js' ),
71
+ [ 'jquery' ],
72
+ $js_ver,
73
+ true
74
+ );
75
+
76
+ $languages = Languages::all();
77
+ $site_lang = Languages::default();
78
+
79
+ $groups = Storage::get_groups();
80
+ $group_data = [];
81
+ foreach ( $groups as $g ) {
82
+ $gid = (int) $g['id'];
83
+ $thumbnails = [];
84
+
85
+ if ( isset( $_GET['post'] ) ) {
86
+ $post_id = (int) $_GET['post'];
87
+ $object_type = get_post_type( $post_id ) ?: 'post';
88
+
89
+ if ( ! empty( $languages ) ) {
90
+ $all_items = Storage::get_group_items( $gid, [ $post_id ], $object_type, null );
91
+ $post_items = $all_items[ $post_id ] ?? [];
92
+
93
+ $default = $post_items['thumbnail'] ?? null;
94
+ $thumbnails['thumbnail'] = [
95
+ 'attachment_id' => $default ? (int) $default['attachment_id'] : 0,
96
+ 'url' => $default ? ( wp_get_attachment_image_url( (int) $default['attachment_id'], 'medium' ) ?: '' ) : '',
97
+ ];
98
+ foreach ( $languages as $code => $name ) {
99
+ if ( $code === $site_lang ) {
100
+ continue;
101
+ }
102
+ $key = 'thumbnail_' . $code;
103
+ $item = $post_items[ $key ] ?? null;
104
+ $thumbnails[ $key ] = [
105
+ 'attachment_id' => $item ? (int) $item['attachment_id'] : 0,
106
+ 'url' => $item ? ( wp_get_attachment_image_url( (int) $item['attachment_id'], 'medium' ) ?: '' ) : '',
107
+ ];
108
+ }
109
+ } else {
110
+ $item = Storage::get_group_item( $gid, $post_id, $object_type );
111
+ $thumb_url = '';
112
+ if ( $item && ! empty( $item['attachment_id'] ) ) {
113
+ $thumb_url = wp_get_attachment_image_url( (int) $item['attachment_id'], 'medium' ) ?: '';
114
+ }
115
+ $thumbnails['thumbnail'] = [
116
+ 'attachment_id' => $item ? (int) $item['attachment_id'] : 0,
117
+ 'url' => $thumb_url,
118
+ ];
119
+ }
120
+ }
121
+
122
+ $group_data[] = [
123
+ 'id' => $gid,
124
+ 'name' => esc_html( $g['name'] ),
125
+ 'thumbnails' => $thumbnails,
126
+ ];
127
+ }
128
+
129
+ wp_localize_script( 'thumbnails-meta-box', 'ThumbnailsData', [
130
+ 'ajax_url' => admin_url( 'admin-ajax.php' ),
131
+ 'admin_url' => admin_url(),
132
+ 'nonce' => wp_create_nonce( 'thumbnails_ajax' ),
133
+ 'groups' => $group_data,
134
+ 'post_id' => (int) ( $_GET['post'] ?? 0 ),
135
+ 'languages' => $languages,
136
+ 'site_lang' => $site_lang,
137
+ ] );
138
+ }
139
+
140
+ public static function render_modal(): void {
141
+ $screen = get_current_screen();
142
+ if ( ! $screen || ! in_array( $screen->base, [ 'post', 'post-new' ], true ) ) {
143
+ return;
144
+ }
145
+ if ( ! self::is_service_post_type( $screen->post_type ) ) {
146
+ return;
147
+ }
148
+ $groups = Storage::get_groups();
149
+ if ( empty( $groups ) ) {
150
+ return;
151
+ }
152
+ ?>
153
+ <div id="thumbnails-modal" class="thumbnails-modal" style="display:none;">
154
+ <div class="thumbnails-modal-backdrop"></div>
155
+ <div class="thumbnails-modal-content">
156
+ <div class="thumbnails-modal-header">
157
+ <h2>Thumbnails</h2>
158
+ <button type="button" class="thumbnails-modal-close">&times;</button>
159
+ </div>
160
+ <div class="thumbnails-modal-body">
161
+ <p class="description">Manage thumbnails of this post.</p>
162
+ <div id="thumbnails-modal-groups"></div>
163
+ </div>
164
+ </div>
165
+ </div>
166
+ <?php
167
+ }
168
+
169
+ private static function is_service_post_type( string $post_type ): bool {
170
+ $obj = get_post_type_object( $post_type );
171
+ return $obj && ! empty( $obj->service_content );
172
+ }
173
+
174
+ public static function get_thumbnail_url( int $post_id, string $size = 'large', int $group_id = 0 ): ?string {
175
+ if ( ! $group_id || ! class_exists( 'Storage' ) ) {
176
+ return null;
177
+ }
178
+
179
+ $object_type = get_post_type( $post_id ) ?: 'post';
180
+ $item = Storage::get_group_item( $group_id, $post_id, $object_type );
181
+ if ( ! $item || empty( $item['attachment_id'] ) ) {
182
+ return null;
183
+ }
184
+
185
+ $url = wp_get_attachment_image_url( (int) $item['attachment_id'], $size );
186
+ return $url ?: null;
187
+ }
188
+ }
@@ -0,0 +1,228 @@
1
+ <?php
2
+ namespace Platform\Editors\Thumbnails;
3
+
4
+ use Platform\Languages;
5
+ use Platform\Platform;
6
+
7
+ defined( 'ABSPATH' ) || exit;
8
+
9
+ class Storage {
10
+ const GROUPS_TABLE = 'thumbnail_groups';
11
+ const ITEMS_TABLE = 'thumbnail_items';
12
+
13
+ public static function create_tables() {
14
+ global $wpdb;
15
+
16
+ $charset = $wpdb->get_charset_collate();
17
+
18
+ $groups = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}" . self::GROUPS_TABLE . "` (
19
+ `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
20
+ `name` VARCHAR(255) NOT NULL DEFAULT '',
21
+ `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
22
+ PRIMARY KEY (`id`)
23
+ ) $charset;";
24
+
25
+ $items = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}" . self::ITEMS_TABLE . "` (
26
+ `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
27
+ `group_id` BIGINT UNSIGNED NOT NULL,
28
+ `object_id` BIGINT UNSIGNED NOT NULL,
29
+ `object_type` VARCHAR(20) NOT NULL DEFAULT 'post',
30
+ `asset_type` VARCHAR(50) NOT NULL DEFAULT 'thumbnail',
31
+ `attachment_id` BIGINT UNSIGNED NOT NULL DEFAULT 0,
32
+ `metadata` LONGTEXT NOT NULL DEFAULT '',
33
+ `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
34
+ PRIMARY KEY (`id`),
35
+ UNIQUE KEY `group_object` (`group_id`, `object_id`, `object_type`, `asset_type`),
36
+ KEY `group_id` (`group_id`),
37
+ KEY `object_id` (`object_id`)
38
+ ) $charset;";
39
+
40
+ require_once ABSPATH . 'wp-admin/includes/upgrade.php';
41
+ dbDelta( $groups );
42
+ dbDelta( $items );
43
+ }
44
+
45
+ public static function get_group( int $id ): ?array {
46
+ global $wpdb;
47
+ $row = $wpdb->get_row(
48
+ $wpdb->prepare(
49
+ "SELECT * FROM `{$wpdb->prefix}" . self::GROUPS_TABLE . "` WHERE id = %d",
50
+ $id
51
+ ),
52
+ ARRAY_A
53
+ );
54
+ return $row ?: null;
55
+ }
56
+
57
+ public static function get_groups(): array {
58
+ global $wpdb;
59
+ return $wpdb->get_results(
60
+ "SELECT * FROM `{$wpdb->prefix}" . self::GROUPS_TABLE . "` ORDER BY created_at DESC",
61
+ ARRAY_A
62
+ ) ?: [];
63
+ }
64
+
65
+ public static function insert_group( string $name ): int|false {
66
+ global $wpdb;
67
+ $ok = $wpdb->insert(
68
+ $wpdb->prefix . self::GROUPS_TABLE,
69
+ [ 'name' => sanitize_text_field( $name ) ],
70
+ [ '%s' ]
71
+ );
72
+ return $ok ? $wpdb->insert_id : false;
73
+ }
74
+
75
+ public static function update_group( int $id, string $name ): bool {
76
+ global $wpdb;
77
+ return (bool) $wpdb->update(
78
+ $wpdb->prefix . self::GROUPS_TABLE,
79
+ [ 'name' => sanitize_text_field( $name ) ],
80
+ [ 'id' => $id ],
81
+ [ '%s' ],
82
+ [ '%d' ]
83
+ );
84
+ }
85
+
86
+ public static function delete_group( int $id ): bool {
87
+ global $wpdb;
88
+ $prefix = $wpdb->prefix;
89
+
90
+ $wpdb->delete( $prefix . self::ITEMS_TABLE, [ 'group_id' => $id ], [ '%d' ] );
91
+ $wpdb->delete( $prefix . self::GROUPS_TABLE, [ 'id' => $id ], [ '%d' ] );
92
+
93
+ return true;
94
+ }
95
+
96
+ public static function clone_group( int $source_id ): int|false {
97
+ global $wpdb;
98
+
99
+ $source = self::get_group( $source_id );
100
+ if ( ! $source ) {
101
+ return false;
102
+ }
103
+
104
+ $new_id = self::insert_group( 'Copy of ' . $source['name'] );
105
+ if ( ! $new_id ) {
106
+ return false;
107
+ }
108
+
109
+ $rows = $wpdb->get_results(
110
+ $wpdb->prepare(
111
+ "SELECT object_id, object_type, asset_type, attachment_id, metadata
112
+ FROM `{$wpdb->prefix}" . self::ITEMS_TABLE . "`
113
+ WHERE group_id = %d",
114
+ $source_id
115
+ ),
116
+ ARRAY_A
117
+ ) ?: [];
118
+
119
+ foreach ( $rows as $row ) {
120
+ $wpdb->insert(
121
+ $wpdb->prefix . self::ITEMS_TABLE,
122
+ [
123
+ 'group_id' => $new_id,
124
+ 'object_id' => (int) $row['object_id'],
125
+ 'object_type' => $row['object_type'],
126
+ 'asset_type' => $row['asset_type'],
127
+ 'attachment_id' => (int) $row['attachment_id'],
128
+ 'metadata' => $row['metadata'],
129
+ ],
130
+ [ '%d', '%d', '%s', '%s', '%d', '%s' ]
131
+ );
132
+ }
133
+
134
+ return $new_id;
135
+ }
136
+
137
+ public static function get_group_item( int $group_id, int $object_id, string $object_type = 'post', string $asset_type = 'thumbnail' ): ?array {
138
+ global $wpdb;
139
+ $row = $wpdb->get_row(
140
+ $wpdb->prepare(
141
+ "SELECT * FROM `{$wpdb->prefix}" . self::ITEMS_TABLE . "`
142
+ WHERE group_id = %d AND object_id = %d AND object_type = %s AND asset_type = %s
143
+ LIMIT 1",
144
+ $group_id, $object_id, $object_type, $asset_type
145
+ ),
146
+ ARRAY_A
147
+ );
148
+ return $row ?: null;
149
+ }
150
+
151
+ public static function get_group_items( int $group_id, array $object_ids = [], string $object_type = 'post', ?string $asset_type = 'thumbnail' ): array {
152
+ global $wpdb;
153
+
154
+ $sql = "SELECT * FROM `{$wpdb->prefix}" . self::ITEMS_TABLE . "`
155
+ WHERE group_id = %d AND object_type = %s";
156
+ $params = [ $group_id, $object_type ];
157
+
158
+ if ( $asset_type !== null ) {
159
+ $sql .= " AND asset_type = %s";
160
+ $params[] = $asset_type;
161
+ }
162
+
163
+ if ( ! empty( $object_ids ) ) {
164
+ $placeholders = implode( ',', array_fill( 0, count( $object_ids ), '%d' ) );
165
+ $sql .= " AND object_id IN ($placeholders)";
166
+ $params = array_merge( $params, $object_ids );
167
+ }
168
+
169
+ $rows = $wpdb->get_results( $wpdb->prepare( $sql, ...$params ), ARRAY_A ) ?: [];
170
+
171
+ if ( $asset_type !== null ) {
172
+ $map = [];
173
+ foreach ( $rows as $r ) {
174
+ $map[ (int) $r['object_id'] ] = $r;
175
+ }
176
+ return $map;
177
+ }
178
+
179
+ $map = [];
180
+ foreach ( $rows as $r ) {
181
+ $map[ (int) $r['object_id'] ][ $r['asset_type'] ] = $r;
182
+ }
183
+ return $map;
184
+ }
185
+
186
+ public static function save_group_item( int $group_id, int $object_id, int $attachment_id, string $object_type = 'post', string $asset_type = 'thumbnail', array $metadata = [] ): bool {
187
+ global $wpdb;
188
+
189
+ $existing = self::get_group_item( $group_id, $object_id, $object_type, $asset_type );
190
+
191
+ $data = [
192
+ 'group_id' => $group_id,
193
+ 'object_id' => $object_id,
194
+ 'object_type' => $object_type,
195
+ 'asset_type' => $asset_type,
196
+ 'attachment_id' => $attachment_id,
197
+ 'metadata' => wp_json_encode( $metadata ),
198
+ ];
199
+
200
+ $formats = [ '%d', '%d', '%s', '%s', '%d', '%s' ];
201
+
202
+ if ( $existing ) {
203
+ return (bool) $wpdb->update(
204
+ $wpdb->prefix . self::ITEMS_TABLE,
205
+ $data,
206
+ [ 'id' => $existing['id'] ],
207
+ $formats,
208
+ [ '%d' ]
209
+ );
210
+ }
211
+
212
+ return (bool) $wpdb->insert( $wpdb->prefix . self::ITEMS_TABLE, $data, $formats );
213
+ }
214
+
215
+ public static function remove_group_item( int $group_id, int $object_id, string $object_type = 'post', string $asset_type = 'thumbnail' ): bool {
216
+ global $wpdb;
217
+ return (bool) $wpdb->delete(
218
+ $wpdb->prefix . self::ITEMS_TABLE,
219
+ [
220
+ 'group_id' => $group_id,
221
+ 'object_id' => $object_id,
222
+ 'object_type' => $object_type,
223
+ 'asset_type' => $asset_type,
224
+ ],
225
+ [ '%d', '%d', '%s', '%s' ]
226
+ );
227
+ }
228
+ }