-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwshop-core.php
168 lines (146 loc) · 5.41 KB
/
wshop-core.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
<?php
/*
* Add Custom Post Type 'Product'
*/
function wshop_create_custom_post() {
$labels = array(
'name' => 'Products',
'all_items' => 'All Products',
'singular_name' => 'Product',
'add_new' => 'Add New Product',
'add_new_item' => 'Add New Product',
'edit_item' => 'Edit Product',
'new_item' => 'New Product',
'view_item' => 'View Product',
'search_items' => 'Search Products',
'not_found' => 'No products found',
'not_found_in_trash' => 'No products found in Trash'
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'publicly_queryable' => true,
'capability_type' => 'page',
'capabilities' => array(
// Removes support for the "Add New" function
'create_posts' => false,
),
'map_meta_cap' => true,
'menu_icon' => 'dashicons-cart',
'menu_position' => 22,
'hierarchical' => true,
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'page-attributes',
'revisions'
),
'rewrite' => array(
'slug' => get_option('wshop_rewrite_slug')
)
);
register_post_type('wps-product', $args);
}
add_action('init', 'wshop_create_custom_post', 10);
/*
* Add custom taxonomy
*/
function wshop_create_custom_taxonomy(){
$labels = array(
'name' => _x( 'Collections', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Collection', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Collections', 'textdomain' ),
'all_items' => __( 'All Collections', 'textdomain' ),
'parent_item' => __( 'Parent Collection', 'textdomain' ),
'parent_item_colon' => __( 'Parent Collection:', 'textdomain' ),
'edit_item' => __( 'Edit Collection', 'textdomain' ),
'update_item' => __( 'Update Collection', 'textdomain' ),
'add_new_item' => __( 'Add New Collection', 'textdomain' ),
'new_item_name' => __( 'New Collection Name', 'textdomain' ),
'menu_name' => __( 'Collections', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => get_option('wshop_collections_slug') ),
);
register_taxonomy( 'wps_collection', 'wps-product', $args );
}
add_action('init', 'wshop_create_custom_taxonomy', 10);
/*
* Make sure products are sorted by menu order */
function sort_wps_products($q) {
if(!$q->is_main_query() || is_admin())
return;
if(
!is_post_type_archive('wps-product') &&
!is_tax(array('wps_collection'))
) return;
$q->set('orderby', 'menu_order');
$q->set('order', 'ASC');
}
add_action('parse_query', 'sort_wps_products');
/*
* Convenience functions for getting and echoing product ID
*/
function get_the_product_id($post = 0){
$post = get_post($post);
$product_id = has_product() ? $post->_wshop_product_id : '';
return $product_id;
}
function the_product_id($post = 0){
echo get_the_product_id($post);
}
/*
* Convenience function for checking if a product exists on a page
*/
function has_product($post = 0){
$post = get_post($post);
return isset( $post->_wshop_product_id ) and strlen( $post->_wshop_product_id );
}
/*
* Convenience function for getting permalinks
*/
function get_wshop_collections_slug(){
return get_option('wshop_collections_slug');
}
function get_wshop_shop_slug(){
return get_option('wshop_rewrite_slug');
}
function get_wshop_domain(){
return get_option('wshop_domain');
}
function get_wshop_api_key(){
return get_option('wshop_api_key');
}
/*
* Ajax endpoint for finding WP ID from product ID
*/
function wps_get_wp_info_from_product_id(){
$product_id = $_REQUEST['product_id'];
$args = array(
'posts_per_page' => 1,
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_key' => '_wshop_product_id',
'meta_value' => $product_id,
'post_type' => 'wps-product'
);
$target = get_posts($args);
$result = array();
if( empty($target) ){
$result['status'] = 'Error! Product not found.';
} else {
$result = apply_filters('rez_serialize_object', reset($target));
}
echo json_encode($result);
wp_die();
}
add_action( 'wp_ajax_wp_url_from_product_id', 'wps_get_wp_info_from_product_id' );
add_action( 'wp_ajax_nopriv_wp_url_from_product_id', 'wps_get_wp_info_from_product_id' );