Skip to content

Commit 6421a73

Browse files
committed
Fix for 500 error when invalid url elementor#825
1 parent bd62c89 commit 6421a73

File tree

1 file changed

+9
-36
lines changed

1 file changed

+9
-36
lines changed

src/SitemapParser.php

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,8 @@ public function parseRecursive( $url ) {
123123
$sitemaps = $this->sitemaps;
124124
$urls = $this->urls;
125125
try {
126-
$this->parse( strval( $todo[0] ) );
126+
$this->parse( $todo[0] );
127127
} catch ( WP2StaticException $e ) {
128-
WsLog::w( $e->getMessage() );
129128
// Keep crawling
130129
continue;
131130
}
@@ -141,7 +140,7 @@ public function parseRecursive( $url ) {
141140
*/
142141
public function addToQueue( array $url_array ) : void {
143142
foreach ( $url_array as $url ) {
144-
$url = $this->urlEncode( strval( $url ) );
143+
$url = $this->urlEncode( $url );
145144
if ( $this->urlValidate( $url ) ) {
146145
$this->queue[] = $url;
147146
}
@@ -181,11 +180,6 @@ public function parse( $url, $url_content = null ) {
181180
}
182181
$this->history[] = $this->current_url;
183182
$response = is_string( $url_content ) ? $url_content : $this->getContent();
184-
185-
if ( ! $response ) {
186-
return;
187-
}
188-
189183
if ( parse_url( $this->current_url, PHP_URL_PATH ) === self::ROBOTSTXT_PATH ) {
190184
$this->parseRobotstxt( $response );
191185
return;
@@ -223,7 +217,7 @@ protected function clean() {
223217
/**
224218
* Request the body content of an URL
225219
*
226-
* @return ?string Raw body content
220+
* @return string Raw body content
227221
* @throws WP2StaticException
228222
*/
229223
protected function getContent() {
@@ -238,22 +232,9 @@ protected function getContent() {
238232
if ( ! isset( $this->config['guzzle']['headers']['User-Agent'] ) ) {
239233
$this->config['guzzle']['headers']['User-Agent'] = $this->user_agent;
240234
}
241-
$client = new WP2StaticGuzzleHttp\Client( [ 'verify' => false ] );
242-
243-
if ( ! is_array( $this->config['guzzle'] ) ) {
244-
WsLog::w( 'Guzzle config is not in expected array format' );
245-
return null;
246-
}
235+
$client = new WP2StaticGuzzleHttp\Client();
247236
$res = $client->request( 'GET', $this->current_url, $this->config['guzzle'] );
248-
if ( $res->getStatusCode() === 200 ) {
249-
return $res->getBody()->getContents();
250-
} else {
251-
WsLog::w(
252-
'Got ' . $res->getStatusCode() .
253-
' for sitemap url "' . $this->current_url . '", skipping.'
254-
);
255-
return null;
256-
}
237+
return $res->getBody()->getContents();
257238
} catch ( WP2StaticGuzzleHttp\Exception\TransferException $e ) {
258239
throw new WP2StaticException( 'Unable to fetch URL contents', 0, $e );
259240
} catch ( WP2StaticGuzzleHttp\Exception\GuzzleException $e ) {
@@ -271,7 +252,7 @@ protected function parseRobotstxt( $robotstxt ) {
271252
// Split lines into array
272253
$lines = array_filter(
273254
array_map(
274-
fn ( $line ) => trim( (string) $line ),
255+
'trim',
275256
(array) preg_split( '/\r\n|\n|\r/', $robotstxt )
276257
)
277258
);
@@ -288,10 +269,7 @@ protected function parseRobotstxt( $robotstxt ) {
288269
$line = $line[0];
289270

290271
// Split by directive and rule
291-
$pair = array_map(
292-
fn ( $line ) => trim( (string) $line ),
293-
(array) preg_split( '/:/', $line, 2 )
294-
);
272+
$pair = array_map( 'trim', (array) preg_split( '/:/', $line, 2 ) );
295273
// Check if the line contains a sitemap
296274
if (
297275
strtolower( $pair[0] ) !== self::XML_TAG_SITEMAP ||
@@ -324,7 +302,7 @@ protected function addArray( $type, array $array ) {
324302
if ( ! isset( $array['loc'] ) ) {
325303
return false;
326304
}
327-
$array['loc'] = $this->urlEncode( trim( strval( $array['loc'] ) ) );
305+
$array['loc'] = $this->urlEncode( trim( $array['loc'] ) );
328306
if ( $this->urlValidate( $array['loc'] ) ) {
329307
switch ( $type ) {
330308
case self::XML_TAG_SITEMAP:
@@ -396,12 +374,7 @@ protected function parseString( $string ) {
396374
// Strings are not part of any documented sitemap standard
397375
return false;
398376
}
399-
$array = array_filter(
400-
array_map(
401-
fn ( $line ) => trim( (string) $line ),
402-
(array) preg_split( '/\r\n|\n|\r/', $string )
403-
)
404-
);
377+
$array = array_filter( array_map( 'trim', (array) preg_split( '/\r\n|\n|\r/', $string ) ) );
405378
foreach ( $array as $line ) {
406379
if ( $this->isSitemapURL( $line ) ) {
407380
$this->addArray( self::XML_TAG_SITEMAP, [ 'loc' => $line ] );

0 commit comments

Comments
 (0)