1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class CPT_Date_Archives |
4
|
|
|
* |
5
|
|
|
* @license GPLv2 or later |
6
|
|
|
* @since 0.1 |
7
|
|
|
*/ |
8
|
|
|
class CPT_Date_Archives { |
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The class constructor |
12
|
|
|
* |
13
|
|
|
* @access public |
14
|
|
|
* @since 0.1 |
15
|
|
|
*/ |
16
|
|
|
public function __construct() { |
17
|
|
|
|
18
|
|
|
add_action( 'init', array( $this, 'custom_rewrite_rules' ) ); |
19
|
|
|
add_action( 'init', array( $this, 'settings_page' ) ); |
20
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Add rewrite rules for supported custom post types to create day, month, and year archives |
25
|
|
|
* |
26
|
|
|
* @access public |
27
|
|
|
* @since 0.1 |
28
|
|
|
*/ |
29
|
|
|
public function custom_rewrite_rules() { |
30
|
|
|
|
31
|
|
|
$settings_page = CPT_Date_Archive_Settings::init(); |
32
|
|
|
$post_types = $settings_page->get_post_type_objects(); |
33
|
|
|
|
34
|
|
|
foreach( $post_types as $post_type ) { |
35
|
|
|
//Day archive |
36
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/([0-9]{2})/feed/(feed|rdf|rss|rss2|atom)/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]$matches[2]$matches[3]&feed=$matches[4]','top'); |
37
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/([0-9]{2})/(feed|rdf|rss|rss2|atom)/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]$matches[2]$matches[3]&feed=$matches[4]','top'); |
38
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/([0-9]{2})/page/?([0-9]{1,})/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]$matches[2]$matches[3]&paged=$matches[4]','top'); |
39
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/([0-9]{1,2})/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]$matches[2]$matches[3]', 'top' ); |
40
|
|
|
//Monthly archive |
41
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/feed/(feed|rdf|rss|rss2|atom)/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]$matches[2]&feed=$matches[3]','top'); |
42
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/(feed|rdf|rss|rss2|atom)/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]$matches[2]&feed=$matches[3]','top'); |
43
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/page/?([0-9]{1,})/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]$matches[2]&paged=$matches[3]','top'); |
44
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]$matches[2]', 'top' ); |
45
|
|
|
//Yearly archive |
46
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]&feed=$matches[2]','top'); |
47
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]&feed=$matches[2]','top'); |
48
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/page/?([0-9]{1,})/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]&paged=$matches[2]','top'); |
49
|
|
|
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/?$", 'index.php?post_type=' . $post_type->name . '&m=$matches[1]', 'top' ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Initialize the settings page for the backend |
56
|
|
|
* |
57
|
|
|
* @access public |
58
|
|
|
* @since 0.2 |
59
|
|
|
*/ |
60
|
|
|
public function settings_page() { |
61
|
|
|
|
62
|
|
|
if ( is_admin() ) { |
63
|
|
|
CPT_Date_Archive_Settings::init(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.