diff --git a/includes/pmid.php b/includes/pmid.php deleted file mode 100644 index e6a371e..0000000 --- a/includes/pmid.php +++ /dev/null @@ -1,175 +0,0 @@ - 403 ) - ); -} - -/** - * REST callback that resolves a PubMed ID to CSL-JSON. - * - * NCBI's CSL endpoint does not currently emit browser CORS headers, so editor - * requests are proxied through WordPress using a fixed URL and numeric PMID. - * - * @param WP_REST_Request $request REST request. - * @return WP_REST_Response|WP_Error - */ -function bibliography_builder_rest_resolve_pmid( WP_REST_Request $request ) { - $pmid = isset( $request['pmid'] ) ? (string) $request['pmid'] : ''; - - if ( ! preg_match( '/^\d{1,8}$/', $pmid ) ) { - return new WP_Error( - 'bibliography_builder_pmid_invalid', - __( 'Invalid PubMed ID.', 'borges-bibliography-builder' ), - array( 'status' => 400 ) - ); - } - - $cached = bibliography_builder_get_cached_pmid_response( $pmid ); - - if ( isset( $cached['type'] ) && 'success' === $cached['type'] && isset( $cached['data'] ) && is_array( $cached['data'] ) ) { - return rest_ensure_response( $cached['data'] ); - } - - if ( isset( $cached['type'] ) && 'not_found' === $cached['type'] ) { - return new WP_Error( - 'bibliography_builder_pmid_not_found', - __( 'The PubMed ID could not be resolved.', 'borges-bibliography-builder' ), - array( 'status' => 404 ) - ); - } - - $url = add_query_arg( - array( - 'format' => 'csl', - 'id' => $pmid, - ), - BIBLIOGRAPHY_BUILDER_PUBMED_CSL_API - ); - $response = wp_remote_get( - $url, - array( - 'timeout' => BIBLIOGRAPHY_BUILDER_PUBMED_TIMEOUT, - 'redirection' => 3, - ) - ); - - if ( is_wp_error( $response ) ) { - return new WP_Error( - 'bibliography_builder_pmid_upstream_error', - __( 'The PubMed citation service could not be reached.', 'borges-bibliography-builder' ), - array( 'status' => 502 ) - ); - } - - $status = (int) wp_remote_retrieve_response_code( $response ); - - if ( $status < 200 || $status >= 300 ) { - if ( 404 === $status ) { - bibliography_builder_set_cached_pmid_response( - $pmid, - array( 'type' => 'not_found' ), - BIBLIOGRAPHY_BUILDER_PUBMED_NOT_FOUND_TTL - ); - - return new WP_Error( - 'bibliography_builder_pmid_not_found', - __( 'The PubMed ID could not be resolved.', 'borges-bibliography-builder' ), - array( 'status' => 404 ) - ); - } - - return new WP_Error( - 'bibliography_builder_pmid_upstream_error', - __( 'The PubMed citation service returned an error.', 'borges-bibliography-builder' ), - array( - 'status' => 502, - 'upstream_status' => $status, - ) - ); - } - - $decoded = json_decode( wp_remote_retrieve_body( $response ), true ); - - if ( ! is_array( $decoded ) || empty( $decoded ) ) { - return new WP_Error( - 'bibliography_builder_pmid_invalid_response', - __( 'The PubMed citation service returned an invalid response.', 'borges-bibliography-builder' ), - array( 'status' => 502 ) - ); - } - - bibliography_builder_set_cached_pmid_response( - $pmid, - array( - 'type' => 'success', - 'data' => $decoded, - ), - BIBLIOGRAPHY_BUILDER_PUBMED_SUCCESS_TTL - ); - - return rest_ensure_response( $decoded ); -}