Skip to content

Commit

Permalink
Show proper status messages as the term cleanup runs. Fix some PHP is…
Browse files Browse the repository at this point in the history
…sues
  • Loading branch information
dkotter committed Oct 9, 2024
1 parent 1272cea commit 6c928b9
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"Headers": "readonly",
"requestAnimationFrame": "readonly",
"React": "readonly",
"Block": "readonly"
"Block": "readonly",
"classifai_term_cleanup_params": "readonly"
},
"extends": ["plugin:@wordpress/eslint-plugin/recommended"],
"ignorePatterns": ["*.json", "webpack.config.js"]
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Admin/SimilarTermsListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function get_columns() {
return array(
'term' => $label,
// translators: %s: Singular label of the taxonomy.
'similar_term' => sprintf( __( 'Similar %s' ), $label ),
'similar_term' => sprintf( __( 'Similar %s', 'classifai' ), $label ),
'actions' => __( 'Actions', 'classifai' ),
);
}
Expand Down
48 changes: 30 additions & 18 deletions includes/Classifai/Features/TermCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public function setup() {
* This will only fire if the Feature is enabled.
*/
public function feature_setup() {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );

// Register the settings page for the Feature.
add_action( 'admin_menu', [ $this, 'register_admin_menu_item' ] );
add_action( 'admin_post_classifai_init_term_cleanup', [ $this, 'start_term_cleanup_process' ] );
Expand All @@ -110,6 +112,26 @@ public function feature_setup() {
add_action( 'admin_notices', [ $this, 'render_notices' ] );
}

/**
* Enqueue the admin scripts.
*
* @param string $hook_suffix The current admin page.
*/
public function enqueue_admin_assets( string $hook_suffix ) {
if ( 'tools_page_classifai-term-cleanup' !== $hook_suffix ) {
return;
}

wp_localize_script(
'classifai-admin-script',
'classifai_term_cleanup_params',
array(
'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
'ajax_nonce' => wp_create_nonce( 'classifai-term-cleanup-status' ),
)
);
}

/**
* Register a sub page under the Tools menu.
*/
Expand Down Expand Up @@ -831,16 +853,12 @@ public function get_background_processing_status( string $taxonomy ): array {
return [];
}

// TODO
// $batches = $this->background_process->get_batches();
$batches = [];

if ( ! empty( $batches ) ) {
foreach ( $batches as $batch ) {
foreach ( $batch->data as $key => $value ) {
if ( 'term_cleanup' === $value['action'] && $taxonomy === $value['taxonomy'] ) {
return $value;
}
$args = $this->background_process->get_args();

if ( ! empty( $args ) ) {
foreach ( $args as $arg ) {
if ( 'term_cleanup' === $arg['action'] && $taxonomy === $arg['taxonomy'] ) {
return $arg;
}
}
}
Expand All @@ -854,7 +872,6 @@ public function get_background_processing_status( string $taxonomy ): array {
* @param string $taxonomy Taxonomy to process.
*/
public function render_background_processing_status( $taxonomy ) {
// TODO
$status = $this->get_background_processing_status( $taxonomy );

if ( empty( $status ) ) {
Expand All @@ -877,7 +894,7 @@ public function render_background_processing_status( $taxonomy ) {
$label = strtolower( $this->get_taxonomy_label( $taxonomy, true ) );
?>

<div class="classifai-process-status" data-taxonomy="<?php echo esc_attr( $taxonomy ); ?>">
<div class="classifai-term-cleanup-process-status" data-taxonomy="<?php echo esc_attr( $taxonomy ); ?>">
<h4 style="font-size: 1.1em;">
<?php
// translators: %s: Taxonomy name.
Expand Down Expand Up @@ -1005,13 +1022,8 @@ public function get_taxonomy_label( $taxonomy, $plural = false ): string {
* Ajax handler for refresh compare status.
*/
public function get_term_cleanup_status() {
// TODO
if ( ! $this->background_process ) {
wp_send_json_error( [ 'error' => __( 'Background processing not enabled.', 'classifai' ) ] );
}

// Check the nonce for security
check_ajax_referer( 'classifai-status', 'nonce' );
check_ajax_referer( 'classifai-term-cleanup-status', 'nonce' );

$data = array(
'is_running' => false,
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ public function rest_endpoint_callback( $attachment_id, string $route_to_call =
}

if ( empty( $image_url ) ) {
return new WP_Error( 'error', esc_html__( 'Valid image size not found. Make sure the image is less than 4MB.' ) );
return new WP_Error( 'error', esc_html__( 'Valid image size not found. Make sure the image is less than 4MB.', 'classifai' ) );
}

switch ( $route_to_call ) {
Expand Down
33 changes: 29 additions & 4 deletions includes/Classifai/TermCleanupScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Classifai\Features\TermCleanup;
use ActionScheduler_Store;

use function as_enqueue_async_action;

class TermCleanupScheduler {

/**
Expand All @@ -30,8 +28,6 @@ public function __construct( string $job_name = '' ) {
*/
public function init() {
add_action( 'classifai_schedule_term_cleanup_job', [ $this, 'run' ] );
// add_filter( 'heartbeat_send', [ $this, 'check_embedding_generation_status' ] );
// add_action( 'classifai_before_feature_nav', [ $this, 'render_embeddings_generation_status' ] );
}

/**
Expand Down Expand Up @@ -163,4 +159,33 @@ public function in_progress(): bool {

return ! empty( $action_id );
}

/**
* Get the arguments for the current job.
*
* @return array|bool
*/
public function get_args() {
if ( ! class_exists( 'ActionScheduler_Store' ) ) {
return false;
}

$store = ActionScheduler_Store::instance();

$action_id = $store->find_action(
$this->job_name,
array(
'status' => ActionScheduler_Store::STATUS_PENDING,
)
);

if ( empty( $action_id ) ) {
return false;
}

$action = $store->fetch_action( $action_id );
$args = $action->get_args();

return $args;
}
}
46 changes: 46 additions & 0 deletions src/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,49 @@ document.addEventListener( 'DOMContentLoaded', function () {
}
} );
} )( jQuery );

// Update the Term Cleanup status.
( function ( $ ) {
const statusWrapper = $( '.classifai-term-cleanup-process-status' );
const processRunning = statusWrapper.length;
const taxonomy = statusWrapper.data( 'taxonomy' );
const ajaxUrl = classifai_term_cleanup_params.ajax_url;

Check failure on line 453 in src/js/admin.js

View workflow job for this annotation

GitHub Actions / eslint

Identifier 'classifai_term_cleanup_params' is not in camel case
const ajaxNonce = classifai_term_cleanup_params.ajax_nonce;

Check failure on line 454 in src/js/admin.js

View workflow job for this annotation

GitHub Actions / eslint

Identifier 'classifai_term_cleanup_params' is not in camel case

if ( ! processRunning || ! taxonomy ) {
return;
}

const intervalId = setInterval( function () {
$.ajax( {
url: ajaxUrl,
type: 'POST',
data: {
action: 'classifai_get_term_cleanup_status',
taxonomy,
nonce: ajaxNonce,
},
success( response ) {
if ( response.success && response.data ) {
if ( response.data.is_running && response.data.status ) {
// Update the sync status on the page
statusWrapper.html( response.data.status );
} else {
// Clear interval and reload the page.
clearInterval( intervalId );
window.location.reload();
}
}
},
error( jqXHR, textStatus, errorThrown ) {
// eslint-disable-next-line no-console
console.error(
'Error: ',
textStatus,
', Details: ',
errorThrown
);
},
} );
}, 30000 ); // 30000 milliseconds = 30 seconds
} )( jQuery );

0 comments on commit 6c928b9

Please sign in to comment.