-
Notifications
You must be signed in to change notification settings - Fork 2
/
class.model.php
118 lines (99 loc) · 3.26 KB
/
class.model.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
<?php
namespace WPS;
class Model {
public $post_type,
$default_cpt_args = [
'public' => false,
'show_ui' => true,
'show_in_nav_menus' => true,
'capability_type' => 'post',
'supports' => [
'title',
'editor',
'thumbnail',
'page-attributes'
]
],
$default_query_args = [
'paged' => 1,
'post_status' => 'publish'
],
$_wpdb;
public function __construct($post_type, array $cpt_args = []) {
$this->post_type = $post_type;
if(post_type_exists($this->post_type)) return;
$this->register_custom_post_type(
$post_type,
wp_parse_args($cpt_args, $this->default_cpt_args)
);
do_action("wps_{$post_type}_cpt_registered", $this->post_type);
}
public function query(array $query_args = null) {
$this->default_query_args['post_type'] = $this->post_type;
$this->default_query_args['posts_per_page'] = get_option('posts_per_page');
$args = array_merge(
$this->default_query_args,
(!empty($query_args) ? $this->set_paged_value($query_args) : [])
);
return new \WP_Query($args);
}
public function wpdb() {
if(!empty($this->_wpdb)) return $this->_wpdb;
$this->_wpdb = $GLOBALS['wpdb'];
return $this->_wpdb;
}
protected function generate_cpt_labels_for($singular, $pural) {
return [
'name' => $pural,
'singular_name' => $singular,
'add_new' => 'Add New',
'add_new_item' => "Add New $singular",
'edit_item' => "Edit $singular",
'new_item' => "New $singular",
'view_item' => "View $singular",
'search_items' => "Search $pural",
'not_found' => "No $pural found",
'not_found_in_trash' => "No $pural found in Trash",
'parent_item_colon' => "Parent $singular",
'all_items' => "All $pural",
'archives' => "$singular Archives",
'insert_into_item' => "Insert into $singular",
'uploaded_to_this_item' => "Uploaded to this $singular",
'not_found' => "No $pural found"
];
}
protected function generate_taxonomy_labels_for($singular, $pural) {
return [
'name' => $pural,
'singular_name' => $singular,
'all_items' => "All $pural",
'parent_item' => "Parent $singular",
'parent_item_colon' => "Parent $singular:",
'edit_item' => "Edit $singular",
'update_item' => "Update $singular",
'add_new_item' => "Add New $singular",
'new_item_name' => "New $singular Name",
'menu_name' => $pural
];
}
protected function register_custom_taxonomies($taxonomies) {
foreach($taxonomies as $taxonomy => $taxonomy_options) {
register_taxonomy(
$taxonomy,
$taxonomy_options['post_type'],
$taxonomy_options['taxonomy_args']
);
}
}
private function register_custom_post_type($cpt, $cpt_args) {
register_post_type($cpt, $cpt_args);
}
private function set_paged_value(array $query_args) {
$default_paged = !empty($query_args['paged']) ?
$query_args['paged'] :
$this->default_query_args['paged'];
$paged_query_var = get_query_var('paged');
$query_args['paged'] = $paged_query_var ? $paged_query_var : $default_paged;
return $query_args;
}
}