Skip to content

Commit 16e1d0b

Browse files
committed
ignore hcards in the first parsing flow
and improve regex to support `<a>` with nested HTML
1 parent b44d6d2 commit 16e1d0b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

includes/Handler/class-mf2.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,18 @@ public function find_representative_hcard( $mf_array, $url ) {
400400
*
401401
* @return array the h-entry node or false
402402
*/
403-
public function find_representative_item( $mf_array, $target ) {
403+
public function find_representative_item( $mf_array, $target, $ignore_hcards = true ) {
404404
$items = $this->get_items( $mf_array );
405405
if ( ! is_array( $items ) || empty( $items ) ) {
406406
return false;
407407
}
408408

409409
foreach ( $items as $item ) {
410+
// we do not want to check h-cards as main items as they are not the content
411+
if ( $this->is_type( $item, 'h-card' ) && $ignore_hcards ) {
412+
continue;
413+
}
414+
410415
// check properties
411416
if ( isset( $item['properties'] ) ) {
412417
// check properties if target urls was mentioned
@@ -468,13 +473,13 @@ public function find_representative_item( $mf_array, $target ) {
468473
'content' === $key &&
469474
! empty( $value['html'] ) &&
470475
is_string( $value['html'] ) &&
471-
preg_match_all( '/<a[^>]+?' . preg_quote( $target, '/' ) . '[^>]*>([^>]+?)<\/a>/i', $value['html'], $context )
476+
preg_match_all( '/<a\b[^>]*href\s*=\s*["\']?' . preg_quote( $target, '/' ) . '["\']/i', $value['html'], $context )
472477
) {
473478
return $item;
474479
} elseif (
475480
'summary' === $key &&
476481
is_string( $value ) &&
477-
preg_match_all( '/<a[^>]+?' . preg_quote( $target, '/' ) . '[^>]*>([^>]+?)<\/a>/i', $value, $context )
482+
preg_match_all( '/<a\b[^>]*href\s*=\s*["\']?' . preg_quote( $target, '/' ) . '["\']/i', $value, $context )
478483
) {
479484
return $item;
480485
}
@@ -497,6 +502,10 @@ public function find_representative_item( $mf_array, $target ) {
497502
*/
498503
protected function get_representative_item( $mf_array, $url ) {
499504
$item = $this->find_representative_item( $mf_array, $url );
505+
if ( empty( $item ) || ! is_array( $item ) ) {
506+
$item = $this->find_representative_item( $mf_array, $url, false );
507+
}
508+
500509
if ( empty( $item ) || ! is_array( $item ) ) {
501510
return array();
502511
}

0 commit comments

Comments
 (0)