This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wpmvcb.php
215 lines (172 loc) · 8.67 KB
/
wpmvcb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class WPMVCB {
/**
* The class version number
*
* @var string
* @access private
* @since 0.4
*/
private $version = '0.4';
/**
* A list of files that must be loaded at plugin initialization
*
* @var array
*/
private $mustload_files = array();
/**
* A list of all classes contained in the framework.
*
* @var array
*/
private $autoload_classes = array();
/**
* A collection of post type slugs and arguments used to register the post type as key/value pairs
*
* @var array
*/
private $post_type_args = array();
/**
* Initialize the class
*/
public function __construct() {
$this->autoload_classes = array(
'WPMVC_Controller_Base' => __DIR__ . '/controllers/class-controller-base.php',
'WPMVCB_Cpt_Base' => __DIR__ . '/controllers/class-cpt-base.php',
'Base_Controller_Plugin' => __DIR__ . '/controllers/class-base-controller-plugin.php',
'WPMVCB_Settings_Base' => __DIR__ . '/controllers/class-settings-base.php',
'WPMVCB_Taxonomy_Base' => __DIR__ . '/controllers/class-taxonomy-base.php',
'WPMVCB_Metabox' => __DIR__ . '/controllers/class-metabox-base.php',
'WPMVCB_Model_Base' => __DIR__ . '/models/class-model-base.php',
'WPMVCB_Admin_Notice_Model_Base' => __DIR__ . '/models/class-admin-notice-model-base.php',
'WPMVCB_Post_Model_Base' => __DIR__ . '/models/class-post-model-base.php',
'Base_Model_Help_Tab' => __DIR__ . '/models/class-base-model-help-tab.php',
'Base_Model_JS_Object' => __DIR__ . '/models/class-base-model-js-object.php',
'WPMVCB_Menu_Page_Model_Base' => __DIR__ . '/models/class-menu-page-model-base.php',
'WPMVCB_Metabox_Model_Base' => __DIR__ . '/models/class-metabox-model-base.php',
'Base_Model_Plugin' => __DIR__ . '/models/class-base-model-plugin.php',
'WPMVCB_Settings_Model_Base' => __DIR__ . '/models/class-settings-model-base.php',
'WPMVCB_Taxonomy_Model_Base' => __DIR__ . '/models/class-base-model-taxonomy.php',
'WPMVCB_Post_View_Base' => __DIR__ . '/views/class-post-view-base.php',
'WPMVCB_Metabox_View_Base' => __DIR__ . '/views/class-metabox-view-base.php',
'WPMVCB_Metabox_Default_View' => __DIR__ . '/views/class-metabox-view-default.php',
'Helper_Functions' => __DIR__ . '/helpers/class-base-helpers.php',
);
spl_autoload_register( array( $this, 'autoloader' ) );
add_action( 'init', array( $this, 'init' ) );
add_action( 'muplugins_loaded', array( $this, 'load_mustloads' ) );
}
/**
* Add additional class name/absloute path pairs to the list of classes able to be autoloaded.
*
* @param array $classes
*/
public function register_autoload_classes( array $classes ) {
$this->autoload_classes = array_merge( $this->autoload_classes, $classes );
}
/**
* Autoload classes
*
* $param string $class The class to be loaded
* @return void
*/
public function autoloader( $class ) {
if ( isset( $this->autoload_classes[ $class ] ) ) {
if ( file_exists( $this->autoload_classes[ $class ] ) ) {
require_once $this->autoload_classes[ $class ];
if ( is_callable( "{$class}::on_load" ) ) {
call_user_func( array( $class, 'on_load' ) );
}
}
}
}
/**
* Add files that must be loaded at plugin initialization
*
* @param array $files
*/
public function register_mustload_files( array $files ) {
$this->mustload_files = array_merge( $files, $this->mustload_files );
}
/**
* Load the files in the $mustload property
*/
public function load_mustloads() {
foreach( $this->mustload_files as $key => $file ) {
if( file_exists( $file ) ) {
require_once $file;
if ( is_callable( "{$key}::on_load" ) ) {
call_user_func( array( $key, 'on_load' ) );
}
}
}
}
/**
* Register the post type slug and its post type arguments
*
* @link http://codex.wordpress.org/Function_Reference/register_post_type
*/
public function register_post_type_args( $slug, $args = array() ) {
$this->post_type_args[ $slug ] = $args;
}
/**
* The WP init action callback
*/
public function init() {
$this->register_post_types();
}
/**
* Register the post types stored in self::$post_type_args
*
* This method also adds rewrite rules for date based archives, if the post type itself supports archives.
*/
private function register_post_types() {
foreach( $this->post_type_args as $slug => $args ) {
register_post_type( $slug, $args );
$post_type = get_post_type_object( $slug );
if ( $post_type && $post_type->has_archive ) {
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=' . $slug . '&m=$matches[1]$matches[2]$matches[3]&feed=$matches[4]','top');
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=' . $slug . '&m=$matches[1]$matches[2]$matches[3]&feed=$matches[4]','top');
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/([0-9]{2})/page/?([0-9]{1,})/?$", 'index.php?post_type=' . $slug . '&m=$matches[1]$matches[2]$matches[3]&paged=$matches[4]','top');
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/([0-9]{1,2})/?$", 'index.php?post_type=' . $slug . '&m=$matches[1]$matches[2]$matches[3]', 'top' );
//Monthly archive
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/feed/(feed|rdf|rss|rss2|atom)/?$", 'index.php?post_type=' . $slug . '&m=$matches[1]$matches[2]&feed=$matches[3]','top');
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/(feed|rdf|rss|rss2|atom)/?$", 'index.php?post_type=' . $slug . '&m=$matches[1]$matches[2]&feed=$matches[3]','top');
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/page/?([0-9]{1,})/?$", 'index.php?post_type=' . $slug . '&m=$matches[1]$matches[2]&paged=$matches[3]','top');
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/([0-9]{2})/?$", 'index.php?post_type=' . $slug . '&m=$matches[1]$matches[2]', 'top' );
//Yearly archive
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$", 'index.php?post_type=' . $slug . '&m=$matches[1]&feed=$matches[2]','top');
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$", 'index.php?post_type=' . $slug . '&m=$matches[1]&feed=$matches[2]','top');
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/page/?([0-9]{1,})/?$", 'index.php?post_type=' . $slug . '&m=$matches[1]&paged=$matches[2]','top');
add_rewrite_rule( "{$post_type->rewrite['slug']}/([0-9]{4})/?$", 'index.php?post_type=' . $slug . '&m=$matches[1]', 'top' );
}
}
}
/**
* Get the main library directory
*
* @return string
*/
public function get_source_dir() {
return __DIR__;
}
/**
* Get the base version number
*/
public function get_version() {
return $this->version;
}
}