WC_Admin_Taxonomies

WC_Admin_Taxonomies class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/class-wc-admin-taxonomies.php  
  1. class WC_Admin_Taxonomies { 
  2.  
  3. /** 
  4. * Constructor. 
  5. */ 
  6. public function __construct() { 
  7. // Category/term ordering 
  8. add_action( 'create_term', array( $this, 'create_term' ), 5, 3 ); 
  9. add_action( 'delete_term', array( $this, 'delete_term' ), 5 ); 
  10.  
  11. // Add form 
  12. add_action( 'product_cat_add_form_fields', array( $this, 'add_category_fields' ) ); 
  13. add_action( 'product_cat_edit_form_fields', array( $this, 'edit_category_fields' ), 10 ); 
  14. add_action( 'created_term', array( $this, 'save_category_fields' ), 10, 3 ); 
  15. add_action( 'edit_term', array( $this, 'save_category_fields' ), 10, 3 ); 
  16.  
  17. // Add columns 
  18. add_filter( 'manage_edit-product_cat_columns', array( $this, 'product_cat_columns' ) ); 
  19. add_filter( 'manage_product_cat_custom_column', array( $this, 'product_cat_column' ), 10, 3 ); 
  20.  
  21. // Taxonomy page descriptions 
  22. add_action( 'product_cat_pre_add_form', array( $this, 'product_cat_description' ) ); 
  23.  
  24. $attribute_taxonomies = wc_get_attribute_taxonomies(); 
  25.  
  26. if ( ! empty( $attribute_taxonomies ) ) { 
  27. foreach ( $attribute_taxonomies as $attribute ) { 
  28. add_action( 'pa_' . $attribute->attribute_name . '_pre_add_form', array( $this, 'product_attribute_description' ) ); 
  29.  
  30. // Maintain hierarchy of terms 
  31. add_filter( 'wp_terms_checklist_args', array( $this, 'disable_checked_ontop' ) ); 
  32.  
  33. /** 
  34. * Order term when created (put in position 0). 
  35. * @param mixed $term_id 
  36. * @param mixed $tt_id 
  37. * @param string $taxonomy 
  38. */ 
  39. public function create_term( $term_id, $tt_id = '', $taxonomy = '' ) { 
  40. if ( 'product_cat' != $taxonomy && ! taxonomy_is_product_attribute( $taxonomy ) ) { 
  41. return; 
  42.  
  43. $meta_name = taxonomy_is_product_attribute( $taxonomy ) ? 'order_' . esc_attr( $taxonomy ) : 'order'; 
  44.  
  45. update_woocommerce_term_meta( $term_id, $meta_name, 0 ); 
  46.  
  47. /** 
  48. * When a term is deleted, delete its meta. 
  49. * @param mixed $term_id 
  50. */ 
  51. public function delete_term( $term_id ) { 
  52. global $wpdb; 
  53.  
  54. $term_id = absint( $term_id ); 
  55.  
  56. if ( $term_id && get_option( 'db_version' ) < 34370 ) { 
  57. $wpdb->delete( $wpdb->woocommerce_termmeta, array( 'woocommerce_term_id' => $term_id ), array( '%d' ) ); 
  58.  
  59. /** 
  60. * Category thumbnail fields. 
  61. */ 
  62. public function add_category_fields() { 
  63. ?> 
  64. <div class="form-field term-display-type-wrap"> 
  65. <label for="display_type"><?php _e( 'Display type', 'woocommerce' ); ?></label> 
  66. <select id="display_type" name="display_type" class="postform"> 
  67. <option value=""><?php _e( 'Default', 'woocommerce' ); ?></option> 
  68. <option value="products"><?php _e( 'Products', 'woocommerce' ); ?></option> 
  69. <option value="subcategories"><?php _e( 'Subcategories', 'woocommerce' ); ?></option> 
  70. <option value="both"><?php _e( 'Both', 'woocommerce' ); ?></option> 
  71. </select> 
  72. </div> 
  73. <div class="form-field term-thumbnail-wrap"> 
  74. <label><?php _e( 'Thumbnail', 'woocommerce' ); ?></label> 
  75. <div id="product_cat_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( wc_placeholder_img_src() ); ?>" width="60px" height="60px" /></div> 
  76. <div style="line-height: 60px;"> 
  77. <input type="hidden" id="product_cat_thumbnail_id" name="product_cat_thumbnail_id" /> 
  78. <button type="button" class="upload_image_button button"><?php _e( 'Upload/Add image', 'woocommerce' ); ?></button> 
  79. <button type="button" class="remove_image_button button"><?php _e( 'Remove image', 'woocommerce' ); ?></button> 
  80. </div> 
  81. <script type="text/javascript"> 
  82.  
  83. // Only show the "remove image" button when needed 
  84. if ( ! jQuery( '#product_cat_thumbnail_id' ).val() ) { 
  85. jQuery( '.remove_image_button' ).hide(); 
  86.  
  87. // Uploading files 
  88. var file_frame; 
  89.  
  90. jQuery( document ).on( 'click', '.upload_image_button', function( event ) { 
  91.  
  92. event.preventDefault(); 
  93.  
  94. // If the media frame already exists, reopen it. 
  95. if ( file_frame ) { 
  96. file_frame.open(); 
  97. return; 
  98.  
  99. // Create the media frame. 
  100. file_frame = wp.media.frames.downloadable_file = wp.media({ 
  101. title: '<?php _e( "Choose an image", "woocommerce" ); ?>',  
  102. button: { 
  103. text: '<?php _e( "Use image", "woocommerce" ); ?>' 
  104. },  
  105. multiple: false 
  106. }); 
  107.  
  108. // When an image is selected, run a callback. 
  109. file_frame.on( 'select', function() { 
  110. var attachment = file_frame.state().get( 'selection' ).first().toJSON(); 
  111.  
  112. jQuery( '#product_cat_thumbnail_id' ).val( attachment.id ); 
  113. jQuery( '#product_cat_thumbnail' ).find( 'img' ).attr( 'src', attachment.sizes.thumbnail.url ); 
  114. jQuery( '.remove_image_button' ).show(); 
  115. }); 
  116.  
  117. // Finally, open the modal. 
  118. file_frame.open(); 
  119. }); 
  120.  
  121. jQuery( document ).on( 'click', '.remove_image_button', function() { 
  122. jQuery( '#product_cat_thumbnail' ).find( 'img' ).attr( 'src', '<?php echo esc_js( wc_placeholder_img_src() ); ?>' ); 
  123. jQuery( '#product_cat_thumbnail_id' ).val( '' ); 
  124. jQuery( '.remove_image_button' ).hide(); 
  125. return false; 
  126. }); 
  127.  
  128. jQuery( document ).ajaxComplete( function( event, request, options ) { 
  129. if ( request && 4 === request.readyState && 200 === request.status 
  130. && options.data && 0 <= options.data.indexOf( 'action=add-tag' ) ) { 
  131.  
  132. var res = wpAjax.parseAjaxResponse( request.responseXML, 'ajax-response' ); 
  133. if ( ! res || res.errors ) { 
  134. return; 
  135. // Clear Thumbnail fields on submit 
  136. jQuery( '#product_cat_thumbnail' ).find( 'img' ).attr( 'src', '<?php echo esc_js( wc_placeholder_img_src() ); ?>' ); 
  137. jQuery( '#product_cat_thumbnail_id' ).val( '' ); 
  138. jQuery( '.remove_image_button' ).hide(); 
  139. // Clear Display type field on submit 
  140. jQuery( '#display_type' ).val( '' ); 
  141. return; 
  142. } ); 
  143.  
  144. </script> 
  145. <div class="clear"></div> 
  146. </div> 
  147. <?php 
  148.  
  149. /** 
  150. * Edit category thumbnail field. 
  151. * @param mixed $term Term (category) being edited 
  152. */ 
  153. public function edit_category_fields( $term ) { 
  154.  
  155. $display_type = get_woocommerce_term_meta( $term->term_id, 'display_type', true ); 
  156. $thumbnail_id = absint( get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true ) ); 
  157.  
  158. if ( $thumbnail_id ) { 
  159. $image = wp_get_attachment_thumb_url( $thumbnail_id ); 
  160. } else { 
  161. $image = wc_placeholder_img_src(); 
  162. ?> 
  163. <tr class="form-field"> 
  164. <th scope="row" valign="top"><label><?php _e( 'Display type', 'woocommerce' ); ?></label></th> 
  165. <td> 
  166. <select id="display_type" name="display_type" class="postform"> 
  167. <option value="" <?php selected( '', $display_type ); ?>><?php _e( 'Default', 'woocommerce' ); ?></option> 
  168. <option value="products" <?php selected( 'products', $display_type ); ?>><?php _e( 'Products', 'woocommerce' ); ?></option> 
  169. <option value="subcategories" <?php selected( 'subcategories', $display_type ); ?>><?php _e( 'Subcategories', 'woocommerce' ); ?></option> 
  170. <option value="both" <?php selected( 'both', $display_type ); ?>><?php _e( 'Both', 'woocommerce' ); ?></option> 
  171. </select> 
  172. </td> 
  173. </tr> 
  174. <tr class="form-field"> 
  175. <th scope="row" valign="top"><label><?php _e( 'Thumbnail', 'woocommerce' ); ?></label></th> 
  176. <td> 
  177. <div id="product_cat_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( $image ); ?>" width="60px" height="60px" /></div> 
  178. <div style="line-height: 60px;"> 
  179. <input type="hidden" id="product_cat_thumbnail_id" name="product_cat_thumbnail_id" value="<?php echo $thumbnail_id; ?>" /> 
  180. <button type="button" class="upload_image_button button"><?php _e( 'Upload/Add image', 'woocommerce' ); ?></button> 
  181. <button type="button" class="remove_image_button button"><?php _e( 'Remove image', 'woocommerce' ); ?></button> 
  182. </div> 
  183. <script type="text/javascript"> 
  184.  
  185. // Only show the "remove image" button when needed 
  186. if ( '0' === jQuery( '#product_cat_thumbnail_id' ).val() ) { 
  187. jQuery( '.remove_image_button' ).hide(); 
  188.  
  189. // Uploading files 
  190. var file_frame; 
  191.  
  192. jQuery( document ).on( 'click', '.upload_image_button', function( event ) { 
  193.  
  194. event.preventDefault(); 
  195.  
  196. // If the media frame already exists, reopen it. 
  197. if ( file_frame ) { 
  198. file_frame.open(); 
  199. return; 
  200.  
  201. // Create the media frame. 
  202. file_frame = wp.media.frames.downloadable_file = wp.media({ 
  203. title: '<?php _e( "Choose an image", "woocommerce" ); ?>',  
  204. button: { 
  205. text: '<?php _e( "Use image", "woocommerce" ); ?>' 
  206. },  
  207. multiple: false 
  208. }); 
  209.  
  210. // When an image is selected, run a callback. 
  211. file_frame.on( 'select', function() { 
  212. var attachment = file_frame.state().get( 'selection' ).first().toJSON(); 
  213.  
  214. jQuery( '#product_cat_thumbnail_id' ).val( attachment.id ); 
  215. jQuery( '#product_cat_thumbnail' ).find( 'img' ).attr( 'src', attachment.sizes.thumbnail.url ); 
  216. jQuery( '.remove_image_button' ).show(); 
  217. }); 
  218.  
  219. // Finally, open the modal. 
  220. file_frame.open(); 
  221. }); 
  222.  
  223. jQuery( document ).on( 'click', '.remove_image_button', function() { 
  224. jQuery( '#product_cat_thumbnail' ).find( 'img' ).attr( 'src', '<?php echo esc_js( wc_placeholder_img_src() ); ?>' ); 
  225. jQuery( '#product_cat_thumbnail_id' ).val( '' ); 
  226. jQuery( '.remove_image_button' ).hide(); 
  227. return false; 
  228. }); 
  229.  
  230. </script> 
  231. <div class="clear"></div> 
  232. </td> 
  233. </tr> 
  234. <?php 
  235.  
  236. /** 
  237. * save_category_fields function. 
  238. * @param mixed $term_id Term ID being saved 
  239. * @param mixed $tt_id 
  240. * @param string $taxonomy 
  241. */ 
  242. public function save_category_fields( $term_id, $tt_id = '', $taxonomy = '' ) { 
  243. if ( isset( $_POST['display_type'] ) && 'product_cat' === $taxonomy ) { 
  244. update_woocommerce_term_meta( $term_id, 'display_type', esc_attr( $_POST['display_type'] ) ); 
  245. if ( isset( $_POST['product_cat_thumbnail_id'] ) && 'product_cat' === $taxonomy ) { 
  246. update_woocommerce_term_meta( $term_id, 'thumbnail_id', absint( $_POST['product_cat_thumbnail_id'] ) ); 
  247.  
  248. /** 
  249. * Description for product_cat page to aid users. 
  250. */ 
  251. public function product_cat_description() { 
  252. echo wpautop( __( 'Product categories for your store can be managed here. To change the order of categories on the front-end you can drag and drop to sort them. To see more categories listed click the "screen options" link at the top of the page.', 'woocommerce' ) ); 
  253.  
  254. /** 
  255. * Description for shipping class page to aid users. 
  256. */ 
  257. public function product_attribute_description() { 
  258. echo wpautop( __( 'Attribute terms can be assigned to products and variations.<br/><br/><b>Note</b>: Deleting a term will remove it from all products and variations to which it has been assigned. Recreating a term will not automatically assign it back to products.', 'woocommerce' ) ); 
  259.  
  260. /** 
  261. * Thumbnail column added to category admin. 
  262. * @param mixed $columns 
  263. * @return array 
  264. */ 
  265. public function product_cat_columns( $columns ) { 
  266. $new_columns = array(); 
  267.  
  268. if ( isset( $columns['cb'] ) ) { 
  269. $new_columns['cb'] = $columns['cb']; 
  270. unset( $columns['cb'] ); 
  271.  
  272. $new_columns['thumb'] = __( 'Image', 'woocommerce' ); 
  273.  
  274. return array_merge( $new_columns, $columns ); 
  275.  
  276. /** 
  277. * Thumbnail column value added to category admin. 
  278. * @param string $columns 
  279. * @param string $column 
  280. * @param int $id 
  281. * @return array 
  282. */ 
  283. public function product_cat_column( $columns, $column, $id ) { 
  284.  
  285. if ( 'thumb' == $column ) { 
  286.  
  287. $thumbnail_id = get_woocommerce_term_meta( $id, 'thumbnail_id', true ); 
  288.  
  289. if ( $thumbnail_id ) { 
  290. $image = wp_get_attachment_thumb_url( $thumbnail_id ); 
  291. } else { 
  292. $image = wc_placeholder_img_src(); 
  293.  
  294. // Prevent esc_url from breaking spaces in urls for image embeds 
  295. // Ref: https://core.trac.wordpress.org/ticket/23605 
  296. $image = str_replace( ' ', '%20', $image ); 
  297.  
  298. $columns .= '<img src="' . esc_url( $image ) . '" alt="' . esc_attr__( 'Thumbnail', 'woocommerce' ) . '" class="wp-post-image" height="48" width="48" />'; 
  299.  
  300.  
  301. return $columns; 
  302.  
  303. /** 
  304. * Maintain term hierarchy when editing a product. 
  305. * @param array $args 
  306. * @return array 
  307. */ 
  308. public function disable_checked_ontop( $args ) { 
  309. if ( ! empty( $args['taxonomy'] ) && 'product_cat' === $args['taxonomy'] ) { 
  310. $args['checked_ontop'] = false; 
  311. return $args;