@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,614 @@
1
+ /* global jQuery, wp, platformAdmin */
2
+ ( function ( $ ) {
3
+ 'use strict';
4
+
5
+ var cfg = window.platformAdmin || {};
6
+ var counter = Date.now();
7
+
8
+ function post( action, data ) {
9
+ return $.post( cfg.ajax, $.extend( { action: action, nonce: cfg.nonce }, data ) );
10
+ }
11
+
12
+ function collect( form, prefix ) {
13
+ var data = {};
14
+
15
+ new window.FormData( form ).forEach( function ( value, name ) {
16
+ if ( typeof value !== 'string' || name.indexOf( prefix + '[' ) !== 0 ) {
17
+ return;
18
+ }
19
+
20
+ var path = ( name.slice( prefix.length ).match( /\[[^\]]*\]/g ) || [] ).map( function ( part ) {
21
+ return part.slice( 1, -1 );
22
+ } );
23
+ var node = data;
24
+
25
+ path.forEach( function ( key, index ) {
26
+ if ( key === '' ) {
27
+ key = String( Object.keys( node ).length );
28
+ }
29
+
30
+ if ( index === path.length - 1 ) {
31
+ node[ key ] = value;
32
+ } else {
33
+ if ( typeof node[ key ] !== 'object' || node[ key ] === null ) {
34
+ node[ key ] = {};
35
+ }
36
+ node = node[ key ];
37
+ }
38
+ } );
39
+ } );
40
+
41
+ return data;
42
+ }
43
+
44
+ function errorMessage( response, fallback ) {
45
+ var data = response && ( response.responseJSON || response ).data;
46
+ return ( data && data.message ) || fallback;
47
+ }
48
+
49
+ /* Language tabs ---------------------------------------------------------- */
50
+
51
+ $( document ).on( 'click', '.platform-langs__tab', function () {
52
+ var $tab = $( this );
53
+ var $wrap = $tab.closest( '.platform-langs' );
54
+ var lang = String( $tab.data( 'lang' ) );
55
+
56
+ $tab.addClass( 'is-active' ).siblings().removeClass( 'is-active' );
57
+ $wrap.children( '.platform-langs__panel' ).each( function () {
58
+ $( this ).toggleClass( 'is-active', String( $( this ).data( 'lang' ) ) === lang );
59
+ } );
60
+ } );
61
+
62
+ /* Repeaters -------------------------------------------------------------- */
63
+
64
+ function rowTitle( $row ) {
65
+ var $body = $row.children( '.platform-repeater__body' );
66
+ var $source = $body.find( '[data-row-title]' ).first();
67
+
68
+ if ( ! $source.length ) {
69
+ $source = $body.children( '.platform-field' ).find( 'input[type=text], textarea' ).first();
70
+ }
71
+
72
+ $row.find( '> .platform-repeater__bar .platform-repeater__title' ).text( String( $source.val() || '' ).split( '\n' )[ 0 ] );
73
+ }
74
+
75
+ $( document ).on( 'click', '.platform-repeater__add', function () {
76
+ var $repeater = $( this ).closest( '.platform-repeater' );
77
+ var token = String( $repeater.data( 'token' ) );
78
+ var html = $repeater.children( '.platform-repeater__template' ).html().split( token ).join( String( counter++ ) );
79
+ var $row = $( $.parseHTML( html.trim(), document, false ) );
80
+
81
+ $repeater.children( '.platform-repeater__rows' ).append( $row );
82
+ init( $row );
83
+ applyNeeds();
84
+ $row.find( 'input, textarea, select' ).first().trigger( 'focus' );
85
+ } );
86
+
87
+ $( document ).on( 'click', '.platform-repeater__remove', function () {
88
+ var $row = $( this ).closest( '.platform-repeater__row' );
89
+ var filled = $row.find( 'input[type=text], input[type=url], input[type=number], textarea' ).filter( function () {
90
+ return this.value !== '';
91
+ } ).length;
92
+
93
+ if ( ! filled || window.confirm( 'Remove this row?' ) ) {
94
+ $row.remove();
95
+ }
96
+ } );
97
+
98
+ $( document ).on( 'click', '.platform-repeater__toggle', function () {
99
+ $( this ).closest( '.platform-repeater__row' ).toggleClass( 'is-collapsed' );
100
+ } );
101
+
102
+ $( document ).on( 'input', '.platform-repeater__body input, .platform-repeater__body textarea', function () {
103
+ rowTitle( $( this ).closest( '.platform-repeater__row' ) );
104
+ } );
105
+
106
+ /* Images and files ------------------------------------------------------- */
107
+
108
+ function watchImages( $context ) {
109
+ $context.find( '.platform-image__preview' ).each( function () {
110
+ var image = this;
111
+ var broken = function () {
112
+ $( image ).closest( '.platform-image' ).addClass( 'is-broken' );
113
+ };
114
+
115
+ image.addEventListener( 'error', broken );
116
+ image.addEventListener( 'load', function () {
117
+ $( image ).closest( '.platform-image' ).removeClass( 'is-broken' );
118
+ } );
119
+
120
+ if ( image.getAttribute( 'src' ) && image.complete && ! image.naturalWidth ) {
121
+ broken();
122
+ }
123
+ } );
124
+ }
125
+
126
+ function attachmentPreview( attachment ) {
127
+ return attachment.url;
128
+ }
129
+
130
+ $( document ).on( 'click', '.platform-image__choose', function () {
131
+ var $wrap = $( this ).closest( '.platform-image' );
132
+ var frame = wp.media( { title: 'Choose image', library: { type: 'image' }, multiple: false } );
133
+
134
+ frame.on( 'select', function () {
135
+ var attachment = frame.state().get( 'selection' ).first().toJSON();
136
+ $wrap.find( '.platform-image__id' ).val( attachment.id );
137
+ $wrap.find( '.platform-image__preview' ).attr( 'src', attachmentPreview( attachment ) );
138
+ $wrap.find( '.platform-image__frame' ).attr( 'href', attachment.url );
139
+ $wrap.find( '.platform-image__name' ).text( attachment.filename || '' );
140
+ $wrap.removeClass( 'is-broken' ).addClass( 'has-value' );
141
+ } );
142
+
143
+ frame.open();
144
+ } );
145
+
146
+ $( document ).on( 'click', '.platform-image__remove', function () {
147
+ var $wrap = $( this ).closest( '.platform-image' );
148
+ $wrap.find( '.platform-image__id' ).val( '' );
149
+ $wrap.find( '.platform-image__preview' ).attr( 'src', '' );
150
+ $wrap.removeClass( 'has-value' );
151
+ } );
152
+
153
+ $( document ).on( 'click', '.platform-files__add', function () {
154
+ var $wrap = $( this ).closest( '.platform-files' );
155
+ var frame = wp.media( { title: 'Add files', multiple: true } );
156
+
157
+ frame.on( 'select', function () {
158
+ frame.state().get( 'selection' ).each( function ( model ) {
159
+ var file = model.toJSON();
160
+ var $item = $( '<li>' );
161
+ $( '<input type="hidden">' ).attr( 'name', $wrap.data( 'name' ) ).val( file.id ).appendTo( $item );
162
+ $( '<a target="_blank" rel="noopener">' ).attr( 'href', file.url ).text( file.title || file.filename ).appendTo( $item );
163
+ $item.append( ' <button type="button" class="button-link platform-files__remove">Remove</button>' );
164
+ $wrap.children( '.platform-files__list' ).append( $item );
165
+ } );
166
+ } );
167
+
168
+ frame.open();
169
+ } );
170
+
171
+ $( document ).on( 'click', '.platform-files__remove', function () {
172
+ $( this ).closest( 'li' ).remove();
173
+ } );
174
+
175
+ /* Uploads ---------------------------------------------------------------- */
176
+
177
+ function fileName( url ) {
178
+ try {
179
+ return decodeURIComponent( url.split( '?' )[ 0 ].split( '/' ).pop() );
180
+ } catch ( e ) {
181
+ return url;
182
+ }
183
+ }
184
+
185
+ function pad( number ) {
186
+ return ( number < 10 ? '0' : '' ) + number;
187
+ }
188
+
189
+ function detectDuration( $wrap, src ) {
190
+ var $audio = $wrap.closest( '.platform-audio' );
191
+ var $video = $wrap.closest( '.platform-video' );
192
+
193
+ if ( ! src || ( ! $audio.length && ! $video.length ) ) {
194
+ return;
195
+ }
196
+
197
+ var media = document.createElement( $video.length ? 'video' : 'audio' );
198
+ media.preload = 'metadata';
199
+ media.onloadedmetadata = function () {
200
+ var total = Math.round( media.duration );
201
+
202
+ if ( isFinite( total ) && total > 0 ) {
203
+ if ( $audio.length ) {
204
+ $audio.find( '.platform-duration' ).val( total );
205
+ } else {
206
+ $video.find( '.platform-runtime__hours' ).val( pad( Math.floor( total / 3600 ) ) );
207
+ $video.find( '.platform-runtime__minutes' ).val( pad( Math.floor( ( total % 3600 ) / 60 ) ) );
208
+ $video.find( '.platform-runtime__seconds' ).val( pad( total % 60 ) );
209
+ }
210
+ }
211
+
212
+ if ( src.indexOf( 'blob:' ) === 0 ) {
213
+ URL.revokeObjectURL( src );
214
+ }
215
+ };
216
+ media.src = src;
217
+ }
218
+
219
+ function setUpload( $wrap, url, file ) {
220
+ $wrap.find( '.platform-upload__url' ).val( url ).prop( 'readonly', true );
221
+ $wrap.find( '.platform-upload__link' ).attr( 'href', url || null ).text( url ? fileName( url ) : '' );
222
+ $wrap.toggleClass( 'has-value', !! url ).removeClass( 'is-editing is-uploading' );
223
+ $wrap.find( '.platform-upload__progress' ).prop( 'hidden', true );
224
+ detectDuration( $wrap, file ? URL.createObjectURL( file ) : url );
225
+ }
226
+
227
+ function uploadFailed( $wrap, message ) {
228
+ $wrap.removeClass( 'is-uploading' ).find( '.platform-upload__progress' ).prop( 'hidden', true );
229
+ window.alert( message );
230
+ }
231
+
232
+ function uploadToStorage( $wrap, file ) {
233
+ var $progress = $wrap.find( '.platform-upload__progress' );
234
+
235
+ $wrap.addClass( 'is-uploading' );
236
+ $progress.prop( 'hidden', false ).children().css( 'width', '0%' );
237
+
238
+ post( 'platform_upload_target', { name: file.name, folder: $wrap.data( 'folder' ) } )
239
+ .done( function ( response ) {
240
+ if ( ! response.success ) {
241
+ uploadFailed( $wrap, errorMessage( response, 'Upload failed.' ) );
242
+ return;
243
+ }
244
+
245
+ var xhr = new XMLHttpRequest();
246
+ xhr.open( 'PUT', response.data.upload );
247
+ xhr.setRequestHeader( 'Content-Type', file.type || response.data.type );
248
+ xhr.upload.onprogress = function ( event ) {
249
+ if ( event.lengthComputable ) {
250
+ $progress.children().css( 'width', Math.round( ( event.loaded / event.total ) * 100 ) + '%' );
251
+ }
252
+ };
253
+ xhr.onload = function () {
254
+ if ( xhr.status >= 200 && xhr.status < 300 ) {
255
+ setUpload( $wrap, response.data.url, file );
256
+ } else {
257
+ uploadFailed( $wrap, 'Upload failed (HTTP ' + xhr.status + ').' );
258
+ }
259
+ };
260
+ xhr.onerror = function () {
261
+ uploadFailed( $wrap, 'Upload failed. Check that the bucket CORS rule allows PUT from this site.' );
262
+ };
263
+ xhr.send( file );
264
+ } )
265
+ .fail( function ( response ) {
266
+ uploadFailed( $wrap, errorMessage( response, 'Upload failed.' ) );
267
+ } );
268
+ }
269
+
270
+ $( document ).on( 'click', '.platform-upload__pick', function () {
271
+ var $wrap = $( this ).closest( '.platform-upload' );
272
+ var accept = String( $wrap.data( 'accept' ) || '' );
273
+
274
+ if ( cfg.storage ) {
275
+ $( '<input type="file">' ).attr( 'accept', accept ).on( 'change', function () {
276
+ if ( this.files && this.files[ 0 ] ) {
277
+ uploadToStorage( $wrap, this.files[ 0 ] );
278
+ }
279
+ } ).trigger( 'click' );
280
+ return;
281
+ }
282
+
283
+ var frame = wp.media( { title: 'Choose file', library: { type: accept.replace( '/*', '' ) }, multiple: false } );
284
+ frame.on( 'select', function () {
285
+ setUpload( $wrap, frame.state().get( 'selection' ).first().toJSON().url );
286
+ } );
287
+ frame.open();
288
+ } );
289
+
290
+ $( document ).on( 'click', '.platform-upload__edit', function () {
291
+ var $wrap = $( this ).closest( '.platform-upload' );
292
+ $wrap.addClass( 'is-editing' ).find( '.platform-upload__url' ).prop( 'readonly', false ).trigger( 'focus' );
293
+ } );
294
+
295
+ $( document ).on( 'change', '.platform-upload__url', function () {
296
+ setUpload( $( this ).closest( '.platform-upload' ), $.trim( this.value ) );
297
+ } );
298
+
299
+ $( document ).on( 'click', '.platform-upload__clear', function () {
300
+ setUpload( $( this ).closest( '.platform-upload' ), '' );
301
+ } );
302
+
303
+ /* Video ------------------------------------------------------------------ */
304
+
305
+ $( document ).on( 'click', '.platform-video .platform-segmented__item', function () {
306
+ var $button = $( this );
307
+ var $video = $button.closest( '.platform-video' );
308
+ var source = String( $button.data( 'source' ) );
309
+
310
+ $button.addClass( 'is-active' ).siblings().removeClass( 'is-active' );
311
+ $video.find( '.platform-video__type' ).val( source );
312
+ $video.children( '.platform-video__panel' ).each( function () {
313
+ $( this ).toggleClass( 'is-active', String( $( this ).data( 'source' ) ) === source );
314
+ } );
315
+ } );
316
+
317
+ /* Scoring and conditional fields ----------------------------------------- */
318
+
319
+ function currentUses() {
320
+ var $select = $( '.platform-scoring__model' ).first();
321
+
322
+ if ( $select.length ) {
323
+ var model = ( $select.closest( '.platform-scoring' ).data( 'models' ) || {} )[ $select.val() ];
324
+ return model ? model.uses : [];
325
+ }
326
+
327
+ var $structure = $( '.platform-structure' ).first();
328
+ return $structure.length ? $structure.data( 'uses' ) || [] : null;
329
+ }
330
+
331
+ function applyNeeds() {
332
+ var uses = currentUses();
333
+
334
+ if ( uses === null ) {
335
+ return;
336
+ }
337
+
338
+ $( '[data-needs]' ).each( function () {
339
+ $( this ).toggle( uses.indexOf( String( $( this ).data( 'needs' ) ) ) !== -1 );
340
+ } );
341
+ }
342
+
343
+ function showModel( $select ) {
344
+ var $scoring = $select.closest( '.platform-scoring' );
345
+ var model = ( $scoring.data( 'models' ) || {} )[ $select.val() ] || {};
346
+
347
+ $scoring.find( '.platform-scoring__summary' ).text( model.summary || '' );
348
+ $scoring.find( '.platform-scoring__examples' ).text( model.examples ? 'For example: ' + model.examples : '' );
349
+ $scoring.find( '.platform-scoring__params' ).each( function () {
350
+ $( this ).prop( 'hidden', $( this ).data( 'model' ) !== $select.val() );
351
+ } );
352
+ applyNeeds();
353
+ }
354
+
355
+ $( document ).on( 'change', '.platform-scoring__model', function () {
356
+ showModel( $( this ) );
357
+ } );
358
+
359
+ /* Nested content builder ------------------------------------------------- */
360
+
361
+ function sortableLists( $context ) {
362
+ $context.find( '.platform-structure__list' ).addBack( '.platform-structure__list' ).each( function () {
363
+ var $list = $( this );
364
+
365
+ if ( $list.data( 'ui-sortable' ) ) {
366
+ return;
367
+ }
368
+
369
+ $list.sortable( {
370
+ handle: '.platform-structure__handle',
371
+ items: '> .platform-structure__item',
372
+ axis: 'y',
373
+ update: function () {
374
+ post( 'platform_structure_sort', {
375
+ order: $list.children( '.platform-structure__item' ).map( function () {
376
+ return $( this ).data( 'id' );
377
+ } ).get()
378
+ } );
379
+ }
380
+ } );
381
+ } );
382
+ }
383
+
384
+ var $modal = $();
385
+ var $editing = $();
386
+
387
+ function closeModal() {
388
+ $modal.prop( 'hidden', true );
389
+ $modal.find( '.platform-modal__body' ).empty();
390
+ $( document.body ).removeClass( 'platform-modal-open' );
391
+ }
392
+
393
+ function openModal( $item ) {
394
+ var $body = $modal.find( '.platform-modal__body' );
395
+
396
+ $editing = $item;
397
+ $modal.prop( 'hidden', false );
398
+ $modal.find( '.platform-modal__title' ).text( 'Loading…' );
399
+ $modal.find( '.platform-modal__status' ).text( '' );
400
+ $body.html( '<p class="platform-modal__loading"><span class="spinner is-active"></span></p>' );
401
+ $( document.body ).addClass( 'platform-modal-open' );
402
+
403
+ post( 'platform_structure_form', { id: $item.data( 'id' ) } )
404
+ .done( function ( response ) {
405
+ if ( ! response.success ) {
406
+ $body.html( '' );
407
+ $modal.find( '.platform-modal__status' ).text( errorMessage( response, 'Could not load.' ) );
408
+ return;
409
+ }
410
+
411
+ $modal.find( '.platform-modal__title' ).text( response.data.title );
412
+ $body.html( response.data.html );
413
+ init( $body );
414
+ applyNeeds();
415
+ $body.find( 'input[name="post[title]"]' ).trigger( 'focus' );
416
+ } )
417
+ .fail( function ( response ) {
418
+ $modal.find( '.platform-modal__status' ).text( errorMessage( response, 'Could not load.' ) );
419
+ } );
420
+ }
421
+
422
+ function saveModal() {
423
+ var $body = $modal.find( '.platform-modal__body' );
424
+ var $status = $modal.find( '.platform-modal__status' );
425
+ var $title = $body.find( 'input[name="post[title]"]' );
426
+
427
+ if ( ! $.trim( $title.val() ) ) {
428
+ $status.text( 'Enter a title.' );
429
+ $title.trigger( 'focus' );
430
+ return;
431
+ }
432
+
433
+ var data = new window.FormData();
434
+
435
+ new window.FormData( $body[ 0 ] ).forEach( function ( value, name ) {
436
+ if ( name.indexOf( 'fields[' ) !== 0 ) {
437
+ data.append( name, value );
438
+ }
439
+ } );
440
+ data.append( 'platform_fields', JSON.stringify( collect( $body[ 0 ], 'fields' ) ) );
441
+ data.append( 'action', 'platform_structure_save' );
442
+ data.append( 'nonce', cfg.nonce );
443
+
444
+ $status.text( 'Saving…' );
445
+ $modal.find( '.platform-modal__save' ).prop( 'disabled', true );
446
+
447
+ $.ajax( { url: cfg.ajax, type: 'POST', data: data, processData: false, contentType: false } )
448
+ .done( function ( response ) {
449
+ if ( ! response.success ) {
450
+ $status.text( errorMessage( response, 'Could not save.' ) );
451
+ return;
452
+ }
453
+
454
+ $editing.children( '.platform-structure__bar' ).replaceWith( response.data.bar );
455
+ closeModal();
456
+ } )
457
+ .fail( function ( response ) {
458
+ $status.text( errorMessage( response, 'Could not save.' ) );
459
+ } )
460
+ .always( function () {
461
+ $modal.find( '.platform-modal__save' ).prop( 'disabled', false );
462
+ } );
463
+ }
464
+
465
+ $( document ).on( 'click', '.platform-structure__add', function () {
466
+ var $button = $( this );
467
+ var $list = $button.prev( '.platform-structure__list' );
468
+
469
+ $button.prop( 'disabled', true );
470
+ post( 'platform_structure_add', { parent: $button.data( 'parent' ), type: $button.data( 'type' ) } )
471
+ .done( function ( response ) {
472
+ if ( ! response.success ) {
473
+ window.alert( errorMessage( response, 'Could not add.' ) );
474
+ return;
475
+ }
476
+
477
+ var $item = $( response.data.html ).appendTo( $list );
478
+ sortableLists( $item );
479
+ $list.closest( '.platform-structure__item' ).addClass( 'is-open' );
480
+ openModal( $item );
481
+ } )
482
+ .fail( function ( response ) {
483
+ window.alert( errorMessage( response, 'Could not add.' ) );
484
+ } )
485
+ .always( function () {
486
+ $button.prop( 'disabled', false );
487
+ } );
488
+ } );
489
+
490
+ $( document ).on( 'click', '.platform-structure__edit', function () {
491
+ openModal( $( this ).closest( '.platform-structure__item' ) );
492
+ } );
493
+
494
+ $( document ).on( 'click', '.platform-structure__bar', function ( event ) {
495
+ if ( ! $( event.target ).closest( 'button' ).length ) {
496
+ $( this ).closest( '.platform-structure__item' ).toggleClass( 'is-open' );
497
+ }
498
+ } );
499
+
500
+ $( document ).on( 'click', '.platform-structure__toggle', function () {
501
+ $( this ).closest( '.platform-structure__item' ).toggleClass( 'is-open' );
502
+ } );
503
+
504
+ $( document ).on( 'click', '.platform-structure__delete', function () {
505
+ var $item = $( this ).closest( '.platform-structure__item' );
506
+ var name = $item.find( '> .platform-structure__bar .platform-structure__title' ).text();
507
+
508
+ if ( ! window.confirm( 'Delete "' + name + '" and everything inside it? This cannot be undone.' ) ) {
509
+ return;
510
+ }
511
+
512
+ post( 'platform_structure_delete', { id: $item.data( 'id' ) } )
513
+ .done( function ( response ) {
514
+ if ( response.success ) {
515
+ $item.remove();
516
+ } else {
517
+ window.alert( errorMessage( response, 'Could not delete.' ) );
518
+ }
519
+ } )
520
+ .fail( function ( response ) {
521
+ window.alert( errorMessage( response, 'Could not delete.' ) );
522
+ } );
523
+ } );
524
+
525
+ $( document ).on( 'click', '.platform-modal__close, .platform-modal__cancel', closeModal );
526
+ $( document ).on( 'click', '.platform-modal__save', saveModal );
527
+ $( document ).on( 'submit', '.platform-modal__body', function ( event ) {
528
+ event.preventDefault();
529
+ saveModal();
530
+ } );
531
+ $( document ).on( 'keydown', function ( event ) {
532
+ if ( event.key === 'Escape' && $modal.length && ! $modal.prop( 'hidden' ) ) {
533
+ closeModal();
534
+ }
535
+ } );
536
+
537
+ /* Small helpers ---------------------------------------------------------- */
538
+
539
+ $( document ).on( 'input', '.platform-filter', function () {
540
+ var query = this.value.toLowerCase();
541
+
542
+ $( this ).nextAll( 'select' ).first().children( 'option' ).each( function () {
543
+ this.hidden = query !== '' && this.text.toLowerCase().indexOf( query ) === -1;
544
+ } );
545
+ } );
546
+
547
+ $( document ).on( 'change', '.platform-scheme', function () {
548
+ var values = this.value ? JSON.parse( this.value ) : null;
549
+
550
+ if ( ! values ) {
551
+ return;
552
+ }
553
+
554
+ $.each( $( this ).data( 'fields' ) || [], function ( i, key ) {
555
+ $( '[name="metadata[' + key + ']"]' ).val( values[ key ] || '' );
556
+ } );
557
+ } );
558
+
559
+ $( document ).on( 'click', '.platform-placeholder', function () {
560
+ var $button = $( this );
561
+ var text = String( $button.data( 'copy' ) );
562
+
563
+ if ( navigator.clipboard ) {
564
+ navigator.clipboard.writeText( text ).then( function () {
565
+ $button.text( 'Copied' );
566
+ window.setTimeout( function () {
567
+ $button.text( text );
568
+ }, 1200 );
569
+ } );
570
+ }
571
+ } );
572
+
573
+ /* Init ------------------------------------------------------------------- */
574
+
575
+ function init( $context ) {
576
+ watchImages( $context );
577
+
578
+ $context.find( '.platform-repeater__rows' ).each( function () {
579
+ $( this ).sortable( { handle: '.platform-repeater__handle', items: '> .platform-repeater__row', axis: 'y' } );
580
+ } );
581
+
582
+ $context.find( '.platform-repeater__row' ).each( function () {
583
+ rowTitle( $( this ) );
584
+ } );
585
+
586
+ $context.find( '.platform-scoring__model' ).each( function () {
587
+ showModel( $( this ) );
588
+ } );
589
+ }
590
+
591
+ $( document ).on( 'submit', 'form#post, form#edittag', function () {
592
+ var form = this;
593
+
594
+ if ( ! $( form ).find( '.platform-fields' ).length ) {
595
+ return;
596
+ }
597
+
598
+ if ( window.tinymce ) {
599
+ window.tinymce.triggerSave();
600
+ }
601
+
602
+ $( '<input type="hidden" name="platform_fields">' ).val( JSON.stringify( collect( form, 'fields' ) ) ).appendTo( form );
603
+ $( form ).find( '[name^="fields["]' ).each( function () {
604
+ this.removeAttribute( 'name' );
605
+ } );
606
+ } );
607
+
608
+ $( function () {
609
+ $modal = $( '.platform-modal' ).first();
610
+ init( $( document.body ) );
611
+ sortableLists( $( '.platform-structure' ) );
612
+ applyNeeds();
613
+ } );
614
+ }( jQuery ) );