Skip to content

Commit be001db

Browse files
committed
Refactor tools page: extract inline JS to separate file, improve code organization, and add spinner styles
- Move migration and undo batch processing JS from inline to tools-page.js - Move copy button initialization from inline to admin-scripts.js - Add wp_localize_script for batch size and translatable strings - Simplify table installation check in admin notices - Add spinner alignment styles for buttons in admin CSS - Auto-initialize copy buttons for all code blocks on page load
1 parent 0a8dd50 commit be001db

16 files changed

Lines changed: 193 additions & 567 deletions

includes/admin/class-admin-notices.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ private function register_missing_table_notice() {
124124
'capability' => 'manage_options',
125125
'conditions' => array(
126126
function () {
127-
return current_user_can( 'manage_options' ) &&
128-
class_exists( '\WebberZone\Contextual_Related_Posts\Pro\Custom_Tables\Table_Manager' ) &&
129-
! ( new \WebberZone\Contextual_Related_Posts\Pro\Custom_Tables\Table_Manager() )->is_table_installed(
130-
( new \WebberZone\Contextual_Related_Posts\Pro\Custom_Tables\Table_Manager() )->content_table
131-
);
127+
if ( ! current_user_can( 'manage_options' ) || ! class_exists( '\WebberZone\Contextual_Related_Posts\Pro\Custom_Tables\Table_Manager' ) ) {
128+
return false;
129+
}
130+
$table_manager = new \WebberZone\Contextual_Related_Posts\Pro\Custom_Tables\Table_Manager();
131+
return ! $table_manager->is_table_installed( $table_manager->content_table );
132132
},
133133
),
134134
)

includes/admin/class-tools-page.php

