Skip to content

Commit

Permalink
Fixed bug if $item['author'] or $feed['author'] are not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
jackjamieson2 committed Jun 19, 2019
1 parent 52d1714 commit 5d5fae5
Showing 1 changed file with 117 additions and 10 deletions.
127 changes: 117 additions & 10 deletions includes/class-yarns-microsub-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,26 @@ public static function clean_post( $item, $feed ) {
public static function clean_author( $item, $feed ) {
// If author is just a string, replace it with an array
// see https://github.com/jackjamieson2/yarns-microsub-server/issues/75
if (! is_array($item['author'])) {
$item['author'] = array(
'type' => 'card',
'name' => $item['author'],
);
if (isset($item['author'])){
if (! is_array($item['author'])) {
$item['author'] = array(
'type' => 'card',
'name' => $item['author'],
);
}
}

if (! is_array($feed['author'])) {
$feed['author'] = array(
'type' => 'card',
'name' => $feed['author'],
);
if (isset($feed['author'])){
if (! is_array($feed['author'])) {
$feed['author'] = array(
'type' => 'card',
'name' => $feed['author'],
);
}
}



if ( isset ( $feed['author'] ) && isset ( $item['author'] ) ) {
$item['author'] = array_merge( $feed['author'], $item['author'] );
}
Expand Down Expand Up @@ -184,6 +189,7 @@ public static function search( $query ) {
}



/**
* Returns a preview of the feed
*
Expand Down Expand Up @@ -296,3 +302,104 @@ public static function is_url( $query ) {
}

}

Yarns_Microsub_Parser::load_parse_this();

class Yarns_Parse_This extends Parse_This {
/**
* Fetches a list of feeds
*
* @param string $url URL to scan
*/
public function fetch_feeds( $url = null ) {
if ( ! $url ) {
$url = $this->url;
}
if ( empty( $url ) ) {
return new WP_Error( 'invalid-url', __( 'A valid URL was not provided.', 'indieweb-post-kinds' ) );
}
$fetch = $this->fetch( $url );
if ( is_wp_error( $fetch ) ) {
return $fetch;
}
// A feed was given
if ( $this->content instanceof SimplePie ) {
if ( ! class_exists( 'Parse_This_RSS', false ) ) {
require_once plugin_dir_path( __FILE__ ) . '/class-parse-this-rss.php';
}
return array(
'results' => array(
array(
'url' => $url,
'type' => 'feed',
'_feed_type' => Parse_This_RSS::get_type( $this->content ),
'name' => $this->content->get_title(),
),
),
);
}
if ( $this->doc instanceof DOMDocument ) {
$xpath = new DOMXPath( $this->doc );
// Fetch and gather <link> data.
$links = array();
foreach ( $xpath->query( '(//link|//a)[@rel and @href]' ) as $link ) {
$rel = $link->getAttribute( 'rel' );
$href = $link->getAttribute( 'href' );
$title = $link->getAttribute( 'title' );
$type = self::get_feed_type( $link->getAttribute( 'type' ) );
if ( in_array( $rel, array( 'alternate', 'feed' ), true ) && ! empty( $type ) ) {
$links[] = array_filter(
array(
'url' => WP_Http::make_absolute_url( $href, $url ),
'type' => 'feed',
'_feed_type' => $type,
'name' => $title,
)
);
}
}
// Check to see if the current page is an h-feed
$this->parse( array( 'feed' => true ) );
if ( isset( $this->jf2['type'] ) && 'feed' === $this->jf2['type'] ) {
$links[] = array_filter(
array(
'url' => $url,
'type' => 'feed',
'_feed_type' => 'microformats',
'name' => $this->jf2['name'],
)
);
} elseif ( isset( $this->jf2['items'] ) ) {
foreach ( $this->jf2['items'] as $item ) {
if ( 'feed' === $item['type'] && isset( $item['uid'] ) ) {
$links[] = array_filter(
array(
'url' => $item['uid'],
'type' => 'feed',
'_feed_type' => 'microformats',
'name' => ifset( $item['name'] ),
)
);
}
}
}
// Sort feeds by priority
$rank = array(
'jf2feed' => 0,
'microformats' => 1,
'jsonfeed' => 2,
'atom' => 3,
'rss' => 4,
);
usort(
$links,
function( $a, $b ) use ( $rank ) {
return $rank[ $a['_feed_type'] ] > $rank[ $b['_feed_type'] ];
}
);

return array( 'results' => $links );
}
return new WP_Error( 'unknown error', null, $this->content );
}
}

0 comments on commit 5d5fae5

Please sign in to comment.