-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.koi-pricing.php
352 lines (308 loc) · 13.7 KB
/
class.koi-pricing.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
class KoiPricing {
private static $initiated = false;
const INV_POST_TYPE = 'inventory';
const TERM_START_DATE = '__term_start_date';
const TERM_END_DATE = '__term_end_date';
const TERM_PHOTO_DATE = '__term_photo_date';
public static $post_statuses = [
'to_review' => 'To Review',
'reviewed' => 'Reviewed',
'deployed' => 'Deployed',
'live' => 'Live',
'sold' => 'Sold',
'unsold' => 'Unsold',
'cancelled' => 'Cancelled',
];
public static function init() {
if ( ! self::$initiated ) {
self::init_hooks();
}
}
private static function init_hooks() {
self::$initiated = true;
add_action('product_taxonomies_added', array( 'KoiPricing', 'init_post_type' ) );
add_action('inventory_post_type_added', array( 'KoiPricing', 'init_taxonomy' ) );
add_action('inventory_post_type_added', array( 'KoiPricing', 'init_taxonomy_meta_data' ) );
add_action('inventory_post_type_added', array( 'KoiPricing', 'init_post_statuses' ) );
}
public static function init_post_statuses() {
foreach(self::$post_statuses as $key => $label) {
register_post_status( $key, array(
'label' => _x( $label, 'post' ),
'public' => true,
'show_in_admin_all_list' => false,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( $label.' <span class="count">(%s)</span>', $label.' <span class="count">(%s)</span>' )
) );
}
}
public static function init_post_type() {
$labels = array(
'name' => _x( 'Inventories', 'Post Type General Name', 'inventory' ),
'singular_name' => _x( 'Inventory', 'Post Type Singular Name', 'inventory' ),
'menu_name' => __( 'Inventory', 'inventory' ),
'name_admin_bar' => __( 'Inventory', 'inventory' ),
'archives' => __( 'Item Archives', 'inventory' ),
'attributes' => __( 'Item Attributes', 'inventory' ),
'parent_item_colon' => __( 'Parent Item:', 'inventory' ),
'all_items' => __( 'All Inventories', 'inventory' ),
'add_new_item' => __( 'Add New Item', 'inventory' ),
'add_new' => __( 'Add New', 'inventory' ),
'new_item' => __( 'New Item', 'inventory' ),
'edit_item' => __( 'Edit Item', 'inventory' ),
'update_item' => __( 'Update Item', 'inventory' ),
'view_item' => __( 'View Item', 'inventory' ),
'view_items' => __( 'View Items', 'inventory' ),
'search_items' => __( 'Search Item', 'inventory' ),
'not_found' => __( 'Not found', 'inventory' ),
'not_found_in_trash' => __( 'Not found in Trash', 'inventory' ),
'featured_image' => __( 'Featured Image', 'inventory' ),
'set_featured_image' => __( 'Set featured image', 'inventory' ),
'remove_featured_image' => __( 'Remove featured image', 'inventory' ),
'use_featured_image' => __( 'Use as featured image', 'inventory' ),
'insert_into_item' => __( 'Insert into item', 'inventory' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'inventory' ),
'items_list' => __( 'Items list', 'inventory' ),
'items_list_navigation' => __( 'Items list navigation', 'inventory' ),
'filter_items_list' => __( 'Filter items list', 'inventory' ),
);
$args = array(
'label' => __( 'Inventory', 'inventory' ),
'description' => __( 'Inventory Description', 'inventory' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'custom-fields' ),
'taxonomies' => array( 'post_tag', 'breeder', 'variety', 'price_type', 'product_cat' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'inventory', $args );
do_action( 'inventory_post_type_added' ); //Register taxnomoies after post_type added
}
public static function init_taxonomy() {
$labels = array(
'name' => _x( 'Auction Groups', 'taxonomy general name' ),
'singular_name' => _x( 'Auction Group', 'taxonomy singular name' ),
'search_items' => __( 'Search Auction Groups' ),
'all_items' => __( 'All Auction Groups' ),
'parent_item' => __( 'Parent Auction Group' ),
'parent_item_colon' => __( 'Parent Auction Group:' ),
'edit_item' => __( 'Edit Auction Group' ),
'update_item' => __( 'Update Auction Group' ),
'add_new_item' => __( 'Add New Auction Group' ),
'new_item_name' => __( 'New Auction Group Name' ),
'menu_name' => __( 'Auction Groups' ),
);
// Now register the taxonomy
register_taxonomy('auction_groups',
array('inventory'),
array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'auction_group' ),
)
);
$labels = array(
'name' => _x( 'Photo Groups', 'taxonomy general name' ),
'singular_name' => _x( 'Photo Group', 'taxonomy singular name' ),
'search_items' => __( 'Search Photo Groups' ),
'popular_items' => __( 'Popular Photo Groups' ),
'all_items' => __( 'All Photo Groups' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Photo Group' ),
'update_item' => __( 'Update Photo Group' ),
'add_new_item' => __( 'Add New Photo Group' ),
'new_item_name' => __( 'New Photo Group Name' ),
'separate_items_with_commas' => __( 'Separate photo groups with commas' ),
'add_or_remove_items' => __( 'Add or remove photo groups' ),
'choose_from_most_used' => __( 'Choose from the most used photo groups' ),
'menu_name' => __( 'Photo Groups' ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('photo_groups',
'inventory',
array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'photo_group' ),
)
);
}
public static function init_taxonomy_meta_data() {
// Auction Group
register_meta( 'term', self::TERM_START_DATE, array( 'KoiPricing_Admin', 'sanitize_term_meta_text' ) );
register_meta( 'term', self::TERM_END_DATE, array( 'KoiPricing_Admin', 'sanitize_term_meta_text' ) );
// Photo Group
register_meta( 'term', self::TERM_PHOTO_DATE, array( 'KoiPricing_Admin', 'sanitize_term_meta_text' ) );
}
public static function plugin_activation() {
}
public static function get_tax_query() {
$term_info = self::get_current_session_term();
$tax_query = self::get_current_session_tax_filter();
if($term_info) {
$field = ($term_info['taxonomy'] == 'photo_groups') ? 'slug' : 'term_id';
$tax_query[] = [
'taxonomy' => $term_info['taxonomy'],
'field' => $field,
'terms' => $term_info['term_id']
];
}
if($tax_query) {
$tax_query['relation'] = 'OR';
return [
$tax_query
];
}
return false;
}
public static function get_max_num_page() {
$tax_query = self::get_tax_query();
$meta_query = self::get_current_session_filter();
$args = array(
'post_type' => KoiPricing::INV_POST_TYPE,
'posts_per_page' => -1
);
if($tax_query) {
$args['tax_query'] = $tax_query;
}
if ($meta_query) {
$args['meta_query'] = $meta_query;
}
$posts = new WP_Query($args);
return ceil($posts->post_count/10);
}
public static function get_inventory_collection($offset = 0, $page_size = 10) {
$tax_query = self::get_tax_query();
$meta_query = self::get_current_session_filter();
$args = array(
'posts_per_page' => $page_size,
'offset' => $offset,
'post_type' => KoiPricing::INV_POST_TYPE,
'fields' => 'ids'
);
if($tax_query) {
$args['tax_query'] = $tax_query;
}
if ($meta_query) {
$args['meta_query'] = $meta_query;
}
return get_posts( $args );
}
public static function get_taxonomy_terms($taxonomy, $meta_query = []) {
$args = [
'taxonomy' => $taxonomy,
'hide_empty' => false
];
if(count($meta_query)) {
$args['meta_query'] = $meta_query;
}
return get_terms($args);
}
public static function set_session_term($term_id, $taxonomy) {
update_user_meta(get_current_user_id(), '_cur_term_id', $term_id);
update_user_meta(get_current_user_id(), '_cur_taxonomy', $taxonomy);
}
public static function set_filter_session($meta_query) {
update_user_meta(get_current_user_id(), '_cur_meta_query', json_encode($meta_query));
}
public static function set_tax_filter_session($tax_query) {
update_user_meta(get_current_user_id(), '_cur_tax_query', json_encode($tax_query));
}
public static function set_filter_request_session($request) {
update_user_meta(get_current_user_id(), '_cur_request', json_encode($request));
}
public static function get_current_session_filter_request() {
$request = get_user_meta(get_current_user_id(), '_cur_request', true);
if( $request ) {
return json_decode($request, true);
}
return false;
}
public static function get_current_session_term() {
$term_id = get_user_meta(get_current_user_id(), '_cur_term_id', true);
$taxonomy = get_user_meta(get_current_user_id(), '_cur_taxonomy', true);
if( $term_id && $taxonomy ) {
return [
'term_id' => $term_id,
'taxonomy' => $taxonomy
];
}
return false;
}
public static function get_current_session_tax_filter() {
$tax_query = get_user_meta(get_current_user_id(), '_cur_tax_query', true);
if( $tax_query ) {
return json_decode($tax_query, true);
}
return false;
}
public static function get_current_session_filter() {
$meta_query = get_user_meta(get_current_user_id(), '_cur_meta_query', true);
if( $meta_query ) {
return json_decode($meta_query, true);
}
return false;
}
public static function unset_session_term() {
delete_user_meta(get_current_user_id(), '_cur_term_id');
delete_user_meta(get_current_user_id(), '_cur_taxonomy');
}
public static function reset_session() {
delete_user_meta(get_current_user_id(), '_cur_term_id');
delete_user_meta(get_current_user_id(), '_cur_taxonomy');
delete_user_meta(get_current_user_id(), '_cur_meta_query');
delete_user_meta(get_current_user_id(), '_cur_tax_query');
delete_user_meta(get_current_user_id(), '_cur_request');
}
public static function get_term_name_by_id($term_id) {
return get_term( $term_id )->name;
}
public static function get_term_name_by_slug($slug) {
return get_term_by('slug', $slug, 'photo_groups')->name;
}
public static function get_taxonomy_name_by_slug($slug) {
$taxonomies = [
'auction_groups' => 'Auction Group',
'photo_groups' => 'Photo Group',
'breeder' => 'Breeder',
'variety' => 'Variety',
'product_cat' => 'Product Category'
];
return $taxonomies[$slug];
}
public static function get_inventory_thumbnail_src($post_id) {
$images = get_children( array (
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if ( empty($images) ) {
return false;
} else {
foreach ( $images as $attachment_id => $attachment ) {
return wp_get_attachment_image_src( $attachment_id, 'medium' );
}
}
}
}