Skip to content

Commit

Permalink
Add REST support and remove plaintext PubMed API key from functions.php
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannimanetti11 committed Apr 9, 2024
1 parent 25f7edc commit 735192a
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ function perseowiki_custom_post_type() {
'has_archive' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-book',
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes', 'post-formats' ),
'rewrite' => array( 'slug' => 'glossario' )
);
Expand Down Expand Up @@ -854,11 +855,14 @@ function save_custom_meta_box($post_id, $post, $update)
/*
* Integrate PubMed API to fetch the publication count based on the scientific name
*/
function fetch_pubmed_publications_count($scientific_name) {

// Include the config.php file to get API key
include(get_template_directory() . '/inc/config.php');

function fetch_pubmed_publications_count($scientific_name) {
$api_key = 'bb4a4fd9b7035c08fbaf115a3099b7c07408'; // Replace with your actual API key
$api_key = $pubmed_api_key;

// Estrai solo le prime due parole dal nome scientifico
// Only extract first two words of scientific name to get binomial name
$parts = explode(' ', $scientific_name);
$binomial_name = $parts[0] . ' ' . $parts[1];

Expand All @@ -873,16 +877,12 @@ function fetch_pubmed_publications_count($scientific_name) {

$response = curl_exec($ch);
if ($response === false) {
error_log('CURL Error: ' . curl_error($ch)); // Log cURL error
curl_close($ch);
return 'Nd'; // Return Nd in case of error
} else {
error_log('Risposta API: ' . $response); // Log API response
}
curl_close($ch);

$data = json_decode($response, true);
error_log('PubMed API Response: ' . print_r($data, true)); // Log the response for debugging

// Extract and return the publication count from the API response
$count = isset($data['esearchresult']['count']) ? $data['esearchresult']['count'] : 0;
Expand Down Expand Up @@ -1119,6 +1119,7 @@ function get_all_posts_titles_and_links() {
'excerpt' => get_the_excerpt(),
'plurale' => get_post_meta(get_the_ID(), '_perseowiki_plurale', true),
'id' => get_the_ID(),
'post_type' => get_post_type(),
);
}
wp_reset_postdata();
Expand Down

0 comments on commit 735192a

Please sign in to comment.