WC_Admin_Permalink_Settings

WC_Admin_Permalink_Settings Class.

Defined (1)

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

/includes/admin/class-wc-admin-permalink-settings.php  
  1. class WC_Admin_Permalink_Settings { 
  2.  
  3. /** 
  4. * Hook in tabs. 
  5. */ 
  6. public function __construct() { 
  7. $this->settings_init(); 
  8. $this->settings_save(); 
  9.  
  10. /** 
  11. * Init our settings. 
  12. */ 
  13. public function settings_init() { 
  14. // Add a section to the permalinks page 
  15. add_settings_section( 'woocommerce-permalink', __( 'Product Permalinks', 'woocommerce' ), array( $this, 'settings' ), 'permalink' ); 
  16.  
  17. // Add our settings 
  18. add_settings_field( 
  19. 'woocommerce_product_category_slug', // id 
  20. __( 'Product category base', 'woocommerce' ), // setting title 
  21. array( $this, 'product_category_slug_input' ), // display callback 
  22. 'permalink', // settings page 
  23. 'optional' // settings section 
  24. ); 
  25. add_settings_field( 
  26. 'woocommerce_product_tag_slug', // id 
  27. __( 'Product tag base', 'woocommerce' ), // setting title 
  28. array( $this, 'product_tag_slug_input' ), // display callback 
  29. 'permalink', // settings page 
  30. 'optional' // settings section 
  31. ); 
  32. add_settings_field( 
  33. 'woocommerce_product_attribute_slug', // id 
  34. __( 'Product attribute base', 'woocommerce' ), // setting title 
  35. array( $this, 'product_attribute_slug_input' ), // display callback 
  36. 'permalink', // settings page 
  37. 'optional' // settings section 
  38. ); 
  39.  
  40. /** 
  41. * Show a slug input box. 
  42. */ 
  43. public function product_category_slug_input() { 
  44. $permalinks = get_option( 'woocommerce_permalinks' ); 
  45. ?> 
  46. <input name="woocommerce_product_category_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['category_base'] ) ) echo esc_attr( $permalinks['category_base'] ); ?>" placeholder="<?php echo esc_attr_x('product-category', 'slug', 'woocommerce') ?>" /> 
  47. <?php 
  48.  
  49. /** 
  50. * Show a slug input box. 
  51. */ 
  52. public function product_tag_slug_input() { 
  53. $permalinks = get_option( 'woocommerce_permalinks' ); 
  54. ?> 
  55. <input name="woocommerce_product_tag_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['tag_base'] ) ) echo esc_attr( $permalinks['tag_base'] ); ?>" placeholder="<?php echo esc_attr_x('product-tag', 'slug', 'woocommerce') ?>" /> 
  56. <?php 
  57.  
  58. /** 
  59. * Show a slug input box. 
  60. */ 
  61. public function product_attribute_slug_input() { 
  62. $permalinks = get_option( 'woocommerce_permalinks' ); 
  63. ?> 
  64. <input name="woocommerce_product_attribute_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['attribute_base'] ) ) echo esc_attr( $permalinks['attribute_base'] ); ?>" /><code>/attribute-name/attribute/</code> 
  65. <?php 
  66.  
  67. /** 
  68. * Show the settings. 
  69. */ 
  70. public function settings() { 
  71. echo wpautop( __( 'These settings control the permalinks used specifically for products.', 'woocommerce' ) ); 
  72.  
  73. $permalinks = get_option( 'woocommerce_permalinks' ); 
  74. $product_permalink = isset( $permalinks['product_base'] ) ? $permalinks['product_base'] : ''; 
  75.  
  76. // Get shop page 
  77. $shop_page_id = wc_get_page_id( 'shop' ); 
  78. $base_slug = urldecode( ( $shop_page_id > 0 && get_post( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : _x( 'shop', 'default-slug', 'woocommerce' ) ); 
  79. $product_base = _x( 'product', 'default-slug', 'woocommerce' ); 
  80.  
  81. $structures = array( 
  82. 0 => '',  
  83. 1 => '/' . trailingslashit( $base_slug ),  
  84. 2 => '/' . trailingslashit( $base_slug ) . trailingslashit( '%product_cat%' ) 
  85. ); 
  86. ?> 
  87. <table class="form-table wc-permalink-structure"> 
  88. <tbody> 
  89. <tr> 
  90. <th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[0] ); ?>" class="wctog" <?php checked( $structures[0], $product_permalink ); ?> /> <?php _e( 'Default', 'woocommerce' ); ?></label></th> 
  91. <td><code class="default-example"><?php echo esc_html( home_url() ); ?>/?product=sample-product</code> <code class="non-default-example"><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $product_base ); ?>/sample-product/</code></td> 
  92. </tr> 
  93. <?php if ( $shop_page_id ) : ?> 
  94. <tr> 
  95. <th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[1] ); ?>" class="wctog" <?php checked( $structures[1], $product_permalink ); ?> /> <?php _e( 'Shop base', 'woocommerce' ); ?></label></th> 
  96. <td><code><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $base_slug ); ?>/sample-product/</code></td> 
  97. </tr> 
  98. <tr> 
  99. <th><label><input name="product_permalink" type="radio" value="<?php echo esc_attr( $structures[2] ); ?>" class="wctog" <?php checked( $structures[2], $product_permalink ); ?> /> <?php _e( 'Shop base with category', 'woocommerce' ); ?></label></th> 
  100. <td><code><?php echo esc_html( home_url() ); ?>/<?php echo esc_html( $base_slug ); ?>/product-category/sample-product/</code></td> 
  101. </tr> 
  102. <?php endif; ?> 
  103. <tr> 
  104. <th><label><input name="product_permalink" id="woocommerce_custom_selection" type="radio" value="custom" class="tog" <?php checked( in_array( $product_permalink, $structures ), false ); ?> /> 
  105. <?php _e( 'Custom Base', 'woocommerce' ); ?></label></th> 
  106. <td> 
  107. <input name="product_permalink_structure" id="woocommerce_permalink_structure" type="text" value="<?php echo esc_attr( $product_permalink ); ?>" class="regular-text code"> <span class="description"><?php _e( 'Enter a custom base to use. A base <strong>must</strong> be set or WordPress will use default instead.', 'woocommerce' ); ?></span> 
  108. </td> 
  109. </tr> 
  110. </tbody> 
  111. </table> 
  112. <script type="text/javascript"> 
  113. jQuery( function() { 
  114. jQuery('input.wctog').change(function() { 
  115. jQuery('#woocommerce_permalink_structure').val( jQuery( this ).val() ); 
  116. }); 
  117. jQuery('.permalink-structure input').change(function() { 
  118. jQuery('.wc-permalink-structure').find('code.non-default-example, code.default-example').hide(); 
  119. if ( jQuery(this).val() ) { 
  120. jQuery('.wc-permalink-structure code.non-default-example').show(); 
  121. jQuery('.wc-permalink-structure input').removeAttr('disabled'); 
  122. } else { 
  123. jQuery('.wc-permalink-structure code.default-example').show(); 
  124. jQuery('.wc-permalink-structure input:eq(0)').click(); 
  125. jQuery('.wc-permalink-structure input').attr('disabled', 'disabled'); 
  126. }); 
  127. jQuery('.permalink-structure input:checked').change(); 
  128. jQuery('#woocommerce_permalink_structure').focus( function() { 
  129. jQuery('#woocommerce_custom_selection').click(); 
  130. } ); 
  131. } ); 
  132. </script> 
  133. <?php 
  134.  
  135. /** 
  136. * Save the settings. 
  137. */ 
  138. public function settings_save() { 
  139. if ( ! is_admin() ) { 
  140. return; 
  141.  
  142. // We need to save the options ourselves; settings api does not trigger save for the permalinks page. 
  143. if ( isset( $_POST['permalink_structure'] ) ) { 
  144. $permalinks = get_option( 'woocommerce_permalinks' ); 
  145.  
  146. if ( ! $permalinks ) { 
  147. $permalinks = array(); 
  148.  
  149. $permalinks['category_base'] = wc_sanitize_permalink( trim( $_POST['woocommerce_product_category_slug'] ) ); 
  150. $permalinks['tag_base'] = wc_sanitize_permalink( trim( $_POST['woocommerce_product_tag_slug'] ) ); 
  151. $permalinks['attribute_base'] = wc_sanitize_permalink( trim( $_POST['woocommerce_product_attribute_slug'] ) ); 
  152.  
  153. // Product base. 
  154. $product_permalink = isset( $_POST['product_permalink'] ) ? wc_clean( $_POST['product_permalink'] ) : ''; 
  155.  
  156. if ( 'custom' === $product_permalink ) { 
  157. if ( isset( $_POST['product_permalink_structure'] ) ) { 
  158. $product_permalink = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', trim( $_POST['product_permalink_structure'] ) ) ); 
  159. } else { 
  160. $product_permalink = '/'; 
  161.  
  162. // This is an invalid base structure and breaks pages. 
  163. if ( '%product_cat%' == $product_permalink ) { 
  164. $product_permalink = '/' . _x( 'product', 'slug', 'woocommerce' ) . '/' . $product_permalink; 
  165. } elseif ( empty( $product_permalink ) ) { 
  166. $product_permalink = false; 
  167.  
  168. $permalinks['product_base'] = wc_sanitize_permalink( $product_permalink ); 
  169.  
  170. // Shop base may require verbose page rules if nesting pages. 
  171. $shop_page_id = wc_get_page_id( 'shop' ); 
  172. $shop_permalink = ( $shop_page_id > 0 && get_post( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : _x( 'shop', 'default-slug', 'woocommerce' ); 
  173.  
  174. if ( $shop_page_id && trim( $permalinks['product_base'], '/' ) === $shop_permalink ) { 
  175. $permalinks['use_verbose_page_rules'] = true; 
  176.  
  177. update_option( 'woocommerce_permalinks', $permalinks );