CPTP_Module_Rewrite::register_taxonomy_rules PHP Method

register_taxonomy_rules() public method

register_taxonomy_rules
public register_taxonomy_rules ( string $taxonomy, array | string $object_type, array | WP_Taxonomy $args ) : void
$taxonomy string
$object_type array | string
$args array | WP_Taxonomy
return void
    public function register_taxonomy_rules($taxonomy, $object_type, $args)
    {
        global $wp_rewrite;
        /* for 4.7 */
        $args = (array) $args;
        if (CPTP_Util::get_no_taxonomy_structure()) {
            return;
        }
        if (!empty($args['_builtin'])) {
            return;
        }
        if (false === $args['rewrite']) {
            return;
        }
        $post_types = $args['object_type'];
        foreach ($post_types as $post_type) {
            $post_type_obj = get_post_type_object($post_type);
            if (!empty($post_type_obj->rewrite['slug'])) {
                $slug = $post_type_obj->rewrite['slug'];
            } else {
                $slug = $post_type;
            }
            if (!empty($post_type_obj->has_archive) && is_string($post_type_obj->has_archive)) {
                $slug = $post_type_obj->has_archive;
            }
            if (!empty($post_type_obj->rewrite['with_front'])) {
                $slug = substr($wp_rewrite->front, 1) . $slug;
            }
            if ('category' == $taxonomy) {
                $taxonomy_slug = ($cb = get_option('category_base')) ? $cb : $taxonomy;
                $taxonomy_key = 'category_name';
            } else {
                // Edit by [Xiphe]
                if (isset($args['rewrite']['slug'])) {
                    $taxonomy_slug = $args['rewrite']['slug'];
                } else {
                    $taxonomy_slug = $taxonomy;
                }
                // [Xiphe] stop
                $taxonomy_key = $taxonomy;
            }
            $rules = array(array('regex' => '%s/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]"), array('regex' => '%s/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/page/?([0-9]{1,})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&paged=\$matches[3]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&paged=\$matches[4]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]&paged=\$matches[5]"), array('regex' => '%s/(.+?)/page/?([0-9]{1,})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&paged=\$matches[2]"), array('regex' => '%s/(.+?)/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]"));
            // no post_type slug.
            foreach ($rules as $rule) {
                $regex = sprintf($rule['regex'], "{$taxonomy_slug}");
                $redirect = $rule['redirect'];
                add_rewrite_rule($regex, $redirect, 'top');
            }
            if (get_option('add_post_type_for_tax')) {
                foreach ($rules as $rule) {
                    $regex = sprintf($rule['regex'], "{$slug}/{$taxonomy_slug}");
                    $redirect = $rule['redirect'] . "&post_type={$post_type}";
                    add_rewrite_rule($regex, $redirect, 'top');
                }
            } else {
                foreach ($rules as $rule) {
                    $regex = sprintf($rule['regex'], "{$slug}/{$taxonomy_slug}");
                    $redirect = $rule['redirect'];
                    add_rewrite_rule($regex, $redirect, 'top');
                }
            }
            do_action('CPTP_registered_' . $taxonomy . '_rules', $object_type, $args, $taxonomy_slug);
        }
    }