@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.
Files changed (119) hide show
  1. package/README.md +6 -3
  2. package/package.json +6 -2
  3. package/src/commands/platform.js +58 -2
  4. package/src/commands/start.js +2 -2
  5. package/src/main.js +19 -8
  6. package/wp-operations/CHANGELOG.md +14 -0
  7. package/wp-operations/README.md +39 -0
  8. package/wp-operations/agents/builder/AGENT.md +15 -0
  9. package/wp-operations/assets/admin.css +631 -0
  10. package/wp-operations/assets/admin.js +614 -0
  11. package/wp-operations/assets/app/app.css +1 -0
  12. package/wp-operations/assets/app/app.js +70 -0
  13. package/wp-operations/assets/app/fonts/inter-medium.woff +0 -0
  14. package/wp-operations/assets/app/fonts/inter-regular.woff +0 -0
  15. package/wp-operations/assets/app/fonts/inter-semibold.woff +0 -0
  16. package/wp-operations/assets/app/login.css +42 -0
  17. package/wp-operations/assets/brand/icon.svg +20 -0
  18. package/wp-operations/assets/brand/mark.svg +19 -0
  19. package/wp-operations/assets/editors/audiobook.css +311 -0
  20. package/wp-operations/assets/editors/audiofile.css +286 -0
  21. package/wp-operations/assets/editors/course.css +701 -0
  22. package/wp-operations/assets/editors/course.js +698 -0
  23. package/wp-operations/assets/editors/faq.css +258 -0
  24. package/wp-operations/assets/editors/faq.js +196 -0
  25. package/wp-operations/assets/editors/interactive.css +220 -0
  26. package/wp-operations/assets/editors/interactive.js +378 -0
  27. package/wp-operations/assets/editors/media-sources.css +3 -0
  28. package/wp-operations/assets/editors/media-sources.js +40 -0
  29. package/wp-operations/assets/editors/open-media.css +114 -0
  30. package/wp-operations/assets/editors/open-media.js +182 -0
  31. package/wp-operations/assets/editors/thumbnails.css +335 -0
  32. package/wp-operations/assets/editors/thumbnails.js +447 -0
  33. package/wp-operations/assets/editors/translator.css +818 -0
  34. package/wp-operations/functions.php +24 -0
  35. package/wp-operations/index.php +14 -0
  36. package/wp-operations/platform.json +1836 -0
  37. package/wp-operations/skills/ai-development-guardrails/SKILL.md +49 -0
  38. package/wp-operations/skills/gutenberg-reference/SKILL.md +45 -0
  39. package/wp-operations/skills/js-tooling-for-themes/SKILL.md +51 -0
  40. package/wp-operations/skills/tailwind-for-wp/SKILL.md +62 -0
  41. package/wp-operations/skills/wp-clean-classic-themes/SKILL.md +112 -0
  42. package/wp-operations/skills/wp-psr4-plugins/SKILL.md +2043 -0
  43. package/wp-operations/skills/wp-security/SKILL.md +90 -0
  44. package/wp-operations/src/Admin.php +78 -0
  45. package/wp-operations/src/App/Access.php +57 -0
  46. package/wp-operations/src/App/Admin.php +442 -0
  47. package/wp-operations/src/App/App.php +157 -0
  48. package/wp-operations/src/App/Apps.php +379 -0
  49. package/wp-operations/src/App/Bundle.php +111 -0
  50. package/wp-operations/src/App/Contents.php +676 -0
  51. package/wp-operations/src/App/Engine.php +554 -0
  52. package/wp-operations/src/App/Github.php +308 -0
  53. package/wp-operations/src/App/Module.php +23 -0
  54. package/wp-operations/src/App/Ops.php +470 -0
  55. package/wp-operations/src/App/Packages.php +289 -0
  56. package/wp-operations/src/App/Rest.php +539 -0
  57. package/wp-operations/src/App/Sandboxes.php +206 -0
  58. package/wp-operations/src/App/Store.php +104 -0
  59. package/wp-operations/src/App/Usage.php +268 -0
  60. package/wp-operations/src/Config.php +249 -0
  61. package/wp-operations/src/Content/Editor.php +223 -0
  62. package/wp-operations/src/Content/Fields.php +681 -0
  63. package/wp-operations/src/Content/Scoring.php +253 -0
  64. package/wp-operations/src/Content/Structure.php +293 -0
  65. package/wp-operations/src/Content/Types.php +150 -0
  66. package/wp-operations/src/Content/Uploads.php +50 -0
  67. package/wp-operations/src/Editors/AudiobookAuthor.php +61 -0
  68. package/wp-operations/src/Editors/AudiobookChapters.php +632 -0
  69. package/wp-operations/src/Editors/AudiobookCover.php +235 -0
  70. package/wp-operations/src/Editors/Audiofile.php +462 -0
  71. package/wp-operations/src/Editors/Course.php +784 -0
  72. package/wp-operations/src/Editors/Ebook.php +242 -0
  73. package/wp-operations/src/Editors/Editors.php +44 -0
  74. package/wp-operations/src/Editors/Faq.php +237 -0
  75. package/wp-operations/src/Editors/Files.php +139 -0
  76. package/wp-operations/src/Editors/Interactive/Builder.php +103 -0
  77. package/wp-operations/src/Editors/Interactive/Card.php +76 -0
  78. package/wp-operations/src/Editors/Interactive/Editor.php +42 -0
  79. package/wp-operations/src/Editors/Interactive/Field.php +272 -0
  80. package/wp-operations/src/Editors/Interactive/Map.php +84 -0
  81. package/wp-operations/src/Editors/Interactive/Models.php +227 -0
  82. package/wp-operations/src/Editors/Interactive/Report.php +59 -0
  83. package/wp-operations/src/Editors/Interactive/Save.php +323 -0
  84. package/wp-operations/src/Editors/Interactive/Scoring.php +244 -0
  85. package/wp-operations/src/Editors/Interactive/Structure.php +195 -0
  86. package/wp-operations/src/Editors/Legal.php +235 -0
  87. package/wp-operations/src/Editors/OpenMedia.php +140 -0
  88. package/wp-operations/src/Editors/OriginalTitle.php +112 -0
  89. package/wp-operations/src/Editors/Sources.php +28 -0
  90. package/wp-operations/src/Editors/Thumbnails/Admin.php +306 -0
  91. package/wp-operations/src/Editors/Thumbnails/Ajax.php +163 -0
  92. package/wp-operations/src/Editors/Thumbnails/Box.php +188 -0
  93. package/wp-operations/src/Editors/Thumbnails/Storage.php +228 -0
  94. package/wp-operations/src/Editors/Translator/Admin.php +913 -0
  95. package/wp-operations/src/Editors/Translator/Api.php +63 -0
  96. package/wp-operations/src/Editors/Translator/Bar.php +83 -0
  97. package/wp-operations/src/Editors/Translator/Fields.php +273 -0
  98. package/wp-operations/src/Editors/Translator/Storage.php +131 -0
  99. package/wp-operations/src/Editors/Translator/Thumbnail.php +215 -0
  100. package/wp-operations/src/Identity.php +104 -0
  101. package/wp-operations/src/Languages.php +45 -0
  102. package/wp-operations/src/Lockdown.php +85 -0
  103. package/wp-operations/src/Modules/Apps/Admin.php +472 -0
  104. package/wp-operations/src/Modules/Apps/Api.php +193 -0
  105. package/wp-operations/src/Modules/Apps/Apps.php +279 -0
  106. package/wp-operations/src/Modules/Apps/Formatters.php +478 -0
  107. package/wp-operations/src/Modules/Apps/Schemes.php +248 -0
  108. package/wp-operations/src/Modules/Apps/Table.php +101 -0
  109. package/wp-operations/src/Modules/Thumbnails.php +68 -0
  110. package/wp-operations/src/Modules/Translations.php +133 -0
  111. package/wp-operations/src/Platform.php +59 -0
  112. package/wp-operations/src/Setup.php +273 -0
  113. package/wp-operations/src/Status.php +99 -0
  114. package/wp-operations/src/Storage/Client.php +168 -0
  115. package/wp-operations/src/Storage/Offload.php +120 -0
  116. package/wp-operations/src/Storage/Settings.php +166 -0
  117. package/wp-operations/src/Storage/Storage.php +35 -0
  118. package/wp-operations/src/Updates.php +151 -0
  119. package/wp-operations/style.css +11 -0
