diff --git a/admin/views/embeddings.php b/admin/views/embeddings.php
index 5295d8f..e23f232 100644
--- a/admin/views/embeddings.php
+++ b/admin/views/embeddings.php
@@ -101,35 +101,35 @@ class="regular-text">
$db_type = $database->get_db_type();
$has_vector_support = $database->has_native_vector_support() ? 'Yes' : 'No';
- error_log( '[WPVDB DEBUG] Performing semantic search for query: ' . $search_query );
- error_log( '[WPVDB DEBUG] API Key exists: ' . ( ! empty( $api_key ) ? 'Yes' : 'No' ) );
- error_log( '[WPVDB DEBUG] Model: ' . $model );
- error_log( '[WPVDB DEBUG] API Base: ' . $api_base );
+ \WPVDB\Logger::debug( 'Performing semantic search for query: ' . $search_query );
+ \WPVDB\Logger::debug( 'API key exists: ' . ( ! empty( $api_key ) ? 'Yes' : 'No' ) );
+ \WPVDB\Logger::debug( 'Model: ' . $model );
+ \WPVDB\Logger::debug( 'API base: ' . $api_base );
if ( $api_key && $model ) {
try {
$embedding_result = \WPVDB\Core::get_embedding( $search_query, $model, $api_base, $api_key );
if ( is_wp_error( $embedding_result ) ) {
- error_log( '[WPVDB ERROR] Error getting embedding: ' . $embedding_result->get_error_message() );
+ \WPVDB\Logger::error( 'Error getting embedding: ' . $embedding_result->get_error_message() );
} else {
- error_log( '[WPVDB DEBUG] Successfully generated embedding with dimensions: ' . count( $embedding_result ) );
+ \WPVDB\Logger::debug( 'Successfully generated embedding with dimensions: ' . count( $embedding_result ) );
$embedding = $embedding_result;
$has_vector = $database->has_native_vector_support();
- error_log( '[WPVDB DEBUG] Database has native vector support: ' . ( $has_vector ? 'Yes' : 'No' ) );
+ \WPVDB\Logger::debug( 'Database has native vector support: ' . ( $has_vector ? 'Yes' : 'No' ) );
if ( $has_vector ) {
// Convert the embedding array to JSON.
- $embedding_json = json_encode( $embedding );
+ $embedding_json = wp_json_encode( $embedding );
// Use Database class to get the appropriate vector function.
$vector_function = $database->get_vector_from_string_function( $embedding_json );
- error_log( '[WPVDB DEBUG] Using vector function: ' . $vector_function );
+ \WPVDB\Logger::debug( 'Using vector function: ' . $vector_function );
// Get total count of vectors.
$total_vectors_searched = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
- error_log( '[WPVDB DEBUG] Total vectors searched: ' . $total_vectors_searched );
+ \WPVDB\Logger::debug( 'Total vectors searched: ' . $total_vectors_searched );
// Use Database class to get the appropriate distance function with both vectors.
$db_type = $database->get_db_type();
@@ -138,7 +138,7 @@ class="regular-text">
} else {
$distance_function = "DISTANCE(e.embedding, $vector_function, 'COSINE')";
}
- error_log( '[WPVDB DEBUG] Using distance function: ' . $distance_function );
+ \WPVDB\Logger::debug( 'Using distance function: ' . $distance_function );
// Optimized query that will use the vector index.
// The ORDER BY + LIMIT pattern is what triggers the vector index usage.
@@ -153,34 +153,34 @@ class="regular-text">
20 // Show top 20 matches.
);
- error_log( '[WPVDB DEBUG] Executing SQL query: ' . $sql );
+ \WPVDB\Logger::debug( 'Executing SQL query: ' . $sql );
$search_results = $wpdb->get_results( $sql );
if ( $wpdb->last_error ) {
- error_log( '[WPVDB ERROR] SQL error: ' . $wpdb->last_error );
+ \WPVDB\Logger::error( 'SQL error: ' . $wpdb->last_error );
// Try executing a simpler query to test database connection.
$test_query = "SELECT COUNT(*) FROM $table_name";
$test_result = $wpdb->get_var( $test_query );
if ( $wpdb->last_error ) {
- error_log( '[WPVDB ERROR] Even simple query failed: ' . $wpdb->last_error );
+ \WPVDB\Logger::error( 'Even simple query failed: ' . $wpdb->last_error );
} else {
- error_log( '[WPVDB DEBUG] Simple query succeeded, embedding count: ' . $test_result );
+ \WPVDB\Logger::debug( 'Simple query succeeded, embedding count: ' . $test_result );
// Try a direct query without the vector function to see if that's the issue.
$basic_query = "SELECT e.* FROM $table_name e LIMIT 20";
$basic_results = $wpdb->get_results( $basic_query );
if ( $wpdb->last_error ) {
- error_log( '[WPVDB ERROR] Basic query failed: ' . $wpdb->last_error );
+ \WPVDB\Logger::error( 'Basic query failed: ' . $wpdb->last_error );
} else {
- error_log( '[WPVDB DEBUG] Basic query succeeded, returned ' . count( $basic_results ) . ' results' );
- error_log( '[WPVDB DEBUG] Issue is likely with the vector function: ' . $distance_function );
+ \WPVDB\Logger::debug( 'Basic query succeeded, returned ' . count( $basic_results ) . ' results' );
+ \WPVDB\Logger::debug( 'Issue is likely with the vector function: ' . $distance_function );
// Fall back to PHP-based distance calculation.
- error_log( '[WPVDB DEBUG] Falling back to PHP-based distance calculation' );
+ \WPVDB\Logger::debug( 'Falling back to PHP-based distance calculation' );
$all_rows = $wpdb->get_results(
$wpdb->prepare( "SELECT * FROM $table_name WHERE model = %s", $model ),
ARRAY_A
@@ -205,16 +205,16 @@ function ( $a, $b ) {
);
$search_results = array_slice( $distances, 0, 20 );
- $search_results = json_decode( json_encode( $search_results ) ); // Convert to objects.
+ $search_results = json_decode( wp_json_encode( $search_results ) ); // Convert to objects.
- error_log( '[WPVDB DEBUG] PHP fallback found ' . count( $search_results ) . ' results' );
+ \WPVDB\Logger::debug( 'PHP fallback found ' . count( $search_results ) . ' results' );
}
}
} else {
- error_log( '[WPVDB DEBUG] Found ' . count( $search_results ) . ' results' );
+ \WPVDB\Logger::debug( 'Found ' . count( $search_results ) . ' results' );
if ( count( $search_results ) > 0 ) {
- error_log(
- '[WPVDB DEBUG] First result distance: ' .
+ \WPVDB\Logger::debug(
+ 'First result distance: ' .
( isset( $search_results[0]->distance ) ?
$search_results[0]->distance : 'Not set' )
);
@@ -222,20 +222,20 @@ function ( $a, $b ) {
}
} else {
// Fallback: do in PHP.
- error_log( '[WPVDB DEBUG] Using PHP fallback search' );
+ \WPVDB\Logger::debug( 'Using PHP fallback search' );
$all_rows = $wpdb->get_results(
$wpdb->prepare( "SELECT * FROM $table_name WHERE model = %s", $model ),
ARRAY_A
);
$total_vectors_searched = count( $all_rows );
- error_log( '[WPVDB DEBUG] Total vectors searched: ' . $total_vectors_searched );
+ \WPVDB\Logger::debug( 'Total vectors searched: ' . $total_vectors_searched );
$distances = array();
foreach ( $all_rows as $r ) {
$stored_emb = json_decode( $r['embedding'], true );
if ( ! is_array( $stored_emb ) ) {
- error_log( '[WPVDB DEBUG] Invalid embedding in row: ' . $r['id'] );
+ \WPVDB\Logger::debug( 'Invalid embedding in row: ' . $r['id'] );
continue;
}
$similarity_score = \WPVDB\REST::cosine_distance( $embedding, $stored_emb );
@@ -251,12 +251,12 @@ function ( $a, $b ) {
);
$search_results = array_slice( $distances, 0, 20 );
- $search_results = json_decode( json_encode( $search_results ) ); // Convert to objects.
+ $search_results = json_decode( wp_json_encode( $search_results ) ); // Convert to objects.
- error_log( '[WPVDB DEBUG] PHP fallback found ' . count( $search_results ) . ' results' );
+ \WPVDB\Logger::debug( 'PHP fallback found ' . count( $search_results ) . ' results' );
if ( count( $search_results ) > 0 ) {
- error_log(
- '[WPVDB DEBUG] First result similarity score: ' .
+ \WPVDB\Logger::debug(
+ 'First result similarity score: ' .
( isset( $search_results[0]->distance ) ?
$search_results[0]->distance : 'Not set' )
);
@@ -271,11 +271,11 @@ function ( $a, $b ) {
}
} catch ( \Exception $e ) {
// Handle errors.
- error_log( '[WPVDB ERROR] Exception: ' . $e->getMessage() );
+ \WPVDB\Logger::error( 'Exception: ' . $e->getMessage() );
echo '
' . esc_html__( 'Error performing semantic search: ', 'wpvdb' ) . esc_html( $e->getMessage() ) . '
';
}
} else {
- error_log( '[WPVDB ERROR] API key or model not configured' );
+ \WPVDB\Logger::error( 'API key or model not configured' );
echo '' . esc_html__( 'API key or model not configured. Please check your settings.', 'wpvdb' ) . '
';
}
}
@@ -323,7 +323,7 @@ function ( $a, $b ) {
if ( $total_embeddings > 0 ) {
$embeddings_query = "SELECT * FROM $table_name ORDER BY id DESC LIMIT 20";
$embeddings = $wpdb->get_results( $embeddings_query );
- error_log( '[WPVDB DEBUG] Loaded ' . count( $embeddings ) . ' embeddings for display' );
+ \WPVDB\Logger::debug( 'Loaded ' . count( $embeddings ) . ' embeddings for display' );
}
}
?>
@@ -410,7 +410,7 @@ function ( $a, $b ) {
} else {
// If no distance property, check what properties are available.
$props = array_keys( get_object_vars( $embedding ) );
- error_log( '[WPVDB DEBUG] Properties available: ' . print_r( $props, true ) );
+ \WPVDB\Logger::debug( 'Properties available: ' . wp_json_encode( $props ) );
echo esc_html__( 'No similarity data available', 'wpvdb' );
}
diff --git a/admin/views/status.php b/admin/views/status.php
index e312fce..1438248 100644
--- a/admin/views/status.php
+++ b/admin/views/status.php
@@ -464,7 +464,7 @@
$debug_settings['automattic']['api_key'] = '********' . substr( $debug_settings['automattic']['api_key'], -4 );
}
- echo esc_html( print_r( $debug_settings, true ) );
+ echo esc_html( wp_json_encode( $debug_settings, JSON_PRETTY_PRINT ) );
?>
@@ -480,7 +480,7 @@
'embedding_table_exists' => $embedding_table_exists,
'embedding_count' => $system_info['embedding_count'],
);
- echo esc_html( print_r( $db_info, true ) );
+ echo esc_html( wp_json_encode( $db_info, JSON_PRETTY_PRINT ) );
?>
diff --git a/includes/class-wpvdb-activation.php b/includes/class-wpvdb-activation.php
index 3d8f0d1..e9adb6e 100644
--- a/includes/class-wpvdb-activation.php
+++ b/includes/class-wpvdb-activation.php
@@ -64,8 +64,7 @@ public static function activate() {
update_option( 'wpvdb_incompatible_db', true );
// Log the incompatible activation.
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Activated on incompatible database. Vector features require MySQL 8.0.32+ or MariaDB 11.7+.' ); }
+ Logger::warning( 'Activated on incompatible database. Vector features require MySQL 8.0.32+ or MariaDB 11.7+.' );
// Restore error reporting and return early (we'll show the warning later).
error_reporting( $old_error_reporting );
@@ -222,8 +221,7 @@ public static function add_vector_index_to_existing_table() {
// Only proceed if database is ready and we've initialized properly.
if ( ! self::$database ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Database not initialized, skipping vector index creation' ); }
+ Logger::warning( 'Database not initialized, skipping vector index creation' );
return false;
}
@@ -233,8 +231,7 @@ public static function add_vector_index_to_existing_table() {
try {
$table_exists = $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) === $table_name;
} catch ( \Exception $e ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Error checking if table exists: ' . $e->getMessage() ); }
+ Logger::error( 'Error checking if table exists: ' . $e->getMessage() );
return false;
}
@@ -246,8 +243,7 @@ public static function add_vector_index_to_existing_table() {
$is_mariadb = self::$database->get_db_type() === 'mariadb';
$has_vector_support = $is_mariadb && self::$database->has_native_vector_support();
} catch ( \Exception $e ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Error checking database type or vector support: ' . $e->getMessage() ); }
+ Logger::error( 'Error checking database type or vector support: ' . $e->getMessage() );
return false;
}
@@ -259,8 +255,7 @@ public static function add_vector_index_to_existing_table() {
$index_check = $wpdb->get_results( "SHOW INDEX FROM $table_name WHERE Key_name = 'embedding_idx'" );
$index_exists = ! empty( $index_check );
} catch ( \Exception $e ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Error checking for existing index: ' . $e->getMessage() ); }
+ Logger::warning( 'Error checking for existing index: ' . $e->getMessage() );
}
if ( ! $index_exists ) {
@@ -273,8 +268,7 @@ public static function add_vector_index_to_existing_table() {
);
if ( false === $result ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Failed to add vector index using new syntax: ' . $wpdb->last_error ); }
+ Logger::warning( 'Failed to add vector index using new syntax: ' . $wpdb->last_error );
// Try with simpler syntax as fallback.
$result = $wpdb->query(
@@ -285,16 +279,15 @@ public static function add_vector_index_to_existing_table() {
);
if ( false !== $result ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Added vector index with simplified syntax' ); }
- } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Failed to add vector index with simplified syntax: ' . $wpdb->last_error );
+ Logger::debug( 'Added vector index with simplified syntax' );
+ } else {
+ Logger::error( 'Failed to add vector index with simplified syntax: ' . $wpdb->last_error );
}
- } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Added optimized vector index to embeddings table' );
+ } else {
+ Logger::debug( 'Added optimized vector index to embeddings table' );
}
- } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Vector index already exists, skipping creation' );
+ } else {
+ Logger::debug( 'Vector index already exists, skipping creation' );
}
// After creating the main vector index, add supporting indexes if needed.
@@ -313,27 +306,23 @@ public static function add_vector_index_to_existing_table() {
}
} catch ( \Exception $e ) {
// Ignore errors for supporting indexes, they're not critical.
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( "[WPVDB] Error creating supporting index $index_name: " . $e->getMessage() ); }
+ Logger::warning( "Error creating supporting index $index_name: " . $e->getMessage() );
}
}
} catch ( \Exception $e ) {
// Ignore errors for supporting indexes, they're not critical.
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Error creating supporting indexes: ' . $e->getMessage() ); }
+ Logger::warning( 'Error creating supporting indexes: ' . $e->getMessage() );
}
} catch ( \Exception $e ) {
// Log error but don't let it crash the activation.
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Error adding vector index: ' . $e->getMessage() ); }
+ Logger::error( 'Error adding vector index: ' . $e->getMessage() );
}
}
return true;
} catch ( \Exception $e ) {
// Catch all exceptions to prevent activation failure.
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Fatal error in add_vector_index_to_existing_table: ' . $e->getMessage() ); }
+ Logger::critical( 'Fatal error in add_vector_index_to_existing_table: ' . $e->getMessage() );
return false;
}
}
@@ -377,13 +366,11 @@ public static function recreate_tables() {
// Update table statistics for optimal query planning.
$wpdb->query( "ANALYZE TABLE $table_name" );
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Added optimized vector index and supporting indexes to embeddings table' ); }
+ Logger::debug( 'Added optimized vector index and supporting indexes to embeddings table' );
}
} catch ( \Exception $e ) {
// Ignore errors.
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Error adding vector index during recreation: ' . $e->getMessage() ); }
+ Logger::error( 'Error adding vector index during recreation: ' . $e->getMessage() );
}
}
diff --git a/includes/class-wpvdb-admin.php b/includes/class-wpvdb-admin.php
index 0c9d2fe..8976c35 100644
--- a/includes/class-wpvdb-admin.php
+++ b/includes/class-wpvdb-admin.php
@@ -1082,8 +1082,7 @@ public function ajax_confirm_provider_change() {
$cancel = true;
}
}
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( 'WPVDB: Cancel flag: ' . ( $cancel ? 'true' : 'false' ) ); }
+ Logger::debug( 'Cancel flag: ' . ( $cancel ? 'true' : 'false' ) );
$settings = get_option( 'wpvdb_settings', array() );
@@ -1141,8 +1140,7 @@ public function ajax_confirm_provider_change() {
} else {
// Mirror of handle_apply_provider_change for the AJAX path.
if ( empty( $settings['pending_provider'] ) || empty( $settings['pending_model'] ) ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( 'WPVDB: No pending provider change found' ); }
+ Logger::debug( 'No pending provider change found' );
wp_send_json_error(
array(
'message' => __( 'No pending provider change found.', 'wpvdb' ),
@@ -1843,7 +1841,7 @@ public function ajax_test_embedding() {
// Get sample of embedding values (first 5).
$sample = array_slice( $embedding, 0, 5 );
- $sample_json = json_encode( $sample, JSON_PRETTY_PRINT );
+ $sample_json = wp_json_encode( $sample, JSON_PRETTY_PRINT );
wp_send_json_success(
array(
@@ -1925,8 +1923,7 @@ public function handle_admin_actions() {
}
// Log that diagnostics were run.
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ADMIN] Running database diagnostics from admin UI' ); }
+ Logger::debug( 'Running database diagnostics from admin UI' );
// Redirect back to the page with a parameter to show diagnostics.
wp_redirect( add_query_arg( 'diagnostics', 'run', admin_url( 'admin.php?page=wpvdb-status' ) ) );
@@ -2747,8 +2744,7 @@ public function handle_cancel_provider_change() {
admin_url( 'admin.php' )
);
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( 'WPVDB CRITICAL: Redirecting to: ' . $redirect_url ); }
+ Logger::debug( 'Redirecting to: ' . $redirect_url );
wp_redirect( $redirect_url );
exit;
}
diff --git a/includes/class-wpvdb-cache.php b/includes/class-wpvdb-cache.php
index 28336c3..1800ee0 100644
--- a/includes/class-wpvdb-cache.php
+++ b/includes/class-wpvdb-cache.php
@@ -123,9 +123,7 @@ public static function invalidate_document_cache( $doc_id ) {
wp_cache_delete( 'db_stats', self::CACHE_GROUP );
// Log cache invalidation.
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( "[WPVDB CACHE] Invalidated cache for document {$doc_id}" );
- }
+ Logger::debug( "Invalidated cache for document {$doc_id}" );
}
/**
@@ -230,10 +228,7 @@ public static function flush_all() {
self::invalidate_query_cache();
wp_cache_delete( 'db_stats', self::CACHE_GROUP );
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB CACHE] Flushed all caches' ); }
- }
+ Logger::debug( 'Flushed all caches' );
}
/**
@@ -317,10 +312,7 @@ public static function preload_popular_embeddings() {
}
}
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( "[WPVDB CACHE] Preloaded {$preloaded} embeddings" ); }
- }
+ Logger::debug( "Preloaded {$preloaded} embeddings" );
return $preloaded;
}
diff --git a/includes/class-wpvdb-core.php b/includes/class-wpvdb-core.php
index e8efb52..dab8b70 100644
--- a/includes/class-wpvdb-core.php
+++ b/includes/class-wpvdb-core.php
@@ -73,7 +73,7 @@ public function default_chunking( $chunks, $text ) {
// Ensure text is a string.
if ( ! is_string( $text ) ) {
if ( is_array( $text ) || is_object( $text ) ) {
- $text = json_encode( $text );
+ $text = wp_json_encode( $text );
} else {
$text = strval( $text );
}
@@ -119,7 +119,7 @@ public function default_summary( $summary, $text ) {
// Ensure text is a string.
if ( ! is_string( $text ) ) {
if ( is_array( $text ) || is_object( $text ) ) {
- $text = json_encode( $text );
+ $text = wp_json_encode( $text );
} else {
$text = strval( $text );
}
@@ -542,7 +542,7 @@ public static function enhanced_chunking( $chunks, $text, $chunk_size = null ) {
// Ensure text is a string.
if ( ! is_string( $text ) ) {
if ( is_array( $text ) || is_object( $text ) ) {
- $text = json_encode( $text );
+ $text = wp_json_encode( $text );
} else {
$text = strval( $text );
}
diff --git a/includes/class-wpvdb-database.php b/includes/class-wpvdb-database.php
index 1728b05..860643c 100644
--- a/includes/class-wpvdb-database.php
+++ b/includes/class-wpvdb-database.php
@@ -144,8 +144,7 @@ public function has_native_vector_support() {
// Check for MariaDB version with vector support.
try {
$version = $wpdb->get_var( 'SELECT VERSION()' );
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Database version: ' . $version ); }
+ Logger::debug( 'Database version: ' . $version );
if ( stripos( $version, 'MariaDB' ) !== false ) {
// Extract version number.
@@ -166,8 +165,7 @@ public function has_native_vector_support() {
}
}
} catch ( \Exception $e ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Error checking database version: ' . $e->getMessage() ); }
+ Logger::warning( 'Error checking database version: ' . $e->getMessage() );
return false;
}
@@ -178,13 +176,11 @@ public function has_native_vector_support() {
$check = $wpdb->get_var( "SELECT COUNT(*) FROM information_schema.columns WHERE column_type LIKE 'VECTOR%' LIMIT 1" );
if ( null === $check && $wpdb->last_error ) {
// Failed to query for VECTOR type, might not be supported.
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Vector type check failed: ' . $wpdb->last_error ); }
+ Logger::warning( 'Vector type check failed: ' . $wpdb->last_error );
$this->has_vector_support = false;
}
} catch ( \Exception $e ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Error checking vector support: ' . $e->getMessage() ); }
+ Logger::warning( 'Error checking vector support: ' . $e->getMessage() );
$this->has_vector_support = false;
}
}
@@ -210,8 +206,7 @@ public function has_native_vector_support() {
}
}
} catch ( \Exception $e ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Error checking database version: ' . $e->getMessage() ); }
+ Logger::warning( 'Error checking database version: ' . $e->getMessage() );
return false;
}
}
@@ -219,7 +214,7 @@ public function has_native_vector_support() {
Logger::info( 'Vector support determination completed', array( 'has_support' => $this->has_vector_support ) );
return $this->has_vector_support;
} catch ( \Exception $e ) {
- Logger::log_exception( $e, 'Fatal error checking vector support' );
+ Logger::log_exception( $e, 'Error checking vector support' );
return false;
}
}
@@ -571,8 +566,7 @@ public function delete_post_embeddings( $post_id ) {
delete_post_meta( $post_id, '_wpvdb_embedded_model' );
// Log the deletion.
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( "[WPVDB] Deleted embeddings for post ID: $post_id" ); }
+ Logger::debug( "Deleted embeddings for post ID: $post_id" );
}
}
@@ -616,8 +610,7 @@ public function add_vector_index( $m_value = 16, $distance_type = 'cosine' ) {
return false !== $result;
} catch ( \Exception $e ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ERROR] Failed to add vector index: ' . $e->getMessage() ); }
+ Logger::error( 'Failed to add vector index: ' . $e->getMessage() );
return false;
}
}
@@ -667,8 +660,7 @@ public function optimize_vector_performance() {
return true;
} catch ( \Exception $e ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ERROR] Failed to optimize vector performance: ' . $e->getMessage() ); }
+ Logger::error( 'Failed to optimize vector performance: ' . $e->getMessage() );
return false;
}
}
diff --git a/includes/class-wpvdb-logger.php b/includes/class-wpvdb-logger.php
index 95a4641..cbb33e2 100644
--- a/includes/class-wpvdb-logger.php
+++ b/includes/class-wpvdb-logger.php
@@ -171,7 +171,7 @@ public static function log( $level, $message, $context = array() ) {
$message,
! empty( $context ) ? wp_json_encode( $context ) : ''
);
- error_log( $formatted_message );
+ error_log( $formatted_message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Centralized log sink.
}
// Store in database for admin viewing.
diff --git a/includes/class-wpvdb-plugin.php b/includes/class-wpvdb-plugin.php
index e577a16..fad4fcc 100644
--- a/includes/class-wpvdb-plugin.php
+++ b/includes/class-wpvdb-plugin.php
@@ -287,8 +287,7 @@ public function maybe_deactivate_plugin() {
// Set a transient to show a notice after deactivation.
set_transient( 'wpvdb_was_deactivated', true, 60 * 60 );
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB] Plugin deactivated due to incompatible database.' ); }
+ Logger::warning( 'Plugin deactivated due to incompatible database.' );
// If this is running in a CRON or admin context without a screen, we're done.
if ( ! function_exists( 'get_current_screen' ) || ! get_current_screen() ) {
diff --git a/includes/class-wpvdb-providers.php b/includes/class-wpvdb-providers.php
index 2ec9506..ac0f2db 100644
--- a/includes/class-wpvdb-providers.php
+++ b/includes/class-wpvdb-providers.php
@@ -102,7 +102,7 @@ public static function is_automattic_ai_proxy_url( $url ) {
return false;
}
- $parts = parse_url( $url );
+ $parts = wp_parse_url( $url );
if ( ! is_array( $parts ) || empty( $parts['host'] ) || empty( $parts['path'] ) ) {
return false;
}
diff --git a/includes/class-wpvdb-query.php b/includes/class-wpvdb-query.php
index 8d43031..749fad2 100644
--- a/includes/class-wpvdb-query.php
+++ b/includes/class-wpvdb-query.php
@@ -64,8 +64,7 @@ public static function maybe_vector_search( $query ) {
return;
}
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] maybe_vector_search triggered with query: ' . $vdb_query ); }
+ Logger::debug( 'maybe_vector_search triggered with query: ' . $vdb_query );
// For simplicity, embed and do a fallback search. Then get the doc_ids, presumably post_id was stored as doc_id.
global $wpdb;
@@ -78,8 +77,7 @@ public static function maybe_vector_search( $query ) {
}
if ( ! $api_key ) {
// If there's no stored key, we can't generate embeddings. We skip.
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ERROR] No API key found, skipping vector search' ); }
+ Logger::error( 'No API key found, skipping vector search' );
return;
}
@@ -88,54 +86,46 @@ public static function maybe_vector_search( $query ) {
if ( empty( $model ) ) {
$model = Models::get_default_model_for_provider( 'openai' );
}
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Using embedding model: ' . $model ); }
+ Logger::debug( 'Using embedding model: ' . $model );
// Get API base URL with fallback.
$api_base = Settings::get_api_base();
if ( empty( $api_base ) ) {
$api_base = 'https://api.openai.com/v1/';
}
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Using API base: ' . $api_base ); }
+ Logger::debug( 'Using API base: ' . $api_base );
try {
$embedding_result = Core::get_embedding( $vdb_query, $model, $api_base, $api_key );
if ( is_wp_error( $embedding_result ) ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ERROR] Error generating embedding: ' . $embedding_result->get_error_message() ); }
+ Logger::error( 'Error generating embedding: ' . $embedding_result->get_error_message() );
return; // skip.
}
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Embedding generated successfully, dimensions: ' . count( $embedding_result ) ); }
+ Logger::debug( 'Embedding generated successfully, dimensions: ' . count( $embedding_result ) );
$embedding = $embedding_result;
$has_vector = self::$database->has_native_vector_support();
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Vector support detected: ' . ( $has_vector ? 'Yes' : 'No' ) ); }
+ Logger::debug( 'Vector support detected: ' . ( $has_vector ? 'Yes' : 'No' ) );
$limit = $query->get( 'posts_per_page' );
$limit = $limit ? $limit : 10;
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Posts per page limit: ' . $limit ); }
+ Logger::debug( 'Posts per page limit: ' . $limit );
$doc_ids = array();
if ( $has_vector ) {
try {
// Convert the embedding array to JSON.
- $embedding_json = json_encode( $embedding );
+ $embedding_json = wp_json_encode( $embedding );
// Use Database class to get the appropriate vector function.
$vector_function = self::$database->get_vector_from_string_function( $embedding_json );
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Using vector function: ' . $vector_function ); }
+ Logger::debug( 'Using vector function: ' . $vector_function );
// Use Database class to get the appropriate distance function.
$distance_function = self::$database->get_vector_distance_function( 'embedding', $vector_function, 'cosine' );
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Using distance function: ' . $distance_function ); }
+ Logger::debug( 'Using distance function: ' . $distance_function );
// Set an appropriate similarity threshold - we discovered this is critical for performance
// Lower values (0.2-0.3) are more strict but faster, higher values (0.4-0.6) give more results.
@@ -158,34 +148,28 @@ public static function maybe_vector_search( $query ) {
$limit * 3 // fetch more candidates than needed.
);
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Vector search SQL: ' . $sql ); }
+ Logger::debug( 'Vector search SQL: ' . $sql );
$rows = $wpdb->get_results( $sql, ARRAY_A );
if ( $wpdb->last_error ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ERROR] Database error in vector search: ' . $wpdb->last_error ); }
+ Logger::error( 'Database error in vector search: ' . $wpdb->last_error );
}
if ( $rows ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Found ' . count( $rows ) . ' results from vector search' ); }
+ Logger::debug( 'Found ' . count( $rows ) . ' results from vector search' );
foreach ( $rows as $r ) {
$doc_ids[] = (int) $r['doc_id'];
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Added doc_id: ' . $r['doc_id'] . ' with distance: ' . $r['distance'] ); }
+ Logger::debug( 'Added doc_id: ' . $r['doc_id'] . ' with distance: ' . $r['distance'] );
}
- } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] No results found from vector search' );
+ } else {
+ Logger::debug( 'No results found from vector search' );
}
} catch ( \Exception $e ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ERROR] Exception in vector search: ' . $e->getMessage() ); }
+ Logger::error( 'Exception in vector search: ' . $e->getMessage() );
}
} else {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] No vector support, using PHP fallback search' ); }
+ Logger::debug( 'No vector support, using PHP fallback search' );
// Fallback: do in PHP.
$all_rows = $wpdb->get_results(
$wpdb->prepare( "SELECT doc_id, embedding FROM $table_name WHERE model = %s", $model ),
@@ -224,8 +208,7 @@ function ( $a, $b ) {
$query->set( 'post__in', $doc_ids );
$query->set( 'orderby', 'post__in' );
} catch ( \Exception $e ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ERROR] Unhandled exception in maybe_vector_search: ' . $e->getMessage() ); }
+ Logger::error( 'Unhandled exception in maybe_vector_search: ' . $e->getMessage() );
}
}
diff --git a/includes/class-wpvdb-queue.php b/includes/class-wpvdb-queue.php
index 9e670d2..6001d8a 100644
--- a/includes/class-wpvdb-queue.php
+++ b/includes/class-wpvdb-queue.php
@@ -456,8 +456,7 @@ private static function process_post( $post, $model, $provider = '' ) {
$existing_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE doc_id = %d", $post->ID ) );
if ( $existing_count > 0 ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( "[WPVDB] Deleting {$existing_count} existing embeddings for post {$post->ID} before creating new ones." ); }
+ Logger::debug( "Deleting {$existing_count} existing embeddings for post {$post->ID} before creating new ones." );
$wpdb->delete( $table_name, array( 'doc_id' => $post->ID ), array( '%d' ) );
// Bust here so cache invalidates even if no inserts succeed below.
Cache::invalidate_query_cache();
diff --git a/includes/class-wpvdb-rest.php b/includes/class-wpvdb-rest.php
index f72bca4..80a75e8 100644
--- a/includes/class-wpvdb-rest.php
+++ b/includes/class-wpvdb-rest.php
@@ -260,7 +260,7 @@ public static function handle_embed( WP_REST_Request $request ) {
// Ensure text is a string.
if ( ! is_string( $text ) ) {
if ( is_array( $text ) || is_object( $text ) ) {
- $text = json_encode( $text );
+ $text = wp_json_encode( $text );
} else {
$text = strval( $text );
}
@@ -439,9 +439,7 @@ public static function handle_query( \WP_REST_Request $request ) {
self::init_database();
- if ( \wpvdb_should_log_to_error_log( 'debug', 'handle_query called' ) ) {
- error_log( '[WPVDB DEBUG] handle_query called' );
- }
+ Logger::debug( 'handle_query called' );
$data = $request->get_json_params();
if ( ! is_array( $data ) ) {
$data = array();
@@ -915,17 +913,15 @@ public static function handle_metadata( \WP_REST_Request $request ) { // phpcs:i
*/
public static function insert_embedding_row( $doc_id, $chunk_id, $chunk_content, $summary, $embedding, $model = '', $doc_type = 'post', $chunk_index = null ) {
// Detect callers using the legacy 5/6/7-arg signature; fall back to 0 for backward
- // compatibility but emit a debug-only warning so the regression is visible.
+ // compatibility but emit a warning so the regression is visible.
if ( null === $chunk_index ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB WARN] insert_embedding_row called without chunk_index; defaulting to 0' ); }
+ Logger::warning( 'insert_embedding_row called without chunk_index; defaulting to 0' );
$chunk_index = 0;
}
$chunk_index = (int) $chunk_index;
if ( ! Core::is_valid_embedding( $embedding ) ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ERROR] insert_embedding_row rejected invalid embedding for doc_id=' . $doc_id . ' chunk_index=' . $chunk_index ); }
+ Logger::error( 'insert_embedding_row rejected invalid embedding for doc_id=' . $doc_id . ' chunk_index=' . $chunk_index );
return new \WP_Error(
'embedding_invalid',
'Refused to store an embedding that is empty, non-finite, or zero-magnitude.',
@@ -944,8 +940,7 @@ public static function insert_embedding_row( $doc_id, $chunk_id, $chunk_content,
// First, check if the table exists.
if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) !== $table_name ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ERROR] Embeddings table does not exist' ); }
+ Logger::error( 'Embeddings table does not exist' );
return new \WP_Error(
'embedding_table_missing',
'The wpvdb embeddings table does not exist. Run plugin activation or the schema migration.',
@@ -958,18 +953,16 @@ public static function insert_embedding_row( $doc_id, $chunk_id, $chunk_content,
// Check for vector support and handle storage differently.
$has_vector = self::$database->has_native_vector_support();
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Vector support detected: ' . ( $has_vector ? 'Yes' : 'No' ) ); }
+ Logger::debug( 'Vector support detected: ' . ( $has_vector ? 'Yes' : 'No' ) );
if ( $has_vector ) {
try {
// Convert the embedding array to a JSON string.
- $embedding_json = json_encode( $embedding );
+ $embedding_json = wp_json_encode( $embedding );
// Use the Database class to determine the vector function to use.
$vector_function = self::$database->get_vector_from_string_function( $embedding_json );
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB DEBUG] Vector function: ' . $vector_function ); }
+ Logger::debug( 'Vector function: ' . $vector_function );
// For MySQL, the prepare statement handles the quoting properly
// For MariaDB, we need to make sure the vector function is inserted as-is.
@@ -1009,8 +1002,7 @@ public static function insert_embedding_row( $doc_id, $chunk_id, $chunk_content,
);
}
} catch ( \Exception $e ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ERROR] Exception in insert_embedding_row: ' . $e->getMessage() ); }
+ Logger::log_exception( $e, 'Exception in insert_embedding_row' );
$result = false;
}
} else {
@@ -1025,7 +1017,7 @@ public static function insert_embedding_row( $doc_id, $chunk_id, $chunk_content,
$chunk_id,
$chunk_content,
$summary,
- json_encode( $embedding ),
+ wp_json_encode( $embedding ),
$model,
$doc_type,
$chunk_index
@@ -1034,8 +1026,7 @@ public static function insert_embedding_row( $doc_id, $chunk_id, $chunk_content,
}
if ( false === $result ) {
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
- error_log( '[WPVDB ERROR] Failed to insert embedding row: ' . $wpdb->last_error ); }
+ Logger::error( 'Failed to insert embedding row: ' . $wpdb->last_error );
return new \WP_Error(
'embedding_insert_failed',
'Database insert failed: ' . $wpdb->last_error,
@@ -1123,7 +1114,7 @@ public static function cosine_distance( $vec1, $vec2 ) {
if ( count( $vec2 ) != $length ) {
// Either truncate or pad vec2 to match vec1's length.
$vec2 = array_slice( $vec2, 0, $length );
- while ( count( $vec2 ) < $length ) {
+ for ( $vec2_length = count( $vec2 ); $vec2_length < $length; $vec2_length++ ) {
$vec2[] = 0.0;
}
}
diff --git a/includes/class-wpvdb-security.php b/includes/class-wpvdb-security.php
index 4a19990..0058045 100644
--- a/includes/class-wpvdb-security.php
+++ b/includes/class-wpvdb-security.php
@@ -179,10 +179,7 @@ public static function log_security_event( $event, $data = array() ) {
'data' => $data,
);
- // Log to WordPress error log if debug is enabled.
- if ( \wpvdb_should_log_to_error_log( 'debug', 'security_event', $log_data ) ) {
- error_log( '[WPVDB SECURITY] ' . wp_json_encode( $log_data ) );
- }
+ Logger::debug( 'Security event', $log_data );
// Allow plugins to hook into security logging.
do_action( 'wpvdb_security_event', $event, $log_data );
diff --git a/includes/class-wpvdb-settings.php b/includes/class-wpvdb-settings.php
index 93d2b4a..3cefe0a 100644
--- a/includes/class-wpvdb-settings.php
+++ b/includes/class-wpvdb-settings.php
@@ -716,16 +716,12 @@ public static function has_pending_provider_change() {
$settings = get_option( 'wpvdb_settings', array() );
if ( ! is_array( $settings ) ) {
- if ( \wpvdb_should_log_to_error_log( 'debug', 'invalid_settings_format' ) ) {
- error_log( 'WPVDB CRITICAL: Invalid settings format - not an array' );
- }
+ Logger::critical( 'Invalid settings format - not an array' );
return false;
}
$has_pending = ( ! empty( $settings['pending_provider'] ) || ! empty( $settings['pending_model'] ) );
- if ( \wpvdb_should_log_to_error_log( 'debug', 'has_pending_provider_change' ) ) {
- error_log( 'WPVDB CRITICAL: Has pending provider change: ' . ( $has_pending ? 'YES' : 'NO' ) );
- }
+ Logger::debug( 'Has pending provider change: ' . ( $has_pending ? 'YES' : 'NO' ) );
return $has_pending;
}
diff --git a/includes/class-wpvdb-utils.php b/includes/class-wpvdb-utils.php
index 2f8d246..95895d3 100644
--- a/includes/class-wpvdb-utils.php
+++ b/includes/class-wpvdb-utils.php
@@ -139,7 +139,8 @@ public static function format_bytes( $bytes, $precision = 2 ) {
$units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB' );
- for ( $i = 0; $bytes > 1024 && $i < count( $units ) - 1; $i++ ) {
+ $unit_count = count( $units );
+ for ( $i = 0; $bytes > 1024 && $i < $unit_count - 1; $i++ ) {
$bytes /= 1024;
}
diff --git a/includes/wpvdb-runtime.php b/includes/wpvdb-runtime.php
index 85dbb8c..a1a5687 100644
--- a/includes/wpvdb-runtime.php
+++ b/includes/wpvdb-runtime.php
@@ -74,8 +74,8 @@ function ( $args, $url ) {
return $args;
}
- $host = parse_url( $url, PHP_URL_HOST );
- $path = (string) parse_url( $url, PHP_URL_PATH );
+ $host = wp_parse_url( $url, PHP_URL_HOST );
+ $path = (string) wp_parse_url( $url, PHP_URL_PATH );
$is_allowed_openai = 'api.openai.com' === $host;
$is_allowed_wpcom_ai_proxy = 'public-api.wordpress.com' === $host
&& strpos( $path, '/wpcom/v2/ai-api-proxy/' ) === 0;