1 <?php
  2 if ( ! defined( 'ABSPATH' ) ) {
  3     exit; // Exit if accessed directly
  4 }
  5 
  6 /**
  7  * Post types
  8  *
  9  * Registers post types and taxonomies
 10  *
 11  * @class       WC_Post_types
 12  * @version     2.1.0
 13  * @package     WooCommerce/Classes/Products
 14  * @category    Class
 15  * @author      WooThemes
 16  */
 17 class WC_Post_types {
 18 
 19     /**
 20      * Constructor
 21      */
 22     public function __construct() {
 23         add_action( 'init', array( __CLASS__, 'register_taxonomies' ), 5 );
 24         add_action( 'init', array( __CLASS__, 'register_post_types' ), 5 );
 25     }
 26 
 27     /**
 28      * Register WooCommerce taxonomies.
 29      */
 30     public static function register_taxonomies() {
 31         if ( taxonomy_exists( 'product_type' ) )
 32             return;
 33 
 34         do_action( 'woocommerce_register_taxonomy' );
 35 
 36         $permalinks = get_option( 'woocommerce_permalinks' );
 37 
 38         register_taxonomy( 'product_type',
 39             apply_filters( 'woocommerce_taxonomy_objects_product_type', array( 'product' ) ),
 40             apply_filters( 'woocommerce_taxonomy_args_product_type', array(
 41                 'hierarchical'          => false,
 42                 'show_ui'               => false,
 43                 'show_in_nav_menus'     => false,
 44                 'query_var'             => is_admin(),
 45                 'rewrite'               => false,
 46                 'public'                => false
 47             ) )
 48         );
 49 
 50         register_taxonomy( 'product_cat',
 51             apply_filters( 'woocommerce_taxonomy_objects_product_cat', array( 'product' ) ),
 52             apply_filters( 'woocommerce_taxonomy_args_product_cat', array(
 53                 'hierarchical'          => true,
 54                 'update_count_callback' => '_wc_term_recount',
 55                 'label'                 => __( 'Product Categories', 'woocommerce' ),
 56                 'labels' => array(
 57                         'name'              => __( 'Product Categories', 'woocommerce' ),
 58                         'singular_name'     => __( 'Product Category', 'woocommerce' ),
 59                         'menu_name'         => _x( 'Categories', 'Admin menu name', 'woocommerce' ),
 60                         'search_items'      => __( 'Search Product Categories', 'woocommerce' ),
 61                         'all_items'         => __( 'All Product Categories', 'woocommerce' ),
 62                         'parent_item'       => __( 'Parent Product Category', 'woocommerce' ),
 63                         'parent_item_colon' => __( 'Parent Product Category:', 'woocommerce' ),
 64                         'edit_item'         => __( 'Edit Product Category', 'woocommerce' ),
 65                         'update_item'       => __( 'Update Product Category', 'woocommerce' ),
 66                         'add_new_item'      => __( 'Add New Product Category', 'woocommerce' ),
 67                         'new_item_name'     => __( 'New Product Category Name', 'woocommerce' )
 68                     ),
 69                 'show_ui'               => true,
 70                 'query_var'             => true,
 71                 'capabilities'          => array(
 72                     'manage_terms'      => 'manage_product_terms',
 73                     'edit_terms'        => 'edit_product_terms',
 74                     'delete_terms'      => 'delete_product_terms',
 75                     'assign_terms'      => 'assign_product_terms',
 76                 ),
 77                 'rewrite'               => array(
 78                     'slug'         => empty( $permalinks['category_base'] ) ? _x( 'product-category', 'slug', 'woocommerce' ) : $permalinks['category_base'],
 79                     'with_front'   => false,
 80                     'hierarchical' => true,
 81                 ),
 82             ) )
 83         );
 84 
 85         register_taxonomy( 'product_tag',
 86             apply_filters( 'woocommerce_taxonomy_objects_product_tag', array( 'product' ) ),
 87             apply_filters( 'woocommerce_taxonomy_args_product_tag', array(
 88                 'hierarchical'          => false,
 89                 'update_count_callback' => '_wc_term_recount',
 90                 'label'                 => __( 'Product Tags', 'woocommerce' ),
 91                 'labels' => array(
 92                         'name'              => __( 'Product Tags', 'woocommerce' ),
 93                         'singular_name'     => __( 'Product Tag', 'woocommerce' ),
 94                         'menu_name'         => _x( 'Tags', 'Admin menu name', 'woocommerce' ),
 95                         'search_items'      => __( 'Search Product Tags', 'woocommerce' ),
 96                         'all_items'         => __( 'All Product Tags', 'woocommerce' ),
 97                         'parent_item'       => __( 'Parent Product Tag', 'woocommerce' ),
 98                         'parent_item_colon' => __( 'Parent Product Tag:', 'woocommerce' ),
 99                         'edit_item'         => __( 'Edit Product Tag', 'woocommerce' ),
100                         'update_item'       => __( 'Update Product Tag', 'woocommerce' ),
101                         'add_new_item'      => __( 'Add New Product Tag', 'woocommerce' ),
102                         'new_item_name'     => __( 'New Product Tag Name', 'woocommerce' )
103                     ),
104                 'show_ui'               => true,
105                 'query_var'             => true,
106                 'capabilities'          => array(
107                     'manage_terms'      => 'manage_product_terms',
108                     'edit_terms'        => 'edit_product_terms',
109                     'delete_terms'      => 'delete_product_terms',
110                     'assign_terms'      => 'assign_product_terms',
111                 ),
112                 'rewrite'               => array(
113                     'slug'       => empty( $permalinks['tag_base'] ) ? _x( 'product-tag', 'slug', 'woocommerce' ) : $permalinks['tag_base'],
114                     'with_front' => false
115                 ),
116             ) )
117         );
118 
119         register_taxonomy( 'product_shipping_class',
120             apply_filters( 'woocommerce_taxonomy_objects_product_shipping_class', array('product', 'product_variation') ),
121             apply_filters( 'woocommerce_taxonomy_args_product_shipping_class', array(
122                 'hierarchical'          => true,
123                 'update_count_callback' => '_update_post_term_count',
124                 'label'                 => __( 'Shipping Classes', 'woocommerce' ),
125                 'labels' => array(
126                         'name'              => __( 'Shipping Classes', 'woocommerce' ),
127                         'singular_name'     => __( 'Shipping Class', 'woocommerce' ),
128                         'menu_name'         => _x( 'Shipping Classes', 'Admin menu name', 'woocommerce' ),
129                         'search_items'      => __( 'Search Shipping Classes', 'woocommerce' ),
130                         'all_items'         => __( 'All Shipping Classes', 'woocommerce' ),
131                         'parent_item'       => __( 'Parent Shipping Class', 'woocommerce' ),
132                         'parent_item_colon' => __( 'Parent Shipping Class:', 'woocommerce' ),
133                         'edit_item'         => __( 'Edit Shipping Class', 'woocommerce' ),
134                         'update_item'       => __( 'Update Shipping Class', 'woocommerce' ),
135                         'add_new_item'      => __( 'Add New Shipping Class', 'woocommerce' ),
136                         'new_item_name'     => __( 'New Shipping Class Name', 'woocommerce' )
137                     ),
138                 'show_ui'               => true,
139                 'show_in_nav_menus'     => false,
140                 'query_var'             => is_admin(),
141                 'capabilities'          => array(
142                     'manage_terms'      => 'manage_product_terms',
143                     'edit_terms'        => 'edit_product_terms',
144                     'delete_terms'      => 'delete_product_terms',
145                     'assign_terms'      => 'assign_product_terms',
146                 ),
147                 'rewrite'               => false,
148             ) )
149         );
150 
151         register_taxonomy( 'shop_order_status',
152             apply_filters( 'woocommerce_taxonomy_objects_shop_order_status', array('shop_order') ),
153             apply_filters( 'woocommerce_taxonomy_args_shop_order_status', array(
154                 'hierarchical'          => false,
155                 'update_count_callback' => '_update_post_term_count',
156                 'show_ui'               => false,
157                 'show_in_nav_menus'     => false,
158                 'query_var'             => is_admin(),
159                 'rewrite'               => false,
160                 'public'                => false
161             ) )
162         );
163 
164         global $wc_product_attributes, $woocommerce;
165 
166         $wc_product_attributes = array();
167 
168         if ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) {
169             foreach ( $attribute_taxonomies as $tax ) {
170                 if ( $name = wc_attribute_taxonomy_name( $tax->attribute_name ) ) {
171 
172                     $label = ! empty( $tax->attribute_label ) ? $tax->attribute_label : $tax->attribute_name;
173 
174                     $wc_product_attributes[ $name ] = $tax;
175 
176                     register_taxonomy( $name,
177                         apply_filters( 'woocommerce_taxonomy_objects_' . $name, array( 'product' ) ),
178                         apply_filters( 'woocommerce_taxonomy_args_' . $name, array(
179                             'hierarchical'              => true,
180                             'update_count_callback'     => '_update_post_term_count',
181                             'labels' => array(
182                                     'name'                      => $label,
183                                     'singular_name'             => $label,
184                                     'search_items'              => sprintf( __( 'Search %s', 'woocommerce' ), $label ),
185                                     'all_items'                 => sprintf( __( 'All %s', 'woocommerce' ), $label ),
186                                     'parent_item'               => sprintf( __( 'Parent %s', 'woocommerce' ), $label ),
187                                     'parent_item_colon'         => sprintf( __( 'Parent %s:', 'woocommerce' ), $label ),
188                                     'edit_item'                 => sprintf( __( 'Edit %s', 'woocommerce' ), $label ),
189                                     'update_item'               => sprintf( __( 'Update %s', 'woocommerce' ), $label ),
190                                     'add_new_item'              => sprintf( __( 'Add New %s', 'woocommerce' ), $label ),
191                                     'new_item_name'             => sprintf( __( 'New %s', 'woocommerce' ), $label )
192                                 ),
193                             'show_ui'                   => false,
194                             'query_var'                 => true,
195                             'capabilities'          => array(
196                                 'manage_terms'      => 'manage_product_terms',
197                                 'edit_terms'        => 'edit_product_terms',
198                                 'delete_terms'      => 'delete_product_terms',
199                                 'assign_terms'      => 'assign_product_terms',
200                             ),
201                             'show_in_nav_menus'         => apply_filters( 'woocommerce_attribute_show_in_nav_menus', false, $name ),
202                             'rewrite'                   => array(
203                                 'slug'         => ( empty( $permalinks['attribute_base'] ) ? '' : trailingslashit( $permalinks['attribute_base'] ) ) . sanitize_title( $tax->attribute_name ),
204                                 'with_front'   => false,
205                                 'hierarchical' => true
206                             ),
207                         ) )
208                     );
209                 }
210             }
211             
212             do_action( 'woocommerce_after_register_taxonomy' );
213         }
214     }
215 
216     /**
217      * Register core post types
218      */
219     public static function register_post_types() {
220         if ( post_type_exists('product') )
221             return;
222 
223         do_action( 'woocommerce_register_post_type' );
224 
225         $permalinks        = get_option( 'woocommerce_permalinks' );
226         $product_permalink = empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base'];
227 
228         register_post_type( "product",
229             apply_filters( 'woocommerce_register_post_type_product',
230                 array(
231                     'labels' => array(
232                             'name'                  => __( 'Products', 'woocommerce' ),
233                             'singular_name'         => __( 'Product', 'woocommerce' ),
234                             'menu_name'             => _x( 'Products', 'Admin menu name', 'woocommerce' ),
235                             'add_new'               => __( 'Add Product', 'woocommerce' ),
236                             'add_new_item'          => __( 'Add New Product', 'woocommerce' ),
237                             'edit'                  => __( 'Edit', 'woocommerce' ),
238                             'edit_item'             => __( 'Edit Product', 'woocommerce' ),
239                             'new_item'              => __( 'New Product', 'woocommerce' ),
240                             'view'                  => __( 'View Product', 'woocommerce' ),
241                             'view_item'             => __( 'View Product', 'woocommerce' ),
242                             'search_items'          => __( 'Search Products', 'woocommerce' ),
243                             'not_found'             => __( 'No Products found', 'woocommerce' ),
244                             'not_found_in_trash'    => __( 'No Products found in trash', 'woocommerce' ),
245                             'parent'                => __( 'Parent Product', 'woocommerce' )
246                         ),
247                     'description'           => __( 'This is where you can add new products to your store.', 'woocommerce' ),
248                     'public'                => true,
249                     'show_ui'               => true,
250                     'capability_type'       => 'product',
251                     'map_meta_cap'          => true,
252                     'publicly_queryable'    => true,
253                     'exclude_from_search'   => false,
254                     'hierarchical'          => false, // Hierarchical causes memory issues - WP loads all records!
255                     'rewrite'               => $product_permalink ? array( 'slug' => untrailingslashit( $product_permalink ), 'with_front' => false, 'feeds' => true ) : false,
256                     'query_var'             => true,
257                     'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', 'custom-fields', 'page-attributes' ),
258                     'has_archive'           => ( $shop_page_id = wc_get_page_id( 'shop' ) ) && get_page( $shop_page_id ) ? get_page_uri( $shop_page_id ) : 'shop',
259                     'show_in_nav_menus'     => true
260                 )
261             )
262         );
263 
264         register_post_type( "product_variation",
265             apply_filters( 'woocommerce_register_post_type_product_variation',
266                 array(
267                     'label'        => __( 'Variations', 'woocommerce' ),
268                     'public'       => false,
269                     'hierarchical' => false,
270                     'supports'     => false
271                 )
272             )
273         );
274 
275         $menu_name = _x('Orders', 'Admin menu name', 'woocommerce' );
276 
277         if ( $order_count = wc_processing_order_count() ) {
278             $menu_name .= " <span class='awaiting-mod update-plugins count-$order_count'><span class='processing-count'>" . number_format_i18n( $order_count ) . "</span></span>" ;
279         }
280 
281         register_post_type( "shop_order",
282             apply_filters( 'woocommerce_register_post_type_shop_order',
283                 array(
284                     'labels' => array(
285                             'name'                  => __( 'Orders', 'woocommerce' ),
286                             'singular_name'         => __( 'Order', 'woocommerce' ),
287                             'add_new'               => __( 'Add Order', 'woocommerce' ),
288                             'add_new_item'          => __( 'Add New Order', 'woocommerce' ),
289                             'edit'                  => __( 'Edit', 'woocommerce' ),
290                             'edit_item'             => __( 'Edit Order', 'woocommerce' ),
291                             'new_item'              => __( 'New Order', 'woocommerce' ),
292                             'view'                  => __( 'View Order', 'woocommerce' ),
293                             'view_item'             => __( 'View Order', 'woocommerce' ),
294                             'search_items'          => __( 'Search Orders', 'woocommerce' ),
295                             'not_found'             => __( 'No Orders found', 'woocommerce' ),
296                             'not_found_in_trash'    => __( 'No Orders found in trash', 'woocommerce' ),
297                             'parent'                => __( 'Parent Orders', 'woocommerce' ),
298                             'menu_name'             => $menu_name
299                         ),
300                     'description'           => __( 'This is where store orders are stored.', 'woocommerce' ),
301                     'public'                => false,
302                     'show_ui'               => true,
303                     'capability_type'       => 'shop_order',
304                     'map_meta_cap'          => true,
305                     'publicly_queryable'    => false,
306                     'exclude_from_search'   => true,
307                     'show_in_menu'          => current_user_can( 'manage_woocommerce' ) ? 'woocommerce' : true,
308                     'hierarchical'          => false,
309                     'show_in_nav_menus'     => false,
310                     'rewrite'               => false,
311                     'query_var'             => false,
312                     'supports'              => array( 'title', 'comments', 'custom-fields' ),
313                     'has_archive'           => false,
314                 )
315             )
316         );
317 
318         if ( get_option( 'woocommerce_enable_coupons' ) == 'yes' ) {
319             register_post_type( "shop_coupon",
320                 apply_filters( 'woocommerce_register_post_type_shop_coupon',
321                     array(
322                         'labels' => array(
323                                 'name'                  => __( 'Coupons', 'woocommerce' ),
324                                 'singular_name'         => __( 'Coupon', 'woocommerce' ),
325                                 'menu_name'             => _x( 'Coupons', 'Admin menu name', 'woocommerce' ),
326                                 'add_new'               => __( 'Add Coupon', 'woocommerce' ),
327                                 'add_new_item'          => __( 'Add New Coupon', 'woocommerce' ),
328                                 'edit'                  => __( 'Edit', 'woocommerce' ),
329                                 'edit_item'             => __( 'Edit Coupon', 'woocommerce' ),
330                                 'new_item'              => __( 'New Coupon', 'woocommerce' ),
331                                 'view'                  => __( 'View Coupons', 'woocommerce' ),
332                                 'view_item'             => __( 'View Coupon', 'woocommerce' ),
333                                 'search_items'          => __( 'Search Coupons', 'woocommerce' ),
334                                 'not_found'             => __( 'No Coupons found', 'woocommerce' ),
335                                 'not_found_in_trash'    => __( 'No Coupons found in trash', 'woocommerce' ),
336                                 'parent'                => __( 'Parent Coupon', 'woocommerce' )
337                             ),
338                         'description'           => __( 'This is where you can add new coupons that customers can use in your store.', 'woocommerce' ),
339                         'public'                => false,
340                         'show_ui'               => true,
341                         'capability_type'       => 'shop_coupon',
342                         'map_meta_cap'          => true,
343                         'publicly_queryable'    => false,
344                         'exclude_from_search'   => true,
345                         'show_in_menu'          => current_user_can( 'manage_woocommerce' ) ? 'woocommerce' : true,
346                         'hierarchical'          => false,
347                         'rewrite'               => false,
348                         'query_var'             => false,
349                         'supports'              => array( 'title' ),
350                         'show_in_nav_menus'     => false,
351                         'show_in_admin_bar'     => true
352                     )
353                 )
354             );
355         }
356     }
357 }
358 
359 new WC_Post_types();
360 
WooCommerce API documentation generated by ApiGen 2.8.0