@@ -0,0 +1,447 @@
1
+ (function($) {
2
+ 'use strict';
3
+
4
+ var data = window.ThumbnailsData || {};
5
+
6
+ var $app = $('#thumbnails-group-app');
7
+
8
+ if ($app.length) {
9
+ var groupId = parseInt($app.data('group-id'), 10);
10
+ var currentType = '';
11
+ var currentSearch = '';
12
+ var currentPage = 1;
13
+ var isLoading = false;
14
+ var hasMore = true;
15
+ var observer = null;
16
+ var currentFilters = {};
17
+ var languages = data.languages || {};
18
+ var siteLang = data.site_lang || 'en';
19
+ var taxonomies = data.taxonomies || {};
20
+ var assetTypes = [];
21
+
22
+ function buildAssetTypes() {
23
+ assetTypes = [];
24
+ var siteName = languages[siteLang] || 'Thumbnail';
25
+ assetTypes.push({ key: 'thumbnail', name: siteName, code: siteLang.toUpperCase() });
26
+ for (var code in languages) {
27
+ if (languages.hasOwnProperty(code) && code !== siteLang) {
28
+ assetTypes.push({ key: 'thumbnail_' + code, name: languages[code], code: code.toUpperCase() });
29
+ }
30
+ }
31
+ }
32
+ buildAssetTypes();
33
+
34
+ function renderTaxFilters() {
35
+ var $container = $('#thumbnails-tax-filters');
36
+ $container.empty();
37
+
38
+ var typeTax = taxonomies[currentType] || {};
39
+ var slugs = Object.keys(typeTax);
40
+ if (slugs.length === 0) return;
41
+
42
+ $.each(typeTax, function(taxSlug, taxData) {
43
+ var activeIds = currentFilters[taxSlug] || [];
44
+ var $select = $('<select class="thumbnails-tax-filter" data-tax="' + taxSlug + '" style="margin-left:8px;">');
45
+ $select.append('<option value="">All ' + taxData.label + '</option>');
46
+ $.each(taxData.terms, function(i, term) {
47
+ var sel = activeIds.indexOf(term.id) !== -1 ? ' selected' : '';
48
+ $select.append('<option value="' + term.id + '"' + sel + '>' + term.name + '</option>');
49
+ });
50
+ $container.append($select);
51
+ });
52
+
53
+ $container.find('.thumbnails-tax-filter').on('change', function() {
54
+ var tax = $(this).data('tax');
55
+ var vals = $(this).val();
56
+ if (vals === null || vals === '') {
57
+ currentFilters[tax] = [];
58
+ } else if (Array.isArray(vals)) {
59
+ currentFilters[tax] = vals.map(function(v) { return parseInt(v, 10); });
60
+ } else {
61
+ currentFilters[tax] = [parseInt(vals, 10)];
62
+ }
63
+ currentPage = 1;
64
+ if (currentType) {
65
+ loadPosts(currentType, currentSearch, 1, false);
66
+ }
67
+ });
68
+ }
69
+
70
+ function buildTableHeader() {
71
+ var html = '<thead><tr><th class="col-title">Title</th>';
72
+ for (var i = 0; i < assetTypes.length; i++) {
73
+ var at = assetTypes[i];
74
+ html += '<th class="col-thumb" data-asset-type="' + at.key + '">' + $('<span>').text(at.name).html() + ' <span class="ct-th-code">' + at.code + '</span></th>';
75
+ }
76
+ html += '</tr></thead>';
77
+ return html;
78
+ }
79
+
80
+ function loadPosts(type, search, page, append) {
81
+ if (isLoading) return;
82
+ isLoading = true;
83
+
84
+ if (!append) {
85
+ $('#thumbnails-posts-container').html('<p class="thumbnails-loading">Loading posts...</p>');
86
+ }
87
+
88
+ var hasFilters = false;
89
+ var filters = {};
90
+ for (var tax in currentFilters) {
91
+ if (currentFilters.hasOwnProperty(tax) && currentFilters[tax].length > 0) {
92
+ filters[tax] = currentFilters[tax];
93
+ hasFilters = true;
94
+ }
95
+ }
96
+
97
+ $.post(data.ajax_url, {
98
+ action: 'thumbnails_search_posts',
99
+ _nonce: data.nonce,
100
+ post_type: type,
101
+ search: search || '',
102
+ paged: page || 1,
103
+ group_id: groupId,
104
+ tax_filters: hasFilters ? filters : {}
105
+ }, function(res) {
106
+ isLoading = false;
107
+
108
+ if (!res.success) {
109
+ $('#thumbnails-posts-container').html('<p>Error loading posts.</p>');
110
+ return;
111
+ }
112
+
113
+ var r = res.data;
114
+ var html = '';
115
+
116
+ if (!append) {
117
+ html = '<table class="thumbnails-posts-table">' + buildTableHeader() + '<tbody>';
118
+ }
119
+
120
+ $.each(r.posts, function(i, post) {
121
+ var row = '<tr data-post-id="' + post.id + '">';
122
+ row += '<td class="col-title"><strong>' + $('<span>').text(post.title).html() + '</strong></td>';
123
+
124
+ for (var j = 0; j < assetTypes.length; j++) {
125
+ var at = assetTypes[j].key;
126
+ var th = post.thumbnails[at] || {};
127
+ var has = th.attachment_id > 0 && th.url;
128
+ row += '<td class="col-thumb" data-asset-type="' + at + '">';
129
+ row += '<div class="thumbnails-thumb-cell">';
130
+ if (has) {
131
+ row += '<div class="thumbnails-thumb-preview"><img src="' + th.url + '" alt=""></div>';
132
+ } else {
133
+ row += '<div class="thumbnails-thumb-empty">No image</div>';
134
+ }
135
+ row += '</div>';
136
+ row += '<div class="thumbnails-actions-cell">';
137
+ if (has) {
138
+ row += '<button type="button" class="button thumbnails-upload-btn" data-post-id="' + post.id + '" data-asset-type="' + at + '">Replace</button> ';
139
+ row += '<button type="button" class="button thumbnails-remove-btn" data-post-id="' + post.id + '" data-asset-type="' + at + '">Remove</button>';
140
+ } else {
141
+ row += '<button type="button" class="button thumbnails-upload-btn" data-post-id="' + post.id + '" data-asset-type="' + at + '">Add Image</button>';
142
+ }
143
+ row += '</div>';
144
+ row += '</td>';
145
+ }
146
+
147
+ row += '</tr>';
148
+
149
+ if (append) {
150
+ $('.thumbnails-posts-table tbody').append(row);
151
+ } else {
152
+ html += row;
153
+ }
154
+ });
155
+
156
+ if (!append) {
157
+ html += '</tbody></table>';
158
+ $('#thumbnails-posts-container').html(html);
159
+ }
160
+
161
+ hasMore = r.has_more;
162
+ currentPage = r.paged;
163
+
164
+ updateSentinel();
165
+ }, 'json');
166
+ }
167
+
168
+ function updateSentinel() {
169
+ $('.thumbnails-infinite-scroll').remove();
170
+
171
+ if (hasMore) {
172
+ $app.append('<div class="thumbnails-infinite-scroll">Loading more...</div>');
173
+ if (observer) observer.disconnect();
174
+ observer = new IntersectionObserver(function(entries) {
175
+ if (entries[0].isIntersecting && hasMore && !isLoading) {
176
+ loadPosts(currentType, currentSearch, currentPage + 1, true);
177
+ }
178
+ });
179
+ observer.observe($('.thumbnails-infinite-scroll')[0]);
180
+ }
181
+ }
182
+
183
+ $('.thumbnails-tab').on('click', function(e) {
184
+ e.preventDefault();
185
+ $('.thumbnails-tab').removeClass('active');
186
+ $(this).addClass('active');
187
+
188
+ currentType = $(this).data('post-type');
189
+ currentSearch = $('#thumbnails-search').val();
190
+ currentPage = 1;
191
+ currentFilters = {};
192
+
193
+ renderTaxFilters();
194
+ loadPosts(currentType, currentSearch, 1, false);
195
+ });
196
+
197
+ $('.thumbnails-tab').first().trigger('click');
198
+
199
+ var searchTimer;
200
+ $('#thumbnails-search').on('input', function() {
201
+ clearTimeout(searchTimer);
202
+ searchTimer = setTimeout(function() {
203
+ currentSearch = $('#thumbnails-search').val();
204
+ currentPage = 1;
205
+ if (currentType) {
206
+ loadPosts(currentType, currentSearch, 1, false);
207
+ }
208
+ }, 300);
209
+ });
210
+
211
+ $(document).on('click', '.thumbnails-upload-btn', function(e) {
212
+ e.preventDefault();
213
+ var $btn = $(this);
214
+ var postId = parseInt($btn.data('post-id'), 10);
215
+ var assetType = $btn.data('asset-type');
216
+
217
+ var frame = wp.media({
218
+ title: 'Select Thumbnail Image',
219
+ multiple: false,
220
+ library: { type: 'image' }
221
+ });
222
+
223
+ frame.on('select', function() {
224
+ var attachment = frame.state().get('selection').first().toJSON();
225
+
226
+ $.post(data.ajax_url, {
227
+ action: 'thumbnails_save_thumbnail',
228
+ _nonce: data.nonce,
229
+ group_id: groupId,
230
+ object_id: postId,
231
+ attachment_id: attachment.id,
232
+ asset_type: assetType
233
+ }, function(res) {
234
+ if (res.success) {
235
+ var $td = $btn.closest('td.col-thumb');
236
+ var thumbUrl = res.data.thumb_url;
237
+ var thumbHtml = thumbUrl
238
+ ? '<div class="thumbnails-thumb-preview"><img src="' + thumbUrl + '" alt=""></div>'
239
+ : '<div class="thumbnails-thumb-empty">No image</div>';
240
+ $td.find('.thumbnails-thumb-cell').html(thumbHtml);
241
+ var newBtnHtml = thumbUrl
242
+ ? '<button type="button" class="button thumbnails-upload-btn" data-post-id="' + postId + '" data-asset-type="' + assetType + '">Replace</button> ' +
243
+ '<button type="button" class="button thumbnails-remove-btn" data-post-id="' + postId + '" data-asset-type="' + assetType + '">Remove</button>'
244
+ : '<button type="button" class="button thumbnails-upload-btn" data-post-id="' + postId + '" data-asset-type="' + assetType + '">Add Image</button>';
245
+ $td.find('.thumbnails-actions-cell').html(newBtnHtml);
246
+ }
247
+ }, 'json');
248
+ });
249
+
250
+ frame.open();
251
+ });
252
+
253
+ $(document).on('click', '.thumbnails-remove-btn', function(e) {
254
+ e.preventDefault();
255
+ if (!confirm('Remove this thumbnail?')) return;
256
+
257
+ var $btn = $(this);
258
+ var postId = parseInt($btn.data('post-id'), 10);
259
+ var assetType = $btn.data('asset-type');
260
+
261
+ $.post(data.ajax_url, {
262
+ action: 'thumbnails_remove_thumbnail',
263
+ _nonce: data.nonce,
264
+ group_id: groupId,
265
+ object_id: postId,
266
+ asset_type: assetType
267
+ }, function(res) {
268
+ if (res.success) {
269
+ var $td = $btn.closest('td.col-thumb');
270
+ $td.find('.thumbnails-thumb-cell').html('<div class="thumbnails-thumb-empty">No image</div>');
271
+ var newBtnHtml = '<button type="button" class="button thumbnails-upload-btn" data-post-id="' + postId + '" data-asset-type="' + assetType + '">Add Image</button>';
272
+ $td.find('.thumbnails-actions-cell').html(newBtnHtml);
273
+ }
274
+ }, 'json');
275
+ });
276
+ }
277
+
278
+ var $modal = $('#thumbnails-modal');
279
+ var $btn = $('#thumbnails-branding-btn');
280
+ var groups = data.groups || [];
281
+ var modalLanguages = data.languages || {};
282
+ var modalSiteLang = data.site_lang || 'en';
283
+ var hasLanguages = Object.keys(modalLanguages).length > 0;
284
+
285
+ function getModalAssetTypes() {
286
+ var types = [];
287
+ var siteName = modalLanguages[modalSiteLang] || 'Thumbnail';
288
+ types.push({ key: 'thumbnail', name: siteName, code: modalSiteLang.toUpperCase() });
289
+ for (var code in modalLanguages) {
290
+ if (modalLanguages.hasOwnProperty(code) && code !== modalSiteLang) {
291
+ types.push({ key: 'thumbnail_' + code, name: modalLanguages[code], code: code.toUpperCase() });
292
+ }
293
+ }
294
+ return types;
295
+ }
296
+
297
+ if ($btn.length && $modal.length) {
298
+
299
+ $btn.on('click', function() {
300
+ renderModalGroups();
301
+ $modal.show();
302
+ });
303
+
304
+ $modal.on('click', '.thumbnails-modal-backdrop, .thumbnails-modal-close', function() {
305
+ $modal.hide();
306
+ });
307
+
308
+ function renderModalGroups() {
309
+ var $container = $('#thumbnails-modal-groups');
310
+ $container.empty();
311
+
312
+ if (!groups.length) {
313
+ $container.html('<p>No thumbnail groups found. <a href="' + data.admin_url + 'admin.php?page=thumbnails-manager">Create one</a>.</p>');
314
+ return;
315
+ }
316
+
317
+ $.each(groups, function(i, g) {
318
+ var $group = $('<div class="thumbnails-modal-group"><h3>' + g.name + '</h3></div>');
319
+ var thumbs = g.thumbnails || {};
320
+
321
+ if (hasLanguages) {
322
+ var types = getModalAssetTypes();
323
+ var $row = $('<div class="thumbnails-modal-group-row"></div>');
324
+ $.each(types, function(j, at) {
325
+ var th = thumbs[at.key] || {};
326
+ var has = th.attachment_id > 0 && th.url;
327
+ var thumbHtml = has
328
+ ? '<div class="thumbnails-thumb-preview"><img src="' + th.url + '" alt=""></div>'
329
+ : '<div class="thumbnails-thumb-empty">No image</div>';
330
+
331
+ var btnHtml = has
332
+ ? '<button type="button" class="button thumbnails-modal-upload-btn" data-group-id="' + g.id + '" data-asset-type="' + at.key + '">Replace</button> ' +
333
+ '<button type="button" class="button thumbnails-modal-remove-btn" data-group-id="' + g.id + '" data-asset-type="' + at.key + '">Remove</button>'
334
+ : '<button type="button" class="button thumbnails-modal-upload-btn" data-group-id="' + g.id + '" data-asset-type="' + at.key + '">Add Image</button>';
335
+
336
+ var $item = $('<div class="thumbnails-modal-lang-item">' +
337
+ '<span class="thumbnails-modal-lang-code">' + at.code + '</span>' +
338
+ '<div class="thumbnails-thumb-cell">' + thumbHtml + '</div>' +
339
+ '<div class="thumbnails-modal-actions">' + btnHtml + '</div>' +
340
+ '</div>');
341
+ $row.append($item);
342
+ });
343
+ $group.append($row);
344
+ } else {
345
+ var th = thumbs['thumbnail'] || {};
346
+ var has = th.attachment_id > 0 && th.url;
347
+ var thumbHtml = has
348
+ ? '<div class="thumbnails-thumb-preview"><img src="' + th.url + '" alt=""></div>'
349
+ : '<div class="thumbnails-thumb-empty">No image</div>';
350
+
351
+ var btnHtml = has
352
+ ? '<button type="button" class="button thumbnails-modal-upload-btn" data-group-id="' + g.id + '">Replace</button> ' +
353
+ '<button type="button" class="button thumbnails-modal-remove-btn" data-group-id="' + g.id + '">Remove</button>'
354
+ : '<button type="button" class="button thumbnails-modal-upload-btn" data-group-id="' + g.id + '">Add Image</button>';
355
+
356
+ $group.append('<div class="thumbnails-thumb-cell">' + thumbHtml + '</div>');
357
+ $group.append('<p style="margin-top:8px;">' + btnHtml + '</p>');
358
+ }
359
+
360
+ $container.append($group);
361
+ });
362
+ }
363
+
364
+ $(document).on('click', '.thumbnails-modal-upload-btn', function() {
365
+ var $btn = $(this);
366
+ var groupId = parseInt($btn.data('group-id'), 10);
367
+ var assetType = $btn.data('asset-type') || 'thumbnail';
368
+ var postId = parseInt(data.post_id, 10);
369
+ var $groupEl = $btn.closest('.thumbnails-modal-group');
370
+ var $cell = $btn.closest('.thumbnails-modal-lang-item').find('.thumbnails-thumb-cell');
371
+ if (!$cell.length) $cell = $groupEl.find('.thumbnails-thumb-cell');
372
+
373
+ var frame = wp.media({
374
+ title: 'Select Thumbnail Image',
375
+ multiple: false,
376
+ library: { type: 'image' }
377
+ });
378
+
379
+ frame.on('select', function() {
380
+ var attachment = frame.state().get('selection').first().toJSON();
381
+
382
+ $.post(data.ajax_url, {
383
+ action: 'thumbnails_save_thumbnail',
384
+ _nonce: data.nonce,
385
+ group_id: groupId,
386
+ object_id: postId,
387
+ attachment_id: attachment.id,
388
+ asset_type: assetType
389
+ }, function(res) {
390
+ if (res.success) {
391
+ var thumbUrl = res.data.thumb_url;
392
+ var thumbHtml = thumbUrl
393
+ ? '<div class="thumbnails-thumb-preview"><img src="' + thumbUrl + '" alt=""></div>'
394
+ : '<div class="thumbnails-thumb-empty">No image</div>';
395
+ $cell.html(thumbHtml);
396
+
397
+ var g = getGroupById(groupId);
398
+ if (g) {
399
+ if (!g.thumbnails) g.thumbnails = {};
400
+ g.thumbnails[assetType] = { attachment_id: attachment.id, url: thumbUrl };
401
+ }
402
+ }
403
+ }, 'json');
404
+ });
405
+
406
+ frame.open();
407
+ });
408
+
409
+ $(document).on('click', '.thumbnails-modal-remove-btn', function() {
410
+ if (!confirm('Remove this thumbnail?')) return;
411
+
412
+ var $btn = $(this);
413
+ var groupId = parseInt($btn.data('group-id'), 10);
414
+ var assetType = $btn.data('asset-type') || 'thumbnail';
415
+ var postId = parseInt(data.post_id, 10);
416
+ var $groupEl = $btn.closest('.thumbnails-modal-group');
417
+ var $cell = $btn.closest('.thumbnails-modal-lang-item').find('.thumbnails-thumb-cell');
418
+ if (!$cell.length) $cell = $groupEl.find('.thumbnails-thumb-cell');
419
+
420
+ $.post(data.ajax_url, {
421
+ action: 'thumbnails_remove_thumbnail',
422
+ _nonce: data.nonce,
423
+ group_id: groupId,
424
+ object_id: postId,
425
+ asset_type: assetType
426
+ }, function(res) {
427
+ if (res.success) {
428
+ var emptyHtml = '<div class="thumbnails-thumb-empty">No image</div>';
429
+ $cell.html(emptyHtml);
430
+
431
+ var g = getGroupById(groupId);
432
+ if (g && g.thumbnails) {
433
+ g.thumbnails[assetType] = { attachment_id: 0, url: '' };
434
+ }
435
+ }
436
+ }, 'json');
437
+ });
438
+
439
+ function getGroupById(id) {
440
+ for (var i = 0; i < groups.length; i++) {
441
+ if (groups[i].id === id) return groups[i];
442
+ }
443
+ return null;
444
+ }
445
+ }
446
+
447
+ })(jQuery);