From fa60b2a2a1ac0b40ded3fafe7a07f73a38ed5f67 Mon Sep 17 00:00:00 2001 From: ozh Date: Wed, 5 Nov 2014 22:54:33 +0100 Subject: [PATCH] Fetch titles with an optional fallback string --- includes/functions-formatting.php | 14 ++++++++++++-- includes/functions.php | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/includes/functions-formatting.php b/includes/functions-formatting.php index 5c5d858..c166efe 100644 --- a/includes/functions-formatting.php +++ b/includes/functions-formatting.php @@ -73,12 +73,22 @@ function yourls_sanitize_keyword( $keyword ) { /** * Sanitize a page title. No HTML per W3C http://www.w3.org/TR/html401/struct/global.html#h-7.4.2 * + * + * @since 1.5 + * @param string $unsafe_title Title, potentially unsafe + * @param string $fallback Optional fallback if after sanitization nothing remains + * @return string Safe title */ -function yourls_sanitize_title( $unsafe_title ) { +function yourls_sanitize_title( $unsafe_title, $fallback = '' ) { $title = $unsafe_title; $title = strip_tags( $title ); $title = preg_replace( "/\s+/", ' ', trim( $title ) ); - return yourls_apply_filter( 'sanitize_title', $title, $unsafe_title ); + + if ( '' === $title || false === $title ) { + $title = $fallback; + } + + return yourls_apply_filter( 'sanitize_title', $title, $unsafe_title, $fallback ); } /** diff --git a/includes/functions.php b/includes/functions.php index 39abd43..f05f2e4 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1856,7 +1856,7 @@ function yourls_get_remote_title( $url ) { $title = html_entity_decode( $title, ENT_QUOTES, 'UTF-8' ); // Strip out evil things - $title = yourls_sanitize_title( $title ); + $title = yourls_sanitize_title( $title, $url ); return yourls_apply_filter( 'get_remote_title', $title, $url ); } -- 2.45.0