get_scan(); if ( is_wp_error( $result ) ) { error_log( 'AI Product Tools: catalog scan failed: ' . $result->get_error_message() ); set_transient( self::FAILED_TRANSIENT, 1, self::FAILED_TTL ); return; } delete_transient( self::FAILED_TRANSIENT ); } /** * Unschedules the background refresh. Called on deactivation. */ public static function unschedule(): void { wp_clear_scheduled_hook( self::REFRESH_HOOK ); } /** * admin-post handler for the scan form. It counts, then returns to the Dashboard. * A failed scan is remembered briefly and the widget says so; a partial scan shows what is stored. */ public static function handle_scan(): void { check_admin_referer( self::SCAN_ACTION ); if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'ai-product-tools' ), '', array( 'response' => 403 ) ); } self::scan(); wp_safe_redirect( admin_url( 'index.php' ) ); exit; } public static function render(): void { if ( ! current_user_can( self::CAPABILITY ) ) { echo '

' . esc_html__( 'You do not have permission to do this.', 'ai-product-tools' ) . '

'; return; } $scan = ( new CatalogScanService() )->get_cached_scan(); $failed = false !== get_transient( self::FAILED_TRANSIENT ); if ( null === $scan ) { if ( $failed ) { echo '

' . esc_html__( 'The last count did not finish. Try Count now.', 'ai-product-tools' ) . '

'; } else { self::schedule_refresh(); echo '

' . esc_html__( 'Counting your product content. Check back in a minute.', 'ai-product-tools' ) . '

'; } self::render_scan_form( __( 'Count now', 'ai-product-tools' ) ); return; } if ( $failed ) { echo '

' . esc_html__( 'The last count did not finish. Try Count again.', 'ai-product-tools' ) . '

'; } if ( ! empty( $scan['partial'] ) && ! $failed ) { self::schedule_refresh(); } $total = isset( $scan['total'] ) && is_numeric( $scan['total'] ) ? (int) $scan['total'] : 0; if ( $total < GapRanking::SMALL_STORE_LIMIT ) { echo '

' . esc_html__( 'Gaps will show up here as your catalog grows.', 'ai-product-tools' ) . '

'; self::render_scan_form( __( 'Count again', 'ai-product-tools' ) ); return; } $rows = array_slice( GapRanking::rank( $scan ), 0, self::MAX_ROWS ); if ( array() === $rows ) { echo '

' . esc_html__( 'Your product content looks complete. Gaps will show up here.', 'ai-product-tools' ) . '

'; self::render_checked_line( $scan ); self::render_scan_form( __( 'Count again', 'ai-product-tools' ) ); return; } echo ''; $url = GetCatalogGapsTool::gapUrl( (string) $rows[0]['gap'] ); echo '

' . esc_html__( 'Open the generator', 'ai-product-tools' ) . '

'; self::render_checked_line( $scan ); self::render_scan_form( __( 'Count again', 'ai-product-tools' ) ); } /** * Queues one background scan. wp_next_scheduled() keeps repeated renders from stacking events. */ private static function schedule_refresh(): void { if ( false === wp_next_scheduled( self::REFRESH_HOOK ) ) { wp_schedule_single_event( time(), self::REFRESH_HOOK ); } } private static function render_scan_form( string $label ): void { echo '
'; echo ''; wp_nonce_field( self::SCAN_ACTION ); echo ''; echo '
'; } /** * @param array $scan Scan payload with `scannedAt`. */ private static function render_checked_line( array $scan ): void { $scanned_at = isset( $scan['scannedAt'] ) ? strtotime( (string) $scan['scannedAt'] ) : false; if ( false === $scanned_at ) { return; } echo '

' . esc_html( sprintf( /* translators: %s: time since the last count, such as "5 minutes". */ __( 'Checked %s ago.', 'ai-product-tools' ), human_time_diff( $scanned_at ) ) ) . '

'; } private static function row_text( string $gap, int $count ): string { $number = number_format_i18n( $count ); switch ( $gap ) { case 'short_description': /* translators: %s: number of products. */ $text = _n( '%s product has no short description', '%s products have no short description', $count, 'ai-product-tools' ); break; case 'tags': /* translators: %s: number of products. */ $text = _n( '%s product has no tags', '%s products have no tags', $count, 'ai-product-tools' ); break; case 'seo_meta': /* translators: %s: number of products. */ $text = _n( '%s product has no SEO description', '%s products have no SEO description', $count, 'ai-product-tools' ); break; case 'faq': /* translators: %s: number of products. */ $text = _n( '%s product has no FAQ', '%s products have no FAQ', $count, 'ai-product-tools' ); break; default: /* translators: %s: number of products. */ $text = _n( '%s product has no description', '%s products have no description', $count, 'ai-product-tools' ); } return sprintf( $text, $number ); } }