public function attachment_link($link, $post_id)
{
/** @var WP_Rewrite $wp_rewrite */
global $wp_rewrite;
if (!$wp_rewrite->permalink_structure) {
return $link;
}
$post = get_post($post_id);
if (!$post->post_parent) {
return $link;
}
$post_parent = get_post($post->post_parent);
if (!$post_parent) {
return $link;
}
$pt_object = get_post_type_object($post_parent->post_type);
if (false === $pt_object->rewrite) {
return $link;
}
$permalink = CPTP_Util::get_permalink_structure($post_parent->post_type);
$post_type = get_post_type_object($post_parent->post_type);
if (false == $post_type->_builtin) {
if (strpos($permalink, '%postname%') < strrpos($permalink, '%post_id%') && false === strrpos($permalink, 'attachment/')) {
$link = str_replace($post->post_name, 'attachment/' . $post->post_name, $link);
}
}
return $link;
}