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. var attachment_thumbnail = attachment.sizes.thumbnail || attachment.sizes.full; 
  112.  
  113. jQuery( '#product_cat_thumbnail_id' ).val( attachment.id ); 
  114. jQuery( '#product_cat_thumbnail' ).find( 'img' ).attr( 'src', attachment_thumbnail.url ); 
  115. jQuery( '.remove_image_button' ).show(); 
  116. }); 
  117.  
  118. // Finally, open the modal. 
  119. file_frame.open(); 
  120. }); 
  121.  
  122. jQuery( document ).on( 'click', '.remove_image_button', function() { 
  123. jQuery( '#product_cat_thumbnail' ).find( 'img' ).attr( 'src', '<?php echo esc_js( wc_placeholder_img_src() ); ?>' ); 
  124. jQuery( '#product_cat_thumbnail_id' ).val( '' ); 
  125. jQuery( '.remove_image_button' ).hide(); 
  126. return false; 
  127. }); 
  128.  
  129. jQuery( document ).ajaxComplete( function( event, request, options ) { 
  130. if ( request && 4 === request.readyState && 200 === request.status 
  131. && options.data && 0 <= options.data.indexOf( 'action=add-tag' ) ) { 
  132.  
  133. var res = wpAjax.parseAjaxResponse( request.responseXML, 'ajax-response' ); 
  134. if ( ! res || res.errors ) { 
  135. return; 
  136. // Clear Thumbnail fields on submit 
  137. jQuery( '#product_cat_thumbnail' ).find( 'img' ).attr( 'src', '<?php echo esc_js( wc_placeholder_img_src() ); ?>' ); 
  138. jQuery( '#product_cat_thumbnail_id' ).val( '' ); 
  139. jQuery( '.remove_image_button' ).hide(); 
  140. // Clear Display type field on submit 
  141. jQuery( '#display_type' ).val( '' ); 
  142. return; 
  143. } ); 
  144.  
  145. </script> 
  146. <div class="clear"></div> 
  147. </div> 
  148. <?php 
  149.  
  150. /** 
  151. * Edit category thumbnail field. 
  152. * @param mixed $term Term (category) being edited 
  153. */ 
  154. public function edit_category_fields( $term ) { 
  155.  
  156. $display_type = get_woocommerce_term_meta( $term->term_id, 'display_type', true ); 
  157. $thumbnail_id = absint( get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true ) ); 
  158.  
  159. if ( $thumbnail_id ) { 
  160. $image = wp_get_attachment_thumb_url( $thumbnail_id ); 
  161. } else { 
  162. $image = wc_placeholder_img_src(); 
  163. ?> 
  164. <tr class="form-field"> 
  165. <th scope="row" valign="top"><label><?php _e( 'Display type', 'woocommerce' ); ?></label></th> 
  166. <td> 
  167. <select id="display_type" name="display_type" class="postform"> 
  168. <option value="" <?php selected( '', $display_type ); ?>><?php _e( 'Default', 'woocommerce' ); ?></option> 
  169. <option value="products" <?php selected( 'products', $display_type ); ?>><?php _e( 'Products', 'woocommerce' ); ?></option> 
  170. <option value="subcategories" <?php selected( 'subcategories', $display_type ); ?>><?php _e( 'Subcategories', 'woocommerce' ); ?></option> 
  171. <option value="both" <?php selected( 'both', $display_type ); ?>><?php _e( 'Both', 'woocommerce' ); ?></option> 
  172. </select> 
  173. </td> 
  174. </tr> 
  175. <tr class="form-field"> 
  176. <th scope="row" valign="top"><label><?php _e( 'Thumbnail', 'woocommerce' ); ?></label></th> 
  177. <td> 
  178. <div id="product_cat_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( $image ); ?>" width="60px" height="60px" /></div> 
  179. <div style="line-height: 60px;"> 
  180. <input type="hidden" id="product_cat_thumbnail_id" name="product_cat_thumbnail_id" value="<?php echo $thumbnail_id; ?>" /> 
  181. <button type="button" class="upload_image_button button"><?php _e( 'Upload/Add image', 'woocommerce' ); ?></button> 
  182. <button type="button" class="remove_image_button button"><?php _e( 'Remove image', 'woocommerce' ); ?></button> 
  183. </div> 
  184. <script type="text/javascript"> 
  185.  
  186. // Only show the "remove image" button when needed 
  187. if ( '0' === jQuery( '#product_cat_thumbnail_id' ).val() ) { 
  188. jQuery( '.remove_image_button' ).hide(); 
  189.  
  190. // Uploading files 
  191. var file_frame; 
  192.  
  193. jQuery( document ).on( 'click', '.upload_image_button', function( event ) { 
  194.  
  195. event.preventDefault(); 
  196.  
  197. // If the media frame already exists, reopen it. 
  198. if ( file_frame ) { 
  199. file_frame.open(); 
  200. return; 
  201.  
  202. // Create the media frame. 
  203. file_frame = wp.media.frames.downloadable_file = wp.media({ 
  204. title: '<?php _e( "Choose an image", "woocommerce" ); ?>',  
  205. button: { 
  206. text: '<?php _e( "Use image", "woocommerce" ); ?>' 
  207. },  
  208. multiple: false 
  209. }); 
  210.  
  211. // When an image is selected, run a callback. 
  212. file_frame.on( 'select', function() { 
  213. var attachment = file_frame.state().get( 'selection' ).first().toJSON(); 
  214. var attachment_thumbnail = attachment.sizes.thumbnail || attachment.sizes.full; 
  215.  
  216. jQuery( '#product_cat_thumbnail_id' ).val( attachment.id ); 
  217. jQuery( '#product_cat_thumbnail' ).find( 'img' ).attr( 'src', attachment_thumbnail.url ); 
  218. jQuery( '.remove_image_button' ).show(); 
  219. }); 
  220.  
  221. // Finally, open the modal. 
  222. file_frame.open(); 
  223. }); 
  224.  
  225. jQuery( document ).on( 'click', '.remove_image_button', function() { 
  226. jQuery( '#product_cat_thumbnail' ).find( 'img' ).attr( 'src', '<?php echo esc_js( wc_placeholder_img_src() ); ?>' ); 
  227. jQuery( '#product_cat_thumbnail_id' ).val( '' ); 
  228. jQuery( '.remove_image_button' ).hide(); 
  229. return false; 
  230. }); 
  231.  
  232. </script> 
  233. <div class="clear"></div> 
  234. </td> 
  235. </tr> 
  236. <?php 
  237.  
  238. /** 
  239. * save_category_fields function. 
  240. * @param mixed $term_id Term ID being saved 
  241. * @param mixed $tt_id 
  242. * @param string $taxonomy 
  243. */ 
  244. public function save_category_fields( $term_id, $tt_id = '', $taxonomy = '' ) { 
  245. if ( isset( $_POST['display_type'] ) && 'product_cat' === $taxonomy ) { 
  246. update_woocommerce_term_meta( $term_id, 'display_type', esc_attr( $_POST['display_type'] ) ); 
  247. if ( isset( $_POST['product_cat_thumbnail_id'] ) && 'product_cat' === $taxonomy ) { 
  248. update_woocommerce_term_meta( $term_id, 'thumbnail_id', absint( $_POST['product_cat_thumbnail_id'] ) ); 
  249.  
  250. /** 
  251. * Description for product_cat page to aid users. 
  252. */ 
  253. public function product_cat_description() { 
  254. 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-right of this page.', 'woocommerce' ) ); 
  255.  
  256. /** 
  257. * Description for shipping class page to aid users. 
  258. */ 
  259. public function product_attribute_description() { 
  260. 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' ) ); 
  261.  
  262. /** 
  263. * Thumbnail column added to category admin. 
  264. * @param mixed $columns 
  265. * @return array 
  266. */ 
  267. public function product_cat_columns( $columns ) { 
  268. $new_columns = array(); 
  269.  
  270. if ( isset( $columns['cb'] ) ) { 
  271. $new_columns['cb'] = $columns['cb']; 
  272. unset( $columns['cb'] ); 
  273.  
  274. $new_columns['thumb'] = __( 'Image', 'woocommerce' ); 
  275.  
  276. return array_merge( $new_columns, $columns ); 
  277.  
  278. /** 
  279. * Thumbnail column value added to category admin. 
  280. * @param string $columns 
  281. * @param string $column 
  282. * @param int $id 
  283. * @return array 
  284. */ 
  285. public function product_cat_column( $columns, $column, $id ) { 
  286.  
  287. if ( 'thumb' == $column ) { 
  288.  
  289. $thumbnail_id = get_woocommerce_term_meta( $id, 'thumbnail_id', true ); 
  290.  
  291. if ( $thumbnail_id ) { 
  292. $image = wp_get_attachment_thumb_url( $thumbnail_id ); 
  293. } else { 
  294. $image = wc_placeholder_img_src(); 
  295.  
  296. // Prevent esc_url from breaking spaces in urls for image embeds 
  297. // Ref: https://core.trac.wordpress.org/ticket/23605 
  298. $image = str_replace( ' ', '%20', $image ); 
  299.  
  300. $columns .= '<img src="' . esc_url( $image ) . '" alt="' . esc_attr__( 'Thumbnail', 'woocommerce' ) . '" class="wp-post-image" height="48" width="48" />'; 
  301.  
  302.  
  303. return $columns; 
  304.  
  305. /** 
  306. * Maintain term hierarchy when editing a product. 
  307. * @param array $args 
  308. * @return array 
  309. */ 
  310. public function disable_checked_ontop( $args ) { 
  311. if ( ! empty( $args['taxonomy'] ) && 'product_cat' === $args['taxonomy'] ) { 
  312. $args['checked_ontop'] = false; 
  313. return $args;