Get permalink structure front for date archive.
public static get_date_front ( string $post_type ) : string | ||
$post_type | string | post type name. |
return | string |
public function test_get_date_front() { $post_type = rand_str(12); register_post_type($post_type, array("public" => true)); update_option($post_type . "_structure", "/%year%/%monthnum%/%day%/%post_id%/"); $this->assertEquals(CPTP_Util::get_date_front($post_type), ""); update_option($post_type . "_structure", "/%post_id%/"); $this->assertEquals(CPTP_Util::get_date_front($post_type), "/date"); }
/** * * 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); }
/** * * get_arcihves_link * @version 2.2 03/27/14 * */ public function get_archives_link($link) { global $wp_rewrite; if (!isset($this->get_archives_where_r['post_type'])) { return $link; } $c = isset($this->get_archives_where_r['taxonomy']) && is_array($this->get_archives_where_r['taxonomy']) ? $this->get_archives_where_r['taxonomy'] : ''; //[steve] $t = $this->get_archives_where_r['post_type']; $this->get_archives_where_r['post_type'] = isset($this->get_archives_where_r['post_type_slug']) ? $this->get_archives_where_r['post_type_slug'] : $t; // [steve] [*** bug fixing] if (isset($this->get_archives_where_r['post_type']) and 'postbypost' != $this->get_archives_where_r['type']) { $blog_url = rtrim(home_url(), '/'); //remove front $front = substr($wp_rewrite->front, 1); $link = str_replace($front, '', $link); $blog_url = preg_replace('/https?:\\/\\//', '', $blog_url); $ret_link = str_replace($blog_url, $blog_url . '/' . '%link_dir%', $link); $post_type = get_post_type_object($this->get_archives_where_r['post_type']); if (empty($c)) { // [steve] if (isset($post_type->rewrite['slug'])) { $link_dir = $post_type->rewrite['slug']; } else { $link_dir = $this->get_archives_where_r['post_type']; } } else { // [steve] $c['name'] = 'category' == $c['name'] && get_option('category_base') ? get_option('category_base') : $c['name']; $link_dir = $post_type->rewrite['slug'] . '/' . $c['name'] . '/' . $c['termslug']; } if (!strstr($link, '/date/')) { $link_dir = $link_dir . CPTP_Util::get_date_front($post_type); } if ($post_type->rewrite['with_front']) { $link_dir = $front . $link_dir; } $ret_link = str_replace('%link_dir%', $link_dir, $ret_link); } else { $ret_link = $link; } $this->get_archives_where_r['post_type'] = $t; // [steve] reverting post_type to previous value return $ret_link; }
/** * * @test * @group rewrite * @runInSeparateProcess * @preserveGlobalState disabled * */ public function test_cpt_date_archive_with_date_front() { update_option($this->post_type . "_structure", "/%year%/%post_id%/"); register_post_type($this->post_type, array("public" => true, 'taxonomies' => array('category'), "has_archive" => true)); $post_type_object = get_post_type_object($this->post_type); $this->factory->post->create_many(10, array('post_type' => $this->post_type, "post_date" => "2012-12-12")); do_action('wp_loaded'); /** @var WP_Rewrite $wp_rewrite */ global $wp_rewrite; $wp_rewrite->flush_rules(); $this->go_to(home_url("/" . $post_type_object->rewrite["slug"] . CPTP_Util::get_date_front($this->post_type) . "/2012")); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_year"); $this->go_to(next_posts(0, false)); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_year", "is_paged"); $this->go_to(home_url("/" . $post_type_object->rewrite["slug"] . CPTP_Util::get_date_front($this->post_type) . "/2012/12")); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_month"); $this->go_to(next_posts(0, false)); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_month", "is_paged"); $this->go_to(home_url("/" . $post_type_object->rewrite["slug"] . CPTP_Util::get_date_front($this->post_type) . "/2012/12/12")); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_day"); $this->go_to(next_posts(0, false)); $this->assertQueryTrue("is_archive", "is_post_type_archive", "is_date", "is_day", "is_paged"); }