PluginProbe ʕ •ᴥ•ʔ
Admin Help Docs / 2.0.3
Admin Help Docs v2.0.3
2.0.3 2.0.2 2.0.1.1 trunk 1.4.3.2 2.0.0 2.0.0.1 2.0.0.2 2.0.1
admin-help-docs / inc / tabs / js / migrate.js
admin-help-docs / inc / tabs / js Last commit date
admin-menu.js 1 day ago documentation.js 1 day ago import.js 1 day ago migrate-notice.js 1 day ago migrate.js 1 day ago settings.js 1 day ago support.js 1 day ago
migrate.js
144 lines
1 jQuery( function ( $ ) {
2
3 var appEl = $( '#helpdocs-migrate-app' );
4 if ( ! appEl.length ) {
5 return;
6 }
7
8 var sourceSelect = $( '#helpdocs-migrate-source' );
9 var listEl = $( '#helpdocs-migrate-list' );
10 var actionsRow = $( '#helpdocs-migrate-actions-row' );
11 var selectAllBtn = $( '#helpdocs-migrate-select-all' );
12 var submitBtn = $( '#helpdocs-migrate-submit' );
13 var statusEl = $( '#helpdocs-migrate-status' );
14 var allSelected = false;
15 var successTimeout = null;
16
17 function loadPosts( sourcePostType, preserveStatus ) {
18 listEl.html( '' );
19 actionsRow.hide();
20 submitBtn.prop( 'disabled', true );
21
22 if ( ! preserveStatus ) {
23 statusEl.html( '' );
24 }
25
26 if ( ! sourcePostType ) {
27 sourceSelect.prop( 'disabled', false );
28 return;
29 }
30
31 sourceSelect.prop( 'disabled', true );
32 listEl.html( '<div class="spinner-row"><span class="spinner is-active" style="float:none;"></span> ' + helpdocs_migrate.i18n.loading + '</div>' );
33
34 $.post( helpdocs_migrate.ajax_url, {
35 action: 'helpdocs_migrate_list_posts',
36 nonce: helpdocs_migrate.nonce,
37 source_post_type: sourcePostType
38 } ).done( function ( response ) {
39 sourceSelect.prop( 'disabled', false );
40 listEl.html( '' );
41
42 if ( ! response.success || ! response.data.posts.length ) {
43 listEl.html( '<p class="helpdocs-table-none">' + helpdocs_migrate.i18n.no_posts + '</p>' );
44 return;
45 }
46
47 var table = $( '<table class="helpdocs-table"><tbody></tbody></table>' );
48 var tbody = table.find( 'tbody' );
49
50 response.data.posts.forEach( function ( post ) {
51 var row = $( '<tr></tr>' ).toggleClass( 'is-imported', post.imported );
52 var checkboxCell = $( '<td></td>' );
53 var checkbox = $( '<input type="checkbox" class="helpdocs-migrate-checkbox">' )
54 .attr( 'value', post.id )
55 .prop( 'disabled', post.imported );
56
57 checkboxCell.append( checkbox );
58
59 var titleCell = $( '<td></td>' ).text( post.title );
60 var statusCell = $( '<td class="helpdocs-migrate-status"></td>' );
61
62 if ( post.imported ) {
63 statusCell.html( '<span class="helpdocs-migrate-badge">' + helpdocs_migrate.i18n.already_migrated + '</span>' );
64 }
65
66 row.append( checkboxCell ).append( titleCell ).append( statusCell );
67 tbody.append( row );
68 } );
69
70 listEl.append( table );
71 actionsRow.show();
72 selectAllBtn.text( helpdocs_migrate.i18n.select_all );
73 allSelected = false;
74 updateSubmitState();
75 } ).fail( function () {
76 sourceSelect.prop( 'disabled', false );
77 listEl.html( '<p class="helpdocs-error">' + helpdocs_migrate.i18n.error + '</p>' );
78 } );
79 } // End loadPosts()
80
81 function updateSubmitState() {
82 var checkedCount = listEl.find( '.helpdocs-migrate-checkbox:checked' ).length;
83 submitBtn.prop( 'disabled', checkedCount === 0 );
84 } // End updateSubmitState()
85
86 sourceSelect.on( 'change', function () {
87 loadPosts( $( this ).val(), false );
88 } );
89
90 listEl.on( 'change', '.helpdocs-migrate-checkbox', updateSubmitState );
91
92 selectAllBtn.on( 'click', function () {
93 allSelected = ! allSelected;
94 listEl.find( '.helpdocs-migrate-checkbox:not(:disabled)' ).prop( 'checked', allSelected );
95 selectAllBtn.text( allSelected ? helpdocs_migrate.i18n.deselect_all : helpdocs_migrate.i18n.select_all );
96 updateSubmitState();
97 } );
98
99 submitBtn.on( 'click', function () {
100 var sourcePostType = sourceSelect.val();
101 var postIds = listEl.find( '.helpdocs-migrate-checkbox:checked' ).map( function () {
102 return $( this ).val();
103 } ).get();
104
105 if ( ! sourcePostType || ! postIds.length ) {
106 return;
107 }
108
109 submitBtn.prop( 'disabled', true );
110 statusEl.html( '<span class="spinner is-active" style="float:none;"></span> ' + helpdocs_migrate.i18n.migrating );
111
112 $.post( helpdocs_migrate.ajax_url, {
113 action: 'helpdocs_migrate_import_posts',
114 nonce: helpdocs_migrate.nonce,
115 source_post_type: sourcePostType,
116 post_ids: postIds
117 } ).done( function ( response ) {
118 if ( ! response.success ) {
119 statusEl.html( '<span class="helpdocs-error">' + ( response.data && response.data.message ? response.data.message : 'Error' ) + '</span>' );
120 return;
121 }
122
123 var message = helpdocs_migrate.i18n.done
124 .replace( '%migrated%', response.data.imported )
125 .replace( '%skipped%', response.data.skipped );
126
127 var docsUrl = helpdocs_migrate.docs_url;
128 if ( response.data.last_id ) {
129 docsUrl += ( docsUrl.indexOf( '?' ) === -1 ? '?' : '&' ) + 'id=' + response.data.last_id;
130 }
131
132 statusEl.html( '<span class="helpdocs-migrate-success"><span class="dashicons dashicons-yes-alt"></span> ' + message + ' <a href="' + docsUrl + '">' + helpdocs_migrate.i18n.go_to_docs + '</a></span>' );
133
134 loadPosts( sourcePostType, true );
135 } );
136 } );
137
138 var preselect = appEl.data( 'preselect' );
139 if ( preselect ) {
140 sourceSelect.val( preselect );
141 loadPosts( preselect, false );
142 }
143
144 } );