Lines changed: 24 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,6 @@ public static function render_page() {
135135
<?php $sql_queries = self::recreate_indices_sql(); ?>
136136
<pre id="crp-indices-sql"><code><?php echo implode( "\n", array_map( 'esc_html', $sql_queries ) ); ?></code></pre>
137137
</div>
138-
<script>
139-
jQuery(document).ready(function($) {
140-
crpAddCopyButton('crp-indices-sql');
141-
});
142-
</script>
143138
<?php wp_nonce_field( 'crp-tools-settings' ); ?>
144139
</form>
145140
</div>
@@ -203,62 +198,6 @@ public static function render_page() {
203198
<p id="crp-migration-status"></p>
204199
</div>
205200
<?php wp_nonce_field( 'crp_migrate_meta_nonce', 'crp_migrate_meta_nonce' ); ?>
206-
<script>
207-
jQuery(document).ready(function($) {
208-
var lastId = 0;
209-
var limit = <?php echo absint( self::BATCH_SIZE ); ?>;
210-
var totalMigrated = 0;
211-
212-
$('#crp_migrate_meta').on('click', function() {
213-
$(this).prop('disabled', true);
214-
$('#crp-migration-progress').show();
215-
migrateBatch();
216-
});
217-
218-
function migrateBatch() {
219-
$.ajax({
220-
url: ajaxurl,
221-
type: 'POST',
222-
data: {
223-
action: 'crp_migrate_meta',
224-
security: $('#crp_migrate_meta_nonce').val(),
225-
last_id: lastId,
226-
limit: limit
227-
},
228-
success: function(response) {
229-
if (response.success) {
230-
totalMigrated += response.data.migrated;
231-
$('#crp-migration-status').text(response.data.message);
232-
if (response.data.last_id !== undefined) {
233-
lastId = response.data.last_id;
234-
}
235-
if (response.data.last_id !== undefined) {
236-
undoLastId = response.data.last_id;
237-
}
238-
if (response.data.complete) {
239-
$('#crp-migration-bar').css('width', '100%');
240-
$('#crp_migrate_meta').text('<?php esc_html_e( 'Migration Complete', 'contextual-related-posts' ); ?>').prop('disabled', true);
241-
setTimeout(function() {
242-
location.reload();
243-
}, 2000);
244-
} else {
245-
var progress = Math.min((totalMigrated / (totalMigrated + response.data.remaining)) * 100, 100);
246-
$('#crp-migration-bar').css('width', progress + '%');
247-
migrateBatch();
248-
}
249-
} else {
250-
$('#crp-migration-status').text('<?php esc_html_e( 'Migration failed. Please try again.', 'contextual-related-posts' ); ?>');
251-
$('#crp_migrate_meta').prop('disabled', false);
252-
}
253-
},
254-
error: function() {
255-
$('#crp-migration-status').text('<?php esc_html_e( 'Migration failed. Please try again.', 'contextual-related-posts' ); ?>');
256-
$('#crp_migrate_meta').prop('disabled', false);
257-
}
258-
});
259-
}
260-
});
261-
</script>
262201
</div>
263202
</div>
264203
<?php elseif ( get_option( 'crp_meta_migration_done', false ) ) : ?>
@@ -289,59 +228,6 @@ function migrateBatch() {
289228
<p id="crp-undo-status"></p>
290229
</div>
291230
<?php wp_nonce_field( 'crp_undo_migrate_meta_nonce', 'crp_undo_migrate_meta_nonce' ); ?>
292-
<script>
293-
jQuery(document).ready(function($) {
294-
var undoLastId = 0;
295-
var undoLimit = <?php echo absint( self::BATCH_SIZE ); ?>;
296-
var totalUndone = 0;
297-
298-
$('#crp_undo_migration').on('click', function() {
299-
if (!confirm('<?php esc_html_e( 'Are you sure you want to undo the migration? This will revert to the old array storage.', 'contextual-related-posts' ); ?>')) {
300-
return;
301-
}
302-
$(this).prop('disabled', true);
303-
$('#crp-undo-progress').show();
304-
undoBatch();
305-
});
306-
307-
function undoBatch() {
308-
$.ajax({
309-
url: ajaxurl,
310-
type: 'POST',
311-
data: {
312-
action: 'crp_undo_migrate_meta',
313-
security: $('#crp_undo_migrate_meta_nonce').val(),
314-
last_id: undoLastId,
315-
limit: undoLimit
316-
},
317-
success: function(response) {
318-
if (response.success) {
319-
totalUndone += response.data.undone;
320-
$('#crp-undo-status').text(response.data.message);
321-
if (response.data.complete) {
322-
$('#crp-undo-bar').css('width', '100%');
323-
$('#crp_undo_migration').text('<?php esc_html_e( 'Undo Complete', 'contextual-related-posts' ); ?>').prop('disabled', true);
324-
setTimeout(function() {
325-
location.reload();
326-
}, 2000);
327-
} else {
328-
var progress = Math.min((totalUndone / (totalUndone + response.data.remaining)) * 100, 100);
329-
$('#crp-undo-bar').css('width', progress + '%');
330-
undoBatch();
331-
}
332-
} else {
333-
$('#crp-undo-status').text('<?php esc_html_e( 'Undo failed. Please try again.', 'contextual-related-posts' ); ?>');
334-
$('#crp_undo_migration').prop('disabled', false);
335-
}
336-
},
337-
error: function() {
338-
$('#crp-undo-status').text('<?php esc_html_e( 'Undo failed. Please try again.', 'contextual-related-posts' ); ?>');
339-
$('#crp_undo_migration').prop('disabled', false);
340-
}
341-
});
342-
}
343-
});
344-
</script>
345231
</div>
346232
</div>
347233
<?php endif; ?>
@@ -509,6 +395,8 @@ public function admin_enqueue_scripts( $hook ) {
509395
$screen = get_current_screen();
510396

511397
if ( $this->parent_id === $screen->id || $this->parent_id === $hook ) {
398+
$file_prefix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
399+
512400
wp_enqueue_script( 'crp-admin-js' );
513401
wp_enqueue_style( 'crp-admin-ui-css' );
514402
wp_enqueue_style( 'wp-spinner' );
@@ -523,6 +411,28 @@ public function admin_enqueue_scripts( $hook ) {
523411
),
524412
)
525413
);
414+
415+
wp_enqueue_script(
416+
'crp-tools-page',
417+
WZ_CRP_PLUGIN_URL . "includes/admin/js/tools-page{$file_prefix}.js",
418+
array( 'jquery' ),
419+
WZ_CRP_VERSION,
420+
true
421+
);
422+
wp_localize_script(
423+
'crp-tools-page',
424+
'crpToolsPage',
425+
array(
426+
'batchSize' => self::BATCH_SIZE,
427+
'strings' => array(
428+
'migrationComplete' => __( 'Migration Complete', 'contextual-related-posts' ),
429+
'migrationFailed' => __( 'Migration failed. Please try again.', 'contextual-related-posts' ),
430+
'undoComplete' => __( 'Undo Complete', 'contextual-related-posts' ),
431+
'undoFailed' => __( 'Undo failed. Please try again.', 'contextual-related-posts' ),
432+
'confirmUndo' => __( 'Are you sure you want to undo the migration? This will revert to the old array storage.', 'contextual-related-posts' ),
433+
),
434+
)
435+
);
526436
}
527437
}
528438

includes/admin/css/admin-styles-rtl.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
button .spinner {
2+
float: none;
3+
vertical-align: middle;
4+
margin: 0 4px 0 0;
5+
}
6+
17
#crp-post-list .ntdelbutton::before {
28
background: none;
39
color: #787c82;

includes/admin/css/admin-styles-rtl.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/admin/css/admin-styles.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
button .spinner {
2+
float: none;
3+
vertical-align: middle;
4+
margin: 0 0 0 4px;
5+
}
6+
17
#crp-post-list .ntdelbutton::before {
28
background: none;
39
color: #787c82;

0 commit comments

Comments
 (0)