CPT_Date_Archives   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B custom_rewrite_rules() 0 24 2
A settings_page() 0 7 2
1
<?php
2
/**
3
 * Class CPT_Date_Archives
4
 *
5
 * @license GPLv2 or later
6
 * @since   0.1
7
 */
8
class CPT_Date_Archives {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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