public function register_post_type_rules($post_type, $args)
{
/** @var WP_Rewrite $wp_rewrite */
global $wp_rewrite;
if ($args->_builtin or !$args->publicly_queryable) {
return;
}
if (false === $args->rewrite) {
return;
}
$permalink = CPTP_Util::get_permalink_structure($post_type);
if (!$permalink) {
$permalink = CPTP_DEFAULT_PERMALINK;
}
$permalink = '%' . $post_type . '_slug%' . $permalink;
$permalink = str_replace('%postname%', '%' . $post_type . '%', $permalink);
add_rewrite_tag('%' . $post_type . '_slug%', '(' . $args->rewrite['slug'] . ')', 'post_type=' . $post_type . '&slug=');
$taxonomies = CPTP_Util::get_taxonomies(true);
foreach ($taxonomies as $taxonomy => $objects) {
$wp_rewrite->add_rewrite_tag("%{$taxonomy}%", '(.+?)', "{$taxonomy}=");
}
$rewrite_args = $args->rewrite;
if (!is_array($rewrite_args)) {
$rewrite_args = array('with_front' => $args->rewrite);
}
$slug = $args->rewrite['slug'];
if ($args->has_archive) {
if (is_string($args->has_archive)) {
$slug = $args->has_archive;
}
if ($args->rewrite['with_front']) {
$slug = substr($wp_rewrite->front, 1) . $slug;
}
$date_front = CPTP_Util::get_date_front($post_type);
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . $date_front . '/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . '/author/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?author_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . '/author/([^/]+)/?$', 'index.php?author_name=$matches[1]&post_type=' . $post_type, 'top');
if (in_array('category', $args->taxonomies)) {
$category_base = get_option('category_base');
if (!$category_base) {
$category_base = 'category';
}
add_rewrite_rule($slug . '/' . $category_base . '/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top');
add_rewrite_rule($slug . '/' . $category_base . '/([^/]+)/?$', 'index.php?category_name=$matches[1]&post_type=' . $post_type, 'top');
}
do_action('CPTP_registered_' . $post_type . '_rules', $args, $slug);
}
$rewrite_args['walk_dirs'] = false;
add_permastruct($post_type, $permalink, $rewrite_args);
}