fix: resolve remaining PHPCS errors (short ternaries, array formatting, Yoda conditions)
This commit is contained in:
@@ -50,7 +50,7 @@ class PSA_AI_Generator {
|
|||||||
$prompt = apply_filters( 'psa_validation_prompt', $prompt, $name );
|
$prompt = apply_filters( 'psa_validation_prompt', $prompt, $name );
|
||||||
$response = self::call_openrouter(
|
$response = self::call_openrouter(
|
||||||
$api_key,
|
$api_key,
|
||||||
$model ?: 'google/gemini-2.0-flash-001',
|
$model ? $model : 'google/gemini-2.0-flash-001',
|
||||||
$prompt,
|
$prompt,
|
||||||
PSA_Config::VALIDATION_MAX_TOKENS
|
PSA_Config::VALIDATION_MAX_TOKENS
|
||||||
);
|
);
|
||||||
@@ -89,21 +89,21 @@ class PSA_AI_Generator {
|
|||||||
public static function background_generate( $post_id, $peptide_name ) {
|
public static function background_generate( $post_id, $peptide_name ) {
|
||||||
$post = get_post( $post_id );
|
$post = get_post( $post_id );
|
||||||
if ( ! $post || 'draft' !== $post->post_status || 'peptide' !== $post->post_type ) {
|
if ( ! $post || 'draft' !== $post->post_status || 'peptide' !== $post->post_type ) {
|
||||||
error_log( 'PSA: background_generate skipped — post ' . $post_id . ' not a draft peptide.' );
|
error_log( 'PSA: background_generate skipped â post ' . $post_id . ' not a draft peptide.' );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only process posts that are still in 'pending' state.
|
// Only process posts that are still in 'pending' state.
|
||||||
$source = get_post_meta( $post_id, 'psa_source', true );
|
$source = get_post_meta( $post_id, 'psa_source', true );
|
||||||
if ( 'pending' !== $source ) {
|
if ( 'pending' !== $source ) {
|
||||||
error_log( 'PSA: background_generate skipped — post ' . $post_id . ' source is "' . $source . '", not "pending".' );
|
error_log( 'PSA: background_generate skipped â post ' . $post_id . ' source is "' . $source . '", not "pending".' );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$options = self::get_settings();
|
$options = self::get_settings();
|
||||||
$auto_publish = $options['auto_publish'];
|
$auto_publish = $options['auto_publish'];
|
||||||
|
|
||||||
error_log( 'PSA: Generating content for "' . $peptide_name . '" (post ' . $post_id . ') using model: ' . ( $options['ai_model'] ?: 'google/gemini-2.5-flash' ) );
|
error_log( 'PSA: Generating content for "' . $peptide_name . '" (post ' . $post_id . ') using model: ' . ( $options['ai_model'] ? $options['ai_model'] : 'google/gemini-2.5-flash' ) );
|
||||||
|
|
||||||
// Allow developers to hook before generation starts.
|
// Allow developers to hook before generation starts.
|
||||||
do_action( 'psa_before_generation', $post_id, $peptide_name );
|
do_action( 'psa_before_generation', $post_id, $peptide_name );
|
||||||
@@ -124,7 +124,7 @@ class PSA_AI_Generator {
|
|||||||
|
|
||||||
// Safeguard: if post_content is still empty after generation, log a warning.
|
// Safeguard: if post_content is still empty after generation, log a warning.
|
||||||
if ( empty( trim( $post_content ) ) ) {
|
if ( empty( trim( $post_content ) ) ) {
|
||||||
error_log( 'PSA: WARNING — AI returned valid JSON but post content (overview/description) is empty for "' . $peptide_name . '". Available keys: ' . implode( ', ', array_keys( $result ) ) );
|
error_log( 'PSA: WARNING â AI returned valid JSON but post content (overview/description) is empty for "' . $peptide_name . '". Available keys: ' . implode( ', ', array_keys( $result ) ) );
|
||||||
update_post_meta( $post_id, 'psa_generation_error', 'AI returned empty overview/description content. Available fields: ' . implode( ', ', array_keys( $result ) ) );
|
update_post_meta( $post_id, 'psa_generation_error', 'AI returned empty overview/description content. Available fields: ' . implode( ', ', array_keys( $result ) ) );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -161,7 +161,7 @@ class PSA_AI_Generator {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save all meta fields — prefer PubChem data for molecular properties.
|
// Save all meta fields â prefer PubChem data for molecular properties.
|
||||||
self::save_peptide_meta( $post_id, $result, $pubchem_data );
|
self::save_peptide_meta( $post_id, $result, $pubchem_data );
|
||||||
|
|
||||||
// Create a matching Knowledge Base article.
|
// Create a matching Knowledge Base article.
|
||||||
@@ -198,7 +198,7 @@ class PSA_AI_Generator {
|
|||||||
$prompt = apply_filters( 'psa_generation_prompt', $prompt, $peptide_name );
|
$prompt = apply_filters( 'psa_generation_prompt', $prompt, $peptide_name );
|
||||||
$response = self::call_openrouter(
|
$response = self::call_openrouter(
|
||||||
$api_key,
|
$api_key,
|
||||||
$model ?: 'google/gemini-2.5-flash',
|
$model ? $model : 'google/gemini-2.5-flash',
|
||||||
$prompt,
|
$prompt,
|
||||||
PSA_Config::GENERATION_MAX_TOKENS
|
PSA_Config::GENERATION_MAX_TOKENS
|
||||||
);
|
);
|
||||||
@@ -393,7 +393,7 @@ PROMPT;
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rate limited — retry with exponential backoff.
|
// Rate limited â retry with exponential backoff.
|
||||||
if ( $attempt < $max_retries ) {
|
if ( $attempt < $max_retries ) {
|
||||||
$delay = $base_delay * pow( 2, $attempt - 1 ); // 5s, 10s, 20s
|
$delay = $base_delay * pow( 2, $attempt - 1 ); // 5s, 10s, 20s
|
||||||
error_log( 'PSA: Rate limited on attempt ' . $attempt . '/' . $max_retries . '. Retrying in ' . $delay . 's...' );
|
error_log( 'PSA: Rate limited on attempt ' . $attempt . '/' . $max_retries . '. Retrying in ' . $delay . 's...' );
|
||||||
@@ -432,8 +432,14 @@ PROMPT;
|
|||||||
array(
|
array(
|
||||||
'model' => $model,
|
'model' => $model,
|
||||||
'messages' => array(
|
'messages' => array(
|
||||||
array( 'role' => 'system', 'content' => 'You are a scientific database assistant. Return only valid JSON.' ),
|
array(
|
||||||
array( 'role' => 'user', 'content' => $prompt ),
|
'role' => 'system',
|
||||||
|
'content' => 'You are a scientific database assistant. Return only valid JSON.',
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'role' => 'user',
|
||||||
|
'content' => $prompt,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
'max_tokens' => $max_tokens,
|
'max_tokens' => $max_tokens,
|
||||||
'temperature' => 0.3,
|
'temperature' => 0.3,
|
||||||
@@ -512,7 +518,7 @@ PROMPT;
|
|||||||
*/
|
*/
|
||||||
private static function create_kb_article( $peptide_post_id, $ai_data, $peptide_name ) {
|
private static function create_kb_article( $peptide_post_id, $ai_data, $peptide_name ) {
|
||||||
if ( ! post_type_exists( 'epkb_post_type_1' ) ) {
|
if ( ! post_type_exists( 'epkb_post_type_1' ) ) {
|
||||||
error_log( 'PSA: Echo Knowledge Base not active — skipping KB article.' );
|
error_log( 'PSA: Echo Knowledge Base not active â skipping KB article.' );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -533,8 +539,8 @@ PROMPT;
|
|||||||
|
|
||||||
// Build header meta line.
|
// Build header meta line.
|
||||||
$cat_label = ! empty( $ai_data['category_label'] ) ? $ai_data['category_label'] : '';
|
$cat_label = ! empty( $ai_data['category_label'] ) ? $ai_data['category_label'] : '';
|
||||||
$origin = ! empty( $ai_data['origin'] ) ? $ai_data['origin'] : '';
|
$origin = ! empty( $ai_data['origin'] ) ? $ai_data['origin'] : '';
|
||||||
$sequence = ! empty( $ai_data['sequence'] ) ? $ai_data['sequence'] : '';
|
$sequence = ! empty( $ai_data['sequence'] ) ? $ai_data['sequence'] : '';
|
||||||
|
|
||||||
$header = '';
|
$header = '';
|
||||||
if ( $cat_label ) {
|
if ( $cat_label ) {
|
||||||
@@ -675,7 +681,7 @@ PROMPT;
|
|||||||
} else {
|
} else {
|
||||||
// Check if first line is a subheading followed by content.
|
// Check if first line is a subheading followed by content.
|
||||||
$first_nl = strpos( $block, "\n" );
|
$first_nl = strpos( $block, "\n" );
|
||||||
if ( $first_nl !== false ) {
|
if ( false !== $first_nl ) {
|
||||||
$first_line = trim( substr( $block, 0, $first_nl ) );
|
$first_line = trim( substr( $block, 0, $first_nl ) );
|
||||||
$rest = trim( substr( $block, $first_nl + 1 ) );
|
$rest = trim( substr( $block, $first_nl + 1 ) );
|
||||||
if ( strlen( $first_line ) < 80 && strpos( $first_line, '.' ) === false ) {
|
if ( strlen( $first_line ) < 80 && strpos( $first_line, '.' ) === false ) {
|
||||||
|
|||||||
Reference in New Issue
Block a user