CPT_Date_Archive_Settings   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 0
Metric Value
wmc 20
lcom 3
cbo 0
dl 0
loc 212
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A admin_menu() 0 9 1
A render() 0 7 1
A render_field_post_types() 0 16 3
A sanitize_input() 0 11 3
A get_post_type_objects() 0 11 2
A get_post_types() 0 5 1
A has_date_archive() 0 15 3
A update_option() 0 5 1
A __clone() 0 1 1
A __wakeup() 0 1 1
A init() 0 9 2
1
<?php
2
/**
3
 * The settings page class
4
 *
5
 * @license GPLv2 or later
6
 * @since   0.2
7
 */
8
class CPT_Date_Archive_Settings {
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
	 * A singleton instance
12
	 *
13
	 * @var    CPT_Date_Archive_Settings
14
	 * @static
15
	 * @access private
16
	 * @since  0.2
17
	 */
18
	private static $instance = null;
19
20
	/**
21
	 * The page object
22
	 *
23
	 * @var    stdClass
24
	 * @access private
25
	 * @since  0.2
26
	 */
27
	private $page;
28
29
	/**
30
	 * The class constructor
31
	 *
32
	 * @access public
33
	 * @since  0.2
34
	 */
35
	protected function __construct() {
36
37
		add_action( 'admin_menu', array( $this, 'admin_menu' ) );
38
		add_action( 'update_option_cpt_date_archive_post_types', array( $this, 'update_option' ) );
39
40
		$this->page     = new stdClass;
41
		$this->page->id = 'cpt_date_archives';
42
	}
43
44
	/**
45
	 * Add the options page, options groups, sections, and fields
46
	 *
47
	 * Hooked to WP admin_menu action
48
	 *
49
	 * @access public
50
	 * @since  0.2
51
	 */
52
	public function admin_menu() {
53
54
		register_setting( $this->page->id, 'cpt_date_archive_post_types', array( $this, 'sanitize_input' ) );
55
56
		add_options_page( __( 'CPT Date Archives Settings', 'cpt_date_archives' ), __( 'CPT Date Archives', 'cpt_date_archives' ),  'manage_options', $this->page->id, array( $this, 'render' ) );
57
		add_settings_section( 'general', __( 'General', 'cpt_date_archives' ), null, $this->page->id );
58
		add_settings_field( 'post-types', __( 'Post Types', 'cpt_date_archives' ), array( $this, 'render_field_post_types' ), $this->page->id, 'general' );
59
60
	}
61
62
	/**
63
	 * Render the settings page
64
	 *
65
	 * @access public
66
	 * @since  0.2
67
	 */
68
	public function render() {
69
70
		$page = $this->page;
71
72
		include dirname( __DIR__ ) . '/templates/settings-page.php';
73
74
	}
75
76
	/**
77
	 * Render the post types field
78
	 *
79
	 * @access public
80
	 * @since  0.2
81
	 */
82
	public function render_field_post_types() {
83
84
		$post_types = get_post_types( array( 'public' => 'true', '_builtin' => false ), 'objects' );
85
		$selected   = $this->get_post_type_objects();
86
87
		esc_html_e( __( 'Inputs are disabled for post types that do not support archives.', 'cpt_date_archive_post_types' ) );
88
89
		foreach ( $post_types as $post_type ) {
90
			if ( $this->has_date_archive( $post_type ) ) {
91
				$post_type->checked = true;
92
			}
93
94
			include dirname( __DIR__ ) . '/templates/post-types-field.php';
95
		}
96
97
	}
98
99
	/**
100
	 * Sanitize the post types field input
101
	 *
102
	 * @param  array $input
103
	 * @access public
104
	 * @since  0.2
105
	 */
106
	public function sanitize_input( $input ) {
107
108
		if ( ! empty( $input ) ) {
109
			foreach ( $input as $key => $post_type ) {
110
				$input[ $key ] = sanitize_text_field( $post_type );
111
			}
112
		}
113
114
		return $input;
115
116
	}
117
118
	/**
119
	 * Get the post type objects that are set to support date archives
120
	 *
121
	 * @return stdClass[]
122
	 * @access public
123
	 * @since  0.2
124
	 */
125
	public function get_post_type_objects() {
126
127
		$post_types = $this->get_post_types();
128
129
		foreach ( $post_types as $key => $post_type ) {
130
			$post_types[ $key ] = get_post_type_object( $post_type );
131
		}
132
133
		return $post_types;
134
135
	}
136
137
	/**
138
	 * Get the post post type names that are set to support date archives
139
	 *
140
	 * @return string[]
141
	 * @access public
142
	 * @since 0.2
143
	 */
144
	public function get_post_types() {
145
146
		return get_option( 'cpt_date_archive_post_types', array() );
147
148
	}
149
150
	/**
151
	 * Does a particular post type have a date archive?
152
	 *
153
	 * @return bool
154
	 * @access public
155
	 * @since  0.2
156
	 */
157
	public function has_date_archive( $post_type ) {
158
159
		$value = false;
160
161
		if ( ! is_object( $post_type ) ) {
162
			$post_type = get_post_type_object( $post_type );
163
		}
164
165
		if ( in_array( $post_type->name, $this->get_post_types() ) ) {
166
			$value = true;
167
		}
168
169
		return $value;
170
171
	}
172
173
	/**
174
	 * Delete the rewrite rules when updating the plugin settings
175
	 *
176
	 * Hooked to WP 'updated_option_cpt_date_archive_post_types' action.
177
	 *
178
	 * @access public
179
	 * @since  0.2
180
	 */
181
	public function update_option() {
182
183
			delete_option( 'rewrite_rules' );
184
185
	}
186
187
	/**
188
	 * Prevent cloning of the singleton instance
189
	 *
190
	 * @access private
191
	 * @since  0.2
192
	 */
193
	private function __clone() {}
194
195
	/**
196
	 * Prevent unserializing of the singleton instance
197
	 *
198
	 * @access private
199
	 * @since  0.2
200
	 */
201
	private function __wakeup () {}
202
203
	/**
204
	 * Singleton model
205
	 *
206
	 * @access public
207
	 * @since  0.2
208
	 */
209
	public static function init() {
210
211
		if ( ! isset( self::$instance ) ) {
212
			self::$instance = new CPT_Date_Archive_Settings;
213
		}
214
215
		return self::$instance;
216
217
	}
218
219
}
220