-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-ya-content-architecture.php
317 lines (240 loc) · 6.34 KB
/
class-ya-content-architecture.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* Contains main architure class
*
* @version 0.1
* @author Saurabh Shukla <[email protected]>
* @author Yapapaya <[email protected]>
* @license GPL-2.0
* @license https://opensource.org/licenses/GPL-2.0 GNU Public License
* @package YA_Content_Architecture
*/
if ( !class_exists( 'YA_Content_Architecture' ) ) {
/**
* Builds the content architecture for WordPress plugins
*
* @author Saurabh Shukla <[email protected]>
*/
class YA_Content_Architecture {
/**
* Architecture configuration
*
* @var array
*/
private $architecture = array();
/**
* Custom table names
*
* @var array
*/
private $tables = array();
/**
* Custom meta data table names
*
* @var array
*/
private $meta_tables = array();
/**
* Table preix
*
* @var string
*/
private $prefix = '';
/**
* Database architecture version
*
* @var string
*/
private $version = '';
/**
* Path to directory where schema information is stored
*
* @var string
*/
private $schema_path = '';
/**
* Constructor
*
* @param string $prefix table prefix
* @param type $schema_path Path to schema directory
* @param string $db_version The version number
* @return type
*/
public function __construct( $prefix, $schema_path, $db_version = '0.0.1' ) {
if ( empty( $prefix ) || empty( $schema_path ) ) {
return;
}
$this->prefix = $prefix;
$this->version = $db_version;
$this->schema_path = trailingslashit( $schema_path );
$this->architecture = include_once $this->schema_path . 'config.php';
$this->initialise_table_names();
}
/**
* Initialises custom table names
*
* @global object $wpdb
*/
private function initialise_table_names() {
global $wpdb;
foreach ( $this->architecture['custom'] as $name => $params ) {
$this->tables[$name] = $wpdb->prefix . $this->prefix . '_' . $this->prettify( $name );
if ( empty( $params ) ) {
continue;
}
if ( !isset( $params['has_meta'] ) ) {
continue;
}
if ( $params['has_meta'] === true ) {
$this->meta_tables[$name] = $wpdb->prefix . $this->prefix . '_' . $this->prettify( $name ) . 'meta';
}
}
}
/*
* ================================
* Installation methods
* ================================
* Use on plugin installation/ activation
*/
/**
* Creates custom tables
*
* @return array result strings from dbDelta()
*/
public function install() {
$update_result_tables = $this->install_tables();
$update_result_meta_tables = $this->install_meta_tables();
$update_results = $update_result_tables + $update_result_meta_tables;
$this->update_db_version();
return $update_results;
}
/**
* Installs custom content tables
*
* @global object $wpdb
* @return array
*/
private function install_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$for_update = array();
foreach ( $this->tables as $file_name => $table_name ) {
$sql = file_get_contents($this->schema_path . 'custom/' . $file_name . '.php');
$sql = sprintf( $sql, $table_name );
$sql .= $charset_collate . ';';
$for_update[] = dbDelta( $sql );
}
return $for_update;
}
/**
* Installs custom meta tables for custom tables
*
* @global object $wpdb
* @return array
*/
private function install_meta_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$for_update = array();
foreach ( $this->meta_tables as $index => $table_name ) {
$sql = file_get_contents($this->schema_path . 'custom-meta/custom-meta.php');
$sql = sprintf( $sql, $table_name );
$sql .= $charset_collate . ';';
$for_update[] = dbDelta( $sql );
}
return $for_update;
}
/**
* Updates database version in options table
*/
private function update_db_version() {
add_option( $this->prefix . '_db_version', $this->version );
}
/**
* Returns all table names (for uninstallation)
*
* @return array
*/
public function get_table_names(){
return $this->tables + $this->meta_tables;
}
/*
* ================================
* Initialisation methods
* ================================
*/
/**
* Initialises all content on WP init
*/
public function init() {
// intialise cpts and taxonomies
add_action( 'init', array( $this, 'init_wp_types' ) );
// initialise meta tables
add_action( 'init', array( $this, 'hook_meta_tables' ), 0 );
add_action( 'switch_blog', array( $this, 'hook_meta_tables' ), 0 );
}
/**
* Initialises cpts & taxonomies
*/
public function init_wp_types() {
$this->register_post_types();
$this->register_taxonomies();
}
/**
* Registers cpts
*
*/
public function register_post_types() {
foreach ( $this->architecture['post_type'] as $post_type ) {
// include the schema
$arguments = include_once $this->schema_path . 'post_type/' . $post_type . '.php';
// register post_type
register_post_type( $post_type, $arguments );
}
}
/**
* Registers taxonomies
*
*/
public function register_taxonomies() {
foreach ( $this->architecture['taxonomy'] as $taxonomy=>$object ) {
if(empty($object)){
$object = array();
}
// include the schema
$arguments = include_once $this->schema_path . 'taxonomy/' . $taxonomy . '.php';
// register taxonomy
register_taxonomy( $taxonomy, $object, $arguments );
}
}
/**
* Initialises custom meta tables for Metadata API
*
* @global object $wpdb
*/
public function hook_meta_tables() {
global $wpdb;
foreach ( $this->meta_tables as $name=>$table ) {
$wpdb->$name = $this->prettify( $name ) . 'meta';
$wpdb->tables[] = $this->prettify( $name ) . 'meta';
}
}
/*
* ================================
* Helper methods
* ================================
*/
/**
* Replaces hyphens with underscores in a string for use in table names
*
* @param string $string_with_hyphens
* @return string
*/
private function prettify( $string_with_hyphens ) {
$string_with__s = str_replace( '-', '_', $string_with_hyphens );
return $string_with__s;
}
}
}