protected function fuzzyHrefMatch($url, $type, $pos)
{
if (!$pos) {
return false;
}
$url = trim($url);
$url = rtrim($url, '/');
$domain = parse_url($url);
$domain = @$domain['host'];
if ($domain) {
$domain2 = parse_url(wp_guess_url());
if (@$domain2['host'] != $domain) {
return false;
// If there is a domain name and it =/= ours, bail.
}
}
$last_part = explode('/', $url);
$last_pos = count($last_part) - 1;
$posttype = @$last_part[$last_pos - 1];
$anchor = '';
// Look for #anchors
if ($last_pos > 0 && '#' == substr(trim($last_part[$last_pos]), 0, 1)) {
$anchor = trim($last_part[$last_pos]);
$last_part = trim($last_part[$last_pos - 1]);
} elseif (false !== strpos($last_part[$last_pos], '#')) {
list($last_part, $anchor) = explode('#', $last_part[$last_pos]);
$anchor = trim("#{$anchor}");
$last_part = trim($last_part);
} else {
$last_part = trim($last_part[$last_pos]);
}
if (!$last_part) {
return false;
}
$lookup = \Pressbooks\Book::getBookStructure();
if ('part' !== $posttype && !isset($lookup['__export_lookup'][$last_part])) {
return false;
} elseif ('part' !== $posttype && isset($lookup['__export_lookup'][$last_part])) {
// Handle front/back matter and chapters
$new_type = $lookup['__export_lookup'][$last_part];
$new_pos = 0;
foreach ($lookup['__export_lookup'] as $p => $t) {
if ($t == $new_type) {
++$new_pos;
}
if ($p == $last_part) {
break;
}
}
$new_url = "{$new_type}-" . sprintf('%03s', $new_pos) . "-{$last_part}.{$this->filext}";
} elseif ('part' == $posttype && !isset($lookup['__export_lookup'][$last_part])) {
// Handle parts
$new_type = 'part';
foreach ($lookup['part'] as $key => $part) {
if ($part['post_name'] == $last_part) {
$new_url = 'part-' . sprintf('%03s', $key + 1) . "-{$last_part}.{$this->filext}";
}
}
}
if ($anchor) {
$new_url .= $anchor;
}
return $new_url;
}