public function setting_structure_callback_function($option)
 {
     $post_type = str_replace('_structure', '', $option);
     $pt_object = get_post_type_object($post_type);
     $slug = $pt_object->rewrite['slug'];
     $with_front = $pt_object->rewrite['with_front'];
     $value = CPTP_Util::get_permalink_structure($post_type);
     $disabled = false;
     if (isset($pt_object->cptp_permalink_structure) and $pt_object->cptp_permalink_structure) {
         $disabled = true;
     }
     if (!$value) {
         $value = CPTP_DEFAULT_PERMALINK;
     }
     global $wp_rewrite;
     $front = substr($wp_rewrite->front, 1);
     if ($front and $with_front) {
         $slug = $front . $slug;
     }
     echo '<p><code>' . home_url() . '/' . esc_html($slug) . '</code> <input name="' . esc_attr($option) . '" id="' . esc_attr($option) . '" type="text" class="regular-text code ' . $this->disabled_string($disabled) . '" value="' . $value . '" ' . $this->disabled_string($disabled) . ' /></p>';
     echo '<p>has_archive: <code>';
     echo $pt_object->has_archive ? 'true' : 'false';
     echo '</code> / ';
     echo 'with_front: <code>';
     echo $pt_object->rewrite['with_front'] ? 'true' : 'false';
     echo '</code></p>';
 }
 /**
  *
  * register_post_type_rules
  *  ** add rewrite tag for Custom Post Type.
  * @version 1.1
  * @since 0.9
  *
  * @param string $post_type
  * @param object $args
  *
  */
 public function register_post_type_rules($post_type, $args)
 {
     /** @var WP_Rewrite $wp_rewrite */
     global $wp_rewrite;
     if ($args->_builtin or !$args->publicly_queryable or !$args->show_ui) {
         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);
 }
    public function setting_structure_callback_function($option)
    {
        $post_type = $option['post_type'];
        $name = $option['label_for'];
        $pt_object = get_post_type_object($post_type);
        $slug = $pt_object->rewrite['slug'];
        $with_front = $pt_object->rewrite['with_front'];
        $value = CPTP_Util::get_permalink_structure($post_type);
        $disabled = false;
        if (isset($pt_object->cptp_permalink_structure) and $pt_object->cptp_permalink_structure) {
            $disabled = true;
        }
        if (!$value) {
            $value = CPTP_DEFAULT_PERMALINK;
        }
        global $wp_rewrite;
        $front = substr($wp_rewrite->front, 1);
        if ($front and $with_front) {
            $slug = $front . $slug;
        }
        ?>
		<p>
			<code><?php 
        echo esc_html(home_url() . ($slug ? '/' : '') . $slug);
        ?>
</code>
			<input name="<?php 
        echo esc_attr($name);
        ?>
" id="<?php 
        echo esc_attr($name);
        ?>
" type="text"
			       class="regular-text code "
			       value="<?php 
        echo esc_attr($value);
        ?>
" <?php 
        disabled($disabled, true, true);
        ?>
 />
		</p>
		<p>has_archive: <code><?php 
        echo esc_html($pt_object->has_archive ? 'true' : 'false');
        ?>
</code> / with_front:
			<code><?php 
        echo esc_html($pt_object->rewrite['with_front'] ? 'true' : 'false');
        ?>
</code></p>
		<?php 
    }
 /**
  *
  * fix attachment output
  *
  * @version 1.0
  * @since 0.8.2
  *
  * @param string $link
  * @param int $postID
  *
  * @return string
  */
 public function attachment_link($link, $postID)
 {
     /** @var WP_Rewrite $wp_rewrite */
     global $wp_rewrite;
     if (!$wp_rewrite->permalink_structure) {
         return $link;
     }
     $post = get_post($postID);
     if (!$post->post_parent) {
         return $link;
     }
     $post_parent = get_post($post->post_parent);
     $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;
 }
 public function test_get_permalink_structure_from_arguments()
 {
     $post_type = rand_str(12);
     register_post_type($post_type, array("public" => true, "cptp_permalink_structure" => "/%year%/%monthnum%/%day%/%post_id%/"));
     $this->assertEquals(CPTP_Util::get_permalink_structure($post_type), "/%year%/%monthnum%/%day%/%post_id%/");
 }
 /**
  * Get permalink structure front for date archive.
  *
  * @since 1.0.0
  * @param string $post_type post type name.
  * @return string
  */
 public static function get_date_front($post_type)
 {
     $structure = CPTP_Util::get_permalink_structure($post_type);
     $front = '';
     preg_match_all('/%.+?%/', $structure, $tokens);
     $tok_index = 1;
     foreach ((array) $tokens[0] as $token) {
         if ('%post_id%' == $token && $tok_index <= 3) {
             $front = '/date';
             break;
         }
         $tok_index++;
     }
     return $front;
 }