CPTP_Module_Permalink::post_type_link PHP Method

post_type_link() public method

Fix permalinks output.
public post_type_link ( String $post_link, WP_Post $post, String $leavename ) : string
$post_link String
$post WP_Post
$leavename String for edit.php
return string
    public function post_type_link($post_link, $post, $leavename)
    {
        global $wp_rewrite;
        if (!$wp_rewrite->permalink_structure) {
            return $post_link;
        }
        $draft_or_pending = isset($post->post_status) && in_array($post->post_status, array('draft', 'pending', 'auto-draft'));
        if ($draft_or_pending and !$leavename) {
            return $post_link;
        }
        $post_type = $post->post_type;
        $pt_object = get_post_type_object($post_type);
        if (false === $pt_object->rewrite) {
            return $post_link;
        }
        $permalink = $wp_rewrite->get_extra_permastruct($post_type);
        $permalink = str_replace('%post_id%', $post->ID, $permalink);
        $permalink = str_replace('%' . $post_type . '_slug%', $pt_object->rewrite['slug'], $permalink);
        // has parent.
        $parentsDirs = '';
        if ($pt_object->hierarchical) {
            if (!$leavename) {
                $postId = $post->ID;
                while ($parent = get_post($postId)->post_parent) {
                    $parentsDirs = get_post($parent)->post_name . '/' . $parentsDirs;
                    $postId = $parent;
                }
            }
        }
        $permalink = str_replace('%' . $post_type . '%', $parentsDirs . '%' . $post_type . '%', $permalink);
        if (!$leavename) {
            $permalink = str_replace('%' . $post_type . '%', $post->post_name, $permalink);
        }
        // %post_id%/attachment/%attachement_name%;
        if (isset($_GET['post']) && $_GET['post'] != $post->ID) {
            $parent_structure = trim(CPTP_Util::get_permalink_structure($post->post_type), '/');
            $parent_dirs = explode('/', $parent_structure);
            if (is_array($parent_dirs)) {
                $last_dir = array_pop($parent_dirs);
            } else {
                $last_dir = $parent_dirs;
            }
            if ('%post_id%' == $parent_structure or '%post_id%' == $last_dir) {
                $permalink = $permalink . '/attachment/';
            }
        }
        $search = array();
        $replace = array();
        $replace_tag = $this->create_taxonomy_replace_tag($post->ID, $permalink);
        $search = $search + $replace_tag['search'];
        $replace = $replace + $replace_tag['replace'];
        // from get_permalink.
        $category = '';
        if (false !== strpos($permalink, '%category%')) {
            $categories = get_the_category($post->ID);
            if ($categories) {
                usort($categories, '_usort_terms_by_ID');
                // order by ID
                $category_object = apply_filters('post_link_category', $categories[0], $categories, $post);
                $category_object = get_term($category_object, 'category');
                $category = $category_object->slug;
                if ($parent = $category_object->parent) {
                    $category = get_category_parents($parent, false, '/', true) . $category;
                }
            }
            // show default category in permalinks, without
            // having to assign it explicitly
            if (empty($category)) {
                $default_category = get_term(get_option('default_category'), 'category');
                $category = is_wp_error($default_category) ? '' : $default_category->slug;
            }
        }
        $author = '';
        if (false !== strpos($permalink, '%author%')) {
            $authordata = get_userdata($post->post_author);
            $author = $authordata->user_nicename;
        }
        $post_date = strtotime($post->post_date);
        $permalink = str_replace(array('%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', '%category%', '%author%'), array(date('Y', $post_date), date('m', $post_date), date('d', $post_date), date('H', $post_date), date('i', $post_date), date('s', $post_date), $category, $author), $permalink);
        $permalink = str_replace($search, $replace, $permalink);
        $permalink = home_url($permalink);
        return $permalink;
    }