Improve check whether an URL exists

This commit is contained in:
Tetrakern 2023-10-24 01:21:10 +02:00
parent 9de54fbf19
commit 7041a5cefb
3 changed files with 9 additions and 19 deletions

View File

@ -171,12 +171,6 @@ The following list credits all third-party resources used in the Fictioneer them
License: [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/)<br>
Source: https://stackoverflow.com/a/17508056/17140970
* **Check whether an URL exists**<br>
Copyright: aman neekhara<br>
License: [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)<br>
Source 1: https://auth.geeksforgeeks.org/user/aman%20neekhara/articles<br>
Source 2: https://www.geeksforgeeks.org/how-to-check-the-existence-of-url-in-php/
* **Add columns in admin view**<br>
Copyright: Misha Rudrastyh<br>
License: [GPLv2 or later](https://www.gnu.org/licenses/gpl-2.0.html)<br>

View File

@ -6,7 +6,7 @@
function fictioneer_display_execution_time() {
// Abort on AJAX
if ( defined('DOING_AJAX') && DOING_AJAX ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}

View File

@ -17,24 +17,20 @@ if ( ! function_exists( 'fictioneer_url_exists' ) ) {
*/
function fictioneer_url_exists( $url ) {
if ( ! $url ) {
if ( empty( $url ) ) {
return false;
}
$curl = curl_init( $url );
curl_setopt( $curl, CURLOPT_NOBODY, true );
$response = wp_remote_head( $url );
if ( curl_exec( $curl ) ) {
$statusCode = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
if ( $statusCode == 404 ) {
return false;
}
return true;
if ( is_wp_error( $response ) ) {
return false;
}
return false;
$statusCode = wp_remote_retrieve_response_code( $response );
// Check for 2xx status codes which indicate success
return ( $statusCode >= 200 && $statusCode < 300 );
}
}